1 /***************************************************************************
2 * Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
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. *
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. *
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 ***************************************************************************/
21 #include "documentchecker.h"
23 #include "docclipbase.h"
24 #include "titlewidget.h"
25 #include "definitions.h"
26 #include "kdenlivesettings.h"
29 #include <KGlobalSettings>
31 #include <KIO/NetAccess>
32 #include <KFileDialog>
33 #include <KApplication>
34 #include <KUrlRequesterDialog>
35 #include <KMessageBox>
37 #include <QTreeWidgetItem>
39 #include <QHeaderView>
43 #include <QCryptographicHash>
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;
52 const int CLIPMISSING = 0;
54 const int CLIPPLACEHOLDER = 2;
55 const int LUMAMISSING = 10;
56 const int LUMAOK = 11;
57 const int LUMAPLACEHOLDER = 12;
59 enum TITLECLIPTYPE { TITLE_IMAGE_ELEMENT = 20, TITLE_FONT_ELEMENT = 21 };
61 DocumentChecker::DocumentChecker(QDomNodeList infoproducers, QDomDocument doc):
62 m_info(infoproducers), m_doc(doc), m_dialog(NULL)
68 bool DocumentChecker::hasMissingClips()
74 QList <QDomElement> missingClips;
75 for (int i = 0; i < m_info.count(); i++) {
76 e = m_info.item(i).toElement();
77 clipType = e.attribute("type").toInt();
78 if (clipType == COLOR) continue;
79 if (clipType == TEXT) {
80 //TODO: Check is clip template is missing (xmltemplate) or hash changed
81 QStringList images = TitleWidget::extractImageList(e.attribute("xmldata"));
82 QStringList fonts = TitleWidget::extractFontList(e.attribute("xmldata"));
83 checkMissingImages(missingClips, images, fonts, e.attribute("id"), e.attribute("name"));
86 id = e.attribute("id");
87 resource = e.attribute("resource");
88 if (clipType == SLIDESHOW) resource = KUrl(resource).directory();
89 if (!KIO::NetAccess::exists(KUrl(resource), KIO::NetAccess::SourceSide, 0)) {
91 missingClips.append(e);
93 // Check if the clip has changed
94 if (clipType != SLIDESHOW && e.hasAttribute("file_hash")) {
95 if (e.attribute("file_hash") != DocClipBase::getHash(e.attribute("resource")))
96 e.removeAttribute("file_hash");
101 QStringList missingLumas;
102 QString root = m_doc.documentElement().attribute("root");
103 if (!root.isEmpty()) root = KUrl(root).path(KUrl::AddTrailingSlash);
104 QDomNodeList trans = m_doc.elementsByTagName("transition");
105 for (int i = 0; i < trans.count(); i++) {
106 QString luma = getProperty(trans.at(i).toElement(), "luma");
107 if (!luma.isEmpty()) {
108 if (!luma.startsWith('/')) luma.prepend(root);
109 if (!QFile::exists(luma) && !missingLumas.contains(luma)) {
110 missingLumas.append(luma);
115 if (missingClips.isEmpty() && missingLumas.isEmpty())
118 m_dialog = new QDialog();
119 m_dialog->setFont(KGlobalSettings::toolBarFont());
120 m_ui.setupUi(m_dialog);
122 foreach(const QString l, missingLumas) {
123 QTreeWidgetItem *item = new QTreeWidgetItem(m_ui.treeWidget, QStringList() << i18n("Luma file") << l);
124 item->setIcon(0, KIcon("dialog-close"));
125 item->setData(0, idRole, l);
126 item->setData(0, statusRole, LUMAMISSING);
129 m_ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
130 for (int i = 0; i < missingClips.count(); i++) {
131 e = missingClips.at(i).toElement();
133 int t = e.attribute("type").toInt();
136 clipType = i18n("Video clip");
139 clipType = i18n("Mute video clip");
142 clipType = i18n("Audio clip");
145 clipType = i18n("Playlist clip");
148 clipType = i18n("Image clip");
151 clipType = i18n("Slideshow clip");
153 case TITLE_IMAGE_ELEMENT:
154 clipType = i18n("Title Image");
156 case TITLE_FONT_ELEMENT:
157 clipType = i18n("Title Font");
160 clipType = i18n("Video clip");
162 QTreeWidgetItem *item = new QTreeWidgetItem(m_ui.treeWidget, QStringList() << clipType);
163 if (t == TITLE_IMAGE_ELEMENT) {
164 item->setIcon(0, KIcon("dialog-warning"));
165 item->setToolTip(1, e.attribute("name"));
166 item->setText(1, e.attribute("resource"));
167 item->setData(0, statusRole, CLIPPLACEHOLDER);
168 item->setData(0, typeOriginalResource, e.attribute("resource"));
169 } else if (t == TITLE_FONT_ELEMENT) {
170 item->setIcon(0, KIcon("dialog-warning"));
171 item->setToolTip(1, e.attribute("name"));
172 QString ft = e.attribute("resource");
173 QString newft = QFontInfo(QFont(ft)).family();
174 item->setText(1, i18n("%1 will be replaced by %2", ft, newft));
175 item->setData(0, statusRole, CLIPPLACEHOLDER);
177 item->setIcon(0, KIcon("dialog-close"));
178 item->setText(1, e.attribute("resource"));
179 item->setData(0, hashRole, e.attribute("file_hash"));
180 item->setData(0, sizeRole, e.attribute("file_size"));
181 item->setData(0, statusRole, CLIPMISSING);
183 item->setData(0, typeRole, t);
184 item->setData(0, idRole, e.attribute("id"));
186 connect(m_ui.recursiveSearch, SIGNAL(pressed()), this, SLOT(slotSearchClips()));
187 connect(m_ui.usePlaceholders, SIGNAL(pressed()), this, SLOT(slotPlaceholders()));
188 connect(m_ui.removeSelected, SIGNAL(pressed()), this, SLOT(slotDeleteSelected()));
189 connect(m_ui.treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, SLOT(slotEditItem(QTreeWidgetItem *, int)));
190 connect(m_ui.treeWidget, SIGNAL(itemSelectionChanged()), this, SLOT(slotCheckButtons()));
192 if (m_ui.treeWidget->topLevelItem(0)) m_ui.treeWidget->setCurrentItem(m_ui.treeWidget->topLevelItem(0));
194 int acceptMissing = m_dialog->exec();
195 if (acceptMissing == QDialog::Accepted) acceptDialog();
196 return (acceptMissing != QDialog::Accepted);
199 DocumentChecker::~DocumentChecker()
201 if (m_dialog) delete m_dialog;
205 QString DocumentChecker::getProperty(QDomElement effect, const QString &name)
207 QDomNodeList params = effect.elementsByTagName("property");
208 for (int i = 0; i < params.count(); i++) {
209 QDomElement e = params.item(i).toElement();
210 if (e.attribute("name") == name) {
211 return e.firstChild().nodeValue();
217 void DocumentChecker::setProperty(QDomElement effect, const QString &name, const QString value)
219 QDomNodeList params = effect.elementsByTagName("property");
220 for (int i = 0; i < params.count(); i++) {
221 QDomElement e = params.item(i).toElement();
222 if (e.attribute("name") == name) {
223 e.firstChild().setNodeValue(value);
228 void DocumentChecker::slotSearchClips()
230 QString newpath = KFileDialog::getExistingDirectory(KUrl("kfiledialog:///clipfolder"), kapp->activeWindow(), i18n("Clips folder"));
231 if (newpath.isEmpty()) return;
233 m_ui.recursiveSearch->setEnabled(false);
234 QTreeWidgetItem *child = m_ui.treeWidget->topLevelItem(ix);
236 if (child->data(0, statusRole).toInt() == CLIPMISSING) {
237 QString clipPath = searchFileRecursively(QDir(newpath), child->data(0, sizeRole).toString(), child->data(0, hashRole).toString());
238 if (!clipPath.isEmpty()) {
239 child->setText(1, clipPath);
240 child->setIcon(0, KIcon("dialog-ok"));
241 child->setData(0, statusRole, CLIPOK);
243 } else if (child->data(0, statusRole).toInt() == LUMAMISSING) {
244 QString fileName = searchLuma(child->data(0, idRole).toString());
245 if (!fileName.isEmpty()) {
246 child->setText(1, fileName);
247 child->setIcon(0, KIcon("dialog-ok"));
248 child->setData(0, statusRole, LUMAOK);
252 child = m_ui.treeWidget->topLevelItem(ix);
254 m_ui.recursiveSearch->setEnabled(true);
259 QString DocumentChecker::searchLuma(QString file) const
261 KUrl searchPath(KdenliveSettings::mltpath());
262 if (file.contains("PAL"))
263 searchPath.cd("../lumas/PAL");
265 searchPath.cd("../lumas/NTSC");
266 QString result = searchPath.path(KUrl::AddTrailingSlash) + KUrl(file).fileName();
267 if (QFile::exists(result))
273 QString DocumentChecker::searchFileRecursively(const QDir &dir, const QString &matchSize, const QString &matchHash) const
275 QString foundFileName;
278 QStringList filesAndDirs = dir.entryList(QDir::Files | QDir::Readable);
279 for (int i = 0; i < filesAndDirs.size() && foundFileName.isEmpty(); i++) {
280 QFile file(dir.absoluteFilePath(filesAndDirs.at(i)));
281 if (QString::number(file.size()) == matchSize) {
282 if (file.open(QIODevice::ReadOnly)) {
284 * 1 MB = 1 second per 450 files (or faster)
285 * 10 MB = 9 seconds per 450 files (or faster)
287 if (file.size() > 1000000 * 2) {
288 fileData = file.read(1000000);
289 if (file.seek(file.size() - 1000000))
290 fileData.append(file.readAll());
292 fileData = file.readAll();
294 fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
295 if (QString(fileHash.toHex()) == matchHash)
296 return file.fileName();
299 //kDebug() << filesAndDirs.at(i) << file.size() << fileHash.toHex();
301 filesAndDirs = dir.entryList(QDir::Dirs | QDir::Readable | QDir::Executable | QDir::NoDotAndDotDot);
302 for (int i = 0; i < filesAndDirs.size() && foundFileName.isEmpty(); i++) {
303 foundFileName = searchFileRecursively(dir.absoluteFilePath(filesAndDirs.at(i)), matchSize, matchHash);
304 if (!foundFileName.isEmpty())
307 return foundFileName;
310 void DocumentChecker::slotEditItem(QTreeWidgetItem *item, int)
312 int t = item->data(0, typeRole).toInt();
313 if (t == TITLE_FONT_ELEMENT) return;
314 //|| t == TITLE_IMAGE_ELEMENT) {
316 KUrl url = KUrlRequesterDialog::getUrl(item->text(1), m_dialog, i18n("Enter new location for file"));
317 if (url.isEmpty()) return;
318 item->setText(1, url.path());
319 if (KIO::NetAccess::exists(url, KIO::NetAccess::SourceSide, 0)) {
320 item->setIcon(0, KIcon("dialog-ok"));
321 int id = item->data(0, statusRole).toInt();
322 if (id < 10) item->setData(0, statusRole, CLIPOK);
323 else item->setData(0, statusRole, LUMAOK);
326 item->setIcon(0, KIcon("dialog-close"));
327 int id = item->data(0, statusRole).toInt();
328 if (id < 10) item->setData(0, statusRole, CLIPMISSING);
329 else item->setData(0, statusRole, LUMAMISSING);
335 void DocumentChecker::acceptDialog()
337 QDomElement e, property;
338 QDomNodeList producers = m_doc.elementsByTagName("producer");
339 QDomNodeList infoproducers = m_doc.elementsByTagName("kdenlive_producer");
340 QDomNodeList properties;
343 // prepare transitions
344 QDomNodeList trans = m_doc.elementsByTagName("transition");
346 // Mark document as modified
347 m_doc.documentElement().setAttribute("modified", 1);
349 QTreeWidgetItem *child = m_ui.treeWidget->topLevelItem(ix);
351 int t = child->data(0, typeRole).toInt();
352 if (child->data(0, statusRole).toInt() == CLIPOK) {
353 QString id = child->data(0, idRole).toString();
354 if (t == TITLE_IMAGE_ELEMENT) {
355 // edit images embedded in titles
356 for (int i = 0; i < infoproducers.count(); i++) {
357 e = infoproducers.item(i).toElement();
358 if (e.attribute("id") == id) {
360 QString xml = e.attribute("xmldata");
361 xml.replace(child->data(0, typeOriginalResource).toString(), child->text(1));
362 e.setAttribute("xmldata", xml);
366 for (int i = 0; i < producers.count(); i++) {
367 e = producers.item(i).toElement();
368 if (e.attribute("id").section('_', 0, 0) == id) {
370 properties = e.childNodes();
371 for (int j = 0; j < properties.count(); ++j) {
372 property = properties.item(j).toElement();
373 if (property.attribute("name") == "xmldata") {
374 QString xml = property.firstChild().nodeValue();
375 xml.replace(child->data(0, typeOriginalResource).toString(), child->text(1));
376 property.firstChild().setNodeValue(xml);
384 for (int i = 0; i < infoproducers.count(); i++) {
385 e = infoproducers.item(i).toElement();
386 if (e.attribute("id") == id) {
388 e.setAttribute("resource", child->text(1));
389 e.setAttribute("name", KUrl(child->text(1)).fileName());
393 for (int i = 0; i < producers.count(); i++) {
394 e = producers.item(i).toElement();
395 if (e.attribute("id").section('_', 0, 0) == id || e.attribute("id").section(':', 1, 1) == id) {
397 properties = e.childNodes();
398 for (int j = 0; j < properties.count(); ++j) {
399 property = properties.item(j).toElement();
400 if (property.attribute("name") == "resource") {
401 QString resource = property.firstChild().nodeValue();
402 if (resource.contains(QRegExp("\\?[0-9]+\\.[0-9]+(&strobe=[0-9]+)?$")))
403 property.firstChild().setNodeValue(child->text(1) + '?' + resource.section('?', -1));
405 property.firstChild().setNodeValue(child->text(1));
412 } else if (child->data(0, statusRole).toInt() == CLIPPLACEHOLDER && t != TITLE_FONT_ELEMENT && t != TITLE_IMAGE_ELEMENT) {
413 QString id = child->data(0, idRole).toString();
414 for (int i = 0; i < infoproducers.count(); i++) {
415 e = infoproducers.item(i).toElement();
416 if (e.attribute("id") == id) {
418 e.setAttribute("placeholder", '1');
422 } else if (child->data(0, statusRole).toInt() == LUMAOK) {
423 for (int i = 0; i < trans.count(); i++) {
424 QString luma = getProperty(trans.at(i).toElement(), "luma");
425 kDebug() << "luma: " << luma;
426 if (!luma.isEmpty() && luma == child->data(0, idRole).toString()) {
427 setProperty(trans.at(i).toElement(), "luma", child->text(1));
428 kDebug() << "replace with; " << child->text(1);
431 } else if (child->data(0, statusRole).toInt() == LUMAMISSING) {
432 for (int i = 0; i < trans.count(); i++) {
433 QString luma = getProperty(trans.at(i).toElement(), "luma");
434 if (!luma.isEmpty() && luma == child->data(0, idRole).toString()) {
435 setProperty(trans.at(i).toElement(), "luma", QString());
440 child = m_ui.treeWidget->topLevelItem(ix);
445 void DocumentChecker::slotPlaceholders()
448 QTreeWidgetItem *child = m_ui.treeWidget->topLevelItem(ix);
450 if (child->data(0, statusRole).toInt() == CLIPMISSING) {
451 child->setData(0, statusRole, CLIPPLACEHOLDER);
452 child->setIcon(0, KIcon("dialog-ok"));
453 } else if (child->data(0, statusRole).toInt() == LUMAMISSING) {
454 child->setData(0, statusRole, LUMAPLACEHOLDER);
455 child->setIcon(0, KIcon("dialog-ok"));
458 child = m_ui.treeWidget->topLevelItem(ix);
464 void DocumentChecker::checkStatus()
468 QTreeWidgetItem *child = m_ui.treeWidget->topLevelItem(ix);
470 if (child->data(0, statusRole).toInt() == CLIPMISSING || child->data(0, statusRole).toInt() == LUMAMISSING) {
475 child = m_ui.treeWidget->topLevelItem(ix);
477 m_ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(status);
481 void DocumentChecker::slotDeleteSelected()
483 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)
485 QStringList deletedIds;
486 QDomNodeList playlists = m_doc.elementsByTagName("playlist");
488 foreach(QTreeWidgetItem *child, m_ui.treeWidget->selectedItems()) {
489 if (child->data(0, statusRole).toInt() < 10) {
490 deletedIds.append(child->data(0, idRole).toString());
494 kDebug() << "// Clips to delete: " << deletedIds;
496 if (!deletedIds.isEmpty()) {
498 QDomNodeList producers = m_doc.elementsByTagName("producer");
499 QDomNodeList infoproducers = m_doc.elementsByTagName("kdenlive_producer");
501 QDomElement mlt = m_doc.firstChildElement("mlt");
502 QDomElement kdenlivedoc = mlt.firstChildElement("kdenlivedoc");
504 for (int i = 0, j = 0; i < infoproducers.count() && j < deletedIds.count(); i++) {
505 e = infoproducers.item(i).toElement();
506 if (deletedIds.contains(e.attribute("id"))) {
508 kdenlivedoc.removeChild(e);
514 for (int i = 0; i < producers.count(); i++) {
515 e = producers.item(i).toElement();
516 if (deletedIds.contains(e.attribute("id").section('_', 0, 0)) || deletedIds.contains(e.attribute("id").section(':', 1, 1).section('_', 0, 0))) {
523 for (int i = 0; i < playlists.count(); i++) {
524 QDomNodeList entries = playlists.at(i).toElement().elementsByTagName("entry");
525 for (int j = 0; j < entries.count(); j++) {
526 e = entries.item(j).toElement();
527 if (deletedIds.contains(e.attribute("producer").section('_', 0, 0)) || deletedIds.contains(e.attribute("producer").section(':', 1, 1).section('_', 0, 0))) {
528 // Replace clip with blank
529 while (e.childNodes().count() > 0)
530 e.removeChild(e.firstChild());
531 e.setTagName("blank");
532 e.removeAttribute("producer");
533 int length = e.attribute("out").toInt() - e.attribute("in").toInt();
534 e.setAttribute("length", length);
543 void DocumentChecker::checkMissingImages(QList <QDomElement>&missingClips, QStringList images, QStringList fonts, QString id, QString baseClip)
546 foreach(const QString &img, images) {
547 if (!KIO::NetAccess::exists(KUrl(img), KIO::NetAccess::SourceSide, 0)) {
548 QDomElement e = doc.createElement("missingclip");
549 e.setAttribute("type", TITLE_IMAGE_ELEMENT);
550 e.setAttribute("resource", img);
551 e.setAttribute("id", id);
552 e.setAttribute("name", baseClip);
553 missingClips.append(e);
556 kDebug() << "/ / / CHK FONTS: " << fonts;
557 foreach(const QString &fontelement, fonts) {
558 QFont f(fontelement);
559 kDebug() << "/ / / CHK FONTS: " << fontelement << " = " << QFontInfo(f).family();
560 if (fontelement != QFontInfo(f).family()) {
561 QDomElement e = doc.createElement("missingclip");
562 e.setAttribute("type", TITLE_FONT_ELEMENT);
563 e.setAttribute("resource", fontelement);
564 e.setAttribute("id", id);
565 e.setAttribute("name", baseClip);
566 missingClips.append(e);
572 void DocumentChecker::slotCheckButtons()
574 if (m_ui.treeWidget->currentItem()) {
575 QTreeWidgetItem *item = m_ui.treeWidget->currentItem();
576 int t = item->data(0, typeRole).toInt();
577 if (t == TITLE_FONT_ELEMENT || t == TITLE_IMAGE_ELEMENT) {
578 m_ui.removeSelected->setEnabled(false);
579 } else m_ui.removeSelected->setEnabled(true);
584 #include "documentchecker.moc"