]> git.sesse.net Git - kdenlive/blobdiff - src/documentchecker.cpp
Warn about missing images and fonts in title clips when opening a document:
[kdenlive] / src / documentchecker.cpp
index 058a537f09f32b5bc0ce63dc6bd84d4f492ab2d7..6af599747055e167cae2244345a2f0e439476ec8 100644 (file)
@@ -20,6 +20,8 @@
 
 #include "documentchecker.h"
 #include "kthumb.h"
+#include "docclipbase.h"
+#include "titlewidget.h"
 #include "definitions.h"
 #include "kdenlivesettings.h"
 
@@ -44,6 +46,8 @@ const int hashRole = Qt::UserRole;
 const int sizeRole = Qt::UserRole + 1;
 const int idRole = Qt::UserRole + 2;
 const int statusRole = Qt::UserRole + 3;
+const int typeRole = Qt::UserRole + 4;
+const int typeOriginalResource = Qt::UserRole + 5;
 
 const int CLIPMISSING = 0;
 const int CLIPOK = 1;
@@ -52,21 +56,62 @@ const int LUMAMISSING = 10;
 const int LUMAOK = 11;
 const int LUMAPLACEHOLDER = 12;
 
-DocumentChecker::DocumentChecker(QList <QDomElement> missingClips, QDomDocument doc, QWidget * parent) :
-        QDialog(parent),
-        m_doc(doc)
+enum TITLECLIPTYPE { TITLE_IMAGE_ELEMENT = 20, TITLE_FONT_ELEMENT = 21 };
+
+DocumentChecker::DocumentChecker(QDomNodeList infoproducers, QDomDocument doc):
+        m_info(infoproducers), m_doc(doc), m_dialog(NULL)
 {
-    setFont(KGlobalSettings::toolBarFont());
-    setupUi(this);
+
+}
+
+
+bool DocumentChecker::hasMissingClips()
+{
+    int clipType;
     QDomElement e;
+    QString id;
+    QString resource;
+    QList <QDomElement> missingClips;
+    for (int i = 0; i < m_info.count(); i++) {
+        e = m_info.item(i).toElement();
+        clipType = e.attribute("type").toInt();
+        if (clipType == COLOR) continue;
+        if (clipType == TEXT) {
+            //TODO: Check is clip template is missing (xmltemplate) or hash changed
+            QStringList images = TitleWidget::extractImageList(e.attribute("xmldata"));
+            QStringList fonts = TitleWidget::extractFontList(e.attribute("xmldata"));
+            checkMissingImages(missingClips, images, fonts, e.attribute("id"), e.attribute("name"));
+            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);
+        } else {
+            // Check if the clip has changed
+            if (clipType != SLIDESHOW && e.hasAttribute("file_hash")) {
+                if (e.attribute("file_hash") != DocClipBase::getHash(e.attribute("resource")))
+                    e.removeAttribute("file_hash");
+            }
+        }
+    }
+    if (missingClips.isEmpty()) {
+        return false;
+    }
+    m_dialog = new QDialog();
+    m_dialog->setFont(KGlobalSettings::toolBarFont());
+    m_ui.setupUi(m_dialog);
+
     QStringList missingLumas;
-    QDomNodeList trans = doc.elementsByTagName("transition");
+    QDomNodeList trans = m_doc.elementsByTagName("transition");
     for (int i = 0; i < trans.count(); i++) {
         QString luma = getProperty(trans.at(i).toElement(), "luma");
         if (!luma.isEmpty() && !QFile::exists(luma)) {
             if (!missingLumas.contains(luma)) {
                 missingLumas.append(luma);
-                QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget, QStringList() << i18n("Luma file") << luma);
+                QTreeWidgetItem *item = new QTreeWidgetItem(m_ui.treeWidget, QStringList() << i18n("Luma file") << luma);
                 item->setIcon(0, KIcon("dialog-close"));
                 item->setData(0, idRole, luma);
                 item->setData(0, statusRole, LUMAMISSING);
@@ -74,11 +119,12 @@ DocumentChecker::DocumentChecker(QList <QDomElement> missingClips, QDomDocument
         }
     }
 
-    buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
+    m_ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
     for (int i = 0; i < missingClips.count(); i++) {
         e = missingClips.at(i).toElement();
         QString clipType;
-        switch (e.attribute("type").toInt()) {
+        int t = e.attribute("type").toInt();
+        switch (t) {
         case AV:
             clipType = i18n("Video clip");
             break;
@@ -97,24 +143,56 @@ DocumentChecker::DocumentChecker(QList <QDomElement> missingClips, QDomDocument
         case SLIDESHOW:
             clipType = i18n("Slideshow clip");
             break;
+        case TITLE_IMAGE_ELEMENT:
+            clipType = i18n("Title Image");
+            break;
+        case TITLE_FONT_ELEMENT:
+            clipType = i18n("Title Font");
+            break;
         default:
             clipType = i18n("Video clip");
         }
-        QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget, QStringList() << clipType << e.attribute("resource"));
-        item->setIcon(0, KIcon("dialog-close"));
-        item->setData(0, hashRole, e.attribute("file_hash"));
-        item->setData(0, sizeRole, e.attribute("file_size"));
+        QTreeWidgetItem *item = new QTreeWidgetItem(m_ui.treeWidget, QStringList() << clipType);
+        if (t == TITLE_IMAGE_ELEMENT) {
+            item->setIcon(0, KIcon("dialog-warning"));
+            item->setToolTip(1, e.attribute("name"));
+            item->setText(1, e.attribute("resource"));
+            item->setData(0, statusRole, CLIPPLACEHOLDER);
+            item->setData(0, typeOriginalResource, e.attribute("resource"));
+        } else if (t == TITLE_FONT_ELEMENT) {
+            item->setIcon(0, KIcon("dialog-warning"));
+            item->setToolTip(1, e.attribute("name"));
+            QString ft = e.attribute("resource");
+            QString newft = QFontInfo(QFont(ft)).family();
+            item->setText(1, i18n("%1, will be replaced by %2", ft, newft));
+            item->setData(0, statusRole, CLIPPLACEHOLDER);
+        } else {
+            item->setIcon(0, KIcon("dialog-close"));
+            item->setText(1, e.attribute("resource"));
+            item->setData(0, hashRole, e.attribute("file_hash"));
+            item->setData(0, sizeRole, e.attribute("file_size"));
+            item->setData(0, statusRole, CLIPMISSING);
+        }
+        item->setData(0, typeRole, t);
         item->setData(0, idRole, e.attribute("id"));
-        item->setData(0, statusRole, CLIPMISSING);
     }
-    connect(recursiveSearch, SIGNAL(pressed()), this, SLOT(slotSearchClips()));
-    connect(usePlaceholders, SIGNAL(pressed()), this, SLOT(slotPlaceholders()));
-    connect(removeSelected, SIGNAL(pressed()), this, SLOT(slotDeleteSelected()));
-    connect(treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, SLOT(slotEditItem(QTreeWidgetItem *, int)));
+    connect(m_ui.recursiveSearch, SIGNAL(pressed()), this, SLOT(slotSearchClips()));
+    connect(m_ui.usePlaceholders, SIGNAL(pressed()), this, SLOT(slotPlaceholders()));
+    connect(m_ui.removeSelected, SIGNAL(pressed()), this, SLOT(slotDeleteSelected()));
+    connect(m_ui.treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, SLOT(slotEditItem(QTreeWidgetItem *, int)));
+    connect(m_ui.treeWidget, SIGNAL(itemSelectionChanged()), this, SLOT(slotCheckButtons()));
     //adjustSize();
+    if (m_ui.treeWidget->topLevelItem(0)) m_ui.treeWidget->setCurrentItem(m_ui.treeWidget->topLevelItem(0));
+    checkStatus();
+    int acceptMissing = m_dialog->exec();
+    if (acceptMissing == QDialog::Accepted) acceptDialog();
+    return (acceptMissing != QDialog::Accepted);
 }
 
-DocumentChecker::~DocumentChecker() {}
+DocumentChecker::~DocumentChecker()
+{
+    if (m_dialog) delete m_dialog;
+}
 
 
 QString DocumentChecker::getProperty(QDomElement effect, const QString &name)
@@ -145,8 +223,8 @@ void DocumentChecker::slotSearchClips()
     QString newpath = KFileDialog::getExistingDirectory(KUrl("kfiledialog:///clipfolder"), kapp->activeWindow(), i18n("Clips folder"));
     if (newpath.isEmpty()) return;
     int ix = 0;
-    recursiveSearch->setEnabled(false);
-    QTreeWidgetItem *child = treeWidget->topLevelItem(ix);
+    m_ui.recursiveSearch->setEnabled(false);
+    QTreeWidgetItem *child = m_ui.treeWidget->topLevelItem(ix);
     while (child) {
         if (child->data(0, statusRole).toInt() == CLIPMISSING) {
             QString clipPath = searchFileRecursively(QDir(newpath), child->data(0, sizeRole).toString(), child->data(0, hashRole).toString());
@@ -164,9 +242,9 @@ void DocumentChecker::slotSearchClips()
             }
         }
         ix++;
-        child = treeWidget->topLevelItem(ix);
+        child = m_ui.treeWidget->topLevelItem(ix);
     }
-    recursiveSearch->setEnabled(true);
+    m_ui.recursiveSearch->setEnabled(true);
     checkStatus();
 }
 
@@ -224,7 +302,11 @@ QString DocumentChecker::searchFileRecursively(const QDir &dir, const QString &m
 
 void DocumentChecker::slotEditItem(QTreeWidgetItem *item, int)
 {
-    KUrl url = KUrlRequesterDialog::getUrl(item->text(1), this, i18n("Enter new location for file"));
+    int t = item->data(0, typeRole).toInt();
+    if (t == TITLE_FONT_ELEMENT) return;
+    //|| t == TITLE_IMAGE_ELEMENT) {
+
+    KUrl url = KUrlRequesterDialog::getUrl(item->text(1), m_dialog, i18n("Enter new location for file"));
     if (url.isEmpty()) return;
     item->setText(1, url.path());
     if (KIO::NetAccess::exists(url, KIO::NetAccess::SourceSide, 0)) {
@@ -233,11 +315,17 @@ void DocumentChecker::slotEditItem(QTreeWidgetItem *item, int)
         if (id < 10) item->setData(0, statusRole, CLIPOK);
         else item->setData(0, statusRole, LUMAOK);
         checkStatus();
+    } else {
+        item->setIcon(0, KIcon("dialog-close"));
+        int id = item->data(0, statusRole).toInt();
+        if (id < 10) item->setData(0, statusRole, CLIPMISSING);
+        else item->setData(0, statusRole, LUMAMISSING);
+        checkStatus();
     }
 }
 
-// virtual
-void DocumentChecker::accept()
+
+void DocumentChecker::acceptDialog()
 {
     QDomElement e, property;
     QDomNodeList producers = m_doc.elementsByTagName("producer");
@@ -248,34 +336,66 @@ void DocumentChecker::accept()
     // prepare transitions
     QDomNodeList trans = m_doc.elementsByTagName("transition");
 
-    QTreeWidgetItem *child = treeWidget->topLevelItem(ix);
+    QTreeWidgetItem *child = m_ui.treeWidget->topLevelItem(ix);
     while (child) {
+        int t = child->data(0, typeRole).toInt();
         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));
-                    e.setAttribute("name", KUrl(child->text(1)).fileName());
-                    break;
+            if (t == TITLE_IMAGE_ELEMENT) {
+                // edit images embedded in titles
+                for (int i = 0; i < infoproducers.count(); i++) {
+                    e = infoproducers.item(i).toElement();
+                    if (e.attribute("id") == id) {
+                        // Fix clip
+                        QString xml = e.attribute("xmldata");
+                        xml.replace(child->data(0, typeOriginalResource).toString(), child->text(1));
+                        e.setAttribute("xmldata", xml);
+                        break;
+                    }
                 }
-            }
-            for (int i = 0; i < producers.count(); i++) {
-                e = producers.item(i).toElement();
-                if (e.attribute("id").section('_', 0, 0) == id) {
-                    // Fix clip
-                    properties = e.childNodes();
-                    for (int j = 0; j < properties.count(); ++j) {
-                        property = properties.item(j).toElement();
-                        if (property.attribute("name") == "resource") {
-                            property.firstChild().setNodeValue(child->text(1));
-                            break;
+                for (int i = 0; i < producers.count(); i++) {
+                    e = producers.item(i).toElement();
+                    if (e.attribute("id").section('_', 0, 0) == id) {
+                        // Fix clip
+                        properties = e.childNodes();
+                        for (int j = 0; j < properties.count(); ++j) {
+                            property = properties.item(j).toElement();
+                            if (property.attribute("name") == "xmldata") {
+                                QString xml = property.firstChild().nodeValue();
+                                xml.replace(child->data(0, typeOriginalResource).toString(), child->text(1));
+                                property.firstChild().setNodeValue(xml);
+                                break;
+                            }
+                        }
+                    }
+                }
+            } else {
+                // edit clip url
+                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));
+                        e.setAttribute("name", KUrl(child->text(1)).fileName());
+                        break;
+                    }
+                }
+                for (int i = 0; i < producers.count(); i++) {
+                    e = producers.item(i).toElement();
+                    if (e.attribute("id").section('_', 0, 0) == id) {
+                        // Fix clip
+                        properties = e.childNodes();
+                        for (int j = 0; j < properties.count(); ++j) {
+                            property = properties.item(j).toElement();
+                            if (property.attribute("name") == "resource") {
+                                property.firstChild().setNodeValue(child->text(1));
+                                break;
+                            }
                         }
                     }
                 }
             }
-        } else if (child->data(0, statusRole).toInt() == CLIPPLACEHOLDER) {
+        } else if (child->data(0, statusRole).toInt() == CLIPPLACEHOLDER && t != TITLE_FONT_ELEMENT && t != TITLE_IMAGE_ELEMENT) {
             QString id = child->data(0, idRole).toString();
             for (int i = 0; i < infoproducers.count(); i++) {
                 e = infoproducers.item(i).toElement();
@@ -303,15 +423,15 @@ void DocumentChecker::accept()
             }
         }
         ix++;
-        child = treeWidget->topLevelItem(ix);
+        child = m_ui.treeWidget->topLevelItem(ix);
     }
-    QDialog::accept();
+    //QDialog::accept();
 }
 
 void DocumentChecker::slotPlaceholders()
 {
     int ix = 0;
-    QTreeWidgetItem *child = treeWidget->topLevelItem(ix);
+    QTreeWidgetItem *child = m_ui.treeWidget->topLevelItem(ix);
     while (child) {
         if (child->data(0, statusRole).toInt() == CLIPMISSING) {
             child->setData(0, statusRole, CLIPPLACEHOLDER);
@@ -321,7 +441,7 @@ void DocumentChecker::slotPlaceholders()
             child->setIcon(0, KIcon("dialog-ok"));
         }
         ix++;
-        child = treeWidget->topLevelItem(ix);
+        child = m_ui.treeWidget->topLevelItem(ix);
     }
     checkStatus();
 }
@@ -331,25 +451,25 @@ void DocumentChecker::checkStatus()
 {
     bool status = true;
     int ix = 0;
-    QTreeWidgetItem *child = treeWidget->topLevelItem(ix);
+    QTreeWidgetItem *child = m_ui.treeWidget->topLevelItem(ix);
     while (child) {
         if (child->data(0, statusRole).toInt() == CLIPMISSING || child->data(0, statusRole).toInt() == LUMAMISSING) {
             status = false;
             break;
         }
         ix++;
-        child = treeWidget->topLevelItem(ix);
+        child = m_ui.treeWidget->topLevelItem(ix);
     }
-    buttonBox->button(QDialogButtonBox::Ok)->setEnabled(status);
+    m_ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(status);
 }
 
 
 void DocumentChecker::slotDeleteSelected()
 {
-    if (KMessageBox::warningContinueCancel(this, i18np("This will remove the selected clip from this project", "This will remove the selected clips from this project", treeWidget->selectedItems().count()), i18n("Remove clips")) == KMessageBox::Cancel) return;
+    if (KMessageBox::warningContinueCancel(m_dialog, i18np("This will remove the selected clip from this project", "This will remove the selected clips from this project", m_ui.treeWidget->selectedItems().count()), i18n("Remove clips")) == KMessageBox::Cancel) return;
     int ix = 0;
     QStringList deletedIds;
-    QTreeWidgetItem *child = treeWidget->topLevelItem(ix);
+    QTreeWidgetItem *child = m_ui.treeWidget->topLevelItem(ix);
     QDomNodeList playlists = m_doc.elementsByTagName("playlist");
 
     while (child) {
@@ -361,7 +481,7 @@ void DocumentChecker::slotDeleteSelected()
                 deletedIds.append(id + '_' + QString::number(j));
             delete child;
         } else ix++;
-        child = treeWidget->topLevelItem(ix);
+        child = m_ui.treeWidget->topLevelItem(ix);
     }
     kDebug() << "// Clips to delete: " << deletedIds;
 
@@ -408,6 +528,47 @@ void DocumentChecker::slotDeleteSelected()
     }
 }
 
+void DocumentChecker::checkMissingImages(QList <QDomElement>&missingClips, QStringList images, QStringList fonts, QString id, QString baseClip)
+{
+    QDomDocument doc;
+    foreach(const QString &img, images) {
+        if (!KIO::NetAccess::exists(KUrl(img), KIO::NetAccess::SourceSide, 0)) {
+            QDomElement e = doc.createElement("missingclip");
+            e.setAttribute("type", TITLE_IMAGE_ELEMENT);
+            e.setAttribute("resource", img);
+            e.setAttribute("id", id);
+            e.setAttribute("name", baseClip);
+            missingClips.append(e);
+        }
+    }
+    kDebug() << "/ / / CHK FONTS: " << fonts;
+    foreach(const QString &fontelement, fonts) {
+        QFont f(fontelement);
+        kDebug() << "/ / / CHK FONTS: " << fontelement << " = " << QFontInfo(f).family();
+        if (fontelement != QFontInfo(f).family()) {
+            QDomElement e = doc.createElement("missingclip");
+            e.setAttribute("type", TITLE_FONT_ELEMENT);
+            e.setAttribute("resource", fontelement);
+            e.setAttribute("id", id);
+            e.setAttribute("name", baseClip);
+            missingClips.append(e);
+        }
+    }
+}
+
+
+void DocumentChecker::slotCheckButtons()
+{
+    if (m_ui.treeWidget->currentItem()) {
+        QTreeWidgetItem *item = m_ui.treeWidget->currentItem();
+        int t = item->data(0, typeRole).toInt();
+        if (t == TITLE_FONT_ELEMENT || t == TITLE_IMAGE_ELEMENT) {
+            m_ui.removeSelected->setEnabled(false);
+        } else m_ui.removeSelected->setEnabled(true);
+    }
+
+}
+
 #include "documentchecker.moc"