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 ***************************************************************************/
20 #include <QCryptographicHash>
24 #include <KStandardDirs>
25 #include <KMessageBox>
27 #include <KFileDialog>
28 #include <KIO/NetAccess>
29 #include <KApplication>
32 #include <mlt++/Mlt.h>
34 #include "kdenlivedoc.h"
35 #include "docclipbase.h"
36 #include "profilesdialog.h"
37 #include "kdenlivesettings.h"
39 #include "clipmanager.h"
40 #include "addfoldercommand.h"
41 #include "editfoldercommand.h"
42 #include "titlewidget.h"
43 #include "mainwindow.h"
46 KdenliveDoc::KdenliveDoc(const KUrl &url, const KUrl &projectFolder, QUndoGroup *undoGroup, const QString &profileName, const QPoint tracks, MainWindow *parent): QObject(parent), m_render(NULL), m_url(url), m_projectFolder(projectFolder), m_commandStack(new QUndoStack(undoGroup)), m_modified(false), m_documentLoadingProgress(0), m_documentLoadingStep(0.0), m_startPos(0), m_zoom(7), m_autosave(NULL) {
47 m_clipManager = new ClipManager(this);
48 m_autoSaveTimer = new QTimer(this);
49 m_autoSaveTimer->setSingleShot(true);
52 if (KIO::NetAccess::download(url.path(), tmpFile, parent)) {
54 m_document.setContent(&file, false);
56 QDomNode infoXmlNode = m_document.elementsByTagName("kdenlivedoc").at(0);
57 QDomNode westley = m_document.elementsByTagName("westley").at(0);
58 if (!infoXmlNode.isNull()) {
59 QDomElement infoXml = infoXmlNode.toElement();
60 QString profilePath = infoXml.attribute("profile");
61 m_startPos = infoXml.attribute("position").toInt();
62 m_zoom = infoXml.attribute("zoom", "7").toInt();
63 setProfilePath(profilePath);
64 double version = infoXml.attribute("version").toDouble();
65 if (version < 0.8) convertDocument(version);
67 //delete all mlt producers and instead, use Kdenlive saved producers
68 /*QDomNodeList prods = m_document.elementsByTagName("producer");
69 int maxprod = prods.count();
71 for (int i = 0; i < maxprod; i++) {
72 QDomNode m = prods.at(pos);
73 QString prodId = m.toElement().attribute("id");
74 if (prodId == "black" || prodId.startsWith("slowmotion"))
76 else westley.removeChild(m);
78 /*prods = m_document.elementsByTagName("kdenlive_producer");
79 maxprod = prods.count();
80 for (int i = 0; i < maxprod; i++) {
81 prods.at(0).toElement().setTagName("producer");
82 westley.insertBefore(prods.at(0), QDomNode());
86 QDomNodeList producers = m_document.elementsByTagName("producer");
87 QDomNodeList infoproducers = m_document.elementsByTagName("kdenlive_producer");
88 const int max = producers.count();
89 const int infomax = infoproducers.count();
92 m_documentLoadingStep = 100.0 / (max + infomax + m_document.elementsByTagName("entry").count());
93 parent->slotGotProgressInfo(i18n("Loading project clips"), (int) m_documentLoadingProgress);
96 for (int i = 0; i < max; i++) {
97 e = producers.item(i).cloneNode().toElement();
98 if (m_documentLoadingStep > 0) {
99 m_documentLoadingProgress += m_documentLoadingStep;
100 parent->slotGotProgressInfo(QString(), (int) m_documentLoadingProgress);
101 //qApp->processEvents();
103 QString prodId = e.attribute("id");
104 if (!e.isNull() && prodId != "black" && !prodId.startsWith("slowmotion")/*&& prodId.toInt() > 0*/) {
105 // addClip(e, prodId, false);
106 kDebug() << "// PROD: " << prodId;
110 for (int i = 0; i < infomax; i++) {
111 e = infoproducers.item(i).cloneNode().toElement();
112 if (m_documentLoadingStep > 0) {
113 m_documentLoadingProgress += m_documentLoadingStep;
114 parent->slotGotProgressInfo(QString(), (int) m_documentLoadingProgress);
115 //qApp->processEvents();
117 QString prodId = e.attribute("id");
118 if (!e.isNull() && prodId != "black" && !prodId.startsWith("slowmotion")) {
119 e.setTagName("producer");
120 addClipInfo(e, prodId);
121 kDebug() << "// NLIVE PROD: " << prodId;
125 QDomNode markers = m_document.elementsByTagName("markers").at(0);
126 if (!markers.isNull()) {
127 QDomNodeList markerslist = markers.childNodes();
128 int maxchild = markerslist.count();
129 for (int k = 0; k < maxchild; k++) {
130 e = markerslist.at(k).toElement();
131 if (e.tagName() == "marker") {
132 m_clipManager->getClipById(e.attribute("id"))->addSnapMarker(GenTime(e.attribute("time").toDouble()), e.attribute("comment"));
135 westley.removeChild(markers);
137 m_document.removeChild(infoXmlNode);
139 kDebug() << "Reading file: " << url.path() << ", found clips: " << producers.count();
141 parent->slotGotProgressInfo(i18n("File %1 is not a Kdenlive project file."), 100);
142 kWarning() << " NO KDENLIVE INFO FOUND IN FILE: " << url.path();
143 m_document = createEmptyDocument(tracks.x(), tracks.y());
144 setProfilePath(profileName);
146 KIO::NetAccess::removeTempFile(tmpFile);
148 KMessageBox::error(parent, KIO::NetAccess::lastErrorString());
149 parent->slotGotProgressInfo(i18n("File %1 is not a Kdenlive project file."), 100);
150 m_document = createEmptyDocument(tracks.x(), tracks.y());
151 setProfilePath(profileName);
154 m_document = createEmptyDocument(tracks.x(), tracks.y());
155 setProfilePath(profileName);
157 m_scenelist = m_document.toString();
158 kDebug() << "KDEnnlive document, init timecode: " << m_fps;
159 if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
160 else m_timecode.setFormat((int) m_fps);
163 connect(m_autoSaveTimer, SIGNAL(timeout()), this, SLOT(slotAutoSave()));
166 KdenliveDoc::~KdenliveDoc() {
167 delete m_commandStack;
168 delete m_clipManager;
169 delete m_autoSaveTimer;
171 m_autosave->remove();
176 QDomDocument KdenliveDoc::createEmptyDocument(const int videotracks, const int audiotracks) {
177 // Creating new document
179 QDomElement westley = doc.createElement("westley");
180 doc.appendChild(westley);
182 QDomElement tractor = doc.createElement("tractor");
183 tractor.setAttribute("id", "maintractor");
184 QDomElement multitrack = doc.createElement("multitrack");
185 QDomElement playlist = doc.createElement("playlist");
186 playlist.setAttribute("id", "black_track");
187 westley.appendChild(playlist);
191 int total = audiotracks + videotracks + 1;
193 for (int i = 1; i < total; i++) {
194 QDomElement playlist = doc.createElement("playlist");
195 playlist.setAttribute("id", "playlist" + QString::number(i));
196 westley.appendChild(playlist);
199 QDomElement track0 = doc.createElement("track");
200 track0.setAttribute("producer", "black_track");
201 tractor.appendChild(track0);
203 // create audio tracks
204 for (int i = 1; i < audiotracks + 1; i++) {
205 QDomElement track = doc.createElement("track");
206 track.setAttribute("producer", "playlist" + QString::number(i));
207 track.setAttribute("hide", "video");
208 tractor.appendChild(track);
211 // create video tracks
212 for (int i = audiotracks + 1; i < total; i++) {
213 QDomElement track = doc.createElement("track");
214 track.setAttribute("producer", "playlist" + QString::number(i));
215 tractor.appendChild(track);
218 for (uint i = 2; i < total ; i++) {
219 QDomElement transition = doc.createElement("transition");
220 transition.setAttribute("always_active", "1");
222 QDomElement property = doc.createElement("property");
223 property.setAttribute("name", "a_track");
224 QDomText value = doc.createTextNode(QString::number(1));
225 property.appendChild(value);
226 transition.appendChild(property);
228 property = doc.createElement("property");
229 property.setAttribute("name", "b_track");
230 value = doc.createTextNode(QString::number(i));
231 property.appendChild(value);
232 transition.appendChild(property);
234 property = doc.createElement("property");
235 property.setAttribute("name", "mlt_service");
236 value = doc.createTextNode("mix");
237 property.appendChild(value);
238 transition.appendChild(property);
240 property = doc.createElement("property");
241 property.setAttribute("name", "combine");
242 value = doc.createTextNode("1");
243 property.appendChild(value);
244 transition.appendChild(property);
246 property = doc.createElement("property");
247 property.setAttribute("name", "internal_added");
248 value = doc.createTextNode("237");
249 property.appendChild(value);
250 transition.appendChild(property);
251 tractor.appendChild(transition);
253 westley.appendChild(tractor);
258 void KdenliveDoc::syncGuides(QList <Guide *> guides) {
262 m_guidesXml = doc.createElement("guides");
264 for (int i = 0; i < guides.count(); i++) {
265 e = doc.createElement("guide");
266 e.setAttribute("time", guides.at(i)->position().ms() / 1000);
267 e.setAttribute("comment", guides.at(i)->label());
268 m_guidesXml.appendChild(e);
270 emit guidesUpdated();
273 QDomElement KdenliveDoc::guidesXml() const {
277 void KdenliveDoc::slotAutoSave() {
278 if (m_render && m_autosave) {
279 if (!m_autosave->isOpen() && !m_autosave->open(QIODevice::ReadWrite)) {
280 // show error: could not open the autosave file
281 kDebug() << "ERROR; CANNOT CREATE AUTOSAVE FILE";
283 kDebug() << "// AUTOSAVE FILE: " << m_autosave->fileName();
285 doc.setContent(m_render->sceneList());
286 saveSceneList(m_autosave->fileName(), doc);
290 void KdenliveDoc::setZoom(int factor) {
294 int KdenliveDoc::zoom() const {
298 void KdenliveDoc::convertDocument(double version) {
299 kDebug() << "Opening a document with version " << version;
300 // Opening a old Kdenlive document
301 if (version == 0.7) {
302 kDebug() << "Unable to open document with version " << version;
303 // TODO: convert 0.7 files to the new document format.
306 QDomNode westley = m_document.elementsByTagName("westley").at(1);
307 QDomNode tractor = m_document.elementsByTagName("tractor").at(0);
308 QDomNode kdenlivedoc = m_document.elementsByTagName("kdenlivedoc").at(0);
309 QDomElement kdenlivedoc_old = kdenlivedoc.cloneNode(true).toElement(); // Needed for folders
310 QDomNode multitrack = m_document.elementsByTagName("multitrack").at(0);
311 QDomNodeList playlists = m_document.elementsByTagName("playlist");
313 m_startPos = kdenlivedoc.toElement().attribute("timeline_position").toInt();
315 QDomNode props = m_document.elementsByTagName("properties").at(0).toElement();
316 QString profile = props.toElement().attribute("videoprofile");
317 if (profile == "dv_wide") profile = "dv_pal_wide";
318 setProfilePath(profile);
320 // move playlists outside of tractor and add the tracks instead
321 int max = playlists.count();
322 for (int i = 0; i < max; i++) {
323 QDomNode n = playlists.at(i);
324 westley.insertBefore(n, QDomNode());
325 QDomElement pl = n.toElement();
326 QDomElement track = m_document.createElement("track");
327 QString trackType = pl.attribute("hide");
328 if (!trackType.isEmpty()) track.setAttribute("hide", trackType);
329 QString playlist_id = pl.attribute("id");
330 if (playlist_id.isEmpty()) {
331 playlist_id = "black_track";
332 pl.setAttribute("id", playlist_id);
334 track.setAttribute("producer", playlist_id);
335 //tractor.appendChild(track);
336 #define KEEP_TRACK_ORDER 1
337 #ifdef KEEP_TRACK_ORDER
338 tractor.insertAfter(track, QDomNode());
340 // Insert the new track in an order that hopefully matches the 3 video, then 2 audio tracks of Kdenlive 0.7.0
341 // insertion sort - O( tracks*tracks )
342 // Note, this breaks _all_ transitions - but you can move them up and down afterwards.
343 QDomElement tractor_elem = tractor.toElement();
344 if (! tractor_elem.isNull()) {
345 QDomNodeList tracks = tractor_elem.elementsByTagName("track");
346 int size = tracks.size();
348 tractor.insertAfter(track, QDomNode());
350 bool inserted = false;
351 for (int i = 0; i < size; ++i) {
352 QDomElement track_elem = tracks.at(i).toElement();
353 if (track_elem.isNull()) {
354 tractor.insertAfter(track, QDomNode());
358 kDebug() << "playlist_id: " << playlist_id << " producer:" << track_elem.attribute("producer");
359 if (playlist_id < track_elem.attribute("producer")) {
360 tractor.insertBefore(track, track_elem);
366 // Reach here, no insertion, insert last
368 tractor.insertAfter(track, QDomNode());
372 kWarning() << "tractor was not a QDomElement";
373 tractor.insertAfter(track, QDomNode());
377 tractor.removeChild(multitrack);
379 //Â audio track mixing transitions should not be added to track view, so add required attribute
380 QDomNodeList transitions = m_document.elementsByTagName("transition");
381 max = transitions.count();
382 for (int i = 0; i < max; i++) {
383 QDomElement tr = transitions.at(i).toElement();
384 if (tr.attribute("combine") == "1" && tr.attribute("mlt_service") == "mix") {
385 QDomElement property = m_document.createElement("property");
386 property.setAttribute("name", "internal_added");
387 QDomText value = m_document.createTextNode("237");
388 property.appendChild(value);
389 tr.appendChild(property);
391 // convert transition
392 QDomNamedNodeMap attrs = tr.attributes();
393 for (unsigned int j = 0; j < attrs.count(); j++) {
394 QString attrName = attrs.item(j).nodeName();
395 if (attrName != "in" && attrName != "out" && attrName != "id") {
396 QDomElement property = m_document.createElement("property");
397 property.setAttribute("name", attrName);
398 QDomText value = m_document.createTextNode(attrs.item(j).nodeValue());
399 property.appendChild(value);
400 tr.appendChild(property);
406 //Â move transitions after tracks
407 for (int i = 0; i < max; i++) {
408 tractor.insertAfter(transitions.at(0), QDomNode());
411 // Fix filters format
412 QDomNodeList entries = m_document.elementsByTagName("entry");
413 max = entries.count();
414 for (int i = 0; i < max; i++) {
417 QDomNode m = entries.at(i).firstChild();
418 while (!m.isNull()) {
419 if (m.toElement().tagName() == "filter") {
420 QDomElement filt = m.toElement();
421 QDomNamedNodeMap attrs = filt.attributes();
422 QString current_id = filt.attribute("kdenlive_id");
423 if (current_id != last_id) {
425 last_id = current_id;
427 QDomElement e = m_document.createElement("property");
428 e.setAttribute("name", "kdenlive_ix");
429 QDomText value = m_document.createTextNode(QString::number(effectix));
430 e.appendChild(value);
432 for (int j = 0; j < attrs.count(); j++) {
433 QDomAttr a = attrs.item(j).toAttr();
435 kDebug() << " FILTER; adding :" << a.name() << ":" << a.value();
436 QDomElement e = m_document.createElement("property");
437 e.setAttribute("name", a.name());
438 QDomText value = m_document.createTextNode(a.value());
439 e.appendChild(value);
450 QDomNodeList filters = m_document.elementsByTagName("filter");
451 max = filters.count();
454 for (int i = 0; i < max; i++) {
455 QDomElement filt = filters.at(i).toElement();
456 QDomNamedNodeMap attrs = filt.attributes();
457 QString current_id = filt.attribute("kdenlive_id");
458 if (current_id != last_id) {
460 last_id = current_id;
462 QDomElement e = m_document.createElement("property");
463 e.setAttribute("name", "kdenlive_ix");
464 QDomText value = m_document.createTextNode(QString::number(1));
465 e.appendChild(value);
467 for (int j = 0; j < attrs.count(); j++) {
468 QDomAttr a = attrs.item(j).toAttr();
470 kDebug() << " FILTER; adding :" << a.name() << ":" << a.value();
471 QDomElement e = m_document.createElement("property");
472 e.setAttribute("name", a.name());
473 QDomText value = m_document.createTextNode(a.value());
474 e.appendChild(value);
481 QDomNodeList producers = westley.toElement().elementsByTagName("producer");
482 max = producers.count();
483 for (int i = 0; i < max; i++) {
484 QDomElement prod = producers.at(i).toElement();
485 if (prod.attribute("mlt_service") == "framebuffer") {
486 QString slowmotionprod = prod.attribute("resource");
487 slowmotionprod.replace(':', '?');
488 kDebug() << "// FOUND WRONG SLOWMO, new: " << slowmotionprod;
489 prod.setAttribute("resource", slowmotionprod);
492 //Â move producers to correct place, markers to a global list, fix clip descriptions
493 QDomElement markers = m_document.createElement("markers");
494 // This will get the westley producers:
495 producers = m_document.elementsByTagName("producer");
496 max = producers.count();
497 for (int i = 0; i < max; i++) {
498 QDomElement prod = producers.at(0).toElement();
499 QDomNode m = prod.firstChild();
501 if (m.toElement().tagName() == "markers") {
502 QDomNodeList prodchilds = m.childNodes();
503 int maxchild = prodchilds.count();
504 for (int k = 0; k < maxchild; k++) {
505 QDomElement mark = prodchilds.at(0).toElement();
506 mark.setAttribute("id", prod.attribute("id"));
507 markers.insertAfter(mark, QDomNode());
510 } else if (prod.attribute("type").toInt() == TEXT) {
511 // convert title clip
512 if (m.toElement().tagName() == "textclip") {
514 QDomElement titleclip = m.toElement();
515 QDomElement title = tdoc.createElement("kdenlivetitle");
516 tdoc.appendChild(title);
517 QDomNodeList objects = titleclip.childNodes();
518 int maxchild = objects.count();
519 for (int k = 0; k < maxchild; k++) {
521 QDomElement ob = objects.at(k).toElement();
522 if (ob.attribute("type") == "3") {
523 // text object - all of this goes into "xmldata"...
524 QDomElement item = tdoc.createElement("item");
525 item.setAttribute("z-index", ob.attribute("z"));
526 item.setAttribute("type", "QGraphicsTextItem");
527 QDomElement position = tdoc.createElement("position");
528 position.setAttribute("x", ob.attribute("x"));
529 position.setAttribute("y", ob.attribute("y"));
530 QDomElement content = tdoc.createElement("content");
531 content.setAttribute("font", ob.attribute("font_family"));
532 content.setAttribute("font-size", ob.attribute("font_size"));
533 content.setAttribute("font-bold", ob.attribute("bold"));
534 content.setAttribute("font-italic", ob.attribute("italic"));
535 content.setAttribute("font-underline", ob.attribute("underline"));
536 QString col = ob.attribute("color");
538 content.setAttribute("font-color", colorToString(c));
539 // todo: These fields are missing from the newly generated xmldata:
540 // transform, startviewport, endviewport, background
542 QDomText conttxt = tdoc.createTextNode(ob.attribute("text"));
543 content.appendChild(conttxt);
544 item.appendChild(position);
545 item.appendChild(content);
546 title.appendChild(item);
547 } else if (ob.attribute("type") == "5") {
549 QDomElement item = tdoc.createElement("item");
550 item.setAttribute("z-index", ob.attribute("z"));
551 item.setAttribute("type", "QGraphicsRectItem");
552 QDomElement position = tdoc.createElement("position");
553 position.setAttribute("x", ob.attribute("x"));
554 position.setAttribute("y", ob.attribute("y"));
555 QDomElement content = tdoc.createElement("content");
556 QString col = ob.attribute("color");
558 content.setAttribute("brushcolor", colorToString(c));
559 QString rect = "0,0,";
560 rect.append(ob.attribute("width"));
562 rect.append(ob.attribute("height"));
563 content.setAttribute("rect", rect);
564 item.appendChild(position);
565 item.appendChild(content);
566 title.appendChild(item);
569 prod.setAttribute("xmldata", tdoc.toString());
570 // mbd todo: This clearly does not work, as every title gets the same name - trying to leave it empty
571 // QStringList titleInfo = TitleWidget::getFreeTitleInfo(projectFolder());
572 // prod.setAttribute("titlename", titleInfo.at(0));
573 // prod.setAttribute("resource", titleInfo.at(1));
574 //kDebug()<<"TITLE DATA:\n"<<tdoc.toString();
576 } // End conversion of title clips.
578 } else if (m.isText()) {
579 QString comment = m.nodeValue();
580 if (!comment.isEmpty()) {
581 prod.setAttribute("description", comment);
586 int duration = prod.attribute("duration").toInt();
587 if (duration > 0) prod.setAttribute("out", QString::number(duration));
588 // The clip goes back in, but text clips should not go back in, at least not modified
589 westley.insertBefore(prod, QDomNode());
593 QDomNode westley0 = m_document.elementsByTagName("westley").at(0);
594 if (!markers.firstChild().isNull()) westley0.appendChild(markers);
597 // Convert as much of the kdenlivedoc as possible. Use the producer in westley
598 // First, remove the old stuff from westley, and add a new empty one
599 // Also, track the max id in order to use it for the adding of groups/folders
600 int max_kproducer_id = 0;
601 westley0.removeChild(kdenlivedoc);
602 QDomElement kdenlivedoc_new = m_document.createElement("kdenlivedoc");
603 kdenlivedoc_new.setAttribute("profile", profile);
604 // Add all the producers that has a ressource in westley
605 QDomElement westley_element = westley0.toElement();
606 if (westley_element.isNull()) {
607 kWarning() << "westley0 element in document was not a QDomElement - unable to add producers to new kdenlivedoc";
609 QDomNodeList wproducers = westley_element.elementsByTagName("producer");
610 int kmax = wproducers.count();
611 for (int i = 0; i < kmax; i++) {
612 QDomElement wproducer = wproducers.at(i).toElement();
613 if (wproducer.isNull()) {
614 kWarning() << "Found producer in westley0, that was not a QDomElement";
616 // We have to do slightly different things, depending on the type
617 kDebug() << "Converting producer element with type " << wproducer.attribute("type");
618 if (wproducer.attribute("type").toInt() == TEXT) {
619 kDebug() << "Found TEXT element in producer" << endl;
620 QDomElement kproducer = wproducer.cloneNode(true).toElement();
621 kproducer.setTagName("kdenlive_producer");
622 kdenlivedoc_new.appendChild(kproducer);
623 // todo: Perhaps needs some more changes here to "frequency", aspect ratio as a float, frame_size, channels, and later, ressource and title name
625 QDomElement kproducer = m_document.createElement("kdenlive_producer");
626 kproducer.setAttribute("id", wproducer.attribute("id"));
627 kproducer.setAttribute("description", wproducer.attribute("description"));
628 kproducer.setAttribute("resource", wproducer.attribute("resource"));
629 kproducer.setAttribute("type", wproducer.attribute("type"));
630 kdenlivedoc_new.appendChild(kproducer);
632 if (wproducer.attribute("id").toInt() > max_kproducer_id) {
633 max_kproducer_id = wproducer.attribute("id").toInt();
638 #define LOOKUP_FOLDER 1
640 // Look through all the folder elements of the old doc, for each folder, for each producer,
641 // get the id, look it up in the new doc, set the groupname and groupid
642 // Note, this does not work at the moment - at least one folders shows up missing, and clips with no folder
644 // QDomElement kdenlivedoc_old = kdenlivedoc.toElement();
645 if (!kdenlivedoc_old.isNull()) {
646 QDomNodeList folders = kdenlivedoc_old.elementsByTagName("folder");
647 int fsize = folders.size();
648 int groupId = max_kproducer_id + 1; // Start at +1 of max id of the kdenlive_producers
649 for (int i = 0; i < fsize; ++i) {
650 QDomElement folder = folders.at(i).toElement();
651 if (!folder.isNull()) {
652 QString groupName = folder.attribute("name");
653 kDebug() << "groupName: " << groupName << " with groupId: " << groupId;
654 QDomNodeList fproducers = folder.elementsByTagName("producer");
655 int psize = fproducers.size();
656 for (int j = 0; j < psize; ++j) {
657 QDomElement fproducer = fproducers.at(j).toElement();
658 if (!fproducer.isNull()) {
659 QString id = fproducer.attribute("id");
660 // This is not very effective, but compared to loading the clips, its a breeze
661 QDomNodeList kdenlive_producers = kdenlivedoc_new.elementsByTagName("kdenlive_producer");
662 int kpsize = kdenlive_producers.size();
663 for (int k = 0; k < kpsize; ++k) {
664 QDomElement kproducer = kdenlive_producers.at(k).toElement(); // Its an element for sure
665 if (id == kproducer.attribute("id")) {
666 // We do not check that it already is part of a folder
667 kproducer.setAttribute("groupid", groupId);
668 kproducer.setAttribute("groupname", groupName);
679 westley0.appendChild(kdenlivedoc_new);
681 QDomNodeList elements = westley.childNodes();
682 max = elements.count();
683 for (int i = 0; i < max; i++) {
684 QDomElement prod = elements.at(0).toElement();
685 westley0.insertAfter(prod, QDomNode());
688 westley0.removeChild(westley);
690 //kDebug() << "///////////////// CONVERTEDÂ DOC:";
691 // kDebug() << m_document.toString();
693 QFile file( "converted.kdenlive" );
694 if ( file.open( QIODevice::WriteOnly ) ) {
695 QTextStream stream( &file );
696 stream << m_document.toString();
699 kDebug() << "Unable to dump file to converted.kdenlive";
702 //kDebug() << "///////////////// END CONVERTEDÂ DOC:";
705 QString KdenliveDoc::colorToString(const QColor& c) {
706 QString ret = "%1,%2,%3,%4";
707 ret = ret.arg(c.red()).arg(c.green()).arg(c.blue()).arg(c.alpha());
711 bool KdenliveDoc::saveSceneList(const QString &path, QDomDocument sceneList) {
712 QDomNode wes = sceneList.elementsByTagName("westley").at(0);
714 QDomElement addedXml = sceneList.createElement("kdenlivedoc");
715 QDomElement markers = sceneList.createElement("markers");
716 addedXml.setAttribute("version", "0.8");
717 addedXml.setAttribute("profile", profilePath());
718 addedXml.setAttribute("position", m_render->seekPosition().frames(m_fps));
719 addedXml.setAttribute("zoom", m_zoom);
722 QList <DocClipBase*> list = m_clipManager->documentClipList();
723 for (int i = 0; i < list.count(); i++) {
724 e = list.at(i)->toXML();
725 e.setTagName("kdenlive_producer");
726 addedXml.appendChild(sceneList.importNode(e, true));
727 QList < CommentedTime > marks = list.at(i)->commentedSnapMarkers();
728 for (int j = 0; j < marks.count(); j++) {
729 QDomElement marker = sceneList.createElement("marker");
730 marker.setAttribute("time", marks.at(j).time().ms() / 1000);
731 marker.setAttribute("comment", marks.at(j).comment());
732 marker.setAttribute("id", e.attribute("id"));
733 markers.appendChild(marker);
736 addedXml.appendChild(markers);
737 if (!m_guidesXml.isNull()) addedXml.appendChild(sceneList.importNode(m_guidesXml, true));
739 wes.appendChild(addedXml);
740 //wes.appendChild(doc.importNode(kdenliveData, true));
743 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
744 kWarning() << "////// ERROR writing to file: " << path;
745 KMessageBox::error(kapp->activeWindow(), i18n("Cannot write to file %1", path));
748 QTextStream out(&file);
749 out << sceneList.toString();
754 QDomElement KdenliveDoc::documentInfoXml() {
757 QDomElement addedXml = doc.createElement("kdenlivedoc");
758 QDomElement markers = doc.createElement("markers");
759 addedXml.setAttribute("version", "0.7");
760 addedXml.setAttribute("profile", profilePath());
761 addedXml.setAttribute("position", m_render->seekPosition().frames(m_fps));
762 addedXml.setAttribute("zoom", m_zoom);
763 QList <DocClipBase*> list = m_clipManager->documentClipList();
764 for (int i = 0; i < list.count(); i++) {
765 e = list.at(i)->toXML();
766 e.setTagName("kdenlive_producer");
767 addedXml.appendChild(doc.importNode(e, true));
768 QList < CommentedTime > marks = list.at(i)->commentedSnapMarkers();
769 for (int j = 0; j < marks.count(); j++) {
770 QDomElement marker = doc.createElement("marker");
771 marker.setAttribute("time", marks.at(j).time().ms() / 1000);
772 marker.setAttribute("comment", marks.at(j).comment());
773 marker.setAttribute("id", e.attribute("id"));
774 markers.appendChild(marker);
777 addedXml.appendChild(markers);
778 if (!m_guidesXml.isNull()) addedXml.appendChild(doc.importNode(m_guidesXml, true));
779 //kDebug() << m_document.toString();
784 ClipManager *KdenliveDoc::clipManager() {
785 return m_clipManager;
788 KUrl KdenliveDoc::projectFolder() const {
789 if (m_projectFolder.isEmpty()) return KUrl(KStandardDirs::locateLocal("appdata", "/projects/"));
790 return m_projectFolder;
793 QString KdenliveDoc::profilePath() const {
794 return m_profile.path;
797 MltVideoProfile KdenliveDoc::mltProfile() const {
801 void KdenliveDoc::setProfilePath(QString path) {
802 if (path.isEmpty()) path = KdenliveSettings::default_profile();
803 if (path.isEmpty()) path = "dv_pal";
804 m_profile = ProfilesDialog::getVideoProfile(path);
805 KdenliveSettings::setProject_display_ratio((double) m_profile.display_aspect_num / m_profile.display_aspect_den);
806 m_fps = (double) m_profile.frame_rate_num / m_profile.frame_rate_den;
807 m_width = m_profile.width;
808 m_height = m_profile.height;
809 kDebug() << "KDEnnlive document, init timecode from path: " << path << ", " << m_fps;
810 if (m_fps == 30000.0 / 1001.0) m_timecode.setFormat(30, true);
811 else m_timecode.setFormat((int) m_fps);
814 const double KdenliveDoc::dar() {
815 return (double) m_profile.display_aspect_num / m_profile.display_aspect_den;
818 void KdenliveDoc::setThumbsProgress(const QString &message, int progress) {
819 emit progressInfo(message, progress);
822 void KdenliveDoc::loadingProgressed() {
823 m_documentLoadingProgress += m_documentLoadingStep;
824 emit progressInfo(QString(), (int) m_documentLoadingProgress);
827 QUndoStack *KdenliveDoc::commandStack() {
828 return m_commandStack;
831 void KdenliveDoc::setRenderer(Render *render) {
832 if (m_render) return;
834 emit progressInfo(i18n("Loading playlist..."), 0);
835 //qApp->processEvents();
837 m_render->setSceneList(m_document.toString(), m_startPos);
840 emit progressInfo(QString(), -1);
843 void KdenliveDoc::checkProjectClips() {
844 if (m_render == NULL) return;
845 QList <Mlt::Producer *> prods = m_render->producersList();
849 for (int i = 0; i < prods.count(); i++) {
850 id = prods.at(i)->get("id");
851 prodId = id.section('_', 0, 0);
852 prodTrack = id.section('_', 1, 1);
853 kDebug() << "CHECK PRO CLIP, ID: " << id;
854 DocClipBase *clip = m_clipManager->getClipById(prodId);
855 if (clip) clip->setProducer(prods.at(i));
856 kDebug() << "CHECK PRO CLIP, ID: " << id << " DONE";
857 if (clip && clip->clipType() == TEXT && !QFile::exists(clip->fileURL().path())) {
858 // regenerate text clip image if required
859 kDebug() << "// TITLE: " << clip->getProperty("titlename") << " Preview file: " << clip->getProperty("resource") << " DOES NOT EXIST";
860 QString titlename = clip->getProperty("titlename");
861 QString titleresource;
862 if (titlename.isEmpty()) {
863 QStringList titleInfo = TitleWidget::getFreeTitleInfo(projectFolder());
864 titlename = titleInfo.at(0);
865 titleresource = titleInfo.at(1);
866 clip->setProperty("titlename", titlename);
867 kDebug() << "// New title set to: " << titlename;
869 titleresource = TitleWidget::getTitleResourceFromName(projectFolder(), titlename);
871 QString titlepath = projectFolder().path() + "/titles/";
872 TitleWidget *dia_ui = new TitleWidget(KUrl(), titlepath, m_render, kapp->activeWindow());
874 doc.setContent(clip->getProperty("xmldata"));
876 QPixmap pix = dia_ui->renderedPixmap();
877 pix.save(titleresource);
878 clip->setProperty("resource", titleresource);
880 clip->producer()->set("force_reload", 1);
885 Render *KdenliveDoc::renderer() {
889 void KdenliveDoc::updateClip(const QString &id) {
890 emit updateClipDisplay(id);
893 int KdenliveDoc::getFramePos(QString duration) {
894 return m_timecode.getFrameCount(duration, m_fps);
897 QString KdenliveDoc::producerName(const QString &id) {
898 QString result = "unnamed";
899 QDomNodeList prods = producersList();
900 int ct = prods.count();
901 for (int i = 0; i < ct ; i++) {
902 QDomElement e = prods.item(i).toElement();
903 if (e.attribute("id") != "black" && e.attribute("id") == id) {
904 result = e.attribute("name");
905 if (result.isEmpty()) result = KUrl(e.attribute("resource")).fileName();
912 void KdenliveDoc::setProducerDuration(const QString &id, int duration) {
913 QDomNodeList prods = producersList();
914 int ct = prods.count();
915 for (int i = 0; i < ct ; i++) {
916 QDomElement e = prods.item(i).toElement();
917 if (e.attribute("id") != "black" && e.attribute("id") == id) {
918 e.setAttribute("duration", QString::number(duration));
924 int KdenliveDoc::getProducerDuration(const QString &id) {
926 QDomNodeList prods = producersList();
927 int ct = prods.count();
928 for (int i = 0; i < ct ; i++) {
929 QDomElement e = prods.item(i).toElement();
930 if (e.attribute("id") != "black" && e.attribute("id") == id) {
931 result = e.attribute("duration").toInt();
939 QDomDocument KdenliveDoc::generateSceneList() {
941 QDomElement westley = doc.createElement("westley");
942 doc.appendChild(westley);
943 QDomElement prod = doc.createElement("producer");
946 QDomDocument KdenliveDoc::toXml() const {
950 Timecode KdenliveDoc::timecode() const {
954 QDomNodeList KdenliveDoc::producersList() {
955 return m_document.elementsByTagName("producer");
958 void KdenliveDoc::backupMltPlaylist() {
959 if (m_render) m_scenelist = m_render->sceneList();
962 double KdenliveDoc::projectDuration() const {
963 if (m_render) return GenTime(m_render->getLength(), m_fps).ms() / 1000;
966 double KdenliveDoc::fps() const {
970 int KdenliveDoc::width() const {
974 int KdenliveDoc::height() const {
978 KUrl KdenliveDoc::url() const {
982 void KdenliveDoc::setUrl(KUrl url) {
986 void KdenliveDoc::setModified(bool mod) {
987 if (!m_url.isEmpty() && mod && KdenliveSettings::crashrecovery()) {
988 m_autoSaveTimer->start(3000);
990 if (mod == m_modified) return;
992 emit docModified(m_modified);
995 bool KdenliveDoc::isModified() const {
999 QString KdenliveDoc::description() const {
1000 if (m_url.isEmpty())
1001 return i18n("Untitled") + " / " + m_profile.description;
1003 return m_url.fileName() + " / " + m_profile.description;
1006 void KdenliveDoc::addClip(QDomElement elem, QString clipId, bool createClipItem) {
1007 const QString producerId = clipId.section('_', 0, 0);
1008 int subtrack = clipId.section('_', 1, 1).toInt();
1009 DocClipBase *clip = m_clipManager->getClipById(producerId);
1011 elem.setAttribute("id", producerId);
1012 QString path = elem.attribute("resource");
1014 if (elem.attribute("type").toInt() == SLIDESHOW) {
1015 extension = KUrl(path).fileName();
1016 path = KUrl(path).directory();
1018 if (!path.isEmpty() && !QFile::exists(path)) {
1019 const QString size = elem.attribute("file_size");
1020 const QString hash = elem.attribute("file_hash");
1022 KMessageBox::ButtonCode action = KMessageBox::No;
1023 if (!size.isEmpty() && !hash.isEmpty()) {
1024 if (!m_searchFolder.isEmpty()) newpath = searchFileRecursively(m_searchFolder, size, hash);
1025 else action = (KMessageBox::ButtonCode)KMessageBox::messageBox(kapp->activeWindow(), KMessageBox::WarningYesNo, i18n("<qt>Clip <b>%1</b><br>is invalid, what do you want to do?", path), i18n("File not found"), KGuiItem(i18n("Search automatically")), /*KGuiItem(i18n("Remove from project")), */KGuiItem(i18n("Keep as placeholder")));
1027 if (elem.attribute("type").toInt() == SLIDESHOW) {
1028 if (KMessageBox::messageBox(kapp->activeWindow(), KMessageBox::WarningYesNo, i18n("<qt>Clip <b>%1</b><br>is invalid, what do you want to do?", path), i18n("File not found"), KGuiItem(i18n("Search automatically")), /*KGuiItem(i18n("Remove from project")),*/ KGuiItem(i18n("Keep as placeholder"))) == KMessageBox::Yes)
1029 newpath = KFileDialog::getExistingDirectory(KUrl("kfiledialog:///clipfolder"), kapp->activeWindow(), i18n("Looking for %1", path));
1030 } else newpath = KFileDialog::getOpenFileName(KUrl("kfiledialog:///clipfolder"), QString(), kapp->activeWindow(), i18n("Looking for %1", path));
1032 if (action == KMessageBox::Yes) {
1033 kDebug() << "// ASKED FOR SRCH CLIP: " << clipId;
1034 m_searchFolder = KFileDialog::getExistingDirectory(KUrl("kfiledialog:///clipfolder"), kapp->activeWindow());
1035 if (!m_searchFolder.isEmpty()) {
1036 newpath = searchFileRecursively(QDir(m_searchFolder), size, hash);
1039 if (!newpath.isEmpty()) {
1040 if (elem.attribute("type").toInt() == SLIDESHOW) newpath.append('/' + extension);
1041 elem.setAttribute("resource", newpath);
1042 setNewClipResource(clipId, newpath);
1046 clip = new DocClipBase(m_clipManager, elem, producerId);
1047 m_clipManager->addClip(clip);
1049 if (createClipItem) emit addProjectClip(clip);
1052 void KdenliveDoc::setNewClipResource(const QString &id, const QString &path) {
1053 QDomNodeList prods = m_document.elementsByTagName("producer");
1054 int maxprod = prods.count();
1055 for (int i = 0; i < maxprod; i++) {
1056 QDomNode m = prods.at(i);
1057 QString prodId = m.toElement().attribute("id");
1058 if (prodId == id || prodId.startsWith(id + "_")) {
1059 QDomNodeList params = m.childNodes();
1060 for (int j = 0; j < params.count(); j++) {
1061 QDomElement e = params.item(j).toElement();
1062 if (e.attribute("name") == "resource") {
1063 e.firstChild().setNodeValue(path);
1071 QString KdenliveDoc::searchFileRecursively(const QDir &dir, const QString &matchSize, const QString &matchHash) const {
1072 QString foundFileName;
1073 QByteArray fileData;
1074 QByteArray fileHash;
1075 QStringList filesAndDirs = dir.entryList(QDir::Files | QDir::Readable);
1076 for (int i = 0; i < filesAndDirs.size() && foundFileName.isEmpty(); i++) {
1077 QFile file(dir.absoluteFilePath(filesAndDirs.at(i)));
1078 if (file.open(QIODevice::ReadOnly)) {
1079 if (QString::number(file.size()) == matchSize) {
1081 * 1 MB = 1 second per 450 files (or faster)
1082 * 10 MB = 9 seconds per 450 files (or faster)
1084 if (file.size() > 1000000*2) {
1085 fileData = file.read(1000000);
1086 if (file.seek(file.size() - 1000000))
1087 fileData.append(file.readAll());
1089 fileData = file.readAll();
1091 fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
1092 if (QString(fileHash.toHex()) == matchHash)
1093 return file.fileName();
1096 kDebug() << filesAndDirs.at(i) << file.size() << fileHash.toHex();
1098 filesAndDirs = dir.entryList(QDir::Dirs | QDir::Readable | QDir::Executable | QDir::NoDotAndDotDot);
1099 for (int i = 0; i < filesAndDirs.size() && foundFileName.isEmpty(); i++) {
1100 foundFileName = searchFileRecursively(dir.absoluteFilePath(filesAndDirs.at(i)), matchSize, matchHash);
1101 if (!foundFileName.isEmpty())
1104 return foundFileName;
1107 void KdenliveDoc::addClipInfo(QDomElement elem, QString clipId) {
1108 DocClipBase *clip = m_clipManager->getClipById(clipId);
1110 addClip(elem, clipId);
1112 QMap <QString, QString> properties;
1113 QDomNamedNodeMap attributes = elem.attributes();
1115 for (unsigned int i = 0; i < attributes.count(); i++) {
1116 attrname = attributes.item(i).nodeName();
1117 if (attrname != "resource")
1118 properties.insert(attrname, attributes.item(i).nodeValue());
1119 kDebug() << attrname << " = " << attributes.item(i).nodeValue();
1121 clip->setProperties(properties);
1122 emit addProjectClip(clip);
1126 void KdenliveDoc::addFolder(const QString foldername, const QString &clipId, bool edit) {
1127 emit addProjectFolder(foldername, clipId, false, edit);
1130 void KdenliveDoc::deleteFolder(const QString foldername, const QString &clipId) {
1131 emit addProjectFolder(foldername, clipId, true);
1134 void KdenliveDoc::deleteProjectClip(QList <QString> ids) {
1135 for (int i = 0; i < ids.size(); ++i) {
1136 emit deleteTimelineClip(ids.at(i));
1137 m_clipManager->slotDeleteClip(ids.at(i));
1142 void KdenliveDoc::deleteProjectFolder(QMap <QString, QString> map) {
1143 QMapIterator<QString, QString> i(map);
1144 while (i.hasNext()) {
1146 slotDeleteFolder(i.key(), i.value());
1151 void KdenliveDoc::deleteClip(const QString &clipId) {
1152 emit signalDeleteProjectClip(clipId);
1153 m_clipManager->deleteClip(clipId);
1156 void KdenliveDoc::slotAddClipList(const KUrl::List urls, const QString group, const QString &groupId) {
1157 m_clipManager->slotAddClipList(urls, group, groupId);
1158 emit selectLastAddedClip(QString::number(m_clipManager->lastClipId()));
1163 void KdenliveDoc::slotAddClipFile(const KUrl url, const QString group, const QString &groupId) {
1164 kDebug() << "///////// DOCUM, ADD CLP: " << url;
1165 m_clipManager->slotAddClipFile(url, group, groupId);
1166 emit selectLastAddedClip(QString::number(m_clipManager->lastClipId()));
1170 void KdenliveDoc::slotAddFolder(const QString folderName) {
1171 AddFolderCommand *command = new AddFolderCommand(this, folderName, QString::number(m_clipManager->getFreeClipId()), true);
1172 commandStack()->push(command);
1176 void KdenliveDoc::slotDeleteFolder(const QString folderName, const QString &id) {
1177 AddFolderCommand *command = new AddFolderCommand(this, folderName, id, false);
1178 commandStack()->push(command);
1182 void KdenliveDoc::slotEditFolder(const QString newfolderName, const QString oldfolderName, const QString &clipId) {
1183 EditFolderCommand *command = new EditFolderCommand(this, newfolderName, oldfolderName, clipId, false);
1184 commandStack()->push(command);
1188 const QString&KdenliveDoc::getFreeClipId() {
1189 return QString::number(m_clipManager->getFreeClipId());
1192 DocClipBase *KdenliveDoc::getBaseClip(const QString &clipId) {
1193 return m_clipManager->getClipById(clipId);
1196 void KdenliveDoc::slotAddColorClipFile(const QString name, const QString color, QString duration, const QString group, const QString &groupId) {
1197 m_clipManager->slotAddColorClipFile(name, color, duration, group, groupId);
1201 void KdenliveDoc::slotAddSlideshowClipFile(const QString name, const QString path, int count, const QString duration, const bool loop, const bool fade, const QString &luma_duration, const QString &luma_file, const int softness, const QString group, const QString &groupId) {
1202 m_clipManager->slotAddSlideshowClipFile(name, path, count, duration, loop, fade, luma_duration, luma_file, softness, group, groupId);
1206 void KdenliveDoc::slotCreateTextClip(QString group, const QString &groupId) {
1207 QString titlesFolder = projectFolder().path() + "/titles/";
1208 KStandardDirs::makeDir(titlesFolder);
1209 TitleWidget *dia_ui = new TitleWidget(KUrl(), titlesFolder, m_render, kapp->activeWindow());
1210 if (dia_ui->exec() == QDialog::Accepted) {
1211 QStringList titleInfo = TitleWidget::getFreeTitleInfo(projectFolder());
1212 QPixmap pix = dia_ui->renderedPixmap();
1213 pix.save(titleInfo.at(1));
1214 //dia_ui->saveTitle(path + ".kdenlivetitle");
1215 m_clipManager->slotAddTextClipFile(titleInfo.at(0), titleInfo.at(1), dia_ui->xml().toString(), QString(), QString());
1223 #include "kdenlivedoc.moc"