2021-11-22 16:03:31 +08:00
|
|
|
#include <QDebug>
|
|
|
|
#include "ukui-search-service.h"
|
2022-03-04 15:15:07 +08:00
|
|
|
#include "dir-watcher.h"
|
2022-04-14 11:16:26 +08:00
|
|
|
#include "common.h"
|
2022-04-12 14:57:22 +08:00
|
|
|
#include <QDBusConnection>
|
|
|
|
|
2021-12-14 14:43:35 +08:00
|
|
|
using namespace UkuiSearch;
|
2021-11-22 16:03:31 +08:00
|
|
|
UkuiSearchService::UkuiSearchService(int &argc, char *argv[], const QString &applicationName): QtSingleApplication (applicationName, argc, argv)
|
|
|
|
{
|
|
|
|
qDebug()<<"ukui search service constructor start";
|
|
|
|
setApplicationVersion(QString("v%1").arg(VERSION));
|
|
|
|
setQuitOnLastWindowClosed(false);
|
|
|
|
|
|
|
|
if (!this->isRunning()) {
|
|
|
|
connect(this, &QtSingleApplication::messageReceived, [=](QString msg) {
|
|
|
|
this->parseCmd(msg, true);
|
|
|
|
});
|
2022-04-25 10:23:20 +08:00
|
|
|
DirWatcher::getDirWatcher();
|
2021-11-22 16:03:31 +08:00
|
|
|
initGsettings();
|
2022-04-14 11:16:26 +08:00
|
|
|
FileIndexManager::getInstance()->initIndexPathSetFunction();
|
2021-11-22 16:03:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//parse cmd
|
|
|
|
qDebug()<<"parse cmd";
|
|
|
|
auto message = this->arguments().join(' ').toUtf8();
|
|
|
|
parseCmd(message, !isRunning());
|
|
|
|
|
|
|
|
qDebug()<<"ukui search service constructor end";
|
|
|
|
}
|
|
|
|
|
|
|
|
void UkuiSearchService::parseCmd(QString msg, bool isPrimary)
|
|
|
|
{
|
|
|
|
QCommandLineParser parser;
|
|
|
|
|
|
|
|
parser.addHelpOption();
|
|
|
|
parser.addVersionOption();
|
|
|
|
|
|
|
|
QCommandLineOption quitOption(QStringList()<<"q"<<"quit", tr("Stop service"));
|
|
|
|
parser.addOption(quitOption);
|
|
|
|
|
|
|
|
QCommandLineOption startOption(QStringList()<<"i"<<"index", tr("start or stop file index"), "option");
|
|
|
|
parser.addOption(startOption);
|
|
|
|
|
|
|
|
// QCommandLineOption statusOption(QStringList()<<"s"<<"status", tr("show status of file index service"));
|
|
|
|
// parser.addOption(statusOption);
|
|
|
|
|
|
|
|
if (isPrimary) {
|
|
|
|
const QStringList args = QString(msg).split(' ');
|
|
|
|
parser.process(args);
|
|
|
|
if(parser.isSet(startOption)) {
|
|
|
|
qDebug() << "options!!!!" << parser.value(startOption);
|
|
|
|
if(parser.value(startOption) == "start") {
|
|
|
|
indexServiceSwitch(true);
|
|
|
|
} else if (parser.value(startOption) == "stop") {
|
|
|
|
indexServiceSwitch(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if(parser.isSet(statusOption)) {
|
|
|
|
// }
|
|
|
|
if (parser.isSet(quitOption)) {
|
|
|
|
qApp->quit();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (arguments().count() < 2) {
|
|
|
|
parser.showHelp();
|
|
|
|
}
|
|
|
|
parser.process(arguments());
|
|
|
|
sendMessage(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UkuiSearchService::initGsettings()
|
|
|
|
{
|
|
|
|
const QByteArray id(UKUI_SEARCH_SCHEMAS);
|
|
|
|
if(QGSettings::isSchemaInstalled(id)) {
|
|
|
|
m_SearchGsettings = new QGSettings(id);
|
|
|
|
connect(m_SearchGsettings, &QGSettings::changed, this, [ = ](const QString &key) {
|
|
|
|
if(key == SEARCH_METHOD_KEY) {
|
|
|
|
setSearchMethodByGsettings();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if(m_SearchGsettings->keys().contains(SEARCH_METHOD_KEY)) {
|
|
|
|
setSearchMethodByGsettings();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
qWarning() << UKUI_SEARCH_SCHEMAS << " is not found!";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UkuiSearchService::setSearchMethodByGsettings()
|
|
|
|
{
|
|
|
|
bool isIndexSearch = m_SearchGsettings->get(SEARCH_METHOD_KEY).toBool();
|
|
|
|
if(isIndexSearch) {
|
|
|
|
FileUtils::searchMethod = FileUtils::SearchMethod::INDEXSEARCH;
|
|
|
|
} else {
|
|
|
|
FileUtils::searchMethod = FileUtils::SearchMethod::DIRECTSEARCH;
|
|
|
|
}
|
2022-04-11 10:22:38 +08:00
|
|
|
FileIndexManager::getInstance()->searchMethod(FileUtils::searchMethod);
|
|
|
|
|
2021-11-22 16:03:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UkuiSearchService::indexServiceSwitch(bool startIndex)
|
|
|
|
{
|
|
|
|
if(startIndex) {
|
|
|
|
FileUtils::searchMethod = FileUtils::SearchMethod::INDEXSEARCH;
|
|
|
|
} else {
|
|
|
|
FileUtils::searchMethod = FileUtils::SearchMethod::DIRECTSEARCH;
|
|
|
|
}
|
2022-04-11 10:22:38 +08:00
|
|
|
FileIndexManager::getInstance()->searchMethod(FileUtils::searchMethod);
|
2021-11-22 16:03:31 +08:00
|
|
|
|
|
|
|
const QByteArray id(UKUI_SEARCH_SCHEMAS);
|
|
|
|
if(QGSettings::isSchemaInstalled(id)) {
|
|
|
|
m_SearchGsettings = new QGSettings(id);
|
|
|
|
if(m_SearchGsettings->keys().contains(SEARCH_METHOD_KEY)) {
|
|
|
|
m_SearchGsettings->set(SEARCH_METHOD_KEY, startIndex);
|
|
|
|
} else {
|
|
|
|
qWarning() << SEARCH_METHOD_KEY << " is not found!";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
qWarning() << UKUI_SEARCH_SCHEMAS << " is not found!";
|
|
|
|
}
|
|
|
|
}
|