1 | /* |
---|---|
2 | This file is part of the KDE Baloo Project |
3 | SPDX-FileCopyrightText: 2014-2015 Vishesh Handa <vhanda@kde.org> |
4 | |
5 | SPDX-License-Identifier: LGPL-2.1-or-later |
6 | */ |
7 | |
8 | #include "taglistjob.h" |
9 | #include "global.h" |
10 | #include "database.h" |
11 | #include "transaction.h" |
12 | |
13 | #include <QStringList> |
14 | |
15 | using namespace Baloo; |
16 | |
17 | class BALOO_CORE_NO_EXPORT TagListJob::Private { |
18 | public: |
19 | QStringList tags; |
20 | }; |
21 | |
22 | TagListJob::TagListJob(QObject* parent) |
23 | : KJob(parent) |
24 | , d(new Private) |
25 | { |
26 | } |
27 | |
28 | TagListJob::~TagListJob() = default; |
29 | |
30 | void TagListJob::start() |
31 | { |
32 | Database *db = globalDatabaseInstance(); |
33 | |
34 | if (!db->open(mode: Database::ReadOnlyDatabase)) { |
35 | // if we have no index, we have no tags |
36 | if (!db->isAvailable()) { |
37 | emitResult(); |
38 | return; |
39 | } |
40 | |
41 | setError(UserDefinedError); |
42 | setErrorText(QStringLiteral("Failed to open the database")); |
43 | emitResult(); |
44 | return; |
45 | } |
46 | |
47 | QVector<QByteArray> tagList; |
48 | { |
49 | Transaction tr(db, Transaction::ReadOnly); |
50 | tagList = tr.fetchTermsStartingWith(term: "TAG-"); |
51 | } |
52 | d->tags.reserve(asize: tagList.size()); |
53 | for (const QByteArray& ba : std::as_const(t&: tagList)) { |
54 | d->tags << QString::fromUtf8(ba: ba.mid(index: 4)); |
55 | } |
56 | |
57 | emitResult(); |
58 | } |
59 | |
60 | QStringList TagListJob::tags() |
61 | { |
62 | return d->tags; |
63 | } |
64 | |
65 | #include "moc_taglistjob.cpp" |
66 |