ukui-search/ukui-search-service/ukui-search-service.cpp

151 lines
5.2 KiB
C++

/*
* Copyright (C) 2022, KylinSoft Co., Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* Authors: iaom <zhangpengfei@kylinos.cn>
*
*/
#include <QDebug>
#include <QDBusConnection>
#include <QQmlContext>
#include <utility>
#include <stdio.h>
#include <QVariant>
#include "ukui-search-service.h"
#include "dir-watcher.h"
#include "file-utils.h"
#include "file-indexer-config.h"
#include "rep_monitor_replica.h"
using namespace UkuiSearch;
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, [=](const QString& msg) {
this->parseCmd(msg, true);
});
qRegisterMetaType<IndexType>("IndexType");
m_indexScheduler = new IndexScheduler(this);
DirWatcher::getDirWatcher()->initDbusService();
m_monitor = new Monitor(m_indexScheduler, this);
m_qroHost.setHostUrl(QUrl(QStringLiteral("local:ukui-search-service-monitor")));
qDebug() << "Init remote status object:" << m_qroHost.enableRemoting<MonitorSourceAPI>(m_monitor);
}
//parse cmd
qDebug()<<"parse cmd";
auto message = arguments().join(' ').toUtf8();
parseCmd(message, !isRunning());
qDebug()<<"ukui search service constructor end";
}
UkuiSearchService::~UkuiSearchService()
{
if(m_quickView) {
delete m_quickView;
m_quickView = nullptr;
}
}
void UkuiSearchService::parseCmd(const QString& msg, bool isPrimary)
{
QCommandLineParser parser;
parser.addHelpOption();
parser.addVersionOption();
QCommandLineOption quitOption(QStringList()<<"q"<<"quit", tr("Stop service"));
parser.addOption(quitOption);
QCommandLineOption monitorWindow(QStringList()<<"m"<<"monitor", tr("Show index monitor window"));
parser.addOption(monitorWindow);
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(monitorWindow)) {
loadMonitorWindow();
m_quickView->show();
return;
}
if (parser.isSet(quitOption)) {
qApp->quit();
return;
}
} else {
if (arguments().count() < 2) {
parser.showHelp();
}
parser.process(arguments());
if(parser.isSet(statusOption)) {
showMonitorState();
return;
} else {
sendMessage(msg);
::exit(0);
}
}
}
void UkuiSearchService::loadMonitorWindow()
{
if(!m_monitor) {
m_monitor = new Monitor(m_indexScheduler, this);
}
if(!m_quickView) {
m_quickView = new QQuickView();
m_quickView->setResizeMode(QQuickView::SizeRootObjectToView);
m_quickView->rootContext()->setContextProperty("monitor", m_monitor);
m_quickView->setSource(m_qmlPath);
}
}
void UkuiSearchService::showMonitorState()
{
m_qroNode.connectToNode(QUrl(QStringLiteral("local:ukui-search-service-monitor")));
auto *m = m_qroNode.acquire<MonitorReplica>();
connect(m, &QRemoteObjectReplica::initialized, [&, m](){
qDebug() << "QRemoteObjectReplica initialized";
QString state = m->indexState();
QString message = "Current index path: " + m->currentIndexPaths().join(",") + "\n"
+ "Current index scheduler state: " + m->indexState() + "\n";
if(state == "Idle" || state == "Stop") {
message += "Basic index size: " + QString::number(m->basicIndexDocNum()) + " \n"
+ "Content index size: " + QString::number(m->contentIndexDocNum()) + " \n"
+ "Ocr index size: " + QString::number(m->ocrContentIndexDocNum()) + " \n";
} else {
message += "Basic index progress: " + QString::number(m->basicIndexProgress()) + " of " + QString::number(m->basicIndexSize())+ " finished\n"
+ "Content index progress: " + QString::number(m->contentIndexProgress()) + " of " + QString::number(m->contentIndexSize())+ " finished\n"
+ "Ocr content index progress: " + QString::number(m->ocrIndexProgress()) + " of " + QString::number(m->ocrIndexSize())+ " finished\n";
}
delete m;
fputs(qPrintable(message), stdout);
qApp->quit();
});
}