1 /* ============================================================
2  *
3  * This file is a part of KDE project
4  *
5  *
6  * Date        : 2012-02-06
7  * Description : help wrapper around libkipi ImageInfo to manage easily
8  *               item properties with KIPI host application.
9  *
10  * Copyright (C) 2012-2018 by Gilles Caulier <caulier dot gilles at gmail dot com>
11  *
12  * This program is free software; you can redistribute it
13  * and/or modify it under the terms of the GNU General
14  * Public License as published by the Free Software Foundation;
15  * either version 2, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * ============================================================ */
23 
24 #include "kpimageinfo.h"
25 
26 // Qt includes
27 
28 #include <QMap>
29 #include <QVariant>
30 
31 // Libkipi includes
32 
33 #include <libkipi_version.h>
34 #include <KIPI/Interface>
35 #include <KIPI/ImageInfo>
36 #include <KIPI/PluginLoader>
37 
38 // Local includes
39 
40 #include "kipiplugins_debug.h"
41 
42 using namespace KIPI;
43 
44 namespace KIPIPlugins
45 {
46 
47 class KPImageInfo::Private
48 {
49 public:
50 
Private()51     Private()
52         : iface(nullptr)
53     {
54         PluginLoader* const pl = PluginLoader::instance();
55 
56         if (pl)
57         {
58             iface = pl->interface();
59         }
60     }
61 
hasValidData() const62     bool hasValidData() const
63     {
64         return (iface && !url.isEmpty());
65     }
66 
attribute(const QString & name) const67     QVariant attribute(const QString& name) const
68     {
69         QMap<QString, QVariant> map;
70 
71         if (hasValidData())
72         {
73             ImageInfo info = iface->info(url);
74             map            = info.attributes();
75 
76             if (!map.isEmpty())
77                 return map.value(name, QVariant());
78         }
79 
80         return QVariant();
81     }
82 
setAttribute(const QString & name,const QVariant & value)83     void setAttribute(const QString& name, const QVariant& value)
84     {
85         if (hasValidData())
86         {
87             ImageInfo info = iface->info(url);
88             QMap<QString, QVariant> map;
89             map.insert(name, value);
90             info.addAttributes(map);
91         }
92     }
93 
removeAttribute(const QString & name)94     void removeAttribute(const QString& name)
95     {
96         ImageInfo info = iface->info(url);
97         info.delAttributes(QStringList() << name);
98     }
99 
hasAttribute(const QString & name) const100     bool hasAttribute(const QString& name) const
101     {
102         return (attribute(name).isValid());
103     }
104 
105 public:
106 
107     QUrl       url;
108     Interface* iface;
109 };
110 
KPImageInfo(const QUrl & url)111 KPImageInfo::KPImageInfo(const QUrl& url)
112     : d(new Private)
113 {
114     d->url = url;
115 }
116 
~KPImageInfo()117 KPImageInfo::~KPImageInfo()
118 {
119     delete d;
120 }
121 
url() const122 QUrl KPImageInfo::url() const
123 {
124     return d->url;
125 }
126 
cloneData(const QUrl & destination)127 void KPImageInfo::cloneData(const QUrl& destination)
128 {
129     if (d->hasValidData())
130     {
131         ImageInfo srcInfo  = d->iface->info(d->url);
132         ImageInfo destInfo = d->iface->info(destination);
133         destInfo.cloneData(srcInfo);
134     }
135 }
136 
fileSize() const137 qlonglong KPImageInfo::fileSize() const
138 {
139     if (hasFileSize())
140         return d->attribute(QLatin1String("filesize")).toLongLong();
141 
142     return (-1);
143 }
144 
hasFileSize() const145 bool KPImageInfo::hasFileSize() const
146 {
147     return d->hasAttribute(QLatin1String("filesize"));
148 }
149 
setDescription(const QString & desc)150 void KPImageInfo::setDescription(const QString& desc)
151 {
152     if (d->iface)
153     {
154         d->setAttribute(QLatin1String("comment"), desc);
155     }
156     else
157     {
158         qCDebug(KIPIPLUGINS_LOG) << "KIPI interface is null";
159     }
160 }
161 
description() const162 QString KPImageInfo::description() const
163 {
164     if (d->iface)
165     {
166         if (hasDescription()) return d->attribute(QLatin1String("comment")).toString();
167     }
168     else
169     {
170         qCDebug(KIPIPLUGINS_LOG) << "KIPI interface is null";
171     }
172 
173     return QString();
174 }
175 
hasDescription() const176 bool KPImageInfo::hasDescription() const
177 {
178     if (d->iface)
179     {
180         return d->hasAttribute(QLatin1String("comment"));
181     }
182     else
183     {
184         qCDebug(KIPIPLUGINS_LOG) << "KIPI interface is null";
185     }
186 
187     return (!description().isNull());
188 }
189 
setDate(const QDateTime & date)190 void KPImageInfo::setDate(const QDateTime& date)
191 {
192     d->setAttribute(QLatin1String("date"), date);
193 }
194 
date() const195 QDateTime KPImageInfo::date() const
196 {
197     if (hasDate())
198         return d->attribute(QLatin1String("date")).toDateTime();
199 
200     return QDateTime();
201 }
202 
hasDate() const203 bool KPImageInfo::hasDate() const
204 {
205     return d->hasAttribute(QLatin1String("date"));
206 }
207 
isExactDate() const208 bool KPImageInfo::isExactDate() const
209 {
210     if (d->hasAttribute(QLatin1String("isexactdate")))
211         return d->attribute(QLatin1String("isexactdate")).toBool();
212 
213     return true;
214 }
215 
setName(const QString & name)216 void KPImageInfo::setName(const QString& name)
217 {
218     d->setAttribute(QLatin1String("name"), name);
219 }
220 
name() const221 QString KPImageInfo::name() const
222 {
223     if (hasName())
224         return d->attribute(QLatin1String("name")).toString();
225 
226     return QString();
227 }
228 
hasName() const229 bool KPImageInfo::hasName() const
230 {
231     return d->hasAttribute(QLatin1String("name"));
232 }
233 
setOrientation(int orientation)234 void KPImageInfo::setOrientation(int orientation)
235 {
236     d->setAttribute(QLatin1String("orientation"), orientation);
237 }
238 
orientation() const239 int KPImageInfo::orientation() const
240 {
241     int orientation = 0;
242 
243     if (d->hasAttribute(QLatin1String("orientation")))
244         orientation = d->attribute(QLatin1String("orientation")).toInt();
245 
246     return orientation;
247 }
248 
hasOrientation() const249 bool KPImageInfo::hasOrientation() const
250 {
251     return d->hasAttribute(QLatin1String("orientation"));
252 }
253 
setTitle(const QString & title)254 void KPImageInfo::setTitle(const QString& title)
255 {
256     d->setAttribute(QLatin1String("title"), title);
257 }
258 
title() const259 QString KPImageInfo::title() const
260 {
261     return d->attribute(QLatin1String("title")).toString();
262 }
263 
hasTitle() const264 bool KPImageInfo::hasTitle() const
265 {
266     return d->hasAttribute(QLatin1String("title"));
267 }
268 
setLatitude(double lat)269 void KPImageInfo::setLatitude(double lat)
270 {
271     if (lat < -90.0 || lat > 90)
272     {
273         qCDebug(KIPIPLUGINS_LOG) << "Latitude value is out of range (" << lat << ")";
274         return;
275     }
276 
277     d->setAttribute(QLatin1String("latitude"), lat);
278 }
279 
hasLatitude() const280 bool KPImageInfo::hasLatitude() const
281 {
282     return d->hasAttribute(QLatin1String("latitude"));
283 }
284 
latitude() const285 double KPImageInfo::latitude() const
286 {
287     return d->attribute(QLatin1String("latitude")).toDouble();
288 }
289 
setLongitude(double lng)290 void KPImageInfo::setLongitude(double lng)
291 {
292     if (lng < -180.0  || lng > 180)
293     {
294         qCDebug(KIPIPLUGINS_LOG) << "Latitude value is out of range (" << lng << ")";
295         return;
296     }
297 
298     d->setAttribute(QLatin1String("longitude"), lng);
299 }
300 
longitude() const301 double KPImageInfo::longitude() const
302 {
303     return d->attribute(QLatin1String("longitude")).toDouble();
304 }
305 
hasLongitude() const306 bool KPImageInfo::hasLongitude() const
307 {
308     return d->hasAttribute(QLatin1String("longitude"));
309 }
310 
setAltitude(double alt)311 void KPImageInfo::setAltitude(double alt)
312 {
313     d->setAttribute(QLatin1String("altitude"), alt);
314 }
315 
altitude() const316 double KPImageInfo::altitude() const
317 {
318     return d->attribute(QLatin1String("altitude")).toDouble();
319 }
320 
hasAltitude() const321 bool KPImageInfo::hasAltitude() const
322 {
323     return d->hasAttribute(QLatin1String("altitude"));
324 }
325 
hasGeolocationInfo() const326 bool KPImageInfo::hasGeolocationInfo() const
327 {
328     return (d->hasAttribute(QLatin1String("latitude"))  &&
329             d->hasAttribute(QLatin1String("longitude")) &&
330             d->hasAttribute(QLatin1String("altitude")));
331 }
332 
removeGeolocationInfo()333 void KPImageInfo::removeGeolocationInfo()
334 {
335     d->removeAttribute(QLatin1String("gpslocation"));
336 }
337 
setRating(int r)338 void KPImageInfo::setRating(int r)
339 {
340     if (r < 0 || r > 5)
341     {
342         qCDebug(KIPIPLUGINS_LOG) << "Rating value is out of range (" << r << ")";
343         return;
344     }
345 
346     d->setAttribute(QLatin1String("rating"), r);
347 }
348 
rating() const349 int KPImageInfo::rating() const
350 {
351     return d->attribute(QLatin1String("rating")).toInt();
352 }
353 
hasRating() const354 bool KPImageInfo::hasRating() const
355 {
356     return d->hasAttribute(QLatin1String("rating"));
357 }
358 
setColorLabel(int cl)359 void KPImageInfo::setColorLabel(int cl)
360 {
361     if (cl < 0 || cl > 10)
362     {
363         qCDebug(KIPIPLUGINS_LOG) << "Color label value is out of range (" << cl << ")";
364         return;
365     }
366 
367     d->setAttribute(QLatin1String("colorlabel"), cl);
368 }
369 
colorLabel() const370 int KPImageInfo::colorLabel() const
371 {
372     return d->attribute(QLatin1String("colorlabel")).toInt();
373 }
374 
hasColorLabel() const375 bool KPImageInfo::hasColorLabel() const
376 {
377     return d->hasAttribute(QLatin1String("colorlabel"));
378 }
379 
setPickLabel(int pl)380 void KPImageInfo::setPickLabel(int pl)
381 {
382     if (pl < 0 || pl > 10)
383     {
384         qCDebug(KIPIPLUGINS_LOG) << "Pick label value is out of range (" << pl << ")";
385         return;
386     }
387 
388     d->setAttribute(QLatin1String("picklabel"), pl);
389 }
390 
pickLabel() const391 int KPImageInfo::pickLabel() const
392 {
393     return d->attribute(QLatin1String("picklabel")).toInt();
394 }
395 
hasPickLabel() const396 bool KPImageInfo::hasPickLabel() const
397 {
398     return d->hasAttribute(QLatin1String("picklabel"));
399 }
400 
setTagsPath(const QStringList & tp)401 void KPImageInfo::setTagsPath(const QStringList& tp)
402 {
403     d->setAttribute(QLatin1String("tagspath"), tp);
404 }
405 
tagsPath() const406 QStringList KPImageInfo::tagsPath() const
407 {
408     return d->attribute(QLatin1String("tagspath")).toStringList();
409 }
410 
hasTagsPath() const411 bool KPImageInfo::hasTagsPath() const
412 {
413     return d->hasAttribute(QLatin1String("tagspath"));
414 }
415 
keywords() const416 QStringList KPImageInfo::keywords() const
417 {
418     QStringList keywords;
419 
420     if (d->iface)
421     {
422         keywords = d->attribute(QLatin1String("keywords")).toStringList();
423     }
424     else
425     {
426         qCDebug(KIPIPLUGINS_LOG) << "KIPI interface is null";
427     }
428 
429     return keywords;
430 }
431 
hasKeywords() const432 bool KPImageInfo::hasKeywords() const
433 {
434     if (d->iface)
435     {
436         return d->hasAttribute(QLatin1String("keywords"));
437     }
438     else
439     {
440         qCDebug(KIPIPLUGINS_LOG) << "KIPI interface is null";
441     }
442 
443     return false;
444 }
445 
setCreators(const QStringList & list)446 void KPImageInfo::setCreators(const QStringList& list)
447 {
448     d->setAttribute(QLatin1String("creators"), list);
449 }
450 
creators() const451 QStringList KPImageInfo::creators() const
452 {
453     return d->attribute(QLatin1String("creators")).toStringList();
454 }
455 
hasCreators() const456 bool KPImageInfo::hasCreators() const
457 {
458     return d->hasAttribute(QLatin1String("creators"));
459 }
460 
setCredit(const QString & val)461 void KPImageInfo::setCredit(const QString& val)
462 {
463     d->setAttribute(QLatin1String("credit"), val);
464 }
465 
credit() const466 QString KPImageInfo::credit() const
467 {
468     return d->attribute(QLatin1String("credit")).toString();
469 }
470 
hasCredit() const471 bool KPImageInfo::hasCredit() const
472 {
473     return d->hasAttribute(QLatin1String("credit"));
474 }
475 
setRights(const QString & val)476 void KPImageInfo::setRights(const QString& val)
477 {
478     d->setAttribute(QLatin1String("rights"), val);
479 }
480 
rights() const481 QString KPImageInfo::rights() const
482 {
483     return d->attribute(QLatin1String("rights")).toString();
484 }
485 
hasRights() const486 bool KPImageInfo::hasRights() const
487 {
488     return d->hasAttribute(QLatin1String("rights"));
489 }
490 
setSource(const QString & val)491 void KPImageInfo::setSource(const QString& val)
492 {
493     d->setAttribute(QLatin1String("source"), val);
494 }
495 
source() const496 QString KPImageInfo::source() const
497 {
498     return d->attribute(QLatin1String("source")).toString();
499 }
500 
hasSource() const501 bool KPImageInfo::hasSource() const
502 {
503     return d->hasAttribute(QLatin1String("source"));
504 }
505 
506 }  // namespace KIPIPlugins
507