diff --git a/libsearch/app-db-common.h b/libsearch/app-db-common.h index c8ef2dd..fba443b 100644 --- a/libsearch/app-db-common.h +++ b/libsearch/app-db-common.h @@ -18,16 +18,16 @@ struct AppInfoResult QString firstLetter; QString category; int top; - int favorate; + int favorite; int launchTimes; int lock; - AppInfoResult() : top(0), favorate(0), launchTimes(0), lock(0) {} + AppInfoResult() : top(0), favorite(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; + << infoResult.category << infoResult.top << infoResult.favorite << infoResult.launchTimes << infoResult.lock; argument.endStructure(); return argument; } @@ -36,7 +36,7 @@ struct AppInfoResult { argument.beginStructure(); argument >> infoResult.desktopPath >> infoResult.iconName >> infoResult.appLocalName >> infoResult.firstLetter - >> infoResult.category >> infoResult.top >> infoResult.favorate >> infoResult.launchTimes >> infoResult.lock; + >> infoResult.category >> infoResult.top >> infoResult.favorite >> infoResult.launchTimes >> infoResult.lock; argument.endStructure(); return argument; diff --git a/libsearch/appdata/app-info-table-private.h b/libsearch/appdata/app-info-table-private.h index c0ef60d..6449993 100644 --- a/libsearch/appdata/app-info-table-private.h +++ b/libsearch/appdata/app-info-table-private.h @@ -19,21 +19,10 @@ public: bool setAppFavoritesState(QString &desktopfp, size_t num); bool setAppTopState(QString &desktopfp, size_t num); - //下面三个暂时未封装 - bool setAppLaunchTimes(QString &desktopfp, size_t num); - bool updateAppLaunchTimes(QString &desktopfp); - 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 getFavoritesAppList(QStringList &list); - bool getTopAppList(QStringList &list); - bool getLaunchTimesAppList(QStringList &list); - bool getAppCategory(QString &desktopfp, QString &category); - //获取所有应用信息并存到一个结构体中 bool getAppInfoResults(QVector &appInfoResults); @@ -42,6 +31,7 @@ public: bool getAppTopState(QString &desktopfp, size_t &num); bool getAppLaunchedState(QString &desktopfp, size_t &num); bool getAppFavoriteState(QString &desktopfp, size_t &num); + bool getAppCategory(QString &desktopfp, QString &category); //添加快捷方式 bool addAppShortcut2Desktop(QString &desktopfp); @@ -51,10 +41,21 @@ public: bool searchInstallApp(QString &keyWord, QStringList &installAppInfoRes); bool searchInstallApp(QStringList &keyWord, QStringList &installAppInfoRes); + //卸载应用 bool uninstallApp(QString &desktopfp); + //数据库错误信息 QString lastError(void) const; + //下面的接口都不外放,暂时没啥用 + bool setAppLaunchTimes(QString &desktopfp, size_t num); + bool updateAppLaunchTimes(QString &desktopfp); + bool setAppLockState(QString &desktopfp, size_t num); + bool getAllAppDesktopList(QStringList &list); + bool getFavoritesAppList(QStringList &list); + bool getTopAppList(QStringList &list); + bool getLaunchTimesAppList(QStringList &list); + private: ~AppInfoTablePrivate(); bool initDateBaseConnection(); diff --git a/libsearch/appdata/app-info-table.cpp b/libsearch/appdata/app-info-table.cpp index 2717745..d99b881 100644 --- a/libsearch/appdata/app-info-table.cpp +++ b/libsearch/appdata/app-info-table.cpp @@ -61,6 +61,13 @@ bool AppInfoTablePrivate::setAppFavoritesState(QString &desktopfp, size_t num) qWarning() << "Failed to commit !" << cmd; m_database.rollback(); res = false; + } else { + QVector results; + AppInfoResult info; + info.desktopPath = desktopfp; + info.favorite = num; + results.append(std::move(info)); + Q_EMIT q->appDBItems2BUpdate(results); } } else { qWarning() << "Failed to start transaction mode!!!"; @@ -86,94 +93,13 @@ bool AppInfoTablePrivate::setAppTopState(QString &desktopfp, size_t num) qWarning() << "Failed to commit !" << cmd; m_database.rollback(); res = false; - } - } else { - qWarning() << "Failed to start transaction mode!!!"; - res = false; - } - return res; -} - -bool AppInfoTablePrivate::setAppLaunchTimes(QString &desktopfp, size_t num) -{ - bool res(true); - if (m_database.transaction()) { - QSqlQuery sql(m_database); - 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)) { - qWarning() << "Set app favorites state failed!" << m_database.lastError(); - res = false; - } - if (!m_database.commit()) { - qWarning() << "Failed to commit !" << cmd; - m_database.rollback(); - res = false; - } - } else { - qWarning() << "Failed to start transaction mode!!!"; - res = false; - } - return res; -} - -bool AppInfoTablePrivate::updateAppLaunchTimes(QString &desktopfp) -{ - bool res(true); - if (m_database.transaction()) { - QSqlQuery sql(m_database); - QString cmd = QString("SELECT LAUNCH_TIMES FROM appInfo WHERE DESKTOP_FILE_PATH='%1'").arg(desktopfp); - 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)) { - qWarning() << "Set app favorites state failed!" << m_database.lastError(); - res = false; - } - } else { - qWarning() << "Failed to exec next!" << cmd; - res = false; - } } else { - qWarning() << "Failed to exec:" << cmd; - res = false; - } - if (!m_database.commit()) { - qWarning() << "Failed to commit !" << cmd; - m_database.rollback(); - res = false; - } - } else { - qWarning() << "Failed to start transaction mode!!!"; - res = false; - } - return res; -} - -bool AppInfoTablePrivate::setAppLockState(QString &desktopfp, size_t num) -{ - bool res(true); - if (m_database.transaction()) { - QSqlQuery sql(m_database); - 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)) { - qWarning() << "Set app favorites state failed!" << m_database.lastError(); - res = false; - } - if (!m_database.commit()) { - qWarning() << "Failed to commit !" << cmd; - m_database.rollback(); - res = false; + QVector results; + AppInfoResult info; + info.desktopPath = desktopfp; + info.top = num; + results.append(std::move(info)); + Q_EMIT q->appDBItems2BUpdate(results); } } else { qWarning() << "Failed to start transaction mode!!!"; @@ -188,14 +114,17 @@ bool AppInfoTablePrivate::changeFavoriteAppPos(const QString &desktopfp, size_t if (m_database.transaction()) { QSqlQuery sql(m_database); QString cmd = QString("SELECT FAVORITES FROM appInfo WHERE DESKTOP_FILE_PATH = %0").arg(desktopfp); + int previousPos = 0; + //记录应用原位置 if (!sql.exec(cmd)) { qWarning() << "Fail to change favorite-app pos, because: " << m_database.lastError() << " when exec :" << cmd; res = false; } else { if (sql.next()) { + previousPos = sql.value(0).toInt(); cmd = QString("UPDATE appInfo SET FAVORITES = FAVORITES + 1 WHRER FAVORITES > %1 AND FAVORITES < %2") - .arg(sql.value(0).toUInt()) + .arg(previousPos) .arg(pos); } else { qWarning() << "Fail to change favorite-app pos when exec next, because: " << m_database.lastError(); @@ -220,6 +149,22 @@ bool AppInfoTablePrivate::changeFavoriteAppPos(const QString &desktopfp, size_t qWarning() << "Failed to commit when exec: " << cmd; m_database.rollback(); res = false; + } else { + cmd = QString("SELECT DESKTOP_FILE_PATH, FAVORITES FROM appInfo WHERE FAVORITES > %1 AND FAVORITES < %2") + .arg(previousPos) + .arg(pos); + if (!sql.exec(cmd)) { + qWarning() << "I'm going to send update favorites signal but fail to exec" << cmd; + } else { + QVector results; + while (sql.next()) { + AppInfoResult info; + info.desktopPath = sql.value("DESKTOP_FILE_PATH").toString(); + info.favorite = sql.value("FAVORITES").toInt(); + results.append(std::move(info)); + } + Q_EMIT q->appDBItems2BUpdate(results); + } } } else { @@ -236,14 +181,17 @@ bool AppInfoTablePrivate::changeTopAppPos(const QString &desktopfp, size_t pos) if (m_database.transaction()) { QSqlQuery sql(m_database); QString cmd = QString("SELECT TOP FROM appInfo WHERE DESKTOP_FILE_PATH = %0").arg(desktopfp); + int previousPos = 0; + //记录应用原位置 if (!sql.exec(cmd)) { qWarning() << "Fail to change top-app pos, because: " << m_database.lastError() << " when exec :" << cmd; res = false; } else { if (sql.next()) { + previousPos = sql.value(0).toInt(); cmd = QString("UPDATE appInfo SET TOP = TOP + 1 WHRER TOP > %1 AND TOP < %2") - .arg(sql.value(0).toUInt()) + .arg(previousPos) .arg(pos); } else { qWarning() << "Fail to change top-app pos when exec next, because: " << m_database.lastError(); @@ -268,139 +216,29 @@ bool AppInfoTablePrivate::changeTopAppPos(const QString &desktopfp, size_t pos) 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 res(true); - QSqlQuery sql(m_database); - QString cmd = QString("SELECT DESKTOP_FILE_PATH FROM appInfo"); - - if (sql.exec(cmd)) { - while (sql.next()) { - list.append(sql.value(0).toString()); - } - } else { - qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database.lastError(); - res = false; - } - - return res; -} - -bool AppInfoTablePrivate::getFavoritesAppList(QStringList &list) -{ - bool res(true); - if (m_database.transaction()) { - QSqlQuery sql(m_database); - QSqlQuery sqlque(m_database); - QString cmd = QString("SELECT DESKTOP_FILE_PATH FROM appInfo WHERE FAVORITES!=0 ORDER BY FAVORITES"); - int count = 0; - if (sql.exec(cmd)) { - while (sql.next()) { - list.append(sql.value(0).toString()); - cmd = QString("UPDATE appInfo SET FAVORITES=%1 WHERE DESKTOP_FILE_PATH='%2'") - .arg(++count) - .arg(sql.value(0).toString()); - if (!sqlque.exec(cmd)) { - qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database.lastError(); - res = false; - break; - } - } } 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 { - qWarning() << "Failed to start transaction mode!!!"; - res = false; - } - return res; -} - -bool AppInfoTablePrivate::getTopAppList(QStringList &list) -{ - bool res(true); - if (m_database.transaction()) { - QSqlQuery sql(m_database); - QSqlQuery sqlque(m_database); - QString cmd = QString("SELECT DESKTOP_FILE_PATH FROM appInfo WHERE TOP!=0 ORDER BY TOP"); - int count = 0; - if (sql.exec(cmd)) { - while (sql.next()) { - list.append(sql.value(0).toString()); - cmd = QString("UPDATE appInfo SET TOP=%1 WHERE DESKTOP_FILE_PATH='%2'") - .arg(++count) - .arg(sql.value(0).toString()); - if (!sqlque.exec(cmd)) { - qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database.lastError(); - res = false; - break; + cmd = QString("SELECT DESKTOP_FILE_PATH, TOP FROM appInfo WHERE TOP > %1 AND TOP < %2") + .arg(previousPos) + .arg(pos); + if (!sql.exec(cmd)) { + qWarning() << "I'm going to send update top signal but fail to exec" << cmd; + } else { + QVector results; + while (sql.next()) { + AppInfoResult info; + info.desktopPath = sql.value("DESKTOP_FILE_PATH").toString(); + info.favorite = sql.value("TOP").toInt(); + results.append(std::move(info)); } + Q_EMIT q->appDBItems2BUpdate(results); } - } 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 { - qWarning() << "Failed to start transaction mode!!!"; - res = false; - } - return res; -} -bool AppInfoTablePrivate::getLaunchTimesAppList(QStringList &list) -{ - bool res(true); - if (m_database.transaction()) { - QSqlQuery sql(m_database); - QSqlQuery sqlque(m_database); - QString cmd = QString("SELECT DESKTOP_FILE_PATH FROM appInfo ORDER BY LAUNCH_TIMES"); - int count = 0; - if (sql.exec(cmd)) { - while (sql.next()) { - list.append(sql.value(0).toString()); - cmd = QString("UPDATE appInfo SET TOP=%1 WHERE DESKTOP_FILE_PATH='%2'") - .arg(++count) - .arg(sql.value(0).toString()); - if (!sqlque.exec(cmd)) { - qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database.lastError(); - res = false; - break; - } - } - } 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 { qWarning() << "Failed to start transaction mode!!!"; res = false; } + return res; } @@ -448,7 +286,7 @@ bool AppInfoTablePrivate::getAppInfoResults(QVector &appInfoResul result.iconName = sql.value(2).toString(); result.category = sql.value(3).toString(); result.top = sql.value(4).toInt(); - result.favorate = sql.value(5).toInt(); + result.favorite = sql.value(5).toInt(); result.launchTimes = sql.value(6).toInt(); result.lock = sql.value(7).toInt(); result.firstLetter = sql.value(8).toString(); @@ -715,7 +553,13 @@ bool AppInfoTablePrivate::searchInstallApp(QStringList &keyWord, QStringList &in bool AppInfoTablePrivate::uninstallApp(QString &desktopfp) { bool res(false); + QString cmd = QString("kylin-uninstaller %1") + .arg(desktopfp.toLocal8Bit().data()); + res = QProcess::startDetached(cmd); + qDebug() << "kylin-uninstaller uninstall:" << cmd << res; + return res; + /* bool isOsReleaseUbuntu(false); QFile file("/etc/os-release"); if (file.open(QFile::ReadOnly)) { @@ -744,8 +588,8 @@ bool AppInfoTablePrivate::uninstallApp(QString &desktopfp) res = QProcess::startDetached(cmd); qDebug() << "dpkg -S uninstall:" << cmd << res; } - return res; + */ } QString AppInfoTablePrivate::lastError() const @@ -753,6 +597,182 @@ QString AppInfoTablePrivate::lastError() const return m_database.lastError().text(); } +bool AppInfoTablePrivate::setAppLaunchTimes(QString &desktopfp, size_t num) +{ + bool res(true); + if (m_database.transaction()) { + QSqlQuery sql(m_database); + 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)) { + qWarning() << "Set app favorites state failed!" << m_database.lastError(); + res = false; + } + if (!m_database.commit()) { + qWarning() << "Failed to commit !" << cmd; + m_database.rollback(); + res = false; + } + } else { + qWarning() << "Failed to start transaction mode!!!"; + res = false; + } + return res; +} + +bool AppInfoTablePrivate::updateAppLaunchTimes(QString &desktopfp) +{ + bool res(true); + if (m_database.transaction()) { + QSqlQuery sql(m_database); + QString cmd = QString("SELECT LAUNCH_TIMES FROM appInfo WHERE DESKTOP_FILE_PATH='%1'").arg(desktopfp); + 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)) { + qWarning() << "Set app favorites state failed!" << m_database.lastError(); + res = false; + } + } else { + qWarning() << "Failed to exec next!" << cmd; + res = false; + } + } else { + qWarning() << "Failed to exec:" << cmd; + res = false; + } + if (!m_database.commit()) { + qWarning() << "Failed to commit !" << cmd; + m_database.rollback(); + res = false; + } + } else { + qWarning() << "Failed to start transaction mode!!!"; + res = false; + } + return res; +} + +bool AppInfoTablePrivate::setAppLockState(QString &desktopfp, size_t num) +{ + bool res(true); + if (m_database.transaction()) { + QSqlQuery sql(m_database); + 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)) { + qWarning() << "Set app favorites state failed!" << m_database.lastError(); + res = false; + } + if (!m_database.commit()) { + qWarning() << "Failed to commit !" << cmd; + m_database.rollback(); + res = false; + } + } else { + qWarning() << "Failed to start transaction mode!!!"; + res = false; + } + return res; +} + +bool AppInfoTablePrivate::getAllAppDesktopList(QStringList &list) +{ + bool res(true); + QSqlQuery sql(m_database); + QString cmd = QString("SELECT DESKTOP_FILE_PATH FROM appInfo"); + + if (sql.exec(cmd)) { + while (sql.next()) { + list.append(sql.value(0).toString()); + } + } else { + qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database.lastError(); + res = false; + } + + return res; +} + +bool AppInfoTablePrivate::getFavoritesAppList(QStringList &list) +{ + bool res(true); + QSqlQuery sql(m_database); + QString cmd = QString("SELECT DESKTOP_FILE_PATH FROM appInfo WHERE FAVORITES!=0 ORDER BY FAVORITES"); + if (sql.exec(cmd)) { + while (sql.next()) { + list.append(sql.value(0).toString()); + } + } else { + qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database.lastError(); + res = false; + } + return res; +} + +bool AppInfoTablePrivate::getTopAppList(QStringList &list) +{ + bool res(true); + QSqlQuery sql(m_database); + QString cmd = QString("SELECT DESKTOP_FILE_PATH FROM appInfo WHERE TOP!=0 ORDER BY TOP"); + + if (sql.exec(cmd)) { + while (sql.next()) { + list.append(sql.value(0).toString()); + } + } else { + qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database.lastError(); + res = false; + } + + return res; +} + +bool AppInfoTablePrivate::getLaunchTimesAppList(QStringList &list) +{ + bool res(true); + if (m_database.transaction()) { + QSqlQuery sql(m_database); + QSqlQuery sqlque(m_database); + QString cmd = QString("SELECT DESKTOP_FILE_PATH FROM appInfo ORDER BY LAUNCH_TIMES"); + int count = 0; + if (sql.exec(cmd)) { + while (sql.next()) { + list.append(sql.value(0).toString()); + cmd = QString("UPDATE appInfo SET TOP=%1 WHERE DESKTOP_FILE_PATH='%2'") + .arg(++count) + .arg(sql.value(0).toString()); + if (!sqlque.exec(cmd)) { + qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database.lastError(); + res = false; + break; + } + } + } 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 { + qWarning() << "Failed to start transaction mode!!!"; + res = false; + } + return res; +} + AppInfoTablePrivate::~AppInfoTablePrivate() { this->closeDataBase(); @@ -816,41 +836,6 @@ bool AppInfoTable::setAppTopState(QString &desktopfp, size_t num) return d->setAppTopState(desktopfp, num); } -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); -} - -bool AppInfoTable::getAllAppDesktopList(QStringList &list) -{ - return d->getAllAppDesktopList(list); -} - -bool AppInfoTable::getFavoritesAppList(QStringList &list) -{ - return d->getFavoritesAppList(list); -} - -bool AppInfoTable::getTopAppList(QStringList &list) -{ - return d->getTopAppList(list); -} - -bool AppInfoTable::getLaunchTimesAppList(QStringList &list) -{ - return d->getLaunchTimesAppList(list); -} - bool AppInfoTable::getAppCategory(QString &desktopfp, QString &category) { return d->getAppCategory(desktopfp, category); @@ -920,3 +905,39 @@ QString AppInfoTable::lastError() const { return d->lastError(); } + +//下面接口暂时没啥用,不外放。 +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); +} + +bool AppInfoTable::getAllAppDesktopList(QStringList &list) +{ + return d->getAllAppDesktopList(list); +} + +bool AppInfoTable::getFavoritesAppList(QStringList &list) +{ + return d->getFavoritesAppList(list); +} + +bool AppInfoTable::getTopAppList(QStringList &list) +{ + return d->getTopAppList(list); +} + +bool AppInfoTable::getLaunchTimesAppList(QStringList &list) +{ + return d->getLaunchTimesAppList(list); +} diff --git a/libsearch/appdata/app-info-table.h b/libsearch/appdata/app-info-table.h index 5ff4e5a..cc3eac4 100644 --- a/libsearch/appdata/app-info-table.h +++ b/libsearch/appdata/app-info-table.h @@ -33,33 +33,6 @@ public: */ 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); - - /** - * @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); - - /** - * @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 getLaunchTimesAppList(QStringList &list); - bool getAppCategory(QString &desktopfp, QString &category); - /** * @brief AppInfoTable::changeFavoriteAppPos * change the position of the app which is one of the Favorites Apps @@ -90,6 +63,7 @@ public: bool getAppTopState(QString &desktopfp, size_t &num); bool getAppLaunchedState(QString &desktopfp, size_t &num); bool getAppFavoriteState(QString &desktopfp, size_t &num); + bool getAppCategory(QString &desktopfp, QString &category); bool addAppShortcut2Desktop(QString &desktopfp); bool addAppShortcut2Panel(QString &desktopfp); @@ -107,10 +81,16 @@ public: QString lastError(void) const; private: + //暂不外放的接口 bool setAppLaunchTimes(QString &desktopfp, size_t num); bool setAppLockState(QString &desktopfp, size_t num); bool updateAppLaunchTimes(QString &desktopfp); + bool getAllAppDesktopList(QStringList &list); + bool getFavoritesAppList(QStringList &list); + bool getTopAppList(QStringList &list); + bool getLaunchTimesAppList(QStringList &list); +private: AppInfoTablePrivate *d; Q_SIGNALS: diff --git a/ukui-search-app-data-service/app-db-manager.cpp b/ukui-search-app-data-service/app-db-manager.cpp index 1c2ddcf..7fd4174 100644 --- a/ukui-search-app-data-service/app-db-manager.cpp +++ b/ukui-search-app-data-service/app-db-manager.cpp @@ -27,7 +27,7 @@ AppDBManager *AppDBManager::getInstance() return global_instance; } -AppDBManager::AppDBManager(QObject *parent) : QObject(parent), m_database(QSqlDatabase()) +AppDBManager::AppDBManager(QObject *parent) : QThread(parent), m_database(QSqlDatabase()) { //链接数据库 if (openDataBase()) { @@ -46,7 +46,7 @@ AppDBManager::AppDBManager(QObject *parent) : QObject(parent), m_database(QSqlDa m_qSettings->endGroup(); //初始化数据库 - initDataBase(); + refreshDataBase(); //初始化FileSystemWatcher m_watchAppDir = new QFileSystemWatcher(this); @@ -63,23 +63,46 @@ AppDBManager::AppDBManager(QObject *parent) : QObject(parent), m_database(QSqlDa } m_watchAppDir->addPath(SNAPD_APP_DESKTOP_PATH); - //监听desktop文件所在目录,TODO:directoryChange会发多次信号,需要计时器阻塞 + //初始化timer + this->start(); + m_timer = new QTimer(); + m_maxProcessTimer = new QTimer(); + + m_timer->setInterval(2*1000); + m_maxProcessTimer->setInterval(5*1000); + + m_timer->setSingleShot(true); + m_maxProcessTimer->setSingleShot(true); + + m_timer->moveToThread(this); + m_maxProcessTimer->moveToThread(this); + + connect(this, SIGNAL(startTimer()), m_timer, SLOT(start())); + connect(this, SIGNAL(maxProcessTimerStart()), m_maxProcessTimer, SLOT(start())); + connect(this, &AppDBManager::stopTimer, m_timer, &QTimer::stop); + connect(this, &AppDBManager::stopTimer, m_maxProcessTimer, &QTimer::stop); + + //监听desktop文件所在目录,由于directoryChange会发多次信号,使用计时器阻塞 connect(m_watchAppDir, &QFileSystemWatcher::directoryChanged, this, [ = ](const QString & path) { qDebug() << "m_watchAppDir directoryChanged:" << path; - if (m_database.transaction()) { -// this->updateAppInfoDB(); - this->updateAllData2DB(); - if (!m_database.commit()) { - qWarning() << "Failed to commit !"; - m_database.rollback(); - } else { - Q_EMIT this->finishHandleAppDB(); - } - } else { - qWarning() << "Failed to start transaction mode!!!"; + Q_EMIT this->startTimer(); + if (!m_maxProcessTimer->isActive()) { + Q_EMIT this->maxProcessTimerStart(); } }); + //计时器超时统一进行更新操作 + connect(m_timer, &QTimer::timeout, this, [ & ] { + qDebug() << "Time out! Now I can update the database!"; + Q_EMIT this->stopTimer(); + this->refreshDataBase(); + }, Qt::DirectConnection); + connect(m_maxProcessTimer, &QTimer::timeout, this, [ & ] { + qDebug() << "I've waited too lang, I have to update the database now!"; + Q_EMIT this->stopTimer(); + this->refreshDataBase(); + }, Qt::DirectConnection); + //监控应用进程开启 connect(KWindowSystem::self(), &KWindowSystem::windowAdded, [ = ](WId id) { QString desktopfp = ConvertWinidToDesktop::getConverter().tranIdToDesktop(id); @@ -299,6 +322,7 @@ void AppDBManager::loadDesktopFilePaths(QString path, QFileInfoList &infolist) void AppDBManager::updateAllData2DB() { + m_dbChanged = false; bool firstExec = false; QSqlQuery sql(m_database); QString cmd = "SELECT DESKTOP_FILE_PATH,MD5 FROM appInfo"; @@ -351,24 +375,28 @@ void AppDBManager::updateAllData2DB() //判断系统语言是否改变 if (m_localeChanged) { this->updateLocaleData2DB(path); + m_dbChanged = true; } dataMap.remove(path); continue; } else { //数据库有记录但md5值改变则update this->updateAppDesktopFile2DB(path); + m_dbChanged = true; dataMap.remove(path); continue; } } else { //数据库中没有记录则insert this->addAppDesktopFile2DB(path); + m_dbChanged = true; dataMap.remove(path); continue; } } //数据库为空则全部insert this->addAppDesktopFile2DB(path); + m_dbChanged = true; dataMap.remove(path); } @@ -379,6 +407,7 @@ void AppDBManager::updateAllData2DB() if (!dataMap.isEmpty()) { for (auto i = dataMap.constBegin(); i != dataMap.constEnd(); i++) { this->deleteAppDesktopFile2DB(i.key()); + m_dbChanged = true; } } } @@ -420,7 +449,12 @@ bool AppDBManager::updateLocaleData2DB(QString desktopPath) return res; } -void AppDBManager::initDataBase() +void AppDBManager::run() +{ + exec(); +} + +void AppDBManager::refreshDataBase() { if (m_database.transaction()) { // this->updateAppInfoDB(); @@ -428,6 +462,8 @@ void AppDBManager::initDataBase() if (!m_database.commit()) { qWarning() << "Failed to commit !"; m_database.rollback(); + } else if (!m_dbChanged) { + qDebug() << "app DataBase has no changes!"; } else { Q_EMIT this->finishHandleAppDB(); } @@ -568,11 +604,37 @@ bool AppDBManager::deleteAppDesktopFile2DB(const QString &desktopfd) { bool res(true); QSqlQuery sql(m_database); - QString cmd = QString("DELETE FROM appInfo WHERE DESKTOP_FILE_PATH = '%0'").arg(desktopfd); + QString cmd = QString("SELECT FAVORITES, TOP FROM appInfo WHERE DESKTOP_FILE_PATH = '%0'").arg(desktopfd); + + //查询要删除信息的应用是否被收藏或顶置过 + if (!sql.exec(cmd)) { + qWarning() << m_database.lastError() << cmd; + } else if (sql.next()) { + int favorites = sql.value("FAVORITES").toInt(); + int top = sql.value("TOP").toInt(); + if (favorites) { + cmd = QString("UPDATE appInfo SET FAVORITES = FAVORITES -1 WHERE FAVORITES > %1").arg(favorites); + if (!sql.exec(cmd)) { + qWarning() << "I'm going to delete item in db, fail to update the FAVORITES because:" << m_database.lastError() << cmd; + } + } + if (top) { + cmd = QString("UPDATE appInfo SET TOP = TOP -1 WHERE TOP > %1").arg(top); + if (!sql.exec(cmd)) { + qWarning() << "I'm going to delete item in db, fail to update the TOP because:" << m_database.lastError() << cmd; + } + } + } else { + qWarning() << "Fail to exec next, because" << m_database.lastError() << "while executing " << cmd; + } + + //执行删除操作 + cmd = QString("DELETE FROM appInfo WHERE DESKTOP_FILE_PATH = '%0'").arg(desktopfd); if (!sql.exec(cmd)) { qWarning() << m_database.lastError() << cmd; res = false; } + if (res) { Q_EMIT this->appDBItemDelete(desktopfd); qDebug() << "app database delete " << desktopfd << "success!"; @@ -689,6 +751,7 @@ bool AppDBManager::updateAppLaunchTimes(QString &desktopfp) res = false; } else { AppInfoResult result; + result.desktopPath = desktopfp; result.launchTimes = launchTimes; Q_EMIT this->appDBItemUpdate(result); } diff --git a/ukui-search-app-data-service/app-db-manager.h b/ukui-search-app-data-service/app-db-manager.h index 5bd0592..8d51816 100644 --- a/ukui-search-app-data-service/app-db-manager.h +++ b/ukui-search-app-data-service/app-db-manager.h @@ -14,6 +14,8 @@ #include #include #include +#include +#include #define CONNECTION_NAME QLatin1String("ukss-appdb-connection") @@ -36,7 +38,7 @@ public: } }; -class AppDBManager : public QObject +class AppDBManager : public QThread { Q_OBJECT @@ -58,6 +60,8 @@ public: bool updateAppLaunchTimes(QString &desktopfp); void updateAllData2DB(); bool updateLocaleData2DB(QString desktopPath); +protected: + void run() override; private: explicit AppDBManager(QObject *parent = nullptr); @@ -65,7 +69,7 @@ private: void loadDesktopFilePaths(QString path, QFileInfoList &infolist); - void initDataBase(); + void refreshDataBase(); bool openDataBase(); void closeDataBase(); @@ -75,7 +79,12 @@ private: void getFilePathList(QStringList &pathList); QSettings *m_qSettings = nullptr; + bool m_localeChanged; + bool m_dbChanged; + + QTimer *m_timer = nullptr; + QTimer *m_maxProcessTimer = nullptr; QSqlDatabase m_database; @@ -131,11 +140,16 @@ private: //原本额外排除的目录,不知道额外的原因,有可能之后有问题--bjj20220621 "/usr/share/applications/screensavers" }; + Q_SIGNALS: void appDBItemUpdate(const AppInfoResult&); void appDBItemAdd(const AppInfoResult&); void appDBItemDelete(const QString&); void finishHandleAppDB(); + + void startTimer(); + void maxProcessTimerStart(); + void stopTimer(); }; } diff --git a/ukui-search-app-data-service/ukui-search-app-data-service.pro b/ukui-search-app-data-service/ukui-search-app-data-service.pro index a48aa90..e34dd85 100644 --- a/ukui-search-app-data-service/ukui-search-app-data-service.pro +++ b/ukui-search-app-data-service/ukui-search-app-data-service.pro @@ -29,13 +29,13 @@ SOURCES += \ main.cpp \ convert-winid-to-desktop.cpp \ app-db-manager.cpp \ - signal-transformer.cpp \ + signal-transformer.cpp \ ukui-search-app-data-service.cpp \ HEADERS += \ convert-winid-to-desktop.h \ app-db-manager.h \ - signal-transformer.h \ + signal-transformer.h \ ukui-search-app-data-service.h \ target.path = /usr/bin