]> git.sesse.net Git - kdenlive/blob - src/kdenlivedoc.cpp
When replacing a missing clip in document, set state to modified
[kdenlive] / src / kdenlivedoc.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 <QCryptographicHash>
21 #include <QFile>
22
23 #include <KDebug>
24 #include <KStandardDirs>
25 #include <KMessageBox>
26 #include <KLocale>
27 #include <KFileDialog>
28 #include <KIO/NetAccess>
29 #include <KApplication>
30
31
32 #include <mlt++/Mlt.h>
33
34 #include "kdenlivedoc.h"
35 #include "docclipbase.h"
36 #include "profilesdialog.h"
37 #include "kdenlivesettings.h"
38 #include "renderer.h"
39 #include "clipmanager.h"
40 #include "addfoldercommand.h"
41 #include "editfoldercommand.h"
42 #include "titlewidget.h"
43 #include "mainwindow.h"
44
45
46 KdenliveDoc::KdenliveDoc(const KUrl &url, const KUrl &projectFolder, QUndoGroup *undoGroup, const QString &profileName, const QPoint tracks, MainWindow *parent): QObject(parent), m_render(NULL), m_url(url), m_projectFolder(projectFolder), m_commandStack(new QUndoStack(undoGroup)), m_modified(false), m_documentLoadingProgress(0), m_documentLoadingStep(0.0), m_startPos(0), m_zoom(7), m_autosave(NULL) {
47     m_clipManager = new ClipManager(this);
48     m_autoSaveTimer = new QTimer(this);
49     m_autoSaveTimer->setSingleShot(true);
50     if (!url.isEmpty()) {
51         QString tmpFile;
52         if (KIO::NetAccess::download(url.path(), tmpFile, parent)) {
53             QFile file(tmpFile);
54             m_document.setContent(&file, false);
55             file.close();
56             QDomNode infoXmlNode = m_document.elementsByTagName("kdenlivedoc").at(0);
57             QDomNode westley = m_document.elementsByTagName("westley").at(0);
58             if (!infoXmlNode.isNull()) {
59                 QDomElement infoXml = infoXmlNode.toElement();
60                 QString profilePath = infoXml.attribute("profile");
61                 m_startPos = infoXml.attribute("position").toInt();
62                 m_zoom = infoXml.attribute("zoom", "7").toInt();
63                 setProfilePath(profilePath);
64                 double version = infoXml.attribute("version").toDouble();
65                 if (version < 0.8) convertDocument(version);
66                 else {
67                     //delete all mlt producers and instead, use Kdenlive saved producers
68                     /*QDomNodeList prods = m_document.elementsByTagName("producer");
69                     int maxprod = prods.count();
70                     int pos = 0;
71                     for (int i = 0; i < maxprod; i++) {
72                         QDomNode m = prods.at(pos);
73                         QString prodId = m.toElement().attribute("id");
74                         if (prodId == "black" || prodId.startsWith("slowmotion"))
75                             pos++;
76                         else westley.removeChild(m);
77                     }*/
78                     /*prods = m_document.elementsByTagName("kdenlive_producer");
79                     maxprod = prods.count();
80                     for (int i = 0; i < maxprod; i++) {
81                         prods.at(0).toElement().setTagName("producer");
82                         westley.insertBefore(prods.at(0), QDomNode());
83                     }*/
84                 }
85                 QDomElement e;
86                 QDomNodeList producers = m_document.elementsByTagName("producer");
87                 QDomNodeList infoproducers = m_document.elementsByTagName("kdenlive_producer");
88                 const int max = producers.count();
89                 const int infomax = infoproducers.count();
90
91                 if (max > 0) {
92                     m_documentLoadingStep = 100.0 / (max + infomax + m_document.elementsByTagName("entry").count());
93                     parent->slotGotProgressInfo(i18n("Loading project clips"), (int) m_documentLoadingProgress);
94                 }
95
96                 for (int i = 0; i < max; i++) {
97                     e = producers.item(i).cloneNode().toElement();
98                     if (m_documentLoadingStep > 0) {
99                         m_documentLoadingProgress += m_documentLoadingStep;
100                         parent->slotGotProgressInfo(QString(), (int) m_documentLoadingProgress);
101                         //qApp->processEvents();
102                     }
103                     QString prodId = e.attribute("id");
104                     if (!e.isNull() && prodId != "black" && !prodId.startsWith("slowmotion")/*&& prodId.toInt() > 0*/) {
105                         // addClip(e, prodId, false);
106                         kDebug() << "// PROD: " << prodId;
107                     }
108                 }
109
110                 for (int i = 0; i < infomax; i++) {
111                     e = infoproducers.item(i).cloneNode().toElement();
112                     if (m_documentLoadingStep > 0) {
113                         m_documentLoadingProgress += m_documentLoadingStep;
114                         parent->slotGotProgressInfo(QString(), (int) m_documentLoadingProgress);
115                         //qApp->processEvents();
116                     }
117                     QString prodId = e.attribute("id");
118                     if (!e.isNull() && prodId != "black" && !prodId.startsWith("slowmotion")) {
119                         e.setTagName("producer");
120                         addClipInfo(e, prodId);
121                         kDebug() << "// NLIVE PROD: " << prodId;
122                     }
123                 }
124
125                 QDomNode markers = m_document.elementsByTagName("markers").at(0);
126                 if (!markers.isNull()) {
127                     QDomNodeList markerslist = markers.childNodes();
128                     int maxchild = markerslist.count();
129                     for (int k = 0; k < maxchild; k++) {
130                         e = markerslist.at(k).toElement();
131                         if (e.tagName() == "marker") {
132                             m_clipManager->getClipById(e.attribute("id"))->addSnapMarker(GenTime(e.attribute("time").toDouble()), e.attribute("comment"));
133                         }
134                     }
135                     westley.removeChild(markers);
136                 }
137                 m_document.removeChild(infoXmlNode);
138
139                 kDebug() << "Reading file: " << url.path() << ", found clips: " << producers.count();
140             } else {
141                 parent->slotGotProgressInfo(i18n("File %1 is not a Kdenlive project file."), 100);
142                 kWarning() << "  NO KDENLIVE INFO FOUND IN FILE: " << url.path();
143                 m_document = createEmptyDocument(tracks.x(), tracks.y());
144                 setProfilePath(profileName);
145             }
146             KIO::NetAccess::removeTempFile(tmpFile);
147         } else {
148             KMessageBox::error(parent, KIO::NetAccess::lastErrorString());
149             parent->slotGotProgressInfo(i18n("File %1 is not a Kdenlive project file."), 100);
150             m_document = createEmptyDocument(tracks.x(), tracks.y());
151             setProfilePath(profileName);
152         }
153     } else {
154         m_document = createEmptyDocument(tracks.x(), tracks.y());
155         setProfilePath(profileName);
156     }
157     m_scenelist = m_document.toString();
158     kDebug() << "KDEnnlive document, init timecode: " << m_fps;
159     if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
160     else m_timecode.setFormat((int) m_fps);
161
162
163     connect(m_autoSaveTimer, SIGNAL(timeout()), this, SLOT(slotAutoSave()));
164 }
165
166 KdenliveDoc::~KdenliveDoc() {
167     delete m_commandStack;
168     delete m_clipManager;
169     delete m_autoSaveTimer;
170     if (m_autosave) {
171         m_autosave->remove();
172         delete m_autosave;
173     }
174 }
175
176 QDomDocument KdenliveDoc::createEmptyDocument(const int videotracks, const int audiotracks) {
177     // Creating new document
178     QDomDocument doc;
179     QDomElement westley = doc.createElement("westley");
180     doc.appendChild(westley);
181
182     QDomElement tractor = doc.createElement("tractor");
183     tractor.setAttribute("id", "maintractor");
184     QDomElement multitrack = doc.createElement("multitrack");
185     QDomElement playlist = doc.createElement("playlist");
186     playlist.setAttribute("id", "black_track");
187     westley.appendChild(playlist);
188
189
190     // create playlists
191     int total = audiotracks + videotracks + 1;
192
193     for (int i = 1; i < total; i++) {
194         QDomElement playlist = doc.createElement("playlist");
195         playlist.setAttribute("id", "playlist" + QString::number(i));
196         westley.appendChild(playlist);
197     }
198
199     QDomElement track0 = doc.createElement("track");
200     track0.setAttribute("producer", "black_track");
201     tractor.appendChild(track0);
202
203     // create audio tracks
204     for (int i = 1; i < audiotracks + 1; i++) {
205         QDomElement track = doc.createElement("track");
206         track.setAttribute("producer", "playlist" + QString::number(i));
207         track.setAttribute("hide", "video");
208         tractor.appendChild(track);
209     }
210
211     // create video tracks
212     for (int i = audiotracks + 1; i < total; i++) {
213         QDomElement track = doc.createElement("track");
214         track.setAttribute("producer", "playlist" + QString::number(i));
215         tractor.appendChild(track);
216     }
217
218     for (uint i = 2; i < total ; i++) {
219         QDomElement transition = doc.createElement("transition");
220         transition.setAttribute("always_active", "1");
221
222         QDomElement property = doc.createElement("property");
223         property.setAttribute("name", "a_track");
224         QDomText value = doc.createTextNode(QString::number(1));
225         property.appendChild(value);
226         transition.appendChild(property);
227
228         property = doc.createElement("property");
229         property.setAttribute("name", "b_track");
230         value = doc.createTextNode(QString::number(i));
231         property.appendChild(value);
232         transition.appendChild(property);
233
234         property = doc.createElement("property");
235         property.setAttribute("name", "mlt_service");
236         value = doc.createTextNode("mix");
237         property.appendChild(value);
238         transition.appendChild(property);
239
240         property = doc.createElement("property");
241         property.setAttribute("name", "combine");
242         value = doc.createTextNode("1");
243         property.appendChild(value);
244         transition.appendChild(property);
245
246         property = doc.createElement("property");
247         property.setAttribute("name", "internal_added");
248         value = doc.createTextNode("237");
249         property.appendChild(value);
250         transition.appendChild(property);
251         tractor.appendChild(transition);
252     }
253     westley.appendChild(tractor);
254     return doc;
255 }
256
257
258 void KdenliveDoc::syncGuides(QList <Guide *> guides) {
259     QDomDocument doc;
260     QDomElement e;
261     m_guidesXml.clear();
262     m_guidesXml = doc.createElement("guides");
263
264     for (int i = 0; i < guides.count(); i++) {
265         e = doc.createElement("guide");
266         e.setAttribute("time", guides.at(i)->position().ms() / 1000);
267         e.setAttribute("comment", guides.at(i)->label());
268         m_guidesXml.appendChild(e);
269     }
270     emit guidesUpdated();
271 }
272
273 QDomElement KdenliveDoc::guidesXml() const {
274     return m_guidesXml;
275 }
276
277 void KdenliveDoc::slotAutoSave() {
278     if (m_render && m_autosave) {
279         if (!m_autosave->isOpen() && !m_autosave->open(QIODevice::ReadWrite)) {
280             // show error: could not open the autosave file
281             kDebug() << "ERROR; CANNOT CREATE AUTOSAVE FILE";
282         }
283         kDebug() << "// AUTOSAVE FILE: " << m_autosave->fileName();
284         QDomDocument doc;
285         doc.setContent(m_render->sceneList());
286         saveSceneList(m_autosave->fileName(), doc);
287     }
288 }
289
290 void KdenliveDoc::setZoom(int factor) {
291     m_zoom = factor;
292 }
293
294 int KdenliveDoc::zoom() const {
295     return m_zoom;
296 }
297
298 void KdenliveDoc::convertDocument(double version) {
299     kDebug() << "Opening a document with version " << version;
300     // Opening a old Kdenlive document
301     if (version == 0.7) {
302         kDebug() << "Unable to open document with version " << version;
303         // TODO: convert 0.7 files to the new document format.
304         return;
305     }
306     QDomNode westley = m_document.elementsByTagName("westley").at(1);
307     QDomNode tractor = m_document.elementsByTagName("tractor").at(0);
308     QDomNode kdenlivedoc = m_document.elementsByTagName("kdenlivedoc").at(0);
309     QDomElement kdenlivedoc_old = kdenlivedoc.cloneNode(true).toElement(); // Needed for folders
310     QDomNode multitrack = m_document.elementsByTagName("multitrack").at(0);
311     QDomNodeList playlists = m_document.elementsByTagName("playlist");
312
313     m_startPos = kdenlivedoc.toElement().attribute("timeline_position").toInt();
314
315     QDomNode props = m_document.elementsByTagName("properties").at(0).toElement();
316     QString profile = props.toElement().attribute("videoprofile");
317     if (profile == "dv_wide") profile = "dv_pal_wide";
318     setProfilePath(profile);
319
320     // move playlists outside of tractor and add the tracks instead
321     int max = playlists.count();
322     for (int i = 0; i < max; i++) {
323         QDomNode n = playlists.at(i);
324         westley.insertBefore(n, QDomNode());
325         QDomElement pl = n.toElement();
326         QDomElement track = m_document.createElement("track");
327         QString trackType = pl.attribute("hide");
328         if (!trackType.isEmpty()) track.setAttribute("hide", trackType);
329         QString playlist_id =  pl.attribute("id");
330         if (playlist_id.isEmpty()) {
331             playlist_id = "black_track";
332             pl.setAttribute("id", playlist_id);
333         }
334         track.setAttribute("producer", playlist_id);
335         //tractor.appendChild(track);
336 #define KEEP_TRACK_ORDER 1
337 #ifdef KEEP_TRACK_ORDER
338         tractor.insertAfter(track, QDomNode());
339 #else
340         // Insert the new track in an order that hopefully matches the 3 video, then 2 audio tracks of Kdenlive 0.7.0
341         // insertion sort - O( tracks*tracks )
342         // Note, this breaks _all_ transitions - but you can move them up and down afterwards.
343         QDomElement tractor_elem = tractor.toElement();
344         if (! tractor_elem.isNull()) {
345             QDomNodeList tracks = tractor_elem.elementsByTagName("track");
346             int size = tracks.size();
347             if (size == 0) {
348                 tractor.insertAfter(track, QDomNode());
349             } else {
350                 bool inserted = false;
351                 for (int i = 0; i < size; ++i) {
352                     QDomElement track_elem = tracks.at(i).toElement();
353                     if (track_elem.isNull()) {
354                         tractor.insertAfter(track, QDomNode());
355                         inserted = true;
356                         break;
357                     } else {
358                         kDebug() << "playlist_id: " << playlist_id << " producer:" << track_elem.attribute("producer");
359                         if (playlist_id < track_elem.attribute("producer")) {
360                             tractor.insertBefore(track, track_elem);
361                             inserted = true;
362                             break;
363                         }
364                     }
365                 }
366                 // Reach here, no insertion, insert last
367                 if (!inserted) {
368                     tractor.insertAfter(track, QDomNode());
369                 }
370             }
371         } else {
372             kWarning() << "tractor was not a QDomElement";
373             tractor.insertAfter(track, QDomNode());
374         }
375 #endif
376     }
377     tractor.removeChild(multitrack);
378
379     // audio track mixing transitions should not be added to track view, so add required attribute
380     QDomNodeList transitions = m_document.elementsByTagName("transition");
381     max = transitions.count();
382     for (int i = 0; i < max; i++) {
383         QDomElement tr = transitions.at(i).toElement();
384         if (tr.attribute("combine") == "1" && tr.attribute("mlt_service") == "mix") {
385             QDomElement property = m_document.createElement("property");
386             property.setAttribute("name", "internal_added");
387             QDomText value = m_document.createTextNode("237");
388             property.appendChild(value);
389             tr.appendChild(property);
390         } else {
391             // convert transition
392             QDomNamedNodeMap attrs = tr.attributes();
393             for (unsigned int j = 0; j < attrs.count(); j++) {
394                 QString attrName = attrs.item(j).nodeName();
395                 if (attrName != "in" && attrName != "out" && attrName != "id") {
396                     QDomElement property = m_document.createElement("property");
397                     property.setAttribute("name", attrName);
398                     QDomText value = m_document.createTextNode(attrs.item(j).nodeValue());
399                     property.appendChild(value);
400                     tr.appendChild(property);
401                 }
402             }
403         }
404     }
405
406     // move transitions after tracks
407     for (int i = 0; i < max; i++) {
408         tractor.insertAfter(transitions.at(0), QDomNode());
409     }
410
411     // Fix filters format
412     QDomNodeList entries = m_document.elementsByTagName("entry");
413     max = entries.count();
414     for (int i = 0; i < max; i++) {
415         QString last_id;
416         int effectix = 0;
417         QDomNode m = entries.at(i).firstChild();
418         while (!m.isNull()) {
419             if (m.toElement().tagName() == "filter") {
420                 QDomElement filt = m.toElement();
421                 QDomNamedNodeMap attrs = filt.attributes();
422                 QString current_id = filt.attribute("kdenlive_id");
423                 if (current_id != last_id) {
424                     effectix++;
425                     last_id = current_id;
426                 }
427                 QDomElement e = m_document.createElement("property");
428                 e.setAttribute("name", "kdenlive_ix");
429                 QDomText value = m_document.createTextNode(QString::number(effectix));
430                 e.appendChild(value);
431                 filt.appendChild(e);
432                 for (int j = 0; j < attrs.count(); j++) {
433                     QDomAttr a = attrs.item(j).toAttr();
434                     if (!a.isNull()) {
435                         kDebug() << " FILTER; adding :" << a.name() << ":" << a.value();
436                         QDomElement e = m_document.createElement("property");
437                         e.setAttribute("name", a.name());
438                         QDomText value = m_document.createTextNode(a.value());
439                         e.appendChild(value);
440                         filt.appendChild(e);
441
442                     }
443                 }
444             }
445             m = m.nextSibling();
446         }
447     }
448
449     /*
450         QDomNodeList filters = m_document.elementsByTagName("filter");
451         max = filters.count();
452         QString last_id;
453         int effectix = 0;
454         for (int i = 0; i < max; i++) {
455             QDomElement filt = filters.at(i).toElement();
456             QDomNamedNodeMap attrs = filt.attributes();
457      QString current_id = filt.attribute("kdenlive_id");
458      if (current_id != last_id) {
459          effectix++;
460          last_id = current_id;
461      }
462      QDomElement e = m_document.createElement("property");
463             e.setAttribute("name", "kdenlive_ix");
464             QDomText value = m_document.createTextNode(QString::number(1));
465             e.appendChild(value);
466             filt.appendChild(e);
467             for (int j = 0; j < attrs.count(); j++) {
468                 QDomAttr a = attrs.item(j).toAttr();
469                 if (!a.isNull()) {
470                     kDebug() << " FILTER; adding :" << a.name() << ":" << a.value();
471                     QDomElement e = m_document.createElement("property");
472                     e.setAttribute("name", a.name());
473                     QDomText value = m_document.createTextNode(a.value());
474                     e.appendChild(value);
475                     filt.appendChild(e);
476                 }
477             }
478         }*/
479
480     // fix slowmotion
481     QDomNodeList producers = westley.toElement().elementsByTagName("producer");
482     max = producers.count();
483     for (int i = 0; i < max; i++) {
484         QDomElement prod = producers.at(i).toElement();
485         if (prod.attribute("mlt_service") == "framebuffer") {
486             QString slowmotionprod = prod.attribute("resource");
487             slowmotionprod.replace(':', '?');
488             kDebug() << "// FOUND WRONG SLOWMO, new: " << slowmotionprod;
489             prod.setAttribute("resource", slowmotionprod);
490         }
491     }
492     // move producers to correct place, markers to a global list, fix clip descriptions
493     QDomElement markers = m_document.createElement("markers");
494     // This will get the westley producers:
495     producers = m_document.elementsByTagName("producer");
496     max = producers.count();
497     for (int i = 0; i < max; i++) {
498         QDomElement prod = producers.at(0).toElement();
499         QDomNode m = prod.firstChild();
500         if (!m.isNull()) {
501             if (m.toElement().tagName() == "markers") {
502                 QDomNodeList prodchilds = m.childNodes();
503                 int maxchild = prodchilds.count();
504                 for (int k = 0; k < maxchild; k++) {
505                     QDomElement mark = prodchilds.at(0).toElement();
506                     mark.setAttribute("id", prod.attribute("id"));
507                     markers.insertAfter(mark, QDomNode());
508                 }
509                 prod.removeChild(m);
510             } else if (prod.attribute("type").toInt() == TEXT) {
511                 // convert title clip
512                 if (m.toElement().tagName() == "textclip") {
513                     QDomDocument tdoc;
514                     QDomElement titleclip = m.toElement();
515                     QDomElement title = tdoc.createElement("kdenlivetitle");
516                     tdoc.appendChild(title);
517                     QDomNodeList objects = titleclip.childNodes();
518                     int maxchild = objects.count();
519                     for (int k = 0; k < maxchild; k++) {
520                         QString objectxml;
521                         QDomElement ob = objects.at(k).toElement();
522                         if (ob.attribute("type") == "3") {
523                             // text object - all of this goes into "xmldata"...
524                             QDomElement item = tdoc.createElement("item");
525                             item.setAttribute("z-index", ob.attribute("z"));
526                             item.setAttribute("type", "QGraphicsTextItem");
527                             QDomElement position = tdoc.createElement("position");
528                             position.setAttribute("x", ob.attribute("x"));
529                             position.setAttribute("y", ob.attribute("y"));
530                             QDomElement content = tdoc.createElement("content");
531                             content.setAttribute("font", ob.attribute("font_family"));
532                             content.setAttribute("font-size", ob.attribute("font_size"));
533                             content.setAttribute("font-bold", ob.attribute("bold"));
534                             content.setAttribute("font-italic", ob.attribute("italic"));
535                             content.setAttribute("font-underline", ob.attribute("underline"));
536                             QString col = ob.attribute("color");
537                             QColor c(col);
538                             content.setAttribute("font-color", colorToString(c));
539                             // todo: These fields are missing from the newly generated xmldata:
540                             // transform, startviewport, endviewport, background
541
542                             QDomText conttxt = tdoc.createTextNode(ob.attribute("text"));
543                             content.appendChild(conttxt);
544                             item.appendChild(position);
545                             item.appendChild(content);
546                             title.appendChild(item);
547                         } else if (ob.attribute("type") == "5") {
548                             // rectangle object
549                             QDomElement item = tdoc.createElement("item");
550                             item.setAttribute("z-index", ob.attribute("z"));
551                             item.setAttribute("type", "QGraphicsRectItem");
552                             QDomElement position = tdoc.createElement("position");
553                             position.setAttribute("x", ob.attribute("x"));
554                             position.setAttribute("y", ob.attribute("y"));
555                             QDomElement content = tdoc.createElement("content");
556                             QString col = ob.attribute("color");
557                             QColor c(col);
558                             content.setAttribute("brushcolor", colorToString(c));
559                             QString rect = "0,0,";
560                             rect.append(ob.attribute("width"));
561                             rect.append(",");
562                             rect.append(ob.attribute("height"));
563                             content.setAttribute("rect", rect);
564                             item.appendChild(position);
565                             item.appendChild(content);
566                             title.appendChild(item);
567                         }
568                     }
569                     prod.setAttribute("xmldata", tdoc.toString());
570                     // mbd todo: This clearly does not work, as every title gets the same name - trying to leave it empty
571                     // QStringList titleInfo = TitleWidget::getFreeTitleInfo(projectFolder());
572                     // prod.setAttribute("titlename", titleInfo.at(0));
573                     // prod.setAttribute("resource", titleInfo.at(1));
574                     //kDebug()<<"TITLE DATA:\n"<<tdoc.toString();
575                     prod.removeChild(m);
576                 } // End conversion of title clips.
577
578             } else if (m.isText()) {
579                 QString comment = m.nodeValue();
580                 if (!comment.isEmpty()) {
581                     prod.setAttribute("description", comment);
582                 }
583                 prod.removeChild(m);
584             }
585         }
586         int duration = prod.attribute("duration").toInt();
587         if (duration > 0) prod.setAttribute("out", QString::number(duration));
588         // The clip goes back in, but text clips should not go back in, at least not modified
589         westley.insertBefore(prod, QDomNode());
590
591     }
592
593     QDomNode westley0 = m_document.elementsByTagName("westley").at(0);
594     if (!markers.firstChild().isNull()) westley0.appendChild(markers);
595
596
597     // Convert as much of the kdenlivedoc as possible. Use the producer in westley
598     // First, remove the old stuff from westley, and add a new empty one
599     // Also, track the max id in order to use it for the adding of groups/folders
600     int max_kproducer_id = 0;
601     westley0.removeChild(kdenlivedoc);
602     QDomElement kdenlivedoc_new = m_document.createElement("kdenlivedoc");
603     kdenlivedoc_new.setAttribute("profile", profile);
604     // Add all the producers that has a ressource in westley
605     QDomElement westley_element = westley0.toElement();
606     if (westley_element.isNull()) {
607         kWarning() << "westley0 element in document was not a QDomElement - unable to add producers to new kdenlivedoc";
608     } else {
609         QDomNodeList wproducers = westley_element.elementsByTagName("producer");
610         int kmax = wproducers.count();
611         for (int i = 0; i < kmax; i++) {
612             QDomElement wproducer = wproducers.at(i).toElement();
613             if (wproducer.isNull()) {
614                 kWarning() << "Found producer in westley0, that was not a QDomElement";
615             } else {
616                 // We have to do slightly different things, depending on the type
617                 kDebug() << "Converting producer element with type " << wproducer.attribute("type");
618                 if (wproducer.attribute("type").toInt() == TEXT) {
619                     kDebug() << "Found TEXT element in producer" << endl;
620                     QDomElement kproducer = wproducer.cloneNode(true).toElement();
621                     kproducer.setTagName("kdenlive_producer");
622                     kdenlivedoc_new.appendChild(kproducer);
623                     // todo: Perhaps needs some more changes here to "frequency", aspect ratio as a float, frame_size, channels, and later, ressource and title name
624                 } else {
625                     QDomElement kproducer = m_document.createElement("kdenlive_producer");
626                     kproducer.setAttribute("id", wproducer.attribute("id"));
627                     kproducer.setAttribute("description", wproducer.attribute("description"));
628                     kproducer.setAttribute("resource", wproducer.attribute("resource"));
629                     kproducer.setAttribute("type", wproducer.attribute("type"));
630                     kdenlivedoc_new.appendChild(kproducer);
631                 }
632                 if (wproducer.attribute("id").toInt() > max_kproducer_id) {
633                     max_kproducer_id = wproducer.attribute("id").toInt();
634                 }
635             }
636         }
637     }
638 #define LOOKUP_FOLDER 1
639 #ifdef LOOKUP_FOLDER
640     // Look through all the folder elements of the old doc, for each folder, for each producer,
641     // get the id, look it up in the new doc, set the groupname and groupid
642     // Note, this does not work at the moment - at least one folders shows up missing, and clips with no folder
643     // does not show up.
644     //    QDomElement kdenlivedoc_old = kdenlivedoc.toElement();
645     if (!kdenlivedoc_old.isNull()) {
646         QDomNodeList folders = kdenlivedoc_old.elementsByTagName("folder");
647         int fsize = folders.size();
648         int groupId = max_kproducer_id + 1; // Start at +1 of max id of the kdenlive_producers
649         for (int i = 0; i < fsize; ++i) {
650             QDomElement folder = folders.at(i).toElement();
651             if (!folder.isNull()) {
652                 QString groupName = folder.attribute("name");
653                 kDebug() << "groupName: " << groupName << " with groupId: " << groupId;
654                 QDomNodeList fproducers = folder.elementsByTagName("producer");
655                 int psize = fproducers.size();
656                 for (int j = 0; j < psize; ++j) {
657                     QDomElement fproducer = fproducers.at(j).toElement();
658                     if (!fproducer.isNull()) {
659                         QString id = fproducer.attribute("id");
660                         // This is not very effective, but compared to loading the clips, its a breeze
661                         QDomNodeList kdenlive_producers = kdenlivedoc_new.elementsByTagName("kdenlive_producer");
662                         int kpsize = kdenlive_producers.size();
663                         for (int k = 0; k < kpsize; ++k) {
664                             QDomElement kproducer = kdenlive_producers.at(k).toElement(); // Its an element for sure
665                             if (id == kproducer.attribute("id")) {
666                                 // We do not check that it already is part of a folder
667                                 kproducer.setAttribute("groupid", groupId);
668                                 kproducer.setAttribute("groupname", groupName);
669                                 break;
670                             }
671                         }
672                     }
673                 }
674                 ++groupId;
675             }
676         }
677     }
678 #endif
679     westley0.appendChild(kdenlivedoc_new);
680
681     QDomNodeList elements = westley.childNodes();
682     max = elements.count();
683     for (int i = 0; i < max; i++) {
684         QDomElement prod = elements.at(0).toElement();
685         westley0.insertAfter(prod, QDomNode());
686     }
687
688     westley0.removeChild(westley);
689
690     //kDebug() << "/////////////////  CONVERTED DOC:";
691     // kDebug() << m_document.toString();
692     /*
693     QFile file( "converted.kdenlive" );
694     if ( file.open( QIODevice::WriteOnly ) ) {
695       QTextStream stream( &file );
696       stream << m_document.toString();
697       file.close();
698     } else {
699       kDebug() << "Unable to dump file to converted.kdenlive";
700     }
701     */
702     //kDebug() << "/////////////////  END CONVERTED DOC:";
703 }
704
705 QString KdenliveDoc::colorToString(const QColor& c) {
706     QString ret = "%1,%2,%3,%4";
707     ret = ret.arg(c.red()).arg(c.green()).arg(c.blue()).arg(c.alpha());
708     return ret;
709 }
710
711 bool KdenliveDoc::saveSceneList(const QString &path, QDomDocument sceneList) {
712     QDomNode wes = sceneList.elementsByTagName("westley").at(0);
713
714     QDomElement addedXml = sceneList.createElement("kdenlivedoc");
715     QDomElement markers = sceneList.createElement("markers");
716     addedXml.setAttribute("version", "0.8");
717     addedXml.setAttribute("profile", profilePath());
718     addedXml.setAttribute("position", m_render->seekPosition().frames(m_fps));
719     addedXml.setAttribute("zoom", m_zoom);
720
721     QDomElement e;
722     QList <DocClipBase*> list = m_clipManager->documentClipList();
723     for (int i = 0; i < list.count(); i++) {
724         e = list.at(i)->toXML();
725         e.setTagName("kdenlive_producer");
726         addedXml.appendChild(sceneList.importNode(e, true));
727         QList < CommentedTime > marks = list.at(i)->commentedSnapMarkers();
728         for (int j = 0; j < marks.count(); j++) {
729             QDomElement marker = sceneList.createElement("marker");
730             marker.setAttribute("time", marks.at(j).time().ms() / 1000);
731             marker.setAttribute("comment", marks.at(j).comment());
732             marker.setAttribute("id", e.attribute("id"));
733             markers.appendChild(marker);
734         }
735     }
736     addedXml.appendChild(markers);
737     if (!m_guidesXml.isNull()) addedXml.appendChild(sceneList.importNode(m_guidesXml, true));
738
739     wes.appendChild(addedXml);
740     //wes.appendChild(doc.importNode(kdenliveData, true));
741
742     QFile file(path);
743     if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
744         kWarning() << "//////  ERROR writing to file: " << path;
745         KMessageBox::error(kapp->activeWindow(), i18n("Cannot write to file %1", path));
746         return false;
747     }
748     QTextStream out(&file);
749     out << sceneList.toString();
750     file.close();
751     return true;
752 }
753
754 QDomElement KdenliveDoc::documentInfoXml() {
755     QDomDocument doc;
756     QDomElement e;
757     QDomElement addedXml = doc.createElement("kdenlivedoc");
758     QDomElement markers = doc.createElement("markers");
759     addedXml.setAttribute("version", "0.7");
760     addedXml.setAttribute("profile", profilePath());
761     addedXml.setAttribute("position", m_render->seekPosition().frames(m_fps));
762     addedXml.setAttribute("zoom", m_zoom);
763     QList <DocClipBase*> list = m_clipManager->documentClipList();
764     for (int i = 0; i < list.count(); i++) {
765         e = list.at(i)->toXML();
766         e.setTagName("kdenlive_producer");
767         addedXml.appendChild(doc.importNode(e, true));
768         QList < CommentedTime > marks = list.at(i)->commentedSnapMarkers();
769         for (int j = 0; j < marks.count(); j++) {
770             QDomElement marker = doc.createElement("marker");
771             marker.setAttribute("time", marks.at(j).time().ms() / 1000);
772             marker.setAttribute("comment", marks.at(j).comment());
773             marker.setAttribute("id", e.attribute("id"));
774             markers.appendChild(marker);
775         }
776     }
777     addedXml.appendChild(markers);
778     if (!m_guidesXml.isNull()) addedXml.appendChild(doc.importNode(m_guidesXml, true));
779     //kDebug() << m_document.toString();
780     return addedXml;
781 }
782
783
784 ClipManager *KdenliveDoc::clipManager() {
785     return m_clipManager;
786 }
787
788 KUrl KdenliveDoc::projectFolder() const {
789     if (m_projectFolder.isEmpty()) return KUrl(KStandardDirs::locateLocal("appdata", "/projects/"));
790     return m_projectFolder;
791 }
792
793 QString KdenliveDoc::profilePath() const {
794     return m_profile.path;
795 }
796
797 MltVideoProfile KdenliveDoc::mltProfile() const {
798     return m_profile;
799 }
800
801 void KdenliveDoc::setProfilePath(QString path) {
802     if (path.isEmpty()) path = KdenliveSettings::default_profile();
803     if (path.isEmpty()) path = "dv_pal";
804     m_profile = ProfilesDialog::getVideoProfile(path);
805     KdenliveSettings::setProject_display_ratio((double) m_profile.display_aspect_num / m_profile.display_aspect_den);
806     m_fps = (double) m_profile.frame_rate_num / m_profile.frame_rate_den;
807     m_width = m_profile.width;
808     m_height = m_profile.height;
809     kDebug() << "KDEnnlive document, init timecode from path: " << path << ",  " << m_fps;
810     if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
811     else m_timecode.setFormat((int) m_fps);
812 }
813
814 const double KdenliveDoc::dar() {
815     return (double) m_profile.display_aspect_num / m_profile.display_aspect_den;
816 }
817
818 void KdenliveDoc::setThumbsProgress(const QString &message, int progress) {
819     emit progressInfo(message, progress);
820 }
821
822 void KdenliveDoc::loadingProgressed() {
823     m_documentLoadingProgress += m_documentLoadingStep;
824     emit progressInfo(QString(), (int) m_documentLoadingProgress);
825 }
826
827 QUndoStack *KdenliveDoc::commandStack() {
828     return m_commandStack;
829 }
830
831 void KdenliveDoc::setRenderer(Render *render) {
832     if (m_render) return;
833     m_render = render;
834     emit progressInfo(i18n("Loading playlist..."), 0);
835     //qApp->processEvents();
836     if (m_render) {
837         m_render->setSceneList(m_document.toString(), m_startPos);
838         checkProjectClips();
839     }
840     emit progressInfo(QString(), -1);
841 }
842
843 void KdenliveDoc::checkProjectClips() {
844     if (m_render == NULL) return;
845     QList <Mlt::Producer *> prods = m_render->producersList();
846     QString id ;
847     QString prodId ;
848     QString prodTrack ;
849     for (int i = 0; i < prods.count(); i++) {
850         id = prods.at(i)->get("id");
851         prodId = id.section('_', 0, 0);
852         prodTrack = id.section('_', 1, 1);
853         kDebug() << "CHECK PRO CLIP, ID: " << id;
854         DocClipBase *clip = m_clipManager->getClipById(prodId);
855         if (clip) clip->setProducer(prods.at(i));
856         kDebug() << "CHECK PRO CLIP, ID: " << id << " DONE";
857         if (clip && clip->clipType() == TEXT && !QFile::exists(clip->fileURL().path())) {
858             // regenerate text clip image if required
859             kDebug() << "// TITLE: " << clip->getProperty("titlename") << " Preview file: " << clip->getProperty("resource") << " DOES NOT EXIST";
860             QString titlename = clip->getProperty("titlename");
861             QString titleresource;
862             if (titlename.isEmpty()) {
863                 QStringList titleInfo = TitleWidget::getFreeTitleInfo(projectFolder());
864                 titlename = titleInfo.at(0);
865                 titleresource = titleInfo.at(1);
866                 clip->setProperty("titlename", titlename);
867                 kDebug() << "// New title set to: " << titlename;
868             } else {
869                 titleresource = TitleWidget::getTitleResourceFromName(projectFolder(), titlename);
870             }
871             QString titlepath = projectFolder().path() + "/titles/";
872             TitleWidget *dia_ui = new TitleWidget(KUrl(), titlepath, m_render, kapp->activeWindow());
873             QDomDocument doc;
874             doc.setContent(clip->getProperty("xmldata"));
875             dia_ui->setXml(doc);
876             QPixmap pix = dia_ui->renderedPixmap();
877             pix.save(titleresource);
878             clip->setProperty("resource", titleresource);
879             delete dia_ui;
880             clip->producer()->set("force_reload", 1);
881         }
882     }
883 }
884
885 Render *KdenliveDoc::renderer() {
886     return m_render;
887 }
888
889 void KdenliveDoc::updateClip(const QString &id) {
890     emit updateClipDisplay(id);
891 }
892
893 int KdenliveDoc::getFramePos(QString duration) {
894     return m_timecode.getFrameCount(duration, m_fps);
895 }
896
897 QString KdenliveDoc::producerName(const QString &id) {
898     QString result = "unnamed";
899     QDomNodeList prods = producersList();
900     int ct = prods.count();
901     for (int i = 0; i <  ct ; i++) {
902         QDomElement e = prods.item(i).toElement();
903         if (e.attribute("id") != "black" && e.attribute("id") == id) {
904             result = e.attribute("name");
905             if (result.isEmpty()) result = KUrl(e.attribute("resource")).fileName();
906             break;
907         }
908     }
909     return result;
910 }
911
912 void KdenliveDoc::setProducerDuration(const QString &id, int duration) {
913     QDomNodeList prods = producersList();
914     int ct = prods.count();
915     for (int i = 0; i <  ct ; i++) {
916         QDomElement e = prods.item(i).toElement();
917         if (e.attribute("id") != "black" && e.attribute("id") == id) {
918             e.setAttribute("duration", QString::number(duration));
919             break;
920         }
921     }
922 }
923
924 int KdenliveDoc::getProducerDuration(const QString &id) {
925     int result = 0;
926     QDomNodeList prods = producersList();
927     int ct = prods.count();
928     for (int i = 0; i <  ct ; i++) {
929         QDomElement e = prods.item(i).toElement();
930         if (e.attribute("id") != "black" && e.attribute("id") == id) {
931             result = e.attribute("duration").toInt();
932             break;
933         }
934     }
935     return result;
936 }
937
938
939 QDomDocument KdenliveDoc::generateSceneList() {
940     QDomDocument doc;
941     QDomElement westley = doc.createElement("westley");
942     doc.appendChild(westley);
943     QDomElement prod = doc.createElement("producer");
944 }
945
946 QDomDocument KdenliveDoc::toXml() const {
947     return m_document;
948 }
949
950 Timecode KdenliveDoc::timecode() const {
951     return m_timecode;
952 }
953
954 QDomNodeList KdenliveDoc::producersList() {
955     return m_document.elementsByTagName("producer");
956 }
957
958 void KdenliveDoc::backupMltPlaylist() {
959     if (m_render) m_scenelist = m_render->sceneList();
960 }
961
962 double KdenliveDoc::projectDuration() const {
963     if (m_render) return GenTime(m_render->getLength(), m_fps).ms() / 1000;
964 }
965
966 double KdenliveDoc::fps() const {
967     return m_fps;
968 }
969
970 int KdenliveDoc::width() const {
971     return m_width;
972 }
973
974 int KdenliveDoc::height() const {
975     return m_height;
976 }
977
978 KUrl KdenliveDoc::url() const {
979     return m_url;
980 }
981
982 void KdenliveDoc::setUrl(KUrl url) {
983     m_url = url;
984 }
985
986 void KdenliveDoc::setModified(bool mod) {
987     if (!m_url.isEmpty() && mod && KdenliveSettings::crashrecovery()) {
988         m_autoSaveTimer->start(3000);
989     }
990     if (mod == m_modified) return;
991     m_modified = mod;
992     emit docModified(m_modified);
993 }
994
995 bool KdenliveDoc::isModified() const {
996     return m_modified;
997 }
998
999 QString KdenliveDoc::description() const {
1000     if (m_url.isEmpty())
1001         return i18n("Untitled") + " / " + m_profile.description;
1002     else
1003         return m_url.fileName() + " / " + m_profile.description;
1004 }
1005
1006 void KdenliveDoc::addClip(QDomElement elem, QString clipId, bool createClipItem) {
1007     const QString producerId = clipId.section('_', 0, 0);
1008     int subtrack = clipId.section('_', 1, 1).toInt();
1009     DocClipBase *clip = m_clipManager->getClipById(producerId);
1010     if (clip == NULL) {
1011         elem.setAttribute("id", producerId);
1012         QString path = elem.attribute("resource");
1013         QString extension;
1014         if (elem.attribute("type").toInt() == SLIDESHOW) {
1015             extension = KUrl(path).fileName();
1016             path = KUrl(path).directory();
1017         }
1018         if (!path.isEmpty() && !QFile::exists(path)) {
1019             const QString size = elem.attribute("file_size");
1020             const QString hash = elem.attribute("file_hash");
1021             QString newpath;
1022             KMessageBox::ButtonCode action = KMessageBox::No;
1023             if (!size.isEmpty() && !hash.isEmpty()) {
1024                 if (!m_searchFolder.isEmpty()) newpath = searchFileRecursively(m_searchFolder, size, hash);
1025                 else action = (KMessageBox::ButtonCode)KMessageBox::messageBox(kapp->activeWindow(), KMessageBox::WarningYesNo, i18n("<qt>Clip <b>%1</b><br>is invalid, what do you want to do?", path), i18n("File not found"), KGuiItem(i18n("Search automatically")), /*KGuiItem(i18n("Remove from project")), */KGuiItem(i18n("Keep as placeholder")));
1026             } else {
1027                 if (elem.attribute("type").toInt() == SLIDESHOW) {
1028                     if (KMessageBox::messageBox(kapp->activeWindow(), KMessageBox::WarningYesNo, i18n("<qt>Clip <b>%1</b><br>is invalid, what do you want to do?", path), i18n("File not found"), KGuiItem(i18n("Search automatically")), /*KGuiItem(i18n("Remove from project")),*/ KGuiItem(i18n("Keep as placeholder"))) == KMessageBox::Yes)
1029                         newpath = KFileDialog::getExistingDirectory(KUrl("kfiledialog:///clipfolder"), kapp->activeWindow(), i18n("Looking for %1", path));
1030                 } else newpath = KFileDialog::getOpenFileName(KUrl("kfiledialog:///clipfolder"), QString(), kapp->activeWindow(), i18n("Looking for %1", path));
1031             }
1032             if (action == KMessageBox::Yes) {
1033                 kDebug() << "// ASKED FOR SRCH CLIP: " << clipId;
1034                 m_searchFolder = KFileDialog::getExistingDirectory(KUrl("kfiledialog:///clipfolder"), kapp->activeWindow());
1035                 if (!m_searchFolder.isEmpty()) {
1036                     newpath = searchFileRecursively(QDir(m_searchFolder), size, hash);
1037                 }
1038             }
1039             if (!newpath.isEmpty()) {
1040                 if (elem.attribute("type").toInt() == SLIDESHOW) newpath.append('/' + extension);
1041                 elem.setAttribute("resource", newpath);
1042                 setNewClipResource(clipId, newpath);
1043                 setModified(true);
1044             }
1045         }
1046         clip = new DocClipBase(m_clipManager, elem, producerId);
1047         m_clipManager->addClip(clip);
1048     }
1049     if (createClipItem) emit addProjectClip(clip);
1050 }
1051
1052 void KdenliveDoc::setNewClipResource(const QString &id, const QString &path) {
1053     QDomNodeList prods = m_document.elementsByTagName("producer");
1054     int maxprod = prods.count();
1055     for (int i = 0; i < maxprod; i++) {
1056         QDomNode m = prods.at(i);
1057         QString prodId = m.toElement().attribute("id");
1058         if (prodId == id || prodId.startsWith(id + "_")) {
1059             QDomNodeList params = m.childNodes();
1060             for (int j = 0; j < params.count(); j++) {
1061                 QDomElement e = params.item(j).toElement();
1062                 if (e.attribute("name") == "resource") {
1063                     e.firstChild().setNodeValue(path);
1064                     break;
1065                 }
1066             }
1067         }
1068     }
1069 }
1070
1071 QString KdenliveDoc::searchFileRecursively(const QDir &dir, const QString &matchSize, const QString &matchHash) const {
1072     QString foundFileName;
1073     QByteArray fileData;
1074     QByteArray fileHash;
1075     QStringList filesAndDirs = dir.entryList(QDir::Files | QDir::Readable);
1076     for (int i = 0; i < filesAndDirs.size() && foundFileName.isEmpty(); i++) {
1077         QFile file(dir.absoluteFilePath(filesAndDirs.at(i)));
1078         if (file.open(QIODevice::ReadOnly)) {
1079             if (QString::number(file.size()) == matchSize) {
1080                 /*
1081                 * 1 MB = 1 second per 450 files (or faster)
1082                 * 10 MB = 9 seconds per 450 files (or faster)
1083                 */
1084                 if (file.size() > 1000000*2) {
1085                     fileData = file.read(1000000);
1086                     if (file.seek(file.size() - 1000000))
1087                         fileData.append(file.readAll());
1088                 } else
1089                     fileData = file.readAll();
1090                 file.close();
1091                 fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
1092                 if (QString(fileHash.toHex()) == matchHash)
1093                     return file.fileName();
1094             }
1095         }
1096         kDebug() << filesAndDirs.at(i) << file.size() << fileHash.toHex();
1097     }
1098     filesAndDirs = dir.entryList(QDir::Dirs | QDir::Readable | QDir::Executable | QDir::NoDotAndDotDot);
1099     for (int i = 0; i < filesAndDirs.size() && foundFileName.isEmpty(); i++) {
1100         foundFileName = searchFileRecursively(dir.absoluteFilePath(filesAndDirs.at(i)), matchSize, matchHash);
1101         if (!foundFileName.isEmpty())
1102             break;
1103     }
1104     return foundFileName;
1105 }
1106
1107 void KdenliveDoc::addClipInfo(QDomElement elem, QString clipId) {
1108     DocClipBase *clip = m_clipManager->getClipById(clipId);
1109     if (clip == NULL) {
1110         addClip(elem, clipId);
1111     } else {
1112         QMap <QString, QString> properties;
1113         QDomNamedNodeMap attributes = elem.attributes();
1114         QString attrname;
1115         for (unsigned int i = 0; i < attributes.count(); i++) {
1116             attrname = attributes.item(i).nodeName();
1117             if (attrname != "resource")
1118                 properties.insert(attrname, attributes.item(i).nodeValue());
1119             kDebug() << attrname << " = " << attributes.item(i).nodeValue();
1120         }
1121         clip->setProperties(properties);
1122         emit addProjectClip(clip);
1123     }
1124 }
1125
1126 void KdenliveDoc::addFolder(const QString foldername, const QString &clipId, bool edit) {
1127     emit addProjectFolder(foldername, clipId, false, edit);
1128 }
1129
1130 void KdenliveDoc::deleteFolder(const QString foldername, const QString &clipId) {
1131     emit addProjectFolder(foldername, clipId, true);
1132 }
1133
1134 void KdenliveDoc::deleteProjectClip(QList <QString> ids) {
1135     for (int i = 0; i < ids.size(); ++i) {
1136         emit deleteTimelineClip(ids.at(i));
1137         m_clipManager->slotDeleteClip(ids.at(i));
1138     }
1139     setModified(true);
1140 }
1141
1142 void KdenliveDoc::deleteProjectFolder(QMap <QString, QString> map) {
1143     QMapIterator<QString, QString> i(map);
1144     while (i.hasNext()) {
1145         i.next();
1146         slotDeleteFolder(i.key(), i.value());
1147     }
1148     setModified(true);
1149 }
1150
1151 void KdenliveDoc::deleteClip(const QString &clipId) {
1152     emit signalDeleteProjectClip(clipId);
1153     m_clipManager->deleteClip(clipId);
1154 }
1155
1156 void KdenliveDoc::slotAddClipList(const KUrl::List urls, const QString group, const QString &groupId) {
1157     m_clipManager->slotAddClipList(urls, group, groupId);
1158     emit selectLastAddedClip(QString::number(m_clipManager->lastClipId()));
1159     setModified(true);
1160 }
1161
1162
1163 void KdenliveDoc::slotAddClipFile(const KUrl url, const QString group, const QString &groupId) {
1164     kDebug() << "/////////  DOCUM, ADD CLP: " << url;
1165     m_clipManager->slotAddClipFile(url, group, groupId);
1166     emit selectLastAddedClip(QString::number(m_clipManager->lastClipId()));
1167     setModified(true);
1168 }
1169
1170 void KdenliveDoc::slotAddFolder(const QString folderName) {
1171     AddFolderCommand *command = new AddFolderCommand(this, folderName, QString::number(m_clipManager->getFreeClipId()), true);
1172     commandStack()->push(command);
1173     setModified(true);
1174 }
1175
1176 void KdenliveDoc::slotDeleteFolder(const QString folderName, const QString &id) {
1177     AddFolderCommand *command = new AddFolderCommand(this, folderName, id, false);
1178     commandStack()->push(command);
1179     setModified(true);
1180 }
1181
1182 void KdenliveDoc::slotEditFolder(const QString newfolderName, const QString oldfolderName, const QString &clipId) {
1183     EditFolderCommand *command = new EditFolderCommand(this, newfolderName, oldfolderName, clipId, false);
1184     commandStack()->push(command);
1185     setModified(true);
1186 }
1187
1188 const QString&KdenliveDoc::getFreeClipId() {
1189     return QString::number(m_clipManager->getFreeClipId());
1190 }
1191
1192 DocClipBase *KdenliveDoc::getBaseClip(const QString &clipId) {
1193     return m_clipManager->getClipById(clipId);
1194 }
1195
1196 void KdenliveDoc::slotAddColorClipFile(const QString name, const QString color, QString duration, const QString group, const QString &groupId) {
1197     m_clipManager->slotAddColorClipFile(name, color, duration, group, groupId);
1198     setModified(true);
1199 }
1200
1201 void KdenliveDoc::slotAddSlideshowClipFile(const QString name, const QString path, int count, const QString duration, const bool loop, const bool fade, const QString &luma_duration, const QString &luma_file, const int softness, const QString group, const QString &groupId) {
1202     m_clipManager->slotAddSlideshowClipFile(name, path, count, duration, loop, fade, luma_duration, luma_file, softness, group, groupId);
1203     setModified(true);
1204 }
1205
1206 void KdenliveDoc::slotCreateTextClip(QString group, const QString &groupId) {
1207     QString titlesFolder = projectFolder().path() + "/titles/";
1208     KStandardDirs::makeDir(titlesFolder);
1209     TitleWidget *dia_ui = new TitleWidget(KUrl(), titlesFolder, m_render, kapp->activeWindow());
1210     if (dia_ui->exec() == QDialog::Accepted) {
1211         QStringList titleInfo = TitleWidget::getFreeTitleInfo(projectFolder());
1212         QPixmap pix = dia_ui->renderedPixmap();
1213         pix.save(titleInfo.at(1));
1214         //dia_ui->saveTitle(path + ".kdenlivetitle");
1215         m_clipManager->slotAddTextClipFile(titleInfo.at(0), titleInfo.at(1), dia_ui->xml().toString(), QString(), QString());
1216         setModified(true);
1217     }
1218     delete dia_ui;
1219 }
1220
1221
1222
1223 #include "kdenlivedoc.moc"
1224