]> git.sesse.net Git - kdenlive/blobdiff - src/archivewidget.cpp
Merge branch 'refs/heads/v0.8.2'
[kdenlive] / src / archivewidget.cpp
index 2f2e373a28f2a309ccd056d6bb5f303873f05304..db2b9e8db2c6d033c1db1f17a42db70beb343ad0 100644 (file)
@@ -31,6 +31,7 @@
 #include <KTar>
 #include <KDebug>
 #include <KApplication>
+#include <kio/directorysizejob.h>
 
 #include <QTreeWidget>
 #include <QtConcurrentRun>
@@ -44,7 +45,8 @@ ArchiveWidget::ArchiveWidget(QString projectName, QDomDocument doc, QList <DocCl
         m_name(projectName.section('.', 0, -2)),
         m_doc(doc),
         m_abortArchive(false),
-        m_extractMode(false)
+        m_extractMode(false),
+        m_extractArchive(NULL)
 {
     setAttribute(Qt::WA_DeleteOnClose);
     setupUi(this);
@@ -138,9 +140,7 @@ ArchiveWidget::ArchiveWidget(QString projectName, QDomDocument doc, QList <DocCl
     generateItems(others, otherUrls);
     generateItems(proxies, proxyUrls);
     
-#if QT_VERSION >= 0x040500
     allFonts.removeDuplicates();
-#endif
 
     //TODO: fonts
 
@@ -164,7 +164,7 @@ ArchiveWidget::ArchiveWidget(QString projectName, QDomDocument doc, QList <DocCl
             parentItem->setText(0, files_list->topLevelItem(i)->text(0) + " " + i18np("(%1 item)", "(%1 items)", items));
         }
     }
-
+    if (m_name.isEmpty()) m_name = i18n("Untitled");
     compressed_archive->setText(compressed_archive->text() + " (" + m_name + ".tar.gz)");
     project_files->setText(i18np("%1 file to archive, requires %2", "%1 files to archive, requires %2", total, KIO::convertSize(m_requestedSize)));
     buttonBox->button(QDialogButtonBox::Apply)->setText(i18n("Archive"));
@@ -181,30 +181,10 @@ ArchiveWidget::ArchiveWidget(const KUrl &url, QWidget * parent):
     m_extractUrl(url)
 {
     //setAttribute(Qt::WA_DeleteOnClose);
-    KTar archive(url.path());
-    archive.open( QIODevice::ReadOnly );
-
-    // Check that it is a kdenlive project archive
-    bool isProjectArchive = false;
-    QStringList files = archive.directory()->entries();
-    for (int i = 0; i < files.count(); i++) {
-        if (files.at(i).endsWith(".kdenlive")) {
-            m_projectName = files.at(i);
-            isProjectArchive = true;
-            break;
-        }
-    }
-    archive.close();
 
-    if (!isProjectArchive) {
-        KMessageBox::sorry(kapp->activeWindow(), i18n("%1 is not an archived Kdenlive project", url.path(), i18n("Cannot open file")));
-        hide();
-        //HACK: find a better way to terminate the dialog
-        QTimer::singleShot(50, this, SLOT(reject()));
-        return;
-    }
     setupUi(this);
     connect(this, SIGNAL(extractingFinished()), this, SLOT(slotExtractingFinished()));
+    connect(this, SIGNAL(showMessage(const QString &, const QString &)), this, SLOT(slotDisplayMessage(const QString &, const QString &)));
     
     compressed_archive->setHidden(true);
     project_files->setHidden(true);
@@ -214,11 +194,51 @@ ArchiveWidget::ArchiveWidget(const KUrl &url, QWidget * parent):
     archive_url->setUrl(KUrl(QDir::homePath()));
     buttonBox->button(QDialogButtonBox::Apply)->setText(i18n("Extract"));
     connect(buttonBox->button(QDialogButtonBox::Apply), SIGNAL(clicked()), this, SLOT(slotStartExtracting()));
+    buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false);
+    adjustSize();
+    m_archiveThread = QtConcurrent::run(this, &ArchiveWidget::openArchiveForExtraction);
 }
 
 
 ArchiveWidget::~ArchiveWidget()
 {
+    if (m_extractArchive) delete m_extractArchive;
+}
+
+void ArchiveWidget::slotDisplayMessage(const QString &icon, const QString &text)
+{
+    icon_info->setPixmap(KIcon(icon).pixmap(16, 16));
+    text_info->setText(text);
+}
+
+void ArchiveWidget::openArchiveForExtraction()
+{
+    emit showMessage("system-run", i18n("Opening archive..."));
+    m_extractArchive = new KTar(m_extractUrl.path());
+    if (!m_extractArchive->isOpen() && !m_extractArchive->open( QIODevice::ReadOnly )) {
+        emit showMessage("dialog-close", i18n("Cannot open archive file:\n %1", m_extractUrl.path()));
+        groupBox->setEnabled(false);
+        return;
+    }
+
+    // Check that it is a kdenlive project archive
+    bool isProjectArchive = false;
+    QStringList files = m_extractArchive->directory()->entries();
+    for (int i = 0; i < files.count(); i++) {
+        if (files.at(i).endsWith(".kdenlive")) {
+            m_projectName = files.at(i);
+            isProjectArchive = true;
+            break;
+        }
+    }
+
+    if (!isProjectArchive) {
+        emit showMessage("dialog-close", i18n("File %1\n is not an archived Kdenlive project", m_extractUrl.path()));
+        groupBox->setEnabled(false);
+        return;
+    }
+    buttonBox->button(QDialogButtonBox::Apply)->setEnabled(true);
+    emit showMessage("dialog-ok", i18n("Ready"));
 }
 
 void ArchiveWidget::done ( int r )
@@ -322,13 +342,11 @@ void ArchiveWidget::slotCheckSpace()
     if (freeSize > m_requestedSize) {
         // everything is ok
         buttonBox->button(QDialogButtonBox::Apply)->setEnabled(true);
-        icon_info->setPixmap(KIcon("dialog-ok").pixmap(16, 16));
-        text_info->setText(i18n("Available space on drive: %1", KIO::convertSize(freeSize)));
+        slotDisplayMessage("dialog-ok", i18n("Available space on drive: %1", KIO::convertSize(freeSize)));
     }
     else {
         buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false);
-        icon_info->setPixmap(KIcon("dialog-close").pixmap(16, 16));
-        text_info->setText(i18n("Not enough space on drive, free space: %1", KIO::convertSize(freeSize)));
+        slotDisplayMessage("dialog-close", i18n("Not enough space on drive, free space: %1", KIO::convertSize(freeSize)));
     }
 }
 
@@ -349,8 +367,7 @@ bool ArchiveWidget::slotStartArchiving(bool firstPass)
         m_replacementList.clear();
         m_foldersList.clear();
         m_filesList.clear();
-        icon_info->setPixmap(KIcon("system-run").pixmap(16, 16));
-        text_info->setText(i18n("Archiving..."));
+        slotDisplayMessage("system-run", i18n("Archiving..."));
         repaint();
         archive_url->setEnabled(false);
         compressed_archive->setEnabled(false);
@@ -459,19 +476,16 @@ void ArchiveWidget::slotArchivingFinished(KJob *job)
             // Archiving finished
             progressBar->setValue(100);
             if (processProjectFile()) {
-                icon_info->setPixmap(KIcon("dialog-ok").pixmap(16, 16));
-                text_info->setText(i18n("Project was successfully archived."));
+                slotDisplayMessage("dialog-ok", i18n("Project was successfully archived."));
             }
             else {
-                icon_info->setPixmap(KIcon("dialog-close").pixmap(16, 16));
-                text_info->setText(i18n("There was an error processing project file"));
+                slotDisplayMessage("dialog-close", i18n("There was an error processing project file"));
             }
         } else processProjectFile();
     }
     else {
         m_copyJob = NULL;
-        icon_info->setPixmap(KIcon("dialog-close").pixmap(16, 16));
-        text_info->setText(i18n("There was an error while copying the files: %1", job->errorString()));
+        slotDisplayMessage("dialog-close", i18n("There was an error while copying the files: %1", job->errorString()));
     }
     if (!compressed_archive->isChecked()) {
         buttonBox->button(QDialogButtonBox::Apply)->setText(i18n("Archive"));
@@ -528,7 +542,7 @@ bool ArchiveWidget::processProjectFile()
     if (isArchive) basePath = "$CURRENTPATH";
     else basePath = archive_url->url().path(KUrl::RemoveTrailingSlash);
     mlt.setAttribute("root", basePath);
-    QDomElement project = mlt.elementsByTagName("kdenlivedoc").at(0).toElement();
+    QDomElement project = mlt.firstChildElement("kdenlivedoc");
     project.setAttribute("projectfolder", basePath);
 
     // process kdenlive producers
@@ -652,12 +666,10 @@ void ArchiveWidget::createArchive()
 void ArchiveWidget::slotArchivingFinished(bool result)
 {
     if (result) {
-        icon_info->setPixmap(KIcon("dialog-ok").pixmap(16, 16));
-        text_info->setText(i18n("Project was successfully archived."));
+        slotDisplayMessage("dialog-ok", i18n("Project was successfully archived."));
     }
     else {
-        icon_info->setPixmap(KIcon("dialog-close").pixmap(16, 16));
-        text_info->setText(i18n("There was an error processing project file"));
+        slotDisplayMessage("dialog-close", i18n("There was an error processing project file"));
     }
     progressBar->setValue(100);
     buttonBox->button(QDialogButtonBox::Apply)->setText(i18n("Archive"));
@@ -677,16 +689,42 @@ void ArchiveWidget::slotArchivingProgress(int p)
 
 void ArchiveWidget::slotStartExtracting()
 {
+    if (m_archiveThread.isRunning()) {
+        //TODO: abort extracting
+        return;
+    }
+    QFileInfo f(m_extractUrl.path());
+    m_requestedSize = f.size();
     KIO::NetAccess::mkdir(archive_url->url().path(KUrl::RemoveTrailingSlash), this);
+    slotDisplayMessage("system-run", i18n("Extracting..."));
+    buttonBox->button(QDialogButtonBox::Apply)->setText(i18n("Abort"));
+    m_progressTimer = new QTimer;
+    m_progressTimer->setInterval(800);
+    m_progressTimer->setSingleShot(false);
+    connect(m_progressTimer, SIGNAL(timeout()), this, SLOT(slotExtractProgress()));
     m_archiveThread = QtConcurrent::run(this, &ArchiveWidget::doExtracting);
+    m_progressTimer->start();
+}
+
+void ArchiveWidget::slotExtractProgress()
+{
+    KIO::DirectorySizeJob *job = KIO::directorySize(archive_url->url());
+    connect(job, SIGNAL(result(KJob*)), this, SLOT(slotGotProgress(KJob*)));
+}
+
+void ArchiveWidget::slotGotProgress(KJob* job)
+{
+    if (!job->error()) {
+        KIO::DirectorySizeJob *j = static_cast <KIO::DirectorySizeJob *>(job);
+        progressBar->setValue((int) 100 * j->totalSize() / m_requestedSize);
+    }
+    job->deleteLater();
 }
 
 void ArchiveWidget::doExtracting()
 {
-    KTar archive(m_extractUrl.path());
-    archive.open( QIODevice::ReadOnly );
-    archive.directory()->copyTo(archive_url->url().path(KUrl::AddTrailingSlash));
-    archive.close();
+    m_extractArchive->directory()->copyTo(archive_url->url().path(KUrl::AddTrailingSlash));
+    m_extractArchive->close();
     emit extractingFinished();    
 }
 
@@ -697,6 +735,8 @@ QString ArchiveWidget::extractedProjectFile()
 
 void ArchiveWidget::slotExtractingFinished()
 {
+    m_progressTimer->stop();
+    delete m_progressTimer;
     // Process project file
     QFile file(extractedProjectFile());
     bool error = false;