2022-12-09 16:30:58 +08:00
/*
* 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 >
*
*/
2021-11-22 16:03:31 +08:00
# include <QDebug>
2022-10-26 18:01:40 +08:00
# include <QQmlContext>
2023-11-03 17:28:22 +08:00
# include <cstdio>
2023-10-09 10:27:29 +08:00
# include <QVariant>
2021-11-22 16:03:31 +08:00
# include "ukui-search-service.h"
2022-10-26 18:01:40 +08:00
# include "file-utils.h"
2022-04-12 14:57:22 +08:00
2021-12-14 14:43:35 +08:00
using namespace UkuiSearch ;
2022-10-26 18:01:40 +08:00
UkuiSearchService : : UkuiSearchService ( int & argc , char * argv [ ] , const QString & applicationName )
2024-01-23 11:18:55 +08:00
: QtSingleApplication ( applicationName , argc , argv )
2021-11-22 16:03:31 +08:00
{
qDebug ( ) < < " ukui search service constructor start " ;
setApplicationVersion ( QString ( " v%1 " ) . arg ( VERSION ) ) ;
setQuitOnLastWindowClosed ( false ) ;
if ( ! this - > isRunning ( ) ) {
2023-10-07 15:44:59 +08:00
connect ( this , & QtSingleApplication : : messageReceived , [ = ] ( const QString & msg ) {
2021-11-22 16:03:31 +08:00
this - > parseCmd ( msg , true ) ;
} ) ;
2022-10-26 18:01:40 +08:00
qRegisterMetaType < IndexType > ( " IndexType " ) ;
m_indexScheduler = new IndexScheduler ( this ) ;
DirWatcher : : getDirWatcher ( ) - > initDbusService ( ) ;
2023-10-07 15:44:59 +08:00
m_monitor = new Monitor ( m_indexScheduler , this ) ;
2023-11-03 17:28:22 +08:00
m_qroHost . setHostUrl ( nodeUrl ( ) ) ;
2023-10-07 15:44:59 +08:00
qDebug ( ) < < " Init remote status object: " < < m_qroHost . enableRemoting < MonitorSourceAPI > ( m_monitor ) ;
2021-11-22 16:03:31 +08:00
}
//parse cmd
qDebug ( ) < < " parse cmd " ;
2023-10-07 15:44:59 +08:00
auto message = arguments ( ) . join ( ' ' ) . toUtf8 ( ) ;
2021-11-22 16:03:31 +08:00
parseCmd ( message , ! isRunning ( ) ) ;
qDebug ( ) < < " ukui search service constructor end " ;
}
2022-10-26 18:01:40 +08:00
UkuiSearchService : : ~ UkuiSearchService ( )
{
if ( m_quickView ) {
delete m_quickView ;
m_quickView = nullptr ;
}
}
2023-10-07 15:44:59 +08:00
void UkuiSearchService : : parseCmd ( const QString & msg , bool isPrimary )
2021-11-22 16:03:31 +08:00
{
QCommandLineParser parser ;
parser . addHelpOption ( ) ;
parser . addVersionOption ( ) ;
QCommandLineOption quitOption ( QStringList ( ) < < " q " < < " quit " , tr ( " Stop service " ) ) ;
parser . addOption ( quitOption ) ;
2022-10-26 18:01:40 +08:00
QCommandLineOption monitorWindow ( QStringList ( ) < < " m " < < " monitor " , tr ( " Show index monitor window " ) ) ;
parser . addOption ( monitorWindow ) ;
2023-10-07 15:44:59 +08:00
QCommandLineOption statusOption ( QStringList ( ) < < " s " < < " status " , tr ( " show status of file index service " ) ) ;
parser . addOption ( statusOption ) ;
2021-11-22 16:03:31 +08:00
2024-01-23 11:18:55 +08:00
QCommandLineOption forceUpdateOption ( QStringList ( ) < < " u " < < " update " ,
tr ( " stop current index job and perform incremental updates for<index type> " ) ,
" indexType " ) ;
parser . addOption ( forceUpdateOption ) ;
2021-11-22 16:03:31 +08:00
if ( isPrimary ) {
const QStringList args = QString ( msg ) . split ( ' ' ) ;
parser . process ( args ) ;
2022-10-26 18:01:40 +08:00
if ( parser . isSet ( monitorWindow ) ) {
loadMonitorWindow ( ) ;
m_quickView - > show ( ) ;
return ;
}
2021-11-22 16:03:31 +08:00
if ( parser . isSet ( quitOption ) ) {
qApp - > quit ( ) ;
return ;
}
2024-01-23 11:18:55 +08:00
if ( parser . isSet ( forceUpdateOption ) ) {
BatchIndexer : : Targets targets = BatchIndexer : : None ;
QString target = parser . value ( forceUpdateOption ) ;
if ( target = = " all " ) {
targets = BatchIndexer : : All ;
} else if ( target = = " basic " ) {
targets = BatchIndexer : : Basic ;
} else if ( target = = " content " ) {
targets = BatchIndexer : : Content ;
} else if ( target = = " ocr " ) {
targets = BatchIndexer : : Ocr ;
2024-04-17 17:43:50 +08:00
} else if ( target = = " ai " ) {
target = BatchIndexer : : Ai ;
2024-01-23 11:18:55 +08:00
}
m_indexScheduler - > forceUpdate ( targets ) ;
}
2023-10-07 15:44:59 +08:00
} else {
2021-11-22 16:03:31 +08:00
if ( arguments ( ) . count ( ) < 2 ) {
parser . showHelp ( ) ;
}
parser . process ( arguments ( ) ) ;
2023-10-07 15:44:59 +08:00
if ( parser . isSet ( statusOption ) ) {
showMonitorState ( ) ;
return ;
} else {
sendMessage ( msg ) ;
: : exit ( 0 ) ;
}
2021-11-22 16:03:31 +08:00
}
}
2022-10-26 18:01:40 +08:00
void UkuiSearchService : : loadMonitorWindow ( )
2021-11-22 16:03:31 +08:00
{
2022-12-09 16:30:58 +08:00
if ( ! m_monitor ) {
m_monitor = new Monitor ( m_indexScheduler , this ) ;
}
2022-10-26 18:01:40 +08:00
if ( ! m_quickView ) {
m_quickView = new QQuickView ( ) ;
2022-12-09 16:30:58 +08:00
m_quickView - > setResizeMode ( QQuickView : : SizeRootObjectToView ) ;
2022-10-26 18:01:40 +08:00
m_quickView - > rootContext ( ) - > setContextProperty ( " monitor " , m_monitor ) ;
m_quickView - > setSource ( m_qmlPath ) ;
2021-11-22 16:03:31 +08:00
}
}
2023-10-07 15:44:59 +08:00
void UkuiSearchService : : showMonitorState ( )
{
2024-04-15 09:52:06 +08:00
auto indexMonitor = IndexMonitor : : self ( ) ;
2023-10-09 10:27:29 +08:00
2024-04-15 09:52:06 +08:00
connect ( indexMonitor , & IndexMonitor : : serviceReady , [ & , indexMonitor ] ( ) {
2023-10-07 15:44:59 +08:00
qDebug ( ) < < " QRemoteObjectReplica initialized " ;
2024-04-15 09:52:06 +08:00
QString state = indexMonitor - > indexState ( ) ;
QString message = " Current index path: " + indexMonitor - > currentIndexPaths ( ) . join ( " , " ) + " \n "
+ " Current index scheduler state: " + indexMonitor - > indexState ( ) + " \n " ;
2023-10-07 15:44:59 +08:00
if ( state = = " Idle " | | state = = " Stop " ) {
2024-04-15 09:52:06 +08:00
message + = " Basic index size: " + QString : : number ( indexMonitor - > basicIndexDocNum ( ) ) + " \n "
+ " Content index size: " + QString : : number ( indexMonitor - > contentIndexDocNum ( ) ) + " \n "
+ " Ocr index size: " + QString : : number ( indexMonitor - > ocrContentIndexDocNum ( ) ) + " \n " ;
2023-10-07 15:44:59 +08:00
} else {
2024-04-15 09:52:06 +08:00
message + = " Basic index progress: " + QString : : number ( indexMonitor - > basicIndexProgress ( ) ) + " of " + QString : : number ( indexMonitor - > basicIndexSize ( ) ) + " finished \n "
+ " Content index progress: " + QString : : number ( indexMonitor - > contentIndexProgress ( ) ) + " of " + QString : : number ( indexMonitor - > contentIndexSize ( ) ) + " finished \n "
+ " Ocr content index progress: " + QString : : number ( indexMonitor - > ocrContentIndexProgress ( ) ) + " of " + QString : : number ( indexMonitor - > ocrContentIndexSize ( ) ) + " finished \n " ;
2023-10-07 15:44:59 +08:00
}
2024-04-15 09:52:06 +08:00
IndexMonitor : : stopMonitor ( ) ;
2023-10-07 15:44:59 +08:00
fputs ( qPrintable ( message ) , stdout ) ;
qApp - > quit ( ) ;
} ) ;
}
2023-11-03 17:28:22 +08:00
QUrl UkuiSearchService : : nodeUrl ( )
{
QString displayEnv = ( qgetenv ( " XDG_SESSION_TYPE " ) = = " wayland " ) ? QLatin1String ( " WAYLAND_DISPLAY " ) : QLatin1String ( " DISPLAY " ) ;
QString display ( qgetenv ( displayEnv . toUtf8 ( ) . data ( ) ) ) ;
return QUrl ( QStringLiteral ( " local:ukui-search-service-monitor- " ) + QString ( qgetenv ( " USER " ) ) + display ) ;
}