File indexing completed on 2024-04-28 07:40:12

0001 /*
0002     This file is part of the KDE Baloo Project
0003     SPDX-FileCopyrightText: 2014-2015 Vishesh Handa <vhanda@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-or-later
0006 */
0007 
0008 #include "taglistjob.h"
0009 #include "global.h"
0010 #include "database.h"
0011 #include "transaction.h"
0012 
0013 #include <QStringList>
0014 
0015 using namespace Baloo;
0016 
0017 class BALOO_CORE_NO_EXPORT TagListJob::Private {
0018 public:
0019     QStringList tags;
0020 };
0021 
0022 TagListJob::TagListJob(QObject* parent)
0023     : KJob(parent)
0024     , d(new Private)
0025 {
0026 }
0027 
0028 TagListJob::~TagListJob() = default;
0029 
0030 void TagListJob::start()
0031 {
0032     Database *db = globalDatabaseInstance();
0033 
0034     if (!db->open(Database::ReadOnlyDatabase)) {
0035         // if we have no index, we have no tags
0036         if (!db->isAvailable()) {
0037             emitResult();
0038             return;
0039         }
0040 
0041         setError(UserDefinedError);
0042         setErrorText(QStringLiteral("Failed to open the database"));
0043         emitResult();
0044         return;
0045     }
0046 
0047     QVector<QByteArray> tagList;
0048     {
0049         Transaction tr(db, Transaction::ReadOnly);
0050         tagList = tr.fetchTermsStartingWith("TAG-");
0051     }
0052     d->tags.reserve(tagList.size());
0053     for (const QByteArray& ba : std::as_const(tagList)) {
0054         d->tags << QString::fromUtf8(ba.mid(4));
0055     }
0056 
0057     emitResult();
0058 }
0059 
0060 QStringList TagListJob::tags()
0061 {
0062     return d->tags;
0063 }
0064 
0065 #include "moc_taglistjob.cpp"