]> git.sesse.net Git - kdenlive/blob - src/projectlist.cpp
Remove debug from previous commit
[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 #include <KStandardDirs>
53
54 #ifdef NEPOMUK
55 #include <nepomuk/global.h>
56 #include <nepomuk/resourcemanager.h>
57 //#include <nepomuk/tag.h>
58 #endif
59
60 #include <QMouseEvent>
61 #include <QStylePainter>
62 #include <QPixmap>
63 #include <QIcon>
64 #include <QMenu>
65 #include <QProcess>
66 #include <QHeaderView>
67 #include <QInputDialog>
68 #include <QtConcurrentRun>
69
70 ProjectList::ProjectList(QWidget *parent) :
71     QWidget(parent),
72     m_render(NULL),
73     m_fps(-1),
74     m_commandStack(NULL),
75     m_openAction(NULL),
76     m_reloadAction(NULL),
77     m_transcodeAction(NULL),
78     m_doc(NULL),
79     m_refreshed(false),
80     m_infoQueue(),
81     m_thumbnailQueue()
82 {
83     QVBoxLayout *layout = new QVBoxLayout;
84     layout->setContentsMargins(0, 0, 0, 0);
85     layout->setSpacing(0);
86     qRegisterMetaType<QDomElement>("QDomElement");
87     // setup toolbar
88     QFrame *frame = new QFrame;
89     frame->setFrameStyle(QFrame::NoFrame);
90     QHBoxLayout *box = new QHBoxLayout;
91     KTreeWidgetSearchLine *searchView = new KTreeWidgetSearchLine;
92
93     box->addWidget(searchView);
94     //int s = style()->pixelMetric(QStyle::PM_SmallIconSize);
95     //m_toolbar->setIconSize(QSize(s, s));
96
97     m_addButton = new QToolButton;
98     m_addButton->setPopupMode(QToolButton::MenuButtonPopup);
99     m_addButton->setAutoRaise(true);
100     box->addWidget(m_addButton);
101
102     m_editButton = new QToolButton;
103     m_editButton->setAutoRaise(true);
104     box->addWidget(m_editButton);
105
106     m_deleteButton = new QToolButton;
107     m_deleteButton->setAutoRaise(true);
108     box->addWidget(m_deleteButton);
109     frame->setLayout(box);
110     layout->addWidget(frame);
111
112     m_listView = new ProjectListView;
113     layout->addWidget(m_listView);
114     setLayout(layout);
115     searchView->setTreeWidget(m_listView);
116
117     connect(this, SIGNAL(processNextThumbnail()), this, SLOT(slotProcessNextThumbnail()));
118     connect(m_listView, SIGNAL(projectModified()), this, SIGNAL(projectModified()));
119     connect(m_listView, SIGNAL(itemSelectionChanged()), this, SLOT(slotClipSelected()));
120     connect(m_listView, SIGNAL(focusMonitor()), this, SLOT(slotClipSelected()));
121     connect(m_listView, SIGNAL(pauseMonitor()), this, SLOT(slotPauseMonitor()));
122     connect(m_listView, SIGNAL(requestMenu(const QPoint &, QTreeWidgetItem *)), this, SLOT(slotContextMenu(const QPoint &, QTreeWidgetItem *)));
123     connect(m_listView, SIGNAL(addClip()), this, SLOT(slotAddClip()));
124     connect(m_listView, SIGNAL(addClip(const QList <QUrl>, const QString &, const QString &)), this, SLOT(slotAddClip(const QList <QUrl>, const QString &, const QString &)));
125     connect(m_listView, SIGNAL(addClipCut(const QString &, int, int)), this, SLOT(slotAddClipCut(const QString &, int, int)));
126     connect(m_listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotItemEdited(QTreeWidgetItem *, int)));
127     connect(m_listView, SIGNAL(showProperties(DocClipBase *)), this, SIGNAL(showClipProperties(DocClipBase *)));
128
129     m_listViewDelegate = new ItemDelegate(m_listView);
130     m_listView->setItemDelegate(m_listViewDelegate);
131 #ifdef NEPOMUK
132     if (KdenliveSettings::activate_nepomuk()) {
133         Nepomuk::ResourceManager::instance()->init();
134         if (!Nepomuk::ResourceManager::instance()->initialized()) {
135             kDebug() << "Cannot communicate with Nepomuk, DISABLING it";
136             KdenliveSettings::setActivate_nepomuk(false);
137         }
138     }
139 #endif
140 }
141
142 ProjectList::~ProjectList()
143 {
144     delete m_menu;
145     m_listView->blockSignals(true);
146     m_listView->clear();
147     delete m_listViewDelegate;
148 }
149
150 void ProjectList::focusTree() const
151 {
152     m_listView->setFocus();
153 }
154
155 void ProjectList::setupMenu(QMenu *addMenu, QAction *defaultAction)
156 {
157     QList <QAction *> actions = addMenu->actions();
158     for (int i = 0; i < actions.count(); i++) {
159         if (actions.at(i)->data().toString() == "clip_properties") {
160             m_editButton->setDefaultAction(actions.at(i));
161             actions.removeAt(i);
162             i--;
163         } else if (actions.at(i)->data().toString() == "delete_clip") {
164             m_deleteButton->setDefaultAction(actions.at(i));
165             actions.removeAt(i);
166             i--;
167         } else if (actions.at(i)->data().toString() == "edit_clip") {
168             m_openAction = actions.at(i);
169             actions.removeAt(i);
170             i--;
171         } else if (actions.at(i)->data().toString() == "reload_clip") {
172             m_reloadAction = actions.at(i);
173             actions.removeAt(i);
174             i--;
175         } else if (actions.at(i)->data().toString() == "proxy_clip") {
176             m_proxyAction = actions.at(i);
177             actions.removeAt(i);
178             i--;
179         }
180     }
181
182     QMenu *m = new QMenu();
183     m->addActions(actions);
184     m_addButton->setMenu(m);
185     m_addButton->setDefaultAction(defaultAction);
186     m_menu = new QMenu();
187     m_menu->addActions(addMenu->actions());
188 }
189
190 void ProjectList::setupGeneratorMenu(QMenu *addMenu, QMenu *transcodeMenu, QMenu *inTimelineMenu)
191 {
192     if (!addMenu)
193         return;
194     QMenu *menu = m_addButton->menu();
195     menu->addMenu(addMenu);
196     m_addButton->setMenu(menu);
197
198     m_menu->addMenu(addMenu);
199     if (addMenu->isEmpty())
200         addMenu->setEnabled(false);
201     m_menu->addMenu(transcodeMenu);
202     if (transcodeMenu->isEmpty())
203         transcodeMenu->setEnabled(false);
204     m_transcodeAction = transcodeMenu;
205     m_menu->addAction(m_reloadAction);
206     m_menu->addAction(m_proxyAction);
207     m_menu->addMenu(inTimelineMenu);
208     inTimelineMenu->setEnabled(false);
209     m_menu->addAction(m_editButton->defaultAction());
210     m_menu->addAction(m_openAction);
211     m_menu->addAction(m_deleteButton->defaultAction());
212     m_menu->insertSeparator(m_deleteButton->defaultAction());
213 }
214
215
216 QByteArray ProjectList::headerInfo() const
217 {
218     return m_listView->header()->saveState();
219 }
220
221 void ProjectList::setHeaderInfo(const QByteArray &state)
222 {
223     m_listView->header()->restoreState(state);
224 }
225
226 void ProjectList::updateProjectFormat(Timecode t)
227 {
228     m_timecode = t;
229 }
230
231 void ProjectList::slotEditClip()
232 {
233     QList<QTreeWidgetItem *> list = m_listView->selectedItems();
234     if (list.isEmpty()) return;
235     if (list.count() > 1 || list.at(0)->type() == PROJECTFOLDERTYPE) {
236         editClipSelection(list);
237         return;
238     }
239     ProjectItem *item;
240     if (!m_listView->currentItem() || m_listView->currentItem()->type() == PROJECTFOLDERTYPE)
241         return;
242     if (m_listView->currentItem()->type() == PROJECTSUBCLIPTYPE)
243         item = static_cast <ProjectItem*>(m_listView->currentItem()->parent());
244     else
245         item = static_cast <ProjectItem*>(m_listView->currentItem());
246     if (item && (item->flags() & Qt::ItemIsDragEnabled)) {
247         emit clipSelected(item->referencedClip());
248         emit showClipProperties(item->referencedClip());
249     }
250 }
251
252 void ProjectList::editClipSelection(QList<QTreeWidgetItem *> list)
253 {
254     // Gather all common properties
255     QMap <QString, QString> commonproperties;
256     QList <DocClipBase *> clipList;
257     commonproperties.insert("force_aspect_num", "-");
258     commonproperties.insert("force_aspect_den", "-");
259     commonproperties.insert("force_fps", "-");
260     commonproperties.insert("force_progressive", "-");
261     commonproperties.insert("force_tff", "-");
262     commonproperties.insert("threads", "-");
263     commonproperties.insert("video_index", "-");
264     commonproperties.insert("audio_index", "-");
265     commonproperties.insert("force_colorspace", "-");
266     commonproperties.insert("full_luma", "-");
267     QString transparency = "-";
268
269     bool allowDurationChange = true;
270     int commonDuration = -1;
271     bool hasImages = false;;
272     ProjectItem *item;
273     for (int i = 0; i < list.count(); i++) {
274         item = NULL;
275         if (list.at(i)->type() == PROJECTFOLDERTYPE) {
276             // Add folder items to the list
277             int ct = list.at(i)->childCount();
278             for (int j = 0; j < ct; j++) {
279                 list.append(list.at(i)->child(j));
280             }
281             continue;
282         }
283         else if (list.at(i)->type() == PROJECTSUBCLIPTYPE)
284             item = static_cast <ProjectItem*>(list.at(i)->parent());
285         else
286             item = static_cast <ProjectItem*>(list.at(i));
287         if (!(item->flags() & Qt::ItemIsDragEnabled))
288             continue;
289         if (item) {
290             // check properties
291             DocClipBase *clip = item->referencedClip();
292             if (clipList.contains(clip)) continue;
293             if (clip->clipType() == IMAGE) {
294                 hasImages = true;
295                 if (clip->getProperty("transparency").isEmpty() || clip->getProperty("transparency").toInt() == 0) {
296                     if (transparency == "-") {
297                         // first non transparent image
298                         transparency = "0";
299                     }
300                     else if (transparency == "1") {
301                         // we have transparent and non transparent clips
302                         transparency = "-1";
303                     }
304                 }
305                 else {
306                     if (transparency == "-") {
307                         // first transparent image
308                         transparency = "1";
309                     }
310                     else if (transparency == "0") {
311                         // we have transparent and non transparent clips
312                         transparency = "-1";
313                     }
314                 }
315             }
316             if (clip->clipType() != COLOR && clip->clipType() != IMAGE && clip->clipType() != TEXT)
317                 allowDurationChange = false;
318             if (allowDurationChange && commonDuration != 0) {
319                 if (commonDuration == -1)
320                     commonDuration = clip->duration().frames(m_fps);
321                 else if (commonDuration != clip->duration().frames(m_fps))
322                     commonDuration = 0;
323             }
324             clipList.append(clip);
325             QMap <QString, QString> clipprops = clip->properties();
326             QMapIterator<QString, QString> p(commonproperties);
327             while (p.hasNext()) {
328                 p.next();
329                 if (p.value().isEmpty()) continue;
330                 if (clipprops.contains(p.key())) {
331                     if (p.value() == "-")
332                         commonproperties.insert(p.key(), clipprops.value(p.key()));
333                     else if (p.value() != clipprops.value(p.key()))
334                         commonproperties.insert(p.key(), QString());
335                 } else {
336                     commonproperties.insert(p.key(), QString());
337                 }
338             }
339         }
340     }
341     if (allowDurationChange)
342         commonproperties.insert("out", QString::number(commonDuration));
343     if (hasImages)
344         commonproperties.insert("transparency", transparency);
345     /*QMapIterator<QString, QString> p(commonproperties);
346     while (p.hasNext()) {
347         p.next();
348         kDebug() << "Result: " << p.key() << " = " << p.value();
349     }*/
350     emit showClipProperties(clipList, commonproperties);
351 }
352
353 void ProjectList::slotOpenClip()
354 {
355     ProjectItem *item;
356     if (!m_listView->currentItem() || m_listView->currentItem()->type() == PROJECTFOLDERTYPE)
357         return;
358     if (m_listView->currentItem()->type() == QTreeWidgetItem::UserType + 1)
359         item = static_cast <ProjectItem*>(m_listView->currentItem()->parent());
360     else
361         item = static_cast <ProjectItem*>(m_listView->currentItem());
362     if (item) {
363         if (item->clipType() == IMAGE) {
364             if (KdenliveSettings::defaultimageapp().isEmpty())
365                 KMessageBox::sorry(kapp->activeWindow(), i18n("Please set a default application to open images in the Settings dialog"));
366             else
367                 QProcess::startDetached(KdenliveSettings::defaultimageapp(), QStringList() << item->clipUrl().path());
368         }
369         if (item->clipType() == AUDIO) {
370             if (KdenliveSettings::defaultaudioapp().isEmpty())
371                 KMessageBox::sorry(kapp->activeWindow(), i18n("Please set a default application to open audio files in the Settings dialog"));
372             else
373                 QProcess::startDetached(KdenliveSettings::defaultaudioapp(), QStringList() << item->clipUrl().path());
374         }
375     }
376 }
377
378 void ProjectList::cleanup()
379 {
380     m_listView->clearSelection();
381     QTreeWidgetItemIterator it(m_listView);
382     ProjectItem *item;
383     while (*it) {
384         if ((*it)->type() != PROJECTCLIPTYPE) {
385             it++;
386             continue;
387         }
388         item = static_cast <ProjectItem *>(*it);
389         if (item->numReferences() == 0)
390             item->setSelected(true);
391         it++;
392     }
393     slotRemoveClip();
394 }
395
396 void ProjectList::trashUnusedClips()
397 {
398     QTreeWidgetItemIterator it(m_listView);
399     ProjectItem *item;
400     QStringList ids;
401     QStringList urls;
402     while (*it) {
403         if ((*it)->type() != PROJECTCLIPTYPE) {
404             it++;
405             continue;
406         }
407         item = static_cast <ProjectItem *>(*it);
408         if (item->numReferences() == 0) {
409             ids << item->clipId();
410             KUrl url = item->clipUrl();
411             if (!url.isEmpty() && !urls.contains(url.path()))
412                 urls << url.path();
413         }
414         it++;
415     }
416
417     // Check that we don't use the URL in another clip
418     QTreeWidgetItemIterator it2(m_listView);
419     while (*it2) {
420         if ((*it2)->type() != PROJECTCLIPTYPE) {
421             it2++;
422             continue;
423         }
424         item = static_cast <ProjectItem *>(*it2);
425         if (item->numReferences() > 0) {
426             KUrl url = item->clipUrl();
427             if (!url.isEmpty() && urls.contains(url.path())) urls.removeAll(url.path());
428         }
429         it2++;
430     }
431
432     emit deleteProjectClips(ids, QMap <QString, QString>());
433     for (int i = 0; i < urls.count(); i++)
434         KIO::NetAccess::del(KUrl(urls.at(i)), this);
435 }
436
437 void ProjectList::slotReloadClip(const QString &id)
438 {
439     QList<QTreeWidgetItem *> selected;
440     if (id.isEmpty())
441         selected = m_listView->selectedItems();
442     else {
443         ProjectItem *itemToReLoad = getItemById(id);
444         if (itemToReLoad) selected.append(itemToReLoad);
445     }
446     ProjectItem *item;
447     for (int i = 0; i < selected.count(); i++) {
448         if (selected.at(i)->type() != PROJECTCLIPTYPE) {
449             if (selected.at(i)->type() == PROJECTFOLDERTYPE) {
450                 for (int j = 0; j < selected.at(i)->childCount(); j++)
451                     selected.append(selected.at(i)->child(j));
452             }
453             continue;
454         }
455         item = static_cast <ProjectItem *>(selected.at(i));
456         if (item) {
457             CLIPTYPE t = item->clipType();
458             if (t == TEXT) {
459                 if (!item->referencedClip()->getProperty("xmltemplate").isEmpty())
460                     regenerateTemplate(item);
461             } else if (t != COLOR && t != SLIDESHOW && item->referencedClip() &&  item->referencedClip()->checkHash() == false) {
462                 item->referencedClip()->setPlaceHolder(true);
463                 item->setProperty("file_hash", QString());
464             } else if (t == IMAGE) {
465                 item->referencedClip()->producer()->set("force_reload", 1);
466             }
467
468             QDomElement e = item->toXml();
469             // Make sure we get the correct producer length if it was adjusted in timeline
470             if (t == COLOR || t == IMAGE || t == SLIDESHOW || t == TEXT) {
471                 int length = QString(item->referencedClip()->producerProperty("length")).toInt();
472                 if (length > 0 && !e.hasAttribute("length")) {
473                     e.setAttribute("length", length);
474                 }
475             }            
476             emit getFileProperties(e, item->clipId(), m_listView->iconSize().height(), true, false);
477         }
478     }
479 }
480
481 void ProjectList::slotModifiedClip(const QString &id)
482 {
483     ProjectItem *item = getItemById(id);
484     if (item) {
485         QPixmap pixmap = qVariantValue<QPixmap>(item->data(0, Qt::DecorationRole));
486         if (!pixmap.isNull()) {
487             QPainter p(&pixmap);
488             p.fillRect(0, 0, pixmap.width(), pixmap.height(), QColor(255, 255, 255, 200));
489             p.drawPixmap(0, 0, KIcon("view-refresh").pixmap(m_listView->iconSize()));
490             p.end();
491         } else {
492             pixmap = KIcon("view-refresh").pixmap(m_listView->iconSize());
493         }
494         item->setData(0, Qt::DecorationRole, pixmap);
495     }
496 }
497
498 void ProjectList::slotMissingClip(const QString &id)
499 {
500     ProjectItem *item = getItemById(id);
501     if (item) {
502         item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDropEnabled);
503         int height = m_listView->iconSize().height();
504         int width = (int)(height  * m_render->dar());
505         QPixmap pixmap = qVariantValue<QPixmap>(item->data(0, Qt::DecorationRole));
506         if (pixmap.isNull()) {
507             pixmap = QPixmap(width, height);
508             pixmap.fill(Qt::transparent);
509         }
510         KIcon icon("dialog-close");
511         QPainter p(&pixmap);
512         p.drawPixmap(3, 3, icon.pixmap(width - 6, height - 6));
513         p.end();
514         item->setData(0, Qt::DecorationRole, pixmap);
515         if (item->referencedClip()) {
516             item->referencedClip()->setPlaceHolder(true);
517             if (m_render == NULL) {
518                 kDebug() << "*********  ERROR, NULL RENDR";
519                 return;
520             }
521             Mlt::Producer *newProd = m_render->invalidProducer(id);
522             if (item->referencedClip()->producer()) {
523                 Mlt::Properties props(newProd->get_properties());
524                 Mlt::Properties src_props(item->referencedClip()->producer()->get_properties());
525                 props.inherit(src_props);
526             }
527             item->referencedClip()->setProducer(newProd, true);
528             item->slotSetToolTip();
529             emit clipNeedsReload(id, true);
530         }
531     }
532     update();
533     emit displayMessage(i18n("Check missing clips"), -2);
534     emit updateRenderStatus();
535 }
536
537 void ProjectList::slotAvailableClip(const QString &id)
538 {
539     ProjectItem *item = getItemById(id);
540     if (item == NULL)
541         return;
542     item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsDropEnabled);
543     if (item->referencedClip()) { // && item->referencedClip()->checkHash() == false) {
544         item->setProperty("file_hash", QString());
545         slotReloadClip(id);
546     }
547     /*else {
548     item->referencedClip()->setValid();
549     item->slotSetToolTip();
550     }
551     update();*/
552     emit updateRenderStatus();
553 }
554
555 bool ProjectList::hasMissingClips()
556 {
557     bool missing = false;
558     QTreeWidgetItemIterator it(m_listView);
559     while (*it) {
560         if ((*it)->type() == PROJECTCLIPTYPE && !((*it)->flags() & Qt::ItemIsDragEnabled)) {
561             missing = true;
562             break;
563         }
564         it++;
565     }
566     return missing;
567 }
568
569 void ProjectList::setRenderer(Render *projectRender)
570 {
571     m_render = projectRender;
572     m_listView->setIconSize(QSize((ProjectItem::itemDefaultHeight() - 2) * m_render->dar(), ProjectItem::itemDefaultHeight() - 2));
573 }
574
575 void ProjectList::slotClipSelected()
576 {
577     if (!m_listView->isEnabled()) return;
578     if (m_listView->currentItem()) {
579         if (m_listView->currentItem()->type() == PROJECTFOLDERTYPE) {
580             emit clipSelected(NULL);
581             m_editButton->defaultAction()->setEnabled(m_listView->currentItem()->childCount() > 0);
582             m_deleteButton->defaultAction()->setEnabled(true);
583             m_openAction->setEnabled(false);
584             m_reloadAction->setEnabled(false);
585             m_transcodeAction->setEnabled(false);
586             m_proxyAction->setEnabled(false);
587         } else {
588             ProjectItem *clip;
589             if (m_listView->currentItem()->type() == PROJECTSUBCLIPTYPE) {
590                 // this is a sub item, use base clip
591                 m_deleteButton->defaultAction()->setEnabled(true);
592                 clip = static_cast <ProjectItem*>(m_listView->currentItem()->parent());
593                 if (clip == NULL) kDebug() << "-----------ERROR";
594                 SubProjectItem *sub = static_cast <SubProjectItem*>(m_listView->currentItem());
595                 emit clipSelected(clip->referencedClip(), sub->zone());
596                 m_transcodeAction->setEnabled(false);
597                 return;
598             }
599             clip = static_cast <ProjectItem*>(m_listView->currentItem());
600             if (clip && clip->referencedClip())
601                 emit clipSelected(clip->referencedClip());
602             m_editButton->defaultAction()->setEnabled(true);
603             m_deleteButton->defaultAction()->setEnabled(true);
604             m_reloadAction->setEnabled(true);
605             m_transcodeAction->setEnabled(true);
606             if (clip && clip->clipType() == IMAGE && !KdenliveSettings::defaultimageapp().isEmpty()) {
607                 m_openAction->setIcon(KIcon(KdenliveSettings::defaultimageapp()));
608                 m_openAction->setEnabled(true);
609             } else if (clip && clip->clipType() == AUDIO && !KdenliveSettings::defaultaudioapp().isEmpty()) {
610                 m_openAction->setIcon(KIcon(KdenliveSettings::defaultaudioapp()));
611                 m_openAction->setEnabled(true);
612             } else {
613                 m_openAction->setEnabled(false);
614             }
615             // Display relevant transcoding actions only
616             adjustTranscodeActions(clip);
617             // Display uses in timeline
618             emit findInTimeline(clip->clipId());
619         }
620     } else {
621         emit clipSelected(NULL);
622         m_editButton->defaultAction()->setEnabled(false);
623         m_deleteButton->defaultAction()->setEnabled(false);
624         m_openAction->setEnabled(false);
625         m_reloadAction->setEnabled(false);
626         m_transcodeAction->setEnabled(false);
627     }
628 }
629
630 void ProjectList::adjustProxyActions(ProjectItem *clip) const
631 {
632     if (clip == NULL || clip->type() != PROJECTCLIPTYPE || clip->clipType() == COLOR || clip->clipType() == TEXT || clip->clipType() == SLIDESHOW || clip->clipType() == AUDIO) {
633         m_proxyAction->setEnabled(false);
634         return;
635     }
636     m_proxyAction->setEnabled(useProxy());
637     m_proxyAction->blockSignals(true);
638     m_proxyAction->setChecked(clip->hasProxy());
639     m_proxyAction->blockSignals(false);
640 }
641
642 void ProjectList::adjustTranscodeActions(ProjectItem *clip) const
643 {
644     if (clip == NULL || clip->type() != PROJECTCLIPTYPE || clip->clipType() == COLOR || clip->clipType() == TEXT || clip->clipType() == PLAYLIST || clip->clipType() == SLIDESHOW) {
645         m_transcodeAction->setEnabled(false);
646         return;
647     }
648     m_transcodeAction->setEnabled(true);
649     QList<QAction *> transcodeActions = m_transcodeAction->actions();
650     QStringList data;
651     QString condition;
652     for (int i = 0; i < transcodeActions.count(); i++) {
653         data = transcodeActions.at(i)->data().toStringList();
654         if (data.count() > 2) {
655             condition = data.at(2);
656             if (condition.startsWith("vcodec"))
657                 transcodeActions.at(i)->setEnabled(clip->referencedClip()->hasVideoCodec(condition.section('=', 1, 1)));
658             else if (condition.startsWith("acodec"))
659                 transcodeActions.at(i)->setEnabled(clip->referencedClip()->hasVideoCodec(condition.section('=', 1, 1)));
660         }
661     }
662
663 }
664
665 void ProjectList::slotPauseMonitor()
666 {
667     if (m_render)
668         m_render->pause();
669 }
670
671 void ProjectList::slotUpdateClipProperties(const QString &id, QMap <QString, QString> properties)
672 {
673     ProjectItem *item = getItemById(id);
674     if (item) {
675         slotUpdateClipProperties(item, properties);
676         if (properties.contains("out") || properties.contains("force_fps") || properties.contains("resource")) {
677             slotReloadClip(id);
678         } else if (properties.contains("colour") ||
679                    properties.contains("xmldata") ||
680                    properties.contains("force_aspect_num") ||
681                    properties.contains("force_aspect_den") ||
682                    properties.contains("templatetext")) {
683             slotRefreshClipThumbnail(item);
684             emit refreshClip(id, true);
685         } else if (properties.contains("full_luma") || properties.contains("force_colorspace") || properties.contains("loop")) {
686             emit refreshClip(id, false);
687         }
688     }
689 }
690
691 void ProjectList::slotUpdateClipProperties(ProjectItem *clip, QMap <QString, QString> properties)
692 {
693     if (!clip)
694         return;
695     clip->setProperties(properties);
696     if (properties.contains("name")) {
697         monitorItemEditing(false);
698         clip->setText(0, properties.value("name"));
699         monitorItemEditing(true);
700         emit clipNameChanged(clip->clipId(), properties.value("name"));
701     }
702     if (properties.contains("description")) {
703         CLIPTYPE type = clip->clipType();
704         monitorItemEditing(false);
705         clip->setText(1, properties.value("description"));
706         monitorItemEditing(true);
707 #ifdef NEPOMUK
708         if (KdenliveSettings::activate_nepomuk() && (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST)) {
709             // Use Nepomuk system to store clip description
710             Nepomuk::Resource f(clip->clipUrl().path());
711             f.setDescription(properties.value("description"));
712         }
713 #endif
714         emit projectModified();
715     }
716 }
717
718 void ProjectList::slotItemEdited(QTreeWidgetItem *item, int column)
719 {
720     if (item->type() == PROJECTSUBCLIPTYPE) {
721         // this is a sub-item
722         if (column == 1) {
723             // user edited description
724             SubProjectItem *sub = static_cast <SubProjectItem*>(item);
725             ProjectItem *item = static_cast <ProjectItem *>(sub->parent());
726             EditClipCutCommand *command = new EditClipCutCommand(this, item->clipId(), sub->zone(), sub->zone(), sub->description(), sub->text(1), true);
727             m_commandStack->push(command);
728             //slotUpdateCutClipProperties(sub->clipId(), sub->zone(), sub->text(1), sub->text(1));
729         }
730         return;
731     }
732     if (item->type() == PROJECTFOLDERTYPE) {
733         if (column == 0) {
734             FolderProjectItem *folder = static_cast <FolderProjectItem*>(item);
735             editFolder(item->text(0), folder->groupName(), folder->clipId());
736             folder->setGroupName(item->text(0));
737             m_doc->clipManager()->addFolder(folder->clipId(), item->text(0));
738             const int children = item->childCount();
739             for (int i = 0; i < children; i++) {
740                 ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
741                 child->setProperty("groupname", item->text(0));
742             }
743         }
744         return;
745     }
746
747     ProjectItem *clip = static_cast <ProjectItem*>(item);
748     if (column == 1) {
749         if (clip->referencedClip()) {
750             QMap <QString, QString> oldprops;
751             QMap <QString, QString> newprops;
752             oldprops["description"] = clip->referencedClip()->getProperty("description");
753             newprops["description"] = item->text(1);
754
755             if (clip->clipType() == TEXT) {
756                 // This is a text template clip, update the image
757                 /*oldprops.insert("xmldata", clip->referencedClip()->getProperty("xmldata"));
758                 newprops.insert("xmldata", generateTemplateXml(clip->referencedClip()->getProperty("xmltemplate"), item->text(2)).toString());*/
759                 oldprops.insert("templatetext", clip->referencedClip()->getProperty("templatetext"));
760                 newprops.insert("templatetext", item->text(1));
761             }
762             slotUpdateClipProperties(clip->clipId(), newprops);
763             EditClipCommand *command = new EditClipCommand(this, clip->clipId(), oldprops, newprops, false);
764             m_commandStack->push(command);
765         }
766     } else if (column == 0) {
767         if (clip->referencedClip()) {
768             QMap <QString, QString> oldprops;
769             QMap <QString, QString> newprops;
770             oldprops["name"] = clip->referencedClip()->getProperty("name");
771             if (oldprops.value("name") != item->text(0)) {
772                 newprops["name"] = item->text(0);
773                 slotUpdateClipProperties(clip, newprops);
774                 emit projectModified();
775                 EditClipCommand *command = new EditClipCommand(this, clip->clipId(), oldprops, newprops, false);
776                 m_commandStack->push(command);
777             }
778         }
779     }
780 }
781
782 void ProjectList::slotContextMenu(const QPoint &pos, QTreeWidgetItem *item)
783 {
784     bool enable = item ? true : false;
785     m_editButton->defaultAction()->setEnabled(enable);
786     m_deleteButton->defaultAction()->setEnabled(enable);
787     m_reloadAction->setEnabled(enable);
788     m_transcodeAction->setEnabled(enable);
789     if (enable) {
790         ProjectItem *clip = NULL;
791         if (m_listView->currentItem()->type() == PROJECTSUBCLIPTYPE) {
792             clip = static_cast <ProjectItem*>(item->parent());
793             m_transcodeAction->setEnabled(false);
794         } else if (m_listView->currentItem()->type() == PROJECTCLIPTYPE) {
795             clip = static_cast <ProjectItem*>(item);
796             // Display relevant transcoding actions only
797             adjustTranscodeActions(clip);
798             adjustProxyActions(clip);
799             // Display uses in timeline
800             emit findInTimeline(clip->clipId());
801         } else {
802             m_transcodeAction->setEnabled(false);
803         }
804         if (clip && clip->clipType() == IMAGE && !KdenliveSettings::defaultimageapp().isEmpty()) {
805             m_openAction->setIcon(KIcon(KdenliveSettings::defaultimageapp()));
806             m_openAction->setEnabled(true);
807         } else if (clip && clip->clipType() == AUDIO && !KdenliveSettings::defaultaudioapp().isEmpty()) {
808             m_openAction->setIcon(KIcon(KdenliveSettings::defaultaudioapp()));
809             m_openAction->setEnabled(true);
810         } else {
811             m_openAction->setEnabled(false);
812         }
813
814     } else {
815         m_openAction->setEnabled(false);
816     }
817     m_menu->popup(pos);
818 }
819
820 void ProjectList::slotRemoveClip()
821 {
822     if (!m_listView->currentItem())
823         return;
824     QStringList ids;
825     QMap <QString, QString> folderids;
826     QList<QTreeWidgetItem *> selected = m_listView->selectedItems();
827
828     QUndoCommand *delCommand = new QUndoCommand();
829     delCommand->setText(i18n("Delete Clip Zone"));
830     for (int i = 0; i < selected.count(); i++) {
831         if (selected.at(i)->type() == PROJECTSUBCLIPTYPE) {
832             // subitem
833             SubProjectItem *sub = static_cast <SubProjectItem *>(selected.at(i));
834             ProjectItem *item = static_cast <ProjectItem *>(sub->parent());
835             new AddClipCutCommand(this, item->clipId(), sub->zone().x(), sub->zone().y(), sub->description(), false, true, delCommand);
836         } else if (selected.at(i)->type() == PROJECTFOLDERTYPE) {
837             // folder
838             FolderProjectItem *folder = static_cast <FolderProjectItem *>(selected.at(i));
839             folderids[folder->groupName()] = folder->clipId();
840             int children = folder->childCount();
841
842             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)
843                 return;
844             for (int i = 0; i < children; ++i) {
845                 ProjectItem *child = static_cast <ProjectItem *>(folder->child(i));
846                 ids << child->clipId();
847             }
848         } else {
849             ProjectItem *item = static_cast <ProjectItem *>(selected.at(i));
850             ids << item->clipId();
851             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) {
852                 KMessageBox::enableMessage("DeleteAll");
853                 return;
854             }
855         }
856     }
857     KMessageBox::enableMessage("DeleteAll");
858     if (delCommand->childCount() == 0)
859         delete delCommand;
860     else
861         m_commandStack->push(delCommand);
862     emit deleteProjectClips(ids, folderids);
863 }
864
865 void ProjectList::updateButtons() const
866 {
867     if (m_listView->topLevelItemCount() == 0) {
868         m_deleteButton->defaultAction()->setEnabled(false);
869         m_editButton->defaultAction()->setEnabled(false);
870     } else {
871         m_deleteButton->defaultAction()->setEnabled(true);
872         if (!m_listView->currentItem())
873             m_listView->setCurrentItem(m_listView->topLevelItem(0));
874         QTreeWidgetItem *item = m_listView->currentItem();
875         if (item && item->type() == PROJECTCLIPTYPE) {
876             m_editButton->defaultAction()->setEnabled(true);
877             m_openAction->setEnabled(true);
878             m_reloadAction->setEnabled(true);
879             m_transcodeAction->setEnabled(true);
880             m_proxyAction->setEnabled(useProxy());
881             return;
882         }
883         else if (item && item->type() == PROJECTFOLDERTYPE && item->childCount() > 0) {
884             m_editButton->defaultAction()->setEnabled(true);
885         }
886         else m_editButton->defaultAction()->setEnabled(false);
887     }
888     m_openAction->setEnabled(false);
889     m_reloadAction->setEnabled(false);
890     m_transcodeAction->setEnabled(false);
891     m_proxyAction->setEnabled(false);
892 }
893
894 void ProjectList::selectItemById(const QString &clipId)
895 {
896     ProjectItem *item = getItemById(clipId);
897     if (item)
898         m_listView->setCurrentItem(item);
899 }
900
901
902 void ProjectList::slotDeleteClip(const QString &clipId)
903 {
904     ProjectItem *item = getItemById(clipId);
905     if (!item) {
906         kDebug() << "/// Cannot find clip to delete";
907         return;
908     }
909     if (item->isProxyRunning()) m_abortProxy.append(item->referencedClip()->getProperty("proxy"));
910     m_listView->blockSignals(true);
911     QTreeWidgetItem *newSelectedItem = m_listView->itemAbove(item);
912     if (!newSelectedItem)
913         newSelectedItem = m_listView->itemBelow(item);
914     delete item;
915     // Pause playing to prevent crash while deleting clip
916     slotPauseMonitor();
917     m_doc->clipManager()->deleteClip(clipId);
918     m_listView->blockSignals(false);
919     if (newSelectedItem) {
920         m_listView->setCurrentItem(newSelectedItem);
921     } else {
922         updateButtons();
923         emit clipSelected(NULL);
924     }
925 }
926
927
928 void ProjectList::editFolder(const QString folderName, const QString oldfolderName, const QString &clipId)
929 {
930     EditFolderCommand *command = new EditFolderCommand(this, folderName, oldfolderName, clipId, false);
931     m_commandStack->push(command);
932     m_doc->setModified(true);
933 }
934
935 void ProjectList::slotAddFolder()
936 {
937     AddFolderCommand *command = new AddFolderCommand(this, i18n("Folder"), QString::number(m_doc->clipManager()->getFreeFolderId()), true);
938     m_commandStack->push(command);
939 }
940
941 void ProjectList::slotAddFolder(const QString foldername, const QString &clipId, bool remove, bool edit)
942 {
943     if (remove) {
944         FolderProjectItem *item = getFolderItemById(clipId);
945         if (item) {
946             m_doc->clipManager()->deleteFolder(clipId);
947             QTreeWidgetItem *newSelectedItem = m_listView->itemAbove(item);
948             if (!newSelectedItem)
949                 newSelectedItem = m_listView->itemBelow(item);
950             delete item;
951             if (newSelectedItem)
952                 m_listView->setCurrentItem(newSelectedItem);
953             else
954                 updateButtons();
955         }
956     } else {
957         if (edit) {
958             FolderProjectItem *item = getFolderItemById(clipId);
959             if (item) {
960                 m_listView->blockSignals(true);
961                 item->setGroupName(foldername);
962                 m_listView->blockSignals(false);
963                 m_doc->clipManager()->addFolder(clipId, foldername);
964                 const int children = item->childCount();
965                 for (int i = 0; i < children; i++) {
966                     ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
967                     child->setProperty("groupname", foldername);
968                 }
969             }
970         } else {
971             m_listView->blockSignals(true);
972             m_listView->setCurrentItem(new FolderProjectItem(m_listView, QStringList() << foldername, clipId));
973             m_doc->clipManager()->addFolder(clipId, foldername);
974             m_listView->blockSignals(false);
975             m_listView->editItem(m_listView->currentItem(), 0);
976         }
977         updateButtons();
978     }
979     m_doc->setModified(true);
980 }
981
982
983
984 void ProjectList::deleteProjectFolder(QMap <QString, QString> map)
985 {
986     QMapIterator<QString, QString> i(map);
987     QUndoCommand *delCommand = new QUndoCommand();
988     delCommand->setText(i18n("Delete Folder"));
989     while (i.hasNext()) {
990         i.next();
991         new AddFolderCommand(this, i.key(), i.value(), false, delCommand);
992     }
993     if (delCommand->childCount() > 0) m_commandStack->push(delCommand);
994     else delete delCommand;
995 }
996
997 void ProjectList::slotAddClip(DocClipBase *clip, bool getProperties)
998 {
999     m_listView->setEnabled(false);
1000     const QString parent = clip->getProperty("groupid");
1001     ProjectItem *item = NULL;
1002     monitorItemEditing(false);
1003     if (!parent.isEmpty()) {
1004         FolderProjectItem *parentitem = getFolderItemById(parent);
1005         if (!parentitem) {
1006             QStringList text;
1007             QString groupName = clip->getProperty("groupname");
1008             //kDebug() << "Adding clip to new group: " << groupName;
1009             if (groupName.isEmpty()) groupName = i18n("Folder");
1010             text << groupName;
1011             parentitem = new FolderProjectItem(m_listView, text, parent);
1012         }
1013
1014         if (parentitem)
1015             item = new ProjectItem(parentitem, clip);
1016     }
1017     if (item == NULL) {
1018         item = new ProjectItem(m_listView, clip);
1019     }
1020     if (item->data(0, DurationRole).isNull()) item->setData(0, DurationRole, i18n("Loading"));
1021     connect(clip, SIGNAL(createProxy(const QString)), this, SLOT(slotCreateProxy(const QString)));
1022     connect(clip, SIGNAL(abortProxy(const QString, const QString)), this, SLOT(slotAbortProxy(const QString, const QString)));
1023     if (getProperties) {
1024         m_listView->processLayout();
1025         QDomElement e = clip->toXML().cloneNode().toElement();
1026         e.removeAttribute("file_hash");
1027         m_infoQueue.insert(clip->getId(), e);
1028     }
1029     else if (item->hasProxy() && !item->isProxyRunning()) {
1030         slotCreateProxy(clip->getId());
1031     }
1032     clip->askForAudioThumbs();
1033     
1034     KUrl url = clip->fileURL();
1035     if (getProperties == false && !clip->getClipHash().isEmpty()) {
1036         QString cachedPixmap = m_doc->projectFolder().path(KUrl::AddTrailingSlash) + "thumbs/" + clip->getClipHash() + ".png";
1037         if (QFile::exists(cachedPixmap)) {
1038             QPixmap pix(cachedPixmap);
1039             if (pix.isNull())
1040                 KIO::NetAccess::del(KUrl(cachedPixmap), this);
1041             item->setData(0, Qt::DecorationRole, pix);
1042         }
1043     }
1044 #ifdef NEPOMUK
1045     if (!url.isEmpty() && KdenliveSettings::activate_nepomuk()) {
1046         // if file has Nepomuk comment, use it
1047         Nepomuk::Resource f(url.path());
1048         QString annotation = f.description();
1049         if (!annotation.isEmpty()) item->setText(1, annotation);
1050         item->setText(2, QString::number(f.rating()));
1051     }
1052 #endif
1053     // Add cut zones
1054     QList <CutZoneInfo> cuts = clip->cutZones();
1055     if (!cuts.isEmpty()) {
1056         for (int i = 0; i < cuts.count(); i++) {
1057             SubProjectItem *sub = new SubProjectItem(item, cuts.at(i).zone.x(), cuts.at(i).zone.y(), cuts.at(i).description);
1058             if (!clip->getClipHash().isEmpty()) {
1059                 QString cachedPixmap = m_doc->projectFolder().path(KUrl::AddTrailingSlash) + "thumbs/" + clip->getClipHash() + '#' + QString::number(cuts.at(i).zone.x()) + ".png";
1060                 if (QFile::exists(cachedPixmap)) {
1061                     QPixmap pix(cachedPixmap);
1062                     if (pix.isNull())
1063                         KIO::NetAccess::del(KUrl(cachedPixmap), this);
1064                     sub->setData(0, Qt::DecorationRole, pix);
1065                 }
1066             }
1067         }
1068     }
1069     monitorItemEditing(true);
1070     if (m_listView->isEnabled()) {
1071         updateButtons();
1072     }
1073     
1074     if (getProperties && m_processingClips.isEmpty())
1075         m_queueRunner = QtConcurrent::run(this, &ProjectList::slotProcessNextClipInQueue);
1076 }
1077
1078 void ProjectList::slotGotProxy(const QString &proxyPath)
1079 {
1080     if (proxyPath.isEmpty() || !m_refreshed) return;
1081     QTreeWidgetItemIterator it(m_listView);
1082     ProjectItem *item;
1083
1084     while (*it) {
1085         if ((*it)->type() == PROJECTCLIPTYPE) {
1086             item = static_cast <ProjectItem *>(*it);
1087             if (item->referencedClip()->getProperty("proxy") == proxyPath)
1088                 slotGotProxy(item);
1089         }
1090         ++it;
1091     }
1092 }
1093
1094 void ProjectList::slotGotProxy(ProjectItem *item)
1095 {
1096     if (item == NULL || !m_refreshed) return;
1097     DocClipBase *clip = item->referencedClip();
1098     // Proxy clip successfully created
1099     QDomElement e = clip->toXML().cloneNode().toElement();
1100
1101     // Make sure we get the correct producer length if it was adjusted in timeline
1102     CLIPTYPE t = item->clipType();
1103     if (t == COLOR || t == IMAGE || t == SLIDESHOW || t == TEXT) {
1104         int length = QString(clip->producerProperty("length")).toInt();
1105         if (length > 0 && !e.hasAttribute("length")) {
1106             e.setAttribute("length", length);
1107         }
1108     }
1109     e.setAttribute("replace", 1);
1110     m_infoQueue.insert(clip->getId(), e);
1111     if (!m_queueRunner.isRunning() && m_processingClips.isEmpty()) m_queueRunner = QtConcurrent::run(this, &ProjectList::slotProcessNextClipInQueue);   
1112 }
1113
1114 void ProjectList::slotResetProjectList()
1115 {
1116     m_listView->clear();
1117     emit clipSelected(NULL);
1118     m_thumbnailQueue.clear();
1119     m_infoQueue.clear();
1120     m_refreshed = false;
1121 }
1122
1123 void ProjectList::requestClipInfo(const QDomElement xml, const QString id)
1124 {
1125     m_infoQueue.insert(id, xml);
1126     //if (m_infoQueue.count() == 1 || ) QTimer::singleShot(300, this, SLOT(slotProcessNextClipInQueue()));
1127 }
1128
1129 void ProjectList::slotProcessNextClipInQueue()
1130 {
1131     if (m_infoQueue.isEmpty()) {
1132         emit processNextThumbnail();
1133         return;
1134     }
1135
1136     QMap<QString, QDomElement>::const_iterator j = m_infoQueue.constBegin();
1137     if (j != m_infoQueue.constEnd()) {
1138         QDomElement dom = j.value().cloneNode().toElement();
1139         const QString id = j.key();
1140         m_infoQueue.remove(id);
1141         m_processingClips.append(id);
1142         bool replace;
1143         if (dom.hasAttribute("replace")) {
1144             // Proxy action was enabled / disabled and we want to replace current producer
1145             dom.removeAttribute("replace");
1146             replace = true;
1147         }
1148         else replace = false;
1149         bool selectClip = !replace;
1150         if (m_infoQueue.count() > 1) selectClip = false;
1151         emit getFileProperties(dom, id, m_listView->iconSize().height(), replace, selectClip);
1152     }
1153 }
1154
1155 void ProjectList::slotUpdateClip(const QString &id)
1156 {
1157     ProjectItem *item = getItemById(id);
1158     monitorItemEditing(false);
1159     if (item) item->setData(0, UsageRole, QString::number(item->numReferences()));
1160     monitorItemEditing(true);
1161 }
1162
1163 void ProjectList::updateAllClips(bool displayRatioChanged, bool fpsChanged)
1164 {
1165     m_listView->setSortingEnabled(false);
1166
1167     QTreeWidgetItemIterator it(m_listView);
1168     DocClipBase *clip;
1169     ProjectItem *item;
1170     monitorItemEditing(false);
1171     int height = m_listView->iconSize().height();
1172     int width = (int)(height  * m_render->dar());
1173     QPixmap missingPixmap = QPixmap(width, height);
1174     missingPixmap.fill(Qt::transparent);
1175     KIcon icon("dialog-close");
1176     QPainter p(&missingPixmap);
1177     p.drawPixmap(3, 3, icon.pixmap(width - 6, height - 6));
1178     p.end();
1179                         
1180     while (*it) {
1181         if ((*it)->type() == PROJECTSUBCLIPTYPE) {
1182             // subitem
1183             SubProjectItem *sub = static_cast <SubProjectItem *>(*it);
1184             if (displayRatioChanged || sub->data(0, Qt::DecorationRole).isNull()) {
1185                 item = static_cast <ProjectItem *>((*it)->parent());
1186                 requestClipThumbnail(item->clipId() + '#' + QString::number(sub->zone().x()));
1187             }
1188             ++it;
1189             continue;
1190         } else if ((*it)->type() == PROJECTFOLDERTYPE) {
1191             // folder
1192             ++it;
1193             continue;
1194         } else {
1195             item = static_cast <ProjectItem *>(*it);
1196             clip = item->referencedClip();
1197             if (item->referencedClip()->producer() == NULL) {
1198                 if (clip->isPlaceHolder() == false) {
1199                     QDomElement xml = clip->toXML();
1200                     if (fpsChanged) {
1201                         xml.removeAttribute("out");
1202                         xml.removeAttribute("file_hash");
1203                         xml.removeAttribute("proxy_out");
1204                     }
1205                     requestClipInfo(xml, clip->getId());
1206                 }
1207                 else {
1208                     item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDropEnabled);
1209                     if (item->data(0, Qt::DecorationRole).isNull()) {
1210                         item->setData(0, Qt::DecorationRole, missingPixmap);
1211                     }
1212                     else {
1213                         QPixmap pixmap = qVariantValue<QPixmap>(item->data(0, Qt::DecorationRole));
1214                         QPainter p(&pixmap);
1215                         p.drawPixmap(3, 3, KIcon("dialog-close").pixmap(pixmap.width() - 6, pixmap.height() - 6));
1216                         p.end();
1217                         item->setData(0, Qt::DecorationRole, pixmap);
1218                     }
1219                 }
1220             } else {
1221                 if (displayRatioChanged || item->data(0, Qt::DecorationRole).isNull())
1222                     requestClipThumbnail(clip->getId());
1223                 if (item->data(0, DurationRole).toString().isEmpty()) {
1224                     item->changeDuration(item->referencedClip()->producer()->get_playtime());
1225                 }
1226                 if (clip->isPlaceHolder()) {
1227                     QPixmap pixmap = qVariantValue<QPixmap>(item->data(0, Qt::DecorationRole));
1228                     if (pixmap.isNull()) {
1229                         pixmap = QPixmap(width, height);
1230                         pixmap.fill(Qt::transparent);
1231                     }
1232                     QPainter p(&pixmap);
1233                     p.drawPixmap(3, 3, KIcon("dialog-close").pixmap(pixmap.width() - 6, pixmap.height() - 6));
1234                     p.end();
1235                     item->setData(0, Qt::DecorationRole, pixmap);
1236                 }
1237             }
1238             item->setData(0, UsageRole, QString::number(item->numReferences()));
1239         }
1240         ++it;
1241     }
1242
1243     if (!m_queueRunner.isRunning() && m_processingClips.isEmpty()) m_queueRunner = QtConcurrent::run(this, &ProjectList::slotProcessNextClipInQueue);
1244     if (m_listView->isEnabled())
1245         monitorItemEditing(true);
1246     m_listView->setSortingEnabled(true);
1247     if (m_infoQueue.isEmpty()) {
1248        slotProcessNextThumbnail();
1249     }
1250 }
1251
1252 // static
1253 QString ProjectList::getExtensions()
1254 {
1255     // Build list of mime types
1256     QStringList mimeTypes = QStringList() << "application/x-kdenlive" << "application/x-kdenlivetitle" << "video/mlt-playlist" << "text/plain"
1257                             << "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"
1258                             << "audio/x-flac" << "audio/x-matroska" << "audio/mp4" << "audio/mpeg" << "audio/x-mp3" << "audio/ogg" << "audio/x-wav" << "audio/x-aiff" << "audio/aiff" << "application/ogg" << "application/mxf" << "application/x-shockwave-flash"
1259                             << "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";
1260
1261     QString allExtensions;
1262     foreach(const QString & mimeType, mimeTypes) {
1263         KMimeType::Ptr mime(KMimeType::mimeType(mimeType));
1264         if (mime) {
1265             allExtensions.append(mime->patterns().join(" "));
1266             allExtensions.append(' ');
1267         }
1268     }
1269     return allExtensions.simplified();
1270 }
1271
1272 void ProjectList::slotAddClip(const QList <QUrl> givenList, const QString &groupName, const QString &groupId)
1273 {
1274     if (!m_commandStack)
1275         kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
1276
1277     KUrl::List list;
1278     if (givenList.isEmpty()) {
1279         QString allExtensions = getExtensions();
1280         const QString dialogFilter = allExtensions + ' ' + QLatin1Char('|') + i18n("All Supported Files") + "\n* " + QLatin1Char('|') + i18n("All Files");
1281         QCheckBox *b = new QCheckBox(i18n("Import image sequence"));
1282         b->setChecked(KdenliveSettings::autoimagesequence());
1283         QCheckBox *c = new QCheckBox(i18n("Transparent background for images"));
1284         c->setChecked(KdenliveSettings::autoimagetransparency());
1285         QFrame *f = new QFrame;
1286         f->setFrameShape(QFrame::NoFrame);
1287         QHBoxLayout *l = new QHBoxLayout;
1288         l->addWidget(b);
1289         l->addWidget(c);
1290         l->addStretch(5);
1291         f->setLayout(l);
1292         KFileDialog *d = new KFileDialog(KUrl("kfiledialog:///clipfolder"), dialogFilter, kapp->activeWindow(), f);
1293         d->setOperationMode(KFileDialog::Opening);
1294         d->setMode(KFile::Files);
1295         if (d->exec() == QDialog::Accepted) {
1296             KdenliveSettings::setAutoimagetransparency(c->isChecked());
1297         }
1298         list = d->selectedUrls();
1299         if (b->isChecked() && list.count() == 1) {
1300             // Check for image sequence
1301             KUrl url = list.at(0);
1302             QString fileName = url.fileName().section('.', 0, -2);
1303             if (fileName.at(fileName.size() - 1).isDigit()) {
1304                 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, url);
1305                 if (item.mimetype().startsWith("image")) {
1306                     // import as sequence if we found more than one image in the sequence
1307                     QStringList list;
1308                     QString pattern = SlideshowClip::selectedPath(url.path(), false, QString(), &list);
1309                     int count = list.count();
1310                     if (count > 1) {
1311                         delete d;
1312                         QStringList groupInfo = getGroup();
1313
1314                         // get image sequence base name
1315                         while (fileName.at(fileName.size() - 1).isDigit()) {
1316                             fileName.chop(1);
1317                         }
1318
1319                         m_doc->slotCreateSlideshowClipFile(fileName, pattern, count, m_timecode.reformatSeparators(KdenliveSettings::sequence_duration()),
1320                                                            false, false, false,
1321                                                            m_timecode.getTimecodeFromFrames(int(ceil(m_timecode.fps()))), QString(), 0,
1322                                                            QString(), groupInfo.at(0), groupInfo.at(1));
1323                         return;
1324                     }
1325                 }
1326             }
1327         }
1328         delete d;
1329     } else {
1330         for (int i = 0; i < givenList.count(); i++)
1331             list << givenList.at(i);
1332     }
1333
1334     foreach(const KUrl & file, list) {
1335         // Check there is no folder here
1336         KMimeType::Ptr type = KMimeType::findByUrl(file);
1337         if (type->is("inode/directory")) {
1338             // user dropped a folder
1339             list.removeAll(file);
1340         }
1341     }
1342
1343     if (list.isEmpty())
1344         return;
1345
1346     if (givenList.isEmpty()) {
1347         QStringList groupInfo = getGroup();
1348         m_doc->slotAddClipList(list, groupInfo.at(0), groupInfo.at(1));
1349     } else {
1350         m_doc->slotAddClipList(list, groupName, groupId);
1351     }
1352 }
1353
1354 void ProjectList::slotRemoveInvalidClip(const QString &id, bool replace)
1355 {
1356     ProjectItem *item = getItemById(id);
1357     m_processingClips.removeAll(id);
1358     m_thumbnailQueue.removeAll(id);
1359     if (!m_queueRunner.isRunning() && m_processingClips.isEmpty()) m_queueRunner = QtConcurrent::run(this, &ProjectList::slotProcessNextClipInQueue);
1360     if (item) {
1361         const QString path = item->referencedClip()->fileURL().path();
1362         if (item->referencedClip()->isPlaceHolder()) replace = false;
1363         if (!path.isEmpty()) {
1364             if (replace)
1365                 KMessageBox::sorry(kapp->activeWindow(), i18n("Clip <b>%1</b><br />is invalid, will be removed from project.", path));
1366             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)
1367                 replace = true;
1368         }
1369         if (replace)
1370             emit deleteProjectClips(QStringList() << id, QMap <QString, QString>());
1371     }
1372 }
1373
1374 void ProjectList::slotRemoveInvalidProxy(const QString &id, bool durationError)
1375 {
1376     ProjectItem *item = getItemById(id);
1377     if (item) {
1378         if (durationError) {
1379             kDebug() << "Proxy duration is wrong, try changing transcoding parameters.";
1380             emit displayMessage(i18n("Proxy clip unusable (duration is different from original)."), -2);
1381         }
1382         item->setProxyStatus(PROXYCRASHED);
1383         QString path = item->referencedClip()->getProperty("proxy");
1384         KUrl proxyFolder(m_doc->projectFolder().path( KUrl::AddTrailingSlash) + "proxy/");
1385
1386         //Security check: make sure the invalid proxy file is in the proxy folder
1387         if (proxyFolder.isParentOf(KUrl(path))) {
1388             QFile::remove(path);
1389         }
1390     }
1391     m_processingClips.removeAll(id);
1392     m_thumbnailQueue.removeAll(id);
1393     if (!m_queueRunner.isRunning() && m_processingClips.isEmpty()) m_queueRunner = QtConcurrent::run(this, &ProjectList::slotProcessNextClipInQueue);
1394 }
1395
1396 void ProjectList::slotAddColorClip()
1397 {
1398     if (!m_commandStack)
1399         kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
1400
1401     QDialog *dia = new QDialog(this);
1402     Ui::ColorClip_UI dia_ui;
1403     dia_ui.setupUi(dia);
1404     dia->setWindowTitle(i18n("Color Clip"));
1405     dia_ui.clip_name->setText(i18n("Color Clip"));
1406
1407     TimecodeDisplay *t = new TimecodeDisplay(m_timecode);
1408     t->setValue(KdenliveSettings::color_duration());
1409     t->setTimeCodeFormat(false);
1410     dia_ui.clip_durationBox->addWidget(t);
1411     dia_ui.clip_color->setColor(KdenliveSettings::colorclipcolor());
1412
1413     if (dia->exec() == QDialog::Accepted) {
1414         QString color = dia_ui.clip_color->color().name();
1415         KdenliveSettings::setColorclipcolor(color);
1416         color = color.replace(0, 1, "0x") + "ff";
1417         QStringList groupInfo = getGroup();
1418         m_doc->slotCreateColorClip(dia_ui.clip_name->text(), color, m_timecode.getTimecode(t->gentime()), groupInfo.at(0), groupInfo.at(1));
1419     }
1420     delete t;
1421     delete dia;
1422 }
1423
1424
1425 void ProjectList::slotAddSlideshowClip()
1426 {
1427     if (!m_commandStack)
1428         kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
1429
1430     SlideshowClip *dia = new SlideshowClip(m_timecode, this);
1431
1432     if (dia->exec() == QDialog::Accepted) {
1433         QStringList groupInfo = getGroup();
1434         m_doc->slotCreateSlideshowClipFile(dia->clipName(), dia->selectedPath(), dia->imageCount(), dia->clipDuration(),
1435                                            dia->loop(), dia->crop(), dia->fade(),
1436                                            dia->lumaDuration(), dia->lumaFile(), dia->softness(),
1437                                            dia->animation(), groupInfo.at(0), groupInfo.at(1));
1438     }
1439     delete dia;
1440 }
1441
1442 void ProjectList::slotAddTitleClip()
1443 {
1444     QStringList groupInfo = getGroup();
1445     m_doc->slotCreateTextClip(groupInfo.at(0), groupInfo.at(1));
1446 }
1447
1448 void ProjectList::slotAddTitleTemplateClip()
1449 {
1450     if (!m_commandStack)
1451         kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
1452
1453     QStringList groupInfo = getGroup();
1454
1455     // Get the list of existing templates
1456     QStringList filter;
1457     filter << "*.kdenlivetitle";
1458     const QString path = m_doc->projectFolder().path(KUrl::AddTrailingSlash) + "titles/";
1459     QStringList templateFiles = QDir(path).entryList(filter, QDir::Files);
1460
1461     QDialog *dia = new QDialog(this);
1462     Ui::TemplateClip_UI dia_ui;
1463     dia_ui.setupUi(dia);
1464     for (int i = 0; i < templateFiles.size(); ++i)
1465         dia_ui.template_list->comboBox()->addItem(templateFiles.at(i), path + templateFiles.at(i));
1466
1467     if (!templateFiles.isEmpty())
1468         dia_ui.buttonBox->button(QDialogButtonBox::Ok)->setFocus();
1469     dia_ui.template_list->fileDialog()->setFilter("application/x-kdenlivetitle");
1470     //warning: setting base directory doesn't work??
1471     KUrl startDir(path);
1472     dia_ui.template_list->fileDialog()->setUrl(startDir);
1473     dia_ui.text_box->setHidden(true);
1474     if (dia->exec() == QDialog::Accepted) {
1475         QString textTemplate = dia_ui.template_list->comboBox()->itemData(dia_ui.template_list->comboBox()->currentIndex()).toString();
1476         if (textTemplate.isEmpty()) textTemplate = dia_ui.template_list->comboBox()->currentText();
1477         // Create a cloned template clip
1478         m_doc->slotCreateTextTemplateClip(groupInfo.at(0), groupInfo.at(1), KUrl(textTemplate));
1479     }
1480     delete dia;
1481 }
1482
1483 QStringList ProjectList::getGroup() const
1484 {
1485     QStringList result;
1486     QTreeWidgetItem *item = m_listView->currentItem();
1487     while (item && item->type() != PROJECTFOLDERTYPE)
1488         item = item->parent();
1489
1490     if (item) {
1491         FolderProjectItem *folder = static_cast <FolderProjectItem *>(item);
1492         result << folder->groupName() << folder->clipId();
1493     } else {
1494         result << QString() << QString();
1495     }
1496     return result;
1497 }
1498
1499 void ProjectList::setDocument(KdenliveDoc *doc)
1500 {
1501     m_listView->blockSignals(true);
1502     m_listView->clear();
1503     m_processingClips.clear();
1504     m_listView->setSortingEnabled(false);
1505     emit clipSelected(NULL);
1506     m_thumbnailQueue.clear();
1507     m_infoQueue.clear();
1508     m_refreshed = false;
1509     m_fps = doc->fps();
1510     m_timecode = doc->timecode();
1511     m_commandStack = doc->commandStack();
1512     m_doc = doc;
1513
1514     QMap <QString, QString> flist = doc->clipManager()->documentFolderList();
1515     QStringList openedFolders = doc->getExpandedFolders();
1516     QMapIterator<QString, QString> f(flist);
1517     while (f.hasNext()) {
1518         f.next();
1519         FolderProjectItem *folder = new FolderProjectItem(m_listView, QStringList() << f.value(), f.key());
1520         folder->setExpanded(openedFolders.contains(f.key()));
1521     }
1522
1523     QList <DocClipBase*> list = doc->clipManager()->documentClipList();
1524     if (list.isEmpty()) m_refreshed = true;
1525     for (int i = 0; i < list.count(); i++)
1526         slotAddClip(list.at(i), false);
1527
1528     m_listView->blockSignals(false);
1529     connect(m_doc->clipManager(), SIGNAL(reloadClip(const QString &)), this, SLOT(slotReloadClip(const QString &)));
1530     connect(m_doc->clipManager(), SIGNAL(modifiedClip(const QString &)), this, SLOT(slotModifiedClip(const QString &)));
1531     connect(m_doc->clipManager(), SIGNAL(missingClip(const QString &)), this, SLOT(slotMissingClip(const QString &)));
1532     connect(m_doc->clipManager(), SIGNAL(availableClip(const QString &)), this, SLOT(slotAvailableClip(const QString &)));
1533     connect(m_doc->clipManager(), SIGNAL(checkAllClips(bool, bool)), this, SLOT(updateAllClips(bool, bool)));
1534 }
1535
1536 QList <DocClipBase*> ProjectList::documentClipList() const
1537 {
1538     if (m_doc == NULL)
1539         return QList <DocClipBase*> ();
1540
1541     return m_doc->clipManager()->documentClipList();
1542 }
1543
1544 QDomElement ProjectList::producersList()
1545 {
1546     QDomDocument doc;
1547     QDomElement prods = doc.createElement("producerlist");
1548     doc.appendChild(prods);
1549     kDebug() << "////////////  PRO LIST BUILD PRDSLIST ";
1550     QTreeWidgetItemIterator it(m_listView);
1551     while (*it) {
1552         if ((*it)->type() != PROJECTCLIPTYPE) {
1553             // subitem
1554             ++it;
1555             continue;
1556         }
1557         prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
1558         ++it;
1559     }
1560     return prods;
1561 }
1562
1563 void ProjectList::slotCheckForEmptyQueue()
1564 {
1565     if (m_processingClips.isEmpty() && m_thumbnailQueue.isEmpty() && m_infoQueue.isEmpty()) {
1566         if (!m_refreshed) {
1567             emit loadingIsOver();
1568             emit displayMessage(QString(), -1);
1569         }
1570         m_refreshed = true;
1571         m_listView->blockSignals(false);
1572         m_listView->setEnabled(true);
1573         updateButtons();
1574     } else if (!m_refreshed) {
1575         QTimer::singleShot(300, this, SLOT(slotCheckForEmptyQueue()));
1576     }
1577 }
1578
1579
1580 void ProjectList::requestClipThumbnail(const QString id)
1581 {
1582     if (!m_thumbnailQueue.contains(id)) m_thumbnailQueue.append(id);
1583 }
1584
1585 void ProjectList::slotProcessNextThumbnail()
1586 {
1587     if (m_thumbnailQueue.isEmpty() && m_infoQueue.isEmpty()) {
1588         slotCheckForEmptyQueue();
1589         return;
1590     }
1591     if (!m_infoQueue.isEmpty()) {
1592         //QTimer::singleShot(300, this, SLOT(slotProcessNextThumbnail()));
1593         return;
1594     }
1595     if (m_thumbnailQueue.count() > 1) {
1596         int max = m_doc->clipManager()->clipsCount();
1597         emit displayMessage(i18n("Loading thumbnails"), (int)(100 *(max - m_thumbnailQueue.count()) / max));
1598     }
1599     slotRefreshClipThumbnail(m_thumbnailQueue.takeFirst(), false);
1600 }
1601
1602 void ProjectList::slotRefreshClipThumbnail(const QString &clipId, bool update)
1603 {
1604     QTreeWidgetItem *item = getAnyItemById(clipId);
1605     if (item)
1606         slotRefreshClipThumbnail(item, update);
1607     else {
1608         slotProcessNextThumbnail();
1609     }
1610 }
1611
1612 void ProjectList::slotRefreshClipThumbnail(QTreeWidgetItem *it, bool update)
1613 {
1614     if (it == NULL) return;
1615     ProjectItem *item = NULL;
1616     bool isSubItem = false;
1617     int frame;
1618     if (it->type() == PROJECTFOLDERTYPE) return;
1619     if (it->type() == PROJECTSUBCLIPTYPE) {
1620         item = static_cast <ProjectItem *>(it->parent());
1621         frame = static_cast <SubProjectItem *>(it)->zone().x();
1622         isSubItem = true;
1623     } else {
1624         item = static_cast <ProjectItem *>(it);
1625         frame = item->referencedClip()->getClipThumbFrame();
1626     }
1627
1628     if (item) {
1629         DocClipBase *clip = item->referencedClip();
1630         if (!clip) {
1631             slotProcessNextThumbnail();
1632             return;
1633         }
1634         QPixmap pix;
1635         int height = m_listView->iconSize().height();
1636         int width = (int)(height  * m_render->dar());
1637         if (clip->clipType() == AUDIO)
1638             pix = KIcon("audio-x-generic").pixmap(QSize(width, height));
1639         else if (clip->clipType() == IMAGE)
1640             pix = QPixmap::fromImage(KThumb::getFrame(item->referencedClip()->producer(), 0, width, height));
1641         else
1642             pix = item->referencedClip()->extractImage(frame, width, height);
1643
1644         if (!pix.isNull()) {
1645             monitorItemEditing(false);
1646             it->setData(0, Qt::DecorationRole, pix);
1647             monitorItemEditing(true);
1648                 
1649             if (!isSubItem)
1650                 m_doc->cachePixmap(item->getClipHash(), pix);
1651             else
1652                 m_doc->cachePixmap(item->getClipHash() + '#' + QString::number(frame), pix);
1653         }
1654         if (update)
1655             emit projectModified();
1656         slotProcessNextThumbnail();
1657     }
1658 }
1659
1660 void ProjectList::slotReplyGetFileProperties(const QString &clipId, Mlt::Producer *producer, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata, bool replace, bool selectClip)
1661 {
1662     QString toReload;
1663     ProjectItem *item = getItemById(clipId);
1664     if (!m_refreshed) {
1665         // we are still finishing to load the document
1666         selectClip = false;
1667     }
1668     m_processingClips.removeAll(clipId);
1669     if (m_infoQueue.isEmpty() && m_processingClips.isEmpty()) m_listView->setEnabled(true);
1670     if (item && producer) {
1671         //m_listView->blockSignals(true);
1672         monitorItemEditing(false);
1673         DocClipBase *clip = item->referencedClip();
1674         item->setProperties(properties, metadata);
1675         if (clip->isPlaceHolder() && producer->is_valid()) {
1676             clip->setValid();
1677             item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsDropEnabled);
1678             toReload = clipId;
1679         }
1680         clip->setProducer(producer, replace);
1681         clip->askForAudioThumbs();
1682         // Proxy stuff
1683         QString size = properties.value("frame_size");
1684         if (!useProxy() && clip->getProperty("proxy").isEmpty()) setProxyStatus(item, NOPROXY);
1685         if (useProxy() && generateProxy() && clip->getProperty("proxy") == "-") setProxyStatus(item, NOPROXY);
1686         else if (useProxy() && !item->isProxyRunning()) {
1687             // proxy video and image clips
1688             int maxSize;
1689             CLIPTYPE t = item->clipType();
1690             if (t == IMAGE) maxSize = m_doc->getDocumentProperty("proxyimageminsize").toInt();
1691             else maxSize = m_doc->getDocumentProperty("proxyminsize").toInt();
1692             if ((((t == AV || t == VIDEO || t == PLAYLIST) && generateProxy()) || (t == IMAGE && generateImageProxy())) && (size.section('x', 0, 0).toInt() > maxSize || size.section('x', 1, 1).toInt() > maxSize)) {
1693                 if (clip->getProperty("proxy").isEmpty()) {
1694                     KUrl proxyPath = m_doc->projectFolder();
1695                     proxyPath.addPath("proxy/");
1696                     proxyPath.addPath(clip->getClipHash() + "." + (t == IMAGE ? "png" : m_doc->getDocumentProperty("proxyextension")));
1697                     QMap <QString, QString> newProps;
1698                     // insert required duration for proxy
1699                     if (t != IMAGE) newProps.insert("proxy_out", clip->producerProperty("out"));
1700                     newProps.insert("proxy", proxyPath.path());
1701                     QMap <QString, QString> oldProps = clip->properties();
1702                     oldProps.insert("proxy", QString());
1703                     EditClipCommand *command = new EditClipCommand(this, clipId, oldProps, newProps, true);
1704                     m_doc->commandStack()->push(command);
1705                 }
1706             }
1707         }
1708
1709         if (!replace && item->data(0, Qt::DecorationRole).isNull())
1710             requestClipThumbnail(clipId);
1711         if (!toReload.isEmpty())
1712             item->slotSetToolTip();
1713
1714         if (m_listView->isEnabled())
1715             monitorItemEditing(true);
1716     } else kDebug() << "////////  COULD NOT FIND CLIP TO UPDATE PRPS...";
1717     if (selectClip && m_infoQueue.isEmpty()) {
1718         if (item && m_infoQueue.isEmpty() && m_thumbnailQueue.isEmpty()) {
1719             m_listView->setCurrentItem(item);
1720             bool updatedProfile = false;
1721             if (item->parent()) {
1722                 if (item->parent()->type() == PROJECTFOLDERTYPE)
1723                     static_cast <FolderProjectItem *>(item->parent())->switchIcon();
1724             } else if (KdenliveSettings::checkfirstprojectclip() &&  m_listView->topLevelItemCount() == 1) {
1725                 // this is the first clip loaded in project, check if we want to adjust project settings to the clip
1726                 updatedProfile = adjustProjectProfileToItem(item);
1727             }
1728             if (updatedProfile == false) emit clipSelected(item->referencedClip());
1729         } else {
1730             int max = m_doc->clipManager()->clipsCount();
1731             emit displayMessage(i18n("Loading clips"), (int)(100 *(max - m_infoQueue.count()) / max));
1732         }
1733     }
1734     if (item && m_listView->isEnabled() && replace) {
1735             // update clip in clip monitor
1736             if (item->isSelected() && m_listView->selectedItems().count() == 1)
1737                 emit clipSelected(item->referencedClip());
1738             //TODO: Make sure the line below has no side effect
1739             toReload = clipId;
1740         }
1741     if (!toReload.isEmpty())
1742         emit clipNeedsReload(toReload, true);
1743
1744     if (!m_queueRunner.isRunning() && m_processingClips.isEmpty()) m_queueRunner = QtConcurrent::run(this, &ProjectList::slotProcessNextClipInQueue);
1745 }
1746
1747 bool ProjectList::adjustProjectProfileToItem(ProjectItem *item)
1748 {
1749     if (item == NULL) {
1750         if (m_listView->currentItem() && m_listView->currentItem()->type() != PROJECTFOLDERTYPE)
1751             item = static_cast <ProjectItem*>(m_listView->currentItem());
1752     }
1753     if (item == NULL || item->referencedClip() == NULL) {
1754         KMessageBox::information(kapp->activeWindow(), i18n("Cannot find profile from current clip"));
1755         return false;
1756     }
1757     bool profileUpdated = false;
1758     QString size = item->referencedClip()->getProperty("frame_size");
1759     int width = size.section('x', 0, 0).toInt();
1760     int height = size.section('x', -1).toInt();
1761     double fps = item->referencedClip()->getProperty("fps").toDouble();
1762     double par = item->referencedClip()->getProperty("aspect_ratio").toDouble();
1763     if (item->clipType() == IMAGE || item->clipType() == AV || item->clipType() == VIDEO) {
1764         if (ProfilesDialog::matchProfile(width, height, fps, par, item->clipType() == IMAGE, m_doc->mltProfile()) == false) {
1765             // get a list of compatible profiles
1766             QMap <QString, QString> suggestedProfiles = ProfilesDialog::getProfilesFromProperties(width, height, fps, par, item->clipType() == IMAGE);
1767             if (!suggestedProfiles.isEmpty()) {
1768                 KDialog *dialog = new KDialog(this);
1769                 dialog->setCaption(i18n("Change project profile"));
1770                 dialog->setButtons(KDialog::Ok | KDialog::Cancel);
1771
1772                 QWidget container;
1773                 QVBoxLayout *l = new QVBoxLayout;
1774                 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));
1775                 l->addWidget(label);
1776                 QListWidget *list = new QListWidget;
1777                 list->setAlternatingRowColors(true);
1778                 QMapIterator<QString, QString> i(suggestedProfiles);
1779                 while (i.hasNext()) {
1780                     i.next();
1781                     QListWidgetItem *item = new QListWidgetItem(i.value(), list);
1782                     item->setData(Qt::UserRole, i.key());
1783                     item->setToolTip(i.key());
1784                 }
1785                 list->setCurrentRow(0);
1786                 l->addWidget(list);
1787                 container.setLayout(l);
1788                 dialog->setButtonText(KDialog::Ok, i18n("Update profile"));
1789                 dialog->setMainWidget(&container);
1790                 if (dialog->exec() == QDialog::Accepted) {
1791                     //Change project profile
1792                     profileUpdated = true;
1793                     if (list->currentItem())
1794                         emit updateProfile(list->currentItem()->data(Qt::UserRole).toString());
1795                 }
1796                 delete list;
1797                 delete label;
1798             } else if (fps > 0) {
1799                 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));
1800             }
1801         }
1802     }
1803     return profileUpdated;
1804 }
1805
1806 QString ProjectList::getDocumentProperty(const QString &key) const
1807 {
1808     return m_doc->getDocumentProperty(key);
1809 }
1810
1811 bool ProjectList::useProxy() const
1812 {
1813     return m_doc->getDocumentProperty("enableproxy").toInt();
1814 }
1815
1816 bool ProjectList::generateProxy() const
1817 {
1818     return m_doc->getDocumentProperty("generateproxy").toInt();
1819 }
1820
1821 bool ProjectList::generateImageProxy() const
1822 {
1823     return m_doc->getDocumentProperty("generateimageproxy").toInt();
1824 }
1825
1826 void ProjectList::slotReplyGetImage(const QString &clipId, const QPixmap &pix)
1827 {
1828     ProjectItem *item = getItemById(clipId);
1829     if (item && !pix.isNull()) {
1830         monitorItemEditing(false);
1831         item->setData(0, Qt::DecorationRole, pix);
1832         monitorItemEditing(true);
1833         m_doc->cachePixmap(item->getClipHash(), pix);
1834         if (m_listView->isEnabled())
1835             m_listView->blockSignals(false);
1836     }
1837 }
1838
1839 QTreeWidgetItem *ProjectList::getAnyItemById(const QString &id)
1840 {
1841     QTreeWidgetItemIterator it(m_listView);
1842     QString lookId = id;
1843     if (id.contains('#'))
1844         lookId = id.section('#', 0, 0);
1845
1846     ProjectItem *result = NULL;
1847     while (*it) {
1848         if ((*it)->type() != PROJECTCLIPTYPE) {
1849             // subitem
1850             ++it;
1851             continue;
1852         }
1853         ProjectItem *item = static_cast<ProjectItem *>(*it);
1854         if (item->clipId() == lookId) {
1855             result = item;
1856             break;
1857         }
1858         ++it;
1859     }
1860     if (result == NULL || !id.contains('#')) {
1861         return result;
1862     } else {
1863         for (int i = 0; i < result->childCount(); i++) {
1864             SubProjectItem *sub = static_cast <SubProjectItem *>(result->child(i));
1865             if (sub && sub->zone().x() == id.section('#', 1, 1).toInt())
1866                 return sub;
1867         }
1868     }
1869
1870     return NULL;
1871 }
1872
1873
1874 ProjectItem *ProjectList::getItemById(const QString &id)
1875 {
1876     ProjectItem *item;
1877     QTreeWidgetItemIterator it(m_listView);
1878     while (*it) {
1879         if ((*it)->type() != PROJECTCLIPTYPE) {
1880             // subitem or folder
1881             ++it;
1882             continue;
1883         }
1884         item = static_cast<ProjectItem *>(*it);
1885         if (item->clipId() == id)
1886             return item;
1887         ++it;
1888     }
1889     return NULL;
1890 }
1891
1892 FolderProjectItem *ProjectList::getFolderItemById(const QString &id)
1893 {
1894     FolderProjectItem *item;
1895     QTreeWidgetItemIterator it(m_listView);
1896     while (*it) {
1897         if ((*it)->type() == PROJECTFOLDERTYPE) {
1898             item = static_cast<FolderProjectItem *>(*it);
1899             if (item->clipId() == id)
1900                 return item;
1901         }
1902         ++it;
1903     }
1904     return NULL;
1905 }
1906
1907 void ProjectList::slotSelectClip(const QString &ix)
1908 {
1909     ProjectItem *clip = getItemById(ix);
1910     if (clip) {
1911         m_listView->setCurrentItem(clip);
1912         m_listView->scrollToItem(clip);
1913         m_editButton->defaultAction()->setEnabled(true);
1914         m_deleteButton->defaultAction()->setEnabled(true);
1915         m_reloadAction->setEnabled(true);
1916         m_transcodeAction->setEnabled(true);
1917         m_proxyAction->setEnabled(useProxy());
1918         if (clip->clipType() == IMAGE && !KdenliveSettings::defaultimageapp().isEmpty()) {
1919             m_openAction->setIcon(KIcon(KdenliveSettings::defaultimageapp()));
1920             m_openAction->setEnabled(true);
1921         } else if (clip->clipType() == AUDIO && !KdenliveSettings::defaultaudioapp().isEmpty()) {
1922             m_openAction->setIcon(KIcon(KdenliveSettings::defaultaudioapp()));
1923             m_openAction->setEnabled(true);
1924         } else {
1925             m_openAction->setEnabled(false);
1926         }
1927     }
1928 }
1929
1930 QString ProjectList::currentClipUrl() const
1931 {
1932     ProjectItem *item;
1933     if (!m_listView->currentItem() || m_listView->currentItem()->type() == PROJECTFOLDERTYPE) return QString();
1934     if (m_listView->currentItem()->type() == PROJECTSUBCLIPTYPE) {
1935         // subitem
1936         item = static_cast <ProjectItem*>(m_listView->currentItem()->parent());
1937     } else {
1938         item = static_cast <ProjectItem*>(m_listView->currentItem());
1939     }
1940     if (item == NULL)
1941         return QString();
1942     return item->clipUrl().path();
1943 }
1944
1945 KUrl::List ProjectList::getConditionalUrls(const QString &condition) const
1946 {
1947     KUrl::List result;
1948     ProjectItem *item;
1949     QList<QTreeWidgetItem *> list = m_listView->selectedItems();
1950     for (int i = 0; i < list.count(); i++) {
1951         if (list.at(i)->type() == PROJECTFOLDERTYPE)
1952             continue;
1953         if (list.at(i)->type() == PROJECTSUBCLIPTYPE) {
1954             // subitem
1955             item = static_cast <ProjectItem*>(list.at(i)->parent());
1956         } else {
1957             item = static_cast <ProjectItem*>(list.at(i));
1958         }
1959         if (item == NULL || item->type() == COLOR || item->type() == SLIDESHOW || item->type() == TEXT)
1960             continue;
1961         DocClipBase *clip = item->referencedClip();
1962         if (!condition.isEmpty()) {
1963             if (condition.startsWith("vcodec") && !clip->hasVideoCodec(condition.section('=', 1, 1)))
1964                 continue;
1965             else if (condition.startsWith("acodec") && !clip->hasAudioCodec(condition.section('=', 1, 1)))
1966                 continue;
1967         }
1968         result.append(item->clipUrl());
1969     }
1970     return result;
1971 }
1972
1973 void ProjectList::regenerateTemplate(const QString &id)
1974 {
1975     ProjectItem *clip = getItemById(id);
1976     if (clip)
1977         regenerateTemplate(clip);
1978 }
1979
1980 void ProjectList::regenerateTemplate(ProjectItem *clip)
1981 {
1982     //TODO: remove this unused method, only force_reload is necessary
1983     clip->referencedClip()->producer()->set("force_reload", 1);
1984 }
1985
1986 QDomDocument ProjectList::generateTemplateXml(QString path, const QString &replaceString)
1987 {
1988     QDomDocument doc;
1989     QFile file(path);
1990     if (!file.open(QIODevice::ReadOnly)) {
1991         kWarning() << "ERROR, CANNOT READ: " << path;
1992         return doc;
1993     }
1994     if (!doc.setContent(&file)) {
1995         kWarning() << "ERROR, CANNOT READ: " << path;
1996         file.close();
1997         return doc;
1998     }
1999     file.close();
2000     QDomNodeList texts = doc.elementsByTagName("content");
2001     for (int i = 0; i < texts.count(); i++) {
2002         QString data = texts.item(i).firstChild().nodeValue();
2003         data.replace("%s", replaceString);
2004         texts.item(i).firstChild().setNodeValue(data);
2005     }
2006     return doc;
2007 }
2008
2009
2010 void ProjectList::slotAddClipCut(const QString &id, int in, int out)
2011 {
2012     ProjectItem *clip = getItemById(id);
2013     if (clip == NULL || clip->referencedClip()->hasCutZone(QPoint(in, out)))
2014         return;
2015     AddClipCutCommand *command = new AddClipCutCommand(this, id, in, out, QString(), true, false);
2016     m_commandStack->push(command);
2017 }
2018
2019 void ProjectList::addClipCut(const QString &id, int in, int out, const QString desc, bool newItem)
2020 {
2021     ProjectItem *clip = getItemById(id);
2022     if (clip) {
2023         DocClipBase *base = clip->referencedClip();
2024         base->addCutZone(in, out);
2025         monitorItemEditing(false);
2026         SubProjectItem *sub = new SubProjectItem(clip, in, out, desc);
2027         if (newItem && desc.isEmpty() && !m_listView->isColumnHidden(1)) {
2028             if (!clip->isExpanded())
2029                 clip->setExpanded(true);
2030             m_listView->scrollToItem(sub);
2031             m_listView->editItem(sub, 1);
2032         }
2033         QPixmap p = clip->referencedClip()->extractImage(in, (int)(sub->sizeHint(0).height()  * m_render->dar()), sub->sizeHint(0).height() - 2);
2034         sub->setData(0, Qt::DecorationRole, p);
2035         m_doc->cachePixmap(clip->getClipHash() + '#' + QString::number(in), p);
2036         monitorItemEditing(true);
2037     }
2038     emit projectModified();
2039 }
2040
2041 void ProjectList::removeClipCut(const QString &id, int in, int out)
2042 {
2043     ProjectItem *clip = getItemById(id);
2044     if (clip) {
2045         DocClipBase *base = clip->referencedClip();
2046         base->removeCutZone(in, out);
2047         SubProjectItem *sub = getSubItem(clip, QPoint(in, out));
2048         if (sub) {
2049             monitorItemEditing(false);
2050             delete sub;
2051             monitorItemEditing(true);
2052         }
2053     }
2054     emit projectModified();
2055 }
2056
2057 SubProjectItem *ProjectList::getSubItem(ProjectItem *clip, QPoint zone)
2058 {
2059     SubProjectItem *sub = NULL;
2060     if (clip) {
2061         for (int i = 0; i < clip->childCount(); i++) {
2062             QTreeWidgetItem *it = clip->child(i);
2063             if (it->type() == PROJECTSUBCLIPTYPE) {
2064                 sub = static_cast <SubProjectItem*>(it);
2065                 if (sub->zone() == zone)
2066                     break;
2067                 else
2068                     sub = NULL;
2069             }
2070         }
2071     }
2072     return sub;
2073 }
2074
2075 void ProjectList::slotUpdateClipCut(QPoint p)
2076 {
2077     if (!m_listView->currentItem() || m_listView->currentItem()->type() != PROJECTSUBCLIPTYPE)
2078         return;
2079     SubProjectItem *sub = static_cast <SubProjectItem*>(m_listView->currentItem());
2080     ProjectItem *item = static_cast <ProjectItem *>(sub->parent());
2081     EditClipCutCommand *command = new EditClipCutCommand(this, item->clipId(), sub->zone(), p, sub->text(1), sub->text(1), true);
2082     m_commandStack->push(command);
2083 }
2084
2085 void ProjectList::doUpdateClipCut(const QString &id, const QPoint oldzone, const QPoint zone, const QString &comment)
2086 {
2087     ProjectItem *clip = getItemById(id);
2088     SubProjectItem *sub = getSubItem(clip, oldzone);
2089     if (sub == NULL || clip == NULL)
2090         return;
2091     DocClipBase *base = clip->referencedClip();
2092     base->updateCutZone(oldzone.x(), oldzone.y(), zone.x(), zone.y(), comment);
2093     monitorItemEditing(false);
2094     sub->setZone(zone);
2095     sub->setDescription(comment);
2096     monitorItemEditing(true);
2097     emit projectModified();
2098 }
2099
2100 void ProjectList::slotForceProcessing(const QString &id)
2101 {
2102     while (m_infoQueue.contains(id)) {
2103         slotProcessNextClipInQueue();
2104     }
2105 }
2106
2107 void ProjectList::slotAddOrUpdateSequence(const QString frameName)
2108 {
2109     QString fileName = KUrl(frameName).fileName().section('_', 0, -2);
2110     QStringList list;
2111     QString pattern = SlideshowClip::selectedPath(frameName, false, QString(), &list);
2112     int count = list.count();
2113     if (count > 1) {
2114         const QList <DocClipBase *> existing = m_doc->clipManager()->getClipByResource(pattern);
2115         if (!existing.isEmpty()) {
2116             // Sequence already exists, update
2117             QString id = existing.at(0)->getId();
2118             //ProjectItem *item = getItemById(id);
2119             QMap <QString, QString> oldprops;
2120             QMap <QString, QString> newprops;
2121             int ttl = existing.at(0)->getProperty("ttl").toInt();
2122             oldprops["out"] = existing.at(0)->getProperty("out");
2123             newprops["out"] = QString::number(ttl * count - 1);
2124             slotUpdateClipProperties(id, newprops);
2125             EditClipCommand *command = new EditClipCommand(this, id, oldprops, newprops, false);
2126             m_commandStack->push(command);
2127         } else {
2128             // Create sequence
2129             QStringList groupInfo = getGroup();
2130             m_doc->slotCreateSlideshowClipFile(fileName, pattern, count, m_timecode.reformatSeparators(KdenliveSettings::sequence_duration()),
2131                                                false, false, false,
2132                                                m_timecode.getTimecodeFromFrames(int(ceil(m_timecode.fps()))), QString(), 0,
2133                                                QString(), groupInfo.at(0), groupInfo.at(1));
2134         }
2135     } else emit displayMessage(i18n("Sequence not found"), -2);
2136 }
2137
2138 QMap <QString, QString> ProjectList::getProxies()
2139 {
2140     QMap <QString, QString> list;
2141     ProjectItem *item;
2142     QTreeWidgetItemIterator it(m_listView);
2143     while (*it) {
2144         if ((*it)->type() != PROJECTCLIPTYPE) {
2145             ++it;
2146             continue;
2147         }
2148         item = static_cast<ProjectItem *>(*it);
2149         if (item && item->referencedClip() != NULL) {
2150             if (item->hasProxy()) {
2151                 QString proxy = item->referencedClip()->getProperty("proxy");
2152                 list.insert(proxy, item->clipUrl().path());
2153             }
2154         }
2155         ++it;
2156     }
2157     return list;
2158 }
2159
2160 void ProjectList::slotCreateProxy(const QString id)
2161 {
2162     ProjectItem *item = getItemById(id);
2163     if (!item || item->isProxyRunning() || item->referencedClip()->isPlaceHolder()) return;
2164     QString path = item->referencedClip()->getProperty("proxy");
2165     if (path.isEmpty()) {
2166         setProxyStatus(path, PROXYCRASHED);
2167         return;
2168     }
2169     setProxyStatus(path, PROXYWAITING);
2170     if (m_abortProxy.contains(path)) m_abortProxy.removeAll(path);
2171     if (m_processingProxy.contains(path)) {
2172         // Proxy is already being generated
2173         return;
2174     }
2175     if (QFile::exists(path)) {
2176         // Proxy already created
2177         setProxyStatus(path, PROXYDONE);
2178         slotGotProxy(path);
2179         return;
2180     }
2181     m_processingProxy.append(path);
2182     QtConcurrent::run(this, &ProjectList::slotGenerateProxy, path, item->clipUrl().path(), item->clipType(), QString(item->referencedClip()->producerProperty("_exif_orientation")).toInt());
2183 }
2184
2185 void ProjectList::slotAbortProxy(const QString id, const QString path)
2186 {
2187     QTreeWidgetItemIterator it(m_listView);
2188     ProjectItem *item = getItemById(id);
2189     setProxyStatus(item, NOPROXY);
2190     slotGotProxy(item);
2191     if (!path.isEmpty() && m_processingProxy.contains(path)) {
2192         m_abortProxy << path;
2193         setProxyStatus(path, NOPROXY);
2194     }
2195 }
2196
2197 void ProjectList::slotGenerateProxy(const QString destPath, const QString sourcePath, int clipType, int exif_orientation)
2198 {
2199     emit projectModified();
2200     // Make sure proxy path is writable
2201     QFile file(destPath);
2202     if (!file.open(QIODevice::WriteOnly)) {
2203         setProxyStatus(destPath, PROXYCRASHED);
2204         m_processingProxy.removeAll(destPath);
2205         return;
2206     }
2207     file.close();
2208     QFile::remove(destPath);
2209     
2210     setProxyStatus(destPath, CREATINGPROXY);
2211
2212     // Special case: playlist clips (.mlt or .kdenlive project files)
2213     if (clipType == PLAYLIST) {
2214         // change FFmpeg params to MLT format
2215         QStringList parameters;
2216         parameters << sourcePath;
2217         parameters << "-consumer" << "avformat:" + destPath;
2218         QStringList params = m_doc->getDocumentProperty("proxyparams").simplified().split('-', QString::SkipEmptyParts);
2219         
2220         foreach(QString s, params) {
2221             s = s.simplified();
2222             if (s.count(' ') == 0) {
2223                 s.append("=1");
2224             }
2225             else s.replace(' ', '=');
2226             parameters << s;
2227         }
2228         
2229         parameters.append(QString("real_time=-%1").arg(KdenliveSettings::mltthreads()));
2230
2231         //TODO: currently, when rendering an xml file through melt, the display ration is lost, so we enforce it manualy
2232         double display_ratio = KdenliveDoc::getDisplayRatio(sourcePath);
2233         parameters << "aspect=" + QString::number(display_ratio);
2234
2235         //kDebug()<<"TRANSCOD: "<<parameters;
2236         QProcess myProcess;
2237         myProcess.start(KdenliveSettings::rendererpath(), parameters);
2238         myProcess.waitForStarted();
2239         int result = -1;
2240         while (myProcess.state() != QProcess::NotRunning) {
2241             // building proxy file
2242             if (m_abortProxy.contains(destPath)) {
2243                 myProcess.close();
2244                 myProcess.waitForFinished();
2245                 m_abortProxy.removeAll(destPath);
2246                 m_processingProxy.removeAll(destPath);
2247                 QFile::remove(destPath);
2248                 setProxyStatus(destPath, NOPROXY);
2249                 result = -2;
2250
2251             }
2252             myProcess.waitForFinished(500);
2253         }
2254         myProcess.waitForFinished();
2255         m_processingProxy.removeAll(destPath);
2256         if (result == -1) result = myProcess.exitStatus();
2257         if (result == 0) {
2258             // proxy successfully created
2259             setProxyStatus(destPath, PROXYDONE);
2260             slotGotProxy(destPath);
2261         }
2262         else if (result == 1) {
2263             // Proxy process crashed
2264             QFile::remove(destPath);
2265             setProxyStatus(destPath, PROXYCRASHED);
2266         }   
2267
2268     }
2269     
2270     if (clipType == IMAGE) {
2271         // Image proxy
2272         QImage i(sourcePath);
2273         if (i.isNull()) {
2274             // Cannot load image
2275             setProxyStatus(destPath, PROXYCRASHED);
2276             return;
2277         }
2278         QImage proxy;
2279         // Images are scaled to profile size. 
2280         //TODO: Make it be configurable?
2281         if (i.width() > i.height()) proxy = i.scaledToWidth(m_render->frameRenderWidth());
2282         else proxy = i.scaledToHeight(m_render->renderHeight());
2283         if (exif_orientation > 1) {
2284             // Rotate image according to exif data
2285             QImage processed;
2286             QMatrix matrix;
2287
2288             switch ( exif_orientation ) {
2289                 case 2:
2290                   matrix.scale( -1, 1 );
2291                   break;
2292                 case 3:
2293                   matrix.rotate( 180 );
2294                   break;
2295                 case 4:
2296                   matrix.scale( 1, -1 );
2297                   break;
2298                 case 5:
2299                   matrix.rotate( 270 );
2300                   matrix.scale( -1, 1 );
2301                   break;
2302                 case 6:
2303                   matrix.rotate( 90 );
2304                   break;
2305                 case 7:
2306                   matrix.rotate( 90 );
2307                   matrix.scale( -1, 1 );
2308                   break;
2309                 case 8:
2310                   matrix.rotate( 270 );
2311                   break;
2312               }
2313               processed = proxy.transformed( matrix );
2314               processed.save(destPath);
2315         }
2316         else proxy.save(destPath);
2317         setProxyStatus(destPath, PROXYDONE);
2318         slotGotProxy(destPath);
2319         m_abortProxy.removeAll(destPath);
2320         m_processingProxy.removeAll(destPath);
2321         return;
2322     }
2323
2324     QStringList parameters;
2325     parameters << "-i" << sourcePath;
2326     QString params = m_doc->getDocumentProperty("proxyparams").simplified();
2327     foreach(QString s, params.split(' '))
2328     parameters << s;
2329
2330     // Make sure we don't block when proxy file already exists
2331     parameters << "-y";
2332     parameters << destPath;
2333     kDebug()<<"// STARTING PROXY GEN: "<<parameters;
2334     QProcess myProcess;
2335     myProcess.start("ffmpeg", parameters);
2336     myProcess.waitForStarted();
2337     int result = -1;
2338     while (myProcess.state() != QProcess::NotRunning) {
2339         // building proxy file
2340         if (m_abortProxy.contains(destPath)) {
2341             myProcess.close();
2342             myProcess.waitForFinished();
2343             m_abortProxy.removeAll(destPath);
2344             m_processingProxy.removeAll(destPath);
2345             QFile::remove(destPath);
2346             setProxyStatus(destPath, NOPROXY);
2347             result = -2;
2348             
2349         }
2350         myProcess.waitForFinished(500);
2351     }
2352     myProcess.waitForFinished();
2353     if (result == -1) result = myProcess.exitStatus();
2354     if (result == 0) {
2355         // proxy successfully created
2356         setProxyStatus(destPath, PROXYDONE);
2357         slotGotProxy(destPath);
2358     }
2359     else if (result == 1) {
2360         // Proxy process crashed
2361         QFile::remove(destPath);
2362         setProxyStatus(destPath, PROXYCRASHED);
2363     }
2364     m_abortProxy.removeAll(destPath);
2365     m_processingProxy.removeAll(destPath);
2366 }
2367
2368 void ProjectList::updateProxyConfig()
2369 {
2370     ProjectItem *item;
2371     QTreeWidgetItemIterator it(m_listView);
2372     QUndoCommand *command = new QUndoCommand();
2373     command->setText(i18n("Update proxy settings"));
2374     QString proxydir = m_doc->projectFolder().path( KUrl::AddTrailingSlash) + "proxy/";
2375     while (*it) {
2376         if ((*it)->type() != PROJECTCLIPTYPE) {
2377             ++it;
2378             continue;
2379         }
2380         item = static_cast<ProjectItem *>(*it);
2381         if (item == NULL) {
2382             ++it;
2383             continue;
2384         }
2385         CLIPTYPE t = item->clipType();
2386         if ((t == VIDEO || t == AV || t == UNKNOWN) && item->referencedClip() != NULL) {
2387             if  (generateProxy() && useProxy() && !item->isProxyRunning()) {
2388                 DocClipBase *clip = item->referencedClip();
2389                 if (clip->getProperty("frame_size").section('x', 0, 0).toInt() > m_doc->getDocumentProperty("proxyminsize").toInt()) {
2390                     if (clip->getProperty("proxy").isEmpty()) {
2391                         // We need to insert empty proxy in old properties so that undo will work
2392                         QMap <QString, QString> oldProps;// = clip->properties();
2393                         oldProps.insert("proxy", QString());
2394                         QMap <QString, QString> newProps;
2395                         newProps.insert("proxy", proxydir + item->referencedClip()->getClipHash() + "." + m_doc->getDocumentProperty("proxyextension"));
2396                         new EditClipCommand(this, clip->getId(), oldProps, newProps, true, command);
2397                     }
2398                 }
2399             }
2400             else if (item->hasProxy()) {
2401                 // remove proxy
2402                 QMap <QString, QString> newProps;
2403                 newProps.insert("proxy", QString());
2404                 newProps.insert("replace", "1");
2405                 // insert required duration for proxy
2406                 newProps.insert("proxy_out", item->referencedClip()->producerProperty("out"));
2407                 new EditClipCommand(this, item->clipId(), item->referencedClip()->properties(), newProps, true, command);
2408             }
2409         }
2410         else if (t == IMAGE && item->referencedClip() != NULL) {
2411             if  (generateImageProxy() && useProxy()) {
2412                 DocClipBase *clip = item->referencedClip();
2413                 int maxImageSize = m_doc->getDocumentProperty("proxyimageminsize").toInt();
2414                 if (clip->getProperty("frame_size").section('x', 0, 0).toInt() > maxImageSize || clip->getProperty("frame_size").section('x', 1, 1).toInt() > maxImageSize) {
2415                     if (clip->getProperty("proxy").isEmpty()) {
2416                         // We need to insert empty proxy in old properties so that undo will work
2417                         QMap <QString, QString> oldProps = clip->properties();
2418                         oldProps.insert("proxy", QString());
2419                         QMap <QString, QString> newProps;
2420                         newProps.insert("proxy", proxydir + item->referencedClip()->getClipHash() + ".png");
2421                         new EditClipCommand(this, clip->getId(), oldProps, newProps, true, command);
2422                     }
2423                 }
2424             }
2425             else if (item->hasProxy()) {
2426                 // remove proxy
2427                 QMap <QString, QString> newProps;
2428                 newProps.insert("proxy", QString());
2429                 newProps.insert("replace", "1");
2430                 new EditClipCommand(this, item->clipId(), item->referencedClip()->properties(), newProps, true, command);
2431             }
2432         }
2433         ++it;
2434     }
2435     if (command->childCount() > 0) m_doc->commandStack()->push(command);
2436     else delete command;
2437     if (!m_infoQueue.isEmpty() && !m_queueRunner.isRunning() && m_processingClips.isEmpty()) m_queueRunner = QtConcurrent::run(this, &ProjectList::slotProcessNextClipInQueue);
2438 }
2439
2440 void ProjectList::slotProxyCurrentItem(bool doProxy)
2441 {
2442     QList<QTreeWidgetItem *> list = m_listView->selectedItems();
2443     QTreeWidgetItem *listItem;
2444     QUndoCommand *command = new QUndoCommand();
2445     if (doProxy) command->setText(i18np("Add proxy clip", "Add proxy clips", list.count()));
2446     else command->setText(i18np("Remove proxy clip", "Remove proxy clips", list.count()));
2447     
2448     // Make sure the proxy folder exists
2449     QString proxydir = m_doc->projectFolder().path( KUrl::AddTrailingSlash) + "proxy/";
2450     KStandardDirs::makeDir(proxydir);
2451                 
2452     QMap <QString, QString> newProps;
2453     QMap <QString, QString> oldProps;
2454     if (!doProxy) newProps.insert("proxy", "-");
2455     for (int i = 0; i < list.count(); i++) {
2456         listItem = list.at(i);
2457         if (listItem->type() == PROJECTFOLDERTYPE) {
2458             for (int j = 0; j < listItem->childCount(); j++) {
2459                 QTreeWidgetItem *sub = listItem->child(j);
2460                 if (!list.contains(sub)) list.append(sub);
2461             }
2462         }
2463         if (listItem->type() == PROJECTCLIPTYPE) {
2464             ProjectItem *item = static_cast <ProjectItem*>(listItem);
2465             CLIPTYPE t = item->clipType();
2466             if ((t == VIDEO || t == AV || t == UNKNOWN || t == IMAGE || t == PLAYLIST) && item->referencedClip()) {
2467                 oldProps = item->referencedClip()->properties();
2468                 if (doProxy) {
2469                     newProps.clear();
2470                     QString path = proxydir + item->referencedClip()->getClipHash() + "." + (t == IMAGE ? "png" : m_doc->getDocumentProperty("proxyextension"));
2471                     // insert required duration for proxy
2472                     newProps.insert("proxy_out", item->referencedClip()->producerProperty("out"));
2473                     newProps.insert("proxy", path);
2474                     // We need to insert empty proxy so that undo will work
2475                     oldProps.insert("proxy", QString());
2476                 }
2477                 new EditClipCommand(this, item->clipId(), oldProps, newProps, true, command);
2478             }
2479         }
2480     }
2481     if (command->childCount() > 0) {
2482         m_doc->commandStack()->push(command);
2483     }
2484     else delete command;
2485     //if (!m_infoQueue.isEmpty() && !m_queueRunner.isRunning() && m_processingClips.isEmpty()) m_queueRunner = QtConcurrent::run(this, &ProjectList::slotProcessNextClipInQueue);
2486 }
2487
2488
2489 void ProjectList::slotDeleteProxy(const QString proxyPath)
2490 {
2491     if (proxyPath.isEmpty()) return;
2492     QUndoCommand *proxyCommand = new QUndoCommand();
2493     proxyCommand->setText(i18n("Remove Proxy"));
2494     QTreeWidgetItemIterator it(m_listView);
2495     ProjectItem *item;
2496     while (*it) {
2497         if ((*it)->type() == PROJECTCLIPTYPE) {
2498             item = static_cast <ProjectItem *>(*it);
2499             if (item->referencedClip()->getProperty("proxy") == proxyPath) {
2500                 QMap <QString, QString> props;
2501                 props.insert("proxy", QString());
2502                 new EditClipCommand(this, item->clipId(), item->referencedClip()->properties(), props, true, proxyCommand);
2503             
2504             }
2505         }
2506         ++it;
2507     }
2508     if (proxyCommand->childCount() == 0)
2509         delete proxyCommand;
2510     else
2511         m_commandStack->push(proxyCommand);
2512     QFile::remove(proxyPath);
2513 }
2514
2515 void ProjectList::setProxyStatus(const QString proxyPath, PROXYSTATUS status)
2516 {
2517     if (proxyPath.isEmpty()) return;
2518     QTreeWidgetItemIterator it(m_listView);
2519     ProjectItem *item;
2520     while (*it) {
2521         if ((*it)->type() == PROJECTCLIPTYPE) {
2522             item = static_cast <ProjectItem *>(*it);
2523             if (item->referencedClip()->getProperty("proxy") == proxyPath) {
2524                 setProxyStatus(item, status);
2525             }
2526         }
2527         ++it;
2528     }
2529 }
2530
2531 void ProjectList::setProxyStatus(ProjectItem *item, PROXYSTATUS status)
2532 {
2533     if (item == NULL) return;
2534     monitorItemEditing(false);
2535     item->setProxyStatus(status);
2536     monitorItemEditing(true);
2537 }
2538
2539 void ProjectList::monitorItemEditing(bool enable)
2540 {
2541     if (enable) connect(m_listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotItemEdited(QTreeWidgetItem *, int)));     
2542     else disconnect(m_listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotItemEdited(QTreeWidgetItem *, int)));     
2543 }
2544
2545 QStringList ProjectList::expandedFolders() const
2546 {
2547     QStringList result;
2548     FolderProjectItem *item;
2549     QTreeWidgetItemIterator it(m_listView);
2550     while (*it) {
2551         if ((*it)->type() != PROJECTFOLDERTYPE) {
2552             ++it;
2553             continue;
2554         }
2555         if ((*it)->isExpanded()) {
2556             item = static_cast<FolderProjectItem *>(*it);
2557             result.append(item->clipId());
2558         }
2559         ++it;
2560     }
2561     return result;
2562 }
2563
2564 #include "projectlist.moc"