]> git.sesse.net Git - kdenlive/blob - src/kdenlivedoc.cpp
80037d38630e2d232e175a43e74cc7229ecb1e0b
[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
21 #include "kdenlivedoc.h"
22 #include "docclipbase.h"
23 #include "profilesdialog.h"
24 #include "kdenlivesettings.h"
25 #include "renderer.h"
26 #include "clipmanager.h"
27 #include "titlewidget.h"
28 #include "mainwindow.h"
29 #include "documentchecker.h"
30 #include "documentvalidator.h"
31 #include "kdenlive-config.h"
32
33 #include <KDebug>
34 #include <KStandardDirs>
35 #include <KMessageBox>
36 #include <KLocale>
37 #include <KFileDialog>
38 #include <KIO/NetAccess>
39 #include <KIO/CopyJob>
40 #include <KApplication>
41
42 #include <QCryptographicHash>
43 #include <QFile>
44 #include <QInputDialog>
45 #include <QDomImplementation>
46
47 #include <mlt++/Mlt.h>
48
49 const double DOCUMENTVERSION = 0.85;
50
51 KdenliveDoc::KdenliveDoc(const KUrl &url, const KUrl &projectFolder, QUndoGroup *undoGroup, QString profileName, const QPoint tracks, Render *render, MainWindow *parent) :
52         QObject(parent),
53         m_autosave(NULL),
54         m_url(url),
55         m_render(render),
56         m_commandStack(new QUndoStack(undoGroup)),
57         m_modified(false),
58         m_projectFolder(projectFolder),
59         m_abortLoading(false)
60 {
61     m_clipManager = new ClipManager(this);
62     m_autoSaveTimer = new QTimer(this);
63     m_autoSaveTimer->setSingleShot(true);
64     bool success = false;
65
66     // init default document properties
67     m_documentProperties["zoom"] = "7";
68     m_documentProperties["verticalzoom"] = "1";
69     m_documentProperties["zonein"] = "0";
70     m_documentProperties["zoneout"] = "100";
71
72     if (!url.isEmpty()) {
73         QString tmpFile;
74         success = KIO::NetAccess::download(url.path(), tmpFile, parent);
75         if (!success) // The file cannot be opened
76             KMessageBox::error(parent, KIO::NetAccess::lastErrorString());
77         else {
78             QFile file(tmpFile);
79             QString errorMsg;
80             QDomImplementation impl;
81             impl.setInvalidDataPolicy(QDomImplementation::DropInvalidChars);
82             success = m_document.setContent(&file, false, &errorMsg);
83             file.close();
84             KIO::NetAccess::removeTempFile(tmpFile);
85
86             if (!success) // It is corrupted
87                 KMessageBox::error(parent, errorMsg);
88             else {
89                 parent->slotGotProgressInfo(i18n("Validating"), 0);
90                 DocumentValidator validator(m_document);
91                 success = validator.isProject();
92                 if (!success) {
93                     // It is not a project file
94                     parent->slotGotProgressInfo(i18n("File %1 is not a Kdenlive project file", m_url.path()), 0);
95                 } else {
96                     /*
97                      * Validate the file against the current version (upgrade
98                      * and recover it if needed). It is NOT a passive operation
99                      */
100                     // TODO: backup the document or alert the user?
101                     success = validator.validate(DOCUMENTVERSION);
102                     if (success) { // Let the validator handle error messages
103                         parent->slotGotProgressInfo(i18n("Loading"), 0);
104                         QDomElement mlt = m_document.firstChildElement("mlt");
105                         QDomElement infoXml = mlt.firstChildElement("kdenlivedoc");
106
107                         profileName = infoXml.attribute("profile");
108                         m_projectFolder = KUrl(infoXml.attribute("projectfolder"));
109                         QDomElement docproperties = infoXml.firstChildElement("documentproperties");
110                         QDomNamedNodeMap props = docproperties.attributes();
111                         for (int i = 0; i < props.count(); i++) {
112                             m_documentProperties.insert(props.item(i).nodeName(), props.item(i).nodeValue());
113                         }
114                         // Build tracks
115                         QDomElement e;
116                         QDomElement tracksinfo = infoXml.firstChildElement("tracksinfo");
117                         TrackInfo projectTrack;
118                         if (!tracksinfo.isNull()) {
119                             QDomNodeList trackslist = tracksinfo.childNodes();
120                             int maxchild = trackslist.count();
121                             for (int k = 0; k < maxchild; k++) {
122                                 e = trackslist.at(k).toElement();
123                                 if (e.tagName() == "trackinfo") {
124                                     if (e.attribute("type") == "audio") projectTrack.type = AUDIOTRACK;
125                                     else projectTrack.type = VIDEOTRACK;
126                                     projectTrack.isMute = e.attribute("mute").toInt();
127                                     projectTrack.isBlind = e.attribute("blind").toInt();
128                                     projectTrack.isLocked = e.attribute("locked").toInt();
129                                     projectTrack.trackName = e.attribute("trackname");
130                                     m_tracksList.append(projectTrack);
131                                 }
132                             }
133                             mlt.removeChild(tracksinfo);
134                         }
135                         QDomNodeList producers = m_document.elementsByTagName("producer");
136                         QDomNodeList infoproducers = m_document.elementsByTagName("kdenlive_producer");
137                         parent->slotGotProgressInfo(i18n("Check missing clips"), 0);
138                         if (checkDocumentClips(infoproducers) == false) m_abortLoading = true;
139                         const int max = producers.count();
140                         const int infomax = infoproducers.count();
141
142                         QDomNodeList                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    folders = m_document.elementsByTagName("folder");
143                         for (int i = 0; i < folders.count(); i++) {
144                             e = folders.item(i).cloneNode().toElement();
145                             m_clipManager->addFolder(e.attribute("id"), e.attribute("name"));
146                         }
147
148                         for (int i = 0; i < infomax && !m_abortLoading; i++) {
149                             e = infoproducers.item(i).cloneNode().toElement();
150                             QString prodId = e.attribute("id");
151                             if (!e.isNull() && prodId != "black" && !prodId.startsWith("slowmotion") && !m_abortLoading) {
152                                 e.setTagName("producer");
153                                 // Get MLT's original producer properties
154                                 QDomElement orig;
155                                 for (int j = 0; j < max; j++) {
156                                     QDomElement o = producers.item(j).cloneNode().toElement();
157                                     QString origId = o.attribute("id").section('_', 0, 0);
158                                     if (origId == prodId) {
159                                         orig = o;
160                                         break;
161                                     }
162                                 }
163                                 addClipInfo(e, orig, prodId);
164                             }
165                         }
166                         if (m_abortLoading) {
167                             //parent->slotGotProgressInfo(i18n("File %1 is not a Kdenlive project file."), 100);
168                             emit resetProjectList();
169                             m_documentProperties.remove("position");
170                             m_url = KUrl();
171                             m_tracksList.clear();
172                             kWarning() << "Aborted loading of: " << url.path();
173                             m_document = createEmptyDocument(KdenliveSettings::videotracks(), KdenliveSettings::audiotracks());
174                             setProfilePath(KdenliveSettings::default_profile());
175                             m_clipManager->clear();
176                         } else {
177                             QDomElement markers = infoXml.firstChildElement("markers");
178                             if (!markers.isNull()) {
179                                 QDomNodeList markerslist = markers.childNodes();
180                                 int maxchild = markerslist.count();
181                                 for (int k = 0; k < maxchild; k++) {
182                                     e = markerslist.at(k).toElement();
183                                     if (e.tagName() == "marker") {
184                                         m_clipManager->getClipById(e.attribute("id"))->addSnapMarker(GenTime(e.attribute("time").toDouble()), e.attribute("comment"));
185                                     }
186                                 }
187                                 infoXml.removeChild(markers);
188                             }
189                             setProfilePath(profileName);
190                             setModified(validator.isModified());
191                             kDebug() << "Reading file: " << url.path() << ", found clips: " << producers.count();
192                         }
193                     }
194                 }
195             }
196         }
197     }
198
199     // Something went wrong, or a new file was requested: create a new project
200     if (!success) {
201         setProfilePath(profileName);
202         m_url = KUrl();
203         m_document = createEmptyDocument(tracks.x(), tracks.y());
204     }
205
206     // Set the video profile (empty == default)
207     KdenliveSettings::setCurrent_profile(profilePath());
208
209     // Ask to create the project directory if it does not exist
210     if (!QFile::exists(m_projectFolder.path())) {
211         int create = KMessageBox::questionYesNo(parent, i18n("Project directory %1 does not exist. Create it?", m_projectFolder.path()));
212         if (create == KMessageBox::Yes) {
213             QDir projectDir(m_projectFolder.path());
214             bool ok = projectDir.mkpath(m_projectFolder.path());
215             if (!ok) {
216                 KMessageBox::sorry(parent, i18n("The directory %1, could not be created.\nPlease make sure you have the required permissions.", m_projectFolder.path()));
217             }
218         }
219     }
220
221     // Make sure the project folder is usable
222     if (m_projectFolder.isEmpty() || !KIO::NetAccess::exists(m_projectFolder.path(), KIO::NetAccess::DestinationSide, parent)) {
223         KMessageBox::information(parent, i18n("Document project folder is invalid, setting it to the default one: %1", KdenliveSettings::defaultprojectfolder()));
224         m_projectFolder = KUrl(KdenliveSettings::defaultprojectfolder());
225     }
226
227     // Make sure that the necessary folders exist
228     KStandardDirs::makeDir(m_projectFolder.path(KUrl::AddTrailingSlash) + "titles/");
229     KStandardDirs::makeDir(m_projectFolder.path(KUrl::AddTrailingSlash) + "thumbs/");
230     KStandardDirs::makeDir(m_projectFolder.path(KUrl::AddTrailingSlash) + "ladspa/");
231
232     //kDebug() << "// SETTING SCENE LIST:\n\n" << m_document.toString();
233     connect(m_autoSaveTimer, SIGNAL(timeout()), this, SLOT(slotAutoSave()));
234 }
235
236 KdenliveDoc::~KdenliveDoc()
237 {
238     m_autoSaveTimer->stop();
239     delete m_commandStack;
240     kDebug() << "// DEL CLP MAN";
241     delete m_clipManager;
242     kDebug() << "// DEL CLP MAN done";
243     delete m_autoSaveTimer;
244     if (m_autosave) {
245         if (!m_autosave->fileName().isEmpty()) m_autosave->remove();
246         delete m_autosave;
247     }
248 }
249
250 int KdenliveDoc::setSceneList()
251 {
252     m_render->resetProfile(KdenliveSettings::current_profile());
253     if (m_render->setSceneList(m_document.toString(), m_documentProperties.value("position").toInt()) == -1) {
254         // INVALID MLT Consumer, something is wrong
255         return -1;
256     }
257     m_documentProperties.remove("position");
258     // m_document xml is now useless, clear it
259     m_document.clear();
260     return 0;
261 }
262
263 QDomDocument KdenliveDoc::createEmptyDocument(int videotracks, int audiotracks)
264 {
265     TrackInfo videoTrack;
266     videoTrack.type = VIDEOTRACK;
267     videoTrack.isMute = false;
268     videoTrack.isBlind = false;
269     videoTrack.isLocked = false;
270
271     TrackInfo audioTrack;
272     audioTrack.type = AUDIOTRACK;
273     audioTrack.isMute = false;
274     audioTrack.isBlind = true;
275     audioTrack.isLocked = false;
276
277     m_tracksList.clear();
278
279     // Tracks are added Â«backwards», so we need to reverse the track numbering
280     // mbt 331: http://www.kdenlive.org/mantis/view.php?id=331
281     // Better default names for tracks: Audio 1 etc. instead of blank numbers
282     for (int i = 0; i < audiotracks; i++) {
283         audioTrack.trackName = QString("Audio ") + QString::number(audiotracks - i);
284         m_tracksList.append(audioTrack);
285
286     }
287     for (int i = 0; i < videotracks; i++) {
288         videoTrack.trackName = QString("Video ") + QString::number(videotracks - i);
289         m_tracksList.append(videoTrack);
290     }
291     return createEmptyDocument(m_tracksList);
292 }
293
294 QDomDocument KdenliveDoc::createEmptyDocument(QList <TrackInfo> tracks)
295 {
296     // Creating new document
297     QDomDocument doc;
298     QDomElement mlt = doc.createElement("mlt");
299     doc.appendChild(mlt);
300
301
302     // Create black producer
303     // For some unknown reason, we have to build the black producer here and not in renderer.cpp, otherwise
304     // the composite transitions with the black track are corrupted.
305     QDomElement blk = doc.createElement("producer");
306     blk.setAttribute("in", 0);
307     blk.setAttribute("out", 500);
308     blk.setAttribute("id", "black");
309
310     QDomElement property = doc.createElement("property");
311     property.setAttribute("name", "mlt_type");
312     QDomText value = doc.createTextNode("producer");
313     property.appendChild(value);
314     blk.appendChild(property);
315
316     property = doc.createElement("property");
317     property.setAttribute("name", "aspect_ratio");
318     value = doc.createTextNode(QString::number(0.0));
319     property.appendChild(value);
320     blk.appendChild(property);
321
322     property = doc.createElement("property");
323     property.setAttribute("name", "length");
324     value = doc.createTextNode(QString::number(15000));
325     property.appendChild(value);
326     blk.appendChild(property);
327
328     property = doc.createElement("property");
329     property.setAttribute("name", "eof");
330     value = doc.createTextNode("pause");
331     property.appendChild(value);
332     blk.appendChild(property);
333
334     property = doc.createElement("property");
335     property.setAttribute("name", "resource");
336     value = doc.createTextNode("black");
337     property.appendChild(value);
338     blk.appendChild(property);
339
340     property = doc.createElement("property");
341     property.setAttribute("name", "mlt_service");
342     value = doc.createTextNode("colour");
343     property.appendChild(value);
344     blk.appendChild(property);
345
346     mlt.appendChild(blk);
347
348
349     QDomElement tractor = doc.createElement("tractor");
350     tractor.setAttribute("id", "maintractor");
351     QDomElement multitrack = doc.createElement("multitrack");
352     QDomElement playlist = doc.createElement("playlist");
353     playlist.setAttribute("id", "black_track");
354     mlt.appendChild(playlist);
355
356     QDomElement blank0 = doc.createElement("entry");
357     blank0.setAttribute("in", "0");
358     blank0.setAttribute("out", "1");
359     blank0.setAttribute("producer", "black");
360     playlist.appendChild(blank0);
361
362     // create playlists
363     int total = tracks.count() + 1;
364
365     for (int i = 1; i < total; i++) {
366         QDomElement playlist = doc.createElement("playlist");
367         playlist.setAttribute("id", "playlist" + QString::number(i));
368         mlt.appendChild(playlist);
369     }
370
371     QDomElement track0 = doc.createElement("track");
372     track0.setAttribute("producer", "black_track");
373     tractor.appendChild(track0);
374
375     // create audio and video tracks
376     for (int i = 1; i < total; i++) {
377         QDomElement track = doc.createElement("track");
378         track.setAttribute("producer", "playlist" + QString::number(i));
379         if (tracks.at(i - 1).type == AUDIOTRACK) {
380             track.setAttribute("hide", "video");
381         } else if (tracks.at(i - 1).isBlind)
382             track.setAttribute("hide", "video");
383         if (tracks.at(i - 1).isMute)
384             track.setAttribute("hide", "audio");
385         tractor.appendChild(track);
386     }
387
388     for (int i = 2; i < total ; i++) {
389         QDomElement transition = doc.createElement("transition");
390         transition.setAttribute("always_active", "1");
391
392         QDomElement property = doc.createElement("property");
393         property.setAttribute("name", "a_track");
394         QDomText value = doc.createTextNode(QString::number(1));
395         property.appendChild(value);
396         transition.appendChild(property);
397
398         property = doc.createElement("property");
399         property.setAttribute("name", "b_track");
400         value = doc.createTextNode(QString::number(i));
401         property.appendChild(value);
402         transition.appendChild(property);
403
404         property = doc.createElement("property");
405         property.setAttribute("name", "mlt_service");
406         value = doc.createTextNode("mix");
407         property.appendChild(value);
408         transition.appendChild(property);
409
410         property = doc.createElement("property");
411         property.setAttribute("name", "combine");
412         value = doc.createTextNode("1");
413         property.appendChild(value);
414         transition.appendChild(property);
415
416         property = doc.createElement("property");
417         property.setAttribute("name", "internal_added");
418         value = doc.createTextNode("237");
419         property.appendChild(value);
420         transition.appendChild(property);
421         tractor.appendChild(transition);
422     }
423     mlt.appendChild(tractor);
424     return doc;
425 }
426
427
428 void KdenliveDoc::syncGuides(QList <Guide *> guides)
429 {
430     m_guidesXml.clear();
431     QDomElement guideNode = m_guidesXml.createElement("guides");
432     m_guidesXml.appendChild(guideNode);
433     QDomElement e;
434
435     for (int i = 0; i < guides.count(); i++) {
436         e = m_guidesXml.createElement("guide");
437         e.setAttribute("time", guides.at(i)->position().ms() / 1000);
438         e.setAttribute("comment", guides.at(i)->label());
439         guideNode.appendChild(e);
440     }
441     setModified(true);
442     emit guidesUpdated();
443 }
444
445 QDomElement KdenliveDoc::guidesXml() const
446 {
447     return m_guidesXml.documentElement();
448 }
449
450 void KdenliveDoc::slotAutoSave()
451 {
452     if (m_render && m_autosave) {
453         if (!m_autosave->isOpen() && !m_autosave->open(QIODevice::ReadWrite)) {
454             // show error: could not open the autosave file
455             kDebug() << "ERROR; CANNOT CREATE AUTOSAVE FILE";
456         }
457         kDebug() << "// AUTOSAVE FILE: " << m_autosave->fileName();
458         QString doc;
459         if (KdenliveSettings::dropbframes()) {
460             KdenliveSettings::setDropbframes(false);
461             m_clipManager->updatePreviewSettings();
462             doc = m_render->sceneList();
463             KdenliveSettings::setDropbframes(true);
464             m_clipManager->updatePreviewSettings();
465         } else doc = m_render->sceneList();
466         saveSceneList(m_autosave->fileName(), doc);
467     }
468 }
469
470 void KdenliveDoc::setZoom(int horizontal, int vertical)
471 {
472     m_documentProperties["zoom"] = QString::number(horizontal);
473     m_documentProperties["verticalzoom"] = QString::number(vertical);
474 }
475
476 QPoint KdenliveDoc::zoom() const
477 {
478     return QPoint(m_documentProperties.value("zoom").toInt(), m_documentProperties.value("verticalzoom").toInt());
479 }
480
481 void KdenliveDoc::setZone(int start, int end)
482 {
483     m_documentProperties["zonein"] = QString::number(start);
484     m_documentProperties["zoneout"] = QString::number(end);
485 }
486
487 QPoint KdenliveDoc::zone() const
488 {
489     return QPoint(m_documentProperties.value("zonein").toInt(), m_documentProperties.value("zoneout").toInt());
490 }
491
492 bool KdenliveDoc::saveSceneList(const QString &path, const QString &scene)
493 {
494     QDomDocument sceneList;
495     sceneList.setContent(scene, true);
496     QDomElement mlt = sceneList.firstChildElement("mlt");
497     if (mlt.isNull() || !mlt.hasChildNodes()) {
498         //Make sure we don't save if scenelist is corrupted
499         KMessageBox::error(kapp->activeWindow(), i18n("Cannot write to file %1, scene list is corrupted.", path));
500         return false;
501     }
502     QDomElement addedXml = sceneList.createElement("kdenlivedoc");
503     mlt.appendChild(addedXml);
504
505     QDomElement markers = sceneList.createElement("markers");
506     addedXml.setAttribute("version", DOCUMENTVERSION);
507     addedXml.setAttribute("kdenliveversion", VERSION);
508     addedXml.setAttribute("profile", profilePath());
509     addedXml.setAttribute("projectfolder", m_projectFolder.path());
510
511     QDomElement docproperties = sceneList.createElement("documentproperties");
512     QMapIterator<QString, QString> i(m_documentProperties);
513     while (i.hasNext()) {
514         i.next();
515         docproperties.setAttribute(i.key(), i.value());
516     }
517     docproperties.setAttribute("position", m_render->seekPosition().frames(m_fps));
518     addedXml.appendChild(docproperties);
519
520     // Add profile info
521     QDomElement profileinfo = sceneList.createElement("profileinfo");
522     profileinfo.setAttribute("description", m_profile.description);
523     profileinfo.setAttribute("frame_rate_num", m_profile.frame_rate_num);
524     profileinfo.setAttribute("frame_rate_den", m_profile.frame_rate_den);
525     profileinfo.setAttribute("width", m_profile.width);
526     profileinfo.setAttribute("height", m_profile.height);
527     profileinfo.setAttribute("progressive", m_profile.progressive);
528     profileinfo.setAttribute("sample_aspect_num", m_profile.sample_aspect_num);
529     profileinfo.setAttribute("sample_aspect_den", m_profile.sample_aspect_den);
530     profileinfo.setAttribute("display_aspect_num", m_profile.display_aspect_num);
531     profileinfo.setAttribute("display_aspect_den", m_profile.display_aspect_den);
532     addedXml.appendChild(profileinfo);
533
534     // tracks info
535     QDomElement tracksinfo = sceneList.createElement("tracksinfo");
536     foreach(const TrackInfo & info, m_tracksList) {
537         QDomElement trackinfo = sceneList.createElement("trackinfo");
538         if (info.type == AUDIOTRACK) trackinfo.setAttribute("type", "audio");
539         trackinfo.setAttribute("mute", info.isMute);
540         trackinfo.setAttribute("blind", info.isBlind);
541         trackinfo.setAttribute("locked", info.isLocked);
542         trackinfo.setAttribute("trackname", info.trackName);
543         tracksinfo.appendChild(trackinfo);
544     }
545     addedXml.appendChild(tracksinfo);
546
547     // save project folders
548     QMap <QString, QString> folderlist = m_clipManager->documentFolderList();
549
550     QMapIterator<QString, QString> f(folderlist);
551     while (f.hasNext()) {
552         f.next();
553         QDomElement folder = sceneList.createElement("folder");
554         folder.setAttribute("id", f.key());
555         folder.setAttribute("name", f.value());
556         addedXml.appendChild(folder);
557     }
558
559     // Save project clips
560     QDomElement e;
561     QList <DocClipBase*> list = m_clipManager->documentClipList();
562     for (int i = 0; i < list.count(); i++) {
563         e = list.at(i)->toXML();
564         e.setTagName("kdenlive_producer");
565         addedXml.appendChild(sceneList.importNode(e, true));
566         QList < CommentedTime > marks = list.at(i)->commentedSnapMarkers();
567         for (int j = 0; j < marks.count(); j++) {
568             QDomElement marker = sceneList.createElement("marker");
569             marker.setAttribute("time", marks.at(j).time().ms() / 1000);
570             marker.setAttribute("comment", marks.at(j).comment());
571             marker.setAttribute("id", e.attribute("id"));
572             markers.appendChild(marker);
573         }
574     }
575     addedXml.appendChild(markers);
576
577     // Add guides
578     if (!m_guidesXml.isNull()) addedXml.appendChild(sceneList.importNode(m_guidesXml.documentElement(), true));
579
580     // Add clip groups
581     addedXml.appendChild(sceneList.importNode(m_clipManager->groupsXml(), true));
582
583     //wes.appendChild(doc.importNode(kdenliveData, true));
584
585     QFile file(path);
586     if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
587         kWarning() << "//////  ERROR writing to file: " << path;
588         KMessageBox::error(kapp->activeWindow(), i18n("Cannot write to file %1", path));
589         return false;
590     }
591
592     file.write(sceneList.toString().toUtf8());
593     if (file.error() != QFile::NoError) {
594         KMessageBox::error(kapp->activeWindow(), i18n("Cannot write to file %1", path));
595         file.close();
596         return false;
597     }
598     file.close();
599     return true;
600 }
601
602 ClipManager *KdenliveDoc::clipManager()
603 {
604     return m_clipManager;
605 }
606
607 KUrl KdenliveDoc::projectFolder() const
608 {
609     //if (m_projectFolder.isEmpty()) return KUrl(KStandardDirs::locateLocal("appdata", "/projects/"));
610     return m_projectFolder;
611 }
612
613 void KdenliveDoc::setProjectFolder(KUrl url)
614 {
615     if (url == m_projectFolder) return;
616     setModified(true);
617     KStandardDirs::makeDir(url.path());
618     KStandardDirs::makeDir(url.path(KUrl::AddTrailingSlash) + "titles/");
619     KStandardDirs::makeDir(url.path(KUrl::AddTrailingSlash) + "thumbs/");
620     if (KMessageBox::questionYesNo(kapp->activeWindow(), i18n("You have changed the project folder. Do you want to copy the cached data from %1 to the new folder %2?").arg(m_projectFolder.path(), url.path())) == KMessageBox::Yes) moveProjectData(url);
621     m_projectFolder = url;
622 }
623
624 void KdenliveDoc::moveProjectData(KUrl url)
625 {
626     QList <DocClipBase*> list = m_clipManager->documentClipList();
627     //TODO: Also move ladspa effects files
628     for (int i = 0; i < list.count(); i++) {
629         DocClipBase *clip = list.at(i);
630         if (clip->clipType() == TEXT) {
631             // the image for title clip must be moved
632             KUrl oldUrl = clip->fileURL();
633             KUrl newUrl = KUrl(url.path(KUrl::AddTrailingSlash) + "titles/" + oldUrl.fileName());
634             KIO::Job *job = KIO::copy(oldUrl, newUrl);
635             if (KIO::NetAccess::synchronousRun(job, 0)) clip->setProperty("resource", newUrl.path());
636         }
637         QString hash = clip->getClipHash();
638         KUrl oldVideoThumbUrl = KUrl(m_projectFolder.path(KUrl::AddTrailingSlash) + "thumbs/" + hash + ".png");
639         KUrl oldAudioThumbUrl = KUrl(m_projectFolder.path(KUrl::AddTrailingSlash) + "thumbs/" + hash + ".thumb");
640         if (KIO::NetAccess::exists(oldVideoThumbUrl, KIO::NetAccess::SourceSide, 0)) {
641             KUrl newUrl = KUrl(url.path(KUrl::AddTrailingSlash) + "thumbs/" + hash + ".png");
642             KIO::Job *job = KIO::copy(oldVideoThumbUrl, newUrl);
643             KIO::NetAccess::synchronousRun(job, 0);
644         }
645         if (KIO::NetAccess::exists(oldAudioThumbUrl, KIO::NetAccess::SourceSide, 0)) {
646             KUrl newUrl = KUrl(url.path(KUrl::AddTrailingSlash) + "thumbs/" + hash + ".thumb");
647             KIO::Job *job = KIO::copy(oldAudioThumbUrl, newUrl);
648             if (KIO::NetAccess::synchronousRun(job, 0)) clip->refreshThumbUrl();
649         }
650     }
651 }
652
653 const QString &KdenliveDoc::profilePath() const
654 {
655     return m_profile.path;
656 }
657
658 MltVideoProfile KdenliveDoc::mltProfile() const
659 {
660     return m_profile;
661 }
662
663 bool KdenliveDoc::setProfilePath(QString path)
664 {
665     if (path.isEmpty()) path = KdenliveSettings::default_profile();
666     if (path.isEmpty()) path = "dv_pal";
667     m_profile = ProfilesDialog::getVideoProfile(path);
668     bool current_fps = m_fps;
669     if (m_profile.path.isEmpty()) {
670         // Profile not found, use embedded profile
671         QDomElement profileInfo = m_document.elementsByTagName("profileinfo").at(0).toElement();
672         if (profileInfo.isNull()) {
673             KMessageBox::information(kapp->activeWindow(), i18n("Project profile was not found, using default profile."), i18n("Missing Profile"));
674             m_profile = ProfilesDialog::getVideoProfile(KdenliveSettings::default_profile());
675         } else {
676             m_profile.description = profileInfo.attribute("description");
677             m_profile.frame_rate_num = profileInfo.attribute("frame_rate_num").toInt();
678             m_profile.frame_rate_den = profileInfo.attribute("frame_rate_den").toInt();
679             m_profile.width = profileInfo.attribute("width").toInt();
680             m_profile.height = profileInfo.attribute("height").toInt();
681             m_profile.progressive = profileInfo.attribute("progressive").toInt();
682             m_profile.sample_aspect_num = profileInfo.attribute("sample_aspect_num").toInt();
683             m_profile.sample_aspect_den = profileInfo.attribute("sample_aspect_den").toInt();
684             m_profile.display_aspect_num = profileInfo.attribute("display_aspect_num").toInt();
685             m_profile.display_aspect_den = profileInfo.attribute("display_aspect_den").toInt();
686             QString existing = ProfilesDialog::existingProfile(m_profile);
687             if (!existing.isEmpty()) {
688                 m_profile = ProfilesDialog::getVideoProfile(existing);
689                 KMessageBox::information(kapp->activeWindow(), i18n("Project profile not found, replacing with existing one: %1", m_profile.description), i18n("Missing Profile"));
690             } else {
691                 QString newDesc = m_profile.description;
692                 bool ok = true;
693                 while (ok && (newDesc.isEmpty() || ProfilesDialog::existingProfileDescription(newDesc))) {
694                     newDesc = QInputDialog::getText(kapp->activeWindow(), i18n("Existing Profile"), i18n("Your project uses an unknown profile.\nIt uses an existing profile name: %1.\nPlease choose a new name to save it", newDesc), QLineEdit::Normal, newDesc, &ok);
695                 }
696                 if (ok == false) {
697                     // User canceled, use default profile
698                     m_profile = ProfilesDialog::getVideoProfile(KdenliveSettings::default_profile());
699                 } else {
700                     if (newDesc != m_profile.description) {
701                         // Profile description existed, was replaced by new one
702                         m_profile.description = newDesc;
703                     } else {
704                         KMessageBox::information(kapp->activeWindow(), i18n("Project profile was not found, it will be added to your system now."), i18n("Missing Profile"));
705                     }
706                     ProfilesDialog::saveProfile(m_profile);
707                 }
708             }
709             setModified(true);
710         }
711     }
712
713     KdenliveSettings::setProject_display_ratio((double) m_profile.display_aspect_num / m_profile.display_aspect_den);
714     m_fps = (double) m_profile.frame_rate_num / m_profile.frame_rate_den;
715     KdenliveSettings::setProject_fps(m_fps);
716     m_width = m_profile.width;
717     m_height = m_profile.height;
718     kDebug() << "Kdenlive document, init timecode from path: " << path << ",  " << m_fps;
719     if (m_fps / 1.00 == (int)m_fps) m_timecode.setFormat(m_fps);
720     else m_timecode.setFormat(m_fps, true);
721     return (current_fps != m_fps);
722 }
723
724 double KdenliveDoc::dar()
725 {
726     return (double) m_profile.display_aspect_num / m_profile.display_aspect_den;
727 }
728
729 void KdenliveDoc::setThumbsProgress(const QString &message, int progress)
730 {
731     emit progressInfo(message, progress);
732 }
733
734 QUndoStack *KdenliveDoc::commandStack()
735 {
736     return m_commandStack;
737 }
738
739 /*
740 void KdenliveDoc::setRenderer(Render *render) {
741     if (m_render) return;
742     m_render = render;
743     emit progressInfo(i18n("Loading playlist..."), 0);
744     //qApp->processEvents();
745     if (m_render) {
746         m_render->setSceneList(m_document.toString(), m_startPos);
747         kDebug() << "// SETTING SCENE LIST:\n\n" << m_document.toString();
748         checkProjectClips();
749     }
750     emit progressInfo(QString(), -1);
751 }*/
752
753 void KdenliveDoc::checkProjectClips()
754 {
755     if (m_render == NULL) return;
756     m_clipManager->resetProducersList(m_render->producersList());
757 }
758
759 void KdenliveDoc::updatePreviewSettings()
760 {
761     m_clipManager->updatePreviewSettings();
762     m_render->updatePreviewSettings();
763     QList <Mlt::Producer *> prods = m_render->producersList();
764     m_clipManager->resetProducersList(m_render->producersList());
765     qDeleteAll(prods);
766     prods.clear();
767 }
768
769 Render *KdenliveDoc::renderer()
770 {
771     return m_render;
772 }
773
774 void KdenliveDoc::updateClip(const QString id)
775 {
776     emit updateClipDisplay(id);
777 }
778
779 int KdenliveDoc::getFramePos(QString duration)
780 {
781     return m_timecode.getFrameCount(duration);
782 }
783
784 QString KdenliveDoc::producerName(const QString &id)
785 {
786     QString result = "unnamed";
787     QDomNodeList prods = producersList();
788     int ct = prods.count();
789     for (int i = 0; i <  ct ; i++) {
790         QDomElement e = prods.item(i).toElement();
791         if (e.attribute("id") != "black" && e.attribute("id") == id) {
792             result = e.attribute("name");
793             if (result.isEmpty()) result = KUrl(e.attribute("resource")).fileName();
794             break;
795         }
796     }
797     return result;
798 }
799
800 QDomDocument KdenliveDoc::toXml()
801 {
802     return m_document;
803 }
804
805 Timecode KdenliveDoc::timecode() const
806 {
807     return m_timecode;
808 }
809
810 QDomNodeList KdenliveDoc::producersList()
811 {
812     return m_document.elementsByTagName("producer");
813 }
814
815 double KdenliveDoc::projectDuration() const
816 {
817     if (m_render)
818         return GenTime(m_render->getLength(), m_fps).ms() / 1000;
819     else
820         return 0;
821 }
822
823 double KdenliveDoc::fps() const
824 {
825     return m_fps;
826 }
827
828 int KdenliveDoc::width() const
829 {
830     return m_width;
831 }
832
833 int KdenliveDoc::height() const
834 {
835     return m_height;
836 }
837
838 KUrl KdenliveDoc::url() const
839 {
840     return m_url;
841 }
842
843 void KdenliveDoc::setUrl(KUrl url)
844 {
845     m_url = url;
846 }
847
848 void KdenliveDoc::setModified(bool mod)
849 {
850     if (!m_url.isEmpty() && mod && KdenliveSettings::crashrecovery()) {
851         m_autoSaveTimer->start(3000);
852     }
853     if (mod == m_modified) return;
854     m_modified = mod;
855     emit docModified(m_modified);
856 }
857
858 bool KdenliveDoc::isModified() const
859 {
860     return m_modified;
861 }
862
863 const QString KdenliveDoc::description() const
864 {
865     if (m_url.isEmpty())
866         return i18n("Untitled") + " / " + m_profile.description;
867     else
868         return m_url.fileName() + " / " + m_profile.description;
869 }
870
871 void KdenliveDoc::addClip(QDomElement elem, QString clipId, bool createClipItem)
872 {
873     const QString producerId = clipId.section('_', 0, 0);
874     DocClipBase *clip = m_clipManager->getClipById(producerId);
875
876     if (clip == NULL) {
877         elem.setAttribute("id", producerId);
878         QString path = elem.attribute("resource");
879         QString extension;
880         if (elem.attribute("type").toInt() == SLIDESHOW) {
881             extension = KUrl(path).fileName();
882             path = KUrl(path).directory();
883         }
884
885         if (path.isEmpty() == false && QFile::exists(path) == false && elem.attribute("type").toInt() != TEXT && !elem.hasAttribute("placeholder")) {
886             kDebug() << "// FOUND MISSING CLIP: " << path << ", TYPE: " << elem.attribute("type").toInt();
887             const QString size = elem.attribute("file_size");
888             const QString hash = elem.attribute("file_hash");
889             QString newpath;
890             int action = KMessageBox::No;
891             if (!size.isEmpty() && !hash.isEmpty()) {
892                 if (!m_searchFolder.isEmpty()) newpath = searchFileRecursively(m_searchFolder, size, hash);
893                 else action = (KMessageBox::ButtonCode) KMessageBox::questionYesNoCancel(kapp->activeWindow(), i18n("Clip <b>%1</b><br />is invalid, what do you want to do?", path), i18n("File not found"), KGuiItem(i18n("Search automatically")), KGuiItem(i18n("Keep as placeholder")));
894             } else {
895                 if (elem.attribute("type").toInt() == SLIDESHOW) {
896                     int res = KMessageBox::questionYesNoCancel(kapp->activeWindow(), i18n("Clip <b>%1</b><br />is invalid or missing, what do you want to do?", path), i18n("File not found"), KGuiItem(i18n("Search manually")), KGuiItem(i18n("Keep as placeholder")));
897                     if (res == KMessageBox::Yes)
898                         newpath = KFileDialog::getExistingDirectory(KUrl("kfiledialog:///clipfolder"), kapp->activeWindow(), i18n("Looking for %1", path));
899                     else {
900                         // Abort project loading
901                         action = res;
902                     }
903                 } else {
904                     int res = KMessageBox::questionYesNoCancel(kapp->activeWindow(), i18n("Clip <b>%1</b><br />is invalid or missing, what do you want to do?", path), i18n("File not found"), KGuiItem(i18n("Search manually")), KGuiItem(i18n("Keep as placeholder")));
905                     if (res == KMessageBox::Yes)
906                         newpath = KFileDialog::getOpenFileName(KUrl("kfiledialog:///clipfolder"), QString(), kapp->activeWindow(), i18n("Looking for %1", path));
907                     else {
908                         // Abort project loading
909                         action = res;
910                     }
911                 }
912             }
913             if (action == KMessageBox::Yes) {
914                 kDebug() << "// ASKED FOR SRCH CLIP: " << clipId;
915                 m_searchFolder = KFileDialog::getExistingDirectory(KUrl("kfiledialog:///clipfolder"), kapp->activeWindow());
916                 if (!m_searchFolder.isEmpty()) {
917                     newpath = searchFileRecursively(QDir(m_searchFolder), size, hash);
918                 }
919             } else if (action == KMessageBox::Cancel) {
920                 m_abortLoading = true;
921                 return;
922             } else if (action == KMessageBox::No) {
923                 // Keep clip as placeHolder
924                 elem.setAttribute("placeholder", '1');
925             }
926             if (!newpath.isEmpty()) {
927                 if (elem.attribute("type").toInt() == SLIDESHOW) newpath.append('/' + extension);
928                 elem.setAttribute("resource", newpath);
929                 setNewClipResource(clipId, newpath);
930                 setModified(true);
931             }
932         }
933         clip = new DocClipBase(m_clipManager, elem, producerId);
934         m_clipManager->addClip(clip);
935     }
936
937     if (createClipItem) {
938         emit addProjectClip(clip);
939         //qApp->processEvents();
940     }
941 }
942
943 void KdenliveDoc::setNewClipResource(const QString &id, const QString &path)
944 {
945     QDomNodeList prods = m_document.elementsByTagName("producer");
946     int maxprod = prods.count();
947     for (int i = 0; i < maxprod; i++) {
948         QDomNode m = prods.at(i);
949         QString prodId = m.toElement().attribute("id");
950         if (prodId == id || prodId.startsWith(id + '_')) {
951             QDomNodeList params = m.childNodes();
952             for (int j = 0; j < params.count(); j++) {
953                 QDomElement e = params.item(j).toElement();
954                 if (e.attribute("name") == "resource") {
955                     e.firstChild().setNodeValue(path);
956                     break;
957                 }
958             }
959         }
960     }
961 }
962
963 QString KdenliveDoc::searchFileRecursively(const QDir &dir, const QString &matchSize, const QString &matchHash) const
964 {
965     QString foundFileName;
966     QByteArray fileData;
967     QByteArray fileHash;
968     QStringList filesAndDirs = dir.entryList(QDir::Files | QDir::Readable);
969     for (int i = 0; i < filesAndDirs.size() && foundFileName.isEmpty(); i++) {
970         QFile file(dir.absoluteFilePath(filesAndDirs.at(i)));
971         if (file.open(QIODevice::ReadOnly)) {
972             if (QString::number(file.size()) == matchSize) {
973                 /*
974                 * 1 MB = 1 second per 450 files (or faster)
975                 * 10 MB = 9 seconds per 450 files (or faster)
976                 */
977                 if (file.size() > 1000000 * 2) {
978                     fileData = file.read(1000000);
979                     if (file.seek(file.size() - 1000000))
980                         fileData.append(file.readAll());
981                 } else
982                     fileData = file.readAll();
983                 file.close();
984                 fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
985                 if (QString(fileHash.toHex()) == matchHash)
986                     return file.fileName();
987             }
988         }
989         kDebug() << filesAndDirs.at(i) << file.size() << fileHash.toHex();
990     }
991     filesAndDirs = dir.entryList(QDir::Dirs | QDir::Readable | QDir::Executable | QDir::NoDotAndDotDot);
992     for (int i = 0; i < filesAndDirs.size() && foundFileName.isEmpty(); i++) {
993         foundFileName = searchFileRecursively(dir.absoluteFilePath(filesAndDirs.at(i)), matchSize, matchHash);
994         if (!foundFileName.isEmpty())
995             break;
996     }
997     return foundFileName;
998 }
999
1000 void KdenliveDoc::addClipInfo(QDomElement elem, QDomElement orig, QString clipId)
1001 {
1002     DocClipBase *clip = m_clipManager->getClipById(clipId);
1003     if (clip == NULL) {
1004         addClip(elem, clipId, false);
1005     } else {
1006         QMap <QString, QString> properties;
1007         QDomNamedNodeMap attributes = elem.attributes();
1008         QString attrname;
1009         for (int i = 0; i < attributes.count(); i++) {
1010             attrname = attributes.item(i).nodeName();
1011             if (attrname != "resource")
1012                 properties.insert(attrname, attributes.item(i).nodeValue());
1013             kDebug() << attrname << " = " << attributes.item(i).nodeValue();
1014         }
1015         clip->setProperties(properties);
1016         emit addProjectClip(clip, false);
1017     }
1018     if (orig != QDomElement()) {
1019         QMap<QString, QString> meta;
1020         QDomNode m = orig.firstChild();
1021         while (!m.isNull()) {
1022             QString name = m.toElement().attribute("name");
1023             if (name.startsWith("meta.attr")) {
1024                 meta.insert(name.section('.', 2, 3), m.firstChild().nodeValue());
1025             }
1026             m = m.nextSibling();
1027         }
1028         if (!meta.isEmpty()) {
1029             if (clip == NULL) clip = m_clipManager->getClipById(clipId);
1030             if (clip) clip->setMetadata(meta);
1031         }
1032     }
1033 }
1034
1035
1036 void KdenliveDoc::deleteClip(const QString &clipId)
1037 {
1038     emit signalDeleteProjectClip(clipId);
1039 }
1040
1041 void KdenliveDoc::slotAddClipList(const KUrl::List urls, const QString group, const QString &groupId)
1042 {
1043     m_clipManager->slotAddClipList(urls, group, groupId);
1044     //emit selectLastAddedClip(QString::number(m_clipManager->lastClipId()));
1045     setModified(true);
1046 }
1047
1048
1049 void KdenliveDoc::slotAddClipFile(const KUrl url, const QString group, const QString &groupId)
1050 {
1051     m_clipManager->slotAddClipFile(url, group, groupId);
1052     emit selectLastAddedClip(QString::number(m_clipManager->lastClipId()));
1053     setModified(true);
1054 }
1055
1056 const QString KdenliveDoc::getFreeClipId()
1057 {
1058     return QString::number(m_clipManager->getFreeClipId());
1059 }
1060
1061 DocClipBase *KdenliveDoc::getBaseClip(const QString &clipId)
1062 {
1063     return m_clipManager->getClipById(clipId);
1064 }
1065
1066 void KdenliveDoc::slotCreateXmlClip(const QString &name, const QDomElement xml, QString group, const QString &groupId)
1067 {
1068     m_clipManager->slotAddXmlClipFile(name, xml, group, groupId);
1069     setModified(true);
1070     emit selectLastAddedClip(QString::number(m_clipManager->lastClipId()));
1071 }
1072
1073 void KdenliveDoc::slotCreateColorClip(const QString &name, const QString &color, const QString &duration, QString group, const QString &groupId)
1074 {
1075     m_clipManager->slotAddColorClipFile(name, color, duration, group, groupId);
1076     setModified(true);
1077     emit selectLastAddedClip(QString::number(m_clipManager->lastClipId()));
1078 }
1079
1080 void KdenliveDoc::slotCreateSlideshowClipFile(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, QString group, const QString &groupId)
1081 {
1082     m_clipManager->slotAddSlideshowClipFile(name, path, count, duration, loop, fade, luma_duration, luma_file, softness, group, groupId);
1083     setModified(true);
1084     emit selectLastAddedClip(QString::number(m_clipManager->lastClipId()));
1085 }
1086
1087 void KdenliveDoc::slotCreateTextClip(QString group, const QString &groupId, const QString &templatePath)
1088 {
1089     QString titlesFolder = projectFolder().path(KUrl::AddTrailingSlash) + "titles/";
1090     KStandardDirs::makeDir(titlesFolder);
1091     TitleWidget *dia_ui = new TitleWidget(templatePath, m_timecode, titlesFolder, m_render, kapp->activeWindow());
1092     if (dia_ui->exec() == QDialog::Accepted) {
1093         m_clipManager->slotAddTextClipFile(i18n("Title clip"), dia_ui->outPoint(), dia_ui->xml().toString(), group, groupId);
1094         setModified(true);
1095         emit selectLastAddedClip(QString::number(m_clipManager->lastClipId()));
1096     }
1097     delete dia_ui;
1098 }
1099
1100 void KdenliveDoc::slotCreateTextTemplateClip(QString group, const QString &groupId, KUrl path)
1101 {
1102     QString titlesFolder = projectFolder().path(KUrl::AddTrailingSlash) + "titles/";
1103     if (path.isEmpty()) {
1104         path = KFileDialog::getOpenUrl(KUrl(titlesFolder), "*.kdenlivetitle", kapp->activeWindow(), i18n("Enter Template Path"));
1105     }
1106
1107     if (path.isEmpty()) return;
1108
1109     //TODO: rewrite with new title system (just set resource)
1110     m_clipManager->slotAddTextTemplateClip(i18n("Template title clip"), path, group, groupId);
1111     setModified(true);
1112     emit selectLastAddedClip(QString::number(m_clipManager->lastClipId()));
1113 }
1114
1115 int KdenliveDoc::tracksCount() const
1116 {
1117     return m_tracksList.count();
1118 }
1119
1120 TrackInfo KdenliveDoc::trackInfoAt(int ix) const
1121 {
1122     return m_tracksList.at(ix);
1123 }
1124
1125 void KdenliveDoc::switchTrackAudio(int ix, bool hide)
1126 {
1127     m_tracksList[ix].isMute = hide; // !m_tracksList.at(ix).isMute;
1128 }
1129
1130 void KdenliveDoc::switchTrackLock(int ix, bool lock)
1131 {
1132     m_tracksList[ix].isLocked = lock;
1133 }
1134
1135 bool KdenliveDoc::isTrackLocked(int ix) const
1136 {
1137     return m_tracksList.at(ix).isLocked;
1138 }
1139
1140 void KdenliveDoc::switchTrackVideo(int ix, bool hide)
1141 {
1142     m_tracksList[ix].isBlind = hide; // !m_tracksList.at(ix).isBlind;
1143 }
1144
1145 void KdenliveDoc::insertTrack(int ix, TrackInfo type)
1146 {
1147     if (ix == -1) m_tracksList << type;
1148     else m_tracksList.insert(ix, type);
1149 }
1150
1151 void KdenliveDoc::deleteTrack(int ix)
1152 {
1153     m_tracksList.removeAt(ix);
1154 }
1155
1156 void KdenliveDoc::setTrackType(int ix, TrackInfo type)
1157 {
1158     m_tracksList[ix].type = type.type;
1159     m_tracksList[ix].isMute = type.isMute;
1160     m_tracksList[ix].isBlind = type.isBlind;
1161     m_tracksList[ix].isLocked = type.isLocked;
1162     m_tracksList[ix].trackName = type.trackName;
1163 }
1164
1165 const QList <TrackInfo> KdenliveDoc::tracksList() const
1166 {
1167     return m_tracksList;
1168 }
1169
1170 QPoint KdenliveDoc::getTracksCount() const
1171 {
1172     int audio = 0;
1173     int video = 0;
1174     foreach(const TrackInfo & info, m_tracksList) {
1175         if (info.type == VIDEOTRACK) video++;
1176         else audio++;
1177     }
1178     return QPoint(video, audio);
1179 }
1180
1181 void KdenliveDoc::cachePixmap(const QString &fileId, const QPixmap &pix) const
1182 {
1183     pix.save(m_projectFolder.path(KUrl::AddTrailingSlash) + "thumbs/" + fileId + ".png");
1184 }
1185
1186 QString KdenliveDoc::getLadspaFile() const
1187 {
1188     int ct = 0;
1189     QString counter = QString::number(ct).rightJustified(5, '0', false);
1190     while (QFile::exists(m_projectFolder.path(KUrl::AddTrailingSlash) + "ladspa/" + counter + ".ladspa")) {
1191         ct++;
1192         counter = QString::number(ct).rightJustified(5, '0', false);
1193     }
1194     return m_projectFolder.path(KUrl::AddTrailingSlash) + "ladspa/" + counter + ".ladspa";
1195 }
1196
1197 bool KdenliveDoc::checkDocumentClips(QDomNodeList infoproducers)
1198 {
1199     DocumentChecker d(infoproducers, m_document);
1200     return (d.hasMissingClips() == false);
1201
1202     /*    int clipType;
1203         QDomElement e;
1204         QString id;
1205         QString resource;
1206         QList <QDomElement> missingClips;
1207         for (int i = 0; i < infoproducers.count(); i++) {
1208             e = infoproducers.item(i).toElement();
1209             clipType = e.attribute("type").toInt();
1210             if (clipType == COLOR) continue;
1211             if (clipType == TEXT) {
1212                 //TODO: Check is clip template is missing (xmltemplate) or hash changed
1213                 continue;
1214             }
1215             id = e.attribute("id");
1216             resource = e.attribute("resource");
1217             if (clipType == SLIDESHOW) resource = KUrl(resource).directory();
1218             if (!KIO::NetAccess::exists(KUrl(resource), KIO::NetAccess::SourceSide, 0)) {
1219                 // Missing clip found
1220                 missingClips.append(e);
1221             } else {
1222                 // Check if the clip has changed
1223                 if (clipType != SLIDESHOW && e.hasAttribute("file_hash")) {
1224                     if (e.attribute("file_hash") != DocClipBase::getHash(e.attribute("resource")))
1225                         e.removeAttribute("file_hash");
1226                 }
1227             }
1228         }
1229         if (missingClips.isEmpty()) return true;
1230         DocumentChecker d(missingClips, m_document);
1231         return (d.exec() == QDialog::Accepted);*/
1232 }
1233
1234 void KdenliveDoc::setDocumentProperty(const QString &name, const QString &value)
1235 {
1236     m_documentProperties[name] = value;
1237 }
1238
1239 const QString KdenliveDoc::getDocumentProperty(const QString &name) const
1240 {
1241     return m_documentProperties.value(name);
1242 }
1243
1244 QMap <QString, QString> KdenliveDoc::getRenderProperties() const
1245 {
1246     QMap <QString, QString> renderProperties;
1247     QMapIterator<QString, QString> i(m_documentProperties);
1248     while (i.hasNext()) {
1249         i.next();
1250         if (i.key().startsWith("render")) renderProperties.insert(i.key(), i.value());
1251     }
1252     return renderProperties;
1253 }
1254
1255 #include "kdenlivedoc.moc"
1256