]> git.sesse.net Git - kdenlive/blob - src/projectlist.cpp
Fix possible crash in tree view
[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 "timecodedisplay.h"
35 #include "profilesdialog.h"
36 #include "editclipcommand.h"
37 #include "editclipcutcommand.h"
38 #include "editfoldercommand.h"
39 #include "addclipcutcommand.h"
40
41 #include "ui_templateclip_ui.h"
42
43 #include <KDebug>
44 #include <KAction>
45 #include <KLocale>
46 #include <KFileDialog>
47 #include <KInputDialog>
48 #include <KMessageBox>
49 #include <KIO/NetAccess>
50 #include <KFileItem>
51 #include <KApplication>
52 #ifdef NEPOMUK
53 #include <nepomuk/global.h>
54 #include <nepomuk/resourcemanager.h>
55 //#include <nepomuk/tag.h>
56 #endif
57
58 #include <QMouseEvent>
59 #include <QStylePainter>
60 #include <QPixmap>
61 #include <QIcon>
62 #include <QMenu>
63 #include <QProcess>
64 #include <QHeaderView>
65 #include <QInputDialog>
66
67 ProjectList::ProjectList(QWidget *parent) :
68     QWidget(parent),
69     m_render(NULL),
70     m_fps(-1),
71     m_commandStack(NULL),
72     m_openAction(NULL),
73     m_reloadAction(NULL),
74     m_transcodeAction(NULL),
75     m_doc(NULL),
76     m_refreshed(false),
77     m_infoQueue(),
78     m_thumbnailQueue()
79 {
80     QVBoxLayout *layout = new QVBoxLayout;
81     layout->setContentsMargins(0, 0, 0, 0);
82     layout->setSpacing(0);
83
84     // setup toolbar
85     QFrame *frame = new QFrame;
86     frame->setFrameStyle(QFrame::NoFrame);
87     QHBoxLayout *box = new QHBoxLayout;
88     KTreeWidgetSearchLine *searchView = new KTreeWidgetSearchLine;
89
90     box->addWidget(searchView);
91     //int s = style()->pixelMetric(QStyle::PM_SmallIconSize);
92     //m_toolbar->setIconSize(QSize(s, s));
93
94     m_addButton = new QToolButton;
95     m_addButton->setPopupMode(QToolButton::MenuButtonPopup);
96     m_addButton->setAutoRaise(true);
97     box->addWidget(m_addButton);
98
99     m_editButton = new QToolButton;
100     m_editButton->setAutoRaise(true);
101     box->addWidget(m_editButton);
102
103     m_deleteButton = new QToolButton;
104     m_deleteButton->setAutoRaise(true);
105     box->addWidget(m_deleteButton);
106     frame->setLayout(box);
107     layout->addWidget(frame);
108
109     m_listView = new ProjectListView;
110     layout->addWidget(m_listView);
111     setLayout(layout);
112     searchView->setTreeWidget(m_listView);
113
114     m_proxyAction = new QAction(i18n("Proxy clip"), this);
115     m_proxyAction->setCheckable(true);
116     m_proxyAction->setChecked(false);
117     connect(m_proxyAction, SIGNAL(toggled(bool)), this, SLOT(slotProxyCurrentItem(bool)));
118     
119     
120     m_queueTimer.setInterval(100);
121     connect(&m_queueTimer, SIGNAL(timeout()), this, SLOT(slotProcessNextClipInQueue()));
122     m_queueTimer.setSingleShot(true);
123
124
125     connect(m_listView, SIGNAL(projectModified()), this, SIGNAL(projectModified()));
126     connect(m_listView, SIGNAL(itemSelectionChanged()), this, SLOT(slotClipSelected()));
127     connect(m_listView, SIGNAL(focusMonitor()), this, SLOT(slotClipSelected()));
128     connect(m_listView, SIGNAL(pauseMonitor()), this, SLOT(slotPauseMonitor()));
129     connect(m_listView, SIGNAL(requestMenu(const QPoint &, QTreeWidgetItem *)), this, SLOT(slotContextMenu(const QPoint &, QTreeWidgetItem *)));
130     connect(m_listView, SIGNAL(addClip()), this, SLOT(slotAddClip()));
131     connect(m_listView, SIGNAL(addClip(const QList <QUrl>, const QString &, const QString &)), this, SLOT(slotAddClip(const QList <QUrl>, const QString &, const QString &)));
132     connect(m_listView, SIGNAL(addClipCut(const QString &, int, int)), this, SLOT(slotAddClipCut(const QString &, int, int)));
133     connect(m_listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotItemEdited(QTreeWidgetItem *, int)));
134     connect(m_listView, SIGNAL(showProperties(DocClipBase *)), this, SIGNAL(showClipProperties(DocClipBase *)));
135
136     m_listViewDelegate = new ItemDelegate(m_listView);
137     m_listView->setItemDelegate(m_listViewDelegate);
138 #ifdef NEPOMUK
139     if (KdenliveSettings::activate_nepomuk()) {
140         Nepomuk::ResourceManager::instance()->init();
141         if (!Nepomuk::ResourceManager::instance()->initialized()) {
142             kDebug() << "Cannot communicate with Nepomuk, DISABLING it";
143             KdenliveSettings::setActivate_nepomuk(false);
144         }
145     }
146 #endif
147 }
148
149 ProjectList::~ProjectList()
150 {
151     delete m_menu;
152     m_listView->blockSignals(true);
153     m_listView->clear();
154     delete m_listViewDelegate;
155 }
156
157 void ProjectList::focusTree() const
158 {
159     m_listView->setFocus();
160 }
161
162 void ProjectList::setupMenu(QMenu *addMenu, QAction *defaultAction)
163 {
164     QList <QAction *> actions = addMenu->actions();
165     for (int i = 0; i < actions.count(); i++) {
166         if (actions.at(i)->data().toString() == "clip_properties") {
167             m_editButton->setDefaultAction(actions.at(i));
168             actions.removeAt(i);
169             i--;
170         } else if (actions.at(i)->data().toString() == "delete_clip") {
171             m_deleteButton->setDefaultAction(actions.at(i));
172             actions.removeAt(i);
173             i--;
174         } else if (actions.at(i)->data().toString() == "edit_clip") {
175             m_openAction = actions.at(i);
176             actions.removeAt(i);
177             i--;
178         } else if (actions.at(i)->data().toString() == "reload_clip") {
179             m_reloadAction = actions.at(i);
180             actions.removeAt(i);
181             i--;
182         }
183     }
184
185     QMenu *m = new QMenu();
186     m->addActions(actions);
187     m_addButton->setMenu(m);
188     m_addButton->setDefaultAction(defaultAction);
189     m_menu = new QMenu();
190     m_menu->addActions(addMenu->actions());
191 }
192
193 void ProjectList::setupGeneratorMenu(QMenu *addMenu, QMenu *transcodeMenu, QMenu *inTimelineMenu)
194 {
195     if (!addMenu)
196         return;
197     QMenu *menu = m_addButton->menu();
198     menu->addMenu(addMenu);
199     m_addButton->setMenu(menu);
200
201     m_menu->addMenu(addMenu);
202     if (addMenu->isEmpty())
203         addMenu->setEnabled(false);
204     m_menu->addMenu(transcodeMenu);
205     if (transcodeMenu->isEmpty())
206         transcodeMenu->setEnabled(false);
207     m_transcodeAction = transcodeMenu;
208     m_menu->addAction(m_reloadAction);
209     m_menu->addAction(m_proxyAction);
210     m_menu->addMenu(inTimelineMenu);
211     inTimelineMenu->setEnabled(false);
212     m_menu->addAction(m_editButton->defaultAction());
213     m_menu->addAction(m_openAction);
214     m_menu->addAction(m_deleteButton->defaultAction());
215     m_menu->insertSeparator(m_deleteButton->defaultAction());
216 }
217
218
219 QByteArray ProjectList::headerInfo() const
220 {
221     return m_listView->header()->saveState();
222 }
223
224 void ProjectList::setHeaderInfo(const QByteArray &state)
225 {
226     m_listView->header()->restoreState(state);
227 }
228
229 void ProjectList::updateProjectFormat(Timecode t)
230 {
231     m_timecode = t;
232 }
233
234 void ProjectList::slotEditClip()
235 {
236     QList<QTreeWidgetItem *> list = m_listView->selectedItems();
237     if (list.count() > 1) {
238         editClipSelection(list);
239         return;
240     }
241     ProjectItem *item;
242     if (!m_listView->currentItem() || m_listView->currentItem()->type() == PROJECTFOLDERTYPE)
243         return;
244     if (m_listView->currentItem()->type() == PROJECTSUBCLIPTYPE)
245         item = static_cast <ProjectItem*>(m_listView->currentItem()->parent());
246     else
247         item = static_cast <ProjectItem*>(m_listView->currentItem());
248     if (item && (item->flags() & Qt::ItemIsDragEnabled)) {
249         emit clipSelected(item->referencedClip());
250         emit showClipProperties(item->referencedClip());
251     }
252 }
253
254 void ProjectList::editClipSelection(QList<QTreeWidgetItem *> list)
255 {
256     // Gather all common properties
257     QMap <QString, QString> commonproperties;
258     QList <DocClipBase *> clipList;
259     commonproperties.insert("force_aspect_num", "-");
260     commonproperties.insert("force_aspect_den", "-");
261     commonproperties.insert("force_fps", "-");
262     commonproperties.insert("force_progressive", "-");
263     commonproperties.insert("force_tff", "-");
264     commonproperties.insert("threads", "-");
265     commonproperties.insert("video_index", "-");
266     commonproperties.insert("audio_index", "-");
267     commonproperties.insert("force_colorspace", "-");
268     commonproperties.insert("full_luma", "-");
269
270     bool allowDurationChange = true;
271     int commonDuration = -1;
272     ProjectItem *item;
273     for (int i = 0; i < list.count(); i++) {
274         item = NULL;
275         if (list.at(i)->type() == PROJECTFOLDERTYPE)
276             continue;
277         if (list.at(i)->type() == PROJECTSUBCLIPTYPE)
278             item = static_cast <ProjectItem*>(list.at(i)->parent());
279         else
280             item = static_cast <ProjectItem*>(list.at(i));
281         if (!(item->flags() & Qt::ItemIsDragEnabled))
282             continue;
283         if (item) {
284             // check properties
285             DocClipBase *clip = item->referencedClip();
286             if (clipList.contains(clip)) continue;
287             if (clip->clipType() != COLOR && clip->clipType() != IMAGE && clip->clipType() != TEXT)
288                 allowDurationChange = false;
289             if (allowDurationChange && commonDuration != 0) {
290                 if (commonDuration == -1)
291                     commonDuration = clip->duration().frames(m_fps);
292                 else if (commonDuration != clip->duration().frames(m_fps))
293                     commonDuration = 0;
294             }
295             clipList.append(clip);
296             QMap <QString, QString> clipprops = clip->properties();
297             QMapIterator<QString, QString> p(commonproperties);
298             while (p.hasNext()) {
299                 p.next();
300                 if (p.value().isEmpty()) continue;
301                 if (clipprops.contains(p.key())) {
302                     if (p.value() == "-")
303                         commonproperties.insert(p.key(), clipprops.value(p.key()));
304                     else if (p.value() != clipprops.value(p.key()))
305                         commonproperties.insert(p.key(), QString());
306                 } else {
307                     commonproperties.insert(p.key(), QString());
308                 }
309             }
310         }
311     }
312     if (allowDurationChange)
313         commonproperties.insert("out", QString::number(commonDuration));
314     QMapIterator<QString, QString> p(commonproperties);
315     while (p.hasNext()) {
316         p.next();
317         kDebug() << "Result: " << p.key() << " = " << p.value();
318     }
319     emit showClipProperties(clipList, commonproperties);
320 }
321
322 void ProjectList::slotOpenClip()
323 {
324     ProjectItem *item;
325     if (!m_listView->currentItem() || m_listView->currentItem()->type() == PROJECTFOLDERTYPE)
326         return;
327     if (m_listView->currentItem()->type() == QTreeWidgetItem::UserType + 1)
328         item = static_cast <ProjectItem*>(m_listView->currentItem()->parent());
329     else
330         item = static_cast <ProjectItem*>(m_listView->currentItem());
331     if (item) {
332         if (item->clipType() == IMAGE) {
333             if (KdenliveSettings::defaultimageapp().isEmpty())
334                 KMessageBox::sorry(kapp->activeWindow(), i18n("Please set a default application to open images in the Settings dialog"));
335             else
336                 QProcess::startDetached(KdenliveSettings::defaultimageapp(), QStringList() << item->clipUrl().path());
337         }
338         if (item->clipType() == AUDIO) {
339             if (KdenliveSettings::defaultaudioapp().isEmpty())
340                 KMessageBox::sorry(kapp->activeWindow(), i18n("Please set a default application to open audio files in the Settings dialog"));
341             else
342                 QProcess::startDetached(KdenliveSettings::defaultaudioapp(), QStringList() << item->clipUrl().path());
343         }
344     }
345 }
346
347 void ProjectList::cleanup()
348 {
349     m_listView->clearSelection();
350     QTreeWidgetItemIterator it(m_listView);
351     ProjectItem *item;
352     while (*it) {
353         if ((*it)->type() != PROJECTCLIPTYPE) {
354             it++;
355             continue;
356         }
357         item = static_cast <ProjectItem *>(*it);
358         if (item->numReferences() == 0)
359             item->setSelected(true);
360         it++;
361     }
362     slotRemoveClip();
363 }
364
365 void ProjectList::trashUnusedClips()
366 {
367     QTreeWidgetItemIterator it(m_listView);
368     ProjectItem *item;
369     QStringList ids;
370     QStringList urls;
371     while (*it) {
372         if ((*it)->type() != PROJECTCLIPTYPE) {
373             it++;
374             continue;
375         }
376         item = static_cast <ProjectItem *>(*it);
377         if (item->numReferences() == 0) {
378             ids << item->clipId();
379             KUrl url = item->clipUrl();
380             if (!url.isEmpty() && !urls.contains(url.path()))
381                 urls << url.path();
382         }
383         it++;
384     }
385
386     // Check that we don't use the URL in another clip
387     QTreeWidgetItemIterator it2(m_listView);
388     while (*it2) {
389         if ((*it2)->type() != PROJECTCLIPTYPE) {
390             it2++;
391             continue;
392         }
393         item = static_cast <ProjectItem *>(*it2);
394         if (item->numReferences() > 0) {
395             KUrl url = item->clipUrl();
396             if (!url.isEmpty() && urls.contains(url.path())) urls.removeAll(url.path());
397         }
398         it2++;
399     }
400
401     emit deleteProjectClips(ids, QMap <QString, QString>());
402     for (int i = 0; i < urls.count(); i++)
403         KIO::NetAccess::del(KUrl(urls.at(i)), this);
404 }
405
406 void ProjectList::slotReloadClip(const QString &id)
407 {
408     QList<QTreeWidgetItem *> selected;
409     if (id.isEmpty())
410         selected = m_listView->selectedItems();
411     else {
412         ProjectItem *itemToReLoad = getItemById(id);
413         if (itemToReLoad) selected.append(itemToReLoad);
414     }
415     ProjectItem *item;
416     for (int i = 0; i < selected.count(); i++) {
417         if (selected.at(i)->type() != PROJECTCLIPTYPE) {
418             if (selected.at(i)->type() == PROJECTFOLDERTYPE) {
419                 for (int j = 0; j < selected.at(i)->childCount(); j++)
420                     selected.append(selected.at(i)->child(j));
421             }
422             continue;
423         }
424         item = static_cast <ProjectItem *>(selected.at(i));
425         if (item) {
426             CLIPTYPE t = item->clipType();
427             if (t == TEXT) {
428                 if (!item->referencedClip()->getProperty("xmltemplate").isEmpty())
429                     regenerateTemplate(item);
430             } else if (t != COLOR && t != SLIDESHOW && item->referencedClip() &&  item->referencedClip()->checkHash() == false) {
431                 item->referencedClip()->setPlaceHolder(true);
432                 item->setProperty("file_hash", QString());
433             } else if (t == IMAGE) {
434                 item->referencedClip()->producer()->set("force_reload", 1);
435             }
436
437             QDomElement e = item->toXml();
438             // Make sure we get the correct producer length if it was adjusted in timeline
439             if (t == COLOR || t == IMAGE || t == SLIDESHOW || t == TEXT) {
440                 int length = QString(item->referencedClip()->producerProperty("length")).toInt();
441                 if (length > 0 && !e.hasAttribute("length")) {
442                     e.setAttribute("length", length);
443                     e.setAttribute("out", length - 1);
444                 }
445             }
446             
447             emit getFileProperties(e, item->clipId(), m_listView->iconSize().height(), true);
448         }
449     }
450 }
451
452 void ProjectList::slotModifiedClip(const QString &id)
453 {
454     ProjectItem *item = getItemById(id);
455     if (item) {
456         QPixmap pixmap = qVariantValue<QPixmap>(item->data(0, Qt::DecorationRole));
457         if (!pixmap.isNull()) {
458             QPainter p(&pixmap);
459             p.fillRect(0, 0, pixmap.width(), pixmap.height(), QColor(255, 255, 255, 200));
460             p.drawPixmap(0, 0, KIcon("view-refresh").pixmap(m_listView->iconSize()));
461             p.end();
462         } else {
463             pixmap = KIcon("view-refresh").pixmap(m_listView->iconSize());
464         }
465         item->setData(0, Qt::DecorationRole, pixmap);
466     }
467 }
468
469 void ProjectList::slotMissingClip(const QString &id)
470 {
471     ProjectItem *item = getItemById(id);
472     if (item) {
473         item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDropEnabled);
474         if (item->referencedClip()) {
475             item->referencedClip()->setPlaceHolder(true);
476             if (m_render == NULL) kDebug() << "*********  ERROR, NULL RENDR";
477             item->referencedClip()->setProducer(m_render->invalidProducer(id), true);
478             item->slotSetToolTip();
479             emit clipNeedsReload(id, true);
480         }
481     }
482     update();
483     emit displayMessage(i18n("Check missing clips"), -2);
484     emit updateRenderStatus();
485 }
486
487 void ProjectList::slotAvailableClip(const QString &id)
488 {
489     ProjectItem *item = getItemById(id);
490     if (item == NULL)
491         return;
492     item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsDropEnabled);
493     if (item->referencedClip()) { // && item->referencedClip()->checkHash() == false) {
494         item->setProperty("file_hash", QString());
495         slotReloadClip(id);
496     }
497     /*else {
498     item->referencedClip()->setValid();
499     item->slotSetToolTip();
500     }
501     update();*/
502     emit updateRenderStatus();
503 }
504
505 bool ProjectList::hasMissingClips()
506 {
507     bool missing = false;
508     QTreeWidgetItemIterator it(m_listView);
509     while (*it) {
510         if ((*it)->type() == PROJECTCLIPTYPE && !((*it)->flags() & Qt::ItemIsDragEnabled)) {
511             missing = true;
512             break;
513         }
514         it++;
515     }
516     return missing;
517 }
518
519 void ProjectList::setRenderer(Render *projectRender)
520 {
521     m_render = projectRender;
522     m_listView->setIconSize(QSize((ProjectItem::itemDefaultHeight() - 2) * m_render->dar(), ProjectItem::itemDefaultHeight() - 2));
523 }
524
525 void ProjectList::slotClipSelected()
526 {
527     if (!m_listView->isEnabled()) return;
528     if (m_listView->currentItem()) {
529         if (m_listView->currentItem()->type() == PROJECTFOLDERTYPE) {
530             emit clipSelected(NULL);
531             m_editButton->defaultAction()->setEnabled(false);
532             m_deleteButton->defaultAction()->setEnabled(true);
533             m_openAction->setEnabled(false);
534             m_reloadAction->setEnabled(false);
535             m_transcodeAction->setEnabled(false);
536             m_proxyAction->setEnabled(false);
537         } else {
538             ProjectItem *clip;
539             if (m_listView->currentItem()->type() == PROJECTSUBCLIPTYPE) {
540                 // this is a sub item, use base clip
541                 m_deleteButton->defaultAction()->setEnabled(true);
542                 clip = static_cast <ProjectItem*>(m_listView->currentItem()->parent());
543                 if (clip == NULL) kDebug() << "-----------ERROR";
544                 SubProjectItem *sub = static_cast <SubProjectItem*>(m_listView->currentItem());
545                 emit clipSelected(clip->referencedClip(), sub->zone());
546                 m_transcodeAction->setEnabled(false);
547                 return;
548             }
549             clip = static_cast <ProjectItem*>(m_listView->currentItem());
550             if (clip)
551                 emit clipSelected(clip->referencedClip());
552             m_editButton->defaultAction()->setEnabled(true);
553             m_deleteButton->defaultAction()->setEnabled(true);
554             m_reloadAction->setEnabled(true);
555             m_transcodeAction->setEnabled(true);
556             if (clip && clip->clipType() == IMAGE && !KdenliveSettings::defaultimageapp().isEmpty()) {
557                 m_openAction->setIcon(KIcon(KdenliveSettings::defaultimageapp()));
558                 m_openAction->setEnabled(true);
559             } else if (clip && clip->clipType() == AUDIO && !KdenliveSettings::defaultaudioapp().isEmpty()) {
560                 m_openAction->setIcon(KIcon(KdenliveSettings::defaultaudioapp()));
561                 m_openAction->setEnabled(true);
562             } else {
563                 m_openAction->setEnabled(false);
564             }
565             // Display relevant transcoding actions only
566             adjustTranscodeActions(clip);
567             // Display uses in timeline
568             emit findInTimeline(clip->clipId());
569         }
570     } else {
571         emit clipSelected(NULL);
572         m_editButton->defaultAction()->setEnabled(false);
573         m_deleteButton->defaultAction()->setEnabled(false);
574         m_openAction->setEnabled(false);
575         m_reloadAction->setEnabled(false);
576         m_transcodeAction->setEnabled(false);
577     }
578 }
579
580 void ProjectList::adjustProxyActions(ProjectItem *clip) const
581 {
582     if (clip == NULL || clip->type() != PROJECTCLIPTYPE || clip->clipType() == COLOR || clip->clipType() == TEXT || clip->clipType() == PLAYLIST || clip->clipType() == SLIDESHOW || clip->clipType() == AUDIO) {
583         m_proxyAction->setEnabled(false);
584         return;
585     }
586     m_proxyAction->setEnabled(true);
587     m_proxyAction->setChecked(clip->hasProxy());
588 }
589
590 void ProjectList::adjustTranscodeActions(ProjectItem *clip) const
591 {
592     if (clip == NULL || clip->type() != PROJECTCLIPTYPE || clip->clipType() == COLOR || clip->clipType() == TEXT || clip->clipType() == PLAYLIST || clip->clipType() == SLIDESHOW) {
593         m_transcodeAction->setEnabled(false);
594         return;
595     }
596     m_transcodeAction->setEnabled(true);
597     QList<QAction *> transcodeActions = m_transcodeAction->actions();
598     QStringList data;
599     QString condition;
600     for (int i = 0; i < transcodeActions.count(); i++) {
601         data = transcodeActions.at(i)->data().toStringList();
602         if (data.count() > 2) {
603             condition = data.at(2);
604             if (condition.startsWith("vcodec"))
605                 transcodeActions.at(i)->setEnabled(clip->referencedClip()->hasVideoCodec(condition.section('=', 1, 1)));
606             else if (condition.startsWith("acodec"))
607                 transcodeActions.at(i)->setEnabled(clip->referencedClip()->hasVideoCodec(condition.section('=', 1, 1)));
608         }
609     }
610
611 }
612
613 void ProjectList::slotPauseMonitor()
614 {
615     if (m_render)
616         m_render->pause();
617 }
618
619 void ProjectList::slotUpdateClipProperties(const QString &id, QMap <QString, QString> properties)
620 {
621     ProjectItem *item = getItemById(id);
622     if (item) {
623         slotUpdateClipProperties(item, properties);
624         if (properties.contains("out") || properties.contains("force_fps") || properties.contains("resource")) {
625             slotReloadClip(id);
626         } else if (properties.contains("colour") ||
627                    properties.contains("xmldata") ||
628                    properties.contains("force_aspect_num") ||
629                    properties.contains("force_aspect_den") ||
630                    properties.contains("templatetext")) {
631             slotRefreshClipThumbnail(item);
632             emit refreshClip();
633         } else if (properties.contains("full_luma") || properties.contains("force_colorspace")) {
634             emit refreshClip();
635         }
636     }
637 }
638
639 void ProjectList::slotUpdateClipProperties(ProjectItem *clip, QMap <QString, QString> properties)
640 {
641     if (!clip)
642         return;
643     clip->setProperties(properties);
644     if (properties.contains("name")) {
645         m_listView->blockSignals(true);
646         clip->setText(0, properties.value("name"));
647         m_listView->blockSignals(false);
648         emit clipNameChanged(clip->clipId(), properties.value("name"));
649     }
650     if (properties.contains("description")) {
651         CLIPTYPE type = clip->clipType();
652         m_listView->blockSignals(true);
653         clip->setText(1, properties.value("description"));
654         m_listView->blockSignals(false);
655 #ifdef NEPOMUK
656         if (KdenliveSettings::activate_nepomuk() && (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST)) {
657             // Use Nepomuk system to store clip description
658             Nepomuk::Resource f(clip->clipUrl().path());
659             f.setDescription(properties.value("description"));
660         }
661 #endif
662         emit projectModified();
663     }
664 }
665
666 void ProjectList::slotItemEdited(QTreeWidgetItem *item, int column)
667 {
668     if (item->type() == PROJECTSUBCLIPTYPE) {
669         // this is a sub-item
670         if (column == 1) {
671             // user edited description
672             SubProjectItem *sub = static_cast <SubProjectItem*>(item);
673             ProjectItem *item = static_cast <ProjectItem *>(sub->parent());
674             EditClipCutCommand *command = new EditClipCutCommand(this, item->clipId(), sub->zone(), sub->zone(), sub->description(), sub->text(1), true);
675             m_commandStack->push(command);
676             //slotUpdateCutClipProperties(sub->clipId(), sub->zone(), sub->text(1), sub->text(1));
677         }
678         return;
679     }
680     if (item->type() == PROJECTFOLDERTYPE) {
681         if (column == 0) {
682             FolderProjectItem *folder = static_cast <FolderProjectItem*>(item);
683             editFolder(item->text(0), folder->groupName(), folder->clipId());
684             folder->setGroupName(item->text(0));
685             m_doc->clipManager()->addFolder(folder->clipId(), item->text(0));
686             const int children = item->childCount();
687             for (int i = 0; i < children; i++) {
688                 ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
689                 child->setProperty("groupname", item->text(0));
690             }
691         }
692         return;
693     }
694
695     ProjectItem *clip = static_cast <ProjectItem*>(item);
696     if (column == 1) {
697         if (clip->referencedClip()) {
698             QMap <QString, QString> oldprops;
699             QMap <QString, QString> newprops;
700             oldprops["description"] = clip->referencedClip()->getProperty("description");
701             newprops["description"] = item->text(1);
702
703             if (clip->clipType() == TEXT) {
704                 // This is a text template clip, update the image
705                 /*oldprops.insert("xmldata", clip->referencedClip()->getProperty("xmldata"));
706                 newprops.insert("xmldata", generateTemplateXml(clip->referencedClip()->getProperty("xmltemplate"), item->text(2)).toString());*/
707                 oldprops.insert("templatetext", clip->referencedClip()->getProperty("templatetext"));
708                 newprops.insert("templatetext", item->text(1));
709             }
710             slotUpdateClipProperties(clip->clipId(), newprops);
711             EditClipCommand *command = new EditClipCommand(this, clip->clipId(), oldprops, newprops, false);
712             m_commandStack->push(command);
713         }
714     } else if (column == 0) {
715         if (clip->referencedClip()) {
716             QMap <QString, QString> oldprops;
717             QMap <QString, QString> newprops;
718             oldprops["name"] = clip->referencedClip()->getProperty("name");
719             newprops["name"] = item->text(0);
720             slotUpdateClipProperties(clip, newprops);
721             emit projectModified();
722             EditClipCommand *command = new EditClipCommand(this, clip->clipId(), oldprops, newprops, false);
723             m_commandStack->push(command);
724         }
725     }
726 }
727
728 void ProjectList::slotContextMenu(const QPoint &pos, QTreeWidgetItem *item)
729 {
730     bool enable = item ? true : false;
731     m_editButton->defaultAction()->setEnabled(enable);
732     m_deleteButton->defaultAction()->setEnabled(enable);
733     m_reloadAction->setEnabled(enable);
734     m_transcodeAction->setEnabled(enable);
735     if (enable) {
736         ProjectItem *clip = NULL;
737         if (m_listView->currentItem()->type() == PROJECTSUBCLIPTYPE) {
738             clip = static_cast <ProjectItem*>(item->parent());
739             m_transcodeAction->setEnabled(false);
740         } else if (m_listView->currentItem()->type() == PROJECTCLIPTYPE) {
741             clip = static_cast <ProjectItem*>(item);
742             // Display relevant transcoding actions only
743             adjustTranscodeActions(clip);
744             adjustProxyActions(clip);
745             // Display uses in timeline
746             emit findInTimeline(clip->clipId());
747         } else {
748             m_transcodeAction->setEnabled(false);
749         }
750         if (clip && clip->clipType() == IMAGE && !KdenliveSettings::defaultimageapp().isEmpty()) {
751             m_openAction->setIcon(KIcon(KdenliveSettings::defaultimageapp()));
752             m_openAction->setEnabled(true);
753         } else if (clip && clip->clipType() == AUDIO && !KdenliveSettings::defaultaudioapp().isEmpty()) {
754             m_openAction->setIcon(KIcon(KdenliveSettings::defaultaudioapp()));
755             m_openAction->setEnabled(true);
756         } else {
757             m_openAction->setEnabled(false);
758         }
759
760     } else {
761         m_openAction->setEnabled(false);
762     }
763     m_menu->popup(pos);
764 }
765
766 void ProjectList::slotRemoveClip()
767 {
768     if (!m_listView->currentItem())
769         return;
770     QStringList ids;
771     QMap <QString, QString> folderids;
772     QList<QTreeWidgetItem *> selected = m_listView->selectedItems();
773
774     QUndoCommand *delCommand = new QUndoCommand();
775     delCommand->setText(i18n("Delete Clip Zone"));
776     for (int i = 0; i < selected.count(); i++) {
777         if (selected.at(i)->type() == PROJECTSUBCLIPTYPE) {
778             // subitem
779             SubProjectItem *sub = static_cast <SubProjectItem *>(selected.at(i));
780             ProjectItem *item = static_cast <ProjectItem *>(sub->parent());
781             new AddClipCutCommand(this, item->clipId(), sub->zone().x(), sub->zone().y(), sub->description(), false, true, delCommand);
782         } else if (selected.at(i)->type() == PROJECTFOLDERTYPE) {
783             // folder
784             FolderProjectItem *folder = static_cast <FolderProjectItem *>(selected.at(i));
785             folderids[folder->groupName()] = folder->clipId();
786             int children = folder->childCount();
787
788             if (children > 0 && KMessageBox::questionYesNo(kapp->activeWindow(), 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, folder->text(1)), i18n("Delete Folder")) != KMessageBox::Yes)
789                 return;
790             for (int i = 0; i < children; ++i) {
791                 ProjectItem *child = static_cast <ProjectItem *>(folder->child(i));
792                 ids << child->clipId();
793             }
794         } else {
795             ProjectItem *item = static_cast <ProjectItem *>(selected.at(i));
796             ids << item->clipId();
797             if (item->numReferences() > 0 && KMessageBox::questionYesNo(kapp->activeWindow(), 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"), KStandardGuiItem::yes(), KStandardGuiItem::no(), "DeleteAll") == KMessageBox::No) {
798                 KMessageBox::enableMessage("DeleteAll");
799                 return;
800             }
801         }
802     }
803     KMessageBox::enableMessage("DeleteAll");
804     if (delCommand->childCount() == 0)
805         delete delCommand;
806     else
807         m_commandStack->push(delCommand);
808     emit deleteProjectClips(ids, folderids);
809 }
810
811 void ProjectList::updateButtons() const
812 {
813     if (m_listView->topLevelItemCount() == 0) {
814         m_deleteButton->defaultAction()->setEnabled(false);
815     } else {
816         m_deleteButton->defaultAction()->setEnabled(true);
817         if (!m_listView->currentItem())
818             m_listView->setCurrentItem(m_listView->topLevelItem(0));
819         QTreeWidgetItem *item = m_listView->currentItem();
820         if (item && item->type() == PROJECTCLIPTYPE) {
821             m_editButton->defaultAction()->setEnabled(true);
822             m_openAction->setEnabled(true);
823             m_reloadAction->setEnabled(true);
824             m_transcodeAction->setEnabled(true);
825             return;
826         }
827     }
828
829     m_editButton->defaultAction()->setEnabled(false);
830     m_openAction->setEnabled(false);
831     m_reloadAction->setEnabled(false);
832     m_transcodeAction->setEnabled(false);
833 }
834
835 void ProjectList::selectItemById(const QString &clipId)
836 {
837     ProjectItem *item = getItemById(clipId);
838     if (item)
839         m_listView->setCurrentItem(item);
840 }
841
842
843 void ProjectList::slotDeleteClip(const QString &clipId)
844 {
845     ProjectItem *item = getItemById(clipId);
846     if (!item) {
847         kDebug() << "/// Cannot find clip to delete";
848         return;
849     }
850     m_listView->blockSignals(true);
851     QTreeWidgetItem *newSelectedItem = m_listView->itemAbove(item);
852     if (!newSelectedItem)
853         newSelectedItem = m_listView->itemBelow(item);
854     delete item;
855     m_doc->clipManager()->deleteClip(clipId);
856     m_listView->blockSignals(false);
857     if (newSelectedItem) {
858         m_listView->setCurrentItem(newSelectedItem);
859     } else {
860         updateButtons();
861         emit clipSelected(NULL);
862     }
863 }
864
865
866 void ProjectList::editFolder(const QString folderName, const QString oldfolderName, const QString &clipId)
867 {
868     EditFolderCommand *command = new EditFolderCommand(this, folderName, oldfolderName, clipId, false);
869     m_commandStack->push(command);
870     m_doc->setModified(true);
871 }
872
873 void ProjectList::slotAddFolder()
874 {
875     AddFolderCommand *command = new AddFolderCommand(this, i18n("Folder"), QString::number(m_doc->clipManager()->getFreeFolderId()), true);
876     m_commandStack->push(command);
877 }
878
879 void ProjectList::slotAddFolder(const QString foldername, const QString &clipId, bool remove, bool edit)
880 {
881     if (remove) {
882         FolderProjectItem *item = getFolderItemById(clipId);
883         if (item) {
884             m_doc->clipManager()->deleteFolder(clipId);
885             QTreeWidgetItem *newSelectedItem = m_listView->itemAbove(item);
886             if (!newSelectedItem)
887                 newSelectedItem = m_listView->itemBelow(item);
888             delete item;
889             if (newSelectedItem)
890                 m_listView->setCurrentItem(newSelectedItem);
891             else
892                 updateButtons();
893         }
894     } else {
895         if (edit) {
896             FolderProjectItem *item = getFolderItemById(clipId);
897             if (item) {
898                 m_listView->blockSignals(true);
899                 item->setGroupName(foldername);
900                 m_listView->blockSignals(false);
901                 m_doc->clipManager()->addFolder(clipId, foldername);
902                 const int children = item->childCount();
903                 for (int i = 0; i < children; i++) {
904                     ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
905                     child->setProperty("groupname", foldername);
906                 }
907             }
908         } else {
909             m_listView->blockSignals(true);
910             m_listView->setCurrentItem(new FolderProjectItem(m_listView, QStringList() << foldername, clipId));
911             m_doc->clipManager()->addFolder(clipId, foldername);
912             m_listView->blockSignals(false);
913             m_listView->editItem(m_listView->currentItem(), 0);
914         }
915         updateButtons();
916     }
917     m_doc->setModified(true);
918 }
919
920
921
922 void ProjectList::deleteProjectFolder(QMap <QString, QString> map)
923 {
924     QMapIterator<QString, QString> i(map);
925     QUndoCommand *delCommand = new QUndoCommand();
926     delCommand->setText(i18n("Delete Folder"));
927     while (i.hasNext()) {
928         i.next();
929         new AddFolderCommand(this, i.key(), i.value(), false, delCommand);
930     }
931     m_commandStack->push(delCommand);
932 }
933
934 void ProjectList::slotAddClip(DocClipBase *clip, bool getProperties)
935 {
936     m_listView->setEnabled(false);
937     if (getProperties) m_listView->blockSignals(true);
938     const QString parent = clip->getProperty("groupid");
939     ProjectItem *item = NULL;
940     if (!parent.isEmpty()) {
941         FolderProjectItem *parentitem = getFolderItemById(parent);
942         if (!parentitem) {
943             QStringList text;
944             QString groupName = clip->getProperty("groupname");
945             //kDebug() << "Adding clip to new group: " << groupName;
946             if (groupName.isEmpty()) groupName = i18n("Folder");
947             text << groupName;
948             parentitem = new FolderProjectItem(m_listView, text, parent);
949         }
950
951         if (parentitem)
952             item = new ProjectItem(parentitem, clip);
953     }
954     if (item == NULL)
955         item = new ProjectItem(m_listView, clip);
956     if (item->data(0, DurationRole).isNull()) item->setData(0, DurationRole, i18n("Loading"));
957     if (getProperties) {
958         m_listView->blockSignals(true);
959         m_refreshed = false;
960         
961         // Proxy clips
962         CLIPTYPE t = clip->clipType();
963         if ((t == VIDEO || t == AV || t == UNKNOWN) && KdenliveSettings::enableproxy()) {
964             if (clip->getProperty("proxy").isEmpty()) {
965                 connect(clip, SIGNAL(proxyReady(const QString, bool)), this, SLOT(slotGotProxy(const QString, bool)));
966                 item->setProxyStatus(1);
967                 clip->generateProxy(m_doc->projectFolder());
968             }
969             else {
970                 // Proxy clip already created
971                 item->setProxyStatus(2);
972                 QDomElement e = clip->toXML().cloneNode().toElement();
973                 e.removeAttribute("file_hash");
974                 m_infoQueue.insert(clip->getId(), e);
975                 
976             }
977         }
978         else {
979             // We don't use proxies
980             // remove file_hash so that we load all properties for the clip
981             QDomElement e = clip->toXML().cloneNode().toElement();
982             e.removeAttribute("file_hash");
983             m_infoQueue.insert(clip->getId(), e);
984         }
985         //m_render->getFileProperties(clip->toXML(), clip->getId(), true);
986     }
987     clip->askForAudioThumbs();
988     
989     KUrl url = clip->fileURL();
990     if (getProperties == false && !clip->getClipHash().isEmpty()) {
991         QString cachedPixmap = m_doc->projectFolder().path(KUrl::AddTrailingSlash) + "thumbs/" + clip->getClipHash() + ".png";
992         if (QFile::exists(cachedPixmap)) {
993             QPixmap pix(cachedPixmap);
994             if (pix.isNull())
995                 KIO::NetAccess::del(KUrl(cachedPixmap), this);
996             item->setData(0, Qt::DecorationRole, pix);
997         }
998     }
999 #ifdef NEPOMUK
1000     if (!url.isEmpty() && KdenliveSettings::activate_nepomuk()) {
1001         // if file has Nepomuk comment, use it
1002         Nepomuk::Resource f(url.path());
1003         QString annotation = f.description();
1004         if (!annotation.isEmpty()) item->setText(1, annotation);
1005         item->setText(2, QString::number(f.rating()));
1006     }
1007 #endif
1008     // Add cut zones
1009     QList <CutZoneInfo> cuts = clip->cutZones();
1010     if (!cuts.isEmpty()) {
1011         for (int i = 0; i < cuts.count(); i++) {
1012             SubProjectItem *sub = new SubProjectItem(item, cuts.at(i).zone.x(), cuts.at(i).zone.y(), cuts.at(i).description);
1013             if (!clip->getClipHash().isEmpty()) {
1014                 QString cachedPixmap = m_doc->projectFolder().path(KUrl::AddTrailingSlash) + "thumbs/" + clip->getClipHash() + '#' + QString::number(cuts.at(i).zone.x()) + ".png";
1015                 if (QFile::exists(cachedPixmap)) {
1016                     QPixmap pix(cachedPixmap);
1017                     if (pix.isNull())
1018                         KIO::NetAccess::del(KUrl(cachedPixmap), this);
1019                     sub->setData(0, Qt::DecorationRole, pix);
1020                 }
1021             }
1022         }
1023     }
1024     if (m_listView->isEnabled()) {
1025         updateButtons();
1026         if (getProperties)
1027             m_listView->blockSignals(false);
1028     }
1029     
1030     if (getProperties && !m_queueTimer.isActive())
1031         slotProcessNextClipInQueue();
1032 }
1033
1034 void ProjectList::slotGotProxy(const QString id, bool success)
1035 {
1036     ProjectItem *item = getItemById(id);
1037     if (item) {
1038         disconnect(m_listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotItemEdited(QTreeWidgetItem *, int)));
1039         if (success) {
1040             // Proxy clip successfully created
1041             item->setProxyStatus(2);
1042             QDomElement e = item->referencedClip()->toXML().cloneNode().toElement();  
1043             e.removeAttribute("file_hash");
1044             e.setAttribute("replace", 1);
1045             m_infoQueue.insert(id, e);
1046             if (!m_queueTimer.isActive()) slotProcessNextClipInQueue();
1047         }
1048         else item->setProxyStatus(0);
1049         connect(m_listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotItemEdited(QTreeWidgetItem *, int)));
1050         update();
1051     }
1052 }
1053
1054 void ProjectList::slotResetProjectList()
1055 {
1056     m_listView->clear();
1057     emit clipSelected(NULL);
1058     m_thumbnailQueue.clear();
1059     m_infoQueue.clear();
1060     m_refreshed = false;
1061 }
1062
1063 void ProjectList::requestClipInfo(const QDomElement xml, const QString id)
1064 {
1065     m_refreshed = false;
1066     m_infoQueue.insert(id, xml);
1067     //if (m_infoQueue.count() == 1 || ) QTimer::singleShot(300, this, SLOT(slotProcessNextClipInQueue()));
1068 }
1069
1070 void ProjectList::slotProcessNextClipInQueue()
1071 {
1072     if (m_infoQueue.isEmpty()) {
1073         slotProcessNextThumbnail();
1074         return;
1075     }
1076
1077     QMap<QString, QDomElement>::const_iterator j = m_infoQueue.constBegin();
1078     if (j != m_infoQueue.constEnd()) {
1079         QDomElement dom = j.value();
1080         const QString id = j.key();
1081         m_infoQueue.remove(j.key());
1082         bool replace;
1083         if (dom.hasAttribute("replace")) {
1084             dom.removeAttribute("replace");
1085             replace = true;
1086         }
1087         else replace = false;
1088         emit getFileProperties(dom, id, m_listView->iconSize().height(), replace);
1089     }
1090     if (!m_infoQueue.isEmpty()) m_queueTimer.start();
1091 }
1092
1093 void ProjectList::slotUpdateClip(const QString &id)
1094 {
1095     ProjectItem *item = getItemById(id);
1096     m_listView->blockSignals(true);
1097     if (item) item->setData(0, UsageRole, QString::number(item->numReferences()));
1098     m_listView->blockSignals(false);
1099 }
1100
1101 void ProjectList::updateAllClips()
1102 {
1103     m_listView->setSortingEnabled(false);
1104     kDebug() << "// UPDATE ALL CLPY";
1105
1106     QTreeWidgetItemIterator it(m_listView);
1107     DocClipBase *clip;
1108     ProjectItem *item;
1109     m_listView->blockSignals(true);
1110     while (*it) {
1111         if ((*it)->type() == PROJECTSUBCLIPTYPE) {
1112             // subitem
1113             SubProjectItem *sub = static_cast <SubProjectItem *>(*it);
1114             if (sub->data(0, Qt::DecorationRole).isNull()) {
1115                 item = static_cast <ProjectItem *>((*it)->parent());
1116                 requestClipThumbnail(item->clipId() + '#' + QString::number(sub->zone().x()));
1117             }
1118             ++it;
1119             continue;
1120         } else if ((*it)->type() == PROJECTFOLDERTYPE) {
1121             // folder
1122             ++it;
1123             continue;
1124         } else {
1125             item = static_cast <ProjectItem *>(*it);
1126             clip = item->referencedClip();
1127             if (item->referencedClip()->producer() == NULL) {
1128                 if (clip->isPlaceHolder() == false)
1129                     requestClipInfo(clip->toXML(), clip->getId());
1130                 else if (!clip->isPlaceHolder())
1131                     item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDropEnabled);
1132             } else {
1133                 if (item->data(0, Qt::DecorationRole).isNull())
1134                     requestClipThumbnail(clip->getId());
1135                 if (item->data(0, DurationRole).toString().isEmpty())
1136                     item->changeDuration(item->referencedClip()->producer()->get_playtime());
1137             }
1138             item->setData(0, UsageRole, QString::number(item->numReferences()));
1139         }
1140         //qApp->processEvents();
1141         ++it;
1142     }
1143     if (!m_queueTimer.isActive())
1144         m_queueTimer.start();
1145     if (m_listView->isEnabled())
1146         m_listView->blockSignals(false);
1147     m_listView->setSortingEnabled(true);
1148     if (m_infoQueue.isEmpty())
1149         slotProcessNextThumbnail();
1150 }
1151
1152 // static
1153 QString ProjectList::getExtensions()
1154 {
1155     // Build list of mime types
1156     QStringList mimeTypes = QStringList() << "application/x-kdenlive" << "application/x-kdenlivetitle" << "video/mlt-playlist" << "text/plain"
1157                             << "video/x-flv" << "application/vnd.rn-realmedia" << "video/x-dv" << "video/dv" << "video/x-msvideo" << "video/x-matroska" << "video/mpeg" << "video/ogg" << "video/x-ms-wmv" << "video/mp4" << "video/quicktime" << "video/webm"
1158                             << "audio/x-flac" << "audio/x-matroska" << "audio/mp4" << "audio/mpeg" << "audio/x-mp3" << "audio/ogg" << "audio/x-wav" << "application/ogg" << "application/mxf" << "application/x-shockwave-flash"
1159                             << "image/gif" << "image/jpeg" << "image/png" << "image/x-tga" << "image/x-bmp" << "image/svg+xml" << "image/tiff" << "image/x-xcf" << "image/x-xcf-gimp" << "image/x-vnd.adobe.photoshop" << "image/x-pcx" << "image/x-exr";
1160
1161     QString allExtensions;
1162     foreach(const QString & mimeType, mimeTypes) {
1163         KMimeType::Ptr mime(KMimeType::mimeType(mimeType));
1164         if (mime) {
1165             allExtensions.append(mime->patterns().join(" "));
1166             allExtensions.append(' ');
1167         }
1168     }
1169     return allExtensions.simplified();
1170 }
1171
1172 void ProjectList::slotAddClip(const QList <QUrl> givenList, const QString &groupName, const QString &groupId)
1173 {
1174     if (!m_commandStack)
1175         kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
1176
1177     KUrl::List list;
1178     if (givenList.isEmpty()) {
1179         QString allExtensions = getExtensions();
1180         const QString dialogFilter = allExtensions + ' ' + QLatin1Char('|') + i18n("All Supported Files") + "\n* " + QLatin1Char('|') + i18n("All Files");
1181         QCheckBox *b = new QCheckBox(i18n("Import image sequence"));
1182         b->setChecked(KdenliveSettings::autoimagesequence());
1183         KFileDialog *d = new KFileDialog(KUrl("kfiledialog:///clipfolder"), dialogFilter, kapp->activeWindow(), b);
1184         d->setOperationMode(KFileDialog::Opening);
1185         d->setMode(KFile::Files);
1186         d->exec();
1187         list = d->selectedUrls();
1188         if (b->isChecked() && list.count() == 1) {
1189             // Check for image sequence
1190             KUrl url = list.at(0);
1191             QString fileName = url.fileName().section('.', 0, -2);
1192             if (fileName.at(fileName.size() - 1).isDigit()) {
1193                 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, url);
1194                 if (item.mimetype().startsWith("image")) {
1195                     // import as sequence if we found more than one image in the sequence
1196                     QStringList list;
1197                     QString pattern = SlideshowClip::selectedPath(url.path(), false, QString(), &list);
1198                     int count = list.count();
1199                     if (count > 1) {
1200                         delete d;
1201                         QStringList groupInfo = getGroup();
1202
1203                         // get image sequence base name
1204                         while (fileName.at(fileName.size() - 1).isDigit()) {
1205                             fileName.chop(1);
1206                         }
1207
1208                         m_doc->slotCreateSlideshowClipFile(fileName, pattern, count, m_timecode.reformatSeparators(KdenliveSettings::sequence_duration()),
1209                                                            false, false, false,
1210                                                            m_timecode.getTimecodeFromFrames(int(ceil(m_timecode.fps()))), QString(), 0,
1211                                                            QString(), groupInfo.at(0), groupInfo.at(1));
1212                         return;
1213                     }
1214                 }
1215             }
1216         }
1217         delete d;
1218     } else {
1219         for (int i = 0; i < givenList.count(); i++)
1220             list << givenList.at(i);
1221     }
1222
1223     foreach(const KUrl & file, list) {
1224         // Check there is no folder here
1225         KMimeType::Ptr type = KMimeType::findByUrl(file);
1226         if (type->is("inode/directory")) {
1227             // user dropped a folder
1228             list.removeAll(file);
1229         }
1230     }
1231
1232     if (list.isEmpty())
1233         return;
1234
1235     if (givenList.isEmpty()) {
1236         QStringList groupInfo = getGroup();
1237         m_doc->slotAddClipList(list, groupInfo.at(0), groupInfo.at(1));
1238     } else {
1239         m_doc->slotAddClipList(list, groupName, groupId);
1240     }
1241 }
1242
1243 void ProjectList::slotRemoveInvalidClip(const QString &id, bool replace)
1244 {
1245     ProjectItem *item = getItemById(id);
1246     QTimer::singleShot(300, this, SLOT(slotProcessNextClipInQueue()));
1247     if (item) {
1248         const QString path = item->referencedClip()->fileURL().path();
1249         if (item->referencedClip()->isPlaceHolder()) replace = false;
1250         if (!path.isEmpty()) {
1251             if (replace)
1252                 KMessageBox::sorry(kapp->activeWindow(), i18n("Clip <b>%1</b><br />is invalid, will be removed from project.", path));
1253             else if (KMessageBox::questionYesNo(kapp->activeWindow(), i18n("Clip <b>%1</b><br />is missing or invalid. Remove it from project?", path), i18n("Invalid clip")) == KMessageBox::Yes)
1254                 replace = true;
1255         }
1256         if (replace)
1257             emit deleteProjectClips(QStringList() << id, QMap <QString, QString>());
1258     }
1259 }
1260
1261 void ProjectList::slotAddColorClip()
1262 {
1263     if (!m_commandStack)
1264         kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
1265
1266     QDialog *dia = new QDialog(this);
1267     Ui::ColorClip_UI dia_ui;
1268     dia_ui.setupUi(dia);
1269     dia->setWindowTitle(i18n("Color Clip"));
1270     dia_ui.clip_name->setText(i18n("Color Clip"));
1271
1272     TimecodeDisplay *t = new TimecodeDisplay(m_timecode);
1273     t->setValue(KdenliveSettings::color_duration());
1274     t->setTimeCodeFormat(false);
1275     dia_ui.clip_durationBox->addWidget(t);
1276     dia_ui.clip_color->setColor(KdenliveSettings::colorclipcolor());
1277
1278     if (dia->exec() == QDialog::Accepted) {
1279         QString color = dia_ui.clip_color->color().name();
1280         KdenliveSettings::setColorclipcolor(color);
1281         color = color.replace(0, 1, "0x") + "ff";
1282         QStringList groupInfo = getGroup();
1283         m_doc->slotCreateColorClip(dia_ui.clip_name->text(), color, m_timecode.getTimecode(t->gentime()), groupInfo.at(0), groupInfo.at(1));
1284     }
1285     delete t;
1286     delete dia;
1287 }
1288
1289
1290 void ProjectList::slotAddSlideshowClip()
1291 {
1292     if (!m_commandStack)
1293         kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
1294
1295     SlideshowClip *dia = new SlideshowClip(m_timecode, this);
1296
1297     if (dia->exec() == QDialog::Accepted) {
1298         QStringList groupInfo = getGroup();
1299         m_doc->slotCreateSlideshowClipFile(dia->clipName(), dia->selectedPath(), dia->imageCount(), dia->clipDuration(),
1300                                            dia->loop(), dia->crop(), dia->fade(),
1301                                            dia->lumaDuration(), dia->lumaFile(), dia->softness(),
1302                                            dia->animation(), groupInfo.at(0), groupInfo.at(1));
1303     }
1304     delete dia;
1305 }
1306
1307 void ProjectList::slotAddTitleClip()
1308 {
1309     QStringList groupInfo = getGroup();
1310     m_doc->slotCreateTextClip(groupInfo.at(0), groupInfo.at(1));
1311 }
1312
1313 void ProjectList::slotAddTitleTemplateClip()
1314 {
1315     if (!m_commandStack)
1316         kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
1317
1318     QStringList groupInfo = getGroup();
1319
1320     // Get the list of existing templates
1321     QStringList filter;
1322     filter << "*.kdenlivetitle";
1323     const QString path = m_doc->projectFolder().path(KUrl::AddTrailingSlash) + "titles/";
1324     QStringList templateFiles = QDir(path).entryList(filter, QDir::Files);
1325
1326     QDialog *dia = new QDialog(this);
1327     Ui::TemplateClip_UI dia_ui;
1328     dia_ui.setupUi(dia);
1329     for (int i = 0; i < templateFiles.size(); ++i)
1330         dia_ui.template_list->comboBox()->addItem(templateFiles.at(i), path + templateFiles.at(i));
1331
1332     if (!templateFiles.isEmpty())
1333         dia_ui.buttonBox->button(QDialogButtonBox::Ok)->setFocus();
1334     dia_ui.template_list->fileDialog()->setFilter("application/x-kdenlivetitle");
1335     //warning: setting base directory doesn't work??
1336     KUrl startDir(path);
1337     dia_ui.template_list->fileDialog()->setUrl(startDir);
1338     dia_ui.text_box->setHidden(true);
1339     if (dia->exec() == QDialog::Accepted) {
1340         QString textTemplate = dia_ui.template_list->comboBox()->itemData(dia_ui.template_list->comboBox()->currentIndex()).toString();
1341         if (textTemplate.isEmpty()) textTemplate = dia_ui.template_list->comboBox()->currentText();
1342         // Create a cloned template clip
1343         m_doc->slotCreateTextTemplateClip(groupInfo.at(0), groupInfo.at(1), KUrl(textTemplate));
1344     }
1345     delete dia;
1346 }
1347
1348 QStringList ProjectList::getGroup() const
1349 {
1350     QStringList result;
1351     QTreeWidgetItem *item = m_listView->currentItem();
1352     while (item && item->type() != PROJECTFOLDERTYPE)
1353         item = item->parent();
1354
1355     if (item) {
1356         FolderProjectItem *folder = static_cast <FolderProjectItem *>(item);
1357         result << folder->groupName() << folder->clipId();
1358     } else {
1359         result << QString() << QString();
1360     }
1361     return result;
1362 }
1363
1364 void ProjectList::setDocument(KdenliveDoc *doc)
1365 {
1366     m_listView->blockSignals(true);
1367     m_listView->clear();
1368     m_listView->setSortingEnabled(false);
1369     emit clipSelected(NULL);
1370     m_thumbnailQueue.clear();
1371     m_infoQueue.clear();
1372     m_refreshed = false;
1373     m_fps = doc->fps();
1374     m_timecode = doc->timecode();
1375     m_commandStack = doc->commandStack();
1376     m_doc = doc;
1377
1378     QMap <QString, QString> flist = doc->clipManager()->documentFolderList();
1379     QMapIterator<QString, QString> f(flist);
1380     while (f.hasNext()) {
1381         f.next();
1382         (void) new FolderProjectItem(m_listView, QStringList() << f.value(), f.key());
1383     }
1384
1385     QList <DocClipBase*> list = doc->clipManager()->documentClipList();
1386     for (int i = 0; i < list.count(); i++)
1387         slotAddClip(list.at(i), false);
1388
1389     m_listView->blockSignals(false);
1390     connect(m_doc->clipManager(), SIGNAL(reloadClip(const QString &)), this, SLOT(slotReloadClip(const QString &)));
1391     connect(m_doc->clipManager(), SIGNAL(modifiedClip(const QString &)), this, SLOT(slotModifiedClip(const QString &)));
1392     connect(m_doc->clipManager(), SIGNAL(missingClip(const QString &)), this, SLOT(slotMissingClip(const QString &)));
1393     connect(m_doc->clipManager(), SIGNAL(availableClip(const QString &)), this, SLOT(slotAvailableClip(const QString &)));
1394     connect(m_doc->clipManager(), SIGNAL(checkAllClips()), this, SLOT(updateAllClips()));
1395 }
1396
1397 QList <DocClipBase*> ProjectList::documentClipList() const
1398 {
1399     if (m_doc == NULL)
1400         return QList <DocClipBase*> ();
1401
1402     return m_doc->clipManager()->documentClipList();
1403 }
1404
1405 QDomElement ProjectList::producersList()
1406 {
1407     QDomDocument doc;
1408     QDomElement prods = doc.createElement("producerlist");
1409     doc.appendChild(prods);
1410     kDebug() << "////////////  PRO LIST BUILD PRDSLIST ";
1411     QTreeWidgetItemIterator it(m_listView);
1412     while (*it) {
1413         if ((*it)->type() != PROJECTCLIPTYPE) {
1414             // subitem
1415             ++it;
1416             continue;
1417         }
1418         prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
1419         ++it;
1420     }
1421     return prods;
1422 }
1423
1424 void ProjectList::slotCheckForEmptyQueue()
1425 {
1426     if (!m_refreshed && m_thumbnailQueue.isEmpty() && m_infoQueue.isEmpty()) {
1427         m_refreshed = true;
1428         emit loadingIsOver();
1429         emit displayMessage(QString(), -1);
1430         m_listView->blockSignals(false);
1431         m_listView->setEnabled(true);
1432         updateButtons();
1433     } else if (!m_refreshed) {
1434         QTimer::singleShot(300, this, SLOT(slotCheckForEmptyQueue()));
1435     }
1436 }
1437
1438 void ProjectList::reloadClipThumbnails()
1439 {
1440     m_thumbnailQueue.clear();
1441     QTreeWidgetItemIterator it(m_listView);
1442     while (*it) {
1443         if ((*it)->type() != PROJECTCLIPTYPE) {
1444             // subitem
1445             ++it;
1446             continue;
1447         }
1448         m_thumbnailQueue << ((ProjectItem *)(*it))->clipId();
1449         ++it;
1450     }
1451     QTimer::singleShot(300, this, SLOT(slotProcessNextThumbnail()));
1452 }
1453
1454 void ProjectList::requestClipThumbnail(const QString id)
1455 {
1456     if (!m_thumbnailQueue.contains(id)) m_thumbnailQueue.append(id);
1457 }
1458
1459 void ProjectList::slotProcessNextThumbnail()
1460 {
1461     if (m_thumbnailQueue.isEmpty() && m_infoQueue.isEmpty()) {
1462         slotCheckForEmptyQueue();
1463         return;
1464     }
1465     if (!m_infoQueue.isEmpty()) {
1466         //QTimer::singleShot(300, this, SLOT(slotProcessNextThumbnail()));
1467         return;
1468     }
1469     if (m_thumbnailQueue.count() > 1) {
1470         int max = m_doc->clipManager()->clipsCount();
1471         emit displayMessage(i18n("Loading thumbnails"), (int)(100 *(max - m_thumbnailQueue.count()) / max));
1472     }
1473     slotRefreshClipThumbnail(m_thumbnailQueue.takeFirst(), false);
1474 }
1475
1476 void ProjectList::slotRefreshClipThumbnail(const QString &clipId, bool update)
1477 {
1478     QTreeWidgetItem *item = getAnyItemById(clipId);
1479     if (item)
1480         slotRefreshClipThumbnail(item, update);
1481     else
1482         slotProcessNextThumbnail();
1483 }
1484
1485 void ProjectList::slotRefreshClipThumbnail(QTreeWidgetItem *it, bool update)
1486 {
1487     if (it == NULL) return;
1488     ProjectItem *item = NULL;
1489     bool isSubItem = false;
1490     int frame;
1491     if (it->type() == PROJECTFOLDERTYPE) return;
1492     if (it->type() == PROJECTSUBCLIPTYPE) {
1493         item = static_cast <ProjectItem *>(it->parent());
1494         frame = static_cast <SubProjectItem *>(it)->zone().x();
1495         isSubItem = true;
1496     } else {
1497         item = static_cast <ProjectItem *>(it);
1498         frame = item->referencedClip()->getClipThumbFrame();
1499     }
1500
1501     if (item) {
1502         DocClipBase *clip = item->referencedClip();
1503         if (!clip) {
1504             slotProcessNextThumbnail();
1505             return;
1506         }
1507         QPixmap pix;
1508         int height = m_listView->iconSize().height();
1509         int width = (int)(height  * m_render->dar());
1510         if (clip->clipType() == AUDIO)
1511             pix = KIcon("audio-x-generic").pixmap(QSize(width, height));
1512         else if (clip->clipType() == IMAGE)
1513             pix = QPixmap::fromImage(KThumb::getFrame(item->referencedClip()->producer(), 0, width, height));
1514         else
1515             pix = item->referencedClip()->thumbProducer()->extractImage(frame, width, height);
1516
1517         if (!pix.isNull()) {
1518             m_listView->blockSignals(true);
1519             it->setData(0, Qt::DecorationRole, pix);
1520             if (m_listView->isEnabled())
1521                 m_listView->blockSignals(false);
1522             if (!isSubItem)
1523                 m_doc->cachePixmap(item->getClipHash(), pix);
1524             else
1525                 m_doc->cachePixmap(item->getClipHash() + '#' + QString::number(frame), pix);
1526         }
1527         if (update)
1528             emit projectModified();
1529
1530         slotProcessNextThumbnail();
1531     }
1532 }
1533
1534 void ProjectList::slotReplyGetFileProperties(const QString &clipId, Mlt::Producer *producer, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata, bool replace)
1535 {
1536     QString toReload;
1537     ProjectItem *item = getItemById(clipId);
1538     if (item && producer) {
1539         m_listView->blockSignals(true);
1540         item->setProperties(properties, metadata);
1541         if (item->referencedClip()->isPlaceHolder() && producer->is_valid()) {
1542             item->referencedClip()->setValid();
1543             item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsDropEnabled);
1544             toReload = clipId;
1545         }
1546         if (item->referencedClip()->getProperty("proxy").isEmpty()) item->setProxyStatus(0);
1547         item->referencedClip()->setProducer(producer, replace);
1548         item->referencedClip()->askForAudioThumbs();
1549         if (!replace && item->data(0, Qt::DecorationRole).isNull())
1550             requestClipThumbnail(clipId);
1551         if (!toReload.isEmpty())
1552             item->slotSetToolTip();
1553
1554         if (m_listView->isEnabled() && replace) {
1555             // update clip in clip monitor
1556             emit clipSelected(NULL);
1557             emit clipSelected(item->referencedClip());
1558             //TODO: Make sure the line below has no side effect
1559             toReload = clipId;
1560         }
1561         /*else {
1562             // Check if duration changed.
1563             emit receivedClipDuration(clipId);
1564             delete producer;
1565         }*/
1566         if (m_listView->isEnabled())
1567             m_listView->blockSignals(false);
1568         /*if (item->icon(0).isNull()) {
1569             requestClipThumbnail(clipId);
1570         }*/
1571     } else kDebug() << "////////  COULD NOT FIND CLIP TO UPDATE PRPS...";
1572     if (item && m_infoQueue.isEmpty() && m_thumbnailQueue.isEmpty()) {
1573         m_listView->setCurrentItem(item);
1574         bool updatedProfile = false;
1575         if (item->parent()) {
1576             if (item->parent()->type() == PROJECTFOLDERTYPE)
1577                 static_cast <FolderProjectItem *>(item->parent())->switchIcon();
1578         } else if (KdenliveSettings::checkfirstprojectclip() &&  m_listView->topLevelItemCount() == 1) {
1579             // this is the first clip loaded in project, check if we want to adjust project settings to the clip
1580             updatedProfile = adjustProjectProfileToItem(item);
1581         }
1582         if (updatedProfile == false) emit clipSelected(item->referencedClip());
1583     } else {
1584         int max = m_doc->clipManager()->clipsCount();
1585         emit displayMessage(i18n("Loading clips"), (int)(100 *(max - m_infoQueue.count()) / max));
1586     }
1587     if (!toReload.isEmpty())
1588         emit clipNeedsReload(toReload, true);
1589
1590     qApp->processEvents();
1591     slotProcessNextClipInQueue();
1592 }
1593
1594 bool ProjectList::adjustProjectProfileToItem(ProjectItem *item)
1595 {
1596     if (item == NULL) {
1597         if (m_listView->currentItem() && m_listView->currentItem()->type() != PROJECTFOLDERTYPE)
1598             item = static_cast <ProjectItem*>(m_listView->currentItem());
1599     }
1600     if (item == NULL || item->referencedClip() == NULL) {
1601         KMessageBox::information(kapp->activeWindow(), i18n("Cannot find profile from current clip"));
1602         return false;
1603     }
1604     bool profileUpdated = false;
1605     QString size = item->referencedClip()->getProperty("frame_size");
1606     int width = size.section('x', 0, 0).toInt();
1607     int height = size.section('x', -1).toInt();
1608     double fps = item->referencedClip()->getProperty("fps").toDouble();
1609     double par = item->referencedClip()->getProperty("aspect_ratio").toDouble();
1610     if (item->clipType() == IMAGE || item->clipType() == AV || item->clipType() == VIDEO) {
1611         if (ProfilesDialog::matchProfile(width, height, fps, par, item->clipType() == IMAGE, m_doc->mltProfile()) == false) {
1612             // get a list of compatible profiles
1613             QMap <QString, QString> suggestedProfiles = ProfilesDialog::getProfilesFromProperties(width, height, fps, par, item->clipType() == IMAGE);
1614             if (!suggestedProfiles.isEmpty()) {
1615                 KDialog *dialog = new KDialog(this);
1616                 dialog->setCaption(i18n("Change project profile"));
1617                 dialog->setButtons(KDialog::Ok | KDialog::Cancel);
1618
1619                 QWidget container;
1620                 QVBoxLayout *l = new QVBoxLayout;
1621                 QLabel *label = new QLabel(i18n("Your clip does not match current project's profile.\nDo you want to change the project profile?\n\nThe following profiles match the clip (size: %1, fps: %2)", size, fps));
1622                 l->addWidget(label);
1623                 QListWidget *list = new QListWidget;
1624                 list->setAlternatingRowColors(true);
1625                 QMapIterator<QString, QString> i(suggestedProfiles);
1626                 while (i.hasNext()) {
1627                     i.next();
1628                     QListWidgetItem *item = new QListWidgetItem(i.value(), list);
1629                     item->setData(Qt::UserRole, i.key());
1630                     item->setToolTip(i.key());
1631                 }
1632                 list->setCurrentRow(0);
1633                 l->addWidget(list);
1634                 container.setLayout(l);
1635                 dialog->setButtonText(KDialog::Ok, i18n("Update profile"));
1636                 dialog->setMainWidget(&container);
1637                 if (dialog->exec() == QDialog::Accepted) {
1638                     //Change project profile
1639                     profileUpdated = true;
1640                     if (list->currentItem())
1641                         emit updateProfile(list->currentItem()->data(Qt::UserRole).toString());
1642                 }
1643                 delete list;
1644                 delete label;
1645             } else if (fps > 0) {
1646                 KMessageBox::information(kapp->activeWindow(), i18n("Your clip does not match current project's profile.\nNo existing profile found to match the clip's properties.\nClip size: %1\nFps: %2\n", size, fps));
1647             }
1648         }
1649     }
1650     return profileUpdated;
1651 }
1652
1653 void ProjectList::slotReplyGetImage(const QString &clipId, const QPixmap &pix)
1654 {
1655     ProjectItem *item = getItemById(clipId);
1656     if (item && !pix.isNull()) {
1657         m_listView->blockSignals(true);
1658         item->setData(0, Qt::DecorationRole, pix);
1659         m_doc->cachePixmap(item->getClipHash(), pix);
1660         if (m_listView->isEnabled())
1661             m_listView->blockSignals(false);
1662     }
1663 }
1664
1665 QTreeWidgetItem *ProjectList::getAnyItemById(const QString &id)
1666 {
1667     QTreeWidgetItemIterator it(m_listView);
1668     QString lookId = id;
1669     if (id.contains('#'))
1670         lookId = id.section('#', 0, 0);
1671
1672     ProjectItem *result = NULL;
1673     while (*it) {
1674         if ((*it)->type() != PROJECTCLIPTYPE) {
1675             // subitem
1676             ++it;
1677             continue;
1678         }
1679         ProjectItem *item = static_cast<ProjectItem *>(*it);
1680         if (item->clipId() == lookId) {
1681             result = item;
1682             break;
1683         }
1684         ++it;
1685     }
1686     if (result == NULL || !id.contains('#')) {
1687         return result;
1688     } else {
1689         for (int i = 0; i < result->childCount(); i++) {
1690             SubProjectItem *sub = static_cast <SubProjectItem *>(result->child(i));
1691             if (sub && sub->zone().x() == id.section('#', 1, 1).toInt())
1692                 return sub;
1693         }
1694     }
1695
1696     return NULL;
1697 }
1698
1699
1700 ProjectItem *ProjectList::getItemById(const QString &id)
1701 {
1702     ProjectItem *item;
1703     QTreeWidgetItemIterator it(m_listView);
1704     while (*it) {
1705         if ((*it)->type() != PROJECTCLIPTYPE) {
1706             // subitem
1707             ++it;
1708             continue;
1709         }
1710         item = static_cast<ProjectItem *>(*it);
1711         if (item->clipId() == id)
1712             return item;
1713         ++it;
1714     }
1715     return NULL;
1716 }
1717
1718 FolderProjectItem *ProjectList::getFolderItemById(const QString &id)
1719 {
1720     FolderProjectItem *item;
1721     QTreeWidgetItemIterator it(m_listView);
1722     while (*it) {
1723         if ((*it)->type() == PROJECTFOLDERTYPE) {
1724             item = static_cast<FolderProjectItem *>(*it);
1725             if (item->clipId() == id)
1726                 return item;
1727         }
1728         ++it;
1729     }
1730     return NULL;
1731 }
1732
1733 void ProjectList::slotSelectClip(const QString &ix)
1734 {
1735     ProjectItem *clip = getItemById(ix);
1736     if (clip) {
1737         m_listView->setCurrentItem(clip);
1738         m_listView->scrollToItem(clip);
1739         m_editButton->defaultAction()->setEnabled(true);
1740         m_deleteButton->defaultAction()->setEnabled(true);
1741         m_reloadAction->setEnabled(true);
1742         m_transcodeAction->setEnabled(true);
1743         if (clip->clipType() == IMAGE && !KdenliveSettings::defaultimageapp().isEmpty()) {
1744             m_openAction->setIcon(KIcon(KdenliveSettings::defaultimageapp()));
1745             m_openAction->setEnabled(true);
1746         } else if (clip->clipType() == AUDIO && !KdenliveSettings::defaultaudioapp().isEmpty()) {
1747             m_openAction->setIcon(KIcon(KdenliveSettings::defaultaudioapp()));
1748             m_openAction->setEnabled(true);
1749         } else {
1750             m_openAction->setEnabled(false);
1751         }
1752     }
1753 }
1754
1755 QString ProjectList::currentClipUrl() const
1756 {
1757     ProjectItem *item;
1758     if (!m_listView->currentItem() || m_listView->currentItem()->type() == PROJECTFOLDERTYPE) return QString();
1759     if (m_listView->currentItem()->type() == PROJECTSUBCLIPTYPE) {
1760         // subitem
1761         item = static_cast <ProjectItem*>(m_listView->currentItem()->parent());
1762     } else {
1763         item = static_cast <ProjectItem*>(m_listView->currentItem());
1764     }
1765     if (item == NULL)
1766         return QString();
1767     return item->clipUrl().path();
1768 }
1769
1770 KUrl::List ProjectList::getConditionalUrls(const QString &condition) const
1771 {
1772     KUrl::List result;
1773     ProjectItem *item;
1774     QList<QTreeWidgetItem *> list = m_listView->selectedItems();
1775     for (int i = 0; i < list.count(); i++) {
1776         if (list.at(i)->type() == PROJECTFOLDERTYPE)
1777             continue;
1778         if (list.at(i)->type() == PROJECTSUBCLIPTYPE) {
1779             // subitem
1780             item = static_cast <ProjectItem*>(list.at(i)->parent());
1781         } else {
1782             item = static_cast <ProjectItem*>(list.at(i));
1783         }
1784         if (item == NULL || item->type() == COLOR || item->type() == SLIDESHOW || item->type() == TEXT)
1785             continue;
1786         DocClipBase *clip = item->referencedClip();
1787         if (!condition.isEmpty()) {
1788             if (condition.startsWith("vcodec") && !clip->hasVideoCodec(condition.section('=', 1, 1)))
1789                 continue;
1790             else if (condition.startsWith("acodec") && !clip->hasAudioCodec(condition.section('=', 1, 1)))
1791                 continue;
1792         }
1793         result.append(item->clipUrl());
1794     }
1795     return result;
1796 }
1797
1798 void ProjectList::regenerateTemplate(const QString &id)
1799 {
1800     ProjectItem *clip = getItemById(id);
1801     if (clip)
1802         regenerateTemplate(clip);
1803 }
1804
1805 void ProjectList::regenerateTemplate(ProjectItem *clip)
1806 {
1807     //TODO: remove this unused method, only force_reload is necessary
1808     clip->referencedClip()->producer()->set("force_reload", 1);
1809 }
1810
1811 QDomDocument ProjectList::generateTemplateXml(QString path, const QString &replaceString)
1812 {
1813     QDomDocument doc;
1814     QFile file(path);
1815     if (!file.open(QIODevice::ReadOnly)) {
1816         kWarning() << "ERROR, CANNOT READ: " << path;
1817         return doc;
1818     }
1819     if (!doc.setContent(&file)) {
1820         kWarning() << "ERROR, CANNOT READ: " << path;
1821         file.close();
1822         return doc;
1823     }
1824     file.close();
1825     QDomNodeList texts = doc.elementsByTagName("content");
1826     for (int i = 0; i < texts.count(); i++) {
1827         QString data = texts.item(i).firstChild().nodeValue();
1828         data.replace("%s", replaceString);
1829         texts.item(i).firstChild().setNodeValue(data);
1830     }
1831     return doc;
1832 }
1833
1834
1835 void ProjectList::slotAddClipCut(const QString &id, int in, int out)
1836 {
1837     ProjectItem *clip = getItemById(id);
1838     if (clip == NULL || clip->referencedClip()->hasCutZone(QPoint(in, out)))
1839         return;
1840     AddClipCutCommand *command = new AddClipCutCommand(this, id, in, out, QString(), true, false);
1841     m_commandStack->push(command);
1842 }
1843
1844 void ProjectList::addClipCut(const QString &id, int in, int out, const QString desc, bool newItem)
1845 {
1846     ProjectItem *clip = getItemById(id);
1847     if (clip) {
1848         DocClipBase *base = clip->referencedClip();
1849         base->addCutZone(in, out);
1850         m_listView->blockSignals(true);
1851         SubProjectItem *sub = new SubProjectItem(clip, in, out, desc);
1852         if (newItem && desc.isEmpty() && !m_listView->isColumnHidden(1)) {
1853             if (!clip->isExpanded())
1854                 clip->setExpanded(true);
1855             m_listView->scrollToItem(sub);
1856             m_listView->editItem(sub, 1);
1857         }
1858         QPixmap p = clip->referencedClip()->thumbProducer()->extractImage(in, (int)(sub->sizeHint(0).height()  * m_render->dar()), sub->sizeHint(0).height() - 2);
1859         sub->setData(0, Qt::DecorationRole, p);
1860         m_doc->cachePixmap(clip->getClipHash() + '#' + QString::number(in), p);
1861         m_listView->blockSignals(false);
1862     }
1863     emit projectModified();
1864 }
1865
1866 void ProjectList::removeClipCut(const QString &id, int in, int out)
1867 {
1868     ProjectItem *clip = getItemById(id);
1869     if (clip) {
1870         DocClipBase *base = clip->referencedClip();
1871         base->removeCutZone(in, out);
1872         SubProjectItem *sub = getSubItem(clip, QPoint(in, out));
1873         if (sub) {
1874             m_listView->blockSignals(true);
1875             delete sub;
1876             m_listView->blockSignals(false);
1877         }
1878     }
1879     emit projectModified();
1880 }
1881
1882 SubProjectItem *ProjectList::getSubItem(ProjectItem *clip, QPoint zone)
1883 {
1884     SubProjectItem *sub = NULL;
1885     if (clip) {
1886         for (int i = 0; i < clip->childCount(); i++) {
1887             QTreeWidgetItem *it = clip->child(i);
1888             if (it->type() == PROJECTSUBCLIPTYPE) {
1889                 sub = static_cast <SubProjectItem*>(it);
1890                 if (sub->zone() == zone)
1891                     break;
1892                 else
1893                     sub = NULL;
1894             }
1895         }
1896     }
1897     return sub;
1898 }
1899
1900 void ProjectList::slotUpdateClipCut(QPoint p)
1901 {
1902     if (!m_listView->currentItem() || m_listView->currentItem()->type() != PROJECTSUBCLIPTYPE)
1903         return;
1904     SubProjectItem *sub = static_cast <SubProjectItem*>(m_listView->currentItem());
1905     ProjectItem *item = static_cast <ProjectItem *>(sub->parent());
1906     EditClipCutCommand *command = new EditClipCutCommand(this, item->clipId(), sub->zone(), p, sub->text(1), sub->text(1), true);
1907     m_commandStack->push(command);
1908 }
1909
1910 void ProjectList::doUpdateClipCut(const QString &id, const QPoint oldzone, const QPoint zone, const QString &comment)
1911 {
1912     ProjectItem *clip = getItemById(id);
1913     SubProjectItem *sub = getSubItem(clip, oldzone);
1914     if (sub == NULL || clip == NULL)
1915         return;
1916     DocClipBase *base = clip->referencedClip();
1917     base->updateCutZone(oldzone.x(), oldzone.y(), zone.x(), zone.y(), comment);
1918     m_listView->blockSignals(true);
1919     sub->setZone(zone);
1920     sub->setDescription(comment);
1921     m_listView->blockSignals(false);
1922     emit projectModified();
1923 }
1924
1925 void ProjectList::slotForceProcessing(const QString &id)
1926 {
1927     while (m_infoQueue.contains(id)) {
1928         slotProcessNextClipInQueue();
1929     }
1930 }
1931
1932 void ProjectList::slotAddOrUpdateSequence(const QString frameName)
1933 {
1934     QString fileName = KUrl(frameName).fileName().section('_', 0, -2);
1935     QStringList list;
1936     QString pattern = SlideshowClip::selectedPath(frameName, false, QString(), &list);
1937     int count = list.count();
1938     if (count > 1) {
1939         const QList <DocClipBase *> existing = m_doc->clipManager()->getClipByResource(pattern);
1940         if (!existing.isEmpty()) {
1941             // Sequence already exists, update
1942             QString id = existing.at(0)->getId();
1943             //ProjectItem *item = getItemById(id);
1944             QMap <QString, QString> oldprops;
1945             QMap <QString, QString> newprops;
1946             int ttl = existing.at(0)->getProperty("ttl").toInt();
1947             oldprops["out"] = existing.at(0)->getProperty("out");
1948             newprops["out"] = QString::number(ttl * count - 1);
1949             slotUpdateClipProperties(id, newprops);
1950             EditClipCommand *command = new EditClipCommand(this, id, oldprops, newprops, false);
1951             m_commandStack->push(command);
1952         } else {
1953             // Create sequence
1954             QStringList groupInfo = getGroup();
1955             m_doc->slotCreateSlideshowClipFile(fileName, pattern, count, m_timecode.reformatSeparators(KdenliveSettings::sequence_duration()),
1956                                                false, false, false,
1957                                                m_timecode.getTimecodeFromFrames(int(ceil(m_timecode.fps()))), QString(), 0,
1958                                                QString(), groupInfo.at(0), groupInfo.at(1));
1959         }
1960     } else emit displayMessage(i18n("Sequence not found"), -2);
1961 }
1962
1963 QMap <QString, QString> ProjectList::getProxies()
1964 {
1965     QMap <QString, QString> list;
1966     ProjectItem *item;
1967     QTreeWidgetItemIterator it(m_listView);
1968     while (*it) {
1969         if ((*it)->type() != PROJECTCLIPTYPE) {
1970             ++it;
1971             continue;
1972         }
1973         item = static_cast<ProjectItem *>(*it);
1974         if (item && item->referencedClip() != NULL) {
1975             QString proxy = item->referencedClip()->getProperty("proxy");
1976             if (!proxy.isEmpty()) list.insert(proxy, item->clipUrl().path());
1977         }
1978         ++it;
1979     }
1980     return list;
1981 }
1982
1983 void ProjectList::updateProxyConfig()
1984 {
1985     ProjectItem *item;
1986     QTreeWidgetItemIterator it(m_listView);
1987     while (*it) {
1988         if ((*it)->type() != PROJECTCLIPTYPE) {
1989             ++it;
1990             continue;
1991         }
1992         item = static_cast<ProjectItem *>(*it);
1993         if (item && item->referencedClip() != NULL) {
1994             if  (KdenliveSettings::enableproxy()) {
1995                 DocClipBase *clip = item->referencedClip();
1996                 connect(clip, SIGNAL(proxyReady(const QString, bool)), this, SLOT(slotGotProxy(const QString, bool)));
1997                 item->setProxyStatus(1);
1998                 clip->generateProxy(m_doc->projectFolder());
1999             }
2000             else if (!item->referencedClip()->getProperty("proxy").isEmpty()) {
2001                 // remove proxy
2002                 item->referencedClip()->clearProperty("proxy");
2003                 QDomElement e = item->toXml().cloneNode().toElement();
2004                 e.removeAttribute("file_hash");
2005                 e.setAttribute("replace", 1);
2006                 m_infoQueue.insert(item->clipId(), e);
2007             }
2008         }
2009         ++it;
2010     }
2011     if (!m_infoQueue.isEmpty() && !m_queueTimer.isActive()) m_queueTimer.start();
2012
2013 }
2014
2015 void ProjectList::slotProxyCurrentItem(bool doProxy)
2016 {
2017     if (m_listView->currentItem()->type() == PROJECTCLIPTYPE) {
2018         ProjectItem *item = static_cast <ProjectItem*>(m_listView->currentItem());
2019         if (item->referencedClip()) {
2020             if (doProxy) {
2021                 DocClipBase *clip = item->referencedClip();
2022                 connect(clip, SIGNAL(proxyReady(const QString, bool)), this, SLOT(slotGotProxy(const QString, bool)));
2023                 item->setProxyStatus(1);
2024                 clip->generateProxy(m_doc->projectFolder());
2025             }
2026             else if (!item->referencedClip()->getProperty("proxy").isEmpty()) {
2027                 // remove proxy
2028                 item->referencedClip()->clearProperty("proxy");
2029                 QDomElement e = item->toXml().cloneNode().toElement();
2030                 e.removeAttribute("file_hash");
2031                 e.setAttribute("replace", 1);
2032                 m_infoQueue.insert(item->clipId(), e);
2033             }
2034         }
2035         if (!m_infoQueue.isEmpty() && !m_queueTimer.isActive()) m_queueTimer.start();
2036     }
2037 }
2038
2039 #include "projectlist.moc"