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