File: SQLImageInfoCollection.cpp

package info (click to toggle)
kphotoalbum 4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 21,324 kB
  • sloc: cpp: 35,900; python: 743; xml: 483; sh: 146; perl: 34; makefile: 16
file content (212 lines) | stat: -rw-r--r-- 6,576 bytes parent folder | download
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
/*
  Copyright (C) 2006-2010 Tuomas Suutari <thsuut@utu.fi>

  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 of the License, 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.

  You should have received a copy of the GNU General Public License
  along with this program (see the file COPYING); if not, write to the
  Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
  MA 02110-1301 USA.
*/

#include "SQLImageInfoCollection.h"
#include "DB/ImageSearchInfo.h"
#include "SQLImageInfo.h"
#include "QueryErrors.h"
#include <QList>

using namespace SQLDB;

SQLImageInfoCollection::SQLImageInfoCollection(QueryHelper& queryHelper):
    _qh(queryHelper),
    _lockingScope(0)
{
     QList< QPair<DB::RawId, QString> > l = _qh.mediaItemIdFileMap();
     for (QList< QPair<DB::RawId, QString> >::const_iterator i = l.constBegin();
         i != l.constEnd(); ++i) {
        _filenameIdMap.insert((*i).second, (*i).first);
        _idFilenameMap.insert((*i).first, (*i).second);
    }
}

SQLImageInfoCollection::~SQLImageInfoCollection()
{
    unsetLock();

    // FIXME: shouldn't be needed?
    clearCache();
}

DB::ImageInfoPtr
SQLImageInfoCollection::getImageInfoOf(const QString& relativeFilename) const
{
    DB::RawId rawId = _filenameIdMap[relativeFilename];
    if (rawId == DB::RawId()) {
        try {
            rawId = _qh.mediaItemId(relativeFilename);
            Q_ASSERT(rawId != DB::RawId());
        }
        catch (NotFoundError& e) {
            return DB::ImageInfoPtr(0);
        }
        _filenameIdMap.insert(relativeFilename, rawId);
    }

    // QMutexLocker locker(&_mutex);
    DB::ImageInfoPtr p = _infoPointers[rawId];
    if (!p) {
        // TODO: Use real context for prefetching

        // make a dummy prefetch list
        QList<DB::RawId> prefetchIdList;
        prefetchIdList << rawId;

        typedef QMap<DB::RawId, DB::ImageInfoPtr> IdInfoMap;
        const IdInfoMap fileInfos = _qh.getInfosOfFiles(prefetchIdList);

        p = fileInfos[rawId];
        for (IdInfoMap::const_iterator i = fileInfos.begin(); i != fileInfos.end(); ++i) {
            _infoPointers.insert(i.key(), i.value());
            setLocking(i.value());
        }
    }
    return p;
}

DB::ImageInfoPtr SQLImageInfoCollection::getImageInfoOf(const DB::Id& id) const
{
    Q_ASSERT(!id.isNull());

    const int prefetchWindowSize = 1000;

    // QMutexLocker locker(&_mutex);
    DB::ImageInfoPtr p = _infoPointers[id.rawId()];
    if (!p) {
        QList<DB::RawId> prefetchIdList;
        const DB::IdList context = id.context();
        if (!context.isEmpty()) {
            const int contextSize = context.size();
            const int rawIdIndex = context.indexOf(id);
            const int firstIndex =
                qMax(0, rawIdIndex - prefetchWindowSize / 2);
            const int onePastLastIndex =
                qMin(contextSize, rawIdIndex + prefetchWindowSize / 2);
            Q_ASSERT(firstIndex < onePastLastIndex);
            for (int i = firstIndex; i < onePastLastIndex; ++i) {
                prefetchIdList.push_back(context.at(i).rawId());
            }
        }
        else {
            prefetchIdList.push_back(id.rawId());
        }

        typedef QMap<DB::RawId, DB::ImageInfoPtr> IdInfoMap;
        const IdInfoMap fileInfos = _qh.getInfosOfFiles(prefetchIdList);

        p = fileInfos[id.rawId()];
        for (IdInfoMap::const_iterator i = fileInfos.begin(); i != fileInfos.end(); ++i) {
            _infoPointers.insert(i.key(), i.value());
            setLocking(i.value());
        }
    }
    return p;
}

QString SQLImageInfoCollection::filenameForId(DB::RawId id) const
{
    QString filename = _idFilenameMap[id];
    if (filename.isNull()) {
        try {
             filename = _qh.mediaItemFilename(id);
        }
        catch (NotFoundError& e) {
            return QString();
        }
        _idFilenameMap.insert(id, filename);
    }
    return filename;
}

void SQLImageInfoCollection::setLock(const DB::ImageSearchInfo& scope, bool invert)
{
    unsetLock();
    _lockingScope = new DB::ImageSearchInfo(scope);
    _invertLock = invert;
    updateLockingInfo();

    // TODO: some other way to prevent locked images to show up in searches
}

void SQLImageInfoCollection::unsetLock()
{
    if (_lockingScope) {
        delete _lockingScope;
        _lockingScope = 0;
        updateLockingInfo();
    }
}

void SQLImageInfoCollection::clearCache()
{
    // QMutexLocker locker(&_mutex);
    for (QMap<DB::RawId, DB::ImageInfoPtr>::iterator i = _infoPointers.begin();
         i != _infoPointers.end();) {

        // Check if only _infoPointers has reference to the pointer.
        if ((*i).count() == 1) {
            // Then it's not needed anymore, because new one could be
            // created easily by loading from the SQL database.
            i = _infoPointers.erase(i);
        }
        else
            ++i;
    }
}

void SQLImageInfoCollection::deleteTag(DB::Category* category,
                                       const QString& item)
{
    if (category) {
        // QMutexLocker locker(&_mutex);
        for (QMap<DB::RawId, DB::ImageInfoPtr>::iterator i = _infoPointers.begin();
             i != _infoPointers.end(); ++i)
            (*i)->removeCategoryInfo(category->name(), item);
    }
}

void SQLImageInfoCollection::renameTag(DB::Category* category,
                                       const QString& oldName,
                                       const QString& newName)
{
    if (category) {
        // QMutexLocker locker(&_mutex);
        for (QMap<DB::RawId, DB::ImageInfoPtr>::iterator i = _infoPointers.begin();
             i != _infoPointers.end(); ++i)
            (*i)->renameItem(category->name(), oldName, newName);
    }
}

void SQLImageInfoCollection::setLocking(DB::ImageInfoPtr p) const
{
    if (_lockingScope)
        p->setLocked(_lockingScope->match(p) ^ _invertLock);
    else
        p->setLocked(false);
}

void SQLImageInfoCollection::updateLockingInfo() const
{
    for (QMap<DB::RawId, DB::ImageInfoPtr>::iterator i = _infoPointers.begin();
         i != _infoPointers.end(); ++i)
        setLocking(*i);
}

#include "SQLImageInfoCollection.moc"