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