修复QSqlDatabasePrivate::removeDatabase警告;修复部分内存泄露;修复QDir的entryInfoList方法在查找不存在目录时小概率崩溃的问题;

This commit is contained in:
jixiaoxu 2022-10-19 17:11:05 +08:00
parent f1a9a65f92
commit 2b01ee0e80
9 changed files with 85 additions and 62 deletions

View File

@ -67,7 +67,7 @@ private:
QDBusInterface *m_appDBInterface = nullptr; QDBusInterface *m_appDBInterface = nullptr;
AppInfoTable *q = nullptr; AppInfoTable *q = nullptr;
QSqlDatabase m_database; QSqlDatabase *m_database = nullptr;
QString m_ConnectionName; QString m_ConnectionName;
public Q_SLOTS: public Q_SLOTS:

View File

@ -19,7 +19,7 @@
using namespace UkuiSearch; using namespace UkuiSearch;
AppInfoTablePrivate::AppInfoTablePrivate(AppInfoTable *parent) : QObject(parent), q(parent), m_database(QSqlDatabase()) AppInfoTablePrivate::AppInfoTablePrivate(AppInfoTable *parent) : QObject(parent), q(parent), m_database(new QSqlDatabase())
{ {
//dbus接收数据库信号 //dbus接收数据库信号
@ -55,7 +55,7 @@ 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();
} }
} }
@ -224,8 +224,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")
@ -240,12 +240,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 {
@ -258,8 +258,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"
@ -285,12 +285,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 {
@ -344,26 +344,26 @@ bool AppInfoTablePrivate::uninstallApp(QString &desktopfp)
QString AppInfoTablePrivate::lastError() const QString AppInfoTablePrivate::lastError() const
{ {
return m_database.lastError().text(); return m_database->lastError().text();
} }
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 {
@ -376,8 +376,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()) {
@ -387,7 +387,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 {
@ -398,9 +398,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 {
@ -413,19 +413,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 {
@ -438,7 +438,7 @@ bool AppInfoTablePrivate::setAppLockState(QString &desktopfp, size_t num)
bool AppInfoTablePrivate::getAllAppDesktopList(QStringList &list) bool AppInfoTablePrivate::getAllAppDesktopList(QStringList &list)
{ {
bool res(true); bool res(true);
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)) {
@ -446,7 +446,7 @@ bool AppInfoTablePrivate::getAllAppDesktopList(QStringList &list)
list.append(sql.value(0).toString()); list.append(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;
} }
@ -456,14 +456,14 @@ bool AppInfoTablePrivate::getAllAppDesktopList(QStringList &list)
bool AppInfoTablePrivate::getFavoritesAppList(QStringList &list) bool AppInfoTablePrivate::getFavoritesAppList(QStringList &list)
{ {
bool res(true); bool res(true);
QSqlQuery sql(m_database); QSqlQuery sql(*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");
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 { } 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;
} }
return res; return res;
@ -472,7 +472,7 @@ bool AppInfoTablePrivate::getFavoritesAppList(QStringList &list)
bool AppInfoTablePrivate::getTopAppList(QStringList &list) bool AppInfoTablePrivate::getTopAppList(QStringList &list)
{ {
bool res(true); bool res(true);
QSqlQuery sql(m_database); QSqlQuery sql(*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");
if (sql.exec(cmd)) { if (sql.exec(cmd)) {
@ -480,7 +480,7 @@ bool AppInfoTablePrivate::getTopAppList(QStringList &list)
list.append(sql.value(0).toString()); list.append(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;
} }
@ -490,9 +490,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)) {
@ -502,18 +502,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 {
@ -526,13 +526,19 @@ bool AppInfoTablePrivate::getLaunchTimesAppList(QStringList &list)
AppInfoTablePrivate::~AppInfoTablePrivate() AppInfoTablePrivate::~AppInfoTablePrivate()
{ {
this->closeDataBase(); this->closeDataBase();
if (m_signalTransInterface)
delete m_signalTransInterface;
m_signalTransInterface = nullptr;
if (m_appDBInterface)
delete m_appDBInterface;
m_appDBInterface = nullptr;
} }
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();
} }
@ -541,10 +547,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;
@ -552,8 +558,9 @@ bool AppInfoTablePrivate::openDataBase()
void AppInfoTablePrivate::closeDataBase() void AppInfoTablePrivate::closeDataBase()
{ {
m_database.close(); m_database->close();
// delete m_database; delete m_database;
m_database = nullptr;
QSqlDatabase::removeDatabase(m_ConnectionName); QSqlDatabase::removeDatabase(m_ConnectionName);
} }

View File

@ -333,16 +333,14 @@ bool AppSearchPlugin::installAppAction(const QString & name) {
"/com/kylin/softwarecenter", "/com/kylin/softwarecenter",
"com.kylin.utiliface", "com.kylin.utiliface",
QDBusConnection::sessionBus()); QDBusConnection::sessionBus());
bool res(false);
if(interface->isValid()) { if(interface->isValid()) {
//软件商店已打开,直接跳转 //软件商店已打开,直接跳转
interface->call("show_search_result", name); interface->call("show_search_result", name);
bool reply = QDBusReply<bool>(interface->call(QString("show_search_result"), name)); res = QDBusReply<bool>(interface->call(QString("show_search_result"), name));
return reply;
} else { } else {
//软件商店未打开,打开软件商店下载此软件 //软件商店未打开,打开软件商店下载此软件
qDebug() << "Softwarecenter has not been launched, now launch it." << name; qDebug() << "Softwarecenter has not been launched, now launch it." << name;
bool res(false);
QDBusInterface * appLaunchInterface = new QDBusInterface("com.kylin.AppManager", QDBusInterface * appLaunchInterface = new QDBusInterface("com.kylin.AppManager",
"/com/kylin/AppManager", "/com/kylin/AppManager",
"com.kylin.AppManager", "com.kylin.AppManager",
@ -363,13 +361,18 @@ bool AppSearchPlugin::installAppAction(const QString & name) {
if(appLaunchInterface) { if(appLaunchInterface) {
delete appLaunchInterface; delete appLaunchInterface;
} }
appLaunchInterface = NULL; appLaunchInterface = nullptr;
if (res) if (!res) {
return true;
QProcess process; QProcess process;
return process.startDetached(QString("kylin-software-center -find %1").arg(name)); res = process.startDetached(QString("kylin-software-center -find %1").arg(name));
} }
} }
if(interface) {
delete interface;
}
interface = nullptr;
return res;
}
//AppSearch::AppSearch(DataQueue<SearchPluginIface::ResultInfo> *searchResult, DataQueue<ResultItem>* appSearchResults, UkuiSearchTask *appSearchTask, QString keyword, size_t uniqueSymbol) //AppSearch::AppSearch(DataQueue<SearchPluginIface::ResultInfo> *searchResult, DataQueue<ResultItem>* appSearchResults, UkuiSearchTask *appSearchTask, QString keyword, size_t uniqueSymbol)
//{ //{

View File

@ -25,6 +25,13 @@ DirWatcher::DirWatcher(QObject *parent)
} }
} }
DirWatcher::~DirWatcher()
{
if (m_dbusInterface)
delete m_dbusInterface;
m_dbusInterface = nullptr;
}
DirWatcher *DirWatcher::getDirWatcher() DirWatcher *DirWatcher::getDirWatcher()
{ {
std::call_once(flag, [ & ] { std::call_once(flag, [ & ] {

View File

@ -31,7 +31,7 @@ Q_SIGNALS:
void removeIndexItem(const QString&); void removeIndexItem(const QString&);
private: private:
DirWatcher(QObject *parent = nullptr); DirWatcher(QObject *parent = nullptr);
~DirWatcher() = default; ~DirWatcher();
QDBusInterface *m_dbusInterface = nullptr; QDBusInterface *m_dbusInterface = nullptr;

View File

@ -31,6 +31,7 @@ UkuiSearchQDBus::UkuiSearchQDBus() {
} }
} }
UkuiSearchQDBus::~UkuiSearchQDBus() { UkuiSearchQDBus::~UkuiSearchQDBus() {
if (this->tmpSystemQDBusInterface)
delete this->tmpSystemQDBusInterface; delete this->tmpSystemQDBusInterface;
this->tmpSystemQDBusInterface = nullptr; this->tmpSystemQDBusInterface = nullptr;
} }

View File

@ -143,7 +143,9 @@ void AppSearchWorker::run()
AppSearchWorker::~AppSearchWorker() AppSearchWorker::~AppSearchWorker()
{ {
if (m_interFace)
delete m_interFace;
m_interFace = nullptr;
} }
void AppSearchWorker::sendErrorMsg(const QString &msg) void AppSearchWorker::sendErrorMsg(const QString &msg)

View File

@ -197,6 +197,8 @@ bool FileSearchWorker::directSearch()
while (!searchPathQueue.empty()) { while (!searchPathQueue.empty()) {
dir.setPath(searchPathQueue.dequeue()); dir.setPath(searchPathQueue.dequeue());
if (!dir.exists())
continue;
infoList = dir.entryInfoList(); infoList = dir.entryInfoList();
for (const auto &fileInfo : infoList) { for (const auto &fileInfo : infoList) {

View File

@ -42,6 +42,7 @@ DirWatcher::~DirWatcher()
if(m_qSettings){ if(m_qSettings){
delete m_qSettings; delete m_qSettings;
} }
m_qSettings = nullptr;
} }
DirWatcher *DirWatcher::getDirWatcher() DirWatcher *DirWatcher::getDirWatcher()