1 /***************************************************************************
2 * Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
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. *
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. *
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 ***************************************************************************/
21 #include "kdenlivedoc.h"
22 #include "docclipbase.h"
23 #include "profilesdialog.h"
24 #include "kdenlivesettings.h"
26 #include "clipmanager.h"
27 #include "titlewidget.h"
28 #include "mainwindow.h"
31 #include <KStandardDirs>
32 #include <KMessageBox>
34 #include <KFileDialog>
35 #include <KIO/NetAccess>
36 #include <KIO/CopyJob>
37 #include <KApplication>
39 #include <QCryptographicHash>
42 #include <mlt++/Mlt.h>
45 KdenliveDoc::KdenliveDoc(const KUrl &url, const KUrl &projectFolder, QUndoGroup *undoGroup, const QString &profileName, const QPoint tracks, Render *render, MainWindow *parent) :
52 m_commandStack(new QUndoStack(undoGroup)),
54 m_projectFolder(projectFolder),
55 m_documentLoadingStep(0.0),
56 m_documentLoadingProgress(0),
57 m_abortLoading(false),
61 m_clipManager = new ClipManager(this);
62 m_autoSaveTimer = new QTimer(this);
63 m_autoSaveTimer->setSingleShot(true);
66 bool success = KIO::NetAccess::download(url.path(), tmpFile, parent);
70 success = m_document.setContent(&file, false, &errorMsg);
72 if (success == false) {
73 // File is corrupted, warn user
74 KMessageBox::error(parent, errorMsg);
76 } else KMessageBox::error(parent, KIO::NetAccess::lastErrorString());
79 QDomNode infoXmlNode = m_document.elementsByTagName("kdenlivedoc").at(0);
80 QDomNode westley = m_document.elementsByTagName("westley").at(0);
81 if (!infoXmlNode.isNull()) {
82 QDomElement infoXml = infoXmlNode.toElement();
83 double version = infoXml.attribute("version").toDouble();
85 // Upgrade old Kdenlive documents to current version
86 if (!convertDocument(version)) {
88 m_document = createEmptyDocument(tracks.x(), tracks.y());
89 setProfilePath(profileName);
92 * read again <kdenlivedoc> and <westley> to get all the new
93 * stuff (convertDocument() can now do anything without breaking
96 infoXmlNode = m_document.elementsByTagName("kdenlivedoc").at(0);
97 infoXml = infoXmlNode.toElement();
98 version = infoXml.attribute("version").toDouble();
99 westley = m_document.elementsByTagName("westley").at(0);
101 QString profilePath = infoXml.attribute("profile");
102 QString projectFolderPath = infoXml.attribute("projectfolder");
103 if (!projectFolderPath.isEmpty()) m_projectFolder = KUrl(projectFolderPath);
105 if (m_projectFolder.isEmpty() || !KIO::NetAccess::exists(m_projectFolder.path(), KIO::NetAccess::DestinationSide, parent)) {
106 // Make sure the project folder is usable
107 KMessageBox::information(parent, i18n("Document project folder is invalid, setting it to the default one: %1", KdenliveSettings::defaultprojectfolder()));
108 m_projectFolder = KUrl(KdenliveSettings::defaultprojectfolder());
110 m_startPos = infoXml.attribute("position").toInt();
111 m_zoom = infoXml.attribute("zoom", "7").toInt();
112 m_zoneStart = infoXml.attribute("zonein", "0").toInt();
113 m_zoneEnd = infoXml.attribute("zoneout", "100").toInt();
114 setProfilePath(profilePath);
118 QDomNode tracksinfo = m_document.elementsByTagName("tracksinfo").at(0);
119 TrackInfo projectTrack;
120 if (!tracksinfo.isNull()) {
121 QDomNodeList trackslist = tracksinfo.childNodes();
122 int maxchild = trackslist.count();
123 for (int k = 0; k < maxchild; k++) {
124 e = trackslist.at(k).toElement();
125 if (e.tagName() == "trackinfo") {
126 if (e.attribute("type") == "audio") projectTrack.type = AUDIOTRACK;
127 else projectTrack.type = VIDEOTRACK;
128 projectTrack.isMute = e.attribute("mute").toInt();
129 projectTrack.isBlind = e.attribute("blind").toInt();
130 projectTrack.isLocked = e.attribute("locked").toInt();
131 m_tracksList.append(projectTrack);
134 westley.removeChild(tracksinfo);
137 QDomNodeList producers = m_document.elementsByTagName("producer");
138 QDomNodeList infoproducers = m_document.elementsByTagName("kdenlive_producer");
139 const int max = producers.count();
140 const int infomax = infoproducers.count();
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"));
149 m_documentLoadingStep = 100.0 / (max + infomax + m_document.elementsByTagName("entry").count());
150 parent->slotGotProgressInfo(i18n("Loading project clips"), (int) m_documentLoadingProgress);
154 for (int i = 0; i < infomax && !m_abortLoading; i++) {
155 e = infoproducers.item(i).cloneNode().toElement();
156 if (m_documentLoadingStep > 0) {
157 m_documentLoadingProgress += m_documentLoadingStep;
158 parent->slotGotProgressInfo(QString(), (int) m_documentLoadingProgress);
159 //qApp->processEvents();
161 QString prodId = e.attribute("id");
162 if (!e.isNull() && prodId != "black" && !prodId.startsWith("slowmotion") && !m_abortLoading) {
163 e.setTagName("producer");
164 // Get MLT's original producer properties
166 for (int j = 0; j < max; j++) {
167 QDomElement o = producers.item(j).cloneNode().toElement();
168 QString origId = o.attribute("id").section('_', 0, 0);
169 if (origId == prodId) {
174 addClipInfo(e, orig, prodId);
175 kDebug() << "// NLIVE PROD: " << prodId;
178 if (m_abortLoading) {
179 //parent->slotGotProgressInfo(i18n("File %1 is not a Kdenlive project file."), 100);
180 emit resetProjectList();
183 m_tracksList.clear();
184 kWarning() << "Aborted loading of: " << url.path();
185 m_document = createEmptyDocument(KdenliveSettings::videotracks(), KdenliveSettings::audiotracks());
186 setProfilePath(KdenliveSettings::default_profile());
187 m_clipManager->clear();
189 QDomNode markers = m_document.elementsByTagName("markers").at(0);
190 if (!markers.isNull()) {
191 QDomNodeList markerslist = markers.childNodes();
192 int maxchild = markerslist.count();
193 for (int k = 0; k < maxchild; k++) {
194 e = markerslist.at(k).toElement();
195 if (e.tagName() == "marker") {
196 m_clipManager->getClipById(e.attribute("id"))->addSnapMarker(GenTime(e.attribute("time").toDouble()), e.attribute("comment"));
199 westley.removeChild(markers);
201 m_document.removeChild(infoXmlNode);
202 kDebug() << "Reading file: " << url.path() << ", found clips: " << producers.count();
206 parent->slotGotProgressInfo(i18n("File %1 is not a Kdenlive project file."), 100);
207 kWarning() << " NO KDENLIVE INFO FOUND IN FILE: " << url.path();
208 m_document = createEmptyDocument(KdenliveSettings::videotracks(), KdenliveSettings::audiotracks());
210 setProfilePath(KdenliveSettings::default_profile());
212 KIO::NetAccess::removeTempFile(tmpFile);
214 parent->slotGotProgressInfo(i18n("File %1 is not a Kdenlive project file."), 100);
216 m_document = createEmptyDocument(KdenliveSettings::videotracks(), KdenliveSettings::audiotracks());
217 setProfilePath(KdenliveSettings::default_profile());
220 m_document = createEmptyDocument(tracks.x(), tracks.y());
221 setProfilePath(profileName);
223 if (m_projectFolder.isEmpty()) m_projectFolder = KUrl(KdenliveSettings::defaultprojectfolder());
225 // make sure that the necessary folders exist
226 KStandardDirs::makeDir(m_projectFolder.path() + "/titles/");
227 KStandardDirs::makeDir(m_projectFolder.path() + "/thumbs/");
228 KStandardDirs::makeDir(m_projectFolder.path() + "/ladspa/");
230 kDebug() << "KDEnlive document, init timecode: " << m_fps;
231 if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
232 else m_timecode.setFormat((int) m_fps);
234 //kDebug() << "// SETTING SCENE LIST:\n\n" << m_document.toString();
235 connect(m_autoSaveTimer, SIGNAL(timeout()), this, SLOT(slotAutoSave()));
239 KdenliveDoc::~KdenliveDoc()
241 delete m_commandStack;
242 delete m_clipManager;
243 delete m_autoSaveTimer;
245 m_autosave->remove();
250 void KdenliveDoc::setSceneList()
252 m_render->setSceneList(m_document.toString(), m_startPos);
253 //checkProjectClips();
256 QDomDocument KdenliveDoc::createEmptyDocument(const int videotracks, const int audiotracks)
258 // Creating new document
260 QDomElement westley = doc.createElement("westley");
261 doc.appendChild(westley);
264 TrackInfo videoTrack;
265 videoTrack.type = VIDEOTRACK;
266 videoTrack.isMute = false;
267 videoTrack.isBlind = false;
268 videoTrack.isLocked = false;
270 TrackInfo audioTrack;
271 audioTrack.type = AUDIOTRACK;
272 audioTrack.isMute = false;
273 audioTrack.isBlind = true;
274 audioTrack.isLocked = false;
276 QDomElement tractor = doc.createElement("tractor");
277 tractor.setAttribute("id", "maintractor");
278 QDomElement multitrack = doc.createElement("multitrack");
279 QDomElement playlist = doc.createElement("playlist");
280 playlist.setAttribute("id", "black_track");
281 westley.appendChild(playlist);
285 int total = audiotracks + videotracks + 1;
287 for (int i = 1; i < total; i++) {
288 QDomElement playlist = doc.createElement("playlist");
289 playlist.setAttribute("id", "playlist" + QString::number(i));
290 westley.appendChild(playlist);
293 QDomElement track0 = doc.createElement("track");
294 track0.setAttribute("producer", "black_track");
295 tractor.appendChild(track0);
297 // create audio tracks
298 for (int i = 1; i < audiotracks + 1; i++) {
299 QDomElement track = doc.createElement("track");
300 track.setAttribute("producer", "playlist" + QString::number(i));
301 track.setAttribute("hide", "video");
302 tractor.appendChild(track);
303 m_tracksList.append(audioTrack);
306 // create video tracks
307 for (int i = audiotracks + 1; i < total; i++) {
308 QDomElement track = doc.createElement("track");
309 track.setAttribute("producer", "playlist" + QString::number(i));
310 tractor.appendChild(track);
311 m_tracksList.append(videoTrack);
314 for (int i = 2; i < total ; i++) {
315 QDomElement transition = doc.createElement("transition");
316 transition.setAttribute("always_active", "1");
318 QDomElement property = doc.createElement("property");
319 property.setAttribute("name", "a_track");
320 QDomText value = doc.createTextNode(QString::number(1));
321 property.appendChild(value);
322 transition.appendChild(property);
324 property = doc.createElement("property");
325 property.setAttribute("name", "b_track");
326 value = doc.createTextNode(QString::number(i));
327 property.appendChild(value);
328 transition.appendChild(property);
330 property = doc.createElement("property");
331 property.setAttribute("name", "mlt_service");
332 value = doc.createTextNode("mix");
333 property.appendChild(value);
334 transition.appendChild(property);
336 property = doc.createElement("property");
337 property.setAttribute("name", "combine");
338 value = doc.createTextNode("1");
339 property.appendChild(value);
340 transition.appendChild(property);
342 property = doc.createElement("property");
343 property.setAttribute("name", "internal_added");
344 value = doc.createTextNode("237");
345 property.appendChild(value);
346 transition.appendChild(property);
347 tractor.appendChild(transition);
349 westley.appendChild(tractor);
354 void KdenliveDoc::syncGuides(QList <Guide *> guides)
357 QDomElement guideNode = m_guidesXml.createElement("guides");
358 m_guidesXml.appendChild(guideNode);
361 for (int i = 0; i < guides.count(); i++) {
362 e = m_guidesXml.createElement("guide");
363 e.setAttribute("time", guides.at(i)->position().ms() / 1000);
364 e.setAttribute("comment", guides.at(i)->label());
365 guideNode.appendChild(e);
367 emit guidesUpdated();
370 QDomElement KdenliveDoc::guidesXml() const
372 return m_guidesXml.documentElement();
375 void KdenliveDoc::slotAutoSave()
377 if (m_render && m_autosave) {
378 if (!m_autosave->isOpen() && !m_autosave->open(QIODevice::ReadWrite)) {
379 // show error: could not open the autosave file
380 kDebug() << "ERROR; CANNOT CREATE AUTOSAVE FILE";
382 kDebug() << "// AUTOSAVE FILE: " << m_autosave->fileName();
384 if (KdenliveSettings::dropbframes()) {
385 KdenliveSettings::setDropbframes(false);
386 m_clipManager->updatePreviewSettings();
387 doc = m_render->sceneList();
388 KdenliveSettings::setDropbframes(true);
389 m_clipManager->updatePreviewSettings();
390 } else doc = m_render->sceneList();
391 saveSceneList(m_autosave->fileName(), doc);
395 void KdenliveDoc::setZoom(int factor)
400 int KdenliveDoc::zoom() const
405 bool KdenliveDoc::convertDocument(double version)
407 kDebug() << "Opening a document with version " << version;
408 const double current_version = 0.82;
410 if (version == current_version) return true;
412 if (version > current_version) {
413 kDebug() << "Unable to open document with version " << version;
414 KMessageBox::sorry(kapp->activeWindow(), i18n("This project type is unsupported (version %1) and can't be loaded.\nPlease consider upgrading you Kdenlive version.", version), i18n("Unable to open project"));
418 // Opening a old Kdenlive document
419 if (version == 0.5 || version == 0.7) {
420 KMessageBox::sorry(kapp->activeWindow(), i18n("This project type is unsupported (version %1) and can't be loaded.", version), i18n("Unable to open project"));
421 kDebug() << "Unable to open document with version " << version;
422 // TODO: convert 0.7 (0.5?) files to the new document format.
428 if (version == 0.81) {
429 // Add correct tracks info
430 QDomNode kdenlivedoc = m_document.elementsByTagName("kdenlivedoc").at(0);
431 QDomElement infoXml = kdenlivedoc.toElement();
432 infoXml.setAttribute("version", current_version);
433 QString currentTrackOrder = infoXml.attribute("tracks");
434 QDomElement tracksinfo = m_document.createElement("tracksinfo");
435 for (int i = 0; i < currentTrackOrder.size(); i++) {
436 QDomElement trackinfo = m_document.createElement("trackinfo");
437 if (currentTrackOrder.data()[i] == 'a') {
438 trackinfo.setAttribute("type", "audio");
439 trackinfo.setAttribute("blind", true);
440 } else trackinfo.setAttribute("blind", false);
441 trackinfo.setAttribute("mute", false);
442 trackinfo.setAttribute("locked", false);
443 tracksinfo.appendChild(trackinfo);
445 infoXml.appendChild(tracksinfo);
449 if (version == 0.8) {
450 // Add the tracks information
451 QDomNodeList tracks = m_document.elementsByTagName("track");
452 int max = tracks.count();
454 QDomNode kdenlivedoc = m_document.elementsByTagName("kdenlivedoc").at(0);
455 QDomElement infoXml = kdenlivedoc.toElement();
456 infoXml.setAttribute("version", current_version);
457 QDomElement tracksinfo = m_document.createElement("tracksinfo");
459 for (int i = 0; i < max; i++) {
460 QDomElement trackinfo = m_document.createElement("trackinfo");
461 QDomElement t = tracks.at(i).toElement();
462 if (t.attribute("hide") == "video") {
463 trackinfo.setAttribute("type", "audio");
464 trackinfo.setAttribute("blind", true);
465 } else trackinfo.setAttribute("blind", false);
466 trackinfo.setAttribute("mute", false);
467 trackinfo.setAttribute("locked", false);
468 if (t.attribute("producer") != "black_track") tracksinfo.appendChild(trackinfo);
470 infoXml.appendChild(tracksinfo);
474 QDomNode westley = m_document.elementsByTagName("westley").at(1);
475 QDomNode tractor = m_document.elementsByTagName("tractor").at(0);
476 QDomNode kdenlivedoc = m_document.elementsByTagName("kdenlivedoc").at(0);
477 QDomElement kdenlivedoc_old = kdenlivedoc.cloneNode(true).toElement(); // Needed for folders
478 QDomElement infoXml = kdenlivedoc.toElement();
479 infoXml.setAttribute("version", current_version);
480 QDomNode multitrack = m_document.elementsByTagName("multitrack").at(0);
481 QDomNodeList playlists = m_document.elementsByTagName("playlist");
483 QDomNode props = m_document.elementsByTagName("properties").at(0).toElement();
484 QString profile = props.toElement().attribute("videoprofile");
485 m_startPos = props.toElement().attribute("timeline_position").toInt();
486 if (profile == "dv_wide") profile = "dv_pal_wide";
488 // move playlists outside of tractor and add the tracks instead
489 int max = playlists.count();
490 if (westley.isNull()) {
491 westley = m_document.createElement("westley");
492 m_document.documentElement().appendChild(westley);
494 if (tractor.isNull()) {
495 kDebug() << "// NO WESTLEY PLAYLIST, building empty one";
496 QDomElement blank_tractor = m_document.createElement("tractor");
497 westley.appendChild(blank_tractor);
498 QDomElement blank_playlist = m_document.createElement("playlist");
499 blank_playlist.setAttribute("id", "black_track");
500 westley.insertBefore(blank_playlist, QDomNode());
501 QDomElement blank_track = m_document.createElement("track");
502 blank_track.setAttribute("producer", "black_track");
503 blank_tractor.appendChild(blank_track);
505 QDomNodeList kdenlivetracks = m_document.elementsByTagName("kdenlivetrack");
506 for (int i = 0; i < kdenlivetracks.count(); i++) {
507 blank_playlist = m_document.createElement("playlist");
508 blank_playlist.setAttribute("id", "playlist" + QString::number(i));
509 westley.insertBefore(blank_playlist, QDomNode());
510 blank_track = m_document.createElement("track");
511 blank_track.setAttribute("producer", "playlist" + QString::number(i));
512 blank_tractor.appendChild(blank_track);
513 if (kdenlivetracks.at(i).toElement().attribute("cliptype") == "Sound") {
514 blank_playlist.setAttribute("hide", "video");
515 blank_track.setAttribute("hide", "video");
518 } else for (int i = 0; i < max; i++) {
519 QDomNode n = playlists.at(i);
520 westley.insertBefore(n, QDomNode());
521 QDomElement pl = n.toElement();
522 QDomElement track = m_document.createElement("track");
523 QString trackType = pl.attribute("hide");
524 if (!trackType.isEmpty())
525 track.setAttribute("hide", trackType);
526 QString playlist_id = pl.attribute("id");
527 if (playlist_id.isEmpty()) {
528 playlist_id = "black_track";
529 pl.setAttribute("id", playlist_id);
531 track.setAttribute("producer", playlist_id);
532 //tractor.appendChild(track);
533 #define KEEP_TRACK_ORDER 1
534 #ifdef KEEP_TRACK_ORDER
535 tractor.insertAfter(track, QDomNode());
537 // Insert the new track in an order that hopefully matches the 3 video, then 2 audio tracks of Kdenlive 0.7.0
538 // insertion sort - O( tracks*tracks )
539 // Note, this breaks _all_ transitions - but you can move them up and down afterwards.
540 QDomElement tractor_elem = tractor.toElement();
541 if (! tractor_elem.isNull()) {
542 QDomNodeList tracks = tractor_elem.elementsByTagName("track");
543 int size = tracks.size();
545 tractor.insertAfter(track, QDomNode());
547 bool inserted = false;
548 for (int i = 0; i < size; ++i) {
549 QDomElement track_elem = tracks.at(i).toElement();
550 if (track_elem.isNull()) {
551 tractor.insertAfter(track, QDomNode());
555 kDebug() << "playlist_id: " << playlist_id << " producer:" << track_elem.attribute("producer");
556 if (playlist_id < track_elem.attribute("producer")) {
557 tractor.insertBefore(track, track_elem);
563 // Reach here, no insertion, insert last
565 tractor.insertAfter(track, QDomNode());
569 kWarning() << "tractor was not a QDomElement";
570 tractor.insertAfter(track, QDomNode());
574 tractor.removeChild(multitrack);
576 //Â audio track mixing transitions should not be added to track view, so add required attribute
577 QDomNodeList transitions = m_document.elementsByTagName("transition");
578 max = transitions.count();
579 for (int i = 0; i < max; i++) {
580 QDomElement tr = transitions.at(i).toElement();
581 if (tr.attribute("combine") == "1" && tr.attribute("mlt_service") == "mix") {
582 QDomElement property = m_document.createElement("property");
583 property.setAttribute("name", "internal_added");
584 QDomText value = m_document.createTextNode("237");
585 property.appendChild(value);
586 tr.appendChild(property);
587 property = m_document.createElement("property");
588 property.setAttribute("name", "mlt_service");
589 value = m_document.createTextNode("mix");
590 property.appendChild(value);
591 tr.appendChild(property);
593 // convert transition
594 QDomNamedNodeMap attrs = tr.attributes();
595 for (int j = 0; j < attrs.count(); j++) {
596 QString attrName = attrs.item(j).nodeName();
597 if (attrName != "in" && attrName != "out" && attrName != "id") {
598 QDomElement property = m_document.createElement("property");
599 property.setAttribute("name", attrName);
600 QDomText value = m_document.createTextNode(attrs.item(j).nodeValue());
601 property.appendChild(value);
602 tr.appendChild(property);
608 //Â move transitions after tracks
609 for (int i = 0; i < max; i++) {
610 tractor.insertAfter(transitions.at(0), QDomNode());
613 // Fix filters format
614 QDomNodeList entries = m_document.elementsByTagName("entry");
615 max = entries.count();
616 for (int i = 0; i < max; i++) {
619 QDomNode m = entries.at(i).firstChild();
620 while (!m.isNull()) {
621 if (m.toElement().tagName() == "filter") {
622 QDomElement filt = m.toElement();
623 QDomNamedNodeMap attrs = filt.attributes();
624 QString current_id = filt.attribute("kdenlive_id");
625 if (current_id != last_id) {
627 last_id = current_id;
629 QDomElement e = m_document.createElement("property");
630 e.setAttribute("name", "kdenlive_ix");
631 QDomText value = m_document.createTextNode(QString::number(effectix));
632 e.appendChild(value);
634 for (int j = 0; j < attrs.count(); j++) {
635 QDomAttr a = attrs.item(j).toAttr();
637 kDebug() << " FILTER; adding :" << a.name() << ":" << a.value();
638 QDomElement e = m_document.createElement("property");
639 e.setAttribute("name", a.name());
640 QDomText value = m_document.createTextNode(a.value());
641 e.appendChild(value);
652 QDomNodeList filters = m_document.elementsByTagName("filter");
653 max = filters.count();
656 for (int i = 0; i < max; i++) {
657 QDomElement filt = filters.at(i).toElement();
658 QDomNamedNodeMap attrs = filt.attributes();
659 QString current_id = filt.attribute("kdenlive_id");
660 if (current_id != last_id) {
662 last_id = current_id;
664 QDomElement e = m_document.createElement("property");
665 e.setAttribute("name", "kdenlive_ix");
666 QDomText value = m_document.createTextNode(QString::number(1));
667 e.appendChild(value);
669 for (int j = 0; j < attrs.count(); j++) {
670 QDomAttr a = attrs.item(j).toAttr();
672 kDebug() << " FILTER; adding :" << a.name() << ":" << a.value();
673 QDomElement e = m_document.createElement("property");
674 e.setAttribute("name", a.name());
675 QDomText value = m_document.createTextNode(a.value());
676 e.appendChild(value);
683 QDomNodeList producers = westley.toElement().elementsByTagName("producer");
684 max = producers.count();
685 for (int i = 0; i < max; i++) {
686 QDomElement prod = producers.at(i).toElement();
687 if (prod.attribute("mlt_service") == "framebuffer") {
688 QString slowmotionprod = prod.attribute("resource");
689 slowmotionprod.replace(':', '?');
690 kDebug() << "// FOUND WRONG SLOWMO, new: " << slowmotionprod;
691 prod.setAttribute("resource", slowmotionprod);
694 //Â move producers to correct place, markers to a global list, fix clip descriptions
695 QDomElement markers = m_document.createElement("markers");
696 // This will get the westley producers:
697 producers = m_document.elementsByTagName("producer");
698 max = producers.count();
699 for (int i = 0; i < max; i++) {
700 QDomElement prod = producers.at(0).toElement();
701 // add resource also as a property (to allow path correction in setNewResource())
702 // TODO: will it work with slowmotion? needs testing
703 /*if (!prod.attribute("resource").isEmpty()) {
704 QDomElement prop_resource = m_document.createElement("property");
705 prop_resource.setAttribute("name", "resource");
706 QDomText resource = m_document.createTextNode(prod.attribute("resource"));
707 prop_resource.appendChild(resource);
708 prod.appendChild(prop_resource);
710 QDomNode m = prod.firstChild();
712 if (m.toElement().tagName() == "markers") {
713 QDomNodeList prodchilds = m.childNodes();
714 int maxchild = prodchilds.count();
715 for (int k = 0; k < maxchild; k++) {
716 QDomElement mark = prodchilds.at(0).toElement();
717 mark.setAttribute("id", prod.attribute("id"));
718 markers.insertAfter(mark, QDomNode());
721 } else if (prod.attribute("type").toInt() == TEXT) {
722 // convert title clip
723 if (m.toElement().tagName() == "textclip") {
725 QDomElement titleclip = m.toElement();
726 QDomElement title = tdoc.createElement("kdenlivetitle");
727 tdoc.appendChild(title);
728 QDomNodeList objects = titleclip.childNodes();
729 int maxchild = objects.count();
730 for (int k = 0; k < maxchild; k++) {
732 QDomElement ob = objects.at(k).toElement();
733 if (ob.attribute("type") == "3") {
734 // text object - all of this goes into "xmldata"...
735 QDomElement item = tdoc.createElement("item");
736 item.setAttribute("z-index", ob.attribute("z"));
737 item.setAttribute("type", "QGraphicsTextItem");
738 QDomElement position = tdoc.createElement("position");
739 position.setAttribute("x", ob.attribute("x"));
740 position.setAttribute("y", ob.attribute("y"));
741 QDomElement content = tdoc.createElement("content");
742 content.setAttribute("font", ob.attribute("font_family"));
743 content.setAttribute("font-size", ob.attribute("font_size"));
744 content.setAttribute("font-bold", ob.attribute("bold"));
745 content.setAttribute("font-italic", ob.attribute("italic"));
746 content.setAttribute("font-underline", ob.attribute("underline"));
747 QString col = ob.attribute("color");
749 content.setAttribute("font-color", colorToString(c));
750 // todo: These fields are missing from the newly generated xmldata:
751 // transform, startviewport, endviewport, background
753 QDomText conttxt = tdoc.createTextNode(ob.attribute("text"));
754 content.appendChild(conttxt);
755 item.appendChild(position);
756 item.appendChild(content);
757 title.appendChild(item);
758 } else if (ob.attribute("type") == "5") {
760 QDomElement item = tdoc.createElement("item");
761 item.setAttribute("z-index", ob.attribute("z"));
762 item.setAttribute("type", "QGraphicsRectItem");
763 QDomElement position = tdoc.createElement("position");
764 position.setAttribute("x", ob.attribute("x"));
765 position.setAttribute("y", ob.attribute("y"));
766 QDomElement content = tdoc.createElement("content");
767 QString col = ob.attribute("color");
769 content.setAttribute("brushcolor", colorToString(c));
770 QString rect = "0,0,";
771 rect.append(ob.attribute("width"));
773 rect.append(ob.attribute("height"));
774 content.setAttribute("rect", rect);
775 item.appendChild(position);
776 item.appendChild(content);
777 title.appendChild(item);
780 prod.setAttribute("xmldata", tdoc.toString());
781 // mbd todo: This clearly does not work, as every title gets the same name - trying to leave it empty
782 // QStringList titleInfo = TitleWidget::getFreeTitleInfo(projectFolder());
783 // prod.setAttribute("titlename", titleInfo.at(0));
784 // prod.setAttribute("resource", titleInfo.at(1));
785 //kDebug()<<"TITLE DATA:\n"<<tdoc.toString();
787 } // End conversion of title clips.
789 } else if (m.isText()) {
790 QString comment = m.nodeValue();
791 if (!comment.isEmpty()) {
792 prod.setAttribute("description", comment);
797 int duration = prod.attribute("duration").toInt();
798 if (duration > 0) prod.setAttribute("out", QString::number(duration));
799 // The clip goes back in, but text clips should not go back in, at least not modified
800 westley.insertBefore(prod, QDomNode());
804 QDomNode westley0 = m_document.elementsByTagName("westley").at(0);
805 if (!markers.firstChild().isNull()) westley0.appendChild(markers);
808 // Convert as much of the kdenlivedoc as possible. Use the producer in westley
809 // First, remove the old stuff from westley, and add a new empty one
810 // Also, track the max id in order to use it for the adding of groups/folders
811 int max_kproducer_id = 0;
812 westley0.removeChild(kdenlivedoc);
813 QDomElement kdenlivedoc_new = m_document.createElement("kdenlivedoc");
814 kdenlivedoc_new.setAttribute("profile", profile);
817 QDomNodeList tracks = m_document.elementsByTagName("track");
818 max = tracks.count();
819 QDomElement tracksinfo = m_document.createElement("tracksinfo");
820 for (int i = 0; i < max; i++) {
821 QDomElement trackinfo = m_document.createElement("trackinfo");
822 QDomElement t = tracks.at(i).toElement();
823 if (t.attribute("hide") == "video") {
824 trackinfo.setAttribute("type", "audio");
825 trackinfo.setAttribute("blind", true);
826 } else trackinfo.setAttribute("blind", false);
827 trackinfo.setAttribute("mute", false);
828 trackinfo.setAttribute("locked", false);
829 if (t.attribute("producer") != "black_track") tracksinfo.appendChild(trackinfo);
831 kdenlivedoc_new.appendChild(tracksinfo);
833 // Add all the producers that has a ressource in westley
834 QDomElement westley_element = westley0.toElement();
835 if (westley_element.isNull()) {
836 kWarning() << "westley0 element in document was not a QDomElement - unable to add producers to new kdenlivedoc";
838 QDomNodeList wproducers = westley_element.elementsByTagName("producer");
839 int kmax = wproducers.count();
840 for (int i = 0; i < kmax; i++) {
841 QDomElement wproducer = wproducers.at(i).toElement();
842 if (wproducer.isNull()) {
843 kWarning() << "Found producer in westley0, that was not a QDomElement";
846 if (wproducer.attribute("id") == "black") continue;
847 // We have to do slightly different things, depending on the type
848 kDebug() << "Converting producer element with type" << wproducer.attribute("type");
849 if (wproducer.attribute("type").toInt() == TEXT) {
850 kDebug() << "Found TEXT element in producer" << endl;
851 QDomElement kproducer = wproducer.cloneNode(true).toElement();
852 kproducer.setTagName("kdenlive_producer");
853 kdenlivedoc_new.appendChild(kproducer);
854 // TODO: Perhaps needs some more changes here to "frequency", aspect ratio as a float, frame_size, channels, and later, ressource and title name
856 QDomElement kproducer = m_document.createElement("kdenlive_producer");
857 kproducer.setAttribute("id", wproducer.attribute("id"));
858 if (!wproducer.attribute("description").isEmpty())
859 kproducer.setAttribute("description", wproducer.attribute("description"));
860 kproducer.setAttribute("resource", wproducer.attribute("resource"));
861 kproducer.setAttribute("type", wproducer.attribute("type"));
862 // Testing fix for 358
863 if (!wproducer.attribute("aspect_ratio").isEmpty()) {
864 kproducer.setAttribute("aspect_ratio", wproducer.attribute("aspect_ratio"));
866 if (!wproducer.attribute("source_fps").isEmpty()) {
867 kproducer.setAttribute("fps", wproducer.attribute("source_fps"));
869 if (!wproducer.attribute("length").isEmpty()) {
870 kproducer.setAttribute("duration", wproducer.attribute("length"));
872 kdenlivedoc_new.appendChild(kproducer);
874 if (wproducer.attribute("id").toInt() > max_kproducer_id) {
875 max_kproducer_id = wproducer.attribute("id").toInt();
879 #define LOOKUP_FOLDER 1
881 // Look through all the folder elements of the old doc, for each folder, for each producer,
882 // get the id, look it up in the new doc, set the groupname and groupid
883 // Note, this does not work at the moment - at least one folders shows up missing, and clips with no folder
885 // QDomElement kdenlivedoc_old = kdenlivedoc.toElement();
886 if (!kdenlivedoc_old.isNull()) {
887 QDomNodeList folders = kdenlivedoc_old.elementsByTagName("folder");
888 int fsize = folders.size();
889 int groupId = max_kproducer_id + 1; // Start at +1 of max id of the kdenlive_producers
890 for (int i = 0; i < fsize; ++i) {
891 QDomElement folder = folders.at(i).toElement();
892 if (!folder.isNull()) {
893 QString groupName = folder.attribute("name");
894 kDebug() << "groupName: " << groupName << " with groupId: " << groupId;
895 QDomNodeList fproducers = folder.elementsByTagName("producer");
896 int psize = fproducers.size();
897 for (int j = 0; j < psize; ++j) {
898 QDomElement fproducer = fproducers.at(j).toElement();
899 if (!fproducer.isNull()) {
900 QString id = fproducer.attribute("id");
901 // This is not very effective, but compared to loading the clips, its a breeze
902 QDomNodeList kdenlive_producers = kdenlivedoc_new.elementsByTagName("kdenlive_producer");
903 int kpsize = kdenlive_producers.size();
904 for (int k = 0; k < kpsize; ++k) {
905 QDomElement kproducer = kdenlive_producers.at(k).toElement(); // Its an element for sure
906 if (id == kproducer.attribute("id")) {
907 // We do not check that it already is part of a folder
908 kproducer.setAttribute("groupid", groupId);
909 kproducer.setAttribute("groupname", groupName);
920 westley0.appendChild(kdenlivedoc_new);
922 QDomNodeList elements = westley.childNodes();
923 max = elements.count();
924 for (int i = 0; i < max; i++) {
925 QDomElement prod = elements.at(0).toElement();
926 westley0.insertAfter(prod, QDomNode());
929 westley0.removeChild(westley);
931 // experimental and probably slow
932 // adds <avfile /> information to <kdenlive_producer />
933 QDomNodeList kproducers = m_document.elementsByTagName("kdenlive_producer");
934 QDomNodeList avfiles = kdenlivedoc_old.elementsByTagName("avfile");
935 kDebug() << "found" << avfiles.count() << "<avfile />s and" << kproducers.count() << "<kdenlive_producer />s";
936 for (int i = 0; i < avfiles.count(); ++i) {
937 QDomElement avfile = avfiles.at(i).toElement();
938 QDomElement kproducer;
940 kWarning() << "found an <avfile /> that is not a QDomElement";
942 QString id = avfile.attribute("id");
943 // this is horrible, must be rewritten, it's just for test
944 for (int j = 0; j < kproducers.count(); ++j) {
945 //kDebug() << "checking <kdenlive_producer /> with id" << kproducers.at(j).toElement().attribute("id");
946 if (kproducers.at(j).toElement().attribute("id") == id) {
947 kproducer = kproducers.at(j).toElement();
951 if (kproducer == QDomElement())
952 kWarning() << "no match for <avfile /> with id =" << id;
954 //kDebug() << "ready to set additional <avfile />'s attributes (id =" << id << ")";
955 kproducer.setAttribute("channels", avfile.attribute("channels"));
956 kproducer.setAttribute("duration", avfile.attribute("duration"));
957 kproducer.setAttribute("frame_size", avfile.attribute("width") + 'x' + avfile.attribute("height"));
958 kproducer.setAttribute("frequency", avfile.attribute("frequency"));
959 if (kproducer.attribute("description").isEmpty() && !avfile.attribute("description").isEmpty())
960 kproducer.setAttribute("description", avfile.attribute("description"));
965 /*kDebug() << "///////////////// CONVERTEDÂ DOC:";
966 kDebug() << m_document.toString();
967 kDebug() << "///////////////// END CONVERTEDÂ DOC:";
969 QFile file("converted.kdenlive");
970 if (file.open(QIODevice::WriteOnly)) {
971 QTextStream stream(&file);
972 stream << m_document.toString().toUtf8();
975 kDebug() << "Unable to dump file to converted.kdenlive";
978 //kDebug() << "///////////////// END CONVERTEDÂ DOC:";
983 QString KdenliveDoc::colorToString(const QColor& c)
985 QString ret = "%1,%2,%3,%4";
986 ret = ret.arg(c.red()).arg(c.green()).arg(c.blue()).arg(c.alpha());
990 void KdenliveDoc::setZone(int start, int end)
996 QPoint KdenliveDoc::zone() const
998 return QPoint(m_zoneStart, m_zoneEnd);
1001 bool KdenliveDoc::saveSceneList(const QString &path, const QString &scene)
1003 QDomDocument sceneList;
1004 sceneList.setContent(scene, true);
1005 QDomNode wes = sceneList.elementsByTagName("westley").at(0);
1006 QDomElement addedXml = sceneList.createElement("kdenlivedoc");
1007 wes.appendChild(addedXml);
1009 QDomElement markers = sceneList.createElement("markers");
1010 addedXml.setAttribute("version", "0.82");
1011 addedXml.setAttribute("profile", profilePath());
1012 addedXml.setAttribute("position", m_render->seekPosition().frames(m_fps));
1013 addedXml.setAttribute("zonein", m_zoneStart);
1014 addedXml.setAttribute("zoneout", m_zoneEnd);
1015 addedXml.setAttribute("projectfolder", m_projectFolder.path());
1016 addedXml.setAttribute("zoom", m_zoom);
1019 QDomElement tracksinfo = sceneList.createElement("tracksinfo");
1020 foreach(const TrackInfo &info, m_tracksList) {
1021 QDomElement trackinfo = sceneList.createElement("trackinfo");
1022 if (info.type == AUDIOTRACK) trackinfo.setAttribute("type", "audio");
1023 trackinfo.setAttribute("mute", info.isMute);
1024 trackinfo.setAttribute("blind", info.isBlind);
1025 trackinfo.setAttribute("locked", info.isLocked);
1026 tracksinfo.appendChild(trackinfo);
1028 addedXml.appendChild(tracksinfo);
1030 // save project folders
1031 QMap <QString, QString> folderlist = m_clipManager->documentFolderList();
1033 QMapIterator<QString, QString> f(folderlist);
1034 while (f.hasNext()) {
1036 QDomElement folder = sceneList.createElement("folder");
1037 folder.setAttribute("id", f.key());
1038 folder.setAttribute("name", f.value());
1039 addedXml.appendChild(folder);
1042 // Save project clips
1044 QList <DocClipBase*> list = m_clipManager->documentClipList();
1045 for (int i = 0; i < list.count(); i++) {
1046 e = list.at(i)->toXML();
1047 e.setTagName("kdenlive_producer");
1048 addedXml.appendChild(sceneList.importNode(e, true));
1049 QList < CommentedTime > marks = list.at(i)->commentedSnapMarkers();
1050 for (int j = 0; j < marks.count(); j++) {
1051 QDomElement marker = sceneList.createElement("marker");
1052 marker.setAttribute("time", marks.at(j).time().ms() / 1000);
1053 marker.setAttribute("comment", marks.at(j).comment());
1054 marker.setAttribute("id", e.attribute("id"));
1055 markers.appendChild(marker);
1058 addedXml.appendChild(markers);
1061 if (!m_guidesXml.isNull()) addedXml.appendChild(sceneList.importNode(m_guidesXml.documentElement(), true));
1064 addedXml.appendChild(sceneList.importNode(m_clipManager->groupsXml(), true));
1066 //wes.appendChild(doc.importNode(kdenliveData, true));
1069 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
1070 kWarning() << "////// ERROR writing to file: " << path;
1071 KMessageBox::error(kapp->activeWindow(), i18n("Cannot write to file %1", path));
1075 file.write(sceneList.toString().toUtf8());
1076 if (file.error() != QFile::NoError) {
1077 KMessageBox::error(kapp->activeWindow(), i18n("Cannot write to file %1", path));
1085 ClipManager *KdenliveDoc::clipManager()
1087 return m_clipManager;
1090 KUrl KdenliveDoc::projectFolder() const
1092 //if (m_projectFolder.isEmpty()) return KUrl(KStandardDirs::locateLocal("appdata", "/projects/"));
1093 return m_projectFolder;
1096 void KdenliveDoc::setProjectFolder(KUrl url)
1098 if (url == m_projectFolder) return;
1100 KStandardDirs::makeDir(url.path());
1101 KStandardDirs::makeDir(url.path() + "/titles/");
1102 KStandardDirs::makeDir(url.path() + "/thumbs/");
1103 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);
1104 m_projectFolder = url;
1107 void KdenliveDoc::moveProjectData(KUrl url)
1109 QList <DocClipBase*> list = m_clipManager->documentClipList();
1110 //TODO: Also move ladspa effects files
1111 for (int i = 0; i < list.count(); i++) {
1112 DocClipBase *clip = list.at(i);
1113 if (clip->clipType() == TEXT) {
1114 // the image for title clip must be moved
1115 KUrl oldUrl = clip->fileURL();
1116 KUrl newUrl = KUrl(url.path() + "/titles/" + oldUrl.fileName());
1117 KIO::Job *job = KIO::copy(oldUrl, newUrl);
1118 if (KIO::NetAccess::synchronousRun(job, 0)) clip->setProperty("resource", newUrl.path());
1120 QString hash = clip->getClipHash();
1121 KUrl oldVideoThumbUrl = KUrl(m_projectFolder.path() + "/thumbs/" + hash + ".png");
1122 KUrl oldAudioThumbUrl = KUrl(m_projectFolder.path() + "/thumbs/" + hash + ".thumb");
1123 if (KIO::NetAccess::exists(oldVideoThumbUrl, KIO::NetAccess::SourceSide, 0)) {
1124 KUrl newUrl = KUrl(url.path() + "/thumbs/" + hash + ".png");
1125 KIO::Job *job = KIO::copy(oldVideoThumbUrl, newUrl);
1126 KIO::NetAccess::synchronousRun(job, 0);
1128 if (KIO::NetAccess::exists(oldAudioThumbUrl, KIO::NetAccess::SourceSide, 0)) {
1129 KUrl newUrl = KUrl(url.path() + "/thumbs/" + hash + ".thumb");
1130 KIO::Job *job = KIO::copy(oldAudioThumbUrl, newUrl);
1131 if (KIO::NetAccess::synchronousRun(job, 0)) clip->refreshThumbUrl();
1136 const QString &KdenliveDoc::profilePath() const
1138 return m_profile.path;
1141 MltVideoProfile KdenliveDoc::mltProfile() const
1146 void KdenliveDoc::setProfilePath(QString path)
1148 if (path.isEmpty()) path = KdenliveSettings::default_profile();
1149 if (path.isEmpty()) path = "dv_pal";
1150 m_profile = ProfilesDialog::getVideoProfile(path);
1151 KdenliveSettings::setProject_display_ratio((double) m_profile.display_aspect_num / m_profile.display_aspect_den);
1152 m_fps = (double) m_profile.frame_rate_num / m_profile.frame_rate_den;
1153 KdenliveSettings::setProject_fps(m_fps);
1154 m_width = m_profile.width;
1155 m_height = m_profile.height;
1156 kDebug() << "KDEnnlive document, init timecode from path: " << path << ", " << m_fps;
1157 if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
1158 else m_timecode.setFormat((int) m_fps);
1161 double KdenliveDoc::dar()
1163 return (double) m_profile.display_aspect_num / m_profile.display_aspect_den;
1166 void KdenliveDoc::setThumbsProgress(const QString &message, int progress)
1168 emit progressInfo(message, progress);
1171 void KdenliveDoc::loadingProgressed()
1173 m_documentLoadingProgress += m_documentLoadingStep;
1174 emit progressInfo(QString(), (int) m_documentLoadingProgress);
1177 QUndoStack *KdenliveDoc::commandStack()
1179 return m_commandStack;
1183 void KdenliveDoc::setRenderer(Render *render) {
1184 if (m_render) return;
1186 emit progressInfo(i18n("Loading playlist..."), 0);
1187 //qApp->processEvents();
1189 m_render->setSceneList(m_document.toString(), m_startPos);
1190 kDebug() << "// SETTING SCENE LIST:\n\n" << m_document.toString();
1191 checkProjectClips();
1193 emit progressInfo(QString(), -1);
1196 void KdenliveDoc::checkProjectClips()
1198 if (m_render == NULL) return;
1199 m_clipManager->resetProducersList(m_render->producersList());
1203 QList <Mlt::Producer *> prods = m_render->producersList();
1207 for (int i = 0; i < prods.count(); i++) {
1208 id = prods.at(i)->get("id");
1209 prodId = id.section('_', 0, 0);
1210 prodTrack = id.section('_', 1, 1);
1211 DocClipBase *clip = m_clipManager->getClipById(prodId);
1212 if (clip) clip->setProducer(prods.at(i));
1213 if (clip && clip->clipType() == TEXT && !QFile::exists(clip->fileURL().path())) {
1214 // regenerate text clip image if required
1215 //kDebug() << "// TITLE: " << clip->getProperty("titlename") << " Preview file: " << clip->getProperty("resource") << " DOES NOT EXIST";
1216 QString titlename = clip->getProperty("name");
1217 QString titleresource;
1218 if (titlename.isEmpty()) {
1219 QStringList titleInfo = TitleWidget::getFreeTitleInfo(projectFolder());
1220 titlename = titleInfo.at(0);
1221 titleresource = titleInfo.at(1);
1222 clip->setProperty("name", titlename);
1223 kDebug() << "// New title set to: " << titlename;
1225 titleresource = TitleWidget::getFreeTitleInfo(projectFolder()).at(1);
1226 //titleresource = TitleWidget::getTitleResourceFromName(projectFolder(), titlename);
1228 TitleWidget *dia_ui = new TitleWidget(KUrl(), KUrl(titleresource).directory(), m_render, kapp->activeWindow());
1230 doc.setContent(clip->getProperty("xmldata"));
1231 dia_ui->setXml(doc);
1232 QImage pix = dia_ui->renderedPixmap();
1233 pix.save(titleresource);
1234 clip->setProperty("resource", titleresource);
1236 clip->producer()->set("force_reload", 1);
1241 void KdenliveDoc::updatePreviewSettings()
1243 m_clipManager->updatePreviewSettings();
1244 m_render->updatePreviewSettings();
1245 m_clipManager->resetProducersList(m_render->producersList());
1249 Render *KdenliveDoc::renderer()
1254 void KdenliveDoc::updateClip(const QString &id)
1256 emit updateClipDisplay(id);
1259 int KdenliveDoc::getFramePos(QString duration)
1261 return m_timecode.getFrameCount(duration, m_fps);
1264 QString KdenliveDoc::producerName(const QString &id)
1266 QString result = "unnamed";
1267 QDomNodeList prods = producersList();
1268 int ct = prods.count();
1269 for (int i = 0; i < ct ; i++) {
1270 QDomElement e = prods.item(i).toElement();
1271 if (e.attribute("id") != "black" && e.attribute("id") == id) {
1272 result = e.attribute("name");
1273 if (result.isEmpty()) result = KUrl(e.attribute("resource")).fileName();
1280 void KdenliveDoc::setProducerDuration(const QString &id, int duration)
1282 QDomNodeList prods = producersList();
1283 int ct = prods.count();
1284 for (int i = 0; i < ct ; i++) {
1285 QDomElement e = prods.item(i).toElement();
1286 if (e.attribute("id") != "black" && e.attribute("id") == id) {
1287 e.setAttribute("duration", QString::number(duration));
1293 int KdenliveDoc::getProducerDuration(const QString &id)
1296 QDomNodeList prods = producersList();
1297 int ct = prods.count();
1298 for (int i = 0; i < ct ; i++) {
1299 QDomElement e = prods.item(i).toElement();
1300 if (e.attribute("id") != "black" && e.attribute("id") == id) {
1301 result = e.attribute("duration").toInt();
1308 QDomDocument KdenliveDoc::toXml()
1313 Timecode KdenliveDoc::timecode() const
1318 QDomNodeList KdenliveDoc::producersList()
1320 return m_document.elementsByTagName("producer");
1323 double KdenliveDoc::projectDuration() const
1326 return GenTime(m_render->getLength(), m_fps).ms() / 1000;
1331 double KdenliveDoc::fps() const
1336 int KdenliveDoc::width() const
1341 int KdenliveDoc::height() const
1346 KUrl KdenliveDoc::url() const
1351 void KdenliveDoc::setUrl(KUrl url)
1356 void KdenliveDoc::setModified(bool mod)
1358 if (!m_url.isEmpty() && mod && KdenliveSettings::crashrecovery()) {
1359 m_autoSaveTimer->start(3000);
1361 if (mod == m_modified) return;
1363 emit docModified(m_modified);
1366 bool KdenliveDoc::isModified() const
1371 const QString KdenliveDoc::description() const
1373 if (m_url.isEmpty())
1374 return i18n("Untitled") + " / " + m_profile.description;
1376 return m_url.fileName() + " / " + m_profile.description;
1379 void KdenliveDoc::addClip(QDomElement elem, QString clipId, bool createClipItem)
1381 const QString producerId = clipId.section('_', 0, 0);
1382 DocClipBase *clip = m_clipManager->getClipById(producerId);
1384 elem.setAttribute("id", producerId);
1385 QString path = elem.attribute("resource");
1387 if (elem.attribute("type").toInt() == SLIDESHOW) {
1388 extension = KUrl(path).fileName();
1389 path = KUrl(path).directory();
1390 } else if (elem.attribute("type").toInt() == TEXT && QFile::exists(path) == false) {
1391 kDebug() << "// TITLE: " << elem.attribute("name") << " Preview file: " << elem.attribute("resource") << " DOES NOT EXIST";
1392 QString titlename = elem.attribute("name");
1393 QString titleresource;
1394 if (titlename.isEmpty()) {
1395 QStringList titleInfo = TitleWidget::getFreeTitleInfo(projectFolder());
1396 titlename = titleInfo.at(0);
1397 titleresource = titleInfo.at(1);
1398 elem.setAttribute("name", titlename);
1399 kDebug() << "// New title set to: " << titlename;
1401 titleresource = TitleWidget::getFreeTitleInfo(projectFolder()).at(1);
1402 //titleresource = TitleWidget::getTitleResourceFromName(projectFolder(), titlename);
1404 TitleWidget *dia_ui = new TitleWidget(KUrl(), KUrl(titleresource).directory(), m_render, kapp->activeWindow());
1406 doc.setContent(elem.attribute("xmldata"));
1407 dia_ui->setXml(doc);
1408 QImage pix = dia_ui->renderedPixmap();
1409 pix.save(titleresource);
1410 elem.setAttribute("resource", titleresource);
1411 setNewClipResource(clipId, titleresource);
1414 if (path.isEmpty() == false && QFile::exists(path) == false && elem.attribute("type").toInt() != TEXT) {
1415 kDebug() << "// FOUNDÂ MISSING CLIP: " << path << ", TYPE: " << elem.attribute("type").toInt();
1416 const QString size = elem.attribute("file_size");
1417 const QString hash = elem.attribute("file_hash");
1419 int action = KMessageBox::No;
1420 if (!size.isEmpty() && !hash.isEmpty()) {
1421 if (!m_searchFolder.isEmpty()) newpath = searchFileRecursively(m_searchFolder, size, hash);
1422 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")));
1424 if (elem.attribute("type").toInt() == SLIDESHOW) {
1425 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")));
1426 if (res == KMessageBox::Yes)
1427 newpath = KFileDialog::getExistingDirectory(KUrl("kfiledialog:///clipfolder"), kapp->activeWindow(), i18n("Looking for %1", path));
1428 else if (res == KMessageBox::Cancel) {
1429 // Abort project loading
1430 action = KMessageBox::Cancel;
1433 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")));
1434 if (res == KMessageBox::Yes)
1435 newpath = KFileDialog::getOpenFileName(KUrl("kfiledialog:///clipfolder"), QString(), kapp->activeWindow(), i18n("Looking for %1", path));
1436 else if (res == KMessageBox::Cancel) {
1437 // Abort project loading
1438 action = KMessageBox::Cancel;
1442 if (action == KMessageBox::Yes) {
1443 kDebug() << "// ASKED FOR SRCH CLIP: " << clipId;
1444 m_searchFolder = KFileDialog::getExistingDirectory(KUrl("kfiledialog:///clipfolder"), kapp->activeWindow());
1445 if (!m_searchFolder.isEmpty()) {
1446 newpath = searchFileRecursively(QDir(m_searchFolder), size, hash);
1448 } else if (action == KMessageBox::Cancel) {
1449 m_abortLoading = true;
1452 if (!newpath.isEmpty()) {
1453 if (elem.attribute("type").toInt() == SLIDESHOW) newpath.append('/' + extension);
1454 elem.setAttribute("resource", newpath);
1455 setNewClipResource(clipId, newpath);
1459 clip = new DocClipBase(m_clipManager, elem, producerId);
1460 m_clipManager->addClip(clip);
1463 if (createClipItem) {
1464 emit addProjectClip(clip);
1465 qApp->processEvents();
1466 m_render->getFileProperties(clip->toXML(), clip->getId());
1471 void KdenliveDoc::setNewClipResource(const QString &id, const QString &path)
1473 QDomNodeList prods = m_document.elementsByTagName("producer");
1474 int maxprod = prods.count();
1475 for (int i = 0; i < maxprod; i++) {
1476 QDomNode m = prods.at(i);
1477 QString prodId = m.toElement().attribute("id");
1478 if (prodId == id || prodId.startsWith(id + '_')) {
1479 QDomNodeList params = m.childNodes();
1480 for (int j = 0; j < params.count(); j++) {
1481 QDomElement e = params.item(j).toElement();
1482 if (e.attribute("name") == "resource") {
1483 e.firstChild().setNodeValue(path);
1491 QString KdenliveDoc::searchFileRecursively(const QDir &dir, const QString &matchSize, const QString &matchHash) const
1493 QString foundFileName;
1494 QByteArray fileData;
1495 QByteArray fileHash;
1496 QStringList filesAndDirs = dir.entryList(QDir::Files | QDir::Readable);
1497 for (int i = 0; i < filesAndDirs.size() && foundFileName.isEmpty(); i++) {
1498 QFile file(dir.absoluteFilePath(filesAndDirs.at(i)));
1499 if (file.open(QIODevice::ReadOnly)) {
1500 if (QString::number(file.size()) == matchSize) {
1502 * 1 MB = 1 second per 450 files (or faster)
1503 * 10 MB = 9 seconds per 450 files (or faster)
1505 if (file.size() > 1000000*2) {
1506 fileData = file.read(1000000);
1507 if (file.seek(file.size() - 1000000))
1508 fileData.append(file.readAll());
1510 fileData = file.readAll();
1512 fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
1513 if (QString(fileHash.toHex()) == matchHash)
1514 return file.fileName();
1517 kDebug() << filesAndDirs.at(i) << file.size() << fileHash.toHex();
1519 filesAndDirs = dir.entryList(QDir::Dirs | QDir::Readable | QDir::Executable | QDir::NoDotAndDotDot);
1520 for (int i = 0; i < filesAndDirs.size() && foundFileName.isEmpty(); i++) {
1521 foundFileName = searchFileRecursively(dir.absoluteFilePath(filesAndDirs.at(i)), matchSize, matchHash);
1522 if (!foundFileName.isEmpty())
1525 return foundFileName;
1528 void KdenliveDoc::addClipInfo(QDomElement elem, QDomElement orig, QString clipId)
1530 DocClipBase *clip = m_clipManager->getClipById(clipId);
1532 addClip(elem, clipId, false);
1534 QMap <QString, QString> properties;
1535 QDomNamedNodeMap attributes = elem.attributes();
1537 for (int i = 0; i < attributes.count(); i++) {
1538 attrname = attributes.item(i).nodeName();
1539 if (attrname != "resource")
1540 properties.insert(attrname, attributes.item(i).nodeValue());
1541 kDebug() << attrname << " = " << attributes.item(i).nodeValue();
1543 clip->setProperties(properties);
1544 emit addProjectClip(clip, false);
1546 if (orig != QDomElement()) {
1547 QMap<QString, QString> meta;
1548 QDomNode m = orig.firstChild();
1549 while (!m.isNull()) {
1550 QString name = m.toElement().attribute("name");
1551 if (name.startsWith("meta.attr")) {
1552 meta.insert(name.section('.', 2, 3), m.firstChild().nodeValue());
1554 m = m.nextSibling();
1556 if (!meta.isEmpty()) {
1557 if (clip == NULL) clip = m_clipManager->getClipById(clipId);
1558 if (clip) clip->setMetadata(meta);
1563 void KdenliveDoc::deleteProjectClip(QList <QString> ids)
1565 for (int i = 0; i < ids.size(); ++i) {
1566 emit deleteTimelineClip(ids.at(i));
1567 m_clipManager->slotDeleteClip(ids.at(i));
1572 void KdenliveDoc::deleteClip(const QString &clipId)
1574 emit signalDeleteProjectClip(clipId);
1575 m_clipManager->deleteClip(clipId);
1578 void KdenliveDoc::slotAddClipList(const KUrl::List urls, const QString group, const QString &groupId)
1580 m_clipManager->slotAddClipList(urls, group, groupId);
1581 emit selectLastAddedClip(QString::number(m_clipManager->lastClipId()));
1586 void KdenliveDoc::slotAddClipFile(const KUrl url, const QString group, const QString &groupId)
1588 //kDebug() << "///////// DOCUM, ADD CLP: " << url;
1589 m_clipManager->slotAddClipFile(url, group, groupId);
1590 emit selectLastAddedClip(QString::number(m_clipManager->lastClipId()));
1594 const QString KdenliveDoc::getFreeClipId()
1596 return QString::number(m_clipManager->getFreeClipId());
1599 DocClipBase *KdenliveDoc::getBaseClip(const QString &clipId)
1601 return m_clipManager->getClipById(clipId);
1604 void KdenliveDoc::slotCreateTextClip(QString /*group*/, const QString &/*groupId*/)
1606 QString titlesFolder = projectFolder().path() + "/titles/";
1607 KStandardDirs::makeDir(titlesFolder);
1608 TitleWidget *dia_ui = new TitleWidget(KUrl(), titlesFolder, m_render, kapp->activeWindow());
1609 if (dia_ui->exec() == QDialog::Accepted) {
1610 QStringList titleInfo = TitleWidget::getFreeTitleInfo(projectFolder());
1611 QImage pix = dia_ui->renderedPixmap();
1612 pix.save(titleInfo.at(1));
1613 //dia_ui->saveTitle(path + ".kdenlivetitle");
1614 m_clipManager->slotAddTextClipFile(titleInfo.at(0), titleInfo.at(1), dia_ui->xml().toString(), QString(), QString());
1620 int KdenliveDoc::tracksCount() const
1622 return m_tracksList.count();
1625 TrackInfo KdenliveDoc::trackInfoAt(int ix) const
1627 return m_tracksList.at(ix);
1630 void KdenliveDoc::switchTrackAudio(int ix, bool hide)
1632 m_tracksList[ix].isMute = hide; // !m_tracksList.at(ix).isMute;
1635 void KdenliveDoc::switchTrackLock(int ix, bool lock)
1637 m_tracksList[ix].isLocked = lock;
1640 bool KdenliveDoc::isTrackLocked(int ix) const
1642 return m_tracksList.at(ix).isLocked;
1645 void KdenliveDoc::switchTrackVideo(int ix, bool hide)
1647 m_tracksList[ix].isBlind = hide; // !m_tracksList.at(ix).isBlind;
1650 void KdenliveDoc::insertTrack(int ix, TrackInfo type)
1652 if (ix == -1) m_tracksList << type;
1653 else m_tracksList.insert(ix, type);
1656 void KdenliveDoc::deleteTrack(int ix)
1658 m_tracksList.removeAt(ix);
1661 void KdenliveDoc::setTrackType(int ix, TrackInfo type)
1663 m_tracksList[ix].type = type.type;
1664 m_tracksList[ix].isMute = type.isMute;
1665 m_tracksList[ix].isBlind = type.isBlind;
1666 m_tracksList[ix].isLocked = type.isLocked;
1669 const QList <TrackInfo> KdenliveDoc::tracksList() const
1671 return m_tracksList;
1674 QPoint KdenliveDoc::getTracksCount() const
1678 foreach(const TrackInfo &info, m_tracksList) {
1679 if (info.type == VIDEOTRACK) video++;
1682 return QPoint(video, audio);
1685 void KdenliveDoc::cachePixmap(const QString &fileId, const QPixmap &pix) const
1687 pix.save(m_projectFolder.path() + "/thumbs/" + fileId + ".png");
1690 QString KdenliveDoc::getLadspaFile() const
1693 QString counter = QString::number(ct).rightJustified(5, '0', false);
1694 while (QFile::exists(m_projectFolder.path() + "/ladspa/" + counter + ".ladspa")) {
1696 counter = QString::number(ct).rightJustified(5, '0', false);
1698 return m_projectFolder.path() + "/ladspa/" + counter + ".ladspa";
1701 #include "kdenlivedoc.moc"