1 /*
2     This file is part of the KDE Baloo Project
3     SPDX-FileCopyrightText: 2015 Pinak Ahuja <pinak.ahuja@gmail.com>
4 
5     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6 */
7 
8 /**
9  * WARNING: make sure baloo_file is not running before running this test
10  * otherwise we'll have two baloo_file_extractor processes running and that will
11  * cause problems when registering the monitor.
12  */
13 
14 #include "database.h"
15 #include "firstrunindexer.h"
16 #include "filecontentindexerprovider.h"
17 #include "filecontentindexer.h"
18 #include "fileindexerconfig.h"
19 #include "timeestimator.h"
20 #include "extractor_interface.h"
21 
22 #include <QCoreApplication>
23 #include <QThreadPool>
24 #include <QTemporaryDir>
25 #include <QObject>
26 #include <QDBusConnection>
27 #include <QTextStream>
28 #include <QString>
29 #include <QDBusServiceWatcher>
30 
31 #include <iostream>
32 
33 namespace org {
34     namespace kde {
35         namespace baloo {
36             typedef OrgKdeBalooExtractorInterface extractorInterface;
37         }
38     }
39 }
40 
41 class Scheduler : public QObject
42 {
43     Q_OBJECT
44 
45 public:
46     explicit Scheduler(QObject* parent = 0);
47     void startIndexing();
48 
49 private Q_SLOTS:
50     void startContentIndexer();
51     void printTime();
52     void finished();
53     void registerMonitor(const QString& service);
54 
55 private:
56     QTemporaryDir m_dir;
57 
58     Baloo::Database m_db;
59     Baloo::FileContentIndexerProvider m_provider;
60     Baloo::FileIndexerConfig* m_config;
61     Baloo::FileContentIndexer* m_contentRunnable;
62     org::kde::baloo::extractorInterface* m_extractorInterface;
63 
64     QThreadPool m_pool;
65 
66     int m_count;
67     int m_batchSize;
68 
69     QTextStream m_out;
70 };
71 
Scheduler(QObject * parent)72 Scheduler::Scheduler(QObject* parent)
73     : QObject(parent)
74     , m_db(m_dir.path())
75     , m_provider(&m_db)
76     , m_contentRunnable(0)
77     , m_count(0)
78     , m_batchSize(40)
79     , m_out(stdout)
80 {
81     m_db.open(Baloo::Database::CreateDatabase);
82     m_pool.setMaxThreadCount(1);
83 
84     QString extractorService = QStringLiteral("org.kde.baloo.extractor");
85     m_extractorInterface = new org::kde::baloo::extractorInterface(extractorService,
86                                                             QStringLiteral("/extractor"),
87                                                             QDBusConnection::sessionBus(),
88                                                             this);
89 
90     connect(m_extractorInterface, &org::kde::baloo::extractorInterface::currentUrlChanged,
91             this, &Scheduler::printTime);
92 
93     QDBusServiceWatcher* extractorWatcher = new QDBusServiceWatcher(extractorService,
94                                                             QDBusConnection::sessionBus(),
95                                                             QDBusServiceWatcher::WatchForRegistration,
96                                                             this);
97 
98     connect(extractorWatcher, &QDBusServiceWatcher::serviceRegistered, this, &Scheduler::registerMonitor);
99 
100     // Set test path
101     qputenv("BALOO_DB_PATH", m_dir.path().toUtf8());
102 
103     QStandardPaths::setTestModeEnabled(true);
104     QString testConfigPath = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) +
105                                                                             QStringLiteral("baloofilerc");
106     // Remove config from previous runs
107     QFile::remove(testConfigPath);
108 
109     m_config = new Baloo::FileIndexerConfig(this);
110     m_config->setInitialRun(true);
111 }
112 
startIndexing()113 void Scheduler::startIndexing()
114 {
115     auto firstRunnable = new Baloo::FirstRunIndexer(&m_db, m_config, m_config->includeFolders());
116     connect(firstRunnable, &Baloo::FirstRunIndexer::done, this, &Scheduler::startContentIndexer);
117     m_pool.start(firstRunnable);
118 }
119 
startContentIndexer()120 void Scheduler::startContentIndexer()
121 {
122     m_contentRunnable = new Baloo::FileContentIndexer(&m_provider);
123     connect(m_contentRunnable, &Baloo::FileContentIndexer::done, this, &Scheduler::finished);
124     m_pool.start(m_contentRunnable);
125 }
126 
printTime()127 void Scheduler::printTime()
128 {
129     Q_ASSERT(m_contentRunnable != 0);
130 
131     if (++m_count == 10 * m_batchSize) {
132         Baloo::TimeEstimator estimator;
133         estimator.setBatchTimings(m_contentRunnable->batchTimings());
134         estimator.setFilesLeft(m_provider.size());
135         // print Remaining time after every 10 batches
136         m_out <<  "Remaining Time: " << estimator.calculateTimeLeft() << endl;
137         m_count = 0;
138     }
139 }
140 
finished()141 void Scheduler::finished()
142 {
143     m_out << "done!" << endl;
144     QCoreApplication::exit();
145 }
146 
registerMonitor(const QString & service)147 void Scheduler::registerMonitor(const QString& service)
148 {
149     Q_UNUSED(service);
150     m_extractorInterface->registerMonitor();
151 }
152 
main(int argc,char ** argv)153 int main (int argc, char** argv)
154 {
155     QCoreApplication app(argc, argv);
156 
157     Scheduler sched;
158     sched.startIndexing();
159     app.exec();
160 }
161 
162 #include "remainingtimetest.moc"
163