]> git.sesse.net Git - kdenlive/blob - src/docclipbase.cpp
Reindent the codebase using 'linux' bracket placement.
[kdenlive] / src / docclipbase.cpp
1 /***************************************************************************
2  *                         DocClipBase.cpp  -  description                 *
3  *                           -------------------                           *
4  *   begin                : Fri Apr 12 2002                                *
5  *   Copyright (C) 2002 by Jason Wood (jasonwood@blueyonder.co.uk)         *
6  *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
7  *                                                                         *
8  *   This program is free software; you can redistribute it and/or modify  *
9  *   it under the terms of the GNU General Public License as published by  *
10  *   the Free Software Foundation; either version 2 of the License, or     *
11  *   (at your option) any later version.                                   *
12  *                                                                         *
13  *   This program is distributed in the hope that it will be useful,       *
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
16  *   GNU General Public License for more details.                          *
17  *                                                                         *
18  *   You should have received a copy of the GNU General Public License     *
19  *   along with this program; if not, write to the                         *
20  *   Free Software Foundation, Inc.,                                       *
21  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
22  ***************************************************************************/
23
24
25
26
27 #include "docclipbase.h"
28 #include "kdenlivesettings.h"
29 #include "kthumb.h"
30 #include "clipmanager.h"
31
32 #include <KDebug>
33
34 #include <QCryptographicHash>
35
36 DocClipBase::DocClipBase(ClipManager *clipManager, QDomElement xml, const QString &id):
37         m_id(id), m_description(QString()), m_refcount(0), m_audioThumbCreated(false), m_duration(GenTime()), m_thumbProd(NULL), m_audioTimer(NULL), m_properties(QMap <QString, QString> ()), audioFrameChache(QMap<int, QMap<int, QByteArray> > ()), m_baseTrackProducers(QList <Mlt::Producer *>()), m_snapMarkers(QList < CommentedTime > ()), m_videoOnlyProducer(NULL), m_audioTrackProducers(QList <Mlt::Producer *>())
38 {
39     int type = xml.attribute("type").toInt();
40     m_clipType = (CLIPTYPE) type;
41
42     QDomNamedNodeMap attributes = xml.attributes();
43     for (unsigned int i = 0; i < attributes.count(); i++) {
44         m_properties.insert(attributes.item(i).nodeName(), attributes.item(i).nodeValue());
45     }
46
47     KUrl url = KUrl(xml.attribute("resource"));
48     if (!m_properties.contains("file_hash") && !url.isEmpty()) getFileHash(url.path());
49
50     if (xml.hasAttribute("duration")) {
51         setDuration(GenTime(xml.attribute("duration").toInt(), KdenliveSettings::project_fps()));
52     } else {
53         int out = xml.attribute("out").toInt();
54         int in = xml.attribute("in").toInt();
55         setDuration(GenTime(out - in, KdenliveSettings::project_fps()));
56     }
57
58     if (!m_properties.contains("name")) m_properties.insert("name", url.fileName());
59
60     //if (!url.isEmpty() && QFile::exists(url.path()))
61     {
62         m_thumbProd = new KThumb(clipManager, url, m_id, m_properties.value("file_hash"));
63         if (m_clipType == AV || m_clipType == AUDIO) slotCreateAudioTimer();
64     }
65     //kDebug() << "type is video" << (m_clipType == AV) << " " << m_clipType;
66 }
67
68 /*DocClipBase & DocClipBase::operator=(const DocClipBase & clip) {
69     DocClipBase::operator=(clip);
70     m_id = clip.getId();
71     m_clipType = clip.clipType();
72     m_name = clip.name();
73     m_duration = clip.duration();
74     m_audioThumbCreated = clip.audioThumbCreated();
75     m_properties = clip.properties();
76     return *this;
77 }*/
78
79 DocClipBase::~DocClipBase()
80 {
81     if (m_thumbProd) {
82         delete m_thumbProd;
83     }
84     if (m_audioTimer) {
85         m_audioTimer->stop();
86         delete m_audioTimer;
87     }
88     qDeleteAll(m_baseTrackProducers);
89     m_baseTrackProducers.clear();
90 }
91
92 void DocClipBase::setZone(QPoint zone)
93 {
94     m_properties.insert("zone_in", QString::number(zone.x()));
95     m_properties.insert("zone_out", QString::number(zone.y()));
96 }
97
98 QPoint DocClipBase::zone() const
99 {
100     QPoint zone;
101     zone.setX(m_properties.value("zone_in").toInt());
102     zone.setY(m_properties.value("zone_out", "50").toInt());
103     return zone;
104 }
105
106 void DocClipBase::slotCreateAudioTimer()
107 {
108     connect(m_thumbProd, SIGNAL(audioThumbReady(QMap <int, QMap <int, QByteArray> >)), this , SLOT(updateAudioThumbnail(QMap <int, QMap <int, QByteArray> >)));
109     connect(this, SIGNAL(getAudioThumbs()), this , SLOT(slotGetAudioThumbs()));
110     m_audioTimer = new QTimer(this);
111     connect(m_audioTimer, SIGNAL(timeout()), this, SLOT(slotGetAudioThumbs()));
112 }
113
114 void DocClipBase::askForAudioThumbs()
115 {
116     if (m_thumbProd) m_thumbProd->askForAudioThumbs(getId());
117 }
118
119 void DocClipBase::slotClearAudioCache()
120 {
121     if (m_thumbProd) m_thumbProd->stopAudioThumbs();
122     if (m_audioTimer != NULL) m_audioTimer->stop();
123     audioFrameChache.clear();
124     m_audioThumbCreated = false;
125 }
126
127 /*void DocClipBase::getClipMainThumb() {
128     if (m_thumbProd) m_thumbProd->getMainThumb(m_properties.value("thumbnail").toInt());
129 }*/
130
131 KThumb *DocClipBase::thumbProducer()
132 {
133     return m_thumbProd;
134 }
135
136 bool DocClipBase::audioThumbCreated() const
137 {
138     return m_audioThumbCreated;
139 }
140
141 const QString DocClipBase::name() const
142 {
143
144     return m_properties.value("name");
145 }
146
147 const QString &DocClipBase::getId() const
148 {
149     return m_id;
150 }
151
152 void DocClipBase::setId(const QString &newId)
153 {
154     m_id = newId;
155 }
156
157 const CLIPTYPE & DocClipBase::clipType() const
158 {
159     return m_clipType;
160 }
161
162 void DocClipBase::setClipType(CLIPTYPE type)
163 {
164     m_clipType = type;
165
166     m_properties.insert("type", QString::number((int) type));
167     if (m_thumbProd && m_audioTimer == NULL && (m_clipType == AV || m_clipType == AUDIO))
168         slotCreateAudioTimer();
169 }
170
171 KUrl DocClipBase::fileURL() const
172 {
173     QString res = m_properties.value("resource");
174     if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
175     return KUrl();
176 }
177
178 void DocClipBase::setClipThumbFrame(const uint &ix)
179 {
180     m_properties.insert("thumbnail", QString::number((int) ix));
181 }
182
183 uint DocClipBase::getClipThumbFrame() const
184 {
185     return (uint) m_properties.value("thumbnail").toInt();
186 }
187
188 const QString DocClipBase::description() const
189 {
190     return m_properties.value("description");
191 }
192
193 bool DocClipBase::isTransparent() const
194 {
195     return (m_properties.value("transparency") == "1");
196 }
197
198 const QString DocClipBase::getProperty(const QString prop) const
199 {
200     return m_properties.value(prop);
201 }
202
203 void DocClipBase::setDuration(GenTime dur)
204 {
205     m_duration = dur;
206     m_properties.insert("duration", QString::number((int) dur.frames(KdenliveSettings::project_fps())));
207 }
208
209 const GenTime &DocClipBase::duration() const
210 {
211     return m_duration;
212 }
213
214 const GenTime DocClipBase::maxDuration() const
215 {
216     if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == TEXT || (m_clipType == SLIDESHOW &&  m_properties.value("loop") == "1")) {
217         const GenTime dur(15000, KdenliveSettings::project_fps());
218         return dur;
219     }
220     return m_duration;
221 }
222
223 bool DocClipBase::hasFileSize() const
224 {
225     return true;
226 }
227
228
229 // virtual
230 QDomElement DocClipBase::toXML() const
231 {
232     QDomDocument doc;
233     QDomElement clip = doc.createElement("producer");
234
235     QMapIterator<QString, QString> i(m_properties);
236     while (i.hasNext()) {
237         i.next();
238         if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
239     }
240     doc.appendChild(clip);
241     //kDebug()<<"/// CLIP XML: "<<doc.toString();
242     return doc.documentElement();
243 }
244
245
246 void DocClipBase::setAudioThumbCreated(bool isDone)
247 {
248     m_audioThumbCreated = isDone;
249 }
250
251
252 void DocClipBase::setThumbnail(const QPixmap & pixmap)
253 {
254     m_thumbnail = pixmap;
255 }
256
257 const QPixmap & DocClipBase::thumbnail() const
258 {
259     return m_thumbnail;
260 }
261
262 void DocClipBase::updateAudioThumbnail(QMap<int, QMap<int, QByteArray> > data)
263 {
264     //kDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
265     audioFrameChache = data;
266     m_audioThumbCreated = true;
267     emit gotAudioData();
268 }
269
270 QList < GenTime > DocClipBase::snapMarkers() const
271 {
272     QList < GenTime > markers;
273
274     for (uint count = 0; count < m_snapMarkers.count(); ++count) {
275         markers.append(m_snapMarkers[count].time());
276     }
277
278     return markers;
279 }
280
281 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const
282 {
283     return m_snapMarkers;
284 }
285
286 void DocClipBase::setSnapMarkers(QList < CommentedTime > markers)
287 {
288     m_snapMarkers = markers;
289 }
290
291 void DocClipBase::addSnapMarker(const GenTime & time, QString comment)
292 {
293     QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
294     for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
295         if ((*it).time() >= time)
296             break;
297     }
298
299     if ((it != m_snapMarkers.end()) && ((*it).time() == time)) {
300         (*it).setComment(comment);
301         //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
302     } else {
303         CommentedTime t(time, comment);
304         m_snapMarkers.insert(it, t);
305     }
306
307 }
308
309 void DocClipBase::editSnapMarker(const GenTime & time, QString comment)
310 {
311     QList < CommentedTime >::Iterator it;
312     for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
313         if ((*it).time() == time)
314             break;
315     }
316     if (it != m_snapMarkers.end()) {
317         (*it).setComment(comment);
318     } else {
319         kError() << "trying to edit Snap Marker that does not already exists";
320     }
321 }
322
323 QString DocClipBase::deleteSnapMarker(const GenTime & time)
324 {
325     QString result = i18n("Marker");
326     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
327
328     while (itt != m_snapMarkers.end()) {
329         if ((*itt).time() == time)
330             break;
331         ++itt;
332     }
333
334     if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
335         result = (*itt).comment();
336         m_snapMarkers.erase(itt);
337     }
338     return result;
339 }
340
341
342 GenTime DocClipBase::hasSnapMarkers(const GenTime & time)
343 {
344     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
345
346     while (itt != m_snapMarkers.end()) {
347         if ((*itt).time() == time)
348             return time;
349         ++itt;
350     }
351
352     return GenTime(0.0);
353 }
354
355 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime)
356 {
357     int it;
358     for (it = 0; it < m_snapMarkers.count(); it++) {
359         if (m_snapMarkers[it].time() >= currTime)
360             break;
361     }
362     if (it == 0) return GenTime();
363     else if (it == m_snapMarkers.count() - 1 && m_snapMarkers[it].time() < currTime)
364         return m_snapMarkers[it].time();
365     else return m_snapMarkers[it-1].time();
366 }
367
368 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime)
369 {
370     int it;
371     for (it = 0; it < m_snapMarkers.count(); it++) {
372         if (m_snapMarkers[it].time() > currTime)
373             break;
374     }
375     if (it < m_snapMarkers.count() && m_snapMarkers[it].time() > currTime) return m_snapMarkers[it].time();
376     return duration();
377 }
378
379 QString DocClipBase::markerComment(GenTime t)
380 {
381     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
382
383     while (itt != m_snapMarkers.end()) {
384         if ((*itt).time() == t)
385             return (*itt).comment();
386         ++itt;
387     }
388     return QString();
389 }
390
391 void DocClipBase::deleteProducers()
392 {
393     qDeleteAll(m_baseTrackProducers);
394     m_baseTrackProducers.clear();
395     if (m_thumbProd) m_thumbProd->clearProducer();
396 }
397
398 void DocClipBase::setProducer(Mlt::Producer *producer)
399 {
400     if (producer == NULL) return;
401     QString id = producer->get("id");
402     if (id.contains('_')) {
403         // this is a subtrack producer, insert it at correct place
404         id = id.section('_', 1);
405         if (id.endsWith("audio")) {
406             int pos = id.section('_', 0, 0).toInt();
407             if (pos >= m_audioTrackProducers.count()) {
408                 while (m_audioTrackProducers.count() - 1 < pos) {
409                     m_audioTrackProducers.append(NULL);
410                 }
411             }
412             if (m_audioTrackProducers.at(pos) == NULL) m_audioTrackProducers[pos] = producer;
413             return;
414         }
415         if (id.endsWith("video")) {
416             m_videoOnlyProducer = producer;
417             return;
418         }
419         int pos = id.toInt();
420         if (pos >= m_baseTrackProducers.count()) {
421             while (m_baseTrackProducers.count() - 1 < pos) {
422                 m_baseTrackProducers.append(NULL);
423             }
424         }
425         if (m_baseTrackProducers.at(pos) == NULL) m_baseTrackProducers[pos] = producer;
426     } else {
427         if (m_baseTrackProducers.isEmpty()) m_baseTrackProducers.append(producer);
428         else if (m_baseTrackProducers.at(0) == NULL) m_baseTrackProducers[0] = producer;
429     }
430     //m_clipProducer = producer;
431     //m_clipProducer->set("transparency", m_properties.value("transparency").toInt());
432     if (m_thumbProd && !m_thumbProd->hasProducer()) m_thumbProd->setProducer(producer);
433 }
434
435 Mlt::Producer *DocClipBase::audioProducer(int track)
436 {
437     if (m_audioTrackProducers.count() <= track) {
438         while (m_audioTrackProducers.count() - 1 < track) {
439             m_audioTrackProducers.append(NULL);
440         }
441     }
442     if (m_audioTrackProducers.at(track) == NULL) {
443         Mlt::Producer *base = producer();
444         m_audioTrackProducers[track] = new Mlt::Producer(*(base->profile()), base->get("resource"));
445         if (m_properties.contains("force_aspect_ratio")) m_audioTrackProducers.at(track)->set("force_aspect_ratio", m_properties.value("force_aspect_ratio").toDouble());
446         if (m_properties.contains("threads")) m_audioTrackProducers.at(track)->set("threads", m_properties.value("threads").toInt());
447         m_audioTrackProducers.at(track)->set("video_index", -1);
448         if (m_properties.contains("audio_index")) m_audioTrackProducers.at(track)->set("audio_index", m_properties.value("audio_index").toInt());
449         char *tmp = (char *) qstrdup(QString(getId() + '_' + QString::number(track) + "_audio").toUtf8().data());
450         m_audioTrackProducers.at(track)->set("id", tmp);
451         delete[] tmp;
452     }
453     return m_audioTrackProducers.at(track);
454 }
455
456 Mlt::Producer *DocClipBase::videoProducer()
457 {
458     if (m_videoOnlyProducer == NULL) {
459         int i;
460         for (i = 0; i < m_baseTrackProducers.count(); i++)
461             if (m_baseTrackProducers.at(i) != NULL) break;
462         if (i >= m_baseTrackProducers.count()) return NULL;
463         m_videoOnlyProducer = new Mlt::Producer(*m_baseTrackProducers.at(i)->profile(), m_baseTrackProducers.at(i)->get("resource"));
464         if (m_properties.contains("force_aspect_ratio")) m_videoOnlyProducer->set("force_aspect_ratio", m_properties.value("force_aspect_ratio").toDouble());
465         if (m_properties.contains("threads")) m_videoOnlyProducer->set("threads", m_properties.value("threads").toInt());
466         m_videoOnlyProducer->set("audio_index", -1);
467         if (m_properties.contains("video_index")) m_videoOnlyProducer->set("video_index", m_properties.value("video_index").toInt());
468         char *tmp = (char *) qstrdup(QString(getId() + "_video").toUtf8().data());
469         m_videoOnlyProducer->set("id", tmp);
470         delete[] tmp;
471     }
472     return m_videoOnlyProducer;
473 }
474
475 Mlt::Producer *DocClipBase::producer(int track)
476 {
477     /*for (int i = 0; i < m_baseTrackProducers.count(); i++) {
478         if (m_baseTrackProducers.at(i)) kDebug() << "// PROD: " << i << ", ID: " << m_baseTrackProducers.at(i)->get("id");
479     }*/
480     if (track == -1 || (m_clipType != AUDIO && m_clipType != AV)) {
481         if (m_baseTrackProducers.count() == 0) return NULL;
482         for (int i = 0; i < m_baseTrackProducers.count(); i++) {
483             if (m_baseTrackProducers.at(i) != NULL)
484                 return m_baseTrackProducers.at(i);
485         }
486         return NULL;
487     }
488     if (track >= m_baseTrackProducers.count()) {
489         while (m_baseTrackProducers.count() - 1 < track) {
490             m_baseTrackProducers.append(NULL);
491         }
492     }
493     if (m_baseTrackProducers.at(track) == NULL) {
494         int i;
495         for (i = 0; i < m_baseTrackProducers.count(); i++)
496             if (m_baseTrackProducers.at(i) != NULL) break;
497         if (i >= m_baseTrackProducers.count()) return NULL;
498         m_baseTrackProducers[track] = new Mlt::Producer(*m_baseTrackProducers.at(i)->profile(), m_baseTrackProducers.at(i)->get("resource"));
499         if (m_properties.contains("force_aspect_ratio")) m_baseTrackProducers[track]->set("force_aspect_ratio", m_properties.value("force_aspect_ratio").toDouble());
500         if (m_properties.contains("threads")) m_baseTrackProducers[track]->set("threads", m_properties.value("threads").toInt());
501         if (m_properties.contains("video_index")) m_baseTrackProducers[track]->set("video_index", m_properties.value("video_index").toInt());
502         if (m_properties.contains("audio_index")) m_baseTrackProducers[track]->set("audio_index", m_properties.value("audio_index").toInt());
503         char *tmp = (char *) qstrdup(QString(getId() + '_' + QString::number(track)).toUtf8().data());
504         m_baseTrackProducers[track]->set("id", tmp);
505         delete[] tmp;
506         if (KdenliveSettings::dropbframes() && m_baseTrackProducers.at(i)->get("skip_loop_filter") && strcmp(m_baseTrackProducers.at(i)->get("skip_loop_filter"), "all") == 0) {
507             m_baseTrackProducers[track]->set("skip_loop_filter", "all");
508             m_baseTrackProducers[track]->set("skip_frame", "bidir");
509         }
510     }
511     return m_baseTrackProducers.at(track);
512 }
513
514 void DocClipBase::setProducerProperty(const char *name, int data)
515 {
516     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
517         if (m_baseTrackProducers.at(i) != NULL)
518             m_baseTrackProducers[i]->set(name, data);
519     }
520 }
521
522 void DocClipBase::setProducerProperty(const char *name, const char *data)
523 {
524     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
525         if (m_baseTrackProducers.at(i) != NULL)
526             m_baseTrackProducers[i]->set(name, data);
527     }
528 }
529
530 const char *DocClipBase::producerProperty(const char *name) const
531 {
532     for (int i = 0; i < m_baseTrackProducers.count(); i++) {
533         if (m_baseTrackProducers.at(i) != NULL) {
534             return m_baseTrackProducers.at(i)->get(name);
535         }
536     }
537     return NULL;
538 }
539
540
541 void DocClipBase::slotRefreshProducer()
542 {
543     if (m_baseTrackProducers.count() == 0) return;
544     kDebug() << "////////////   REFRESH CLIP !!!!!!!!!!!!!!!!";
545     if (m_clipType == SLIDESHOW) {
546         /*char *tmp = (char *) qstrdup(getProperty("resource").toUtf8().data());
547                Mlt::Producer producer(*(m_clipProducer->profile()), tmp);
548                delete[] tmp;
549         delete m_clipProducer;
550         m_clipProducer = new Mlt::Producer(producer.get_producer());
551         if (!getProperty("out").isEmpty()) m_clipProducer->set_in_and_out(getProperty("in").toInt(), getProperty("out").toInt());*/
552         setProducerProperty("ttl", getProperty("ttl").toInt());
553         //m_clipProducer->set("id", getProperty("id"));
554         if (getProperty("fade") == "1") {
555             // we want a fade filter effect
556             kDebug() << "////////////   FADE WANTED";
557             Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
558             int ct = 0;
559             Mlt::Filter *filter = clipService.filter(ct);
560             while (filter) {
561                 if (filter->get("mlt_service") == "luma") {
562                     break;
563                 }
564                 ct++;
565                 filter = clipService.filter(ct);
566             }
567
568             if (filter && filter->get("mlt_service") == "luma") {
569                 filter->set("period", getProperty("ttl").toInt() - 1);
570                 filter->set("luma.out", getProperty("luma_duration").toInt());
571                 QString resource = getProperty("luma_file");
572                 char *tmp = (char *) qstrdup(resource.toUtf8().data());
573                 filter->set("luma.resource", tmp);
574                 delete[] tmp;
575                 if (!getProperty("softness").isEmpty()) {
576                     int soft = getProperty("softness").toInt();
577                     filter->set("luma.softness", (double) soft / 100.0);
578                 }
579             } else {
580                 // filter does not exist, create it...
581                 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "luma");
582                 filter->set("period", getProperty("ttl").toInt() - 1);
583                 filter->set("luma.out", getProperty("luma_duration").toInt());
584                 QString resource = getProperty("luma_file");
585                 char *tmp = (char *) qstrdup(resource.toUtf8().data());
586                 filter->set("luma.resource", tmp);
587                 delete[] tmp;
588                 if (!getProperty("softness").isEmpty()) {
589                     int soft = getProperty("softness").toInt();
590                     filter->set("luma.softness", (double) soft / 100.0);
591                 }
592                 clipService.attach(*filter);
593             }
594         } else {
595             kDebug() << "////////////   FADE NOT WANTED!!!";
596             Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
597             int ct = 0;
598             Mlt::Filter *filter = clipService.filter(0);
599             while (filter) {
600                 if (filter->get("mlt_service") == "luma") {
601                     clipService.detach(*filter);
602                 } else ct++;
603                 filter = clipService.filter(ct);
604             }
605         }
606     }
607 }
608
609 void DocClipBase::setProperties(QMap <QString, QString> properties)
610 {
611     // changing clip type is not allowed
612     properties.remove("type");
613     QMapIterator<QString, QString> i(properties);
614     bool refreshProducer = false;
615     QStringList keys;
616     keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness";
617     while (i.hasNext()) {
618         i.next();
619         setProperty(i.key(), i.value());
620         if (m_clipType == SLIDESHOW && keys.contains(i.key())) refreshProducer = true;
621     }
622     if (refreshProducer) slotRefreshProducer();
623 }
624
625 void DocClipBase::setMetadata(QMap <QString, QString> properties)
626 {
627     m_metadata = properties;
628 }
629
630 QMap <QString, QString> DocClipBase::metadata() const
631 {
632     return m_metadata;
633 }
634
635 void DocClipBase::clearProperty(const QString &key)
636 {
637     m_properties.remove(key);
638 }
639
640 void DocClipBase::getFileHash(const QString url)
641 {
642     if (m_clipType == SLIDESHOW) return;
643     QFile file(url);
644     if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
645         QByteArray fileData;
646         QByteArray fileHash;
647         //kDebug() << "SETTING HASH of" << value;
648         m_properties.insert("file_size", QString::number(file.size()));
649         /*
650                * 1 MB = 1 second per 450 files (or faster)
651                * 10 MB = 9 seconds per 450 files (or faster)
652                */
653         if (file.size() > 1000000*2) {
654             fileData = file.read(1000000);
655             if (file.seek(file.size() - 1000000))
656                 fileData.append(file.readAll());
657         } else
658             fileData = file.readAll();
659         file.close();
660         fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
661         m_properties.insert("file_hash", QString(fileHash.toHex()));
662         //kDebug() << file.fileName() << file.size() << fileHash.toHex();
663     }
664 }
665
666 QString DocClipBase::getClipHash() const
667 {
668     QString hash;
669     if (m_clipType == SLIDESHOW) hash = QCryptographicHash::hash(m_properties.value("resource").toAscii().data(), QCryptographicHash::Md5).toHex();
670     else if (m_clipType == COLOR) hash = QCryptographicHash::hash(m_properties.value("colour").toAscii().data(), QCryptographicHash::Md5).toHex();
671     else hash = m_properties.value("file_hash");
672     return hash;
673 }
674
675 void DocClipBase::refreshThumbUrl()
676 {
677     if (m_thumbProd) m_thumbProd->updateThumbUrl(m_properties.value("file_hash"));
678 }
679
680 void DocClipBase::setProperty(const QString &key, const QString &value)
681 {
682     m_properties.insert(key, value);
683     if (key == "resource") {
684         getFileHash(value);
685         if (m_thumbProd) m_thumbProd->updateClipUrl(KUrl(value), m_properties.value("file_hash"));
686     } else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
687     //else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
688     else if (key == "colour") {
689         char *tmp = (char *) qstrdup(value.toUtf8().data());
690         setProducerProperty("colour", tmp);
691         delete[] tmp;
692     } else if (key == "xmldata") {
693         setProducerProperty("force_reload", 1);
694     } else if (key == "force_aspect_ratio") {
695         if (value.isEmpty()) {
696             m_properties.remove("force_aspect_ratio");
697             setProducerProperty("force_aspect_ratio", 0);
698         } else setProducerProperty("force_aspect_ratio", value.toDouble());
699     } else if (key == "threads") {
700         if (value.isEmpty()) {
701             m_properties.remove("threads");
702             setProducerProperty("threads", 1);
703         } else setProducerProperty("threads", value.toInt());
704     } else if (key == "video_index") {
705         if (value.isEmpty()) {
706             m_properties.remove("video_index");
707             setProducerProperty("video_index", m_properties.value("default_video").toInt());
708         } else setProducerProperty("video_index", value.toInt());
709     } else if (key == "audio_index") {
710         if (value.isEmpty()) {
711             m_properties.remove("audio_index");
712             setProducerProperty("audio_index", m_properties.value("default_audio").toInt());
713         } else setProducerProperty("audio_index", value.toInt());
714     }
715 }
716
717 QMap <QString, QString> DocClipBase::properties() const
718 {
719     return m_properties;
720 }
721
722 bool DocClipBase::slotGetAudioThumbs()
723 {
724     if (m_thumbProd == NULL) return false;
725     if (!KdenliveSettings::audiothumbnails()) {
726         if (m_audioTimer != NULL) m_audioTimer->stop();
727         return false;
728     }
729     if (m_audioThumbCreated) {
730         if (m_audioTimer != NULL) m_audioTimer->stop();
731         return false;
732     }
733     if (m_audioTimer != NULL)
734         m_audioTimer->start(1500);
735     double lengthInFrames = duration().frames(KdenliveSettings::project_fps());
736     m_thumbProd->getAudioThumbs(2, 0, lengthInFrames /*must be number of frames*/, 20);
737     return true;
738 }
739
740
741