1 /* ============================================================
2  *
3  * This file is a part of digiKam project
4  * https://www.digikam.org
5  *
6  * Date        : 2007-03-20
7  * Description : Listing information from database.
8  *
9  * Copyright (C) 2005      by Renchi Raju    <renchi dot raju at gmail dot com>
10  * Copyright (C) 2007-2012 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
11  * Copyright (C) 2007-2021 by Gilles Caulier <caulier dot gilles at gmail dot com>
12  * Copyright (C) 2015      by Mohamed_Anwer  <m_dot_anwer at gmx dot com>
13  * Copyright (C) 2018      by Mario Frank    <mario dot frank at uni minus potsdam dot de>
14  *
15  * This program is free software; you can redistribute it
16  * and/or modify it under the terms of the GNU General
17  * Public License as published by the Free Software Foundation;
18  * either version 2, or (at your option)
19  * any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * ============================================================ */
27 
28 #include "itemlister_p.h"
29 
30 namespace Digikam
31 {
32 
ItemLister()33 ItemLister::ItemLister()
34     : d(new Private)
35 {
36 }
37 
~ItemLister()38 ItemLister::~ItemLister()
39 {
40     delete d;
41 }
42 
setRecursive(bool recursive)43 void ItemLister::setRecursive(bool recursive)
44 {
45     d->recursive = recursive;
46 }
47 
setListOnlyAvailable(bool listOnlyAvailable)48 void ItemLister::setListOnlyAvailable(bool listOnlyAvailable)
49 {
50     d->listOnlyAvailableImages = listOnlyAvailable;
51 }
52 
list(ItemListerReceiver * const receiver,const CoreDbUrl & url)53 void ItemLister::list(ItemListerReceiver* const receiver,
54                       const CoreDbUrl& url)
55 {
56     if      (url.isAlbumUrl())
57     {
58         int albumRootId = url.albumRootId();
59         QString album   = url.album();
60 
61         listPAlbum(receiver, albumRootId, album);
62     }
63     else if (url.isTagUrl())
64     {
65         listTag(receiver, url.tagIds());
66     }
67     else if (url.isDateUrl())
68     {
69         listDateRange(receiver, url.startDate(), url.endDate());
70     }
71     else if (url.isMapImagesUrl())
72     {
73         double lat1, lat2, lon1, lon2;
74         url.areaCoordinates(&lat1, &lat2, &lon1, &lon2);
75         listAreaRange(receiver, lat1, lat2, lon1, lon2);
76     }
77 }
78 
listDateRange(ItemListerReceiver * const receiver,const QDate & startDate,const QDate & endDate)79 void ItemLister::listDateRange(ItemListerReceiver* const receiver,
80                                const QDate& startDate,
81                                const QDate& endDate)
82 {
83     QList<QVariant> values;
84 
85     {
86         CoreDbAccess access;
87         access.backend()->execSql(QString::fromUtf8("SELECT DISTINCT Images.id, Images.name, Images.album, "
88                                           "       Albums.albumRoot, "
89                                           "       ImageInformation.rating, Images.category, "
90                                           "       ImageInformation.format, ImageInformation.creationDate, "
91                                           "       Images.modificationDate, Images.fileSize, "
92                                           "       ImageInformation.width, ImageInformation.height "
93                                           " FROM Images "
94                                           "       LEFT JOIN ImageInformation ON Images.id=ImageInformation.imageid "
95                                           "       INNER JOIN Albums ON Albums.id=Images.album "
96                                           " WHERE Images.status=1 "
97                                           "   AND ImageInformation.creationDate < ? "
98                                           "   AND ImageInformation.creationDate >= ? "
99                                           " ORDER BY Images.album;"),
100                                   QDateTime(endDate),
101                                   QDateTime(startDate),
102                                   &values);
103     }
104 
105     QSet<int> albumRoots = albumRootsToList();
106     int       width, height;
107 
108     for (QList<QVariant>::const_iterator it = values.constBegin() ; it != values.constEnd() ;)
109     {
110         ItemListerRecord record;
111 
112         record.imageID           = (*it).toLongLong();
113         ++it;
114         record.name              = (*it).toString();
115         ++it;
116         record.albumID           = (*it).toInt();
117         ++it;
118         record.albumRootID       = (*it).toInt();
119         ++it;
120         record.rating            = (*it).toInt();
121         ++it;
122         record.category          = (DatabaseItem::Category)(*it).toInt();
123         ++it;
124         record.format            = (*it).toString();
125         ++it;
126         record.creationDate      = (*it).toDateTime();
127         ++it;
128         record.modificationDate  = (*it).toDateTime();
129         ++it;
130         record.fileSize          = (*it).toLongLong();
131         ++it;
132         width                    = (*it).toInt();
133         ++it;
134         height                   = (*it).toInt();
135         ++it;
136 
137         if (d->listOnlyAvailableImages && !albumRoots.contains(record.albumRootID))
138         {
139             continue;
140         }
141 
142         record.imageSize         = QSize(width, height);
143 
144         receiver->receive(record);
145     }
146 }
147 
148 } // namespace Digikam
149