~ubuntu-branches/ubuntu/saucy/digikam/saucy

« back to all changes in this revision

Viewing changes to libs/database/tagscache.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2010-12-21 23:19:11 UTC
  • mfrom: (1.2.33 upstream) (3.1.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20101221231911-z9jip7s5aht1jqn9
Tags: 2:1.7.0-1ubuntu1
* Merge from Debian Experimental. Remaining Ubuntu changes:
  - Export .pot name and copy to plugins in debian/rules
  - Version build-depends on kipi-plugins-dev to ensure build is against the
    same version on all archs
* Drop debian/patches/kubuntu_01_linker.diff, incoporated upstream
* Remove patches directory and unused patches

Show diffs side-by-side

added added

removed removed

Lines of Context:
55
55
{
56
56
public:
57
57
 
58
 
    TagsCachePriv()
 
58
    TagsCachePriv() :
 
59
        initialized(false),
 
60
        needUpdateInfos(true),
 
61
        needUpdateHash(true),
 
62
        changingDB(false)
59
63
    {
60
 
        initialized     = false;
61
 
        needUpdateInfos = true;
62
 
        needUpdateHash  = true;
63
 
        changingDB      = false;
64
64
    }
65
65
 
66
66
    bool                     initialized;
85
85
    void checkNameHash()
86
86
    {
87
87
        checkInfos();
 
88
 
88
89
        if (needUpdateHash && initialized)
89
90
        {
90
91
            QWriteLocker locker(&lock);
128
129
 
129
130
// -----------------------------------------------------------------------------------------------
130
131
 
131
 
class TagsCacheCreator { public: TagsCache object; };
 
132
class TagsCacheCreator
 
133
{
 
134
public:
 
135
    TagsCache object;
 
136
};
132
137
K_GLOBAL_STATIC(TagsCacheCreator, creator)
133
138
 
134
139
// -----------------------------------------------------------------------------------------------
139
144
}
140
145
 
141
146
TagsCache::TagsCache()
142
 
         : d(new TagsCachePriv)
 
147
    : d(new TagsCachePriv)
143
148
{
144
149
}
145
150
 
151
156
void TagsCache::initialize()
152
157
{
153
158
    if (d->initialized)
 
159
    {
154
160
        return;
 
161
    }
155
162
 
156
 
    connect(DatabaseAccess::databaseWatch(), SIGNAL(tagChange(const TagChangeset &)),
157
 
            this, SLOT(slotTagChanged(const TagChangeset &)),
 
163
    connect(DatabaseAccess::databaseWatch(), SIGNAL(tagChange(const TagChangeset&)),
 
164
            this, SLOT(slotTagChanged(const TagChangeset&)),
158
165
            Qt::DirectConnection);
159
166
 
160
167
    d->initialized = true;
166
173
 
167
174
    QReadLocker locker(&d->lock);
168
175
    QList<TagShortInfo>::const_iterator it = d->find(id);
 
176
 
169
177
    if (it != d->infos.constEnd())
 
178
    {
170
179
        return it->name;
 
180
    }
 
181
 
171
182
    return QString();
172
183
}
173
184
 
174
185
QStringList TagsCache::tagNames(const QList<int>& ids)
175
186
{
176
187
    QStringList names;
 
188
 
177
189
    if (!ids.isEmpty())
178
190
    {
179
191
        foreach (int id, ids)
181
193
            names << tagName(id);
182
194
        }
183
195
    }
 
196
 
184
197
    return names;
185
198
}
186
199
 
191
204
    QString path;
192
205
    QReadLocker locker(&d->lock);
193
206
    QList<TagShortInfo>::const_iterator it;
 
207
 
194
208
    for (it = d->find(id); it != d->infos.constEnd(); it = d->find(it->pid))
195
209
    {
196
210
        if (path.isNull())
 
211
        {
197
212
            path = it->name;
 
213
        }
198
214
        else
 
215
        {
199
216
            path = it->name + "/" + path;
 
217
        }
200
218
    }
201
219
 
202
220
    if (slashPolicy == IncludeLeadingSlash)
 
221
    {
203
222
        path.prepend("/");
 
223
    }
204
224
 
205
225
    return path;
206
226
}
208
228
QStringList TagsCache::tagPaths(const QList<int>& ids, LeadingSlashPolicy slashPolicy)
209
229
{
210
230
    QStringList paths;
 
231
 
211
232
    if (!ids.isEmpty())
212
233
    {
213
234
        foreach (int id, ids)
215
236
            paths << tagPath(id, slashPolicy);
216
237
        }
217
238
    }
 
239
 
218
240
    return paths;
219
241
}
220
242
 
233
255
    foreach (int id, d->nameHash.values(tagName))
234
256
    {
235
257
        tag = d->find(id);
 
258
 
236
259
        if (tag == d->infos.constEnd())
237
 
            continue; // error
 
260
        {
 
261
            continue;    // error
 
262
        }
238
263
 
239
264
        if (tag->pid == parentId)
 
265
        {
240
266
            return tag->id;
 
267
        }
241
268
    }
242
269
    return 0;
243
270
}
254
281
    d->checkInfos();
255
282
    QReadLocker locker(&d->lock);
256
283
    QList<TagShortInfo>::const_iterator tag = d->find(id);
 
284
 
257
285
    if (tag != d->infos.constEnd())
 
286
    {
258
287
        return tag->pid;
 
288
    }
 
289
 
259
290
    return 0;
260
291
}
261
292
 
263
294
{
264
295
    // split full tag "url" into list of single tag names
265
296
    QStringList tagHierarchy = tagPath.split('/', QString::SkipEmptyParts);
 
297
 
266
298
    if (tagHierarchy.isEmpty())
 
299
    {
267
300
        return 0;
 
301
    }
268
302
 
269
303
    d->checkNameHash();
270
304
 
280
314
    foreach (int id, d->nameHash.values(tagName))
281
315
    {
282
316
        tag = d->find(id);
 
317
 
283
318
        if (tag == d->infos.constEnd())
284
 
            continue; // error
 
319
        {
 
320
            continue;    // error
 
321
        }
285
322
 
286
323
        int parentID = tag->pid;
287
324
 
343
380
    QStringList tagHierarchy = tagPathToCreate.split('/', QString::SkipEmptyParts);
344
381
 
345
382
    if (tagHierarchy.isEmpty())
 
383
    {
346
384
        return 0;
 
385
    }
347
386
 
348
387
    d->checkNameHash();
349
388
 
372
411
                foreach (int id, d->nameHash.values(tagName))
373
412
                {
374
413
                    tag = d->find(id);
 
414
 
375
415
                    if (tag != d->infos.constEnd() && tag->pid == parentTagID)
376
416
                    {
377
417
                        tagID = tag->id;
390
430
            else
391
431
            {
392
432
                tagsToCreate << tagName;
 
433
 
393
434
                if (parentTagExisted)
 
435
                {
394
436
                    parentTagIDForCreation = parentTagID;
 
437
                }
395
438
 
396
439
                parentTagID      = 0;
397
440
                parentTagExisted = false;
404
447
        foreach (const QString& tagName, tagsToCreate)
405
448
        {
406
449
            tagID = access.db()->addTag(parentTagIDForCreation, tagName, QString(), 0);
 
450
 
407
451
            if (tagID == -1)
408
452
            {
409
453
                break; // something wrong with DB
414
458
                d->needUpdateInfos = true;
415
459
                d->needUpdateHash  = true;
416
460
            }
 
461
 
417
462
            parentTagIDForCreation = tagID;
418
463
        }
419
464
    }
424
469
QList<int> TagsCache::createTags(const QStringList& tagPaths)
425
470
{
426
471
    QList<int> ids;
 
472
 
427
473
    if (!tagPaths.isEmpty())
428
474
    {
429
475
        foreach (const QString& tagPath, tagPaths)
431
477
            ids << createTag(tagPath);
432
478
        }
433
479
    }
 
480
 
434
481
    return ids;
435
482
}
436
483
 
437
484
QList<int> TagsCache::getOrCreateTags(const QStringList& tagPaths)
438
485
{
439
486
    QList<int> ids;
 
487
 
440
488
    if (!tagPaths.isEmpty())
441
489
    {
442
 
    foreach (const QString& tagPath, tagPaths)
443
 
    {
444
 
        ids << getOrCreateTag(tagPath);
445
 
    }
446
 
    }
 
490
        foreach (const QString& tagPath, tagPaths)
 
491
        {
 
492
            ids << getOrCreateTag(tagPath);
 
493
        }
 
494
    }
 
495
 
447
496
    return ids;
448
497
}
449
498
 
450
499
int TagsCache::getOrCreateTag(const QString& tagPath)
451
500
{
452
501
    int id = tagForPath(tagPath);
 
502
 
453
503
    if (!id)
 
504
    {
454
505
        id = createTag(tagPath);
 
506
    }
 
507
 
455
508
    return id;
456
509
}
457
510
 
462
515
        d->needUpdateInfos = true;
463
516
        d->needUpdateHash  = true;
464
517
    }
 
518
 
465
519
    if (changeset.operation() == TagChangeset::Added)
 
520
    {
466
521
        emit tagAdded(changeset.tagId());
 
522
    }
467
523
    else if (changeset.operation() == TagChangeset::Deleted)
 
524
    {
468
525
        emit tagDeleted(changeset.tagId());
 
526
    }
469
527
}
470
528
 
471
529
} // namespace Digikam