]> git.sesse.net Git - kdenlive/blob - src/documentchecker.cpp
f172d40708cd0850d8c6542010c0416cf5f8519d
[kdenlive] / src / documentchecker.cpp
1 /***************************************************************************
2  *   Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
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.                                   *
8  *                                                                         *
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.                          *
13  *                                                                         *
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  ***************************************************************************/
19
20
21 #include "documentchecker.h"
22 #include "kthumb.h"
23 #include "definitions.h"
24
25 #include <KDebug>
26 #include <KGlobalSettings>
27 #include <KFileItem>
28 #include <KIO/NetAccess>
29 #include <KFileDialog>
30 #include <KApplication>
31 #include <KUrlRequesterDialog>
32 #include <KMessageBox>
33
34 #include <QTreeWidgetItem>
35 #include <QFile>
36 #include <QHeaderView>
37 #include <QIcon>
38 #include <QPixmap>
39 #include <QTimer>
40 #include <QCryptographicHash>
41
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;
46
47 const int CLIPMISSING = 0;
48 const int CLIPOK = 1;
49 const int CLIPPLACEHOLDER = 2;
50
51 DocumentChecker::DocumentChecker(QDomNodeList producers, QDomNodeList infoproducers, QList <QDomElement> missingClips, QDomDocument doc, QWidget * parent) :
52         QDialog(parent),
53         m_doc(doc)
54 {
55     setFont(KGlobalSettings::toolBarFont());
56     m_view.setupUi(this);
57     QDomElement e;
58
59     m_view.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
60     for (int i = 0; i < missingClips.count(); i++) {
61         e = missingClips.at(i).toElement();
62         QString clipType;
63         switch (e.attribute("type").toInt()) {
64         case AV:
65             clipType = i18n("Video clip");
66             break;
67         case VIDEO:
68             clipType = i18n("Mute video clip");
69             break;
70         case AUDIO:
71             clipType = i18n("Audio clip");
72             break;
73         case PLAYLIST:
74             clipType = i18n("Playlist clip");
75             break;
76         case IMAGE:
77             clipType = i18n("Image clip");
78             break;
79         case SLIDESHOW:
80             clipType = i18n("Slideshow clip");
81             break;
82         default:
83             clipType = i18n("Video clip");
84         }
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);
91     }
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)));
96     //adjustSize();
97 }
98
99 DocumentChecker::~DocumentChecker() {}
100
101 void DocumentChecker::slotSearchClips()
102 {
103     QString newpath = KFileDialog::getExistingDirectory(KUrl("kfiledialog:///clipfolder"), kapp->activeWindow(), i18n("Clips folder"));
104     if (newpath.isEmpty()) return;
105     int ix = 0;
106     m_view.recursiveSearch->setEnabled(false);
107     QTreeWidgetItem *child = m_view.treeWidget->topLevelItem(ix);
108     while (child) {
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);
115             }
116         }
117         ix++;
118         child = m_view.treeWidget->topLevelItem(ix);
119     }
120     m_view.recursiveSearch->setEnabled(true);
121     checkStatus();
122 }
123
124 QString DocumentChecker::searchFileRecursively(const QDir &dir, const QString &matchSize, const QString &matchHash) const
125 {
126     QString foundFileName;
127     QByteArray fileData;
128     QByteArray fileHash;
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)) {
134                 /*
135                 * 1 MB = 1 second per 450 files (or faster)
136                 * 10 MB = 9 seconds per 450 files (or faster)
137                 */
138                 if (file.size() > 1000000*2) {
139                     fileData = file.read(1000000);
140                     if (file.seek(file.size() - 1000000))
141                         fileData.append(file.readAll());
142                 } else
143                     fileData = file.readAll();
144                 file.close();
145                 fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
146                 if (QString(fileHash.toHex()) == matchHash)
147                     return file.fileName();
148             }
149         }
150         //kDebug() << filesAndDirs.at(i) << file.size() << fileHash.toHex();
151     }
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())
156             break;
157     }
158     return foundFileName;
159 }
160
161 void DocumentChecker::slotEditItem(QTreeWidgetItem *item, int)
162 {
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);
169         checkStatus();
170     }
171 }
172
173 // virtual
174 void DocumentChecker::accept()
175 {
176     QDomElement e, property;
177     QDomNodeList producers = m_doc.elementsByTagName("producer");
178     QDomNodeList infoproducers = m_doc.elementsByTagName("kdenlive_producer");
179     QDomNodeList properties;
180     int ix = 0;
181     QTreeWidgetItem *child = m_view.treeWidget->topLevelItem(ix);
182     while (child) {
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) {
188                     // Fix clip
189                     e.setAttribute("resource", child->text(1));
190                     break;
191                 }
192             }
193             for (int i = 0; i < producers.count(); i++) {
194                 e = producers.item(i).toElement();
195                 if (e.attribute("id").section('_', 0, 0) == id) {
196                     // Fix clip
197                     properties = e.childNodes();
198                     for (int j = 0; j < properties.count(); ++j) {
199                         property = properties.item(j).toElement();
200                         if (property.attribute("name") == "resource") {
201                             property.firstChild().setNodeValue(child->text(1));
202                             break;
203                         }
204                     }
205                     break;
206                 }
207             }
208         } else if (child->data(0, statusRole).toInt() == CLIPPLACEHOLDER) {
209             QString id = child->data(0, idRole).toString();
210             for (int i = 0; i < infoproducers.count(); i++) {
211                 e = infoproducers.item(i).toElement();
212                 if (e.attribute("id") == id) {
213                     // Fix clip
214                     e.setAttribute("placeholder", '1');
215                     break;
216                 }
217             }
218         }
219         ix++;
220         child = m_view.treeWidget->topLevelItem(ix);
221     }
222     QDialog::accept();
223 }
224
225 void DocumentChecker::slotPlaceholders()
226 {
227     int ix = 0;
228     QTreeWidgetItem *child = m_view.treeWidget->topLevelItem(ix);
229     while (child) {
230         if (child->data(0, statusRole).toInt() == CLIPMISSING) {
231             child->setData(0, statusRole, CLIPPLACEHOLDER);
232             child->setIcon(0, KIcon("dialog-ok"));
233         }
234         ix++;
235         child = m_view.treeWidget->topLevelItem(ix);
236     }
237     checkStatus();
238 }
239
240
241 void DocumentChecker::checkStatus()
242 {
243     bool status = true;
244     int ix = 0;
245     QTreeWidgetItem *child = m_view.treeWidget->topLevelItem(ix);
246     while (child) {
247         if (child->data(0, statusRole).toInt() == CLIPMISSING) {
248             status = false;
249             break;
250         }
251         ix++;
252         child = m_view.treeWidget->topLevelItem(ix);
253     }
254     m_view.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(status);
255 }
256
257
258 void DocumentChecker::slotDeleteSelected()
259 {
260     if (KMessageBox::warningContinueCancel(this, i18n("This will remove the selected clips from this project"), i18n("Remove clips")) == KMessageBox::Cancel) return;
261     int ix = 0;
262     QStringList deletedIds;
263     QTreeWidgetItem *child = m_view.treeWidget->topLevelItem(ix);
264     QDomNodeList playlists = m_doc.elementsByTagName("playlist");
265
266     while (child) {
267         if (child->isSelected()) {
268             QString id = child->data(0, idRole).toString();
269             deletedIds.append(id);
270             for (int j = 0; j < playlists.count(); j++)
271                 deletedIds.append(id + '_' + QString::number(j));
272             delete child;
273         } else ix++;
274         child = m_view.treeWidget->topLevelItem(ix);
275     }
276     kDebug() << "// Clips to delete: " << deletedIds;
277
278     if (!deletedIds.isEmpty()) {
279         QDomElement e;
280         QDomNodeList producers = m_doc.elementsByTagName("producer");
281         QDomNodeList infoproducers = m_doc.elementsByTagName("kdenlive_producer");
282
283         QDomElement mlt = m_doc.firstChildElement("mlt");
284         QDomElement kdenlivedoc = mlt.firstChildElement("kdenlivedoc");
285
286         for (int i = 0; i < infoproducers.count(); i++) {
287             e = infoproducers.item(i).toElement();
288             if (deletedIds.contains(e.attribute("id"))) {
289                 // Remove clip
290                 kdenlivedoc.removeChild(e);
291                 break;
292             }
293         }
294
295         for (int i = 0; i < producers.count(); i++) {
296             e = producers.item(i).toElement();
297             if (deletedIds.contains(e.attribute("id"))) {
298                 // Remove clip
299                 mlt.removeChild(e);
300                 break;
301             }
302         }
303
304         for (int i = 0; i < playlists.count(); i++) {
305             QDomNodeList entries = playlists.at(i).toElement().elementsByTagName("entry");
306             for (int j = 0; j < playlists.count(); j++) {
307                 e = entries.item(j).toElement();
308                 if (deletedIds.contains(e.attribute("producer"))) {
309                     // Replace clip with blank
310                     e.setTagName("blank");
311                     e.removeAttribute("producer");
312                     int length = e.attribute("out").toInt() - e.attribute("in").toInt();
313                     e.setAttribute("length", length);
314                 }
315             }
316         }
317         checkStatus();
318     }
319 }
320
321 #include "documentchecker.moc"
322
323