summaryrefslogtreecommitdiffstats
path: root/digikam/tdeioslave/digikamdates.cpp
blob: a82e978ddb65baffe7c6be86ac7f3303059a3438 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* ============================================================
 *
 * This file is a part of digiKam project
 * http://www.digikam.org
 *
 * Date        : 2005-04-21
 * Description : a tdeio-slave to process date query on 
 *               digiKam albums. 
 *
 * Copyright (C) 2005 by Renchi Raju <renchi@pooh.tam.uiuc.edu>
 * Copyright (C) 2006-2008 by Gilles Caulier <caulier dot gilles at gmail dot com>
 *
 * This program is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General
 * Public License as published by the Free Software Foundation;
 * either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * ============================================================ */

// C ansi includes.

extern "C"
{
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
}

// C++ includes.

#include <cstdlib>
#include <cstdio>
#include <ctime>

// TQt includes.

#include <tqfile.h>
#include <tqdatastream.h>
#include <tqregexp.h>
#include <tqbuffer.h>

// KDE includes.

#include <tdeio/global.h>
#include <tdeglobal.h>
#include <tdelocale.h>
#include <kinstance.h>
#include <tdefilemetainfo.h>
#include <kdebug.h>

// Local includes.

#include "digikam_export.h"
#include "digikamdates.h"

tdeio_digikamdates::tdeio_digikamdates(const TQCString &pool_socket,
                                   const TQCString &app_socket)
                : SlaveBase("tdeio_digikamdates", pool_socket, app_socket)
{
}

tdeio_digikamdates::~tdeio_digikamdates()
{
}

static TQValueList<TQRegExp> makeFilterList( const TQString &filter )
{
    TQValueList<TQRegExp> regExps;
    if ( filter.isEmpty() )
        return regExps;

    TQChar sep( ';' );
    int i = filter.find( sep, 0 );
    if ( i == -1 && filter.find( ' ', 0 ) != -1 )
        sep = TQChar( ' ' );

    TQStringList list = TQStringList::split( sep, filter );
    TQStringList::Iterator it = list.begin();
    while ( it != list.end() ) {
        regExps << TQRegExp( (*it).stripWhiteSpace(), false, true );
        ++it;
    }
    return regExps;
}

static bool matchFilterList( const TQValueList<TQRegExp>& filters,
                             const TQString &fileName )
{
    TQValueList<TQRegExp>::ConstIterator rit = filters.begin();
    while ( rit != filters.end() ) {
        if ( (*rit).exactMatch(fileName) )
            return true;
        ++rit;
    }
    return false;
}

void tdeio_digikamdates::special(const TQByteArray& data)
{
    bool folders = (metaData("folders") == "yes");

    TQString libraryPath;
    KURL    kurl;
    TQString url;
    TQString filter;
    int     getDimensions;
    int     recurseAlbums;
    int     recurseTags;

    TQDataStream ds(data, IO_ReadOnly);
    ds >> libraryPath;
    ds >> kurl;
    ds >> filter;
    ds >> getDimensions;
    ds >> recurseAlbums;
    ds >> recurseTags;

    url = kurl.path();

    TQValueList<TQRegExp> regex = makeFilterList(filter);

    if (m_libraryPath != libraryPath)
    {
        m_libraryPath = libraryPath;
        m_db.closeDB();
        m_db.openDB(libraryPath);
    }

    TQByteArray  ba;

    if (folders)       // Special mode to stats all dates from collection
    {
        TQMap<TQDateTime, int> datesStatMap;
        TQStringList          values;
        TQString              name, dateStr;
        TQDateTime            dateTime;

        m_db.execSql( "SELECT name, datetime FROM Images;", &values );

        for ( TQStringList::iterator it = values.begin(); it != values.end(); )
        {
            name    = *it;
            ++it;
            dateStr = *it;
            ++it;

            if ( !matchFilterList( regex, name ) )
                continue;

            dateTime = TQDateTime::fromString( dateStr, Qt::ISODate );
            if ( !dateTime.isValid() )
                continue;

            TQMap<TQDateTime, int>::iterator it2 = datesStatMap.find(dateTime);
            if ( it2 == datesStatMap.end() )
            {
                datesStatMap.insert( dateTime, 1 );
            }
            else
            {
                datesStatMap.replace( dateTime, it2.data() + 1 );
            }
        }

        TQDataStream os(ba, IO_WriteOnly);
        os << datesStatMap;
    }
    else
    {
        TQStringList subpaths = TQStringList::split("/", url, false);
        if (subpaths.count() == 4)
        {
            int yrStart = TQString(subpaths[0]).toInt();
            int moStart = TQString(subpaths[1]).toInt();
            int yrEnd   = TQString(subpaths[2]).toInt();
            int moEnd   = TQString(subpaths[3]).toInt();

            TQString moStartStr, moEndStr;
            moStartStr.sprintf("%.2d", moStart);
            moEndStr.sprintf("%.2d", moEnd);

            TQStringList values;

            m_db.execSql(TQString("SELECT Images.id, Images.name, Images.dirid, \n "
                                 "  Images.datetime, Albums.url \n "
                                 "FROM Images, Albums \n "
                                 "WHERE Images.datetime < '%1-%2-01' \n "
                                 "AND Images.datetime >= '%3-%4-01' \n "
                                 "AND Albums.id=Images.dirid \n "
                                 "ORDER BY Albums.id;")
                         .arg(yrEnd, 4)
                         .arg(moEndStr, 2)
                         .arg(yrStart, 4)
                         .arg(moStartStr, 2),
                         &values, 0, false);

            TQ_LLONG     imageid;
            TQString     name;
            TQString     path;
            int         dirid;
            TQString     date;
            TQString     purl;
            TQSize       dims;
            struct stat stbuf;

            int  count = 0;
            TQDataStream* os = new TQDataStream(ba, IO_WriteOnly);

            for (TQStringList::iterator it = values.begin(); it != values.end();)
            {
                imageid = (*it).toLongLong();
                ++it;
                name  = *it;
                ++it;
                dirid = (*it).toInt();
                ++it;
                date  = *it;
                ++it;
                purl  = *it;
                ++it;

                if (!matchFilterList(regex, name))
                    continue;

                path = m_libraryPath + purl + '/' + name;
                if (::stat(TQFile::encodeName(path), &stbuf) != 0)
                    continue;

                dims = TQSize();
                if (getDimensions)
                {
                    KFileMetaInfo metaInfo(path);
                    if (metaInfo.isValid())
                    {
                        if (metaInfo.containsGroup("Jpeg EXIF Data"))
                        {
                            dims = metaInfo.group("Jpeg EXIF Data").
                                   item("Dimensions").value().toSize();
                        }
                        else if (metaInfo.containsGroup("General"))
                        {
                            dims = metaInfo.group("General").
                                   item("Dimensions").value().toSize();
                        }
                        else if (metaInfo.containsGroup("Technical"))
                        {
                            dims = metaInfo.group("Technical").
                                   item("Dimensions").value().toSize();
                        }
                    }
                }

                *os << imageid;
                *os << dirid;
                *os << name;
                *os << date;
                *os << static_cast<size_t>(stbuf.st_size);
                *os << dims;

                count++;

                if (count > 200)
                {
                    delete os;
                    os = 0;

                    SlaveBase::data(ba);
                    ba.resize(0);

                    count = 0;
                    os = new TQDataStream(ba, IO_WriteOnly);
                }
            }

            delete os;
        }
    }

    SlaveBase::data(ba);

    finished();
}

/* TDEIO slave registration */

extern "C"  
{
    DIGIKAM_EXPORT int kdemain(int argc, char **argv)
    {
        TDELocale::setMainCatalogue("digikam");
        TDEInstance instance( "tdeio_digikamdates" );
        TDEGlobal::locale();

        if (argc != 4) {
            kdDebug() << "Usage: tdeio_digikamdates  protocol domain-socket1 domain-socket2"
                      << endl;
            exit(-1);
        }

        tdeio_digikamdates slave(argv[2], argv[3]);
        slave.dispatchLoop();

        return 0;
    }
}