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