]> git.sesse.net Git - kdenlive/blob - src/documentchecker.cpp
fix producers resources when searching missing files:
[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 && child->data(0, statusRole).toInt() == CLIPMISSING) {
109         QString clipPath = searchFileRecursively(QDir(newpath), child->data(0, sizeRole).toString(), child->data(0, hashRole).toString());
110         if (!clipPath.isEmpty()) {
111             child->setText(1, clipPath);
112             child->setIcon(0, KIcon("dialog-ok"));
113             child->setData(0, statusRole, CLIPOK);
114         }
115         ix++;
116         child = m_view.treeWidget->topLevelItem(ix);
117     }
118     m_view.recursiveSearch->setEnabled(true);
119     checkStatus();
120 }
121
122 QString DocumentChecker::searchFileRecursively(const QDir &dir, const QString &matchSize, const QString &matchHash) const
123 {
124     QString foundFileName;
125     QByteArray fileData;
126     QByteArray fileHash;
127     QStringList filesAndDirs = dir.entryList(QDir::Files | QDir::Readable);
128     for (int i = 0; i < filesAndDirs.size() && foundFileName.isEmpty(); i++) {
129         QFile file(dir.absoluteFilePath(filesAndDirs.at(i)));
130         if (file.open(QIODevice::ReadOnly)) {
131             if (QString::number(file.size()) == matchSize) {
132                 /*
133                 * 1 MB = 1 second per 450 files (or faster)
134                 * 10 MB = 9 seconds per 450 files (or faster)
135                 */
136                 if (file.size() > 1000000*2) {
137                     fileData = file.read(1000000);
138                     if (file.seek(file.size() - 1000000))
139                         fileData.append(file.readAll());
140                 } else
141                     fileData = file.readAll();
142                 file.close();
143                 fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
144                 if (QString(fileHash.toHex()) == matchHash)
145                     return file.fileName();
146             }
147         }
148         //kDebug() << filesAndDirs.at(i) << file.size() << fileHash.toHex();
149     }
150     filesAndDirs = dir.entryList(QDir::Dirs | QDir::Readable | QDir::Executable | QDir::NoDotAndDotDot);
151     for (int i = 0; i < filesAndDirs.size() && foundFileName.isEmpty(); i++) {
152         foundFileName = searchFileRecursively(dir.absoluteFilePath(filesAndDirs.at(i)), matchSize, matchHash);
153         if (!foundFileName.isEmpty())
154             break;
155     }
156     return foundFileName;
157 }
158
159 void DocumentChecker::slotEditItem(QTreeWidgetItem *item, int)
160 {
161     KUrl url = KUrlRequesterDialog::getUrl(item->text(1), this, i18n("Enter new location for file"));
162     if (url.isEmpty()) return;
163     item->setText(1, url.path());
164     if (KIO::NetAccess::exists(url, KIO::NetAccess::SourceSide, 0)) {
165         item->setIcon(0, KIcon("dialog-ok"));
166         item->setData(0, statusRole, CLIPOK);
167         checkStatus();
168     }
169 }
170
171 // virtual
172 void DocumentChecker::accept()
173 {
174     QDomElement e, property;
175     QDomNodeList producers = m_doc.elementsByTagName("producer");
176     QDomNodeList infoproducers = m_doc.elementsByTagName("kdenlive_producer");
177     QDomNodeList properties;
178     int ix = 0;
179     QTreeWidgetItem *child = m_view.treeWidget->topLevelItem(ix);
180     while (child) {
181         if (child->data(0, statusRole).toInt() == CLIPOK) {
182             QString id = child->data(0, idRole).toString();
183             for (int i = 0; i < infoproducers.count(); i++) {
184                 e = infoproducers.item(i).toElement();
185                 if (e.attribute("id") == id) {
186                     // Fix clip
187                     e.setAttribute("resource", child->text(1));
188                     break;
189                 }
190             }
191             for (int i = 0; i < producers.count(); i++) {
192                 e = producers.item(i).toElement();
193                 if (e.attribute("id").section('_', 0, 0) == id) {
194                     // Fix clip
195                     properties = e.childNodes();
196                     for (int j = 0; j < properties.count(); ++j) {
197                         property = properties.item(j).toElement();
198                         if (property.attribute("name") == "resource") {
199                             property.firstChild().setNodeValue(child->text(1));
200                             break;
201                         }
202                     }
203                     break;
204                 }
205             }
206         } else if (child->data(0, statusRole).toInt() == CLIPPLACEHOLDER) {
207             QString id = child->data(0, idRole).toString();
208             for (int i = 0; i < infoproducers.count(); i++) {
209                 e = infoproducers.item(i).toElement();
210                 if (e.attribute("id") == id) {
211                     // Fix clip
212                     e.setAttribute("placeholder", '1');
213                     break;
214                 }
215             }
216         }
217         ix++;
218         child = m_view.treeWidget->topLevelItem(ix);
219     }
220     QDialog::accept();
221 }
222
223 void DocumentChecker::slotPlaceholders()
224 {
225     int ix = 0;
226     QTreeWidgetItem *child = m_view.treeWidget->topLevelItem(ix);
227     while (child) {
228         if (child->data(0, statusRole).toInt() == CLIPMISSING) {
229             child->setData(0, statusRole, CLIPPLACEHOLDER);
230             child->setIcon(0, KIcon("dialog-ok"));
231         }
232         ix++;
233         child = m_view.treeWidget->topLevelItem(ix);
234     }
235     checkStatus();
236 }
237
238
239 void DocumentChecker::checkStatus()
240 {
241     bool status = true;
242     int ix = 0;
243     QTreeWidgetItem *child = m_view.treeWidget->topLevelItem(ix);
244     while (child) {
245         if (child->data(0, statusRole).toInt() == CLIPMISSING) {
246             status = false;
247             break;
248         }
249         ix++;
250         child = m_view.treeWidget->topLevelItem(ix);
251     }
252     m_view.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(status);
253 }
254
255
256 void DocumentChecker::slotDeleteSelected()
257 {
258     if (KMessageBox::warningContinueCancel(this, i18n("This will remove the selected clips from this project"), i18n("Remove clips")) == KMessageBox::Cancel) return;
259     int ix = 0;
260     QStringList deletedIds;
261     QTreeWidgetItem *child = m_view.treeWidget->topLevelItem(ix);
262     QDomNodeList playlists = m_doc.elementsByTagName("playlist");
263
264     while (child) {
265         if (child->isSelected()) {
266             QString id = child->data(0, idRole).toString();
267             deletedIds.append(id);
268             for (int j = 0; j < playlists.count(); j++)
269                 deletedIds.append(id + '_' + QString::number(j));
270             delete child;
271         } else ix++;
272         child = m_view.treeWidget->topLevelItem(ix);
273     }
274     kDebug() << "// Clips to delete: " << deletedIds;
275
276     if (!deletedIds.isEmpty()) {
277         QDomElement e;
278         QDomNodeList producers = m_doc.elementsByTagName("producer");
279         QDomNodeList infoproducers = m_doc.elementsByTagName("kdenlive_producer");
280
281         QDomElement westley = m_doc.firstChildElement("westley");
282         QDomElement kdenlivedoc = westley.firstChildElement("kdenlivedoc");
283
284         for (int i = 0; i < infoproducers.count(); i++) {
285             e = infoproducers.item(i).toElement();
286             if (deletedIds.contains(e.attribute("id"))) {
287                 // Remove clip
288                 kdenlivedoc.removeChild(e);
289                 break;
290             }
291         }
292
293         for (int i = 0; i < producers.count(); i++) {
294             e = producers.item(i).toElement();
295             if (deletedIds.contains(e.attribute("id"))) {
296                 // Remove clip
297                 westley.removeChild(e);
298                 break;
299             }
300         }
301
302         for (int i = 0; i < playlists.count(); i++) {
303             QDomNodeList entries = playlists.at(i).toElement().elementsByTagName("entry");
304             for (int j = 0; j < playlists.count(); j++) {
305                 e = entries.item(j).toElement();
306                 if (deletedIds.contains(e.attribute("producer"))) {
307                     // Replace clip with blank
308                     e.setTagName("blank");
309                     e.removeAttribute("producer");
310                     int length = e.attribute("out").toInt() - e.attribute("in").toInt();
311                     e.setAttribute("length", length);
312                 }
313             }
314         }
315         checkStatus();
316     }
317 }
318
319 #include "documentchecker.moc"
320
321