]> git.sesse.net Git - kdenlive/blob - src/documentchecker.cpp
Cleanup
[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         QDomElement infoXml = m_doc.elementsByTagName("kdenlivedoc").at(0).toElement();
341         infoXml.setAttribute("modified", "1");
342     }
343     
344     connect(m_ui.recursiveSearch, SIGNAL(pressed()), this, SLOT(slotSearchClips()));
345     connect(m_ui.usePlaceholders, SIGNAL(pressed()), this, SLOT(slotPlaceholders()));
346     connect(m_ui.removeSelected, SIGNAL(pressed()), this, SLOT(slotDeleteSelected()));
347     connect(m_ui.fixDuration, SIGNAL(pressed()), this, SLOT(slotFixDuration()));
348     connect(m_ui.treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, SLOT(slotEditItem(QTreeWidgetItem *, int)));
349     connect(m_ui.treeWidget, SIGNAL(itemSelectionChanged()), this, SLOT(slotCheckButtons()));
350     //adjustSize();
351     if (m_ui.treeWidget->topLevelItem(0)) m_ui.treeWidget->setCurrentItem(m_ui.treeWidget->topLevelItem(0));
352     checkStatus();
353     int acceptMissing = m_dialog->exec();
354     if (acceptMissing == QDialog::Accepted) acceptDialog();
355     return (acceptMissing != QDialog::Accepted);
356 }
357
358 DocumentChecker::~DocumentChecker()
359 {
360     if (m_dialog) delete m_dialog;
361 }
362
363
364 QString DocumentChecker::getProperty(QDomElement effect, const QString &name)
365 {
366     QDomNodeList params = effect.elementsByTagName("property");
367     for (int i = 0; i < params.count(); i++) {
368         QDomElement e = params.item(i).toElement();
369         if (e.attribute("name") == name) {
370             return e.firstChild().nodeValue();
371         }
372     }
373     return QString();
374 }
375
376 void DocumentChecker::setProperty(QDomElement effect, const QString &name, const QString value)
377 {
378     QDomNodeList params = effect.elementsByTagName("property");
379     for (int i = 0; i < params.count(); i++) {
380         QDomElement e = params.item(i).toElement();
381         if (e.attribute("name") == name) {
382             e.firstChild().setNodeValue(value);
383         }
384     }
385 }
386
387 void DocumentChecker::slotSearchClips()
388 {
389     QString newpath = KFileDialog::getExistingDirectory(KUrl("kfiledialog:///clipfolder"), kapp->activeWindow(), i18n("Clips folder"));
390     if (newpath.isEmpty()) return;
391     int ix = 0;
392     bool fixed = false;
393     m_ui.recursiveSearch->setEnabled(false);
394     QTreeWidgetItem *child = m_ui.treeWidget->topLevelItem(ix);
395     QDir searchDir(newpath);
396     while (child) {
397         if (child->data(0, statusRole).toInt() == CLIPMISSING) {
398             QString clipPath = searchFileRecursively(searchDir, child->data(0, sizeRole).toString(), child->data(0, hashRole).toString());
399             if (!clipPath.isEmpty()) {
400                 fixed = true;
401                 child->setText(1, clipPath);
402                 child->setIcon(0, KIcon("dialog-ok"));
403                 child->setData(0, statusRole, CLIPOK);
404             }
405         } else if (child->data(0, statusRole).toInt() == LUMAMISSING) {
406             QString fileName = searchLuma(child->data(0, idRole).toString());
407             if (!fileName.isEmpty()) {
408                 fixed = true;
409                 child->setText(1, fileName);
410                 child->setIcon(0, KIcon("dialog-ok"));
411                 child->setData(0, statusRole, LUMAOK);
412             }
413         }
414         else if (child->data(0, typeRole).toInt() == TITLE_IMAGE_ELEMENT && child->data(0, statusRole).toInt() == CLIPPLACEHOLDER) {
415             // Search missing title images
416             QString missingFileName = KUrl(child->text(1)).fileName();
417             QString newPath = searchPathRecursively(searchDir, missingFileName);
418             if (!newPath.isEmpty()) {
419                 // File found
420                 fixed = true;
421                 child->setText(1, newPath);
422                 child->setIcon(0, KIcon("dialog-ok"));
423                 child->setData(0, statusRole, CLIPOK);
424             }
425         }
426         ix++;
427         child = m_ui.treeWidget->topLevelItem(ix);
428     }
429     m_ui.recursiveSearch->setEnabled(true);
430     if (fixed) {
431         // original doc was modified
432         QDomElement infoXml = m_doc.elementsByTagName("kdenlivedoc").at(0).toElement();
433         infoXml.setAttribute("modified", "1");
434     }
435     checkStatus();
436 }
437
438
439 QString DocumentChecker::searchLuma(QString file) const
440 {
441     KUrl searchPath(KdenliveSettings::mltpath());
442     if (file.contains("PAL"))
443         searchPath.cd("../lumas/PAL");
444     else
445         searchPath.cd("../lumas/NTSC");
446     QString result = searchPath.path(KUrl::AddTrailingSlash) + KUrl(file).fileName();
447     if (QFile::exists(result))
448         return result;
449     // try to find luma in application path
450     searchPath.clear();
451     searchPath = KUrl(QCoreApplication::applicationDirPath());
452     searchPath.cd("../share/apps/kdenlive/lumas");
453     result = searchPath.path(KUrl::AddTrailingSlash) + KUrl(file).fileName();
454     if (QFile::exists(result))
455         return result;
456     return QString();
457 }
458
459 QString DocumentChecker::searchPathRecursively(const QDir &dir, const QString &fileName) const
460 {
461     QString foundFileName;
462     QStringList filters;
463     filters << fileName;
464     QDir searchDir(dir);
465     searchDir.setNameFilters(filters);
466     QStringList filesAndDirs = searchDir.entryList(QDir::Files | QDir::Readable);
467     if (!filesAndDirs.isEmpty()) return searchDir.absoluteFilePath(filesAndDirs.at(0));
468     searchDir.setNameFilters(QStringList());
469     filesAndDirs = searchDir.entryList(QDir::Dirs | QDir::Readable | QDir::Executable | QDir::NoDotAndDotDot);
470     for (int i = 0; i < filesAndDirs.size() && foundFileName.isEmpty(); i++) {
471         foundFileName = searchPathRecursively(searchDir.absoluteFilePath(filesAndDirs.at(i)), fileName);
472         if (!foundFileName.isEmpty())
473             break;
474     }
475     return foundFileName;
476 }
477
478 QString DocumentChecker::searchFileRecursively(const QDir &dir, const QString &matchSize, const QString &matchHash) const
479 {
480     QString foundFileName;
481     QByteArray fileData;
482     QByteArray fileHash;
483     QStringList filesAndDirs = dir.entryList(QDir::Files | QDir::Readable);
484     for (int i = 0; i < filesAndDirs.size() && foundFileName.isEmpty(); i++) {
485         QFile file(dir.absoluteFilePath(filesAndDirs.at(i)));
486         if (QString::number(file.size()) == matchSize) {
487             if (file.open(QIODevice::ReadOnly)) {
488                 /*
489                 * 1 MB = 1 second per 450 files (or faster)
490                 * 10 MB = 9 seconds per 450 files (or faster)
491                 */
492                 if (file.size() > 1000000 * 2) {
493                     fileData = file.read(1000000);
494                     if (file.seek(file.size() - 1000000))
495                         fileData.append(file.readAll());
496                 } else
497                     fileData = file.readAll();
498                 file.close();
499                 fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
500                 if (QString(fileHash.toHex()) == matchHash)
501                     return file.fileName();
502             }
503         }
504         //kDebug() << filesAndDirs.at(i) << file.size() << fileHash.toHex();
505     }
506     filesAndDirs = dir.entryList(QDir::Dirs | QDir::Readable | QDir::Executable | QDir::NoDotAndDotDot);
507     for (int i = 0; i < filesAndDirs.size() && foundFileName.isEmpty(); i++) {
508         foundFileName = searchFileRecursively(dir.absoluteFilePath(filesAndDirs.at(i)), matchSize, matchHash);
509         if (!foundFileName.isEmpty())
510             break;
511     }
512     return foundFileName;
513 }
514
515 void DocumentChecker::slotEditItem(QTreeWidgetItem *item, int)
516 {
517     int t = item->data(0, typeRole).toInt();
518     if (t == TITLE_FONT_ELEMENT) return;
519     //|| t == TITLE_IMAGE_ELEMENT) {
520
521     KUrl url = KUrlRequesterDialog::getUrl(item->text(1), m_dialog, i18n("Enter new location for file"));
522     if (url.isEmpty()) return;
523     item->setText(1, url.path());
524     if (KIO::NetAccess::exists(url, KIO::NetAccess::SourceSide, 0)) {
525         item->setIcon(0, KIcon("dialog-ok"));
526         int id = item->data(0, statusRole).toInt();
527         if (id < 10) item->setData(0, statusRole, CLIPOK);
528         else item->setData(0, statusRole, LUMAOK);
529         checkStatus();
530     } else {
531         item->setIcon(0, KIcon("dialog-close"));
532         int id = item->data(0, statusRole).toInt();
533         if (id < 10) item->setData(0, statusRole, CLIPMISSING);
534         else item->setData(0, statusRole, LUMAMISSING);
535         checkStatus();
536     }
537 }
538
539
540 void DocumentChecker::acceptDialog()
541 {
542     QDomElement e, property;
543     QDomNodeList producers = m_doc.elementsByTagName("producer");
544     QDomNodeList infoproducers = m_doc.elementsByTagName("kdenlive_producer");
545     QDomNodeList properties;
546     int ix = 0;
547
548     // prepare transitions
549     QDomNodeList trans = m_doc.elementsByTagName("transition");
550
551     // Mark document as modified
552     m_doc.documentElement().setAttribute("modified", 1);
553
554     QTreeWidgetItem *child = m_ui.treeWidget->topLevelItem(ix);
555     while (child) {
556         int t = child->data(0, typeRole).toInt();
557         if (child->data(0, statusRole).toInt() == CLIPOK) {
558             QString id = child->data(0, idRole).toString();
559             if (t == TITLE_IMAGE_ELEMENT) {
560                 // edit images embedded in titles
561                 for (int i = 0; i < infoproducers.count(); i++) {
562                     e = infoproducers.item(i).toElement();
563                     if (e.attribute("id") == id) {
564                         // Fix clip
565                         QString xml = e.attribute("xmldata");
566                         xml.replace(child->data(0, typeOriginalResource).toString(), child->text(1));
567                         e.setAttribute("xmldata", xml);
568                         break;
569                     }
570                 }
571                 for (int i = 0; i < producers.count(); i++) {
572                     e = producers.item(i).toElement();
573                     if (e.attribute("id").section('_', 0, 0) == id) {
574                         // Fix clip
575                         properties = e.childNodes();
576                         for (int j = 0; j < properties.count(); ++j) {
577                             property = properties.item(j).toElement();
578                             if (property.attribute("name") == "xmldata") {
579                                 QString xml = property.firstChild().nodeValue();
580                                 xml.replace(child->data(0, typeOriginalResource).toString(), child->text(1));
581                                 property.firstChild().setNodeValue(xml);
582                                 break;
583                             }
584                         }
585                     }
586                 }
587             } else {
588                 // edit clip url
589                 for (int i = 0; i < infoproducers.count(); i++) {
590                     e = infoproducers.item(i).toElement();
591                     if (e.attribute("id") == id) {
592                         // Fix clip
593                         e.setAttribute("resource", child->text(1));
594                         e.setAttribute("name", KUrl(child->text(1)).fileName());
595                         break;
596                     }
597                 }
598                 for (int i = 0; i < producers.count(); i++) {
599                     e = producers.item(i).toElement();
600                     if (e.attribute("id").section('_', 0, 0) == id || e.attribute("id").section(':', 1, 1) == id) {
601                         // Fix clip
602                         properties = e.childNodes();
603                         for (int j = 0; j < properties.count(); ++j) {
604                             property = properties.item(j).toElement();
605                             if (property.attribute("name") == "resource") {
606                                 QString resource = property.firstChild().nodeValue();
607                                 if (resource.contains(QRegExp("\\?[0-9]+\\.[0-9]+(&amp;strobe=[0-9]+)?$")))
608                                     property.firstChild().setNodeValue(child->text(1) + '?' + resource.section('?', -1));
609                                 else
610                                     property.firstChild().setNodeValue(child->text(1));
611                                 break;
612                             }
613                         }
614                     }
615                 }
616             }
617         } else if (child->data(0, statusRole).toInt() == CLIPPLACEHOLDER && t != TITLE_FONT_ELEMENT && t != TITLE_IMAGE_ELEMENT) {
618             QString id = child->data(0, idRole).toString();
619             for (int i = 0; i < infoproducers.count(); i++) {
620                 e = infoproducers.item(i).toElement();
621                 if (e.attribute("id") == id) {
622                     // Fix clip
623                     e.setAttribute("placeholder", '1');
624                     break;
625                 }
626             }
627         } else if (child->data(0, statusRole).toInt() == LUMAOK) {
628             for (int i = 0; i < trans.count(); i++) {
629                 QString luma = getProperty(trans.at(i).toElement(), "luma");
630                 
631                 kDebug() << "luma: " << luma;
632                 if (!luma.isEmpty() && luma == child->data(0, idRole).toString()) {
633                     setProperty(trans.at(i).toElement(), "luma", child->text(1));
634                     kDebug() << "replace with; " << child->text(1);
635                 }
636             }
637         } else if (child->data(0, statusRole).toInt() == LUMAMISSING) {
638             for (int i = 0; i < trans.count(); i++) {
639                 QString luma = getProperty(trans.at(i).toElement(), "luma");
640                 if (!luma.isEmpty() && luma == child->data(0, idRole).toString()) {
641                     setProperty(trans.at(i).toElement(), "luma", QString());
642                 }
643             }
644         }
645         ix++;
646         child = m_ui.treeWidget->topLevelItem(ix);
647     }
648     //QDialog::accept();
649 }
650
651 void DocumentChecker::slotPlaceholders()
652 {
653     int ix = 0;
654     QTreeWidgetItem *child = m_ui.treeWidget->topLevelItem(ix);
655     while (child) {
656         if (child->data(0, statusRole).toInt() == CLIPMISSING) {
657             child->setData(0, statusRole, CLIPPLACEHOLDER);
658             child->setIcon(0, KIcon("dialog-ok"));
659         } else if (child->data(0, statusRole).toInt() == LUMAMISSING) {
660             child->setData(0, statusRole, LUMAPLACEHOLDER);
661             child->setIcon(0, KIcon("dialog-ok"));
662         }
663         ix++;
664         child = m_ui.treeWidget->topLevelItem(ix);
665     }
666     checkStatus();
667 }
668
669 void DocumentChecker::slotFixDuration()
670 {
671     int ix = 0;
672     QTreeWidgetItem *child = m_ui.treeWidget->topLevelItem(ix);
673     QDomNodeList documentProducers = m_doc.elementsByTagName("producer");
674     while (child) {
675         if (child->data(0, statusRole).toInt() == CLIPWRONGDURATION) {
676             QString id = child->data(0, idRole).toString();
677             bool resetDuration = child->data(0, resetDurationRole).toInt();
678
679             for (int i = 0; i < m_info.count(); i++) {
680                 QDomElement e = m_info.at(i).toElement();
681                 if (e.attribute("id") == id) {
682                     if (m_missingClips.contains(e)) {
683                         // we cannot fix duration of missing clips
684                         resetDuration = false;
685                     }
686                     else {
687                         if (resetDuration) e.removeAttribute("duration");
688                         else e.setAttribute("duration", child->data(0, sizeRole).toString());
689                         child->setData(0, statusRole, CLIPOK);
690                         child->setIcon(0, KIcon("dialog-ok"));
691                     }
692                     break;
693                 }
694             }
695             if (resetDuration) {
696                 // something is wrong in clip durations, so remove them so mlt fetches them again
697                 for (int j = 0; j < documentProducers.count(); j++) {
698                     QDomElement mltProd = documentProducers.at(j).toElement();
699                     QString prodId = mltProd.attribute("id");
700                     if (prodId == id || prodId.startsWith(id + "_")) {
701                         EffectsList::removeProperty(mltProd, "length");
702                     }
703                 }
704             }
705         }
706         ix++;
707         child = m_ui.treeWidget->topLevelItem(ix);
708     }
709     QDomElement infoXml = m_doc.elementsByTagName("kdenlivedoc").at(0).toElement();
710     infoXml.setAttribute("modified", "1");
711     m_ui.fixDuration->setEnabled(false);
712     checkStatus();
713 }
714
715
716 void DocumentChecker::checkStatus()
717 {
718     bool status = true;
719     int ix = 0;
720     QTreeWidgetItem *child = m_ui.treeWidget->topLevelItem(ix);
721     while (child) {
722         int status = child->data(0, statusRole).toInt();
723         if (status == CLIPMISSING || status == LUMAMISSING || status == CLIPWRONGDURATION) {
724             status = false;
725             break;
726         }
727         ix++;
728         child = m_ui.treeWidget->topLevelItem(ix);
729     }
730     m_ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(status);
731 }
732
733
734 void DocumentChecker::slotDeleteSelected()
735 {
736     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)
737         return;
738     QStringList deletedIds;
739     QStringList deletedLumas;
740     QDomNodeList playlists = m_doc.elementsByTagName("playlist");
741
742     foreach(QTreeWidgetItem *child, m_ui.treeWidget->selectedItems()) {
743         int id = child->data(0, statusRole).toInt();
744         if (id == CLIPMISSING) {
745             deletedIds.append(child->data(0, idRole).toString());
746             delete child;
747         }
748         else if (id == LUMAMISSING) {
749             deletedLumas.append(child->data(0, idRole).toString());
750             delete child;
751         }
752     }
753
754     if (!deletedLumas.isEmpty()) {
755         QDomElement e;
756         QDomNodeList transitions = m_doc.elementsByTagName("transition");
757         foreach (QString lumaPath, deletedLumas) {
758             for (int i = 0; i < transitions.count(); i++) {
759                 e = transitions.item(i).toElement();
760                 QString resource = EffectsList::property(e, "luma");
761                 if (resource == lumaPath) EffectsList::removeProperty(e, "luma");
762             }
763         }
764     }
765
766     if (!deletedIds.isEmpty()) {
767         QDomElement e;
768         QDomNodeList producers = m_doc.elementsByTagName("producer");
769         QDomNodeList infoproducers = m_doc.elementsByTagName("kdenlive_producer");
770
771         QDomNode mlt = m_doc.elementsByTagName("mlt").at(0);
772         QDomNode kdenlivedoc = m_doc.elementsByTagName("kdenlivedoc").at(0);
773
774         for (int i = 0, j = 0; i < infoproducers.count() && j < deletedIds.count(); i++) {
775             e = infoproducers.item(i).toElement();
776             if (deletedIds.contains(e.attribute("id"))) {
777                 // Remove clip
778                 kdenlivedoc.removeChild(e);
779                 i--;
780                 j++;
781             }
782         }
783
784         for (int i = 0; i < producers.count(); i++) {
785             e = producers.item(i).toElement();
786             if (deletedIds.contains(e.attribute("id").section('_', 0, 0)) || deletedIds.contains(e.attribute("id").section(':', 1, 1).section('_', 0, 0))) {
787                 // Remove clip
788                 mlt.removeChild(e);
789                 i--;
790             }
791         }
792
793         for (int i = 0; i < playlists.count(); i++) {
794             QDomNodeList entries = playlists.at(i).toElement().elementsByTagName("entry");
795             for (int j = 0; j < entries.count(); j++) {
796                 e = entries.item(j).toElement();
797                 if (deletedIds.contains(e.attribute("producer").section('_', 0, 0)) || deletedIds.contains(e.attribute("producer").section(':', 1, 1).section('_', 0, 0))) {
798                     // Replace clip with blank
799                     while (e.childNodes().count() > 0)
800                         e.removeChild(e.firstChild());
801                     e.setTagName("blank");
802                     e.removeAttribute("producer");
803                     int length = e.attribute("out").toInt() - e.attribute("in").toInt();
804                     e.setAttribute("length", length);
805                     j--;
806                 }
807             }
808         }
809         QDomElement infoXml = m_doc.elementsByTagName("kdenlivedoc").at(0).toElement();
810         infoXml.setAttribute("modified", "1");
811         checkStatus();
812     }
813 }
814
815 void DocumentChecker::checkMissingImages(QStringList images, QStringList fonts, QString id, QString baseClip)
816 {
817     QDomDocument doc;
818     foreach(const QString &img, images) {
819         if (!KIO::NetAccess::exists(KUrl(img), KIO::NetAccess::SourceSide, 0)) {
820             QDomElement e = doc.createElement("missingclip");
821             e.setAttribute("type", TITLE_IMAGE_ELEMENT);
822             e.setAttribute("resource", img);
823             e.setAttribute("id", id);
824             e.setAttribute("name", baseClip);
825             m_missingClips.append(e);
826         }
827     }
828     kDebug() << "/ / / CHK FONTS: " << fonts;
829     foreach(const QString &fontelement, fonts) {
830         QFont f(fontelement);
831         kDebug() << "/ / / CHK FONTS: " << fontelement << " = " << QFontInfo(f).family();
832         if (fontelement != QFontInfo(f).family()) {
833             QDomElement e = doc.createElement("missingclip");
834             e.setAttribute("type", TITLE_FONT_ELEMENT);
835             e.setAttribute("resource", fontelement);
836             e.setAttribute("id", id);
837             e.setAttribute("name", baseClip);
838             m_missingClips.append(e);
839         }
840     }
841 }
842
843
844 void DocumentChecker::slotCheckButtons()
845 {
846     if (m_ui.treeWidget->currentItem()) {
847         QTreeWidgetItem *item = m_ui.treeWidget->currentItem();
848         int t = item->data(0, typeRole).toInt();
849         int s = item->data(0, statusRole).toInt();
850         if (t == TITLE_FONT_ELEMENT || t == TITLE_IMAGE_ELEMENT || s == PROXYMISSING) {
851             m_ui.removeSelected->setEnabled(false);
852         } else m_ui.removeSelected->setEnabled(true);
853     }
854
855 }
856
857 #include "documentchecker.moc"
858
859