2022-07-12 11:26:03 +08:00
|
|
|
|
#include "pending-app-info-queue.h"
|
|
|
|
|
#include "app-db-manager.h"
|
|
|
|
|
|
|
|
|
|
using namespace UkuiSearch;
|
|
|
|
|
|
|
|
|
|
QMutex PendingAppInfoQueue::s_mutex;
|
|
|
|
|
|
|
|
|
|
PendingAppInfoQueue &PendingAppInfoQueue::getAppInfoQueue()
|
|
|
|
|
{
|
|
|
|
|
static PendingAppInfoQueue instance;
|
|
|
|
|
return instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PendingAppInfoQueue::enqueue(const PendingAppInfo &appInfo)
|
|
|
|
|
{
|
|
|
|
|
QMutexLocker locker(&s_mutex);
|
|
|
|
|
m_handleTimes++;
|
2022-08-08 13:57:08 +08:00
|
|
|
|
int index = m_cache.lastIndexOf(appInfo);
|
2022-07-12 11:26:03 +08:00
|
|
|
|
if (index == -1) {
|
|
|
|
|
m_cache << appInfo;
|
|
|
|
|
} else {
|
2023-03-15 11:32:42 +08:00
|
|
|
|
//刷新数据库操作直接插入
|
|
|
|
|
if (appInfo.handleType() == PendingAppInfo::RefreshDataBase) {
|
|
|
|
|
m_cache << appInfo;
|
|
|
|
|
}
|
2022-08-08 13:57:08 +08:00
|
|
|
|
//要插入项操作类型为删除,清除之前所有操作,替换为删除
|
|
|
|
|
if (appInfo.handleType() == PendingAppInfo::Delete) {
|
|
|
|
|
m_cache.removeAll(appInfo);
|
|
|
|
|
m_cache << appInfo;
|
|
|
|
|
} else if (m_cache[index].handleType() == PendingAppInfo::Delete) {
|
|
|
|
|
//先删后建,分别处理
|
|
|
|
|
if (appInfo.handleType() == PendingAppInfo::Insert) {
|
|
|
|
|
m_cache << appInfo;
|
|
|
|
|
}
|
|
|
|
|
} else if (m_cache[index].handleType() <= PendingAppInfo::UpdateLocaleData
|
|
|
|
|
and appInfo.handleType() <= PendingAppInfo::UpdateLocaleData) {
|
|
|
|
|
//类型为insert, updateall, updatelocaledata时,设置为优先级高的操作类型
|
|
|
|
|
if (m_cache[index].handleType() > appInfo.handleType()) {
|
|
|
|
|
m_cache.remove(index);
|
|
|
|
|
m_cache << appInfo;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
m_cache[index].merge(appInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2022-07-14 19:01:56 +08:00
|
|
|
|
//只要操作类型为delete,直接覆盖
|
2022-08-08 13:57:08 +08:00
|
|
|
|
if (m_cache[index].handleType() == PendingAppInfo::Delete
|
|
|
|
|
or appInfo.handleType() == PendingAppInfo::Delete) {
|
|
|
|
|
m_cache[index].setHandleType(PendingAppInfo::Delete);
|
2022-07-14 19:01:56 +08:00
|
|
|
|
|
|
|
|
|
//已插入项操作类型为对所有desktop文件相关数据进行操作
|
2022-08-08 13:57:08 +08:00
|
|
|
|
} else if (m_cache[index].handleType() < PendingAppInfo::UpdateLocaleData
|
|
|
|
|
and appInfo.handleType() < PendingAppInfo::UpdateLocaleData) {
|
2022-07-14 19:01:56 +08:00
|
|
|
|
//设置为优先级高的操作类型
|
2022-07-12 11:26:03 +08:00
|
|
|
|
if (m_cache[index].handleType() > appInfo.handleType()) {
|
|
|
|
|
m_cache[index].setHandleType(appInfo);
|
|
|
|
|
}
|
2022-07-14 19:01:56 +08:00
|
|
|
|
} else {
|
|
|
|
|
m_cache[index].merge(appInfo);
|
2022-07-12 11:26:03 +08:00
|
|
|
|
}
|
2022-08-08 13:57:08 +08:00
|
|
|
|
*/
|
2022-07-12 11:26:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//启动定时器
|
|
|
|
|
if (!m_maxProcessTimer->isActive()) {
|
|
|
|
|
qDebug() << "start max timer";
|
|
|
|
|
Q_EMIT this->startMaxProcessTimer();
|
|
|
|
|
}
|
|
|
|
|
Q_EMIT this->startMinProcessTimer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PendingAppInfoQueue::run()
|
|
|
|
|
{
|
|
|
|
|
exec();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PendingAppInfoQueue::PendingAppInfoQueue(QObject *parent) : QThread(parent)
|
|
|
|
|
{
|
|
|
|
|
this->start();
|
|
|
|
|
|
|
|
|
|
m_handleTimes = 0;
|
|
|
|
|
|
|
|
|
|
m_minProcessTimer = new QTimer;
|
|
|
|
|
m_maxProcessTimer = new QTimer;
|
|
|
|
|
|
|
|
|
|
m_minProcessTimer->setInterval(500);
|
|
|
|
|
m_maxProcessTimer->setInterval(5*1000);
|
|
|
|
|
|
|
|
|
|
m_minProcessTimer->setSingleShot(true);
|
|
|
|
|
m_maxProcessTimer->setSingleShot(true);
|
|
|
|
|
|
|
|
|
|
m_minProcessTimer->moveToThread(this);
|
|
|
|
|
m_maxProcessTimer->moveToThread(this);
|
|
|
|
|
|
|
|
|
|
connect(this, SIGNAL(startMinProcessTimer()), m_minProcessTimer, SLOT(start()));
|
|
|
|
|
connect(this, SIGNAL(startMaxProcessTimer()), m_maxProcessTimer, SLOT(start()));
|
|
|
|
|
|
|
|
|
|
connect(this, &PendingAppInfoQueue::stopTimer, m_minProcessTimer, &QTimer::stop);
|
|
|
|
|
connect(this, &PendingAppInfoQueue::stopTimer, m_maxProcessTimer, &QTimer::stop);
|
|
|
|
|
|
|
|
|
|
connect(m_minProcessTimer, &QTimer::timeout, this, &PendingAppInfoQueue::processCache);
|
|
|
|
|
connect(m_maxProcessTimer, &QTimer::timeout, this, &PendingAppInfoQueue::processCache);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PendingAppInfoQueue::~PendingAppInfoQueue()
|
|
|
|
|
{
|
2022-11-02 16:56:29 +08:00
|
|
|
|
this->quit();
|
|
|
|
|
this->wait();
|
2022-07-12 11:26:03 +08:00
|
|
|
|
if (m_minProcessTimer) {
|
|
|
|
|
delete m_minProcessTimer;
|
|
|
|
|
}
|
|
|
|
|
if (m_maxProcessTimer) {
|
|
|
|
|
delete m_maxProcessTimer;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PendingAppInfoQueue::processCache()
|
|
|
|
|
{
|
|
|
|
|
s_mutex.lock();
|
|
|
|
|
qDebug() << "handle apps:" << m_handleTimes;
|
|
|
|
|
m_handleTimes = 0;
|
|
|
|
|
m_cache.swap(m_pendingAppInfos);
|
|
|
|
|
s_mutex.unlock();
|
|
|
|
|
|
|
|
|
|
if (m_pendingAppInfos.isEmpty()) {
|
|
|
|
|
qWarning() << "No App Info will be process, I'll stop process cache of app queue.";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (AppDBManager::getInstance()->startTransaction()) {
|
|
|
|
|
for (const PendingAppInfo &info : m_pendingAppInfos) {
|
|
|
|
|
PendingAppInfo::HandleTypes handleTypes = info.handleType();
|
2023-03-15 11:32:42 +08:00
|
|
|
|
if (handleTypes <= PendingAppInfo::UpdateLocaleData || handleTypes == PendingAppInfo::RefreshDataBase) {
|
2022-07-12 11:26:03 +08:00
|
|
|
|
switch (handleTypes) {
|
2022-08-08 13:57:08 +08:00
|
|
|
|
case PendingAppInfo::Delete:
|
2022-07-12 11:26:03 +08:00
|
|
|
|
AppDBManager::getInstance()->handleDBItemDelete(info.path());
|
|
|
|
|
break;
|
2022-08-08 13:57:08 +08:00
|
|
|
|
case PendingAppInfo::Insert:
|
2022-07-12 11:26:03 +08:00
|
|
|
|
AppDBManager::getInstance()->handleDBItemInsert(info.path());
|
|
|
|
|
break;
|
2022-08-08 13:57:08 +08:00
|
|
|
|
case PendingAppInfo::UpdateAll:
|
2022-07-12 11:26:03 +08:00
|
|
|
|
AppDBManager::getInstance()->handleDBItemUpdate(info.path());
|
|
|
|
|
break;
|
2022-08-08 13:57:08 +08:00
|
|
|
|
case PendingAppInfo::UpdateLocaleData:
|
|
|
|
|
AppDBManager::getInstance()->handleLocaleDataUpdate(info.path());
|
|
|
|
|
break;
|
2023-03-15 11:32:42 +08:00
|
|
|
|
case PendingAppInfo::RefreshDataBase:
|
|
|
|
|
AppDBManager::getInstance()->handleDataBaseRefresh(info.pathsNeedRefreshData());
|
|
|
|
|
break;
|
2022-07-12 11:26:03 +08:00
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2022-08-08 13:57:08 +08:00
|
|
|
|
if (handleTypes & PendingAppInfo::Insert) {
|
2022-07-14 19:01:56 +08:00
|
|
|
|
AppDBManager::getInstance()->handleDBItemInsert(info.path());
|
|
|
|
|
}
|
2022-08-08 13:57:08 +08:00
|
|
|
|
if (handleTypes & PendingAppInfo::UpdateAll) {
|
2022-07-14 19:01:56 +08:00
|
|
|
|
AppDBManager::getInstance()->handleDBItemUpdate(info.path());
|
|
|
|
|
}
|
2022-08-08 13:57:08 +08:00
|
|
|
|
if (handleTypes & PendingAppInfo::UpdateLocaleData) {
|
2022-07-12 11:26:03 +08:00
|
|
|
|
AppDBManager::getInstance()->handleLocaleDataUpdate(info.path());
|
|
|
|
|
}
|
2022-08-08 13:57:08 +08:00
|
|
|
|
if (handleTypes & PendingAppInfo::UpdateLaunchTimes) {
|
2022-07-14 19:01:56 +08:00
|
|
|
|
AppDBManager::getInstance()->handleLaunchTimesUpdate(info.path(), info.launchTimes());
|
2022-07-12 11:26:03 +08:00
|
|
|
|
}
|
2022-08-08 13:57:08 +08:00
|
|
|
|
if (handleTypes & PendingAppInfo::UpdateFavorites) {
|
2023-02-15 17:17:11 +08:00
|
|
|
|
AppDBManager::getInstance()->handleFavoritesStateUpdate(info.path(), info.favoritesState(), info.isFavoritePosChanged());
|
2022-07-12 11:26:03 +08:00
|
|
|
|
}
|
2022-08-08 13:57:08 +08:00
|
|
|
|
if (handleTypes & PendingAppInfo::UpdateTop) {
|
2023-02-15 17:17:11 +08:00
|
|
|
|
AppDBManager::getInstance()->handleTopStateUpdate(info.path(), info.topState(), info.isTopPosChanged());
|
2022-07-12 11:26:03 +08:00
|
|
|
|
}
|
2022-08-08 13:57:08 +08:00
|
|
|
|
if (handleTypes & PendingAppInfo::UpdateLock) {
|
2022-07-12 11:26:03 +08:00
|
|
|
|
AppDBManager::getInstance()->handleLockStateUpdate(info.path(), info.lockState());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (AppDBManager::getInstance()->startCommit()) {
|
|
|
|
|
Q_EMIT AppDBManager::getInstance()->finishHandleAppDB();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_pendingAppInfos.clear();
|
|
|
|
|
m_pendingAppInfos.squeeze();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|