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(QDomNodeList producers, QDomNodeList infoproducers, QList <QDomElement> missingClips, QDomDocument doc, QWidget * parent) :
52 QDialog(parent), m_doc(doc)
54 setFont(KGlobalSettings::toolBarFont());
58 m_view.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
59 for (int i = 0; i < missingClips.count(); i++) {
60 e = missingClips.at(i).toElement();
62 switch (e.attribute("type").toInt()) {
64 clipType = i18n("Video clip");
67 clipType = i18n("Mute video clip");
70 clipType = i18n("Audio clip");
73 clipType = i18n("Playlist clip");
76 clipType = i18n("Image clip");
79 clipType = i18n("Slideshow clip");
82 clipType = i18n("Video clip");
84 QTreeWidgetItem *item = new QTreeWidgetItem(m_view.treeWidget, QStringList() << clipType << e.attribute("resource"));
85 item->setIcon(0, KIcon("dialog-close"));
86 item->setData(0, hashRole, e.attribute("file_hash"));
87 item->setData(0, sizeRole, e.attribute("file_size"));
88 item->setData(0, idRole, e.attribute("id"));
89 item->setData(0, statusRole, CLIPMISSING);
91 connect(m_view.recursiveSearch, SIGNAL(pressed()), this, SLOT(slotSearchClips()));
92 connect(m_view.usePlaceholders, SIGNAL(pressed()), this, SLOT(slotPlaceholders()));
93 connect(m_view.removeSelected, SIGNAL(pressed()), this, SLOT(slotDeleteSelected()));
94 connect(m_view.treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, SLOT(slotEditItem(QTreeWidgetItem *, int)));
98 DocumentChecker::~DocumentChecker() {}
100 void DocumentChecker::slotSearchClips()
102 QString newpath = KFileDialog::getExistingDirectory(KUrl("kfiledialog:///clipfolder"), kapp->activeWindow(), i18n("Clips folder"));
103 if (newpath.isEmpty()) return;
105 m_view.recursiveSearch->setEnabled(false);
106 QTreeWidgetItem *child = m_view.treeWidget->topLevelItem(ix);
107 while (child && child->data(0, statusRole).toInt() == CLIPMISSING) {
108 QString clipPath = searchFileRecursively(QDir(newpath), child->data(0, sizeRole).toString(), child->data(0, hashRole).toString());
109 if (!clipPath.isEmpty()) {
110 child->setText(1, clipPath);
111 child->setIcon(0, KIcon("dialog-ok"));
112 child->setData(0, statusRole, CLIPOK);
115 child = m_view.treeWidget->topLevelItem(ix);
117 m_view.recursiveSearch->setEnabled(true);
121 QString DocumentChecker::searchFileRecursively(const QDir &dir, const QString &matchSize, const QString &matchHash) const
123 QString foundFileName;
126 QStringList filesAndDirs = dir.entryList(QDir::Files | QDir::Readable);
127 for (int i = 0; i < filesAndDirs.size() && foundFileName.isEmpty(); i++) {
128 QFile file(dir.absoluteFilePath(filesAndDirs.at(i)));
129 if (file.open(QIODevice::ReadOnly)) {
130 if (QString::number(file.size()) == matchSize) {
132 * 1 MB = 1 second per 450 files (or faster)
133 * 10 MB = 9 seconds per 450 files (or faster)
135 if (file.size() > 1000000*2) {
136 fileData = file.read(1000000);
137 if (file.seek(file.size() - 1000000))
138 fileData.append(file.readAll());
140 fileData = file.readAll();
142 fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
143 if (QString(fileHash.toHex()) == matchHash)
144 return file.fileName();
147 //kDebug() << filesAndDirs.at(i) << file.size() << fileHash.toHex();
149 filesAndDirs = dir.entryList(QDir::Dirs | QDir::Readable | QDir::Executable | QDir::NoDotAndDotDot);
150 for (int i = 0; i < filesAndDirs.size() && foundFileName.isEmpty(); i++) {
151 foundFileName = searchFileRecursively(dir.absoluteFilePath(filesAndDirs.at(i)), matchSize, matchHash);
152 if (!foundFileName.isEmpty())
155 return foundFileName;
158 void DocumentChecker::slotEditItem(QTreeWidgetItem *item, int)
160 KUrl url = KUrlRequesterDialog::getUrl(item->text(1), this, i18n("Enter new location for file"));
161 if (url.isEmpty()) return;
162 item->setText(1, url.path());
163 if (KIO::NetAccess::exists(url, KIO::NetAccess::SourceSide, 0)) {
164 item->setIcon(0, KIcon("dialog-ok"));
165 item->setData(0, statusRole, CLIPOK);
171 void DocumentChecker::accept()
174 QDomNodeList producers = m_doc.elementsByTagName("producer");
175 QDomNodeList infoproducers = m_doc.elementsByTagName("kdenlive_producer");
177 QTreeWidgetItem *child = m_view.treeWidget->topLevelItem(ix);
179 if (child->data(0, statusRole).toInt() == CLIPOK) {
180 QString id = child->data(0, idRole).toString();
181 for (int i = 0; i < infoproducers.count(); i++) {
182 e = infoproducers.item(i).toElement();
183 if (e.attribute("id") == id) {
185 e.setAttribute("resource", child->text(1));
189 for (int i = 0; i < producers.count(); i++) {
190 e = producers.item(i).toElement();
191 if (e.attribute("id") == id) {
193 e.setAttribute("resource", child->text(1));
197 } else if (child->data(0, statusRole).toInt() == CLIPPLACEHOLDER) {
198 QString id = child->data(0, idRole).toString();
199 for (int i = 0; i < infoproducers.count(); i++) {
200 e = infoproducers.item(i).toElement();
201 if (e.attribute("id") == id) {
203 e.setAttribute("placeholder", '1');
209 child = m_view.treeWidget->topLevelItem(ix);
214 void DocumentChecker::slotPlaceholders()
217 QTreeWidgetItem *child = m_view.treeWidget->topLevelItem(ix);
219 if (child->data(0, statusRole).toInt() == CLIPMISSING) {
220 child->setData(0, statusRole, CLIPPLACEHOLDER);
221 child->setIcon(0, KIcon("dialog-ok"));
224 child = m_view.treeWidget->topLevelItem(ix);
230 void DocumentChecker::checkStatus()
234 QTreeWidgetItem *child = m_view.treeWidget->topLevelItem(ix);
236 if (child->data(0, statusRole).toInt() == CLIPMISSING) {
241 child = m_view.treeWidget->topLevelItem(ix);
243 m_view.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(status);
247 void DocumentChecker::slotDeleteSelected()
249 if (KMessageBox::warningContinueCancel(this, i18n("This will remove the selected clips from this project"), i18n("Remove clips")) == KMessageBox::Cancel) return;
251 QStringList deletedIds;
252 QTreeWidgetItem *child = m_view.treeWidget->topLevelItem(ix);
253 QDomNodeList playlists = m_doc.elementsByTagName("playlist");
256 if (child->isSelected()) {
257 QString id = child->data(0, idRole).toString();
258 deletedIds.append(id);
259 for (int j = 0; j < playlists.count(); j++)
260 deletedIds.append(id + '_' + QString::number(j));
263 child = m_view.treeWidget->topLevelItem(ix);
265 kDebug() << "// Clips to delete: " << deletedIds;
267 if (!deletedIds.isEmpty()) {
269 QDomNodeList producers = m_doc.elementsByTagName("producer");
270 QDomNodeList infoproducers = m_doc.elementsByTagName("kdenlive_producer");
272 QDomElement westley = m_doc.firstChildElement("westley");
273 QDomElement kdenlivedoc = westley.firstChildElement("kdenlivedoc");
275 for (int i = 0; i < infoproducers.count(); i++) {
276 e = infoproducers.item(i).toElement();
277 if (deletedIds.contains(e.attribute("id"))) {
279 kdenlivedoc.removeChild(e);
284 for (int i = 0; i < producers.count(); i++) {
285 e = producers.item(i).toElement();
286 if (deletedIds.contains(e.attribute("id"))) {
288 westley.removeChild(e);
293 for (int i = 0; i < playlists.count(); i++) {
294 QDomNodeList entries = playlists.at(i).toElement().elementsByTagName("entry");
295 for (int j = 0; j < playlists.count(); j++) {
296 e = entries.item(j).toElement();
297 if (deletedIds.contains(e.attribute("producer"))) {
298 // Replace clip with blank
299 e.setTagName("blank");
300 e.removeAttribute("producer");
301 int length = e.attribute("out").toInt() - e.attribute("in").toInt();
302 e.setAttribute("length", length);
310 #include "documentchecker.moc"