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>
32 #include <KMessageBox>
34 #include <QTreeWidgetItem>
36 #include <QHeaderView>
40 #include <QCryptographicHash>
42 const int hashRole = Qt::UserRole;
43 const int sizeRole = Qt::UserRole + 1;
44 const int idRole = Qt::UserRole + 2;
45 const int statusRole = Qt::UserRole + 3;
47 const int CLIPMISSING = 0;
49 const int CLIPPLACEHOLDER = 2;
51 DocumentChecker::DocumentChecker(QDomDocument doc, QWidget * parent) :
52 QDialog(parent), m_doc(doc)
54 setFont(KGlobalSettings::toolBarFont());
57 QDomNodeList producers = m_doc.elementsByTagName("producer");
58 QDomNodeList infoproducers = m_doc.elementsByTagName("kdenlive_producer");
64 QList <QDomElement> missingClips;
65 for (int i = 0; i < infoproducers.count(); i++) {
66 e = infoproducers.item(i).toElement();
67 clipType = e.attribute("type").toInt();
68 if (clipType == TEXT) continue;
69 id = e.attribute("id");
70 resource = e.attribute("resource");
71 if (clipType == SLIDESHOW) resource = KUrl(resource).directory();
72 if (!KIO::NetAccess::exists(KUrl(resource), KIO::NetAccess::SourceSide, 0)) {
74 missingClips.append(e);
78 if (missingClips.isEmpty()) QTimer::singleShot(0, this, SLOT(accept()));
79 m_view.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
80 for (int i = 0; i < missingClips.count(); i++) {
81 e = missingClips.at(i).toElement();
83 switch (e.attribute("type").toInt()) {
85 clipType = i18n("Video clip");
88 clipType = i18n("Mute video clip");
91 clipType = i18n("Audio clip");
94 clipType = i18n("Playlist clip");
97 clipType = i18n("Image clip");
100 clipType = i18n("Slideshow clip");
103 clipType = i18n("Video clip");
105 QTreeWidgetItem *item = new QTreeWidgetItem(m_view.treeWidget, QStringList() << clipType << e.attribute("resource"));
106 item->setIcon(0, KIcon("dialog-close"));
107 item->setData(0, hashRole, e.attribute("file_hash"));
108 item->setData(0, sizeRole, e.attribute("file_size"));
109 item->setData(0, idRole, e.attribute("id"));
110 item->setData(0, statusRole, CLIPMISSING);
112 connect(m_view.recursiveSearch, SIGNAL(pressed()), this, SLOT(slotSearchClips()));
113 connect(m_view.usePlaceholders, SIGNAL(pressed()), this, SLOT(slotPlaceholders()));
114 connect(m_view.removeSelected, SIGNAL(pressed()), this, SLOT(slotDeleteSelected()));
115 connect(m_view.treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, SLOT(slotEditItem(QTreeWidgetItem *, int)));
119 DocumentChecker::~DocumentChecker() {}
121 void DocumentChecker::slotSearchClips()
123 QString newpath = KFileDialog::getExistingDirectory(KUrl("kfiledialog:///clipfolder"), kapp->activeWindow(), i18n("Clips folder"));
124 if (newpath.isEmpty()) return;
126 QTreeWidgetItem *child = m_view.treeWidget->topLevelItem(ix);
127 while (child && child->data(0, statusRole).toInt() == CLIPMISSING) {
128 QString clipPath = searchFileRecursively(QDir(newpath), child->data(0, sizeRole).toString(), child->data(0, hashRole).toString());
129 if (!clipPath.isEmpty()) {
130 child->setText(1, clipPath);
131 child->setIcon(0, KIcon("dialog-ok"));
132 child->setData(0, statusRole, CLIPOK);
135 child = m_view.treeWidget->topLevelItem(ix);
140 QString DocumentChecker::searchFileRecursively(const QDir &dir, const QString &matchSize, const QString &matchHash) const
142 QString foundFileName;
145 QStringList filesAndDirs = dir.entryList(QDir::Files | QDir::Readable);
146 for (int i = 0; i < filesAndDirs.size() && foundFileName.isEmpty(); i++) {
147 QFile file(dir.absoluteFilePath(filesAndDirs.at(i)));
148 if (file.open(QIODevice::ReadOnly)) {
149 if (QString::number(file.size()) == matchSize) {
151 * 1 MB = 1 second per 450 files (or faster)
152 * 10 MB = 9 seconds per 450 files (or faster)
154 if (file.size() > 1000000*2) {
155 fileData = file.read(1000000);
156 if (file.seek(file.size() - 1000000))
157 fileData.append(file.readAll());
159 fileData = file.readAll();
161 fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
162 if (QString(fileHash.toHex()) == matchHash)
163 return file.fileName();
166 kDebug() << filesAndDirs.at(i) << file.size() << fileHash.toHex();
168 filesAndDirs = dir.entryList(QDir::Dirs | QDir::Readable | QDir::Executable | QDir::NoDotAndDotDot);
169 for (int i = 0; i < filesAndDirs.size() && foundFileName.isEmpty(); i++) {
170 foundFileName = searchFileRecursively(dir.absoluteFilePath(filesAndDirs.at(i)), matchSize, matchHash);
171 if (!foundFileName.isEmpty())
174 return foundFileName;
177 void DocumentChecker::slotEditItem(QTreeWidgetItem *item, int)
179 KUrl url = KUrlRequesterDialog::getUrl(item->text(1), this, i18n("Enter new location for file"));
180 if (url.isEmpty()) return;
181 item->setText(1, url.path());
182 if (KIO::NetAccess::exists(url, KIO::NetAccess::SourceSide, 0)) {
183 item->setIcon(0, KIcon("dialog-ok"));
184 item->setData(0, statusRole, CLIPOK);
190 void DocumentChecker::accept()
193 QDomNodeList producers = m_doc.elementsByTagName("producer");
194 QDomNodeList infoproducers = m_doc.elementsByTagName("kdenlive_producer");
196 QTreeWidgetItem *child = m_view.treeWidget->topLevelItem(ix);
198 if (child->data(0, statusRole).toInt() == CLIPOK) {
199 QString id = child->data(0, idRole).toString();
200 for (int i = 0; i < infoproducers.count(); i++) {
201 e = infoproducers.item(i).toElement();
202 if (e.attribute("id") == id) {
204 e.setAttribute("resource", child->text(1));
208 for (int i = 0; i < producers.count(); i++) {
209 e = producers.item(i).toElement();
210 if (e.attribute("id") == id) {
212 e.setAttribute("resource", child->text(1));
216 } else if (child->data(0, statusRole).toInt() == CLIPPLACEHOLDER) {
217 QString id = child->data(0, idRole).toString();
218 for (int i = 0; i < infoproducers.count(); i++) {
219 e = infoproducers.item(i).toElement();
220 if (e.attribute("id") == id) {
222 e.setAttribute("placeholder", '1');
228 child = m_view.treeWidget->topLevelItem(ix);
233 void DocumentChecker::slotPlaceholders()
236 QTreeWidgetItem *child = m_view.treeWidget->topLevelItem(ix);
238 if (child->data(0, statusRole).toInt() == CLIPMISSING) {
239 child->setData(0, statusRole, CLIPPLACEHOLDER);
240 child->setIcon(0, KIcon("dialog-ok"));
243 child = m_view.treeWidget->topLevelItem(ix);
249 void DocumentChecker::checkStatus()
253 QTreeWidgetItem *child = m_view.treeWidget->topLevelItem(ix);
255 if (child->data(0, statusRole).toInt() == CLIPMISSING) {
260 child = m_view.treeWidget->topLevelItem(ix);
262 m_view.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(status);
266 void DocumentChecker::slotDeleteSelected()
268 if (KMessageBox::warningContinueCancel(this, i18n("This will remove the selected clips from this project"), i18n("Remove clips")) == KMessageBox::Cancel) return;
270 QStringList deletedIds;
271 QTreeWidgetItem *child = m_view.treeWidget->topLevelItem(ix);
272 QDomNodeList playlists = m_doc.elementsByTagName("playlist");
275 if (child->isSelected()) {
276 QString id = child->data(0, idRole).toString();
277 deletedIds.append(id);
278 for (int j = 0; j < playlists.count(); j++)
279 deletedIds.append(id + '_' + QString::number(j));
282 child = m_view.treeWidget->topLevelItem(ix);
284 kDebug() << "// Clips to delete: " << deletedIds;
286 if (!deletedIds.isEmpty()) {
288 QDomNodeList producers = m_doc.elementsByTagName("producer");
289 QDomNodeList infoproducers = m_doc.elementsByTagName("kdenlive_producer");
291 QDomElement westley = m_doc.firstChildElement("westley");
292 QDomElement kdenlivedoc = westley.firstChildElement("kdenlivedoc");
294 for (int i = 0; i < infoproducers.count(); i++) {
295 e = infoproducers.item(i).toElement();
296 if (deletedIds.contains(e.attribute("id"))) {
298 kdenlivedoc.removeChild(e);
303 for (int i = 0; i < producers.count(); i++) {
304 e = producers.item(i).toElement();
305 if (deletedIds.contains(e.attribute("id"))) {
307 westley.removeChild(e);
312 for (int i = 0; i < playlists.count(); i++) {
313 QDomNodeList entries = playlists.at(i).toElement().elementsByTagName("entry");
314 for (int j = 0; j < playlists.count(); j++) {
315 e = entries.item(j).toElement();
316 if (deletedIds.contains(e.attribute("producer"))) {
317 // Replace clip with blank
318 e.setTagName("blank");
319 e.removeAttribute("producer");
320 int length = e.attribute("out").toInt() - e.attribute("in").toInt();
321 e.setAttribute("length", length);
329 #include "documentchecker.moc"