Add interfaces for changing position of top&favorites apps.

Add appDB update signal.

Add signal transformer and register its signals as dbus signals.
This commit is contained in:
JunjieBai 2022-06-28 11:20:36 +08:00
parent 72aae0bf98
commit 13288b97bb
12 changed files with 557 additions and 184 deletions

51
libsearch/app-db-common.h Normal file
View File

@ -0,0 +1,51 @@
#ifndef APPDBCOMMON_H
#define APPDBCOMMON_H
#include <QDir>
#include <QMetaType>
#include <QDBusArgument>
namespace UkuiSearch {
#define APP_DATABASE_PATH QDir::homePath()+"/.config/org.ukui/ukui-search/appdata/"
#define APP_DATABASE_NAME "app-info.db"
struct AppInfoResult
{
QString desktopPath;
QString iconName;
QString appLocalName;
QString firstLetter;
QString category;
int top;
int favorate;
int launchTimes;
int lock;
AppInfoResult() : top(0), favorate(0), launchTimes(0), lock(0) {}
friend QDBusArgument &operator << (QDBusArgument &argument, const AppInfoResult &infoResult)
{
argument.beginStructure();
argument << infoResult.desktopPath << infoResult.iconName << infoResult.appLocalName << infoResult.firstLetter
<< infoResult.category << infoResult.top << infoResult.favorate << infoResult.launchTimes << infoResult.lock;
argument.endStructure();
return argument;
}
friend const QDBusArgument &operator >> (const QDBusArgument &argument, AppInfoResult &infoResult)
{
argument.beginStructure();
argument >> infoResult.desktopPath >> infoResult.iconName >> infoResult.appLocalName >> infoResult.firstLetter
>> infoResult.category >> infoResult.top >> infoResult.favorate >> infoResult.launchTimes >> infoResult.lock;
argument.endStructure();
return argument;
}
};
}
Q_DECLARE_METATYPE(UkuiSearch::AppInfoResult)
#endif // APPDBCOMMON_H

View File

@ -1,10 +1,13 @@
#ifndef APPINFOTABLEPRIVATE_H #ifndef APPINFOTABLEPRIVATE_H
#define APPINFOTABLEPRIVATE_H #define APPINFOTABLEPRIVATE_H
#include "app-info-table.h"
#include <QObject> #include <QObject>
#include <QDBusInterface>
#include <QSqlDatabase> #include <QSqlDatabase>
#include <app-info-table.h>
namespace UkuiSearch { namespace UkuiSearch {
class AppInfoTablePrivate : public QObject class AppInfoTablePrivate : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -15,27 +18,36 @@ public:
bool setAppFavoritesState(QString &desktopfp, size_t num); bool setAppFavoritesState(QString &desktopfp, size_t num);
bool setAppTopState(QString &desktopfp, size_t num); bool setAppTopState(QString &desktopfp, size_t num);
//下面三个暂时未封装
bool setAppLaunchTimes(QString &desktopfp, size_t num); bool setAppLaunchTimes(QString &desktopfp, size_t num);
bool updateAppLaunchTimes(QString &desktopfp); bool updateAppLaunchTimes(QString &desktopfp);
bool setAppLockState(QString &desktopfp, size_t num); bool setAppLockState(QString &desktopfp, size_t num);
//拖动改变置顶和收藏应用位置
bool changeFavoriteAppPos(const QString &desktopfp, size_t pos);
bool changeTopAppPos(const QString &desktopfp, size_t pos);
bool getAllAppDesktopList(QStringList &list); bool getAllAppDesktopList(QStringList &list);
bool getFavoritesAppList(QStringList &list); bool getFavoritesAppList(QStringList &list);
bool getTopAppList(QStringList &list); bool getTopAppList(QStringList &list);
bool getLaunchTimesAppList(QStringList &list); bool getLaunchTimesAppList(QStringList &list);
bool getAppCategory(QString &desktopfp, QString &category); bool getAppCategory(QString &desktopfp, QString &category);
bool getAppInfoResults(QVector<AppInfoTable::AppInfoResult> &appInfoResults); //获取所有应用信息并存到一个结构体中
bool getAppInfoResults(QVector<AppInfoResult> &appInfoResults);
//获取单个应用的某个状态(锁定,置顶,打开状态,收藏)
bool getAppLockState(QString &desktopfp, size_t &num); bool getAppLockState(QString &desktopfp, size_t &num);
bool getAppTopState(QString &desktopfp, size_t &num); bool getAppTopState(QString &desktopfp, size_t &num);
bool getAppLaunchedState(QString &desktopfp, size_t &num); bool getAppLaunchedState(QString &desktopfp, size_t &num);
bool getAppFavoriteState(QString &desktopfp, size_t &num); bool getAppFavoriteState(QString &desktopfp, size_t &num);
//添加快捷方式
bool addAppShortcut2Desktop(QString &desktopfp); bool addAppShortcut2Desktop(QString &desktopfp);
bool addAppShortcut2Panel(QString &desktopfp); bool addAppShortcut2Panel(QString &desktopfp);
bool getInstallAppMap(QMultiMap<QString, QStringList> &installAppMap); //搜索接口
bool searchInstallApp(QString &keyWord, QStringList &installAppInfoRes); bool searchInstallApp(QString &keyWord, QStringList &installAppInfoRes);
bool searchInstallApp(QStringList &keyWord, QStringList &installAppInfoRes); bool searchInstallApp(QStringList &keyWord, QStringList &installAppInfoRes);
@ -49,10 +61,17 @@ private:
bool openDataBase(); bool openDataBase();
void closeDataBase(); void closeDataBase();
QDBusInterface *m_interface = nullptr;
AppInfoTable *q = nullptr; AppInfoTable *q = nullptr;
QSqlDatabase *m_database = nullptr; QSqlDatabase m_database;
QString m_ConnectionName; QString m_ConnectionName;
public Q_SLOTS:
void sendAppDBItemsUpdate(QVector<AppInfoResult> results);
void sendAppDBItemsAdd(QVector<AppInfoResult> results);
void sendAppDBItemsDelete(QStringList desktopfps);
}; };

View File

@ -1,3 +1,6 @@
#include "app-info-table.h"
#include "app-info-table-private.h"
#include <QDebug> #include <QDebug>
#include <QtGlobal> #include <QtGlobal>
#include <string> #include <string>
@ -11,13 +14,23 @@
#include <QSqlError> #include <QSqlError>
#include <QApplication> #include <QApplication>
#include <qt5xdg/XdgDesktopFile> #include <qt5xdg/XdgDesktopFile>
#include "app-info-table.h"
#include "app-info-table-private.h"
#include "../ukui-search-app-data-service/app-db-common-defines.h"
using namespace UkuiSearch; using namespace UkuiSearch;
AppInfoTablePrivate::AppInfoTablePrivate(AppInfoTable *parent) : QObject(parent), q(parent), m_database(new QSqlDatabase)
AppInfoTablePrivate::AppInfoTablePrivate(AppInfoTable *parent) : QObject(parent), q(parent), m_database(QSqlDatabase())
{ {
m_interface = new QDBusInterface("com.ukui.search.appdb.service",
"/org/ukui/search/appDataBase",
"org.ukui.search.appdb");
if (!m_interface->isValid()) {
qCritical() << "Create privateDirWatcher Interface Failed Because: " << QDBusConnection::sessionBus().lastError();
return;
} else {
connect(m_interface, SIGNAL(appDBItemsAdd(QVector<AppInfoResult>)), this, SLOT(sendAppDBItemsAdd(QVector<AppInfoResult>)));
connect(m_interface, SIGNAL(appDBItemsUpdate(QVector<AppInfoResult>)), this, SLOT(sendAppDBItemsUpdate(QVector<AppInfoResult>)));
connect(m_interface, SIGNAL(appDBItemsDelete(QStringList)), this, SLOT(sendAppDBItemsDelete(QStringList)));
}
while(1) { while(1) {
srand(QTime(0,0,0).secsTo(QTime::currentTime())); srand(QTime(0,0,0).secsTo(QTime::currentTime()));
m_ConnectionName = QString::fromStdString(std::to_string(rand()));//随机生产链接 m_ConnectionName = QString::fromStdString(std::to_string(rand()));//随机生产链接
@ -27,26 +40,26 @@ AppInfoTablePrivate::AppInfoTablePrivate(AppInfoTable *parent) : QObject(parent)
qDebug() << "App info database currunt connection name:" << m_ConnectionName; qDebug() << "App info database currunt connection name:" << m_ConnectionName;
if (!this->openDataBase()) { if (!this->openDataBase()) {
Q_EMIT q->DBOpenFailed(); Q_EMIT q->DBOpenFailed();
qWarning() << "Fail to open App DataBase, because:" << m_database->lastError(); qWarning() << "Fail to open App DataBase, because:" << m_database.lastError();
} }
} }
bool AppInfoTablePrivate::setAppFavoritesState(QString &desktopfp, size_t num) bool AppInfoTablePrivate::setAppFavoritesState(QString &desktopfp, size_t num)
{ {
bool res(true); bool res(true);
if (m_database->transaction()) { if (m_database.transaction()) {
QSqlQuery sql(*m_database); QSqlQuery sql(m_database);
QString cmd = QString("UPDATE appInfo SET MODIFYED_TIME='%0', FAVORITES=%1 WHERE DESKTOP_FILE_PATH='%2'") QString cmd = QString("UPDATE appInfo SET MODIFYED_TIME='%0', FAVORITES=%1 WHERE DESKTOP_FILE_PATH='%2'")
.arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")) .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"))
.arg(num) .arg(num)
.arg(desktopfp); .arg(desktopfp);
if (!sql.exec(cmd)) { if (!sql.exec(cmd)) {
qWarning() << "Set app favorites state failed!" << m_database->lastError(); qWarning() << "Set app favorites state failed!" << m_database.lastError();
res = false; res = false;
} }
if (!m_database->commit()) { if (!m_database.commit()) {
qWarning() << "Failed to commit !" << cmd; qWarning() << "Failed to commit !" << cmd;
m_database->rollback(); m_database.rollback();
res = false; res = false;
} }
} else { } else {
@ -59,19 +72,19 @@ bool AppInfoTablePrivate::setAppFavoritesState(QString &desktopfp, size_t num)
bool AppInfoTablePrivate::setAppTopState(QString &desktopfp, size_t num) bool AppInfoTablePrivate::setAppTopState(QString &desktopfp, size_t num)
{ {
bool res(true); bool res(true);
if (m_database->transaction()) { if (m_database.transaction()) {
QSqlQuery sql(*m_database); QSqlQuery sql(m_database);
QString cmd = QString("UPDATE appInfo SET MODIFYED_TIME='%0', TOP=%1 WHERE DESKTOP_FILE_PATH='%2'") QString cmd = QString("UPDATE appInfo SET MODIFYED_TIME='%0', TOP=%1 WHERE DESKTOP_FILE_PATH='%2'")
.arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")) .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"))
.arg(num) .arg(num)
.arg(desktopfp); .arg(desktopfp);
if (!sql.exec(cmd)) { if (!sql.exec(cmd)) {
qWarning() << "Set app favorites state failed!" << m_database->lastError(); qWarning() << "Set app favorites state failed!" << m_database.lastError();
res = false; res = false;
} }
if (!m_database->commit()) { if (!m_database.commit()) {
qWarning() << "Failed to commit !" << cmd; qWarning() << "Failed to commit !" << cmd;
m_database->rollback(); m_database.rollback();
res = false; res = false;
} }
} else { } else {
@ -84,20 +97,20 @@ bool AppInfoTablePrivate::setAppTopState(QString &desktopfp, size_t num)
bool AppInfoTablePrivate::setAppLaunchTimes(QString &desktopfp, size_t num) bool AppInfoTablePrivate::setAppLaunchTimes(QString &desktopfp, size_t num)
{ {
bool res(true); bool res(true);
if (m_database->transaction()) { if (m_database.transaction()) {
QSqlQuery sql(*m_database); QSqlQuery sql(m_database);
QString cmd = QString("UPDATE appInfo SET MODIFYED_TIME='%0', LAUNCH_TIMES=%1, LAUNCHED=%2 WHERE DESKTOP_FILE_PATH='%3'") 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(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"))
.arg(num) .arg(num)
.arg(1) .arg(1)
.arg(desktopfp); .arg(desktopfp);
if (!sql.exec(cmd)) { if (!sql.exec(cmd)) {
qWarning() << "Set app favorites state failed!" << m_database->lastError(); qWarning() << "Set app favorites state failed!" << m_database.lastError();
res = false; res = false;
} }
if (!m_database->commit()) { if (!m_database.commit()) {
qWarning() << "Failed to commit !" << cmd; qWarning() << "Failed to commit !" << cmd;
m_database->rollback(); m_database.rollback();
res = false; res = false;
} }
} else { } else {
@ -110,8 +123,8 @@ bool AppInfoTablePrivate::setAppLaunchTimes(QString &desktopfp, size_t num)
bool AppInfoTablePrivate::updateAppLaunchTimes(QString &desktopfp) bool AppInfoTablePrivate::updateAppLaunchTimes(QString &desktopfp)
{ {
bool res(true); bool res(true);
if (m_database->transaction()) { if (m_database.transaction()) {
QSqlQuery sql(*m_database); QSqlQuery sql(m_database);
QString cmd = QString("SELECT LAUNCH_TIMES FROM appInfo WHERE DESKTOP_FILE_PATH='%1'").arg(desktopfp); QString cmd = QString("SELECT LAUNCH_TIMES FROM appInfo WHERE DESKTOP_FILE_PATH='%1'").arg(desktopfp);
if (sql.exec(cmd)) { if (sql.exec(cmd)) {
if (sql.next()) { if (sql.next()) {
@ -121,7 +134,7 @@ bool AppInfoTablePrivate::updateAppLaunchTimes(QString &desktopfp)
.arg(1) .arg(1)
.arg(desktopfp); .arg(desktopfp);
if (!sql.exec(cmd)) { if (!sql.exec(cmd)) {
qWarning() << "Set app favorites state failed!" << m_database->lastError(); qWarning() << "Set app favorites state failed!" << m_database.lastError();
res = false; res = false;
} }
} else { } else {
@ -132,9 +145,9 @@ bool AppInfoTablePrivate::updateAppLaunchTimes(QString &desktopfp)
qWarning() << "Failed to exec:" << cmd; qWarning() << "Failed to exec:" << cmd;
res = false; res = false;
} }
if (!m_database->commit()) { if (!m_database.commit()) {
qWarning() << "Failed to commit !" << cmd; qWarning() << "Failed to commit !" << cmd;
m_database->rollback(); m_database.rollback();
res = false; res = false;
} }
} else { } else {
@ -147,19 +160,19 @@ bool AppInfoTablePrivate::updateAppLaunchTimes(QString &desktopfp)
bool AppInfoTablePrivate::setAppLockState(QString &desktopfp, size_t num) bool AppInfoTablePrivate::setAppLockState(QString &desktopfp, size_t num)
{ {
bool res(true); bool res(true);
if (m_database->transaction()) { if (m_database.transaction()) {
QSqlQuery sql(*m_database); QSqlQuery sql(m_database);
QString cmd = QString("UPDATE appInfo SET MODIFYED_TIME='%0', LOCK=%1 WHERE DESKTOP_FILE_PATH='%2'") 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(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"))
.arg(num) .arg(num)
.arg(desktopfp); .arg(desktopfp);
if (!sql.exec(cmd)) { if (!sql.exec(cmd)) {
qWarning() << "Set app favorites state failed!" << m_database->lastError(); qWarning() << "Set app favorites state failed!" << m_database.lastError();
res = false; res = false;
} }
if (!m_database->commit()) { if (!m_database.commit()) {
qWarning() << "Failed to commit !" << cmd; qWarning() << "Failed to commit !" << cmd;
m_database->rollback(); m_database.rollback();
res = false; res = false;
} }
} else { } else {
@ -169,38 +182,126 @@ bool AppInfoTablePrivate::setAppLockState(QString &desktopfp, size_t num)
return res; return res;
} }
bool AppInfoTablePrivate::changeFavoriteAppPos(const QString &desktopfp, size_t pos)
{
bool res(true);
if (m_database.transaction()) {
QSqlQuery sql(m_database);
QString cmd = QString("SELECT FAVORITES FROM appInfo WHERE DESKTOP_FILE_PATH = %0").arg(desktopfp);
//记录应用原位置
if (!sql.exec(cmd)) {
qWarning() << "Fail to change favorite-app pos, because: " << m_database.lastError() << " when exec :" << cmd;
res = false;
} else {
if (sql.next()) {
cmd = QString("UPDATE appInfo SET FAVORITES = FAVORITES + 1 WHRER FAVORITES > %1 AND FAVORITES < %2")
.arg(sql.value(0).toUInt())
.arg(pos);
} else {
qWarning() << "Fail to change favorite-app pos when exec next, because: " << m_database.lastError();
}
}
//更新范围内的应用的位置(原位置和新位置之间)
if (!sql.exec(cmd)) {
qWarning() << "Fail to change favorite-app pos, because: " << m_database.lastError() << " when exec :" << cmd;
res = false;
} else {
cmd = QString("UPDATE appInfo SET FAVORITES = %1 WHERE DESKTOP_FILE_PATH = %2")
.arg(pos)
.arg(desktopfp);
}
//更新应用位置
if (!sql.exec(cmd)) {
qWarning() << "Fail to change favorite-app pos, because: " << m_database.lastError() << " when exec :" << cmd;
res = false;
}
if (!m_database.commit()) {
qWarning() << "Failed to commit when exec: " << cmd;
m_database.rollback();
res = false;
}
} else {
qWarning() << "Failed to start transaction mode!!!";
res = false;
}
return res;
}
bool AppInfoTablePrivate::changeTopAppPos(const QString &desktopfp, size_t pos)
{
bool res(true);
if (m_database.transaction()) {
QSqlQuery sql(m_database);
QString cmd = QString("SELECT TOP FROM appInfo WHERE DESKTOP_FILE_PATH = %0").arg(desktopfp);
//记录应用原位置
if (!sql.exec(cmd)) {
qWarning() << "Fail to change top-app pos, because: " << m_database.lastError() << " when exec :" << cmd;
res = false;
} else {
if (sql.next()) {
cmd = QString("UPDATE appInfo SET TOP = TOP + 1 WHRER TOP > %1 AND TOP < %2")
.arg(sql.value(0).toUInt())
.arg(pos);
} else {
qWarning() << "Fail to change top-app pos when exec next, because: " << m_database.lastError();
}
}
//更新范围内的应用的位置(原位置和新位置之间)
if (!sql.exec(cmd)) {
qWarning() << "Fail to change top-app pos, because: " << m_database.lastError() << " when exec :" << cmd;
res = false;
} else {
cmd = QString("UPDATE appInfo SET TOP = %1 WHERE DESKTOP_FILE_PATH = %2")
.arg(pos)
.arg(desktopfp);
}
//更新应用位置
if (!sql.exec(cmd)) {
qWarning() << "Fail to change top-app pos, because: " << m_database.lastError() << " when exec :" << cmd;
res = false;
}
if (!m_database.commit()) {
qWarning() << "Failed to commit when exec: " << cmd;
m_database.rollback();
res = false;
}
} else {
qWarning() << "Failed to start transaction mode!!!";
res = false;
}
return res;
}
bool AppInfoTablePrivate::getAllAppDesktopList(QStringList &list) bool AppInfoTablePrivate::getAllAppDesktopList(QStringList &list)
{ {
bool res(true); bool res(true);
if (m_database->transaction()) { QSqlQuery sql(m_database);
QSqlQuery sql(*m_database); QString cmd = QString("SELECT DESKTOP_FILE_PATH FROM appInfo");
QString cmd = QString("SELECT DESKTOP_FILE_PATH FROM appInfo");
if (sql.exec(cmd)) { if (sql.exec(cmd)) {
while (sql.next()) { while (sql.next()) {
list.append(sql.value(0).toString()); list.append(sql.value(0).toString());
}
} else {
qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database->lastError();
res = false;
}
if (!m_database->commit()) {
qWarning() << "Failed to commit !" << cmd;
m_database->rollback();
res = false;
} }
} else { } else {
qWarning() << "Failed to start transaction mode!!!"; qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database.lastError();
res = false; res = false;
} }
return res; return res;
} }
bool AppInfoTablePrivate::getFavoritesAppList(QStringList &list) bool AppInfoTablePrivate::getFavoritesAppList(QStringList &list)
{ {
bool res(true); bool res(true);
if (m_database->transaction()) { if (m_database.transaction()) {
QSqlQuery sql(*m_database); QSqlQuery sql(m_database);
QSqlQuery sqlque(*m_database); QSqlQuery sqlque(m_database);
QString cmd = QString("SELECT DESKTOP_FILE_PATH FROM appInfo WHERE FAVORITES!=0 ORDER BY FAVORITES"); QString cmd = QString("SELECT DESKTOP_FILE_PATH FROM appInfo WHERE FAVORITES!=0 ORDER BY FAVORITES");
int count = 0; int count = 0;
if (sql.exec(cmd)) { if (sql.exec(cmd)) {
@ -210,18 +311,18 @@ bool AppInfoTablePrivate::getFavoritesAppList(QStringList &list)
.arg(++count) .arg(++count)
.arg(sql.value(0).toString()); .arg(sql.value(0).toString());
if (!sqlque.exec(cmd)) { if (!sqlque.exec(cmd)) {
qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database->lastError(); qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database.lastError();
res = false; res = false;
break; break;
} }
} }
} else { } else {
qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database->lastError(); qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database.lastError();
res = false; res = false;
} }
if (!m_database->commit()) { if (!m_database.commit()) {
qWarning() << "Failed to commit !" << cmd; qWarning() << "Failed to commit !" << cmd;
m_database->rollback(); m_database.rollback();
res = false; res = false;
} }
} else { } else {
@ -234,9 +335,9 @@ bool AppInfoTablePrivate::getFavoritesAppList(QStringList &list)
bool AppInfoTablePrivate::getTopAppList(QStringList &list) bool AppInfoTablePrivate::getTopAppList(QStringList &list)
{ {
bool res(true); bool res(true);
if (m_database->transaction()) { if (m_database.transaction()) {
QSqlQuery sql(*m_database); QSqlQuery sql(m_database);
QSqlQuery sqlque(*m_database); QSqlQuery sqlque(m_database);
QString cmd = QString("SELECT DESKTOP_FILE_PATH FROM appInfo WHERE TOP!=0 ORDER BY TOP"); QString cmd = QString("SELECT DESKTOP_FILE_PATH FROM appInfo WHERE TOP!=0 ORDER BY TOP");
int count = 0; int count = 0;
if (sql.exec(cmd)) { if (sql.exec(cmd)) {
@ -246,18 +347,18 @@ bool AppInfoTablePrivate::getTopAppList(QStringList &list)
.arg(++count) .arg(++count)
.arg(sql.value(0).toString()); .arg(sql.value(0).toString());
if (!sqlque.exec(cmd)) { if (!sqlque.exec(cmd)) {
qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database->lastError(); qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database.lastError();
res = false; res = false;
break; break;
} }
} }
} else { } else {
qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database->lastError(); qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database.lastError();
res = false; res = false;
} }
if (!m_database->commit()) { if (!m_database.commit()) {
qWarning() << "Failed to commit !" << cmd; qWarning() << "Failed to commit !" << cmd;
m_database->rollback(); m_database.rollback();
res = false; res = false;
} }
} else { } else {
@ -270,9 +371,9 @@ bool AppInfoTablePrivate::getTopAppList(QStringList &list)
bool AppInfoTablePrivate::getLaunchTimesAppList(QStringList &list) bool AppInfoTablePrivate::getLaunchTimesAppList(QStringList &list)
{ {
bool res(true); bool res(true);
if (m_database->transaction()) { if (m_database.transaction()) {
QSqlQuery sql(*m_database); QSqlQuery sql(m_database);
QSqlQuery sqlque(*m_database); QSqlQuery sqlque(m_database);
QString cmd = QString("SELECT DESKTOP_FILE_PATH FROM appInfo ORDER BY LAUNCH_TIMES"); QString cmd = QString("SELECT DESKTOP_FILE_PATH FROM appInfo ORDER BY LAUNCH_TIMES");
int count = 0; int count = 0;
if (sql.exec(cmd)) { if (sql.exec(cmd)) {
@ -282,18 +383,18 @@ bool AppInfoTablePrivate::getLaunchTimesAppList(QStringList &list)
.arg(++count) .arg(++count)
.arg(sql.value(0).toString()); .arg(sql.value(0).toString());
if (!sqlque.exec(cmd)) { if (!sqlque.exec(cmd)) {
qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database->lastError(); qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database.lastError();
res = false; res = false;
break; break;
} }
} }
} else { } else {
qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database->lastError(); qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database.lastError();
res = false; res = false;
} }
if (!m_database->commit()) { if (!m_database.commit()) {
qWarning() << "Failed to commit !" << cmd; qWarning() << "Failed to commit !" << cmd;
m_database->rollback(); m_database.rollback();
res = false; res = false;
} }
} else { } else {
@ -306,24 +407,24 @@ bool AppInfoTablePrivate::getLaunchTimesAppList(QStringList &list)
bool AppInfoTablePrivate::getAppCategory(QString &desktopfp, QString &category) bool AppInfoTablePrivate::getAppCategory(QString &desktopfp, QString &category)
{ {
bool res(true); bool res(true);
if (m_database->transaction()) { if (m_database.transaction()) {
QSqlQuery sql(*m_database); QSqlQuery sql(m_database);
QString cmd = QString("SELECT CATEGORY FROM appInfo WHERE DESKTOP_FILE_PATH='%0'") QString cmd = QString("SELECT CATEGORY FROM appInfo WHERE DESKTOP_FILE_PATH='%0'")
.arg(desktopfp); .arg(desktopfp);
if (sql.exec(cmd)) { if (sql.exec(cmd)) {
if (sql.next()) { if (sql.next()) {
category = sql.value(0).toString(); category = sql.value(0).toString();
} else { } else {
qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database->lastError(); qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database.lastError();
res = false; res = false;
} }
} else { } else {
qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database->lastError(); qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database.lastError();
res = false; res = false;
} }
if (!m_database->commit()) { if (!m_database.commit()) {
qWarning() << "Failed to commit !" << cmd; qWarning() << "Failed to commit !" << cmd;
m_database->rollback(); m_database.rollback();
res = false; res = false;
} }
} else { } else {
@ -333,15 +434,15 @@ bool AppInfoTablePrivate::getAppCategory(QString &desktopfp, QString &category)
return res; return res;
} }
bool AppInfoTablePrivate::getAppInfoResults(QVector<AppInfoTable::AppInfoResult> &appInfoResults) bool AppInfoTablePrivate::getAppInfoResults(QVector<AppInfoResult> &appInfoResults)
{ {
bool res(true); bool res(true);
if (m_database->transaction()) { if (m_database.transaction()) {
QSqlQuery sql(*m_database); QSqlQuery sql(m_database);
QString cmd = QString("SELECT DESKTOP_FILE_PATH,LOCAL_NAME,ICON,CATEGORY,TOP,FAVORITES,LAUNCH_TIMES,LOCK FROM appInfo"); QString cmd = QString("SELECT DESKTOP_FILE_PATH,LOCAL_NAME,ICON,CATEGORY,TOP,FAVORITES,LAUNCH_TIMES,LOCK,FIRST_LETTER_ALL FROM appInfo");
if (sql.exec(cmd)) { if (sql.exec(cmd)) {
while (sql.next()) { while (sql.next()) {
AppInfoTable::AppInfoResult result; AppInfoResult result;
result.desktopPath = sql.value(0).toString(); result.desktopPath = sql.value(0).toString();
result.appLocalName = sql.value(1).toString(); result.appLocalName = sql.value(1).toString();
result.iconName = sql.value(2).toString(); result.iconName = sql.value(2).toString();
@ -350,15 +451,16 @@ bool AppInfoTablePrivate::getAppInfoResults(QVector<AppInfoTable::AppInfoResult>
result.favorate = sql.value(5).toInt(); result.favorate = sql.value(5).toInt();
result.launchTimes = sql.value(6).toInt(); result.launchTimes = sql.value(6).toInt();
result.lock = sql.value(7).toInt(); result.lock = sql.value(7).toInt();
result.firstLetter = sql.value(8).toString();
appInfoResults.append(std::move(result)); appInfoResults.append(std::move(result));
} }
} else { } else {
qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database->lastError(); qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database.lastError();
res = false; res = false;
} }
if (!m_database->commit()) { if (!m_database.commit()) {
qWarning() << "Failed to commit !" << cmd; qWarning() << "Failed to commit !" << cmd;
m_database->rollback(); m_database.rollback();
res = false; res = false;
} }
} else { } else {
@ -371,24 +473,24 @@ bool AppInfoTablePrivate::getAppInfoResults(QVector<AppInfoTable::AppInfoResult>
bool AppInfoTablePrivate::getAppLockState(QString &desktopfp, size_t &num) bool AppInfoTablePrivate::getAppLockState(QString &desktopfp, size_t &num)
{ {
bool res(true); bool res(true);
if (m_database->transaction()) { if (m_database.transaction()) {
QSqlQuery sql(*m_database); QSqlQuery sql(m_database);
QString cmd = QString("SELECT LOCK FROM appInfo WHERE DESKTOP_FILE_PATH='%0'") QString cmd = QString("SELECT LOCK FROM appInfo WHERE DESKTOP_FILE_PATH='%0'")
.arg(desktopfp); .arg(desktopfp);
if (sql.exec(cmd)) { if (sql.exec(cmd)) {
if (sql.next()) { if (sql.next()) {
num = sql.value(0).toInt(); num = sql.value(0).toInt();
} else { } else {
qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database->lastError(); qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database.lastError();
res = false; res = false;
} }
} else { } else {
qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database->lastError(); qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database.lastError();
res = false; res = false;
} }
if (!m_database->commit()) { if (!m_database.commit()) {
qWarning() << "Failed to commit !" << cmd; qWarning() << "Failed to commit !" << cmd;
m_database->rollback(); m_database.rollback();
res = false; res = false;
} }
} else { } else {
@ -401,24 +503,24 @@ bool AppInfoTablePrivate::getAppLockState(QString &desktopfp, size_t &num)
bool AppInfoTablePrivate::getAppTopState(QString &desktopfp, size_t &num) bool AppInfoTablePrivate::getAppTopState(QString &desktopfp, size_t &num)
{ {
bool res(true); bool res(true);
if (m_database->transaction()) { if (m_database.transaction()) {
QSqlQuery sql(*m_database); QSqlQuery sql(m_database);
QString cmd = QString("SELECT TOP FROM appInfo WHERE DESKTOP_FILE_PATH='%0'") QString cmd = QString("SELECT TOP FROM appInfo WHERE DESKTOP_FILE_PATH='%0'")
.arg(desktopfp); .arg(desktopfp);
if (sql.exec(cmd)) { if (sql.exec(cmd)) {
if (sql.next()) { if (sql.next()) {
num = sql.value(0).toInt(); num = sql.value(0).toInt();
} else { } else {
qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database->lastError(); qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database.lastError();
res = false; res = false;
} }
} else { } else {
qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database->lastError(); qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database.lastError();
res = false; res = false;
} }
if (!m_database->commit()) { if (!m_database.commit()) {
qWarning() << "Failed to commit !" << cmd; qWarning() << "Failed to commit !" << cmd;
m_database->rollback(); m_database.rollback();
res = false; res = false;
} }
} else { } else {
@ -431,24 +533,24 @@ bool AppInfoTablePrivate::getAppTopState(QString &desktopfp, size_t &num)
bool AppInfoTablePrivate::getAppLaunchedState(QString &desktopfp, size_t &num) bool AppInfoTablePrivate::getAppLaunchedState(QString &desktopfp, size_t &num)
{ {
bool res(true); bool res(true);
if (m_database->transaction()) { if (m_database.transaction()) {
QSqlQuery sql(*m_database); QSqlQuery sql(m_database);
QString cmd = QString("SELECT LAUNCHED FROM appInfo WHERE DESKTOP_FILE_PATH='%0'") QString cmd = QString("SELECT LAUNCHED FROM appInfo WHERE DESKTOP_FILE_PATH='%0'")
.arg(desktopfp); .arg(desktopfp);
if (sql.exec(cmd)) { if (sql.exec(cmd)) {
if (sql.next()) { if (sql.next()) {
num = sql.value(0).toInt(); num = sql.value(0).toInt();
} else { } else {
qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database->lastError(); qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database.lastError();
res = false; res = false;
} }
} else { } else {
qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database->lastError(); qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database.lastError();
res = false; res = false;
} }
if (!m_database->commit()) { if (!m_database.commit()) {
qWarning() << "Failed to commit !" << cmd; qWarning() << "Failed to commit !" << cmd;
m_database->rollback(); m_database.rollback();
res = false; res = false;
} }
} else { } else {
@ -461,24 +563,24 @@ bool AppInfoTablePrivate::getAppLaunchedState(QString &desktopfp, size_t &num)
bool AppInfoTablePrivate::getAppFavoriteState(QString &desktopfp, size_t &num) bool AppInfoTablePrivate::getAppFavoriteState(QString &desktopfp, size_t &num)
{ {
bool res(true); bool res(true);
if (m_database->transaction()) { if (m_database.transaction()) {
QSqlQuery sql(*m_database); QSqlQuery sql(m_database);
QString cmd = QString("SELECT FAVORITES FROM appInfo WHERE DESKTOP_FILE_PATH='%0'") QString cmd = QString("SELECT FAVORITES FROM appInfo WHERE DESKTOP_FILE_PATH='%0'")
.arg(desktopfp); .arg(desktopfp);
if (sql.exec(cmd)) { if (sql.exec(cmd)) {
if (sql.next()) { if (sql.next()) {
num = sql.value(0).toInt(); num = sql.value(0).toInt();
} else { } else {
qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database->lastError(); qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database.lastError();
res = false; res = false;
} }
} else { } else {
qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database->lastError(); qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database.lastError();
res = false; res = false;
} }
if (!m_database->commit()) { if (!m_database.commit()) {
qWarning() << "Failed to commit !" << cmd; qWarning() << "Failed to commit !" << cmd;
m_database->rollback(); m_database.rollback();
res = false; res = false;
} }
} else { } else {
@ -534,8 +636,8 @@ bool AppInfoTablePrivate::addAppShortcut2Panel(QString &desktopfp)
bool AppInfoTablePrivate::searchInstallApp(QString &keyWord, QStringList &installAppInfoRes) bool AppInfoTablePrivate::searchInstallApp(QString &keyWord, QStringList &installAppInfoRes)
{ {
bool res(true); bool res(true);
if (m_database->transaction()) { if (m_database.transaction()) {
QSqlQuery sql(*m_database); QSqlQuery sql(m_database);
QString cmd; QString cmd;
if (keyWord.size() < 2) { if (keyWord.size() < 2) {
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") 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")
@ -550,12 +652,12 @@ bool AppInfoTablePrivate::searchInstallApp(QString &keyWord, QStringList &instal
installAppInfoRes << sql.value(0).toString() << sql.value(1).toString() << sql.value(2).toString(); installAppInfoRes << sql.value(0).toString() << sql.value(1).toString() << sql.value(2).toString();
} }
} else { } else {
qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database->lastError(); qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database.lastError();
res = false; res = false;
} }
if (!m_database->commit()) { if (!m_database.commit()) {
qWarning() << "Failed to commit !" << cmd; qWarning() << "Failed to commit !" << cmd;
m_database->rollback(); m_database.rollback();
res = false; res = false;
} }
} else { } else {
@ -568,8 +670,8 @@ bool AppInfoTablePrivate::searchInstallApp(QString &keyWord, QStringList &instal
bool AppInfoTablePrivate::searchInstallApp(QStringList &keyWord, QStringList &installAppInfoRes) bool AppInfoTablePrivate::searchInstallApp(QStringList &keyWord, QStringList &installAppInfoRes)
{ {
bool res(true); bool res(true);
if (m_database->transaction() or keyWord.size() != 0) { if (m_database.transaction() or keyWord.size() != 0) {
QSqlQuery sql(*m_database); QSqlQuery sql(m_database);
QString cmd; QString cmd;
if (keyWord.at(0).size() < 2) { if (keyWord.at(0).size() < 2) {
cmd = QString("SELECT DESKTOP_FILE_PATH,LOCAL_NAME,ICON,NAME_EN,NAME_ZH,FIRST_LETTER_OF_PINYIN FROM appInfo" cmd = QString("SELECT DESKTOP_FILE_PATH,LOCAL_NAME,ICON,NAME_EN,NAME_ZH,FIRST_LETTER_OF_PINYIN FROM appInfo"
@ -595,12 +697,12 @@ bool AppInfoTablePrivate::searchInstallApp(QStringList &keyWord, QStringList &in
installAppInfoRes << sql.value(0).toString() << sql.value(1).toString() << sql.value(2).toString(); installAppInfoRes << sql.value(0).toString() << sql.value(1).toString() << sql.value(2).toString();
} }
} else { } else {
qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database->lastError(); qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database.lastError();
res = false; res = false;
} }
if (!m_database->commit()) { if (!m_database.commit()) {
qWarning() << "Failed to commit !" << cmd; qWarning() << "Failed to commit !" << cmd;
m_database->rollback(); m_database.rollback();
res = false; res = false;
} }
} else { } else {
@ -648,7 +750,7 @@ bool AppInfoTablePrivate::uninstallApp(QString &desktopfp)
QString AppInfoTablePrivate::lastError() const QString AppInfoTablePrivate::lastError() const
{ {
return m_database->lastError().text(); return m_database.lastError().text();
} }
AppInfoTablePrivate::~AppInfoTablePrivate() AppInfoTablePrivate::~AppInfoTablePrivate()
@ -658,9 +760,9 @@ AppInfoTablePrivate::~AppInfoTablePrivate()
bool AppInfoTablePrivate::initDateBaseConnection() bool AppInfoTablePrivate::initDateBaseConnection()
{ {
m_database->setDatabaseName(APP_DATABASE_PATH + APP_DATABASE_NAME); m_database.setDatabaseName(APP_DATABASE_PATH + APP_DATABASE_NAME);
if(!m_database->open()) { if(!m_database.open()) {
qWarning() << m_database->lastError(); qWarning() << m_database.lastError();
return false; return false;
// QApplication::quit(); // QApplication::quit();
} }
@ -669,10 +771,10 @@ bool AppInfoTablePrivate::initDateBaseConnection()
bool AppInfoTablePrivate::openDataBase() bool AppInfoTablePrivate::openDataBase()
{ {
*m_database = QSqlDatabase::addDatabase("QSQLITE", m_ConnectionName); m_database = QSqlDatabase::addDatabase("QSQLITE", m_ConnectionName);
m_database->setDatabaseName(APP_DATABASE_PATH + APP_DATABASE_NAME); m_database.setDatabaseName(APP_DATABASE_PATH + APP_DATABASE_NAME);
if(!m_database->open()) { if(!m_database.open()) {
return false; return false;
} }
return true; return true;
@ -680,11 +782,26 @@ bool AppInfoTablePrivate::openDataBase()
void AppInfoTablePrivate::closeDataBase() void AppInfoTablePrivate::closeDataBase()
{ {
m_database->close(); m_database.close();
delete m_database; // delete m_database;
QSqlDatabase::removeDatabase(m_ConnectionName); QSqlDatabase::removeDatabase(m_ConnectionName);
} }
void AppInfoTablePrivate::sendAppDBItemsUpdate(QVector<AppInfoResult> results)
{
Q_EMIT q->appDBItems2BUpdate(results);
}
void AppInfoTablePrivate::sendAppDBItemsAdd(QVector<AppInfoResult> results)
{
Q_EMIT q->appDBItems2BAdd(results);
}
void AppInfoTablePrivate::sendAppDBItemsDelete(QStringList desktopfps)
{
Q_EMIT q->appDBItems2BDelete(desktopfps);
}
AppInfoTable::AppInfoTable(QObject *parent) : QObject(parent), d(new AppInfoTablePrivate(this)) AppInfoTable::AppInfoTable(QObject *parent) : QObject(parent), d(new AppInfoTablePrivate(this))
{ {
} }
@ -739,7 +856,17 @@ bool AppInfoTable::getAppCategory(QString &desktopfp, QString &category)
return d->getAppCategory(desktopfp, category); return d->getAppCategory(desktopfp, category);
} }
bool AppInfoTable::getAppInfoResults(QVector<AppInfoTable::AppInfoResult> &appInfoResults) bool AppInfoTable::changeFavoriteAppPos(const QString &desktopfp, size_t pos)
{
return d->changeFavoriteAppPos(desktopfp, pos);
}
bool AppInfoTable::changeTopAppPos(const QString &desktopfp, size_t pos)
{
return d->changeTopAppPos(desktopfp, pos);
}
bool AppInfoTable::getAppInfoResults(QVector<AppInfoResult> &appInfoResults)
{ {
return d->getAppInfoResults(appInfoResults); return d->getAppInfoResults(appInfoResults);
} }

View File

@ -2,54 +2,89 @@
#define APPINFOTABLE_H #define APPINFOTABLE_H
#include <QObject> #include <QObject>
#include "app-db-common.h"
namespace UkuiSearch { namespace UkuiSearch {
class AppInfoTablePrivate; class AppInfoTablePrivate;
class AppInfoTable : public QObject class AppInfoTable : public QObject
{ {
Q_OBJECT Q_OBJECT
public:
typedef struct appInfoResult
{
appInfoResult() {
top = 0;
favorate = 0;
launchTimes = 0;
lock = 0;
}
QString desktopPath;
QString iconName;
QString appLocalName;
QString firstLetter;
QString category;
size_t top;
size_t favorate;
size_t launchTimes;
size_t lock;
} AppInfoResult;
public: public:
explicit AppInfoTable(QObject *parent = nullptr); explicit AppInfoTable(QObject *parent = nullptr);
AppInfoTable(AppInfoTable &) = delete; AppInfoTable(AppInfoTable &) = delete;
AppInfoTable &operator =(const AppInfoTable &) = delete; AppInfoTable &operator =(const AppInfoTable &) = delete;
/**
* @brief AppInfoTable::setAppFavoritesState
* set the favorites state of the app
* @param desktopfp: the desktop file path of app
* @param num: the favorites app's order(from 1)
* @return bool: true if success, else false
*/
bool setAppFavoritesState(QString &desktopfp, size_t num); bool setAppFavoritesState(QString &desktopfp, size_t num);
/**
* @brief AppInfoTable::setAppTopState
* set the top state of the app
* @param desktopfp: the desktop file path of app
* @param num: the top app's order(from 1)
* @return bool: true if success, else false
*/
bool setAppTopState(QString &desktopfp, size_t num); bool setAppTopState(QString &desktopfp, size_t num);
/**
* @brief AppInfoTable::getAllAppDesktopList
* get the desktop file paths of all apps
* @param list: a stringlist include all paths
* @return bool: true if success, else false
*/
bool getAllAppDesktopList(QStringList &list); bool getAllAppDesktopList(QStringList &list);
/**
* @brief AppInfoTable::getFavoritesAppList
* get all favorites apps
* @param list: a stringlist include all apps' paths
* @return bool: true if success, else false
*/
bool getFavoritesAppList(QStringList &list); bool getFavoritesAppList(QStringList &list);
/**
* @brief AppInfoTable::getTopAppList
* get all top apps
* @param list: a stringlist include all apps' paths
* @return bool: true if success, else false
*/
bool getTopAppList(QStringList &list); bool getTopAppList(QStringList &list);
bool getLaunchTimesAppList(QStringList &list); bool getLaunchTimesAppList(QStringList &list);
bool getAppCategory(QString &desktopfp, QString &category); bool getAppCategory(QString &desktopfp, QString &category);
/** /**
* @brief getAppInfoResults * @brief AppInfoTable::changeFavoriteAppPos
* @param appInfoResults * change the position of the app which is one of the Favorites Apps
* @return * @param desktopfp: desktop file path of app
* @param pos: the position which the app will be changed into
* @return bool: true if success, else false
*/ */
bool getAppInfoResults(QVector<AppInfoTable::AppInfoResult> &appInfoResults); bool changeFavoriteAppPos(const QString &desktopfp, size_t pos);
/**
* @brief AppInfoTable::changeTopAppPos
* hange the position of the app which is one of the Top Apps
* @param desktopfp: desktop file path of app
* @param pos: the position which the app will be changed into
* @return bool: true if success, else false
*/
bool changeTopAppPos(const QString &desktopfp, size_t pos);
/**
* @brief AppInfoTable::getAppInfoResults
* Get all App infos by passing AppInforesult Vector.
* @param AppInfoResults: a struct which includes all infos of each application
* @return bool: true if success, else false
*/
bool getAppInfoResults(QVector<AppInfoResult> &appInfoResults);
bool getAppLockState(QString &desktopfp, size_t &num); bool getAppLockState(QString &desktopfp, size_t &num);
bool getAppTopState(QString &desktopfp, size_t &num); bool getAppTopState(QString &desktopfp, size_t &num);
@ -65,8 +100,9 @@ public:
bool uninstallApp(QString &desktopfp); bool uninstallApp(QString &desktopfp);
/** /**
* @brief lastError * @brief AppInfoTable::lastError
* @return * the last error of the database
* @return QString: the text of the last error
*/ */
QString lastError(void) const; QString lastError(void) const;
@ -76,9 +112,12 @@ private:
bool updateAppLaunchTimes(QString &desktopfp); bool updateAppLaunchTimes(QString &desktopfp);
AppInfoTablePrivate *d; AppInfoTablePrivate *d;
Q_SIGNALS: Q_SIGNALS:
void DBOpenFailed(); void DBOpenFailed();
void appDBItems2BUpdate(QVector<AppInfoResult>);
void appDBItems2BAdd(QVector<AppInfoResult>);
void appDBItems2BDelete(QStringList);
}; };
} }

View File

@ -50,6 +50,7 @@ SOURCES += \
libsearch.cpp libsearch.cpp
HEADERS += \ HEADERS += \
app-db-common.h \
common.h \ common.h \
file-utils.h \ file-utils.h \
global-settings.h \ global-settings.h \

View File

@ -1,11 +0,0 @@
#ifndef APPDBCOMMONDEFINES_H
#define APPDBCOMMONDEFINES_H
#include <QDir>
namespace UkuiSearch {
#define APP_DATABASE_PATH QDir::homePath()+"/.config/org.ukui/ukui-search/appdata/"
#define APP_DATABASE_NAME "app-info.db"
}
#endif // APPDBCOMMONDEFINES_H

View File

@ -1,10 +1,11 @@
#include "app-db-manager.h"
#include "file-utils.h"
#include "convert-winid-to-desktop.h"
#include <qt5xdg/XdgDesktopFile> #include <qt5xdg/XdgDesktopFile>
#include <QMutexLocker> #include <QMutexLocker>
#include <QCryptographicHash> #include <QCryptographicHash>
#include <QFile> #include <QFile>
#include "app-db-manager.h"
#include "file-utils.h"
#include "convert-winid-to-desktop.h"
#define GENERAL_APP_DESKTOP_PATH "/usr/share/applications/" #define GENERAL_APP_DESKTOP_PATH "/usr/share/applications/"
#define ANDROID_APP_DESKTOP_PATH QDir::homePath() + "/.local/share/applications/" #define ANDROID_APP_DESKTOP_PATH QDir::homePath() + "/.local/share/applications/"
@ -71,6 +72,8 @@ AppDBManager::AppDBManager(QObject *parent) : QObject(parent), m_database(QSqlDa
if (!m_database.commit()) { if (!m_database.commit()) {
qWarning() << "Failed to commit !"; qWarning() << "Failed to commit !";
m_database.rollback(); m_database.rollback();
} else {
Q_EMIT this->finishHandleAppDB();
} }
} else { } else {
qWarning() << "Failed to start transaction mode!!!"; qWarning() << "Failed to start transaction mode!!!";
@ -111,7 +114,7 @@ void AppDBManager::buildAppInfoDB()
.arg("NAME_ZH TEXT")//应用中文名称 .arg("NAME_ZH TEXT")//应用中文名称
.arg("PINYIN_NAME TEXT")//中文拼音 .arg("PINYIN_NAME TEXT")//中文拼音
.arg("FIRST_LETTER_OF_PINYIN TEXT")//中文拼音首字母 .arg("FIRST_LETTER_OF_PINYIN TEXT")//中文拼音首字母
.arg("FIRST_LETTER_ALL")//中英文首字母(只包含一个,目前方案取拼音首字母的第一项,由于模糊拼音算法可能出问题) .arg("FIRST_LETTER_ALL")//拼音和英文全拼
.arg("ICON TEXT")//图标名称(或路径) .arg("ICON TEXT")//图标名称(或路径)
.arg("TYPE TEXT")//应用类型 .arg("TYPE TEXT")//应用类型
.arg("CATEGORY TEXT")//应用分类 .arg("CATEGORY TEXT")//应用分类
@ -406,6 +409,10 @@ bool AppDBManager::updateLocaleData2DB(QString desktopPath)
res = false; res = false;
} }
if (res) { if (res) {
AppInfoResult result;
result.appLocalName = localName;
result.firstLetter = firstLetter2All;
Q_EMIT this->appDBItemUpdate(result);
qDebug() << "Already to update the locale app-data of " << desktopPath; qDebug() << "Already to update the locale app-data of " << desktopPath;
} else { } else {
qDebug() << "Fail to update the locale app-data of " << desktopPath; qDebug() << "Fail to update the locale app-data of " << desktopPath;
@ -421,6 +428,8 @@ void AppDBManager::initDataBase()
if (!m_database.commit()) { if (!m_database.commit()) {
qWarning() << "Failed to commit !"; qWarning() << "Failed to commit !";
m_database.rollback(); m_database.rollback();
} else {
Q_EMIT this->finishHandleAppDB();
} }
} else { } else {
qWarning() << "Failed to start transaction mode!!!"; qWarning() << "Failed to start transaction mode!!!";
@ -482,11 +491,11 @@ bool AppDBManager::addAppDesktopFile2DB(QString &desktopfd)
desktopfile.load(desktopfd); desktopfile.load(desktopfd);
QString hanzi, pinyin, firstLetterOfPinyin; QString hanzi, pinyin, firstLetterOfPinyin;
QString localName = desktopfile.localizedValue("Name", "NULL").toString(); QString localName = desktopfile.localizedValue("Name", "NULL").toString();
QString firstLtter2All = localName; QString firstLetter2All = localName;
bool isHanzi = true; bool isHanzi = true;
if (localName.contains(QRegExp("[\\x4e00-\\x9fa5]+"))) { if (localName.contains(QRegExp("[\\x4e00-\\x9fa5]+"))) {
firstLtter2All = FileUtils::findMultiToneWords(localName).at(0); firstLetter2All = FileUtils::findMultiToneWords(localName).at(0);
} }
if (desktopfile.contains("Name[zh_CN]")) { if (desktopfile.contains("Name[zh_CN]")) {
@ -524,7 +533,7 @@ bool AppDBManager::addAppDesktopFile2DB(QString &desktopfd)
.arg(hanzi) .arg(hanzi)
.arg(pinyin) .arg(pinyin)
.arg(firstLetterOfPinyin) .arg(firstLetterOfPinyin)
.arg(firstLtter2All) .arg(firstLetter2All)
.arg(desktopfile.value("Icon").toString()) .arg(desktopfile.value("Icon").toString())
.arg(desktopfile.value("Type").toString()) .arg(desktopfile.value("Type").toString())
.arg(desktopfile.value("Categories").toString()) .arg(desktopfile.value("Categories").toString())
@ -541,6 +550,13 @@ bool AppDBManager::addAppDesktopFile2DB(QString &desktopfd)
res = false; res = false;
} }
if (res) { if (res) {
AppInfoResult result;
result.desktopPath = desktopfd;
result.iconName = desktopfile.value("Icon").toString();
result.appLocalName = localName;
result.firstLetter = firstLetter2All;
result.category = desktopfile.value("Categories").toString();
Q_EMIT this->appDBItemAdd(result);
qDebug() << "app database add " << desktopfd << "success!"; qDebug() << "app database add " << desktopfd << "success!";
} else { } else {
qDebug() << "app database add " << desktopfd << "failed!"; qDebug() << "app database add " << desktopfd << "failed!";
@ -558,6 +574,7 @@ bool AppDBManager::deleteAppDesktopFile2DB(const QString &desktopfd)
res = false; res = false;
} }
if (res) { if (res) {
Q_EMIT this->appDBItemDelete(desktopfd);
qDebug() << "app database delete " << desktopfd << "success!"; qDebug() << "app database delete " << desktopfd << "success!";
} else { } else {
qDebug() << "app database delete " << desktopfd << "failed!"; qDebug() << "app database delete " << desktopfd << "failed!";
@ -612,7 +629,7 @@ bool AppDBManager::updateAppDesktopFile2DB(QString &desktopfd)
"NAME_ZH='%3'" "NAME_ZH='%3'"
",PINYIN_NAME='%4'," ",PINYIN_NAME='%4',"
"FIRST_LETTER_OF_PINYIN='%5'," "FIRST_LETTER_OF_PINYIN='%5',"
"FIRST_LETTER_ALL='%6'" "FIRST_LETTER_ALL='%6',"
"ICON='%7'," "ICON='%7',"
"TYPE='%8'," "TYPE='%8',"
"CATEGORY='%9'," "CATEGORY='%9',"
@ -639,6 +656,13 @@ bool AppDBManager::updateAppDesktopFile2DB(QString &desktopfd)
res = false; res = false;
} }
if (res) { if (res) {
AppInfoResult result;
result.desktopPath = desktopfd;
result.iconName = desktopfile.value("Icon").toString();
result.appLocalName = localName;
result.firstLetter = firstLetter2All;
result.category = desktopfile.value("Categories").toString();
Q_EMIT this->appDBItemUpdate(result);
qDebug() << "app database update " << desktopfd << "success!"; qDebug() << "app database update " << desktopfd << "success!";
} else { } else {
qDebug() << "app database update " << desktopfd << "failed!"; qDebug() << "app database update " << desktopfd << "failed!";
@ -654,14 +678,19 @@ bool AppDBManager::updateAppLaunchTimes(QString &desktopfp)
QString cmd = QString("SELECT LAUNCH_TIMES FROM appInfo WHERE DESKTOP_FILE_PATH='%1'").arg(desktopfp); QString cmd = QString("SELECT LAUNCH_TIMES FROM appInfo WHERE DESKTOP_FILE_PATH='%1'").arg(desktopfp);
if (sql.exec(cmd)) { if (sql.exec(cmd)) {
if (sql.next()) { if (sql.next()) {
int launchTimes = sql.value(0).toInt() + 1;
cmd = QString("UPDATE appInfo SET MODIFYED_TIME='%0', LAUNCH_TIMES=%1, LAUNCHED=%2 WHERE DESKTOP_FILE_PATH='%3'") 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(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"))
.arg(sql.value(0).toInt() + 1) .arg(launchTimes)
.arg(1) .arg(1)
.arg(desktopfp); .arg(desktopfp);
if (!sql.exec(cmd)) { if (!sql.exec(cmd)) {
qWarning() << "Set app favorites state failed!" << m_database.lastError(); qWarning() << "Set app favorites state failed!" << m_database.lastError();
res = false; res = false;
} else {
AppInfoResult result;
result.launchTimes = launchTimes;
Q_EMIT this->appDBItemUpdate(result);
} }
} else { } else {
qWarning() << "Failed to exec next!" << cmd; qWarning() << "Failed to exec next!" << cmd;
@ -675,6 +704,8 @@ bool AppDBManager::updateAppLaunchTimes(QString &desktopfp)
qWarning() << "Failed to commit !" << cmd; qWarning() << "Failed to commit !" << cmd;
m_database.rollback(); m_database.rollback();
res = false; res = false;
} else {
Q_EMIT this->finishHandleAppDB();
} }
} else { } else {
qWarning() << "Failed to start transaction mode!!!"; qWarning() << "Failed to start transaction mode!!!";

View File

@ -1,6 +1,8 @@
#ifndef APPDBMANAGER_H #ifndef APPDBMANAGER_H
#define APPDBMANAGER_H #define APPDBMANAGER_H
#include "app-db-common.h"
#include <QDir> #include <QDir>
#include <QObject> #include <QObject>
#include <QSqlDatabase> #include <QSqlDatabase>
@ -12,7 +14,6 @@
#include <QFileSystemWatcher> #include <QFileSystemWatcher>
#include <QMutex> #include <QMutex>
#include <QSettings> #include <QSettings>
#include "app-db-common-defines.h"
#define CONNECTION_NAME QLatin1String("ukss-appdb-connection") #define CONNECTION_NAME QLatin1String("ukss-appdb-connection")
@ -130,7 +131,11 @@ private:
//原本额外排除的目录,不知道额外的原因,有可能之后有问题--bjj20220621 //原本额外排除的目录,不知道额外的原因,有可能之后有问题--bjj20220621
"/usr/share/applications/screensavers" "/usr/share/applications/screensavers"
}; };
Q_SIGNALS:
void appDBItemUpdate(const AppInfoResult&);
void appDBItemAdd(const AppInfoResult&);
void appDBItemDelete(const QString&);
void finishHandleAppDB();
}; };
} }

View File

@ -0,0 +1,47 @@
#include "signal-transformer.h"
QMutex SignalTransformer::s_mutex;
SignalTransformer *SignalTransformer::getTransformer()
{
static SignalTransformer *instance = new SignalTransformer();
return instance;
}
void SignalTransformer::handleItemInsert(const AppInfoResult &item)
{
QMutexLocker locker(&s_mutex);
m_items2BInsert.append(item);
}
void SignalTransformer::handleItemUpdate(const AppInfoResult &item)
{
QMutexLocker locker(&s_mutex);
m_items2BUpdate.append(item);
}
void SignalTransformer::handleItemDelete(const QString &desktopfp)
{
QMutexLocker locker(&s_mutex);
m_items2BDelete.append(desktopfp);
}
void SignalTransformer::handleSignalTransform()
{
QMutexLocker locker(&s_mutex);
Q_EMIT this->appDBItemsAdd(m_items2BInsert);
Q_EMIT this->appDBItemsUpdate(m_items2BUpdate);
Q_EMIT this->appDBItemsDelete(m_items2BDelete);
m_items2BInsert.clear();
m_items2BUpdate.clear();
m_items2BDelete.clear();
}
SignalTransformer::SignalTransformer(QObject *parent) : QObject(parent)
{
connect(AppDBManager::getInstance(), &AppDBManager::appDBItemAdd, this, &SignalTransformer::handleItemInsert);
connect(AppDBManager::getInstance(), &AppDBManager::appDBItemUpdate, this, &SignalTransformer::handleItemUpdate);
connect(AppDBManager::getInstance(), &AppDBManager::appDBItemDelete, this, &SignalTransformer::handleItemDelete);
connect(AppDBManager::getInstance(), &AppDBManager::finishHandleAppDB, this, &SignalTransformer::handleSignalTransform);
}

View File

@ -0,0 +1,43 @@
#ifndef SIGNALTRANSFORMER_H
#define SIGNALTRANSFORMER_H
#include <QObject>
#include <QDBusInterface>
#include <QMutexLocker>
#include "app-db-manager.h"
using namespace UkuiSearch;
class SignalTransformer : public QObject
{
Q_CLASSINFO("D-Bus Interface","org.ukui.search.appdb")
Q_OBJECT
public:
static SignalTransformer *getTransformer();
static QMutex s_mutex;
public Q_SLOTS:
void handleItemInsert(const AppInfoResult &item);
void handleItemUpdate(const AppInfoResult &item);
void handleItemDelete(const QString &desktopfp);
void handleSignalTransform();
private:
SignalTransformer(QObject *parent = nullptr);
SignalTransformer(const SignalTransformer &) = delete;
SignalTransformer& operator = (const SignalTransformer&) = delete;
QVector<AppInfoResult> m_items2BUpdate;
QVector<AppInfoResult> m_items2BInsert;
QStringList m_items2BDelete;
Q_SIGNALS:
void appDBItemsUpdate(QVector<AppInfoResult>);
void appDBItemsAdd(QVector<AppInfoResult>);
void appDBItemsDelete(QStringList);
};
#endif // SIGNALTRANSFORMER_H

View File

@ -1,6 +1,11 @@
#include <QDebug>
#include "ukui-search-app-data-service.h" #include "ukui-search-app-data-service.h"
#include "app-db-manager.h" #include "app-db-manager.h"
#include "signal-transformer.h"
#include <QDBusConnection>
#include <QDBusMetaType>
#include <QDBusError>
#include <QDebug>
using namespace UkuiSearch; using namespace UkuiSearch;
@ -12,9 +17,24 @@ UkuiSearchAppDataService::UkuiSearchAppDataService(int &argc, char *argv[], cons
setQuitOnLastWindowClosed(false); setQuitOnLastWindowClosed(false);
if (!this->isRunning()) { if (!this->isRunning()) {
qDebug() << "First running"; qDebug() << "First running, I'm in app-db manager dbus rigister.";
qRegisterMetaType<AppInfoResult>("AppInfoResult");
qRegisterMetaType<QVector<AppInfoResult>>("QVector<AppInfoResult>");
qDBusRegisterMetaType<AppInfoResult>();
qDBusRegisterMetaType<QVector<AppInfoResult>>();
AppDBManager::getInstance(); AppDBManager::getInstance();
QDBusConnection sessionBus = QDBusConnection::sessionBus();
if (!sessionBus.registerService("com.ukui.search.appdb.service")) {
qCritical() << "QDbus register service failed reason:" << sessionBus.lastError();
}
if(!sessionBus.registerObject("/org/ukui/search/appDataBase", SignalTransformer::getTransformer(), QDBusConnection::ExportAllSignals)) {
qCritical() << "ukui-search-fileindex dbus register object failed reason:" << sessionBus.lastError();
}
connect(this, &QtSingleApplication::messageReceived, [ & ](QString msg) { connect(this, &QtSingleApplication::messageReceived, [ & ](QString msg) {
this->parseCmd(msg, true); this->parseCmd(msg, true);
}); });

View File

@ -29,13 +29,14 @@ SOURCES += \
main.cpp \ main.cpp \
convert-winid-to-desktop.cpp \ convert-winid-to-desktop.cpp \
app-db-manager.cpp \ app-db-manager.cpp \
signal-transformer.cpp \
ukui-search-app-data-service.cpp \ ukui-search-app-data-service.cpp \
HEADERS += \ HEADERS += \
convert-winid-to-desktop.h \ convert-winid-to-desktop.h \
app-db-manager.h \ app-db-manager.h \
signal-transformer.h \
ukui-search-app-data-service.h \ ukui-search-app-data-service.h \
app-db-common-defines.h \
target.path = /usr/bin target.path = /usr/bin
INSTALLS += target INSTALLS += target