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