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