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(QList <QDomElement> missingClips, QDomDocument doc, QWidget * parent) :
55 setFont(KGlobalSettings::toolBarFont());
59 m_view.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
60 for (int i = 0; i < missingClips.count(); i++) {
61 e = missingClips.at(i).toElement();
63 switch (e.attribute("type").toInt()) {
65 clipType = i18n("Video clip");
68 clipType = i18n("Mute video clip");
71 clipType = i18n("Audio clip");
74 clipType = i18n("Playlist clip");
77 clipType = i18n("Image clip");
80 clipType = i18n("Slideshow clip");
83 clipType = i18n("Video clip");
85 QTreeWidgetItem *item = new QTreeWidgetItem(m_view.treeWidget, QStringList() << clipType << e.attribute("resource"));
86 item->setIcon(0, KIcon("dialog-close"));
87 item->setData(0, hashRole, e.attribute("file_hash"));
88 item->setData(0, sizeRole, e.attribute("file_size"));
89 item->setData(0, idRole, e.attribute("id"));
90 item->setData(0, statusRole, CLIPMISSING);
92 connect(m_view.recursiveSearch, SIGNAL(pressed()), this, SLOT(slotSearchClips()));
93 connect(m_view.usePlaceholders, SIGNAL(pressed()), this, SLOT(slotPlaceholders()));
94 connect(m_view.removeSelected, SIGNAL(pressed()), this, SLOT(slotDeleteSelected()));
95 connect(m_view.treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, SLOT(slotEditItem(QTreeWidgetItem *, int)));
99 DocumentChecker::~DocumentChecker() {}
101 void DocumentChecker::slotSearchClips()
103 QString newpath = KFileDialog::getExistingDirectory(KUrl("kfiledialog:///clipfolder"), kapp->activeWindow(), i18n("Clips folder"));
104 if (newpath.isEmpty()) return;
106 m_view.recursiveSearch->setEnabled(false);
107 QTreeWidgetItem *child = m_view.treeWidget->topLevelItem(ix);
109 if (child->data(0, statusRole).toInt() == CLIPMISSING) {
110 QString clipPath = searchFileRecursively(QDir(newpath), child->data(0, sizeRole).toString(), child->data(0, hashRole).toString());
111 if (!clipPath.isEmpty()) {
112 child->setText(1, clipPath);
113 child->setIcon(0, KIcon("dialog-ok"));
114 child->setData(0, statusRole, CLIPOK);
118 child = m_view.treeWidget->topLevelItem(ix);
120 m_view.recursiveSearch->setEnabled(true);
124 QString DocumentChecker::searchFileRecursively(const QDir &dir, const QString &matchSize, const QString &matchHash) const
126 QString foundFileName;
129 QStringList filesAndDirs = dir.entryList(QDir::Files | QDir::Readable);
130 for (int i = 0; i < filesAndDirs.size() && foundFileName.isEmpty(); i++) {
131 QFile file(dir.absoluteFilePath(filesAndDirs.at(i)));
132 if (QString::number(file.size()) == matchSize) {
133 if (file.open(QIODevice::ReadOnly)) {
135 * 1 MB = 1 second per 450 files (or faster)
136 * 10 MB = 9 seconds per 450 files (or faster)
138 if (file.size() > 1000000*2) {
139 fileData = file.read(1000000);
140 if (file.seek(file.size() - 1000000))
141 fileData.append(file.readAll());
143 fileData = file.readAll();
145 fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
146 if (QString(fileHash.toHex()) == matchHash)
147 return file.fileName();
150 //kDebug() << filesAndDirs.at(i) << file.size() << fileHash.toHex();
152 filesAndDirs = dir.entryList(QDir::Dirs | QDir::Readable | QDir::Executable | QDir::NoDotAndDotDot);
153 for (int i = 0; i < filesAndDirs.size() && foundFileName.isEmpty(); i++) {
154 foundFileName = searchFileRecursively(dir.absoluteFilePath(filesAndDirs.at(i)), matchSize, matchHash);
155 if (!foundFileName.isEmpty())
158 return foundFileName;
161 void DocumentChecker::slotEditItem(QTreeWidgetItem *item, int)
163 KUrl url = KUrlRequesterDialog::getUrl(item->text(1), this, i18n("Enter new location for file"));
164 if (url.isEmpty()) return;
165 item->setText(1, url.path());
166 if (KIO::NetAccess::exists(url, KIO::NetAccess::SourceSide, 0)) {
167 item->setIcon(0, KIcon("dialog-ok"));
168 item->setData(0, statusRole, CLIPOK);
174 void DocumentChecker::accept()
176 QDomElement e, property;
177 QDomNodeList producers = m_doc.elementsByTagName("producer");
178 QDomNodeList infoproducers = m_doc.elementsByTagName("kdenlive_producer");
179 QDomNodeList properties;
181 QTreeWidgetItem *child = m_view.treeWidget->topLevelItem(ix);
183 if (child->data(0, statusRole).toInt() == CLIPOK) {
184 QString id = child->data(0, idRole).toString();
185 for (int i = 0; i < infoproducers.count(); i++) {
186 e = infoproducers.item(i).toElement();
187 if (e.attribute("id") == id) {
189 e.setAttribute("resource", child->text(1));
190 e.setAttribute("name", KUrl(child->text(1)).fileName());
194 for (int i = 0; i < producers.count(); i++) {
195 e = producers.item(i).toElement();
196 if (e.attribute("id").section('_', 0, 0) == id) {
198 properties = e.childNodes();
199 for (int j = 0; j < properties.count(); ++j) {
200 property = properties.item(j).toElement();
201 if (property.attribute("name") == "resource") {
202 property.firstChild().setNodeValue(child->text(1));
209 } else if (child->data(0, statusRole).toInt() == CLIPPLACEHOLDER) {
210 QString id = child->data(0, idRole).toString();
211 for (int i = 0; i < infoproducers.count(); i++) {
212 e = infoproducers.item(i).toElement();
213 if (e.attribute("id") == id) {
215 e.setAttribute("placeholder", '1');
221 child = m_view.treeWidget->topLevelItem(ix);
226 void DocumentChecker::slotPlaceholders()
229 QTreeWidgetItem *child = m_view.treeWidget->topLevelItem(ix);
231 if (child->data(0, statusRole).toInt() == CLIPMISSING) {
232 child->setData(0, statusRole, CLIPPLACEHOLDER);
233 child->setIcon(0, KIcon("dialog-ok"));
236 child = m_view.treeWidget->topLevelItem(ix);
242 void DocumentChecker::checkStatus()
246 QTreeWidgetItem *child = m_view.treeWidget->topLevelItem(ix);
248 if (child->data(0, statusRole).toInt() == CLIPMISSING) {
253 child = m_view.treeWidget->topLevelItem(ix);
255 m_view.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(status);
259 void DocumentChecker::slotDeleteSelected()
261 if (KMessageBox::warningContinueCancel(this, i18n("This will remove the selected clips from this project"), i18n("Remove clips")) == KMessageBox::Cancel) return;
263 QStringList deletedIds;
264 QTreeWidgetItem *child = m_view.treeWidget->topLevelItem(ix);
265 QDomNodeList playlists = m_doc.elementsByTagName("playlist");
268 if (child->isSelected()) {
269 QString id = child->data(0, idRole).toString();
270 deletedIds.append(id);
271 for (int j = 0; j < playlists.count(); j++)
272 deletedIds.append(id + '_' + QString::number(j));
275 child = m_view.treeWidget->topLevelItem(ix);
277 kDebug() << "// Clips to delete: " << deletedIds;
279 if (!deletedIds.isEmpty()) {
281 QDomNodeList producers = m_doc.elementsByTagName("producer");
282 QDomNodeList infoproducers = m_doc.elementsByTagName("kdenlive_producer");
284 QDomElement mlt = m_doc.firstChildElement("mlt");
285 QDomElement kdenlivedoc = mlt.firstChildElement("kdenlivedoc");
287 for (int i = 0; i < infoproducers.count(); i++) {
288 e = infoproducers.item(i).toElement();
289 if (deletedIds.contains(e.attribute("id"))) {
291 kdenlivedoc.removeChild(e);
296 for (int i = 0; i < producers.count(); i++) {
297 e = producers.item(i).toElement();
298 if (deletedIds.contains(e.attribute("id"))) {
305 for (int i = 0; i < playlists.count(); i++) {
306 QDomNodeList entries = playlists.at(i).toElement().elementsByTagName("entry");
307 for (int j = 0; j < playlists.count(); j++) {
308 e = entries.item(j).toElement();
309 if (deletedIds.contains(e.attribute("producer"))) {
310 // Replace clip with blank
311 e.setTagName("blank");
312 e.removeAttribute("producer");
313 int length = e.attribute("out").toInt() - e.attribute("in").toInt();
314 e.setAttribute("length", length);
322 #include "documentchecker.moc"