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