]> git.sesse.net Git - kdenlive/blob - src/documentchecker.cpp
Fix display name of recovered clips:
[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(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                     e.setAttribute("name", KUrl(child->text(1)).fileName());
191                     break;
192                 }
193             }
194             for (int i = 0; i < producers.count(); i++) {
195                 e = producers.item(i).toElement();
196                 if (e.attribute("id").section('_', 0, 0) == id) {
197                     // Fix clip
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));
203                             break;
204                         }
205                     }
206                     break;
207                 }
208             }
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) {
214                     // Fix clip
215                     e.setAttribute("placeholder", '1');
216                     break;
217                 }
218             }
219         }
220         ix++;
221         child = m_view.treeWidget->topLevelItem(ix);
222     }
223     QDialog::accept();
224 }
225
226 void DocumentChecker::slotPlaceholders()
227 {
228     int ix = 0;
229     QTreeWidgetItem *child = m_view.treeWidget->topLevelItem(ix);
230     while (child) {
231         if (child->data(0, statusRole).toInt() == CLIPMISSING) {
232             child->setData(0, statusRole, CLIPPLACEHOLDER);
233             child->setIcon(0, KIcon("dialog-ok"));
234         }
235         ix++;
236         child = m_view.treeWidget->topLevelItem(ix);
237     }
238     checkStatus();
239 }
240
241
242 void DocumentChecker::checkStatus()
243 {
244     bool status = true;
245     int ix = 0;
246     QTreeWidgetItem *child = m_view.treeWidget->topLevelItem(ix);
247     while (child) {
248         if (child->data(0, statusRole).toInt() == CLIPMISSING) {
249             status = false;
250             break;
251         }
252         ix++;
253         child = m_view.treeWidget->topLevelItem(ix);
254     }
255     m_view.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(status);
256 }
257
258
259 void DocumentChecker::slotDeleteSelected()
260 {
261     if (KMessageBox::warningContinueCancel(this, i18n("This will remove the selected clips from this project"), i18n("Remove clips")) == KMessageBox::Cancel) return;
262     int ix = 0;
263     QStringList deletedIds;
264     QTreeWidgetItem *child = m_view.treeWidget->topLevelItem(ix);
265     QDomNodeList playlists = m_doc.elementsByTagName("playlist");
266
267     while (child) {
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));
273             delete child;
274         } else ix++;
275         child = m_view.treeWidget->topLevelItem(ix);
276     }
277     kDebug() << "// Clips to delete: " << deletedIds;
278
279     if (!deletedIds.isEmpty()) {
280         QDomElement e;
281         QDomNodeList producers = m_doc.elementsByTagName("producer");
282         QDomNodeList infoproducers = m_doc.elementsByTagName("kdenlive_producer");
283
284         QDomElement mlt = m_doc.firstChildElement("mlt");
285         QDomElement kdenlivedoc = mlt.firstChildElement("kdenlivedoc");
286
287         for (int i = 0; i < infoproducers.count(); i++) {
288             e = infoproducers.item(i).toElement();
289             if (deletedIds.contains(e.attribute("id"))) {
290                 // Remove clip
291                 kdenlivedoc.removeChild(e);
292                 break;
293             }
294         }
295
296         for (int i = 0; i < producers.count(); i++) {
297             e = producers.item(i).toElement();
298             if (deletedIds.contains(e.attribute("id"))) {
299                 // Remove clip
300                 mlt.removeChild(e);
301                 break;
302             }
303         }
304
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);
315                 }
316             }
317         }
318         checkStatus();
319     }
320 }
321
322 #include "documentchecker.moc"
323
324