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