1 /***************************************************************************
2 * Copyright (C) 2008 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 "clipmanager.h"
22 #include "addclipcommand.h"
23 #include "kdenlivesettings.h"
24 #include "docclipbase.h"
25 #include "kdenlivedoc.h"
26 #include "abstractclipitem.h"
27 #include "abstractgroupitem.h"
28 #include "titledocument.h"
30 #include <mlt++/Mlt.h>
33 #include <KMessageBox>
34 #include <KFileDialog>
35 #include <KApplication>
36 #include <kio/netaccess.h>
38 #include <QGraphicsItemGroup>
40 #include <KFileMetaInfo>
42 ClipManager::ClipManager(KdenliveDoc *doc) :
49 m_folderIdCounter = 1;
50 m_modifiedTimer.setInterval(1500);
51 connect(&m_fileWatcher, SIGNAL(dirty(const QString &)), this, SLOT(slotClipModified(const QString &)));
52 connect(&m_fileWatcher, SIGNAL(deleted(const QString &)), this, SLOT(slotClipMissing(const QString &)));
53 connect(&m_fileWatcher, SIGNAL(created(const QString &)), this, SLOT(slotClipAvailable(const QString &)));
54 connect(&m_modifiedTimer, SIGNAL(timeout()), this, SLOT(slotProcessModifiedClips()));
57 ClipManager::~ClipManager()
59 qDeleteAll(m_clipList);
61 m_audioThumbsQueue.clear();
62 m_generatingAudioId.clear();
65 void ClipManager::clear()
67 qDeleteAll(m_clipList);
70 m_folderIdCounter = 1;
72 m_audioThumbsQueue.clear();
73 m_modifiedClips.clear();
76 void ClipManager::checkAudioThumbs()
78 if (!KdenliveSettings::audiothumbnails()) {
79 if (!m_generatingAudioId.isEmpty()) {
80 DocClipBase *clip = getClipById(m_generatingAudioId);
81 if (clip) clip->slotClearAudioCache();
83 m_audioThumbsQueue.clear();
84 m_generatingAudioId.clear();
88 for (int i = 0; i < m_clipList.count(); i++) {
89 m_audioThumbsQueue.append(m_clipList.at(i)->getId());
91 if (m_generatingAudioId.isEmpty()) startAudioThumbsGeneration();
94 void ClipManager::askForAudioThumb(const QString &id)
96 DocClipBase *clip = getClipById(id);
97 if (clip && KdenliveSettings::audiothumbnails()) {
98 m_audioThumbsQueue.append(id);
99 if (m_generatingAudioId.isEmpty()) startAudioThumbsGeneration();
103 void ClipManager::startAudioThumbsGeneration()
105 if (!KdenliveSettings::audiothumbnails()) {
106 m_audioThumbsQueue.clear();
107 m_generatingAudioId.clear();
110 if (!m_audioThumbsQueue.isEmpty()) {
111 m_generatingAudioId = m_audioThumbsQueue.takeFirst();
112 DocClipBase *clip = getClipById(m_generatingAudioId);
113 if (!clip || !clip->slotGetAudioThumbs())
114 endAudioThumbsGeneration(m_generatingAudioId);
116 m_generatingAudioId.clear();
120 void ClipManager::endAudioThumbsGeneration(const QString &requestedId)
122 if (!KdenliveSettings::audiothumbnails()) {
123 m_audioThumbsQueue.clear();
124 m_generatingAudioId.clear();
127 if (!m_audioThumbsQueue.isEmpty()) {
128 if (m_generatingAudioId == requestedId) {
129 startAudioThumbsGeneration();
132 m_generatingAudioId.clear();
136 void ClipManager::setThumbsProgress(const QString &message, int progress)
138 m_doc->setThumbsProgress(message, progress);
141 QList <DocClipBase*> ClipManager::documentClipList() const
146 QMap <QString, QString> ClipManager::documentFolderList() const
151 void ClipManager::addClip(DocClipBase *clip)
153 m_clipList.append(clip);
154 if (clip->clipType() != COLOR && clip->clipType() != SLIDESHOW && !clip->fileURL().isEmpty()) {
155 // listen for file change
156 //kDebug() << "// LISTEN FOR: " << clip->fileURL().path();
157 m_fileWatcher.addFile(clip->fileURL().path());
159 const QString id = clip->getId();
160 if (id.toInt() >= m_clipIdCounter) m_clipIdCounter = id.toInt() + 1;
161 const QString gid = clip->getProperty("groupid");
162 if (!gid.isEmpty() && gid.toInt() >= m_folderIdCounter) m_folderIdCounter = gid.toInt() + 1;
165 void ClipManager::slotDeleteClips(QStringList ids)
167 QUndoCommand *delClips = new QUndoCommand();
168 delClips->setText(i18np("Delete clip", "Delete clips", ids.size()));
170 for (int i = 0; i < ids.size(); i++) {
171 DocClipBase *clip = getClipById(ids.at(i));
173 new AddClipCommand(m_doc, clip->toXML(), ids.at(i), false, delClips);
176 m_doc->commandStack()->push(delClips);
179 void ClipManager::deleteClip(const QString &clipId)
181 for (int i = 0; i < m_clipList.count(); i++) {
182 if (m_clipList.at(i)->getId() == clipId) {
183 if (m_clipList.at(i)->clipType() != COLOR && m_clipList.at(i)->clipType() != SLIDESHOW && !m_clipList.at(i)->fileURL().isEmpty()) {
184 //if (m_clipList.at(i)->clipType() == IMAGE || m_clipList.at(i)->clipType() == AUDIO || (m_clipList.at(i)->clipType() == TEXT && !m_clipList.at(i)->fileURL().isEmpty())) {
185 // listen for file change
186 m_fileWatcher.removeFile(m_clipList.at(i)->fileURL().path());
188 DocClipBase *clip = m_clipList.takeAt(i);
196 DocClipBase *ClipManager::getClipAt(int pos)
198 return m_clipList.at(pos);
201 DocClipBase *ClipManager::getClipById(QString clipId)
203 //kDebug() << "++++ CLIP MAN, LOOKING FOR CLIP ID: " << clipId;
204 clipId = clipId.section('_', 0, 0);
205 for (int i = 0; i < m_clipList.count(); i++) {
206 if (m_clipList.at(i)->getId() == clipId) {
207 //kDebug() << "++++ CLIP MAN, FOUND FOR CLIP ID: " << clipId;
208 return m_clipList.at(i);
214 const QList <DocClipBase *> ClipManager::getClipByResource(QString resource)
216 QList <DocClipBase *> list;
217 QString clipResource;
218 for (int i = 0; i < m_clipList.count(); i++) {
219 clipResource = m_clipList.at(i)->getProperty("resource");
220 if (clipResource.isEmpty()) clipResource = m_clipList.at(i)->getProperty("colour");
221 if (clipResource == resource) {
222 list.append(m_clipList.at(i));
229 void ClipManager::clearUnusedProducers()
231 for (int i = 0; i < m_clipList.count(); i++) {
232 if (m_clipList.at(i)->numReferences() == 0) m_clipList.at(i)->deleteProducers();
236 void ClipManager::resetProducersList(const QList <Mlt::Producer *> prods)
238 for (int i = 0; i < m_clipList.count(); i++) {
239 if (m_clipList.at(i)->numReferences() > 0) {
240 m_clipList.at(i)->clearProducers();
244 for (int i = 0; i < prods.count(); i++) {
245 id = prods.at(i)->get("id");
246 kDebug() << "// // // REPLACE CLIP: " << id;
247 if (id.contains('_')) id = id.section('_', 0, 0);
248 DocClipBase *clip = getClipById(id);
250 clip->setProducer(prods.at(i));
253 emit checkAllClips();
256 void ClipManager::slotAddClipList(const KUrl::List urls, const QString group, const QString &groupId)
258 QUndoCommand *addClips = new QUndoCommand();
260 foreach(const KUrl & file, urls) {
261 if (KIO::NetAccess::exists(file, KIO::NetAccess::SourceSide, NULL)) {
262 if (!getClipByResource(file.path()).empty()) {
263 if (KMessageBox::warningContinueCancel(kapp->activeWindow(), i18n("Clip <b>%1</b><br />already exists in project, what do you want to do?", file.path()), i18n("Clip already exists")) == KMessageBox::Cancel)
266 kDebug() << "Adding clip: " << file.path();
268 QDomElement prod = doc.createElement("producer");
269 doc.appendChild(prod);
270 prod.setAttribute("resource", file.path());
271 uint id = m_clipIdCounter++;
272 prod.setAttribute("id", QString::number(id));
273 if (!group.isEmpty()) {
274 prod.setAttribute("groupname", group);
275 prod.setAttribute("groupid", groupId);
277 KMimeType::Ptr type = KMimeType::findByUrl(file);
278 if (type->name().startsWith("image/")) {
279 prod.setAttribute("type", (int) IMAGE);
280 prod.setAttribute("in", 0);
281 prod.setAttribute("out", m_doc->getFramePos(KdenliveSettings::image_duration()) - 1);
282 // Read EXIF metadata for JPEG
283 if (type->is("image/jpeg")) {
284 KFileMetaInfo metaInfo(file.path(), QString("image/jpeg"), KFileMetaInfo::TechnicalInfo);
285 const QHash<QString, KFileMetaInfoItem> metaInfoItems = metaInfo.items();
286 foreach(const KFileMetaInfoItem & metaInfoItem, metaInfoItems) {
287 prod.setAttribute("meta.attr." + metaInfoItem.name().section("#", 1), metaInfoItem.value().toString());
290 } else if (type->is("application/x-kdenlivetitle")) {
291 // opening a title file
292 QDomDocument txtdoc("titledocument");
293 QFile txtfile(file.path());
294 if (txtfile.open(QIODevice::ReadOnly) && txtdoc.setContent(&txtfile)) {
296 prod.setAttribute("type", (int) TEXT);
297 // extract embeded images
298 QDomNodeList items = txtdoc.elementsByTagName("content");
299 for (int i = 0; i < items.count() ; i++) {
300 QDomElement content = items.item(i).toElement();
301 if (content.hasAttribute("base64")) {
302 QString titlesFolder = m_doc->projectFolder().path(KUrl::AddTrailingSlash) + "titles/";
303 QString path = TitleDocument::extractBase64Image(titlesFolder, content.attribute("base64"));
304 if (!path.isEmpty()) {
305 content.setAttribute("url", path);
306 content.removeAttribute("base64");
310 QString titleData = txtdoc.toString();
311 prod.setAttribute("xmldata", titleData);
312 prod.setAttribute("transparency", 1);
313 prod.setAttribute("in", 0);
314 int out = txtdoc.documentElement().attribute("out").toInt();
316 prod.setAttribute("out", out);
318 prod.setAttribute("out", m_doc->getFramePos(KdenliveSettings::title_duration()) - 1);
322 new AddClipCommand(m_doc, doc.documentElement(), QString::number(id), true, addClips);
325 if (addClips->childCount() > 0) {
326 addClips->setText(i18np("Add clip", "Add clips", addClips->childCount()));
327 m_doc->commandStack()->push(addClips);
331 void ClipManager::slotAddClipFile(const KUrl url, const QString group, const QString &groupId)
333 slotAddClipList(KUrl::List(url), group, groupId);
336 void ClipManager::slotAddXmlClipFile(const QString name, const QDomElement xml, const QString group, const QString &groupId)
339 doc.appendChild(doc.importNode(xml, true));
340 QDomElement prod = doc.documentElement();
341 prod.setAttribute("type", (int) PLAYLIST);
342 uint id = m_clipIdCounter++;
343 prod.setAttribute("id", QString::number(id));
344 prod.setAttribute("name", name);
345 if (!group.isEmpty()) {
346 prod.setAttribute("groupname", group);
347 prod.setAttribute("groupid", groupId);
349 AddClipCommand *command = new AddClipCommand(m_doc, doc.documentElement(), QString::number(id), true);
350 m_doc->commandStack()->push(command);
353 void ClipManager::slotAddColorClipFile(const QString name, const QString color, QString duration, const QString group, const QString &groupId)
356 QDomElement prod = doc.createElement("producer");
357 doc.appendChild(prod);
358 prod.setAttribute("mlt_service", "colour");
359 prod.setAttribute("colour", color);
360 prod.setAttribute("type", (int) COLOR);
361 uint id = m_clipIdCounter++;
362 prod.setAttribute("id", QString::number(id));
363 prod.setAttribute("in", "0");
364 prod.setAttribute("out", m_doc->getFramePos(duration) - 1);
365 prod.setAttribute("name", name);
366 if (!group.isEmpty()) {
367 prod.setAttribute("groupname", group);
368 prod.setAttribute("groupid", groupId);
370 AddClipCommand *command = new AddClipCommand(m_doc, doc.documentElement(), QString::number(id), true);
371 m_doc->commandStack()->push(command);
374 void ClipManager::slotAddSlideshowClipFile(const QString name, const QString path, int count, const QString duration,
375 const bool loop, const bool crop, const bool fade,
376 const QString &luma_duration, const QString &luma_file, const int softness,
377 const QString &animation, QString group, const QString &groupId)
380 QDomElement prod = doc.createElement("producer");
381 doc.appendChild(prod);
382 prod.setAttribute("resource", path);
383 prod.setAttribute("type", (int) SLIDESHOW);
384 uint id = m_clipIdCounter++;
385 prod.setAttribute("id", QString::number(id));
386 prod.setAttribute("in", "0");
387 prod.setAttribute("out", m_doc->getFramePos(duration) * count - 1);
388 prod.setAttribute("ttl", m_doc->getFramePos(duration));
389 prod.setAttribute("luma_duration", m_doc->getFramePos(luma_duration));
390 prod.setAttribute("name", name);
391 prod.setAttribute("loop", loop);
392 prod.setAttribute("crop", crop);
393 prod.setAttribute("fade", fade);
394 prod.setAttribute("softness", QString::number(softness));
395 prod.setAttribute("luma_file", luma_file);
396 prod.setAttribute("animation", animation);
397 if (!group.isEmpty()) {
398 prod.setAttribute("groupname", group);
399 prod.setAttribute("groupid", groupId);
401 AddClipCommand *command = new AddClipCommand(m_doc, doc.documentElement(), QString::number(id), true);
402 m_doc->commandStack()->push(command);
407 void ClipManager::slotAddTextClipFile(const QString titleName, int out, const QString xml, const QString group, const QString &groupId)
410 QDomElement prod = doc.createElement("producer");
411 doc.appendChild(prod);
412 //prod.setAttribute("resource", imagePath);
413 prod.setAttribute("name", titleName);
414 prod.setAttribute("xmldata", xml);
415 uint id = m_clipIdCounter++;
416 prod.setAttribute("id", QString::number(id));
417 if (!group.isEmpty()) {
418 prod.setAttribute("groupname", group);
419 prod.setAttribute("groupid", groupId);
421 prod.setAttribute("type", (int) TEXT);
422 prod.setAttribute("transparency", "1");
423 prod.setAttribute("in", "0");
424 prod.setAttribute("out", out);
425 AddClipCommand *command = new AddClipCommand(m_doc, doc.documentElement(), QString::number(id), true);
426 m_doc->commandStack()->push(command);
429 void ClipManager::slotAddTextTemplateClip(QString titleName, const KUrl path, const QString group, const QString &groupId)
432 QDomElement prod = doc.createElement("producer");
433 doc.appendChild(prod);
434 prod.setAttribute("name", titleName);
435 prod.setAttribute("resource", path.path());
436 uint id = m_clipIdCounter++;
437 prod.setAttribute("id", QString::number(id));
438 if (!group.isEmpty()) {
439 prod.setAttribute("groupname", group);
440 prod.setAttribute("groupid", groupId);
442 prod.setAttribute("type", (int) TEXT);
443 prod.setAttribute("transparency", "1");
444 prod.setAttribute("in", "0");
447 QDomDocument titledoc;
448 QFile txtfile(path.path());
449 if (txtfile.open(QIODevice::ReadOnly) && titledoc.setContent(&txtfile)) {
451 out = titledoc.documentElement().attribute("out").toInt();
452 } else txtfile.close();
454 if (out == 0) out = m_doc->getFramePos(KdenliveSettings::image_duration()) - 1;
455 prod.setAttribute("out", out);
457 AddClipCommand *command = new AddClipCommand(m_doc, doc.documentElement(), QString::number(id), true);
458 m_doc->commandStack()->push(command);
461 int ClipManager::getFreeClipId()
463 return m_clipIdCounter++;
466 int ClipManager::getFreeFolderId()
468 return m_folderIdCounter++;
471 int ClipManager::lastClipId() const
473 return m_clipIdCounter - 1;
476 QString ClipManager::projectFolder() const
478 return m_doc->projectFolder().path();
481 void ClipManager::addFolder(const QString &id, const QString &name)
483 m_folderList.insert(id, name);
486 void ClipManager::deleteFolder(const QString &id)
488 m_folderList.remove(id);
491 AbstractGroupItem *ClipManager::createGroup()
493 AbstractGroupItem *group = new AbstractGroupItem(m_doc->fps());
494 m_groupsList.append(group);
498 void ClipManager::removeGroup(AbstractGroupItem *group)
500 m_groupsList.removeAll(group);
503 QDomElement ClipManager::groupsXml() const
506 QDomElement groups = doc.createElement("groups");
507 doc.appendChild(groups);
508 for (int i = 0; i < m_groupsList.count(); i++) {
509 QDomElement group = doc.createElement("group");
510 groups.appendChild(group);
511 QList <QGraphicsItem *> children = m_groupsList.at(i)->childItems();
512 for (int j = 0; j < children.count(); j++) {
513 if (children.at(j)->type() == AVWIDGET || children.at(j)->type() == TRANSITIONWIDGET) {
514 AbstractClipItem *item = static_cast <AbstractClipItem *>(children.at(j));
515 ItemInfo info = item->info();
516 if (item->type() == AVWIDGET) {
517 QDomElement clip = doc.createElement("clipitem");
518 clip.setAttribute("track", info.track);
519 clip.setAttribute("position", info.startPos.frames(m_doc->fps()));
520 group.appendChild(clip);
521 } else if (item->type() == TRANSITIONWIDGET) {
522 QDomElement clip = doc.createElement("transitionitem");
523 clip.setAttribute("track", info.track);
524 clip.setAttribute("position", info.startPos.frames(m_doc->fps()));
525 group.appendChild(clip);
530 return doc.documentElement();
534 void ClipManager::slotClipModified(const QString &path)
536 //kDebug() << "// CLIP: " << path << " WAS MODIFIED";
537 const QList <DocClipBase *> list = getClipByResource(path);
538 for (int i = 0; i < list.count(); i++) {
539 DocClipBase *clip = list.at(i);
541 QString id = clip->getId();
542 if (!m_modifiedClips.contains(id))
543 emit modifiedClip(id);
544 m_modifiedClips[id] = QTime::currentTime();
547 if (!m_modifiedTimer.isActive()) m_modifiedTimer.start();
550 void ClipManager::slotProcessModifiedClips()
552 if (!m_modifiedClips.isEmpty()) {
553 QMapIterator<QString, QTime> i(m_modifiedClips);
554 while (i.hasNext()) {
556 if (QTime::currentTime().msecsTo(i.value()) <= -1500) {
557 emit reloadClip(i.key());
558 m_modifiedClips.remove(i.key());
563 if (m_modifiedClips.isEmpty()) m_modifiedTimer.stop();
566 void ClipManager::slotClipMissing(const QString &path)
568 // kDebug() << "// CLIP: " << path << " WAS MISSING";
569 const QList <DocClipBase *> list = getClipByResource(path);
570 for (int i = 0; i < list.count(); i++) {
571 DocClipBase *clip = list.at(i);
572 if (clip != NULL) emit missingClip(clip->getId());
576 void ClipManager::slotClipAvailable(const QString &path)
578 // kDebug() << "// CLIP: " << path << " WAS ADDED";
579 const QList <DocClipBase *> list = getClipByResource(path);
580 for (int i = 0; i < list.count(); i++) {
581 DocClipBase *clip = list.at(i);
582 if (clip != NULL) emit availableClip(clip->getId());
586 int ClipManager::clipsCount() const
588 return m_clipList.count();