]> git.sesse.net Git - kdenlive/blob - src/projectlist.cpp
Add option to remove unused clips from project tree:
[kdenlive] / src / projectlist.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 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 #include "projectlist.h"
21 #include "projectitem.h"
22 #include "addfoldercommand.h"
23 #include "kdenlivesettings.h"
24 #include "slideshowclip.h"
25 #include "ui_colorclip_ui.h"
26 #include "titlewidget.h"
27 #include "definitions.h"
28 #include "clipmanager.h"
29 #include "docclipbase.h"
30 #include "kdenlivedoc.h"
31 #include "renderer.h"
32 #include "kthumb.h"
33 #include "projectlistview.h"
34 #include "editclipcommand.h"
35 #include "editfoldercommand.h"
36 #include "ui_templateclip_ui.h"
37
38 #include <KDebug>
39 #include <KAction>
40 #include <KLocale>
41 #include <KFileDialog>
42 #include <KInputDialog>
43 #include <KMessageBox>
44
45 #include <nepomuk/global.h>
46 #include <nepomuk/resourcemanager.h>
47 //#include <nepomuk/tag.h>
48
49 #include <QMouseEvent>
50 #include <QStylePainter>
51 #include <QPixmap>
52 #include <QIcon>
53 #include <QMenu>
54 #include <QProcess>
55 #include <QHeaderView>
56
57 ProjectList::ProjectList(QWidget *parent) :
58         QWidget(parent),
59         m_render(NULL),
60         m_fps(-1),
61         m_commandStack(NULL),
62         m_editAction(NULL),
63         m_deleteAction(NULL),
64         m_openAction(NULL),
65         m_reloadAction(NULL),
66         m_transcodeAction(NULL),
67         m_selectedItem(NULL),
68         m_refreshed(false),
69         m_infoQueue(),
70         m_thumbnailQueue()
71 {
72
73     m_listView = new ProjectListView(this);;
74     QVBoxLayout *layout = new QVBoxLayout;
75     layout->setContentsMargins(0, 0, 0, 0);
76
77     // setup toolbar
78     KTreeWidgetSearchLine *searchView = new KTreeWidgetSearchLine(this);
79     m_toolbar = new QToolBar("projectToolBar", this);
80     m_toolbar->addWidget(searchView);
81     searchView->setTreeWidget(m_listView);
82
83     m_addButton = new QToolButton(m_toolbar);
84     m_addButton->setPopupMode(QToolButton::MenuButtonPopup);
85     m_toolbar->addWidget(m_addButton);
86
87     layout->addWidget(m_toolbar);
88     layout->addWidget(m_listView);
89     setLayout(layout);
90
91     m_queueTimer.setInterval(100);
92     connect(&m_queueTimer, SIGNAL(timeout()), this, SLOT(slotProcessNextClipInQueue()));
93     m_queueTimer.setSingleShot(true);
94
95
96
97     connect(m_listView, SIGNAL(itemSelectionChanged()), this, SLOT(slotClipSelected()));
98     connect(m_listView, SIGNAL(focusMonitor()), this, SLOT(slotClipSelected()));
99     connect(m_listView, SIGNAL(pauseMonitor()), this, SLOT(slotPauseMonitor()));
100     connect(m_listView, SIGNAL(requestMenu(const QPoint &, QTreeWidgetItem *)), this, SLOT(slotContextMenu(const QPoint &, QTreeWidgetItem *)));
101     connect(m_listView, SIGNAL(addClip()), this, SLOT(slotAddClip()));
102     connect(m_listView, SIGNAL(addClip(const QList <QUrl>, const QString &, const QString &)), this, SLOT(slotAddClip(const QList <QUrl>, const QString &, const QString &)));
103     connect(m_listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotItemEdited(QTreeWidgetItem *, int)));
104     connect(m_listView, SIGNAL(showProperties(DocClipBase *)), this, SIGNAL(showClipProperties(DocClipBase *)));
105
106     m_listViewDelegate = new ItemDelegate(m_listView);
107     m_listView->setItemDelegate(m_listViewDelegate);
108
109     if (KdenliveSettings::activate_nepomuk()) {
110         Nepomuk::ResourceManager::instance()->init();
111         if (!Nepomuk::ResourceManager::instance()->initialized()) {
112             kDebug() << "Cannot communicate with Nepomuk, DISABLING it";
113             KdenliveSettings::setActivate_nepomuk(false);
114         }
115     }
116 }
117
118 ProjectList::~ProjectList()
119 {
120     delete m_menu;
121     delete m_toolbar;
122     m_listView->blockSignals(true);
123     m_listView->clear();
124     delete m_listViewDelegate;
125 }
126
127 void ProjectList::setupMenu(QMenu *addMenu, QAction *defaultAction)
128 {
129     QList <QAction *> actions = addMenu->actions();
130     for (int i = 0; i < actions.count(); i++) {
131         if (actions.at(i)->data().toString() == "clip_properties") {
132             m_editAction = actions.at(i);
133             m_toolbar->addAction(m_editAction);
134             actions.removeAt(i);
135             i--;
136         } else if (actions.at(i)->data().toString() == "delete_clip") {
137             m_deleteAction = actions.at(i);
138             m_toolbar->addAction(m_deleteAction);
139             actions.removeAt(i);
140             i--;
141         } else if (actions.at(i)->data().toString() == "edit_clip") {
142             m_openAction = actions.at(i);
143             actions.removeAt(i);
144             i--;
145         } else if (actions.at(i)->data().toString() == "reload_clip") {
146             m_reloadAction = actions.at(i);
147             actions.removeAt(i);
148             i--;
149         }
150     }
151
152     QMenu *m = new QMenu();
153     m->addActions(actions);
154     m_addButton->setMenu(m);
155     m_addButton->setDefaultAction(defaultAction);
156     m_menu = new QMenu();
157     m_menu->addActions(addMenu->actions());
158 }
159
160 void ProjectList::setupGeneratorMenu(QMenu *addMenu, QMenu *transcodeMenu)
161 {
162     if (!addMenu) return;
163     QMenu *menu = m_addButton->menu();
164     menu->addMenu(addMenu);
165     m_addButton->setMenu(menu);
166
167     m_menu->addMenu(addMenu);
168     if (addMenu->isEmpty()) addMenu->setEnabled(false);
169     m_menu->addMenu(transcodeMenu);
170     if (transcodeMenu->isEmpty()) transcodeMenu->setEnabled(false);
171     m_transcodeAction = transcodeMenu;
172     m_menu->addAction(m_reloadAction);
173     m_menu->addAction(m_editAction);
174     m_menu->addAction(m_openAction);
175     m_menu->addAction(m_deleteAction);
176     m_menu->insertSeparator(m_deleteAction);
177 }
178
179
180 QByteArray ProjectList::headerInfo() const
181 {
182     return m_listView->header()->saveState();
183 }
184
185 void ProjectList::setHeaderInfo(const QByteArray &state)
186 {
187     m_listView->header()->restoreState(state);
188 }
189
190 void ProjectList::slotEditClip()
191 {
192     ProjectItem *item = static_cast <ProjectItem*>(m_listView->currentItem());
193     if (!(item->flags() & Qt::ItemIsDragEnabled)) return;
194     if (item && !item->isGroup()) {
195         emit clipSelected(item->referencedClip());
196         emit showClipProperties(item->referencedClip());
197     }
198 }
199
200 void ProjectList::slotOpenClip()
201 {
202     ProjectItem *item = static_cast <ProjectItem*>(m_listView->currentItem());
203     if (item && !item->isGroup()) {
204         if (item->clipType() == IMAGE) {
205             if (KdenliveSettings::defaultimageapp().isEmpty()) KMessageBox::sorry(this, i18n("Please set a default application to open images in the Settings dialog"));
206             else QProcess::startDetached(KdenliveSettings::defaultimageapp(), QStringList() << item->clipUrl().path());
207         }
208         if (item->clipType() == AUDIO) {
209             if (KdenliveSettings::defaultaudioapp().isEmpty()) KMessageBox::sorry(this, i18n("Please set a default application to open audio files in the Settings dialog"));
210             else QProcess::startDetached(KdenliveSettings::defaultaudioapp(), QStringList() << item->clipUrl().path());
211         }
212     }
213 }
214
215 void ProjectList::cleanup()
216 {
217     m_listView->clearSelection();
218     QTreeWidgetItemIterator it(m_listView);
219     ProjectItem *item;
220     while (*it) {
221         item = static_cast <ProjectItem *>(*it);
222         if (item->numReferences() == 0) item->setSelected(true);
223         it++;
224     }
225     slotRemoveClip();
226 }
227
228 void ProjectList::slotReloadClip(const QString &id)
229 {
230     QList<QTreeWidgetItem *> selected;
231     if (id.isEmpty()) selected = m_listView->selectedItems();
232     else selected.append(getItemById(id));
233     ProjectItem *item;
234     for (int i = 0; i < selected.count(); i++) {
235         item = static_cast <ProjectItem *>(selected.at(i));
236         if (item && !item->isGroup()) {
237             if (item->clipType() == IMAGE) {
238                 item->referencedClip()->producer()->set("force_reload", 1);
239             } else if (item->clipType() == TEXT) {
240                 if (!item->referencedClip()->getProperty("xmltemplate").isEmpty()) regenerateTemplate(item);
241             }
242             //requestClipInfo(item->toXml(), item->clipId(), true);
243             // Clear the file_hash value, which will cause a complete reload of the clip
244             emit getFileProperties(item->toXml(), item->clipId(), true);
245         }
246     }
247 }
248
249 void ProjectList::setRenderer(Render *projectRender)
250 {
251     m_render = projectRender;
252     m_listView->setIconSize(QSize(40 * m_render->dar(), 40));
253 }
254
255 void ProjectList::slotClipSelected()
256 {
257     if (m_listView->currentItem()) {
258         ProjectItem *clip = static_cast <ProjectItem*>(m_listView->currentItem());
259         if (!clip->isGroup()) {
260             m_selectedItem = clip;
261             emit clipSelected(clip->referencedClip());
262         }
263         m_editAction->setEnabled(true);
264         m_deleteAction->setEnabled(true);
265         m_reloadAction->setEnabled(true);
266         m_transcodeAction->setEnabled(true);
267         if (clip->clipType() == IMAGE && !KdenliveSettings::defaultimageapp().isEmpty()) {
268             m_openAction->setIcon(KIcon(KdenliveSettings::defaultimageapp()));
269             m_openAction->setEnabled(true);
270         } else if (clip->clipType() == AUDIO && !KdenliveSettings::defaultaudioapp().isEmpty()) {
271             m_openAction->setIcon(KIcon(KdenliveSettings::defaultaudioapp()));
272             m_openAction->setEnabled(true);
273         } else m_openAction->setEnabled(false);
274     } else {
275         emit clipSelected(NULL);
276         m_editAction->setEnabled(false);
277         m_deleteAction->setEnabled(false);
278         m_openAction->setEnabled(false);
279         m_reloadAction->setEnabled(false);
280         m_transcodeAction->setEnabled(false);
281     }
282 }
283
284 void ProjectList::slotPauseMonitor()
285 {
286     if (m_render) m_render->pause();
287 }
288
289 void ProjectList::slotUpdateClipProperties(const QString &id, QMap <QString, QString> properties)
290 {
291     ProjectItem *item = getItemById(id);
292     if (item) {
293         slotUpdateClipProperties(item, properties);
294         if (properties.contains("colour") || properties.contains("resource") || properties.contains("xmldata") || properties.contains("force_aspect_ratio") || properties.contains("templatetext")) {
295             slotRefreshClipThumbnail(item);
296             emit refreshClip();
297         }
298         if (properties.contains("out")) item->changeDuration(properties.value("out").toInt());
299     }
300 }
301
302 void ProjectList::slotUpdateClipProperties(ProjectItem *clip, QMap <QString, QString> properties)
303 {
304     if (!clip) return;
305     if (!clip->isGroup()) clip->setProperties(properties);
306     if (properties.contains("name")) {
307         m_listView->blockSignals(true);
308         clip->setText(1, properties.value("name"));
309         m_listView->blockSignals(false);
310         emit clipNameChanged(clip->clipId(), properties.value("name"));
311     }
312     if (properties.contains("description")) {
313         CLIPTYPE type = clip->clipType();
314         m_listView->blockSignals(true);
315         clip->setText(2, properties.value("description"));
316         m_listView->blockSignals(false);
317         if (KdenliveSettings::activate_nepomuk() && (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST)) {
318             // Use Nepomuk system to store clip description
319             Nepomuk::Resource f(clip->clipUrl().path());
320             f.setDescription(properties.value("description"));
321         }
322         emit projectModified();
323     }
324 }
325
326 void ProjectList::slotItemEdited(QTreeWidgetItem *item, int column)
327 {
328     ProjectItem *clip = static_cast <ProjectItem*>(item);
329     if (column == 2) {
330         if (clip->referencedClip()) {
331             QMap <QString, QString> oldprops;
332             QMap <QString, QString> newprops;
333             oldprops["description"] = clip->referencedClip()->getProperty("description");
334             newprops["description"] = item->text(2);
335
336             if (clip->clipType() == TEXT) {
337                 // This is a text template clip, update the image
338                 /*oldprops.insert("xmldata", clip->referencedClip()->getProperty("xmldata"));
339                 newprops.insert("xmldata", generateTemplateXml(clip->referencedClip()->getProperty("xmltemplate"), item->text(2)).toString());*/
340                 oldprops.insert("templatetext", clip->referencedClip()->getProperty("templatetext"));
341                 newprops.insert("templatetext", item->text(2));
342             }
343
344             slotUpdateClipProperties(clip->clipId(), newprops);
345             EditClipCommand *command = new EditClipCommand(this, clip->clipId(), oldprops, newprops, false);
346             m_commandStack->push(command);
347         }
348     } else if (column == 1) {
349         if (clip->isGroup()) {
350             editFolder(item->text(1), clip->groupName(), clip->clipId());
351             clip->setGroupName(item->text(1));
352             m_doc->clipManager()->addFolder(clip->clipId(), item->text(1));
353             const int children = item->childCount();
354             for (int i = 0; i < children; i++) {
355                 ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
356                 child->setProperty("groupname", item->text(1));
357             }
358         } else {
359             if (clip->referencedClip()) {
360                 QMap <QString, QString> oldprops;
361                 QMap <QString, QString> newprops;
362                 oldprops["name"] = clip->referencedClip()->getProperty("name");
363                 newprops["name"] = item->text(1);
364                 slotUpdateClipProperties(clip, newprops);
365                 emit projectModified();
366                 EditClipCommand *command = new EditClipCommand(this, clip->clipId(), oldprops, newprops, false);
367                 m_commandStack->push(command);
368             }
369         }
370     }
371 }
372
373 void ProjectList::slotContextMenu(const QPoint &pos, QTreeWidgetItem *item)
374 {
375     bool enable = false;
376     if (item) {
377         enable = true;
378     }
379     m_editAction->setEnabled(enable);
380     m_deleteAction->setEnabled(enable);
381     m_reloadAction->setEnabled(enable);
382     m_transcodeAction->setEnabled(enable);
383     if (enable) {
384         ProjectItem *clip = static_cast <ProjectItem*>(item);
385         if (clip->clipType() == IMAGE && !KdenliveSettings::defaultimageapp().isEmpty()) {
386             m_openAction->setIcon(KIcon(KdenliveSettings::defaultimageapp()));
387             m_openAction->setEnabled(true);
388         } else if (clip->clipType() == AUDIO && !KdenliveSettings::defaultaudioapp().isEmpty()) {
389             m_openAction->setIcon(KIcon(KdenliveSettings::defaultaudioapp()));
390             m_openAction->setEnabled(true);
391         } else m_openAction->setEnabled(false);
392     } else m_openAction->setEnabled(false);
393     m_menu->popup(pos);
394 }
395
396 void ProjectList::slotRemoveClip()
397 {
398     if (!m_listView->currentItem()) return;
399     QStringList ids;
400     QMap <QString, QString> folderids;
401     QList<QTreeWidgetItem *> selected = m_listView->selectedItems();
402     ProjectItem *item;
403     for (int i = 0; i < selected.count(); i++) {
404         item = static_cast <ProjectItem *>(selected.at(i));
405         if (item->isGroup()) folderids[item->groupName()] = item->clipId();
406         else ids << item->clipId();
407         if (item->numReferences() > 0) {
408             if (KMessageBox::questionYesNo(this, i18np("Delete clip <b>%2</b>?<br>This will also remove the clip in timeline", "Delete clip <b>%2</b>?<br>This will also remove its %1 clips in timeline", item->numReferences(), item->names().at(1)), i18n("Delete Clip")) != KMessageBox::Yes) return;
409         } else if (item->isGroup() && item->childCount() > 0) {
410             int children = item->childCount();
411             if (KMessageBox::questionYesNo(this, i18np("Delete folder <b>%2</b>?<br>This will also remove the clip in that folder", "Delete folder <b>%2</b>?<br>This will also remove the %1 clips in that folder",  children, item->names().at(1)), i18n("Delete Folder")) != KMessageBox::Yes) return;
412             for (int i = 0; i < children; ++i) {
413                 ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
414                 ids << child->clipId();
415             }
416         }
417     }
418     if (!ids.isEmpty()) m_doc->deleteProjectClip(ids);
419     if (!folderids.isEmpty()) deleteProjectFolder(folderids);
420     if (m_listView->topLevelItemCount() == 0) {
421         m_editAction->setEnabled(false);
422         m_deleteAction->setEnabled(false);
423         m_openAction->setEnabled(false);
424         m_reloadAction->setEnabled(false);
425         m_transcodeAction->setEnabled(false);
426     }
427 }
428
429 void ProjectList::selectItemById(const QString &clipId)
430 {
431     ProjectItem *item = getItemById(clipId);
432     if (item) m_listView->setCurrentItem(item);
433 }
434
435
436 void ProjectList::slotDeleteClip(const QString &clipId)
437 {
438     ProjectItem *item = getItemById(clipId);
439     if (!item) {
440         kDebug() << "/// Cannot find clip to delete";
441         return;
442     }
443     m_listView->blockSignals(true);
444     delete item;
445     m_doc->clipManager()->deleteClip(clipId);
446     m_listView->blockSignals(false);
447     slotClipSelected();
448 }
449
450
451 void ProjectList::editFolder(const QString folderName, const QString oldfolderName, const QString &clipId)
452 {
453     EditFolderCommand *command = new EditFolderCommand(this, folderName, oldfolderName, clipId, false);
454     m_commandStack->push(command);
455     m_doc->setModified(true);
456 }
457
458 void ProjectList::slotAddFolder()
459 {
460     AddFolderCommand *command = new AddFolderCommand(this, i18n("Folder"), QString::number(m_doc->clipManager()->getFreeFolderId()), true);
461     m_commandStack->push(command);
462 }
463
464 void ProjectList::slotAddFolder(const QString foldername, const QString &clipId, bool remove, bool edit)
465 {
466     if (remove) {
467         ProjectItem *item = getFolderItemById(clipId);
468         if (item) {
469             m_doc->clipManager()->deleteFolder(clipId);
470             delete item;
471         }
472     } else {
473         if (edit) {
474             ProjectItem *item = getFolderItemById(clipId);
475             if (item) {
476                 m_listView->blockSignals(true);
477                 item->setGroupName(foldername);
478                 m_listView->blockSignals(false);
479                 m_doc->clipManager()->addFolder(clipId, foldername);
480                 const int children = item->childCount();
481                 for (int i = 0; i < children; i++) {
482                     ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
483                     child->setProperty("groupname", foldername);
484                 }
485             }
486         } else {
487             QStringList text;
488             text << QString() << foldername;
489             m_listView->blockSignals(true);
490             m_listView->setCurrentItem(new ProjectItem(m_listView, text, clipId));
491             m_doc->clipManager()->addFolder(clipId, foldername);
492             m_listView->blockSignals(false);
493         }
494     }
495 }
496
497
498
499 void ProjectList::deleteProjectFolder(QMap <QString, QString> map)
500 {
501     QMapIterator<QString, QString> i(map);
502     QUndoCommand *delCommand = new QUndoCommand();
503     delCommand->setText(i18n("Delete Folder"));
504     while (i.hasNext()) {
505         i.next();
506         new AddFolderCommand(this, i.key(), i.value(), false, delCommand);
507     }
508     m_commandStack->push(delCommand);
509     m_doc->setModified(true);
510 }
511
512 void ProjectList::slotAddClip(DocClipBase *clip, bool getProperties)
513 {
514     m_listView->setEnabled(false);
515     if (getProperties) {
516         m_listView->blockSignals(true);
517         m_refreshed = false;
518         // remove file_hash so that we load all properties for the clip
519         QDomElement e = clip->toXML().cloneNode().toElement();
520         e.removeAttribute("file_hash");
521         m_infoQueue.insert(clip->getId(), e);
522         //m_render->getFileProperties(clip->toXML(), clip->getId(), true);
523     }
524     const QString parent = clip->getProperty("groupid");
525     kDebug() << "Adding clip with groupid: " << parent;
526     ProjectItem *item = NULL;
527     if (!parent.isEmpty()) {
528         ProjectItem *parentitem = getFolderItemById(parent);
529         if (!parentitem) {
530             QStringList text;
531             QString groupName = clip->getProperty("groupname");
532             //kDebug() << "Adding clip to new group: " << groupName;
533             if (groupName.isEmpty()) groupName = i18n("Folder");
534             text << QString() << groupName;
535             parentitem = new ProjectItem(m_listView, text, parent);
536         } else {
537             //kDebug() << "Adding clip to existing group: " << parentitem->groupName();
538         }
539         if (parentitem) item = new ProjectItem(parentitem, clip);
540     }
541     if (item == NULL) item = new ProjectItem(m_listView, clip);
542     KUrl url = clip->fileURL();
543
544     if (getProperties == false && !clip->getClipHash().isEmpty()) {
545         QString cachedPixmap = m_doc->projectFolder().path(KUrl::AddTrailingSlash) + "thumbs/" + clip->getClipHash() + ".png";
546         if (QFile::exists(cachedPixmap)) {
547             item->setIcon(0, QPixmap(cachedPixmap));
548         }
549     }
550
551     if (!url.isEmpty() && KdenliveSettings::activate_nepomuk()) {
552         // if file has Nepomuk comment, use it
553         Nepomuk::Resource f(url.path());
554         QString annotation = f.description();
555         if (!annotation.isEmpty()) item->setText(2, annotation);
556         item->setText(3, QString::number(f.rating()));
557     }
558     if (getProperties && m_listView->isEnabled()) m_listView->blockSignals(false);
559     if (getProperties && !m_queueTimer.isActive()) m_queueTimer.start();
560 }
561
562 void ProjectList::slotResetProjectList()
563 {
564     m_listView->clear();
565     emit clipSelected(NULL);
566     m_thumbnailQueue.clear();
567     m_infoQueue.clear();
568     m_refreshed = false;
569 }
570
571 void ProjectList::requestClipInfo(const QDomElement xml, const QString id)
572 {
573     m_refreshed = false;
574     m_infoQueue.insert(id, xml);
575     //if (m_infoQueue.count() == 1 || ) QTimer::singleShot(300, this, SLOT(slotProcessNextClipInQueue()));
576 }
577
578 void ProjectList::slotProcessNextClipInQueue()
579 {
580     if (m_infoQueue.isEmpty()) {
581         slotProcessNextThumbnail();
582         return;
583     }
584
585     QMap<QString, QDomElement>::const_iterator j = m_infoQueue.constBegin();
586     if (j != m_infoQueue.constEnd()) {
587         const QDomElement dom = j.value();
588         const QString id = j.key();
589         m_infoQueue.remove(j.key());
590         emit getFileProperties(dom, id, false);
591     }
592
593     if (!m_infoQueue.isEmpty() && !m_queueTimer.isActive()) m_queueTimer.start();
594 }
595
596 void ProjectList::slotUpdateClip(const QString &id)
597 {
598     ProjectItem *item = getItemById(id);
599     m_listView->blockSignals(true);
600     if (item) item->setData(1, UsageRole, QString::number(item->numReferences()));
601     m_listView->blockSignals(false);
602 }
603
604 void ProjectList::updateAllClips()
605 {
606     m_listView->setSortingEnabled(false);
607
608     QTreeWidgetItemIterator it(m_listView);
609     DocClipBase *clip;
610     ProjectItem *item;
611     m_listView->blockSignals(true);
612     while (*it) {
613         item = static_cast <ProjectItem *>(*it);
614         if (!item->isGroup()) {
615             clip = item->referencedClip();
616             if (item->referencedClip()->producer() == NULL) {
617                 if (clip->isPlaceHolder() == false) {
618                     requestClipInfo(clip->toXML(), clip->getId());
619                 } else item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
620             } else {
621                 if (item->icon(0).isNull()) {
622                     requestClipThumbnail(clip->getId());
623                 }
624                 if (item->data(1, DurationRole).toString().isEmpty()) {
625                     item->changeDuration(item->referencedClip()->producer()->get_playtime());
626                 }
627             }
628             item->setData(1, UsageRole, QString::number(item->numReferences()));
629             //qApp->processEvents();
630         }
631         ++it;
632     }
633     qApp->processEvents();
634     if (!m_queueTimer.isActive()) m_queueTimer.start();
635
636     if (m_listView->isEnabled()) m_listView->blockSignals(false);
637     m_listView->setSortingEnabled(true);
638     if (m_infoQueue.isEmpty()) slotProcessNextThumbnail();
639 }
640
641 void ProjectList::slotAddClip(const QList <QUrl> givenList, const QString &groupName, const QString &groupId)
642 {
643     if (!m_commandStack) {
644         kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
645     }
646     KUrl::List list;
647     if (givenList.isEmpty()) {
648         // Build list of mime types
649         QStringList mimeTypes = QStringList() << "application/x-kdenlive" << "application/x-kdenlivetitle" << "video/x-flv" << "application/vnd.rn-realmedia" << "video/x-dv" << "video/dv" << "video/x-msvideo" << "video/x-matroska" << "video/mlt-playlist" << "video/mpeg" << "video/ogg" << "video/x-ms-wmv" << "audio/x-flac" << "audio/x-matroska" << "audio/mp4" << "audio/mpeg" << "audio/x-mp3" << "audio/ogg" << "audio/x-wav" << "application/ogg" << "video/mp4" << "video/quicktime" << "image/gif" << "image/jpeg" << "image/png" << "image/x-tga" << "image/x-bmp" << "image/svg+xml" << "image/tiff" << "image/x-xcf-gimp" << "image/x-vnd.adobe.photoshop" << "image/x-pcx" << "image/x-exr";
650
651         QString allExtensions;
652         foreach(const QString& mimeType, mimeTypes) {
653             KMimeType::Ptr mime(KMimeType::mimeType(mimeType));
654             if (mime) {
655                 allExtensions.append(mime->patterns().join(" "));
656                 allExtensions.append(' ');
657             }
658         }
659         const QString dialogFilter = allExtensions.simplified() + ' ' + QLatin1Char('|') + i18n("All Supported Files") + "\n* " + QLatin1Char('|') + i18n("All Files");
660         list = KFileDialog::getOpenUrls(KUrl("kfiledialog:///clipfolder"), dialogFilter, this);
661
662     } else {
663         for (int i = 0; i < givenList.count(); i++)
664             list << givenList.at(i);
665     }
666     if (list.isEmpty()) return;
667
668     if (givenList.isEmpty()) {
669         QStringList groupInfo = getGroup();
670         m_doc->slotAddClipList(list, groupInfo.at(0), groupInfo.at(1));
671     } else m_doc->slotAddClipList(list, groupName, groupId);
672 }
673
674 void ProjectList::slotRemoveInvalidClip(const QString &id, bool replace)
675 {
676     ProjectItem *item = getItemById(id);
677     QTimer::singleShot(300, this, SLOT(slotProcessNextClipInQueue()));
678     if (item) {
679         const QString path = item->referencedClip()->fileURL().path();
680         if (!path.isEmpty()) {
681             if (replace) KMessageBox::sorry(this, i18n("Clip <b>%1</b><br>is invalid, will be removed from project.", path));
682             else {
683                 if (KMessageBox::questionYesNo(this, i18n("Clip <b>%1</b><br>is missing or invalid. Remove it from project?", path), i18n("Invalid clip")) == KMessageBox::Yes) replace = true;
684             }
685         }
686         QStringList ids;
687         ids << id;
688         if (replace) m_doc->deleteProjectClip(ids);
689     }
690 }
691
692 void ProjectList::slotAddColorClip()
693 {
694     if (!m_commandStack) {
695         kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
696     }
697     QDialog *dia = new QDialog(this);
698     Ui::ColorClip_UI dia_ui;
699     dia_ui.setupUi(dia);
700     dia_ui.clip_name->setText(i18n("Color Clip"));
701     dia_ui.clip_duration->setText(KdenliveSettings::color_duration());
702     if (dia->exec() == QDialog::Accepted) {
703         QString color = dia_ui.clip_color->color().name();
704         color = color.replace(0, 1, "0x") + "ff";
705         QStringList groupInfo = getGroup();
706         m_doc->slotCreateColorClip(dia_ui.clip_name->text(), color, dia_ui.clip_duration->text(), groupInfo.at(0), groupInfo.at(1));
707     }
708     delete dia;
709 }
710
711
712 void ProjectList::slotAddSlideshowClip()
713 {
714     if (!m_commandStack) {
715         kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
716     }
717     SlideshowClip *dia = new SlideshowClip(m_timecode, this);
718
719     if (dia->exec() == QDialog::Accepted) {
720         QStringList groupInfo = getGroup();
721         m_doc->slotCreateSlideshowClipFile(dia->clipName(), dia->selectedPath(), dia->imageCount(), dia->clipDuration(), dia->loop(), dia->fade(), dia->lumaDuration(), dia->lumaFile(), dia->softness(), groupInfo.at(0), groupInfo.at(1));
722     }
723     delete dia;
724 }
725
726 void ProjectList::slotAddTitleClip()
727 {
728     QStringList groupInfo = getGroup();
729     m_doc->slotCreateTextClip(groupInfo.at(0), groupInfo.at(1));
730 }
731
732 void ProjectList::slotAddTitleTemplateClip()
733 {
734     QStringList groupInfo = getGroup();
735     if (!m_commandStack) {
736         kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
737     }
738
739     // Get the list of existing templates
740     QStringList filter;
741     filter << "*.kdenlivetitle";
742     const QString path = m_doc->projectFolder().path(KUrl::AddTrailingSlash) + "titles/";
743     QStringList templateFiles = QDir(path).entryList(filter, QDir::Files);
744
745     QDialog *dia = new QDialog(this);
746     Ui::TemplateClip_UI dia_ui;
747     dia_ui.setupUi(dia);
748     for (int i = 0; i < templateFiles.size(); ++i) {
749         dia_ui.template_list->comboBox()->addItem(templateFiles.at(i), path + templateFiles.at(i));
750     }
751     dia_ui.template_list->fileDialog()->setFilter("*.kdenlivetitle");
752     //warning: setting base directory doesn't work??
753     KUrl startDir(path);
754     dia_ui.template_list->fileDialog()->setUrl(startDir);
755     dia_ui.text_box->setHidden(true);
756     if (dia->exec() == QDialog::Accepted) {
757         QString textTemplate = dia_ui.template_list->comboBox()->itemData(dia_ui.template_list->comboBox()->currentIndex()).toString();
758         if (textTemplate.isEmpty()) textTemplate = dia_ui.template_list->comboBox()->currentText();
759         // Create a cloned template clip
760         m_doc->slotCreateTextTemplateClip(groupInfo.at(0), groupInfo.at(1), KUrl(textTemplate));
761     }
762     delete dia;
763 }
764
765 QStringList ProjectList::getGroup() const
766 {
767     QStringList result;
768     ProjectItem *item = static_cast <ProjectItem*>(m_listView->currentItem());
769     if (item && !item->isGroup()) {
770         while (item->parent()) {
771             item = static_cast <ProjectItem*>(item->parent());
772             if (item->isGroup()) break;
773         }
774     }
775     if (item && item->isGroup()) {
776         result << item->groupName();
777         result << item->clipId();
778     } else result << QString() << QString();
779     return result;
780 }
781
782 void ProjectList::setDocument(KdenliveDoc *doc)
783 {
784     m_listView->blockSignals(true);
785     m_listView->clear();
786     m_listView->setSortingEnabled(false);
787     emit clipSelected(NULL);
788     m_thumbnailQueue.clear();
789     m_infoQueue.clear();
790     m_refreshed = false;
791     m_fps = doc->fps();
792     m_timecode = doc->timecode();
793     m_commandStack = doc->commandStack();
794     m_doc = doc;
795
796     QMap <QString, QString> flist = doc->clipManager()->documentFolderList();
797     QMapIterator<QString, QString> f(flist);
798     while (f.hasNext()) {
799         f.next();
800         (void) new ProjectItem(m_listView, QStringList() << QString() << f.value(), f.key());
801     }
802
803     QList <DocClipBase*> list = doc->clipManager()->documentClipList();
804     for (int i = 0; i < list.count(); i++) {
805         slotAddClip(list.at(i), false);
806     }
807
808     m_listView->blockSignals(false);
809     m_toolbar->setEnabled(true);
810     connect(m_doc->clipManager(), SIGNAL(reloadClip(const QString &)), this, SLOT(slotReloadClip(const QString &)));
811     connect(m_doc->clipManager(), SIGNAL(checkAllClips()), this, SLOT(updateAllClips()));
812 }
813
814 QDomElement ProjectList::producersList()
815 {
816     QDomDocument doc;
817     QDomElement prods = doc.createElement("producerlist");
818     doc.appendChild(prods);
819     kDebug() << "////////////  PRO LISTĀ BUILD PRDSLIST ";
820     QTreeWidgetItemIterator it(m_listView);
821     while (*it) {
822         if (!((ProjectItem *)(*it))->isGroup())
823             prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
824         ++it;
825     }
826     return prods;
827 }
828
829 void ProjectList::slotCheckForEmptyQueue()
830 {
831     if (!m_refreshed && m_thumbnailQueue.isEmpty() && m_infoQueue.isEmpty()) {
832         m_refreshed = true;
833         emit loadingIsOver();
834         emit displayMessage(QString(), DefaultMessage);
835         m_listView->blockSignals(false);
836         m_listView->setEnabled(true);
837     } else if (!m_refreshed) QTimer::singleShot(300, this, SLOT(slotCheckForEmptyQueue()));
838 }
839
840 void ProjectList::reloadClipThumbnails()
841 {
842     m_thumbnailQueue.clear();
843     QTreeWidgetItemIterator it(m_listView);
844     while (*it) {
845         if (!((ProjectItem *)(*it))->isGroup())
846             m_thumbnailQueue << ((ProjectItem *)(*it))->clipId();
847         ++it;
848     }
849     QTimer::singleShot(300, this, SLOT(slotProcessNextThumbnail()));
850 }
851
852 void ProjectList::requestClipThumbnail(const QString id)
853 {
854     m_thumbnailQueue.append(id);
855 }
856
857 void ProjectList::slotProcessNextThumbnail()
858 {
859     if (m_thumbnailQueue.isEmpty() && m_infoQueue.isEmpty()) {
860         slotCheckForEmptyQueue();
861         return;
862     }
863     if (!m_infoQueue.isEmpty()) {
864         //QTimer::singleShot(300, this, SLOT(slotProcessNextThumbnail()));
865         return;
866     }
867     slotRefreshClipThumbnail(m_thumbnailQueue.takeFirst(), false);
868     if (m_thumbnailQueue.count() > 1) {
869         emit displayMessage(i18n("Loading clips (%1)", m_thumbnailQueue.count()), InformationMessage);
870         qApp->processEvents();
871     }
872 }
873
874 void ProjectList::slotRefreshClipThumbnail(const QString &clipId, bool update)
875 {
876     ProjectItem *item = getItemById(clipId);
877     if (item) slotRefreshClipThumbnail(item, update);
878     else slotProcessNextThumbnail();
879 }
880
881 void ProjectList::slotRefreshClipThumbnail(ProjectItem *item, bool update)
882 {
883     if (item) {
884         DocClipBase *clip = item->referencedClip();
885         if (!clip) {
886             slotProcessNextThumbnail();
887             return;
888         }
889         QPixmap pix;
890         int height = 50;
891         int width = (int)(height  * m_render->dar());
892         if (clip->clipType() == AUDIO) pix = KIcon("audio-x-generic").pixmap(QSize(width, height));
893         else if (clip->clipType() == IMAGE) pix = QPixmap::fromImage(KThumb::getFrame(item->referencedClip()->producer(), 0, width, height));
894         else pix = item->referencedClip()->thumbProducer()->extractImage(item->referencedClip()->getClipThumbFrame(), width, height);
895         if (!pix.isNull()) {
896             m_listView->blockSignals(true);
897             item->setIcon(0, pix);
898             if (m_listView->isEnabled()) m_listView->blockSignals(false);
899             m_doc->cachePixmap(item->getClipHash(), pix);
900         }
901         if (update) emit projectModified();
902         QTimer::singleShot(100, this, SLOT(slotProcessNextThumbnail()));
903     }
904 }
905
906 void ProjectList::slotReplyGetFileProperties(const QString &clipId, Mlt::Producer *producer, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata, bool replace)
907 {
908     ProjectItem *item = getItemById(clipId);
909     if (item && producer) {
910         m_listView->blockSignals(true);
911         item->setProperties(properties, metadata);
912         Q_ASSERT_X(item->referencedClip(), "void ProjectList::slotReplyGetFileProperties", QString("Item with groupName %1 does not have a clip associated").arg(item->groupName()).toLatin1());
913         item->referencedClip()->setProducer(producer, replace);
914         emit receivedClipDuration(clipId);
915         if (m_listView->isEnabled() && replace) {
916             // update clip in clip monitor
917             emit clipSelected(NULL);
918             emit clipSelected(item->referencedClip());
919         }
920         /*else {
921             // Check if duration changed.
922             emit receivedClipDuration(clipId);
923             delete producer;
924         }*/
925         if (m_listView->isEnabled()) m_listView->blockSignals(false);
926         if (item->icon(0).isNull()) {
927             requestClipThumbnail(clipId);
928         }
929     } else kDebug() << "////////  COULD NOT FIND CLIP TO UPDATE PRPS...";
930
931     slotProcessNextClipInQueue();
932 }
933
934 void ProjectList::slotReplyGetImage(const QString &clipId, const QPixmap &pix)
935 {
936     ProjectItem *item = getItemById(clipId);
937     if (item && !pix.isNull()) {
938         m_listView->blockSignals(true);
939         item->setIcon(0, pix);
940         m_doc->cachePixmap(item->getClipHash(), pix);
941         if (m_listView->isEnabled()) m_listView->blockSignals(false);
942     }
943 }
944
945 ProjectItem *ProjectList::getItemById(const QString &id)
946 {
947     ProjectItem *item;
948     QTreeWidgetItemIterator it(m_listView);
949     while (*it) {
950         item = static_cast<ProjectItem *>(*it);
951         if (item->clipId() == id && item->clipType() != FOLDER)
952             return item;
953         ++it;
954     }
955     return NULL;
956 }
957
958 ProjectItem *ProjectList::getFolderItemById(const QString &id)
959 {
960     ProjectItem *item;
961     QTreeWidgetItemIterator it(m_listView);
962     while (*it) {
963         item = static_cast<ProjectItem *>(*it);
964         if (item->clipId() == id && item->clipType() == FOLDER)
965             return item;
966         ++it;
967     }
968     return NULL;
969 }
970
971 void ProjectList::slotSelectClip(const QString &ix)
972 {
973     ProjectItem *clip = getItemById(ix);
974     if (clip) {
975         m_listView->setCurrentItem(clip);
976         m_listView->scrollToItem(clip);
977         m_editAction->setEnabled(true);
978         m_deleteAction->setEnabled(true);
979         m_reloadAction->setEnabled(true);
980         m_transcodeAction->setEnabled(true);
981         if (clip->clipType() == IMAGE && !KdenliveSettings::defaultimageapp().isEmpty()) {
982             m_openAction->setIcon(KIcon(KdenliveSettings::defaultimageapp()));
983             m_openAction->setEnabled(true);
984         } else if (clip->clipType() == AUDIO && !KdenliveSettings::defaultaudioapp().isEmpty()) {
985             m_openAction->setIcon(KIcon(KdenliveSettings::defaultaudioapp()));
986             m_openAction->setEnabled(true);
987         } else m_openAction->setEnabled(false);
988     }
989 }
990
991 QString ProjectList::currentClipUrl() const
992 {
993     ProjectItem *item = static_cast <ProjectItem*>(m_listView->currentItem());
994     if (item == NULL) return QString();
995     return item->clipUrl().path();
996 }
997
998 void ProjectList::regenerateTemplate(const QString &id)
999 {
1000     ProjectItem *clip = getItemById(id);
1001     if (clip) regenerateTemplate(clip);
1002 }
1003
1004 void ProjectList::regenerateTemplate(ProjectItem *clip)
1005 {
1006     //TODO: remove this unused method, only force_reload is necessary
1007     clip->referencedClip()->producer()->set("force_reload", 1);
1008 }
1009
1010 QDomDocument ProjectList::generateTemplateXml(QString path, const QString &replaceString)
1011 {
1012     QDomDocument doc;
1013     QFile file(path);
1014     if (!file.open(QIODevice::ReadOnly)) {
1015         kWarning() << "ERROR, CANNOT READ: " << path;
1016         return doc;
1017     }
1018     if (!doc.setContent(&file)) {
1019         kWarning() << "ERROR, CANNOT READ: " << path;
1020         file.close();
1021         return doc;
1022     }
1023     file.close();
1024     QDomNodeList texts = doc.elementsByTagName("content");
1025     for (int i = 0; i < texts.count(); i++) {
1026         QString data = texts.item(i).firstChild().nodeValue();
1027         data.replace("%s", replaceString);
1028         texts.item(i).firstChild().setNodeValue(data);
1029     }
1030     return doc;
1031 }
1032
1033 #include "projectlist.moc"