]> git.sesse.net Git - kdenlive/blobdiff - src/documentchecker.cpp
Improve handling of missing clips:
[kdenlive] / src / documentchecker.cpp
index 724cdf9878bbecabb2d0362353ebe3af10d33958..3444503f3137cdf6b5be39d2818e5f6ea223b81a 100644 (file)
@@ -29,6 +29,7 @@
 #include <KFileDialog>
 #include <KApplication>
 #include <KUrlRequesterDialog>
+#include <KMessageBox>
 
 #include <QTreeWidgetItem>
 #include <QFile>
@@ -43,35 +44,18 @@ const int sizeRole = Qt::UserRole + 1;
 const int idRole = Qt::UserRole + 2;
 const int statusRole = Qt::UserRole + 3;
 
-DocumentChecker::DocumentChecker(QDomDocument doc, QWidget * parent) :
+const int CLIPMISSING = 0;
+const int CLIPOK = 1;
+const int CLIPPLACEHOLDER = 2;
+
+DocumentChecker::DocumentChecker(QDomNodeList producers, QDomNodeList infoproducers, QList <QDomElement> missingClips, QDomDocument doc, QWidget * parent) :
         QDialog(parent), m_doc(doc)
 {
     setFont(KGlobalSettings::toolBarFont());
     m_view.setupUi(this);
-
-    QDomNodeList producers = m_doc.elementsByTagName("producer");
-    QDomNodeList infoproducers = m_doc.elementsByTagName("kdenlive_producer");
-
-    int clipType;
     QDomElement e;
-    QString id;
-    QString resource;
-    QList <QDomElement> missingClips;
-    for (int i = 0; i < infoproducers.count(); i++) {
-        e = infoproducers.item(i).toElement();
-        clipType = e.attribute("type").toInt();
-        if (clipType == TEXT) continue;
-        id = e.attribute("id");
-        resource = e.attribute("resource");
-        if (clipType == SLIDESHOW) resource = KUrl(resource).directory();
-        if (!KIO::NetAccess::exists(KUrl(resource), KIO::NetAccess::SourceSide, 0)) {
-            // Missing clip found
-            missingClips.append(e);
-        }
-    }
-
-    if (missingClips.isEmpty()) QTimer::singleShot(0, this, SLOT(reject()));
 
+    m_view.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
     for (int i = 0; i < missingClips.count(); i++) {
         e = missingClips.at(i).toElement();
         QString clipType;
@@ -102,9 +86,11 @@ DocumentChecker::DocumentChecker(QDomDocument doc, QWidget * parent) :
         item->setData(0, hashRole, e.attribute("file_hash"));
         item->setData(0, sizeRole, e.attribute("file_size"));
         item->setData(0, idRole, e.attribute("id"));
-        item->setData(0, statusRole, '1');
+        item->setData(0, statusRole, CLIPMISSING);
     }
     connect(m_view.recursiveSearch, SIGNAL(pressed()), this, SLOT(slotSearchClips()));
+    connect(m_view.usePlaceholders, SIGNAL(pressed()), this, SLOT(slotPlaceholders()));
+    connect(m_view.removeSelected, SIGNAL(pressed()), this, SLOT(slotDeleteSelected()));
     connect(m_view.treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, SLOT(slotEditItem(QTreeWidgetItem *, int)));
     //adjustSize();
 }
@@ -116,17 +102,20 @@ void DocumentChecker::slotSearchClips()
     QString newpath = KFileDialog::getExistingDirectory(KUrl("kfiledialog:///clipfolder"), kapp->activeWindow(), i18n("Clips folder"));
     if (newpath.isEmpty()) return;
     int ix = 0;
+    m_view.recursiveSearch->setEnabled(false);
     QTreeWidgetItem *child = m_view.treeWidget->topLevelItem(ix);
-    while (child && !child->data(0, statusRole).toString().isEmpty()) {
+    while (child && child->data(0, statusRole).toInt() == CLIPMISSING) {
         QString clipPath = searchFileRecursively(QDir(newpath), child->data(0, sizeRole).toString(), child->data(0, hashRole).toString());
         if (!clipPath.isEmpty()) {
             child->setText(1, clipPath);
             child->setIcon(0, KIcon("dialog-ok"));
-            child->setData(0, statusRole, QString());
+            child->setData(0, statusRole, CLIPOK);
         }
         ix++;
         child = m_view.treeWidget->topLevelItem(ix);
     }
+    m_view.recursiveSearch->setEnabled(true);
+    checkStatus();
 }
 
 QString DocumentChecker::searchFileRecursively(const QDir &dir, const QString &matchSize, const QString &matchHash) const
@@ -155,7 +144,7 @@ QString DocumentChecker::searchFileRecursively(const QDir &dir, const QString &m
                     return file.fileName();
             }
         }
-        kDebug() << filesAndDirs.at(i) << file.size() << fileHash.toHex();
+        //kDebug() << filesAndDirs.at(i) << file.size() << fileHash.toHex();
     }
     filesAndDirs = dir.entryList(QDir::Dirs | QDir::Readable | QDir::Executable | QDir::NoDotAndDotDot);
     for (int i = 0; i < filesAndDirs.size() && foundFileName.isEmpty(); i++) {
@@ -173,7 +162,8 @@ void DocumentChecker::slotEditItem(QTreeWidgetItem *item, int)
     item->setText(1, url.path());
     if (KIO::NetAccess::exists(url, KIO::NetAccess::SourceSide, 0)) {
         item->setIcon(0, KIcon("dialog-ok"));
-        item->setData(0, statusRole, QString());
+        item->setData(0, statusRole, CLIPOK);
+        checkStatus();
     }
 }
 
@@ -185,29 +175,136 @@ void DocumentChecker::accept()
     QDomNodeList infoproducers = m_doc.elementsByTagName("kdenlive_producer");
     int ix = 0;
     QTreeWidgetItem *child = m_view.treeWidget->topLevelItem(ix);
-    while (child && child->data(0, statusRole).toString().isEmpty()) {
-        QString id = child->data(0, idRole).toString();
+    while (child) {
+        if (child->data(0, statusRole).toInt() == CLIPOK) {
+            QString id = child->data(0, idRole).toString();
+            for (int i = 0; i < infoproducers.count(); i++) {
+                e = infoproducers.item(i).toElement();
+                if (e.attribute("id") == id) {
+                    // Fix clip
+                    e.setAttribute("resource", child->text(1));
+                    break;
+                }
+            }
+            for (int i = 0; i < producers.count(); i++) {
+                e = producers.item(i).toElement();
+                if (e.attribute("id") == id) {
+                    // Fix clip
+                    e.setAttribute("resource", child->text(1));
+                    break;
+                }
+            }
+        } else if (child->data(0, statusRole).toInt() == CLIPPLACEHOLDER) {
+            QString id = child->data(0, idRole).toString();
+            for (int i = 0; i < infoproducers.count(); i++) {
+                e = infoproducers.item(i).toElement();
+                if (e.attribute("id") == id) {
+                    // Fix clip
+                    e.setAttribute("placeholder", '1');
+                    break;
+                }
+            }
+        }
+        ix++;
+        child = m_view.treeWidget->topLevelItem(ix);
+    }
+    QDialog::accept();
+}
+
+void DocumentChecker::slotPlaceholders()
+{
+    int ix = 0;
+    QTreeWidgetItem *child = m_view.treeWidget->topLevelItem(ix);
+    while (child) {
+        if (child->data(0, statusRole).toInt() == CLIPMISSING) {
+            child->setData(0, statusRole, CLIPPLACEHOLDER);
+            child->setIcon(0, KIcon("dialog-ok"));
+        }
+        ix++;
+        child = m_view.treeWidget->topLevelItem(ix);
+    }
+    checkStatus();
+}
+
+
+void DocumentChecker::checkStatus()
+{
+    bool status = true;
+    int ix = 0;
+    QTreeWidgetItem *child = m_view.treeWidget->topLevelItem(ix);
+    while (child) {
+        if (child->data(0, statusRole).toInt() == CLIPMISSING) {
+            status = false;
+            break;
+        }
+        ix++;
+        child = m_view.treeWidget->topLevelItem(ix);
+    }
+    m_view.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(status);
+}
+
+
+void DocumentChecker::slotDeleteSelected()
+{
+    if (KMessageBox::warningContinueCancel(this, i18n("This will remove the selected clips from this project"), i18n("Remove clips")) == KMessageBox::Cancel) return;
+    int ix = 0;
+    QStringList deletedIds;
+    QTreeWidgetItem *child = m_view.treeWidget->topLevelItem(ix);
+    QDomNodeList playlists = m_doc.elementsByTagName("playlist");
+
+    while (child) {
+        if (child->isSelected()) {
+            QString id = child->data(0, idRole).toString();
+            deletedIds.append(id);
+            for (int j = 0; j < playlists.count(); j++)
+                deletedIds.append(id + '_' + QString::number(j));
+            delete child;
+        } else ix++;
+        child = m_view.treeWidget->topLevelItem(ix);
+    }
+    kDebug() << "// Clips to delete: " << deletedIds;
+
+    if (!deletedIds.isEmpty()) {
+        QDomElement e;
+        QDomNodeList producers = m_doc.elementsByTagName("producer");
+        QDomNodeList infoproducers = m_doc.elementsByTagName("kdenlive_producer");
+
+        QDomElement westley = m_doc.firstChildElement("westley");
+        QDomElement kdenlivedoc = westley.firstChildElement("kdenlivedoc");
+
         for (int i = 0; i < infoproducers.count(); i++) {
             e = infoproducers.item(i).toElement();
-            if (e.attribute("id") == id) {
-                // Fix clip
-                e.setAttribute("resource", child->text(1));
+            if (deletedIds.contains(e.attribute("id"))) {
+                // Remove clip
+                kdenlivedoc.removeChild(e);
                 break;
             }
         }
+
         for (int i = 0; i < producers.count(); i++) {
             e = producers.item(i).toElement();
-            if (e.attribute("id") == id) {
-                // Fix clip
-                e.setAttribute("resource", child->text(1));
+            if (deletedIds.contains(e.attribute("id"))) {
+                // Remove clip
+                westley.removeChild(e);
                 break;
             }
         }
 
-        ix++;
-        child = m_view.treeWidget->topLevelItem(ix);
+        for (int i = 0; i < playlists.count(); i++) {
+            QDomNodeList entries = playlists.at(i).toElement().elementsByTagName("entry");
+            for (int j = 0; j < playlists.count(); j++) {
+                e = entries.item(j).toElement();
+                if (deletedIds.contains(e.attribute("producer"))) {
+                    // Replace clip with blank
+                    e.setTagName("blank");
+                    e.removeAttribute("producer");
+                    int length = e.attribute("out").toInt() - e.attribute("in").toInt();
+                    e.setAttribute("length", length);
+                }
+            }
+        }
+        checkStatus();
     }
-    QDialog::accept();
 }
 
 #include "documentchecker.moc"