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 QTreeWidgetItem *child = m_ui.treeWidget->topLevelItem(ix);
348 int t = child->data(0, typeRole).toInt();
349 if (child->data(0, statusRole).toInt() == CLIPOK) {
350 QString id = child->data(0, idRole).toString();
351 if (t == TITLE_IMAGE_ELEMENT) {
352 // edit images embedded in titles
353 for (int i = 0; i < infoproducers.count(); i++) {
354 e = infoproducers.item(i).toElement();
355 if (e.attribute("id") == id) {
357 QString xml = e.attribute("xmldata");
358 xml.replace(child->data(0, typeOriginalResource).toString(), child->text(1));
359 e.setAttribute("xmldata", xml);
363 for (int i = 0; i < producers.count(); i++) {
364 e = producers.item(i).toElement();
365 if (e.attribute("id").section('_', 0, 0) == id) {
367 properties = e.childNodes();
368 for (int j = 0; j < properties.count(); ++j) {
369 property = properties.item(j).toElement();
370 if (property.attribute("name") == "xmldata") {
371 QString xml = property.firstChild().nodeValue();
372 xml.replace(child->data(0, typeOriginalResource).toString(), child->text(1));
373 property.firstChild().setNodeValue(xml);
381 for (int i = 0; i < infoproducers.count(); i++) {
382 e = infoproducers.item(i).toElement();
383 if (e.attribute("id") == id) {
385 e.setAttribute("resource", child->text(1));
386 e.setAttribute("name", KUrl(child->text(1)).fileName());
390 for (int i = 0; i < producers.count(); i++) {
391 e = producers.item(i).toElement();
392 if (e.attribute("id").section('_', 0, 0) == id || e.attribute("id").section(':', 1, 1) == id) {
394 properties = e.childNodes();
395 for (int j = 0; j < properties.count(); ++j) {
396 property = properties.item(j).toElement();
397 if (property.attribute("name") == "resource") {
398 QString resource = property.firstChild().nodeValue();
399 if (resource.contains(QRegExp("\\?[0-9]+\\.[0-9]+(&strobe=[0-9]+)?$")))
400 property.firstChild().setNodeValue(child->text(1) + '?' + resource.section('?', -1));
402 property.firstChild().setNodeValue(child->text(1));
409 } else if (child->data(0, statusRole).toInt() == CLIPPLACEHOLDER && t != TITLE_FONT_ELEMENT && t != TITLE_IMAGE_ELEMENT) {
410 QString id = child->data(0, idRole).toString();
411 for (int i = 0; i < infoproducers.count(); i++) {
412 e = infoproducers.item(i).toElement();
413 if (e.attribute("id") == id) {
415 e.setAttribute("placeholder", '1');
419 } else if (child->data(0, statusRole).toInt() == LUMAOK) {
420 for (int i = 0; i < trans.count(); i++) {
421 QString luma = getProperty(trans.at(i).toElement(), "luma");
422 kDebug() << "luma: " << luma;
423 if (!luma.isEmpty() && luma == child->data(0, idRole).toString()) {
424 setProperty(trans.at(i).toElement(), "luma", child->text(1));
425 kDebug() << "replace with; " << child->text(1);
428 } else if (child->data(0, statusRole).toInt() == LUMAMISSING) {
429 for (int i = 0; i < trans.count(); i++) {
430 QString luma = getProperty(trans.at(i).toElement(), "luma");
431 if (!luma.isEmpty() && luma == child->data(0, idRole).toString()) {
432 setProperty(trans.at(i).toElement(), "luma", QString());
437 child = m_ui.treeWidget->topLevelItem(ix);
442 void DocumentChecker::slotPlaceholders()
445 QTreeWidgetItem *child = m_ui.treeWidget->topLevelItem(ix);
447 if (child->data(0, statusRole).toInt() == CLIPMISSING) {
448 child->setData(0, statusRole, CLIPPLACEHOLDER);
449 child->setIcon(0, KIcon("dialog-ok"));
450 } else if (child->data(0, statusRole).toInt() == LUMAMISSING) {
451 child->setData(0, statusRole, LUMAPLACEHOLDER);
452 child->setIcon(0, KIcon("dialog-ok"));
455 child = m_ui.treeWidget->topLevelItem(ix);
461 void DocumentChecker::checkStatus()
465 QTreeWidgetItem *child = m_ui.treeWidget->topLevelItem(ix);
467 if (child->data(0, statusRole).toInt() == CLIPMISSING || child->data(0, statusRole).toInt() == LUMAMISSING) {
472 child = m_ui.treeWidget->topLevelItem(ix);
474 m_ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(status);
478 void DocumentChecker::slotDeleteSelected()
480 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)
482 QStringList deletedIds;
483 QDomNodeList playlists = m_doc.elementsByTagName("playlist");
485 foreach(QTreeWidgetItem *child, m_ui.treeWidget->selectedItems()) {
486 if (child->data(0, statusRole).toInt() < 10) {
487 deletedIds.append(child->data(0, idRole).toString());
491 kDebug() << "// Clips to delete: " << deletedIds;
493 if (!deletedIds.isEmpty()) {
495 QDomNodeList producers = m_doc.elementsByTagName("producer");
496 QDomNodeList infoproducers = m_doc.elementsByTagName("kdenlive_producer");
498 QDomElement mlt = m_doc.firstChildElement("mlt");
499 QDomElement kdenlivedoc = mlt.firstChildElement("kdenlivedoc");
501 for (int i = 0, j = 0; i < infoproducers.count() && j < deletedIds.count(); i++) {
502 e = infoproducers.item(i).toElement();
503 if (deletedIds.contains(e.attribute("id"))) {
505 kdenlivedoc.removeChild(e);
511 for (int i = 0; i < producers.count(); i++) {
512 e = producers.item(i).toElement();
513 if (deletedIds.contains(e.attribute("id").section('_', 0, 0)) || deletedIds.contains(e.attribute("id").section(':', 1, 1).section('_', 0, 0))) {
520 for (int i = 0; i < playlists.count(); i++) {
521 QDomNodeList entries = playlists.at(i).toElement().elementsByTagName("entry");
522 for (int j = 0; j < entries.count(); j++) {
523 e = entries.item(j).toElement();
524 if (deletedIds.contains(e.attribute("producer").section('_', 0, 0)) || deletedIds.contains(e.attribute("producer").section(':', 1, 1).section('_', 0, 0))) {
525 // Replace clip with blank
526 while (e.childNodes().count() > 0)
527 e.removeChild(e.firstChild());
528 e.setTagName("blank");
529 e.removeAttribute("producer");
530 int length = e.attribute("out").toInt() - e.attribute("in").toInt();
531 e.setAttribute("length", length);
540 void DocumentChecker::checkMissingImages(QList <QDomElement>&missingClips, QStringList images, QStringList fonts, QString id, QString baseClip)
543 foreach(const QString &img, images) {
544 if (!KIO::NetAccess::exists(KUrl(img), KIO::NetAccess::SourceSide, 0)) {
545 QDomElement e = doc.createElement("missingclip");
546 e.setAttribute("type", TITLE_IMAGE_ELEMENT);
547 e.setAttribute("resource", img);
548 e.setAttribute("id", id);
549 e.setAttribute("name", baseClip);
550 missingClips.append(e);
553 kDebug() << "/ / / CHK FONTS: " << fonts;
554 foreach(const QString &fontelement, fonts) {
555 QFont f(fontelement);
556 kDebug() << "/ / / CHK FONTS: " << fontelement << " = " << QFontInfo(f).family();
557 if (fontelement != QFontInfo(f).family()) {
558 QDomElement e = doc.createElement("missingclip");
559 e.setAttribute("type", TITLE_FONT_ELEMENT);
560 e.setAttribute("resource", fontelement);
561 e.setAttribute("id", id);
562 e.setAttribute("name", baseClip);
563 missingClips.append(e);
569 void DocumentChecker::slotCheckButtons()
571 if (m_ui.treeWidget->currentItem()) {
572 QTreeWidgetItem *item = m_ui.treeWidget->currentItem();
573 int t = item->data(0, typeRole).toInt();
574 if (t == TITLE_FONT_ELEMENT || t == TITLE_IMAGE_ELEMENT) {
575 m_ui.removeSelected->setEnabled(false);
576 } else m_ui.removeSelected->setEnabled(true);
581 #include "documentchecker.moc"