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 "definitions.h"
26 #include <KGlobalSettings>
28 #include <KIO/NetAccess>
29 #include <KFileDialog>
30 #include <KApplication>
31 #include <KUrlRequesterDialog>
33 #include <QTreeWidgetItem>
35 #include <QHeaderView>
39 #include <QCryptographicHash>
41 const int hashRole = Qt::UserRole;
42 const int sizeRole = Qt::UserRole + 1;
43 const int idRole = Qt::UserRole + 2;
44 const int statusRole = Qt::UserRole + 3;
46 const int CLIPMISSING = 0;
48 const int CLIPPLACEHOLDER = 2;
50 DocumentChecker::DocumentChecker(QDomDocument doc, QWidget * parent) :
51 QDialog(parent), m_doc(doc)
53 setFont(KGlobalSettings::toolBarFont());
56 QDomNodeList producers = m_doc.elementsByTagName("producer");
57 QDomNodeList infoproducers = m_doc.elementsByTagName("kdenlive_producer");
63 QList <QDomElement> missingClips;
64 for (int i = 0; i < infoproducers.count(); i++) {
65 e = infoproducers.item(i).toElement();
66 clipType = e.attribute("type").toInt();
67 if (clipType == TEXT) continue;
68 id = e.attribute("id");
69 resource = e.attribute("resource");
70 if (clipType == SLIDESHOW) resource = KUrl(resource).directory();
71 if (!KIO::NetAccess::exists(KUrl(resource), KIO::NetAccess::SourceSide, 0)) {
73 missingClips.append(e);
77 if (missingClips.isEmpty()) QTimer::singleShot(0, this, SLOT(accept()));
78 m_view.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
79 for (int i = 0; i < missingClips.count(); i++) {
80 e = missingClips.at(i).toElement();
82 switch (e.attribute("type").toInt()) {
84 clipType = i18n("Video clip");
87 clipType = i18n("Mute video clip");
90 clipType = i18n("Audio clip");
93 clipType = i18n("Playlist clip");
96 clipType = i18n("Image clip");
99 clipType = i18n("Slideshow clip");
102 clipType = i18n("Video clip");
104 QTreeWidgetItem *item = new QTreeWidgetItem(m_view.treeWidget, QStringList() << clipType << e.attribute("resource"));
105 item->setIcon(0, KIcon("dialog-close"));
106 item->setData(0, hashRole, e.attribute("file_hash"));
107 item->setData(0, sizeRole, e.attribute("file_size"));
108 item->setData(0, idRole, e.attribute("id"));
109 item->setData(0, statusRole, CLIPMISSING);
111 connect(m_view.recursiveSearch, SIGNAL(pressed()), this, SLOT(slotSearchClips()));
112 connect(m_view.usePlaceholders, SIGNAL(pressed()), this, SLOT(slotPlaceholders()));
113 connect(m_view.treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, SLOT(slotEditItem(QTreeWidgetItem *, int)));
117 DocumentChecker::~DocumentChecker() {}
119 void DocumentChecker::slotSearchClips()
121 QString newpath = KFileDialog::getExistingDirectory(KUrl("kfiledialog:///clipfolder"), kapp->activeWindow(), i18n("Clips folder"));
122 if (newpath.isEmpty()) return;
124 QTreeWidgetItem *child = m_view.treeWidget->topLevelItem(ix);
125 while (child && child->data(0, statusRole).toInt() == CLIPMISSING) {
126 QString clipPath = searchFileRecursively(QDir(newpath), child->data(0, sizeRole).toString(), child->data(0, hashRole).toString());
127 if (!clipPath.isEmpty()) {
128 child->setText(1, clipPath);
129 child->setIcon(0, KIcon("dialog-ok"));
130 child->setData(0, statusRole, CLIPOK);
133 child = m_view.treeWidget->topLevelItem(ix);
138 QString DocumentChecker::searchFileRecursively(const QDir &dir, const QString &matchSize, const QString &matchHash) const
140 QString foundFileName;
143 QStringList filesAndDirs = dir.entryList(QDir::Files | QDir::Readable);
144 for (int i = 0; i < filesAndDirs.size() && foundFileName.isEmpty(); i++) {
145 QFile file(dir.absoluteFilePath(filesAndDirs.at(i)));
146 if (file.open(QIODevice::ReadOnly)) {
147 if (QString::number(file.size()) == matchSize) {
149 * 1 MB = 1 second per 450 files (or faster)
150 * 10 MB = 9 seconds per 450 files (or faster)
152 if (file.size() > 1000000*2) {
153 fileData = file.read(1000000);
154 if (file.seek(file.size() - 1000000))
155 fileData.append(file.readAll());
157 fileData = file.readAll();
159 fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
160 if (QString(fileHash.toHex()) == matchHash)
161 return file.fileName();
164 kDebug() << filesAndDirs.at(i) << file.size() << fileHash.toHex();
166 filesAndDirs = dir.entryList(QDir::Dirs | QDir::Readable | QDir::Executable | QDir::NoDotAndDotDot);
167 for (int i = 0; i < filesAndDirs.size() && foundFileName.isEmpty(); i++) {
168 foundFileName = searchFileRecursively(dir.absoluteFilePath(filesAndDirs.at(i)), matchSize, matchHash);
169 if (!foundFileName.isEmpty())
172 return foundFileName;
175 void DocumentChecker::slotEditItem(QTreeWidgetItem *item, int)
177 KUrl url = KUrlRequesterDialog::getUrl(item->text(1), this, i18n("Enter new location for file"));
178 if (url.isEmpty()) return;
179 item->setText(1, url.path());
180 if (KIO::NetAccess::exists(url, KIO::NetAccess::SourceSide, 0)) {
181 item->setIcon(0, KIcon("dialog-ok"));
182 item->setData(0, statusRole, CLIPOK);
188 void DocumentChecker::accept()
191 QDomNodeList producers = m_doc.elementsByTagName("producer");
192 QDomNodeList infoproducers = m_doc.elementsByTagName("kdenlive_producer");
194 QTreeWidgetItem *child = m_view.treeWidget->topLevelItem(ix);
196 if (child->data(0, statusRole).toInt() == CLIPOK) {
197 QString id = child->data(0, idRole).toString();
198 for (int i = 0; i < infoproducers.count(); i++) {
199 e = infoproducers.item(i).toElement();
200 if (e.attribute("id") == id) {
202 e.setAttribute("resource", child->text(1));
206 for (int i = 0; i < producers.count(); i++) {
207 e = producers.item(i).toElement();
208 if (e.attribute("id") == id) {
210 e.setAttribute("resource", child->text(1));
214 } else if (child->data(0, statusRole).toInt() == CLIPPLACEHOLDER) {
215 QString id = child->data(0, idRole).toString();
216 for (int i = 0; i < infoproducers.count(); i++) {
217 e = infoproducers.item(i).toElement();
218 if (e.attribute("id") == id) {
220 e.setAttribute("placeholder", '1');
226 child = m_view.treeWidget->topLevelItem(ix);
231 void DocumentChecker::slotPlaceholders()
234 QTreeWidgetItem *child = m_view.treeWidget->topLevelItem(ix);
236 if (child->data(0, statusRole).toInt() == CLIPMISSING) {
237 child->setData(0, statusRole, CLIPPLACEHOLDER);
238 child->setIcon(0, KIcon("dialog-ok"));
241 child = m_view.treeWidget->topLevelItem(ix);
247 void DocumentChecker::checkStatus()
251 QTreeWidgetItem *child = m_view.treeWidget->topLevelItem(ix);
253 if (child->data(0, statusRole).toInt() == CLIPMISSING) {
258 child = m_view.treeWidget->topLevelItem(ix);
260 m_view.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(status);
263 #include "documentchecker.moc"