]> git.sesse.net Git - kdenlive/blob - src/documentchecker.cpp
Try to find missing images in title clips with recursive search
[kdenlive] / src / documentchecker.cpp
1 /***************************************************************************
2  *   Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
18  ***************************************************************************/
19
20
21 #include "documentchecker.h"
22 #include "kthumb.h"
23 #include "docclipbase.h"
24 #include "titlewidget.h"
25 #include "definitions.h"
26 #include "kdenlivesettings.h"
27
28 #include <KDebug>
29 #include <KGlobalSettings>
30 #include <KFileItem>
31 #include <KIO/NetAccess>
32 #include <KFileDialog>
33 #include <KApplication>
34 #include <KUrlRequesterDialog>
35 #include <KMessageBox>
36
37 #include <QTreeWidgetItem>
38 #include <QFile>
39 #include <QHeaderView>
40 #include <QIcon>
41 #include <QPixmap>
42 #include <QTimer>
43 #include <QCryptographicHash>
44
45 const int hashRole = Qt::UserRole;
46 const int sizeRole = Qt::UserRole + 1;
47 const int idRole = Qt::UserRole + 2;
48 const int statusRole = Qt::UserRole + 3;
49 const int typeRole = Qt::UserRole + 4;
50 const int typeOriginalResource = Qt::UserRole + 5;
51 const int resetDurationRole = Qt::UserRole + 6;
52
53 const int CLIPMISSING = 0;
54 const int CLIPOK = 1;
55 const int CLIPPLACEHOLDER = 2;
56 const int CLIPWRONGDURATION = 3;
57 const int PROXYMISSING = 4;
58
59 const int LUMAMISSING = 10;
60 const int LUMAOK = 11;
61 const int LUMAPLACEHOLDER = 12;
62
63 enum TITLECLIPTYPE { TITLE_IMAGE_ELEMENT = 20, TITLE_FONT_ELEMENT = 21 };
64
65 DocumentChecker::DocumentChecker(QDomNodeList infoproducers, QDomDocument doc):
66     m_info(infoproducers), m_doc(doc), m_dialog(NULL)
67 {
68
69 }
70
71
72 bool DocumentChecker::hasErrorInClips()
73 {
74     int clipType;
75     QDomElement e;
76     QString resource;
77     QDomNodeList documentProducers = m_doc.elementsByTagName("producer");
78     QList <QDomElement> wrongDurationClips;
79     QList <QDomElement> missingProxies;
80     for (int i = 0; i < m_info.count(); i++) {
81         e = m_info.item(i).toElement();
82         clipType = e.attribute("type").toInt();
83         if (clipType == COLOR) continue;
84         if (clipType != TEXT && clipType != IMAGE && clipType != SLIDESHOW) {
85             QString id = e.attribute("id");
86             int duration = e.attribute("duration").toInt();
87             int mltDuration = -1;
88             // Check that the duration is in sync between Kdenlive's info and MLT's playlist
89             for (int j = 0; j < documentProducers.count(); j++) {
90                 QDomElement mltProd = documentProducers.at(j).toElement();
91                 QString prodId = mltProd.attribute("id");
92                 // Don't check slowmotion clips for now... (TODO?)
93                 if (prodId.startsWith("slowmotion")) continue;
94                 if (prodId.contains("_")) prodId = prodId.section("_", 0, 0);
95                 if (prodId != id) continue;
96                 if (mltDuration > 0 ) {
97                     // We have several MLT producers for the same clip (probably track producers)
98                     int newLength = EffectsList::property(mltProd, "length").toInt();
99                     if (newLength != mltDuration) {
100                         // we have a different duration for the same clip, that is not safe
101                         e.setAttribute("_resetDuration", 1);
102                     }
103                 }
104                 mltDuration = EffectsList::property(mltProd, "length").toInt();
105                 if (mltDuration != duration) {
106                     // Duration mismatch
107                     e.setAttribute("_mismatch", mltDuration);
108                     if (mltDuration == 15000) {
109                         // a length of 15000 might indicate a wrong clip length since it is a default length
110                         e.setAttribute("_resetDuration", 1);
111                     }
112                     if (!wrongDurationClips.contains(e)) wrongDurationClips.append(e);
113                 }
114             }
115         }
116         
117         if (clipType == TEXT) {
118             //TODO: Check is clip template is missing (xmltemplate) or hash changed
119             QStringList images = TitleWidget::extractImageList(e.attribute("xmldata"));
120             QStringList fonts = TitleWidget::extractFontList(e.attribute("xmldata"));
121             checkMissingImages(images, fonts, e.attribute("id"), e.attribute("name"));
122             continue;
123         }
124         resource = e.attribute("resource");
125         if (e.hasAttribute("proxy")) {
126             QString proxyresource = e.attribute("proxy");
127             if (!proxyresource.isEmpty() && proxyresource != "-" && !KIO::NetAccess::exists(KUrl(proxyresource), KIO::NetAccess::SourceSide, 0)) {
128                 // Missing clip found
129                 missingProxies.append(e);
130             }
131         }
132         if (clipType == SLIDESHOW) resource = KUrl(resource).directory();
133         if (!KIO::NetAccess::exists(KUrl(resource), KIO::NetAccess::SourceSide, 0)) {
134             // Missing clip found
135             m_missingClips.append(e);
136         } else {
137             // Check if the clip has changed
138             if (clipType != SLIDESHOW && e.hasAttribute("file_hash")) {
139                 if (e.attribute("file_hash") != DocClipBase::getHash(e.attribute("resource")))
140                     e.removeAttribute("file_hash");
141             }
142         }
143     }
144
145     QStringList missingLumas;
146     QString root = m_doc.documentElement().attribute("root");
147     if (!root.isEmpty()) root = KUrl(root).path(KUrl::AddTrailingSlash);
148     QDomNodeList trans = m_doc.elementsByTagName("transition");
149     for (int i = 0; i < trans.count(); i++) {
150         QString luma = getProperty(trans.at(i).toElement(), "luma");
151         if (!luma.isEmpty()) {
152             QString lumaPath = luma;
153             if (!lumaPath.startsWith('/')) lumaPath.prepend(root);
154             if (!QFile::exists(lumaPath) && !missingLumas.contains(luma)) {
155                 missingLumas.append(luma);
156             }
157         }
158     }
159
160     if (m_missingClips.isEmpty() && missingLumas.isEmpty() && wrongDurationClips.isEmpty() && missingProxies.isEmpty())
161         return false;
162
163     m_dialog = new QDialog();
164     m_dialog->setFont(KGlobalSettings::toolBarFont());
165     m_ui.setupUi(m_dialog);
166
167     foreach(const QString l, missingLumas) {
168         QTreeWidgetItem *item = new QTreeWidgetItem(m_ui.treeWidget, QStringList() << i18n("Luma file") << l);
169         item->setIcon(0, KIcon("dialog-close"));
170         item->setData(0, idRole, l);
171         item->setData(0, statusRole, LUMAMISSING);
172     }
173
174     m_ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
175     for (int i = 0; i < m_missingClips.count(); i++) {
176         e = m_missingClips.at(i).toElement();
177         QString clipType;
178         int t = e.attribute("type").toInt();
179         switch (t) {
180         case AV:
181             clipType = i18n("Video clip");
182             break;
183         case VIDEO:
184             clipType = i18n("Mute video clip");
185             break;
186         case AUDIO:
187             clipType = i18n("Audio clip");
188             break;
189         case PLAYLIST:
190             clipType = i18n("Playlist clip");
191             break;
192         case IMAGE:
193             clipType = i18n("Image clip");
194             break;
195         case SLIDESHOW:
196             clipType = i18n("Slideshow clip");
197             break;
198         case TITLE_IMAGE_ELEMENT:
199             clipType = i18n("Title Image");
200             break;
201         case TITLE_FONT_ELEMENT:
202             clipType = i18n("Title Font");
203             break;
204         default:
205             clipType = i18n("Video clip");
206         }
207         QTreeWidgetItem *item = new QTreeWidgetItem(m_ui.treeWidget, QStringList() << clipType);
208         if (t == TITLE_IMAGE_ELEMENT) {
209             item->setIcon(0, KIcon("dialog-warning"));
210             item->setToolTip(1, e.attribute("name"));
211             item->setText(1, e.attribute("resource"));
212             item->setData(0, statusRole, CLIPPLACEHOLDER);
213             item->setData(0, typeOriginalResource, e.attribute("resource"));
214         } else if (t == TITLE_FONT_ELEMENT) {
215             item->setIcon(0, KIcon("dialog-warning"));
216             item->setToolTip(1, e.attribute("name"));
217             QString ft = e.attribute("resource");
218             QString newft = QFontInfo(QFont(ft)).family();
219             item->setText(1, i18n("%1 will be replaced by %2", ft, newft));
220             item->setData(0, statusRole, CLIPPLACEHOLDER);
221         } else {
222             item->setIcon(0, KIcon("dialog-close"));
223             item->setText(1, e.attribute("resource"));
224             item->setData(0, hashRole, e.attribute("file_hash"));
225             item->setData(0, sizeRole, e.attribute("file_size"));
226             item->setData(0, statusRole, CLIPMISSING);
227         }
228         item->setData(0, typeRole, t);
229         item->setData(0, idRole, e.attribute("id"));
230         item->setToolTip(0, i18n("Missing item"));
231     }
232
233     if (m_missingClips.count() > 0) {
234         if (wrongDurationClips.count() > 0) {
235             m_ui.infoLabel->setText(i18n("The project file contains missing clips or files and clip duration mismatch"));
236         }
237         else {
238             m_ui.infoLabel->setText(i18n("The project file contains missing clips or files"));
239         }
240     }
241     else if (wrongDurationClips.count() > 0) {
242         m_ui.infoLabel->setText(i18n("The project file contains clips with duration mismatch"));
243     }
244     if (missingProxies.count() > 0) {
245         if (!m_ui.infoLabel->text().isEmpty()) m_ui.infoLabel->setText(m_ui.infoLabel->text() + ". ");
246         m_ui.infoLabel->setText(m_ui.infoLabel->text() + i18n("Missing proxies will be recreated after opening."));
247     }
248
249     m_ui.removeSelected->setEnabled(!m_missingClips.isEmpty());
250     m_ui.recursiveSearch->setEnabled(!m_missingClips.isEmpty() || !missingLumas.isEmpty());
251     m_ui.usePlaceholders->setEnabled(!m_missingClips.isEmpty());
252     m_ui.fixDuration->setEnabled(!wrongDurationClips.isEmpty());
253         
254     for (int i = 0; i < wrongDurationClips.count(); i++) {
255         e = wrongDurationClips.at(i).toElement();
256         QString clipType;
257         int t = e.attribute("type").toInt();
258         switch (t) {
259         case AV:
260             clipType = i18n("Video clip");
261             break;
262         case VIDEO:
263             clipType = i18n("Mute video clip");
264             break;
265         case AUDIO:
266             clipType = i18n("Audio clip");
267             break;
268         case PLAYLIST:
269             clipType = i18n("Playlist clip");
270             break;
271         case IMAGE:
272             clipType = i18n("Image clip");
273             break;
274         case SLIDESHOW:
275             clipType = i18n("Slideshow clip");
276             break;
277         default:
278             clipType = i18n("Video clip");
279         }
280         QTreeWidgetItem *item = new QTreeWidgetItem(m_ui.treeWidget, QStringList() << clipType);
281         item->setIcon(0, KIcon("timeadjust"));
282         item->setText(1, e.attribute("resource"));
283         item->setData(0, hashRole, e.attribute("file_hash"));
284         item->setData(0, sizeRole, e.attribute("_mismatch"));
285         e.removeAttribute("_mismatch");
286         item->setData(0, resetDurationRole, (int) e.hasAttribute("_resetDuration"));
287         e.removeAttribute("_resetDuration");
288         item->setData(0, statusRole, CLIPWRONGDURATION);
289         item->setData(0, typeRole, t);
290         item->setData(0, idRole, e.attribute("id"));
291         item->setToolTip(0, i18n("Duration mismatch"));
292     }
293
294     if (missingProxies.count() > 0) {
295         QTreeWidgetItem *item = new QTreeWidgetItem(m_ui.treeWidget, QStringList() << i18n("Proxy clip"));
296         item->setIcon(0, KIcon("dialog-warning"));
297         item->setText(1, i18n("%1 missing proxy clips, will be recreated on project opening", missingProxies.count()));
298         item->setData(0, hashRole, e.attribute("file_hash"));
299         item->setData(0, statusRole, PROXYMISSING);
300         item->setToolTip(0, i18n("Missing proxy"));
301     }
302
303     for (int i = 0; i < missingProxies.count(); i++) {
304         e = missingProxies.at(i).toElement();
305         QString clipType;
306         QString realPath = e.attribute("resource");
307         QString id = e.attribute("id");
308         // Replace proxy url with real clip in MLT producers
309         QDomNodeList properties;
310         QDomElement mltProd;
311         QDomElement property;
312         for (int j = 0; j < documentProducers.count(); j++) {
313             mltProd = documentProducers.at(j).toElement();
314             QString prodId = mltProd.attribute("id");
315             bool slowmotion = false;
316             if (prodId.startsWith("slowmotion")) {
317                 slowmotion = true;
318                 prodId = prodId.section(':', 1, 1);
319             }
320             if (prodId.contains('_')) prodId = prodId.section('_', 0, 0);
321             if (prodId == id) {
322                 // Hit, we must replace url
323                 properties = mltProd.childNodes();
324                 for (int k = 0; k < properties.count(); ++k) {
325                     property = properties.item(k).toElement();
326                     if (property.attribute("name") == "resource") {
327                         QString resource = property.firstChild().nodeValue();                    
328                         QString suffix;
329                         if (slowmotion) suffix = "?" + resource.section('?', -1);
330                         property.firstChild().setNodeValue(realPath + suffix);
331                         break;
332                     }
333                 }
334             }
335         }
336     }
337     
338     if (missingProxies.count() > 0) {
339         // original doc was modified
340         QDomNode infoXmlNode = m_doc.elementsByTagName("kdenlivedoc").at(0);
341         QDomElement infoXml = infoXmlNode.toElement();
342         infoXml.setAttribute("modified", "1");
343     }
344     
345     connect(m_ui.recursiveSearch, SIGNAL(pressed()), this, SLOT(slotSearchClips()));
346     connect(m_ui.usePlaceholders, SIGNAL(pressed()), this, SLOT(slotPlaceholders()));
347     connect(m_ui.removeSelected, SIGNAL(pressed()), this, SLOT(slotDeleteSelected()));
348     connect(m_ui.fixDuration, SIGNAL(pressed()), this, SLOT(slotFixDuration()));
349     connect(m_ui.treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, SLOT(slotEditItem(QTreeWidgetItem *, int)));
350     connect(m_ui.treeWidget, SIGNAL(itemSelectionChanged()), this, SLOT(slotCheckButtons()));
351     //adjustSize();
352     if (m_ui.treeWidget->topLevelItem(0)) m_ui.treeWidget->setCurrentItem(m_ui.treeWidget->topLevelItem(0));
353     checkStatus();
354     int acceptMissing = m_dialog->exec();
355     if (acceptMissing == QDialog::Accepted) acceptDialog();
356     return (acceptMissing != QDialog::Accepted);
357 }
358
359 DocumentChecker::~DocumentChecker()
360 {
361     if (m_dialog) delete m_dialog;
362 }
363
364
365 QString DocumentChecker::getProperty(QDomElement effect, const QString &name)
366 {
367     QDomNodeList params = effect.elementsByTagName("property");
368     for (int i = 0; i < params.count(); i++) {
369         QDomElement e = params.item(i).toElement();
370         if (e.attribute("name") == name) {
371             return e.firstChild().nodeValue();
372         }
373     }
374     return QString();
375 }
376
377 void DocumentChecker::setProperty(QDomElement effect, const QString &name, const QString value)
378 {
379     QDomNodeList params = effect.elementsByTagName("property");
380     for (int i = 0; i < params.count(); i++) {
381         QDomElement e = params.item(i).toElement();
382         if (e.attribute("name") == name) {
383             e.firstChild().setNodeValue(value);
384         }
385     }
386 }
387
388 void DocumentChecker::slotSearchClips()
389 {
390     QString newpath = KFileDialog::getExistingDirectory(KUrl("kfiledialog:///clipfolder"), kapp->activeWindow(), i18n("Clips folder"));
391     if (newpath.isEmpty()) return;
392     int ix = 0;
393     bool fixed = false;
394     m_ui.recursiveSearch->setEnabled(false);
395     QTreeWidgetItem *child = m_ui.treeWidget->topLevelItem(ix);
396     QDir searchDir(newpath);
397     while (child) {
398         if (child->data(0, statusRole).toInt() == CLIPMISSING) {
399             QString clipPath = searchFileRecursively(searchDir, child->data(0, sizeRole).toString(), child->data(0, hashRole).toString());
400             if (!clipPath.isEmpty()) {
401                 fixed = true;
402                 child->setText(1, clipPath);
403                 child->setIcon(0, KIcon("dialog-ok"));
404                 child->setData(0, statusRole, CLIPOK);
405             }
406         } else if (child->data(0, statusRole).toInt() == LUMAMISSING) {
407             QString fileName = searchLuma(child->data(0, idRole).toString());
408             if (!fileName.isEmpty()) {
409                 fixed = true;
410                 child->setText(1, fileName);
411                 child->setIcon(0, KIcon("dialog-ok"));
412                 child->setData(0, statusRole, LUMAOK);
413             }
414         }
415         else if (child->data(0, typeRole).toInt() == TITLE_IMAGE_ELEMENT && child->data(0, statusRole).toInt() == CLIPPLACEHOLDER) {
416             // Search missing title images
417             QString missingFileName = KUrl(child->text(1)).fileName();
418             QString newPath = searchPathRecursively(searchDir, missingFileName);
419             if (!newPath.isEmpty()) {
420                 // File found
421                 fixed = true;
422                 child->setText(1, newPath);
423                 child->setIcon(0, KIcon("dialog-ok"));
424                 child->setData(0, statusRole, CLIPOK);
425             }
426         }
427         ix++;
428         child = m_ui.treeWidget->topLevelItem(ix);
429     }
430     m_ui.recursiveSearch->setEnabled(true);
431     if (fixed) {
432         // original doc was modified
433         QDomNode infoXmlNode = m_doc.elementsByTagName("kdenlivedoc").at(0);
434         QDomElement infoXml = infoXmlNode.toElement();
435         infoXml.setAttribute("modified", "1");
436     }
437     checkStatus();
438 }
439
440
441 QString DocumentChecker::searchLuma(QString file) const
442 {
443     KUrl searchPath(KdenliveSettings::mltpath());
444     if (file.contains("PAL"))
445         searchPath.cd("../lumas/PAL");
446     else
447         searchPath.cd("../lumas/NTSC");
448     QString result = searchPath.path(KUrl::AddTrailingSlash) + KUrl(file).fileName();
449     if (QFile::exists(result))
450         return result;
451     // try to find luma in application path
452     searchPath.clear();
453     searchPath = KUrl(QCoreApplication::applicationDirPath());
454     searchPath.cd("../share/apps/kdenlive/lumas");
455     result = searchPath.path(KUrl::AddTrailingSlash) + KUrl(file).fileName();
456     if (QFile::exists(result))
457         return result;
458     return QString();
459 }
460
461 QString DocumentChecker::searchPathRecursively(const QDir &dir, const QString &fileName) const
462 {
463     QString foundFileName;
464     QStringList filters;
465     filters << fileName;
466     QDir searchDir(dir);
467     searchDir.setNameFilters(filters);
468     QStringList filesAndDirs = searchDir.entryList(QDir::Files | QDir::Readable);
469     if (!filesAndDirs.isEmpty()) return searchDir.absoluteFilePath(filesAndDirs.at(0));
470     searchDir.setNameFilters(QStringList());
471     filesAndDirs = searchDir.entryList(QDir::Dirs | QDir::Readable | QDir::Executable | QDir::NoDotAndDotDot);
472     for (int i = 0; i < filesAndDirs.size() && foundFileName.isEmpty(); i++) {
473         foundFileName = searchPathRecursively(searchDir.absoluteFilePath(filesAndDirs.at(i)), fileName);
474         if (!foundFileName.isEmpty())
475             break;
476     }
477     return foundFileName;
478 }
479
480 QString DocumentChecker::searchFileRecursively(const QDir &dir, const QString &matchSize, const QString &matchHash) const
481 {
482     QString foundFileName;
483     QByteArray fileData;
484     QByteArray fileHash;
485     QStringList filesAndDirs = dir.entryList(QDir::Files | QDir::Readable);
486     for (int i = 0; i < filesAndDirs.size() && foundFileName.isEmpty(); i++) {
487         QFile file(dir.absoluteFilePath(filesAndDirs.at(i)));
488         if (QString::number(file.size()) == matchSize) {
489             if (file.open(QIODevice::ReadOnly)) {
490                 /*
491                 * 1 MB = 1 second per 450 files (or faster)
492                 * 10 MB = 9 seconds per 450 files (or faster)
493                 */
494                 if (file.size() > 1000000 * 2) {
495                     fileData = file.read(1000000);
496                     if (file.seek(file.size() - 1000000))
497                         fileData.append(file.readAll());
498                 } else
499                     fileData = file.readAll();
500                 file.close();
501                 fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
502                 if (QString(fileHash.toHex()) == matchHash)
503                     return file.fileName();
504             }
505         }
506         //kDebug() << filesAndDirs.at(i) << file.size() << fileHash.toHex();
507     }
508     filesAndDirs = dir.entryList(QDir::Dirs | QDir::Readable | QDir::Executable | QDir::NoDotAndDotDot);
509     for (int i = 0; i < filesAndDirs.size() && foundFileName.isEmpty(); i++) {
510         foundFileName = searchFileRecursively(dir.absoluteFilePath(filesAndDirs.at(i)), matchSize, matchHash);
511         if (!foundFileName.isEmpty())
512             break;
513     }
514     return foundFileName;
515 }
516
517 void DocumentChecker::slotEditItem(QTreeWidgetItem *item, int)
518 {
519     int t = item->data(0, typeRole).toInt();
520     if (t == TITLE_FONT_ELEMENT) return;
521     //|| t == TITLE_IMAGE_ELEMENT) {
522
523     KUrl url = KUrlRequesterDialog::getUrl(item->text(1), m_dialog, i18n("Enter new location for file"));
524     if (url.isEmpty()) return;
525     item->setText(1, url.path());
526     if (KIO::NetAccess::exists(url, KIO::NetAccess::SourceSide, 0)) {
527         item->setIcon(0, KIcon("dialog-ok"));
528         int id = item->data(0, statusRole).toInt();
529         if (id < 10) item->setData(0, statusRole, CLIPOK);
530         else item->setData(0, statusRole, LUMAOK);
531         checkStatus();
532     } else {
533         item->setIcon(0, KIcon("dialog-close"));
534         int id = item->data(0, statusRole).toInt();
535         if (id < 10) item->setData(0, statusRole, CLIPMISSING);
536         else item->setData(0, statusRole, LUMAMISSING);
537         checkStatus();
538     }
539 }
540
541
542 void DocumentChecker::acceptDialog()
543 {
544     QDomElement e, property;
545     QDomNodeList producers = m_doc.elementsByTagName("producer");
546     QDomNodeList infoproducers = m_doc.elementsByTagName("kdenlive_producer");
547     QDomNodeList properties;
548     int ix = 0;
549
550     // prepare transitions
551     QDomNodeList trans = m_doc.elementsByTagName("transition");
552
553     // Mark document as modified
554     m_doc.documentElement().setAttribute("modified", 1);
555
556     QTreeWidgetItem *child = m_ui.treeWidget->topLevelItem(ix);
557     while (child) {
558         int t = child->data(0, typeRole).toInt();
559         if (child->data(0, statusRole).toInt() == CLIPOK) {
560             QString id = child->data(0, idRole).toString();
561             if (t == TITLE_IMAGE_ELEMENT) {
562                 // edit images embedded in titles
563                 for (int i = 0; i < infoproducers.count(); i++) {
564                     e = infoproducers.item(i).toElement();
565                     if (e.attribute("id") == id) {
566                         // Fix clip
567                         QString xml = e.attribute("xmldata");
568                         xml.replace(child->data(0, typeOriginalResource).toString(), child->text(1));
569                         e.setAttribute("xmldata", xml);
570                         break;
571                     }
572                 }
573                 for (int i = 0; i < producers.count(); i++) {
574                     e = producers.item(i).toElement();
575                     if (e.attribute("id").section('_', 0, 0) == id) {
576                         // Fix clip
577                         properties = e.childNodes();
578                         for (int j = 0; j < properties.count(); ++j) {
579                             property = properties.item(j).toElement();
580                             if (property.attribute("name") == "xmldata") {
581                                 QString xml = property.firstChild().nodeValue();
582                                 xml.replace(child->data(0, typeOriginalResource).toString(), child->text(1));
583                                 property.firstChild().setNodeValue(xml);
584                                 break;
585                             }
586                         }
587                     }
588                 }
589             } else {
590                 // edit clip url
591                 for (int i = 0; i < infoproducers.count(); i++) {
592                     e = infoproducers.item(i).toElement();
593                     if (e.attribute("id") == id) {
594                         // Fix clip
595                         e.setAttribute("resource", child->text(1));
596                         e.setAttribute("name", KUrl(child->text(1)).fileName());
597                         break;
598                     }
599                 }
600                 for (int i = 0; i < producers.count(); i++) {
601                     e = producers.item(i).toElement();
602                     if (e.attribute("id").section('_', 0, 0) == id || e.attribute("id").section(':', 1, 1) == id) {
603                         // Fix clip
604                         properties = e.childNodes();
605                         for (int j = 0; j < properties.count(); ++j) {
606                             property = properties.item(j).toElement();
607                             if (property.attribute("name") == "resource") {
608                                 QString resource = property.firstChild().nodeValue();
609                                 if (resource.contains(QRegExp("\\?[0-9]+\\.[0-9]+(&amp;strobe=[0-9]+)?$")))
610                                     property.firstChild().setNodeValue(child->text(1) + '?' + resource.section('?', -1));
611                                 else
612                                     property.firstChild().setNodeValue(child->text(1));
613                                 break;
614                             }
615                         }
616                     }
617                 }
618             }
619         } else if (child->data(0, statusRole).toInt() == CLIPPLACEHOLDER && t != TITLE_FONT_ELEMENT && t != TITLE_IMAGE_ELEMENT) {
620             QString id = child->data(0, idRole).toString();
621             for (int i = 0; i < infoproducers.count(); i++) {
622                 e = infoproducers.item(i).toElement();
623                 if (e.attribute("id") == id) {
624                     // Fix clip
625                     e.setAttribute("placeholder", '1');
626                     break;
627                 }
628             }
629         } else if (child->data(0, statusRole).toInt() == LUMAOK) {
630             for (int i = 0; i < trans.count(); i++) {
631                 QString luma = getProperty(trans.at(i).toElement(), "luma");
632                 
633                 kDebug() << "luma: " << luma;
634                 if (!luma.isEmpty() && luma == child->data(0, idRole).toString()) {
635                     setProperty(trans.at(i).toElement(), "luma", child->text(1));
636                     kDebug() << "replace with; " << child->text(1);
637                 }
638             }
639         } else if (child->data(0, statusRole).toInt() == LUMAMISSING) {
640             for (int i = 0; i < trans.count(); i++) {
641                 QString luma = getProperty(trans.at(i).toElement(), "luma");
642                 if (!luma.isEmpty() && luma == child->data(0, idRole).toString()) {
643                     setProperty(trans.at(i).toElement(), "luma", QString());
644                 }
645             }
646         }
647         ix++;
648         child = m_ui.treeWidget->topLevelItem(ix);
649     }
650     //QDialog::accept();
651 }
652
653 void DocumentChecker::slotPlaceholders()
654 {
655     int ix = 0;
656     QTreeWidgetItem *child = m_ui.treeWidget->topLevelItem(ix);
657     while (child) {
658         if (child->data(0, statusRole).toInt() == CLIPMISSING) {
659             child->setData(0, statusRole, CLIPPLACEHOLDER);
660             child->setIcon(0, KIcon("dialog-ok"));
661         } else if (child->data(0, statusRole).toInt() == LUMAMISSING) {
662             child->setData(0, statusRole, LUMAPLACEHOLDER);
663             child->setIcon(0, KIcon("dialog-ok"));
664         }
665         ix++;
666         child = m_ui.treeWidget->topLevelItem(ix);
667     }
668     checkStatus();
669 }
670
671 void DocumentChecker::slotFixDuration()
672 {
673     int ix = 0;
674     QTreeWidgetItem *child = m_ui.treeWidget->topLevelItem(ix);
675     QDomNodeList documentProducers = m_doc.elementsByTagName("producer");
676     while (child) {
677         if (child->data(0, statusRole).toInt() == CLIPWRONGDURATION) {
678             QString id = child->data(0, idRole).toString();
679             bool resetDuration = child->data(0, resetDurationRole).toInt();
680
681             for (int i = 0; i < m_info.count(); i++) {
682                 QDomElement e = m_info.at(i).toElement();
683                 if (e.attribute("id") == id) {
684                     if (m_missingClips.contains(e)) {
685                         // we cannot fix duration of missing clips
686                         resetDuration = false;
687                     }
688                     else {
689                         if (resetDuration) e.removeAttribute("duration");
690                         else e.setAttribute("duration", child->data(0, sizeRole).toString());
691                         child->setData(0, statusRole, CLIPOK);
692                         child->setIcon(0, KIcon("dialog-ok"));
693                     }
694                     break;
695                 }
696             }
697             if (resetDuration) {
698                 // something is wrong in clip durations, so remove them so mlt fetches them again
699                 for (int j = 0; j < documentProducers.count(); j++) {
700                     QDomElement mltProd = documentProducers.at(j).toElement();
701                     QString prodId = mltProd.attribute("id");
702                     if (prodId == id || prodId.startsWith(id + "_")) {
703                         EffectsList::removeProperty(mltProd, "length");
704                     }
705                 }
706             }
707         }
708         ix++;
709         child = m_ui.treeWidget->topLevelItem(ix);
710     }
711     QDomNode infoXmlNode = m_doc.elementsByTagName("kdenlivedoc").at(0);
712     QDomElement infoXml = infoXmlNode.toElement();
713     infoXml.setAttribute("modified", "1");
714     m_ui.fixDuration->setEnabled(false);
715     checkStatus();
716 }
717
718
719 void DocumentChecker::checkStatus()
720 {
721     bool status = true;
722     int ix = 0;
723     QTreeWidgetItem *child = m_ui.treeWidget->topLevelItem(ix);
724     while (child) {
725         int status = child->data(0, statusRole).toInt();
726         if (status == CLIPMISSING || status == LUMAMISSING || status == CLIPWRONGDURATION) {
727             status = false;
728             break;
729         }
730         ix++;
731         child = m_ui.treeWidget->topLevelItem(ix);
732     }
733     m_ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(status);
734 }
735
736
737 void DocumentChecker::slotDeleteSelected()
738 {
739     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)
740         return;
741     QStringList deletedIds;
742     QStringList deletedLumas;
743     QDomNodeList playlists = m_doc.elementsByTagName("playlist");
744
745     foreach(QTreeWidgetItem *child, m_ui.treeWidget->selectedItems()) {
746         int id = child->data(0, statusRole).toInt();
747         if (id == CLIPMISSING) {
748             deletedIds.append(child->data(0, idRole).toString());
749             delete child;
750         }
751         else if (id == LUMAMISSING) {
752             deletedLumas.append(child->data(0, idRole).toString());
753             delete child;
754         }
755     }
756
757     if (!deletedLumas.isEmpty()) {
758         QDomElement e;
759         QDomNodeList transitions = m_doc.elementsByTagName("transition");
760         foreach (QString lumaPath, deletedLumas) {
761             for (int i = 0; i < transitions.count(); i++) {
762                 e = transitions.item(i).toElement();
763                 QString resource = EffectsList::property(e, "luma");
764                 if (resource == lumaPath) EffectsList::removeProperty(e, "luma");
765             }
766         }
767     }
768
769     if (!deletedIds.isEmpty()) {
770         QDomElement e;
771         QDomNodeList producers = m_doc.elementsByTagName("producer");
772         QDomNodeList infoproducers = m_doc.elementsByTagName("kdenlive_producer");
773
774         QDomElement mlt = m_doc.firstChildElement("mlt");
775         QDomElement kdenlivedoc = mlt.firstChildElement("kdenlivedoc");
776
777         for (int i = 0, j = 0; i < infoproducers.count() && j < deletedIds.count(); i++) {
778             e = infoproducers.item(i).toElement();
779             if (deletedIds.contains(e.attribute("id"))) {
780                 // Remove clip
781                 kdenlivedoc.removeChild(e);
782                 i--;
783                 j++;
784             }
785         }
786
787         for (int i = 0; i < producers.count(); i++) {
788             e = producers.item(i).toElement();
789             if (deletedIds.contains(e.attribute("id").section('_', 0, 0)) || deletedIds.contains(e.attribute("id").section(':', 1, 1).section('_', 0, 0))) {
790                 // Remove clip
791                 mlt.removeChild(e);
792                 i--;
793             }
794         }
795
796         for (int i = 0; i < playlists.count(); i++) {
797             QDomNodeList entries = playlists.at(i).toElement().elementsByTagName("entry");
798             for (int j = 0; j < entries.count(); j++) {
799                 e = entries.item(j).toElement();
800                 if (deletedIds.contains(e.attribute("producer").section('_', 0, 0)) || deletedIds.contains(e.attribute("producer").section(':', 1, 1).section('_', 0, 0))) {
801                     // Replace clip with blank
802                     while (e.childNodes().count() > 0)
803                         e.removeChild(e.firstChild());
804                     e.setTagName("blank");
805                     e.removeAttribute("producer");
806                     int length = e.attribute("out").toInt() - e.attribute("in").toInt();
807                     e.setAttribute("length", length);
808                     j--;
809                 }
810             }
811         }
812         QDomNode infoXmlNode = m_doc.elementsByTagName("kdenlivedoc").at(0);
813         QDomElement infoXml = infoXmlNode.toElement();
814         infoXml.setAttribute("modified", "1");
815         checkStatus();
816     }
817 }
818
819 void DocumentChecker::checkMissingImages(QStringList images, QStringList fonts, QString id, QString baseClip)
820 {
821     QDomDocument doc;
822     foreach(const QString &img, images) {
823         if (!KIO::NetAccess::exists(KUrl(img), KIO::NetAccess::SourceSide, 0)) {
824             QDomElement e = doc.createElement("missingclip");
825             e.setAttribute("type", TITLE_IMAGE_ELEMENT);
826             e.setAttribute("resource", img);
827             e.setAttribute("id", id);
828             e.setAttribute("name", baseClip);
829             m_missingClips.append(e);
830         }
831     }
832     kDebug() << "/ / / CHK FONTS: " << fonts;
833     foreach(const QString &fontelement, fonts) {
834         QFont f(fontelement);
835         kDebug() << "/ / / CHK FONTS: " << fontelement << " = " << QFontInfo(f).family();
836         if (fontelement != QFontInfo(f).family()) {
837             QDomElement e = doc.createElement("missingclip");
838             e.setAttribute("type", TITLE_FONT_ELEMENT);
839             e.setAttribute("resource", fontelement);
840             e.setAttribute("id", id);
841             e.setAttribute("name", baseClip);
842             m_missingClips.append(e);
843         }
844     }
845 }
846
847
848 void DocumentChecker::slotCheckButtons()
849 {
850     if (m_ui.treeWidget->currentItem()) {
851         QTreeWidgetItem *item = m_ui.treeWidget->currentItem();
852         int t = item->data(0, typeRole).toInt();
853         int s = item->data(0, statusRole).toInt();
854         if (t == TITLE_FONT_ELEMENT || t == TITLE_IMAGE_ELEMENT || s == PROXYMISSING) {
855             m_ui.removeSelected->setEnabled(false);
856         } else m_ui.removeSelected->setEnabled(true);
857     }
858
859 }
860
861 #include "documentchecker.moc"
862
863