fix(app-database-service):数据库删除后重建失败,应用打开次数记录错误.

建数据库时缺少START_UP_WMCLAS字段导致所有数据写入失败,记录打开次数换成使用processmanager的信号来触发.
This commit is contained in:
JunjieBai 2023-11-14 14:55:31 +08:00 committed by iaom
parent 1bcf2922c0
commit e18bb390a7
2 changed files with 43 additions and 16 deletions

View File

@ -25,6 +25,8 @@
#include "appdatabaseadaptor.h" #include "appdatabaseadaptor.h"
#include "file-utils.h" #include "file-utils.h"
#include "application-property-helper.h" #include "application-property-helper.h"
#include "app-db-manager.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"
@ -168,14 +170,24 @@ AppDBManager::AppDBManager(QObject *parent) : QThread(parent), m_database(QSqlDa
}, Qt::DirectConnection); }, Qt::DirectConnection);
*/ */
//监控应用进程开启 m_processManagerInterface = new QDBusInterface(QStringLiteral("com.kylin.ProcessManager"),
connect(KWindowSystem::self(), &KWindowSystem::windowAdded, [ = ](WId id) { QStringLiteral("/com/kylin/ProcessManager/AppLauncher"),
QDBusVariant dbusVariant(id); QStringLiteral("com.kylin.ProcessManager.AppLauncher"),
QString desktopFilePath = this->tranWinIdToDesktopFilePath(dbusVariant); QDBusConnection::sessionBus(), this);
if (!desktopFilePath.isEmpty()) { if (!m_processManagerInterface->isValid()) {
this->updateLaunchTimes(desktopFilePath); qWarning() << qPrintable(QDBusConnection::sessionBus().lastError().message());
} else {
connect(m_processManagerInterface, SIGNAL(AppLaunched(QString)), this, SLOT(handleAppLaunched(QString)));
} }
});
//监控应用进程开启
// connect(KWindowSystem::self(), &KWindowSystem::windowAdded, [ = ](WId id) {
// QDBusVariant dbusVariant(id);
// QString desktopFilePath = this->tranWinIdToDesktopFilePath(dbusVariant);
// if (!desktopFilePath.isEmpty()) {
// this->updateLaunchTimes(desktopFilePath);
// }
// });
new AppDBManagerAdaptor(this); new AppDBManagerAdaptor(this);
} else { } else {
qDebug() << "App-db-manager does nothing."; qDebug() << "App-db-manager does nothing.";
@ -187,12 +199,12 @@ AppDBManager::~AppDBManager()
if (m_watcher) { if (m_watcher) {
delete m_watcher; delete m_watcher;
} }
m_watcher = NULL; m_watcher = nullptr;
if (m_snapdWatcher) { if (m_snapdWatcher) {
delete m_snapdWatcher; delete m_snapdWatcher;
} }
m_snapdWatcher = NULL; m_snapdWatcher = nullptr;
// if(m_watchAppDir) { // if(m_watchAppDir) {
// delete m_watchAppDir; // delete m_watchAppDir;
@ -205,7 +217,7 @@ void AppDBManager::buildAppInfoDB()
{ {
qDebug() << "I'm going to build app info database."; qDebug() << "I'm going to build app info database.";
QSqlQuery sql(m_database); QSqlQuery sql(m_database);
QString cmd = QString("CREATE TABLE IF NOT EXISTS appInfo(%1, %2, %3, %4, %5, %6, %7, %8,%9, %10, %11, %12, %13, %14, %15, %16, %17, %18, %19, %20, %21, %22)") QString cmd = QString("CREATE TABLE IF NOT EXISTS appInfo(%1, %2, %3, %4, %5, %6, %7, %8,%9, %10, %11, %12, %13, %14, %15, %16, %17, %18, %19, %20, %21, %22, %23)")
// .arg("ID INT")//自增id // .arg("ID INT")//自增id
.arg("DESKTOP_FILE_PATH TEXT PRIMARY KEY NOT NULL")//desktop文件路径 .arg("DESKTOP_FILE_PATH TEXT PRIMARY KEY NOT NULL")//desktop文件路径
.arg("MODIFYED_TIME TEXT")//YYYYMMDDHHmmSS 修改日期 .arg("MODIFYED_TIME TEXT")//YYYYMMDDHHmmSS 修改日期
@ -228,7 +240,8 @@ void AppDBManager::buildAppInfoDB()
.arg("TOP INT")//置顶顺序 0:未置顶;>0的数字表示置顶顺序 .arg("TOP INT")//置顶顺序 0:未置顶;>0的数字表示置顶顺序
.arg("LOCK INT")//应用是否锁定管控0未锁定1锁定 .arg("LOCK INT")//应用是否锁定管控0未锁定1锁定
.arg("DONT_DISPLAY INT")//应用隐藏(NoDisplay, NotShowIn) .arg("DONT_DISPLAY INT")//应用隐藏(NoDisplay, NotShowIn)
.arg("AUTO_START INT");//自启应用 .arg("AUTO_START INT")//自启应用
.arg("START_UP_WMCLASS TEXT");//classname
if (!sql.exec(cmd)) { if (!sql.exec(cmd)) {
qWarning() << m_database.lastError() << cmd; qWarning() << m_database.lastError() << cmd;
return; return;
@ -262,7 +275,7 @@ void AppDBManager::initFileSystemWatcher()
connect(m_snapdWatcher, &FileSystemWatcher::created, this, [ = ] (const QString &path, bool isDir) { connect(m_snapdWatcher, &FileSystemWatcher::created, this, [ = ] (const QString &path, bool isDir) {
if (isDir) { if (isDir) {
//监测新增目录为/var/lib/snapd时将其替换为snapdWatcher的watchpath //监测新增目录为/var/lib/snapd时将其替换为snapdWatcher的watch path
if (path == "/var/lib/snapd") { if (path == "/var/lib/snapd") {
m_snapdWatcher->removeWatch(m_snapdPath); m_snapdWatcher->removeWatch(m_snapdPath);
m_snapdWatcher->addWatch(path); m_snapdWatcher->addWatch(path);
@ -1311,7 +1324,7 @@ void AppDBManager::handleDataBaseRefresh(const QStringList &appPaths, bool dbVer
this->loadDesktopFilePaths(path, infos); this->loadDesktopFilePaths(path, infos);
} }
if(infos.size() < 1) { if(infos.isEmpty()) {
return; return;
} }
XdgDesktopFile desktopfile; XdgDesktopFile desktopfile;
@ -1329,7 +1342,7 @@ void AppDBManager::handleDataBaseRefresh(const QStringList &appPaths, bool dbVer
} }
desktopfile.load(path); desktopfile.load(path);
//排除loaclized名字为空 //排除localized名字为空
if (desktopfile.localizedValue("Name").toString().isEmpty()) { if (desktopfile.localizedValue("Name").toString().isEmpty()) {
continue; continue;
} }
@ -1379,8 +1392,8 @@ bool AppDBManager::handleValueSet(const ApplicationInfoMap appInfoMap)
QSqlQuery query(m_database); QSqlQuery query(m_database);
for (auto iter = appInfoMap.constBegin(); iter != appInfoMap.constEnd(); iter++) { for (auto iter = appInfoMap.constBegin(); iter != appInfoMap.constEnd(); iter++) {
QString desktopFilePath = iter.key(); const QString& desktopFilePath = iter.key();
ApplicationPropertyMap propMap = iter.value(); const ApplicationPropertyMap& propMap = iter.value();
for (auto propIter = propMap.constBegin(); propIter != propMap.constEnd(); ++propIter) { for (auto propIter = propMap.constBegin(); propIter != propMap.constEnd(); ++propIter) {
QString field = ApplicationPropertyHelper(propIter.key()).dataBaseField(); QString field = ApplicationPropertyHelper(propIter.key()).dataBaseField();
@ -1643,3 +1656,11 @@ void AppDBManager::setValue(const ApplicationInfoMap &infos2BSet)
PendingAppInfoQueue::getAppInfoQueue().enqueue(item); PendingAppInfoQueue::getAppInfoQueue().enqueue(item);
} }
} }
void AppDBManager::handleAppLaunched(QString desktopFilePath) {
if (!desktopFilePath.isEmpty()) {
this->updateLaunchTimes(desktopFilePath);
} else {
qWarning() << "desktopFilePath is empty.";
}
}

View File

@ -30,6 +30,7 @@
#include <QMutex> #include <QMutex>
#include <QSettings> #include <QSettings>
#include <QThread> #include <QThread>
#include <QDBusInterface>
#include <QDBusVariant> #include <QDBusVariant>
#include "pending-app-info-queue.h" #include "pending-app-info-queue.h"
#include "file-system-watcher.h" #include "file-system-watcher.h"
@ -105,6 +106,9 @@ public Q_SLOTS:
void setValue(const ApplicationInfoMap &infos2BSet); void setValue(const ApplicationInfoMap &infos2BSet);
private Q_SLOTS:
void handleAppLaunched(QString desktopFilePath);
protected: protected:
void run() override; void run() override;
@ -143,6 +147,8 @@ private:
QSettings *m_lastLocaleNameQsettings = nullptr; QSettings *m_lastLocaleNameQsettings = nullptr;
QSettings *m_dbVersionQsettings = nullptr; QSettings *m_dbVersionQsettings = nullptr;
QDBusInterface *m_processManagerInterface = nullptr;
// QTimer *m_timer = nullptr; // QTimer *m_timer = nullptr;
// QTimer *m_maxProcessTimer = nullptr; // QTimer *m_maxProcessTimer = nullptr;
// QFileSystemWatcher *m_watchAppDir = nullptr; // QFileSystemWatcher *m_watchAppDir = nullptr;