2022-06-28 11:20:36 +08:00
# include "app-info-table.h"
# include "app-info-table-private.h"
2022-07-12 11:26:03 +08:00
# include "app-info-dbus-argument.h"
2022-06-28 11:20:36 +08:00
2022-06-13 13:38:47 +08:00
# include <QDebug>
# include <QtGlobal>
# include <string>
# include <QStandardPaths>
# include <QProcess>
# include <QDBusInterface>
# include <QDBusReply>
2022-07-12 11:26:03 +08:00
# include <QDBusMetaType>
2022-06-13 13:38:47 +08:00
# include <QFileInfo>
# include <QTime>
# include <QSqlQuery>
# include <QSqlError>
2023-03-07 17:33:08 +08:00
# include <mutex>
# include <QFile>
# include <application-property-helper.h>
2022-06-13 13:38:47 +08:00
2022-03-01 17:39:53 +08:00
using namespace UkuiSearch ;
2023-03-07 17:33:08 +08:00
static std : : once_flag flag ;
static AppInfoTable * global_intance = nullptr ;
2022-06-28 11:20:36 +08:00
2022-10-19 17:11:05 +08:00
AppInfoTablePrivate : : AppInfoTablePrivate ( AppInfoTable * parent ) : QObject ( parent ) , q ( parent ) , m_database ( new QSqlDatabase ( ) )
2022-06-13 13:38:47 +08:00
{
2022-07-12 11:26:03 +08:00
//dbus接收数据库信号
2023-03-14 14:01:28 +08:00
qRegisterMetaType < PropertyMap > ( " PropertyMap " ) ;
qDBusRegisterMetaType < PropertyMap > ( ) ;
2022-06-28 11:20:36 +08:00
2023-03-14 14:01:28 +08:00
qRegisterMetaType < ApplicationInfoMap > ( " ApplicationInfoMap " ) ;
qDBusRegisterMetaType < ApplicationInfoMap > ( ) ;
2022-07-12 11:26:03 +08:00
m_signalTransInterface = new QDBusInterface ( " com.ukui.search.appdb.service " ,
" /org/ukui/search/appDataBase/signalTransformer " ,
" org.ukui.search.signalTransformer " ) ;
if ( ! m_signalTransInterface - > isValid ( ) ) {
2022-06-28 11:20:36 +08:00
qCritical ( ) < < " Create privateDirWatcher Interface Failed Because: " < < QDBusConnection : : sessionBus ( ) . lastError ( ) ;
return ;
} else {
2023-03-14 14:01:28 +08:00
connect ( m_signalTransInterface , SIGNAL ( appDBItemsUpdate ( ApplicationInfoMap ) ) , this , SLOT ( sendAppDBItemsUpdate ( ApplicationInfoMap ) ) ) ;
connect ( m_signalTransInterface , SIGNAL ( appDBItemsUpdateAll ( QStringList ) ) , this , SLOT ( sendAppDBItemsUpdateAll ( QStringList ) ) ) ;
connect ( m_signalTransInterface , SIGNAL ( appDBItemsAdd ( QStringList ) ) , this , SLOT ( sendAppDBItemsAdd ( QStringList ) ) ) ;
2022-07-12 11:26:03 +08:00
connect ( m_signalTransInterface , SIGNAL ( appDBItemsDelete ( QStringList ) ) , this , SLOT ( sendAppDBItemsDelete ( QStringList ) ) ) ;
2022-06-28 11:20:36 +08:00
}
2022-07-12 11:26:03 +08:00
//使用dbus操作数据库获取数据
m_appDBInterface = new QDBusInterface ( " com.ukui.search.appdb.service " ,
" /org/ukui/search/appDataBase/dbManager " ,
" org.ukui.search.appDBManager " ) ;
2022-06-13 13:38:47 +08:00
while ( 1 ) {
srand ( QTime ( 0 , 0 , 0 ) . secsTo ( QTime : : currentTime ( ) ) ) ;
m_ConnectionName = QString : : fromStdString ( std : : to_string ( rand ( ) ) ) ; //随机生产链接
if ( ! QSqlDatabase : : contains ( m_ConnectionName ) )
break ;
}
qDebug ( ) < < " App info database currunt connection name: " < < m_ConnectionName ;
2022-06-23 10:12:02 +08:00
if ( ! this - > openDataBase ( ) ) {
Q_EMIT q - > DBOpenFailed ( ) ;
2022-10-19 17:11:05 +08:00
qWarning ( ) < < " Fail to open App DataBase, because: " < < m_database - > lastError ( ) ;
2022-06-23 10:12:02 +08:00
}
2022-06-13 13:38:47 +08:00
}
2022-07-12 11:26:03 +08:00
void AppInfoTablePrivate : : setAppFavoritesState ( QString & desktopfp , int num )
2022-06-13 13:38:47 +08:00
{
2022-07-12 11:26:03 +08:00
m_appDBInterface - > call ( " updateFavoritesState " , desktopfp , num ) ;
2022-06-13 13:38:47 +08:00
}
2022-07-12 11:26:03 +08:00
void AppInfoTablePrivate : : setAppTopState ( QString & desktopfp , int num )
2022-06-13 13:38:47 +08:00
{
2022-07-12 11:26:03 +08:00
m_appDBInterface - > call ( " updateTopState " , desktopfp , num ) ;
2022-06-13 13:38:47 +08:00
}
2022-07-12 11:26:03 +08:00
bool AppInfoTablePrivate : : changeFavoriteAppPos ( const QString & desktopfp , int pos )
2022-06-13 13:38:47 +08:00
{
2022-07-12 11:26:03 +08:00
QDBusReply < bool > reply = m_appDBInterface - > call ( " changeFavoriteAppPos " , desktopfp , pos ) ;
if ( reply . isValid ( ) ) {
return reply . value ( ) ;
2022-06-28 11:20:36 +08:00
} else {
2022-07-12 11:26:03 +08:00
qDebug ( ) < < m_appDBInterface - > lastError ( ) ;
return false ;
2022-06-28 11:20:36 +08:00
}
}
2022-07-12 11:26:03 +08:00
bool AppInfoTablePrivate : : changeTopAppPos ( const QString & desktopfp , int pos )
2022-06-28 11:20:36 +08:00
{
2022-07-12 11:26:03 +08:00
QDBusReply < bool > reply = m_appDBInterface - > call ( " changeTopAppPos " , desktopfp , pos ) ;
if ( reply . isValid ( ) ) {
return reply . value ( ) ;
2022-06-13 13:38:47 +08:00
} else {
2022-07-12 11:26:03 +08:00
qDebug ( ) < < m_appDBInterface - > lastError ( ) ;
return false ;
2022-06-13 13:38:47 +08:00
}
}
bool AppInfoTablePrivate : : searchInstallApp ( QString & keyWord , QStringList & installAppInfoRes )
{
bool res ( true ) ;
2022-10-19 17:11:05 +08:00
if ( m_database - > transaction ( ) ) {
QSqlQuery sql ( * m_database ) ;
2022-06-13 13:38:47 +08:00
QString cmd ;
if ( keyWord . size ( ) < 2 ) {
2022-07-12 11:26:03 +08:00
cmd = QString ( " SELECT DESKTOP_FILE_PATH,LOCAL_NAME,ICON FROM APPINFO WHERE LOCAL_NAME OR NAME_EN OR NAME_ZH OR FIRST_LETTER_OF_PINYIN LIKE '%%0%' ORDER BY FAVORITES DESC " )
2022-06-13 13:38:47 +08:00
. arg ( keyWord ) ;
} else {
2022-07-12 11:26:03 +08:00
cmd = QString ( " SELECT DESKTOP_FILE_PATH,LOCAL_NAME,ICON FROM APPINFO WHERE LOCAL_NAME OR NAME_EN OR NAME_ZH OR PINYIN_NAME OR FIRST_LETTER_OF_PINYIN LIKE '%%0%' ORDER BY FAVORITES DESC " )
2022-06-13 13:38:47 +08:00
. arg ( keyWord ) ;
}
if ( sql . exec ( cmd ) ) {
while ( sql . next ( ) ) {
installAppInfoRes < < sql . value ( 0 ) . toString ( ) < < sql . value ( 1 ) . toString ( ) < < sql . value ( 2 ) . toString ( ) ;
}
} else {
2022-10-19 17:11:05 +08:00
qWarning ( ) < < QString ( " cmd %0 failed! " ) . arg ( cmd ) < < m_database - > lastError ( ) ;
2022-06-13 13:38:47 +08:00
res = false ;
}
2022-10-19 17:11:05 +08:00
if ( ! m_database - > commit ( ) ) {
2022-06-13 13:38:47 +08:00
qWarning ( ) < < " Failed to commit ! " < < cmd ;
2022-10-19 17:11:05 +08:00
m_database - > rollback ( ) ;
2022-06-13 13:38:47 +08:00
res = false ;
}
} else {
qWarning ( ) < < " Failed to start transaction mode!!! " ;
res = false ;
}
return res ;
}
bool AppInfoTablePrivate : : searchInstallApp ( QStringList & keyWord , QStringList & installAppInfoRes )
{
bool res ( true ) ;
2022-10-19 17:11:05 +08:00
if ( m_database - > transaction ( ) or keyWord . size ( ) ! = 0 ) {
QSqlQuery sql ( * m_database ) ;
2022-06-13 13:38:47 +08:00
QString cmd ;
if ( keyWord . at ( 0 ) . size ( ) < 2 ) {
2022-07-12 11:26:03 +08:00
cmd = QString ( " SELECT DESKTOP_FILE_PATH,LOCAL_NAME,ICON,NAME_EN,NAME_ZH,FIRST_LETTER_OF_PINYIN FROM APPINFO "
2022-06-13 13:38:47 +08:00
" WHERE LOCAL_NAME LIKE '%%0%' OR NAME_EN LIKE '%%0%' OR NAME_ZH LIKE '%%0%' OR FIRST_LETTER_OF_PINYIN LIKE '%%0%' " )
. arg ( keyWord . at ( 0 ) ) ;
} else {
2022-07-12 11:26:03 +08:00
cmd = QString ( " SELECT DESKTOP_FILE_PATH,LOCAL_NAME,ICON,NAME_EN,NAME_ZH,FIRST_LETTER_OF_PINYIN FROM APPINFO "
2022-06-13 13:38:47 +08:00
" WHERE LOCAL_NAME LIKE '%%0%' OR NAME_EN LIKE '%%0%' OR NAME_ZH LIKE '%%0%' OR PINYIN_NAME LIKE '%%0%' OR FIRST_LETTER_OF_PINYIN LIKE '%%0%' " )
. arg ( keyWord . at ( 0 ) ) ;
}
for ( int i = 0 ; + + i < keyWord . size ( ) ; ) {
if ( keyWord . at ( i ) . size ( ) < 2 ) {
cmd + = QString ( " AND (LOCAL_NAME LIKE '%%1%' OR NAME_EN LIKE '%%1%' OR NAME_ZH LIKE '%%1%' OR FIRST_LETTER_OF_PINYIN LIKE '%%1%') " )
. arg ( keyWord . at ( i ) ) ;
} else {
cmd + = QString ( " AND (LOCAL_NAME LIKE '%%1%' OR NAME_EN LIKE '%%1%' OR NAME_ZH LIKE '%%1%' OR PINYIN_NAME LIKE '%%1%' OR FIRST_LETTER_OF_PINYIN LIKE '%%1%') " )
. arg ( keyWord . at ( i ) ) ;
}
}
cmd + = QString ( " ORDER BY LENGTH(LOCAL_NAME) " ) ;
if ( sql . exec ( cmd ) ) {
while ( sql . next ( ) ) {
installAppInfoRes < < sql . value ( 0 ) . toString ( ) < < sql . value ( 1 ) . toString ( ) < < sql . value ( 2 ) . toString ( ) ;
}
} else {
2022-10-19 17:11:05 +08:00
qWarning ( ) < < QString ( " cmd %0 failed! " ) . arg ( cmd ) < < m_database - > lastError ( ) ;
2022-06-13 13:38:47 +08:00
res = false ;
}
2022-10-19 17:11:05 +08:00
if ( ! m_database - > commit ( ) ) {
2022-06-13 13:38:47 +08:00
qWarning ( ) < < " Failed to commit ! " < < cmd ;
2022-10-19 17:11:05 +08:00
m_database - > rollback ( ) ;
2022-06-13 13:38:47 +08:00
res = false ;
}
} else {
qWarning ( ) < < " Failed to start transaction mode!!! keyword size: " < < keyWord . size ( ) ;
res = false ;
}
return res ;
}
QString AppInfoTablePrivate : : lastError ( ) const
{
2022-10-19 17:11:05 +08:00
return m_database - > lastError ( ) . text ( ) ;
2022-06-13 13:38:47 +08:00
}
2023-02-17 15:55:10 +08:00
bool AppInfoTablePrivate : : tranPidToDesktopFp ( int pid , QString & desktopfp )
{
QDBusReply < QString > reply = m_appDBInterface - > call ( " tranPidToDesktopFp " , pid ) ;
if ( reply . isValid ( ) ) {
desktopfp = reply . value ( ) ;
return true ;
} else {
qDebug ( ) < < m_appDBInterface - > lastError ( ) ;
return false ;
}
}
2022-06-28 14:00:57 +08:00
bool AppInfoTablePrivate : : setAppLaunchTimes ( QString & desktopfp , size_t num )
{
bool res ( true ) ;
2022-10-19 17:11:05 +08:00
if ( m_database - > transaction ( ) ) {
QSqlQuery sql ( * m_database ) ;
2022-06-28 14:00:57 +08:00
QString cmd = QString ( " UPDATE appInfo SET MODIFYED_TIME='%0', LAUNCH_TIMES=%1, LAUNCHED=%2 WHERE DESKTOP_FILE_PATH='%3' " )
. arg ( QDateTime : : currentDateTime ( ) . toString ( " yyyy-MM-dd hh:mm:ss " ) )
. arg ( num )
. arg ( 1 )
. arg ( desktopfp ) ;
if ( ! sql . exec ( cmd ) ) {
2022-10-19 17:11:05 +08:00
qWarning ( ) < < " Set app favorites state failed! " < < m_database - > lastError ( ) ;
2022-06-28 14:00:57 +08:00
res = false ;
}
2022-10-19 17:11:05 +08:00
if ( ! m_database - > commit ( ) ) {
2022-06-28 14:00:57 +08:00
qWarning ( ) < < " Failed to commit ! " < < cmd ;
2022-10-19 17:11:05 +08:00
m_database - > rollback ( ) ;
2022-06-28 14:00:57 +08:00
res = false ;
}
} else {
qWarning ( ) < < " Failed to start transaction mode!!! " ;
res = false ;
}
return res ;
}
bool AppInfoTablePrivate : : updateAppLaunchTimes ( QString & desktopfp )
{
bool res ( true ) ;
2022-10-19 17:11:05 +08:00
if ( m_database - > transaction ( ) ) {
QSqlQuery sql ( * m_database ) ;
2022-07-12 11:26:03 +08:00
QString cmd = QString ( " SELECT LAUNCH_TIMES FROM APPINFO WHERE DESKTOP_FILE_PATH='%1' " ) . arg ( desktopfp ) ;
2022-06-28 14:00:57 +08:00
if ( sql . exec ( cmd ) ) {
if ( sql . next ( ) ) {
cmd = QString ( " UPDATE appInfo SET MODIFYED_TIME='%0', LAUNCH_TIMES=%1, LAUNCHED=%2 WHERE DESKTOP_FILE_PATH='%3' " )
. arg ( QDateTime : : currentDateTime ( ) . toString ( " yyyy-MM-dd hh:mm:ss " ) )
. arg ( sql . value ( 0 ) . toInt ( ) + 1 )
. arg ( 1 )
. arg ( desktopfp ) ;
if ( ! sql . exec ( cmd ) ) {
2022-10-19 17:11:05 +08:00
qWarning ( ) < < " Set app favorites state failed! " < < m_database - > lastError ( ) ;
2022-06-28 14:00:57 +08:00
res = false ;
}
} else {
qWarning ( ) < < " Failed to exec next! " < < cmd ;
res = false ;
}
} else {
qWarning ( ) < < " Failed to exec: " < < cmd ;
res = false ;
}
2022-10-19 17:11:05 +08:00
if ( ! m_database - > commit ( ) ) {
2022-06-28 14:00:57 +08:00
qWarning ( ) < < " Failed to commit ! " < < cmd ;
2022-10-19 17:11:05 +08:00
m_database - > rollback ( ) ;
2022-06-28 14:00:57 +08:00
res = false ;
}
} else {
qWarning ( ) < < " Failed to start transaction mode!!! " ;
res = false ;
}
return res ;
}
bool AppInfoTablePrivate : : setAppLockState ( QString & desktopfp , size_t num )
{
bool res ( true ) ;
2022-10-19 17:11:05 +08:00
if ( m_database - > transaction ( ) ) {
QSqlQuery sql ( * m_database ) ;
2022-06-28 14:00:57 +08:00
QString cmd = QString ( " UPDATE appInfo SET MODIFYED_TIME='%0', LOCK=%1 WHERE DESKTOP_FILE_PATH='%2' " )
. arg ( QDateTime : : currentDateTime ( ) . toString ( " yyyy-MM-dd hh:mm:ss " ) )
. arg ( num )
. arg ( desktopfp ) ;
if ( ! sql . exec ( cmd ) ) {
2022-10-19 17:11:05 +08:00
qWarning ( ) < < " Set app favorites state failed! " < < m_database - > lastError ( ) ;
2022-06-28 14:00:57 +08:00
res = false ;
}
2022-10-19 17:11:05 +08:00
if ( ! m_database - > commit ( ) ) {
2022-06-28 14:00:57 +08:00
qWarning ( ) < < " Failed to commit ! " < < cmd ;
2022-10-19 17:11:05 +08:00
m_database - > rollback ( ) ;
2022-06-28 14:00:57 +08:00
res = false ;
}
} else {
qWarning ( ) < < " Failed to start transaction mode!!! " ;
res = false ;
}
return res ;
}
bool AppInfoTablePrivate : : getAllAppDesktopList ( QStringList & list )
{
bool res ( true ) ;
2022-10-19 17:11:05 +08:00
QSqlQuery sql ( * m_database ) ;
2022-07-12 11:26:03 +08:00
QString cmd = QString ( " SELECT DESKTOP_FILE_PATH FROM APPINFO " ) ;
2022-06-28 14:00:57 +08:00
if ( sql . exec ( cmd ) ) {
while ( sql . next ( ) ) {
list . append ( sql . value ( 0 ) . toString ( ) ) ;
}
} else {
2022-10-19 17:11:05 +08:00
qWarning ( ) < < QString ( " cmd %0 failed! " ) . arg ( cmd ) < < m_database - > lastError ( ) ;
2022-06-28 14:00:57 +08:00
res = false ;
}
return res ;
}
2022-06-13 13:38:47 +08:00
AppInfoTablePrivate : : ~ AppInfoTablePrivate ( )
{
this - > closeDataBase ( ) ;
2022-10-19 17:11:05 +08:00
if ( m_signalTransInterface )
delete m_signalTransInterface ;
m_signalTransInterface = nullptr ;
if ( m_appDBInterface )
delete m_appDBInterface ;
m_appDBInterface = nullptr ;
2022-06-13 13:38:47 +08:00
}
2022-06-23 10:12:02 +08:00
bool AppInfoTablePrivate : : initDateBaseConnection ( )
2022-06-13 13:38:47 +08:00
{
2022-10-19 17:11:05 +08:00
m_database - > setDatabaseName ( APP_DATABASE_PATH + APP_DATABASE_NAME ) ;
if ( ! m_database - > open ( ) ) {
qWarning ( ) < < m_database - > lastError ( ) ;
2022-06-23 10:12:02 +08:00
return false ;
// QApplication::quit();
2022-06-13 13:38:47 +08:00
}
2022-06-23 10:12:02 +08:00
return true ;
2022-06-13 13:38:47 +08:00
}
2022-06-23 10:12:02 +08:00
bool AppInfoTablePrivate : : openDataBase ( )
2022-06-13 13:38:47 +08:00
{
2022-10-19 17:11:05 +08:00
* m_database = QSqlDatabase : : addDatabase ( " QSQLITE " , m_ConnectionName ) ;
m_database - > setDatabaseName ( APP_DATABASE_PATH + APP_DATABASE_NAME ) ;
2022-06-13 13:38:47 +08:00
2022-10-19 17:11:05 +08:00
if ( ! m_database - > open ( ) ) {
2022-06-23 10:12:02 +08:00
return false ;
2022-06-13 13:38:47 +08:00
}
2022-06-23 10:12:02 +08:00
return true ;
2022-06-13 13:38:47 +08:00
}
void AppInfoTablePrivate : : closeDataBase ( )
2022-03-01 17:39:53 +08:00
{
2022-10-19 17:11:05 +08:00
m_database - > close ( ) ;
delete m_database ;
m_database = nullptr ;
2022-06-13 13:38:47 +08:00
QSqlDatabase : : removeDatabase ( m_ConnectionName ) ;
2022-03-01 17:39:53 +08:00
}
2023-03-14 14:01:28 +08:00
void AppInfoTablePrivate : : sendAppDBItemsUpdate ( ApplicationInfoMap results )
2022-06-28 11:20:36 +08:00
{
Q_EMIT q - > appDBItems2BUpdate ( results ) ;
}
2023-03-14 14:01:28 +08:00
void AppInfoTablePrivate : : sendAppDBItemsUpdateAll ( QStringList desktopFilePaths )
2022-06-28 11:20:36 +08:00
{
2023-03-14 14:01:28 +08:00
Q_EMIT q - > appDBItems2BUpdateAll ( desktopFilePaths ) ;
2022-06-28 11:20:36 +08:00
}
2023-03-14 14:01:28 +08:00
void AppInfoTablePrivate : : sendAppDBItemsAdd ( QStringList desktopFilePaths )
2022-06-28 11:20:36 +08:00
{
2023-03-14 14:01:28 +08:00
Q_EMIT q - > appDBItems2BAdd ( desktopFilePaths ) ;
}
void AppInfoTablePrivate : : sendAppDBItemsDelete ( QStringList desktopFilePaths )
{
Q_EMIT q - > appDBItems2BDelete ( desktopFilePaths ) ;
2022-06-28 11:20:36 +08:00
}
2023-03-07 17:33:08 +08:00
AppInfoTable * AppInfoTable : : self ( )
{
std : : call_once ( flag , [ & ] {
global_intance = new AppInfoTable ( ) ;
} ) ;
return global_intance ;
}
2022-03-01 17:39:53 +08:00
AppInfoTable : : AppInfoTable ( QObject * parent ) : QObject ( parent ) , d ( new AppInfoTablePrivate ( this ) )
{
}
2022-06-13 13:38:47 +08:00
2023-03-07 17:33:08 +08:00
bool AppInfoTable : : query ( PropertyMap & propertyMap , const QString & desktopFile , Properties properties )
{
QString field ;
for ( const ApplicationProperty : : Property & pro : properties ) {
field . append ( ApplicationPropertyHelper ( pro ) . dataBaseField ( ) + " , " ) ;
}
field . remove ( field . length ( ) - 1 , 1 ) ;
QSqlQuery query ( * d - > m_database ) ;
query . setForwardOnly ( true ) ;
query . prepare ( QString ( " SELECT %0 FROM APPINFO WHERE DESKTOP_FILE_PATH=:desktopFile " ) . arg ( field ) ) ;
query . bindValue ( " :desktopFile " , desktopFile ) ;
if ( ! query . exec ( ) ) {
qWarning ( ) < < d - > m_database - > lastError ( ) < < query . lastError ( ) ;
return false ;
}
while ( query . next ( ) ) {
for ( int i = 0 ; i < properties . size ( ) ; i + + ) {
propertyMap . insert ( properties . at ( i ) , query . value ( i ) ) ;
}
}
return true ;
}
bool AppInfoTable : : query ( ApplicationInfoMap & infoMap , Properties properties )
{
QString field ( ApplicationPropertyHelper ( ApplicationProperty : : Property : : DesktopFilePath ) . dataBaseField ( ) ) ;
for ( const ApplicationProperty : : Property & pro : properties ) {
field . append ( " , " + ApplicationPropertyHelper ( pro ) . dataBaseField ( ) ) ;
}
QString sql = QString ( " SELECT %0 FROM APPINFO " ) . arg ( field ) ;
QSqlQuery query ( * d - > m_database ) ;
query . setForwardOnly ( true ) ;
if ( ! query . exec ( sql ) ) {
qWarning ( ) < < d - > m_database - > lastError ( ) < < sql ;
return false ;
}
while ( query . next ( ) ) {
PropertyMap propertyMap ;
2023-03-14 14:01:28 +08:00
for ( int i = 0 ; i < properties . size ( ) ; i + + ) {
propertyMap . insert ( properties . at ( i ) , query . value ( i + 1 ) ) ;
2023-03-07 17:33:08 +08:00
}
infoMap . insert ( query . value ( 0 ) . toString ( ) , propertyMap ) ;
}
return true ;
}
2023-03-14 14:01:28 +08:00
bool AppInfoTable : : query ( ApplicationInfoMap & infoMap , Properties properties , PropertyMap restrictions )
2022-06-13 13:38:47 +08:00
{
2023-03-14 14:01:28 +08:00
QString field = ApplicationPropertyHelper ( ApplicationProperty : : Property : : DesktopFilePath ) . dataBaseField ( ) ;
for ( const ApplicationProperty : : Property & pro : properties ) {
field . append ( " , " + ApplicationPropertyHelper ( pro ) . dataBaseField ( ) ) ;
}
2022-06-13 13:38:47 +08:00
2023-03-14 14:01:28 +08:00
QString condition ;
for ( const ApplicationProperty : : Property prop : restrictions . keys ( ) ) {
condition . append ( ApplicationPropertyHelper ( prop ) . dataBaseField ( ) + " =? AND " ) ;
}
condition = condition . left ( condition . lastIndexOf ( " AND " ) ) ;
2022-06-13 13:38:47 +08:00
2023-03-14 14:01:28 +08:00
QSqlQuery query ( * d - > m_database ) ;
query . setForwardOnly ( true ) ;
query . prepare ( QString ( " SELECT %0 FROM APPINFO WHERE %1 " )
. arg ( field )
. arg ( condition ) ) ;
int i = 0 ;
for ( const QVariant & conditionValue : restrictions ) {
query . bindValue ( i , conditionValue ) ;
i + + ;
}
if ( ! query . exec ( ) ) {
qWarning ( ) < < d - > m_database - > lastError ( ) < < query . lastError ( ) ;
return false ;
}
while ( query . next ( ) ) {
PropertyMap propertyMap ;
for ( int i = 0 ; i < properties . size ( ) ; i + + ) {
propertyMap . insert ( properties . at ( i ) , query . value ( i + 1 ) ) ;
}
infoMap . insert ( query . value ( 0 ) . toString ( ) , propertyMap ) ;
}
return true ;
2022-06-13 13:38:47 +08:00
}
2023-03-14 14:01:28 +08:00
bool AppInfoTable : : query ( ApplicationInfoMap & infoMap , Properties properties , const QString & keyword )
2022-06-28 11:20:36 +08:00
{
2023-03-14 14:01:28 +08:00
QString field ( ApplicationPropertyHelper ( ApplicationProperty : : Property : : DesktopFilePath ) . dataBaseField ( ) ) ;
for ( const ApplicationProperty : : Property & pro : properties ) {
field . append ( " , " + ApplicationPropertyHelper ( pro ) . dataBaseField ( ) ) ;
}
2022-06-28 11:20:36 +08:00
2023-03-14 14:01:28 +08:00
QString cmd ;
if ( keyword . size ( ) < 2 ) {
cmd = QString ( " SELECT %0 FROM APPINFO WHERE (LOCAL_NAME || NAME_EN || NAME_ZH || FIRST_LETTER_OF_PINYIN) LIKE :keyword " ) . arg ( field ) ;
} else {
cmd = QString ( " SELECT %0 FROM APPINFO WHERE (LOCAL_NAME || NAME_EN || NAME_ZH || PINYIN_NAME || FIRST_LETTER_OF_PINYIN) LIKE :keyword " ) . arg ( field ) ;
}
2022-06-28 11:20:36 +08:00
2023-03-14 14:01:28 +08:00
QSqlQuery query ( * d - > m_database ) ;
query . setForwardOnly ( true ) ;
query . prepare ( cmd ) ;
query . bindValue ( " :keyword " , " % " + keyword + " % " ) ;
if ( ! query . exec ( ) ) {
qWarning ( ) < < d - > m_database - > lastError ( ) < < query . lastError ( ) ;
return false ;
}
while ( query . next ( ) ) {
PropertyMap propertyMap ;
for ( int i = 0 ; i < properties . size ( ) ; i + + ) {
propertyMap . insert ( properties . at ( i ) , query . value ( i + 1 ) ) ;
}
infoMap . insert ( query . value ( 0 ) . toString ( ) , propertyMap ) ;
}
return true ;
2022-06-13 13:38:47 +08:00
}
2023-03-14 14:01:28 +08:00
void AppInfoTable : : setAppFavoritesState ( QString & desktopfp , size_t num )
2022-06-13 13:38:47 +08:00
{
2023-03-14 14:01:28 +08:00
return d - > setAppFavoritesState ( desktopfp , num ) ;
2022-06-13 13:38:47 +08:00
}
2023-03-14 14:01:28 +08:00
void AppInfoTable : : setAppTopState ( QString & desktopfp , size_t num )
2022-06-13 13:38:47 +08:00
{
2023-03-14 14:01:28 +08:00
return d - > setAppTopState ( desktopfp , num ) ;
2022-06-13 13:38:47 +08:00
}
2023-03-14 14:01:28 +08:00
bool AppInfoTable : : changeFavoriteAppPos ( const QString & desktopfp , size_t pos )
2022-06-13 13:38:47 +08:00
{
2023-03-14 14:01:28 +08:00
return d - > changeFavoriteAppPos ( desktopfp , pos ) ;
2022-06-13 13:38:47 +08:00
}
2023-03-14 14:01:28 +08:00
bool AppInfoTable : : changeTopAppPos ( const QString & desktopfp , size_t pos )
2022-06-13 13:38:47 +08:00
{
2023-03-14 14:01:28 +08:00
return d - > changeTopAppPos ( desktopfp , pos ) ;
2022-06-13 13:38:47 +08:00
}
bool AppInfoTable : : searchInstallApp ( QString & keyWord , QStringList & installAppInfoRes )
{
return d - > searchInstallApp ( keyWord , installAppInfoRes ) ;
}
bool AppInfoTable : : searchInstallApp ( QStringList & keyWord , QStringList & installAppInfoRes )
{
return d - > searchInstallApp ( keyWord , installAppInfoRes ) ;
}
QString AppInfoTable : : lastError ( ) const
{
return d - > lastError ( ) ;
}
2022-06-28 14:00:57 +08:00
2023-02-17 15:55:10 +08:00
bool AppInfoTable : : tranPidToDesktopFp ( int pid , QString & desktopfp )
{
return d - > tranPidToDesktopFp ( pid , desktopfp ) ;
}
2022-06-28 14:00:57 +08:00
//下面接口暂时没啥用,不外放。
bool AppInfoTable : : setAppLaunchTimes ( QString & desktopfp , size_t num )
{
return d - > setAppLaunchTimes ( desktopfp , num ) ;
}
bool AppInfoTable : : updateAppLaunchTimes ( QString & desktopfp )
{
return d - > updateAppLaunchTimes ( desktopfp ) ;
}
bool AppInfoTable : : setAppLockState ( QString & desktopfp , size_t num )
{
return d - > setAppLockState ( desktopfp , num ) ;
}