1/*
2 This file is part of the KDE Baloo Project
3 SPDX-FileCopyrightText: 2013-2015 Vishesh Handa <vhanda@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6*/
7
8#include <KCrash>
9
10#include <iostream>
11
12#include "global.h"
13#include "database.h"
14#include "fileindexerconfig.h"
15#include "priority.h"
16#include "migrator.h"
17#include "mainhub.h"
18
19#include <QDBusConnection>
20#include <QCoreApplication>
21#include <QFile>
22
23#include <KAboutData>
24
25int main(int argc, char** argv)
26{
27 lowerIOPriority();
28 lowerSchedulingPriority();
29 lowerPriority();
30
31 QCoreApplication app(argc, argv);
32
33 Baloo::FileIndexerConfig indexerConfig;
34 if (!indexerConfig.indexingEnabled()) {
35 std::cout << "Baloo File Indexing has been disabled" << std::endl;
36 return 0;
37 }
38
39 if (!QDBusConnection::sessionBus().registerService(QStringLiteral("org.kde.baloo"))) {
40 qWarning() << "Failed to register via dbus. Another instance is running";
41 return 1;
42 }
43
44 KAboutData aboutData(QStringLiteral("baloo_file"), QString(), QLatin1String(PROJECT_VERSION));
45 KAboutData::setApplicationData(aboutData);
46
47 // Crash Handling
48 KCrash::initialize();
49 KCrash::setFlags(KCrash::AutoRestart);
50
51 const QString path = Baloo::fileIndexDbPath();
52
53 Baloo::Migrator migrator(path, &indexerConfig);
54 if (migrator.migrationRequired()) {
55 migrator.migrate();
56 }
57
58 bool firstRun = !QFile::exists(fileName: path + QStringLiteral("/index"));
59
60 Baloo::Database *db = Baloo::globalDatabaseInstance();
61
62 /**
63 * try to open, if that fails, try to unlink the index db and retry
64 */
65 using OpenResult = Baloo::Database::OpenResult;
66 if (auto rc = db->open(mode: Baloo::Database::CreateDatabase); rc != OpenResult::Success) {
67 if (rc == OpenResult::InvalidPath) {
68 return 1;
69 }
70 // delete old stuff, set to initial run!
71 qWarning() << "Failed to create database, removing corrupted database.";
72 QFile::remove(fileName: path + QStringLiteral("/index"));
73 QFile::remove(fileName: path + QStringLiteral("/index-lock"));
74 firstRun = true;
75
76 // try to create now after cleanup, if still no works => fail
77 if (db->open(mode: Baloo::Database::CreateDatabase) != OpenResult::Success) {
78 qWarning() << "Failed to create database after deleting corrupted one.";
79 return 1;
80 }
81 }
82
83 Baloo::MainHub hub(db, &indexerConfig, firstRun);
84 return app.exec();
85}
86

source code of baloo/src/file/main.cpp