排除dbus导致启动速度变慢问题

This commit is contained in:
hewenfei 2023-05-31 15:47:11 +08:00 committed by He Sir
parent fb2f286d39
commit 9290c71214
4 changed files with 128 additions and 168 deletions

View File

@ -27,6 +27,10 @@
#include <QDir> #include <QDir>
#include <QTranslator> #include <QTranslator>
#include <QCoreApplication> #include <QCoreApplication>
#include <QDBusConnection>
#include <QDBusMessage>
#include <QThread>
#include <QAbstractListModel>
#include "recent-file-extension.h" #include "recent-file-extension.h"
@ -39,6 +43,50 @@
namespace UkuiMenu { namespace UkuiMenu {
class RecentFileProvider : public QObject
{
Q_OBJECT
public:
explicit RecentFileProvider(QObject *parent = nullptr);
void dataProcess(QVector<RecentFile> &recentFiles);
public Q_SLOT:
void getRecentData();
void openFileByGFile(const QString &fileUrl);
Q_SIGNALS:
void dataLoadCompleted(QVector<RecentFile> recentFiles);
};
class RecentFilesModel : public QAbstractListModel
{
Q_OBJECT
public:
enum RoleMessage
{
UriRole = Qt::UserRole,
NameRole = Qt::UserRole + 1,
IconRole = Qt::UserRole + 2,
};
explicit RecentFilesModel(QObject *parent = nullptr);
int rowCount(const QModelIndex &parent) const override;
QVariant data(const QModelIndex &index, int role) const override;
QHash<int, QByteArray> roleNames() const override;
QString getInfoId(const int &index);
QStringList getAllInfoId();
Q_INVOKABLE void updateData();
public Q_SLOT:
void updateRecentFiles(QVector<RecentFile> recentFiles);
private:
QVector<RecentFile> m_recentFileData;
Q_SIGNALS:
void updateRecentData();
};
// GVFS 最近文件获取工具 // GVFS 最近文件获取工具
class GVFSRecentFileData class GVFSRecentFileData
{ {
@ -68,9 +116,9 @@ void GVFSRecentFileData::fileMonitor(RecentFileProvider *p_recentFileProvider)
{ {
GError *error = nullptr; GError *error = nullptr;
s_recentFileMonitor = g_file_monitor_directory(GVFSRecentFileData::s_recentFileRootDir, s_recentFileMonitor = g_file_monitor_directory(GVFSRecentFileData::s_recentFileRootDir,
G_FILE_MONITOR_NONE, G_FILE_MONITOR_NONE,
nullptr, nullptr,
&error); &error);
if (error) { if (error) {
qWarning() << "recentFile monitor creat error"; qWarning() << "recentFile monitor creat error";
@ -149,7 +197,8 @@ GVFSRecentFileData::enumerateFinish(GFile *file, GAsyncResult *res, RecentFilePr
} }
GAsyncReadyCallback GAsyncReadyCallback
GVFSRecentFileData::parseRecentFiles(GFileEnumerator *enumerator, GAsyncResult *res, RecentFileProvider *p_recentFileProvider) GVFSRecentFileData::parseRecentFiles(GFileEnumerator *enumerator, GAsyncResult *res,
RecentFileProvider *p_recentFileProvider)
{ {
GError *error = nullptr; GError *error = nullptr;
GList *fileList = g_file_enumerator_next_files_finish(enumerator, res, &error); GList *fileList = g_file_enumerator_next_files_finish(enumerator, res, &error);
@ -189,11 +238,11 @@ GVFSRecentFileData::parseRecentFiles(GFileEnumerator *enumerator, GAsyncResult *
GIcon *icon = g_file_info_get_icon(info); GIcon *icon = g_file_info_get_icon(info);
if (icon) { if (icon) {
const gchar* const *iconNames = g_themed_icon_get_names(G_THEMED_ICON(icon)); const gchar *const *iconNames = g_themed_icon_get_names(G_THEMED_ICON(icon));
if (iconNames) { if (iconNames) {
auto iconNameIterator = iconNames; auto iconNameIterator = iconNames;
while(*iconNameIterator) { while (*iconNameIterator) {
if(QIcon::hasThemeIcon(*iconNameIterator)) { if (QIcon::hasThemeIcon(*iconNameIterator)) {
recentFile.icon = "image://appicon/" + QString(*iconNameIterator); recentFile.icon = "image://appicon/" + QString(*iconNameIterator);
break; break;
} else { } else {
@ -229,15 +278,15 @@ RecentFileExtension::RecentFileExtension(QObject *parent) : MenuExtensionIFace(p
QCoreApplication::installTranslator(translator); QCoreApplication::installTranslator(translator);
} }
qRegisterMetaType<UkuiMenu::RecentFilesModel*>("RecentFilesModel*"); qRegisterMetaType<UkuiMenu::RecentFilesModel *>("RecentFilesModel*");
qRegisterMetaType<QVector<RecentFile> >("QVector<RecentFile>"); qRegisterMetaType<QVector<RecentFile> >("QVector<RecentFile>");
m_recentFilesProviderThread = new QThread(this); m_recentFilesProviderThread = new QThread(this);
m_recentFilesModel = new RecentFilesModel(this); m_recentFilesModel = new RecentFilesModel(this);
m_recentFileProvider = new RecentFileProvider(); m_recentFileProvider = new RecentFileProvider();
if ((m_recentFilesProviderThread == nullptr) || (m_recentFilesModel == nullptr) || (m_recentFileProvider == nullptr)) { if (!m_recentFilesProviderThread || !m_recentFilesModel || !m_recentFileProvider) {
qWarning() << "recentfile construction error" ; qWarning() << "recentfile construction error";
return; return;
} }
@ -245,12 +294,13 @@ RecentFileExtension::RecentFileExtension(QObject *parent) : MenuExtensionIFace(p
m_recentFileProvider->moveToThread(m_recentFilesProviderThread); m_recentFileProvider->moveToThread(m_recentFilesProviderThread);
connect(this, &RecentFileExtension::loadRecentFiles, m_recentFileProvider, &RecentFileProvider::getRecentData); connect(this, &RecentFileExtension::loadRecentFiles, m_recentFileProvider, &RecentFileProvider::getRecentData);
connect(m_recentFilesModel, &RecentFilesModel::updateRecentData, m_recentFileProvider, &RecentFileProvider::getRecentData); connect(m_recentFilesModel, &RecentFilesModel::updateRecentData, m_recentFileProvider,
connect(m_recentFileProvider, &RecentFileProvider::dataLoadCompleted, m_recentFilesModel, &RecentFilesModel::updateRecentFiles); &RecentFileProvider::getRecentData);
connect(m_recentFileProvider, &RecentFileProvider::dataLoadCompleted, m_recentFilesModel,
&RecentFilesModel::updateRecentFiles);
connect(this, &RecentFileExtension::openFileASync, m_recentFileProvider, &RecentFileProvider::openFileByGFile); connect(this, &RecentFileExtension::openFileASync, m_recentFileProvider, &RecentFileProvider::openFileByGFile);
m_data.insert("recentFilesModel", QVariant::fromValue(m_recentFilesModel)); m_data.insert("recentFilesModel", QVariant::fromValue(m_recentFilesModel));
initFileDbus();
GVFSRecentFileData::fileMonitor(m_recentFileProvider); GVFSRecentFileData::fileMonitor(m_recentFileProvider);
Q_EMIT loadRecentFiles(); Q_EMIT loadRecentFiles();
@ -305,40 +355,24 @@ void RecentFileExtension::receive(QVariantMap data)
creatMenu(path, index); creatMenu(path, index);
return; return;
} }
if (!openFile(path)) {
Q_EMIT openFileASync(path); openFile(path);
}
} }
void RecentFileExtension::initFileDbus() void RecentFileExtension::openFile(const QString &fileUrl)
{ {
m_appManagerDbusInterface = new QDBusInterface(KYLIN_APP_MANAGER_NAME, QDBusMessage message = QDBusMessage::createMethodCall(KYLIN_APP_MANAGER_NAME, KYLIN_APP_MANAGER_PATH, KYLIN_APP_MANAGER_INTERFACE, "LaunchDefaultAppWithUrl");
KYLIN_APP_MANAGER_PATH, message << fileUrl;
KYLIN_APP_MANAGER_INTERFACE,
QDBusConnection::sessionBus());
m_fileManagerDbusInterface = new QDBusInterface(FREEDESKTOP_FILEMANAGER_NAME, auto watcher = new QDBusPendingCallWatcher(QDBusConnection::sessionBus().asyncCall(message), this);
FREEDESKTOP_FILEMANAGER_PATH, connect(watcher, &QDBusPendingCallWatcher::finished, this, [fileUrl, this] (QDBusPendingCallWatcher *self) {
FREEDESKTOP_FILEMANAGER_INTERFACE, QDBusPendingReply<bool> reply = *self;
QDBusConnection::sessionBus()); if (reply.isError() || !reply.value()) {
Q_EMIT openFileASync(fileUrl);
}
if (!m_appManagerDbusInterface) { self->deleteLater();
qWarning() << "recentfile open failed: appmanager dbus does not exists."; });
}
if (!m_fileManagerDbusInterface) {
qWarning() << "recentfile directory open failed: filemanager dbus does not exists.";
}
}
bool RecentFileExtension::openFile(const QString &desktopFile)
{
if (m_appManagerDbusInterface != nullptr) {
QDBusReply<bool> status = m_appManagerDbusInterface->call("LaunchApp", desktopFile);
return status;
} else {
qWarning()<<"LaunchApp is failed,return false";
return false;
}
} }
void RecentFileExtension::creatMenu(const QString &path, const int &index) void RecentFileExtension::creatMenu(const QString &path, const int &index)
@ -351,28 +385,27 @@ void RecentFileExtension::creatMenu(const QString &path, const int &index)
QAction directory(tr("Open the directory where the file is located")); QAction directory(tr("Open the directory where the file is located"));
connect(&open, &QAction::triggered, this, [this, path]() { connect(&open, &QAction::triggered, this, [this, path]() {
if (!openFile(path)) { openFile(path);
Q_EMIT openFileASync(path); });
}
});
connect(&remove, &QAction::triggered, this, [this, index]() { connect(&remove, &QAction::triggered, this, [this, index]() {
GVFSRecentFileData::removeRecentFileByInfoId(m_recentFilesModel->getInfoId(index)); GVFSRecentFileData::removeRecentFileByInfoId(m_recentFilesModel->getInfoId(index));
}); });
connect(&clear, &QAction::triggered, this, [this]() { connect(&clear, &QAction::triggered, this, [this]() {
QStringList infoIdList = m_recentFilesModel->getAllInfoId(); QStringList infoIdList = m_recentFilesModel->getAllInfoId();
for (const QString &infoId : infoIdList) { for (const QString &infoId : infoIdList) {
GVFSRecentFileData::removeRecentFileByInfoId(infoId); GVFSRecentFileData::removeRecentFileByInfoId(infoId);
} }
}); });
connect(&directory, &QAction::triggered, this, [this, path]() { connect(&directory, &QAction::triggered, this, [this, path]() {
if (!m_fileManagerDbusInterface) { return; } QDBusMessage message = QDBusMessage::createMethodCall(FREEDESKTOP_FILEMANAGER_NAME,
QStringList pathList; FREEDESKTOP_FILEMANAGER_PATH,
pathList.append(path); FREEDESKTOP_FILEMANAGER_INTERFACE, "ShowFolders");
m_fileManagerDbusInterface->call("ShowFolders", pathList, "arg"); message << QStringList(path);
}); QDBusConnection::sessionBus().asyncCall(message);
});
menu.addAction(&open); menu.addAction(&open);
menu.addSeparator(); menu.addSeparator();
@ -468,19 +501,16 @@ void RecentFileProvider::getRecentData()
GVFSRecentFileData::loadRecentFileASync(this); GVFSRecentFileData::loadRecentFileASync(this);
} }
void RecentFileProvider::openFileByGFile(const QString &path) void RecentFileProvider::openFileByGFile(const QString &fileUrl)
{ {
GFile *file = g_file_new_for_uri(path.toUtf8().constData()); GFile *file = g_file_new_for_uri(fileUrl.toUtf8().constData());
if (!file) { if (!file) {
return; return;
} }
GFileInfo *fileInfo = g_file_query_info(file, GFileInfo *fileInfo = g_file_query_info(file, "standard::*," G_FILE_ATTRIBUTE_ID_FILE, G_FILE_QUERY_INFO_NONE, nullptr, nullptr);
"standard::*," "time::*," "access::*," "mountable::*," "metadata::*," "trash::*," G_FILE_ATTRIBUTE_ID_FILE,
G_FILE_QUERY_INFO_NONE,
nullptr,
nullptr);
if (!fileInfo) { if (!fileInfo) {
g_object_unref(file);
return; return;
} }
@ -492,62 +522,49 @@ void RecentFileProvider::openFileByGFile(const QString &path)
} }
GError *error = NULL; GError *error = NULL;
GAppInfo *info = NULL; GAppInfo *info = NULL;
QString mimeAppsListPath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + "/.config/mimeapps.list"; QString mimeAppsListPath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + "/.config/mimeapps.list";
GKeyFile *keyfile = g_key_file_new(); GKeyFile *keyfile = g_key_file_new();
gboolean ret = g_key_file_load_from_file(keyfile, mimeAppsListPath.toUtf8(), G_KEY_FILE_NONE, &error); gboolean ret = g_key_file_load_from_file(keyfile, mimeAppsListPath.toUtf8(), G_KEY_FILE_NONE, &error);
if (!ret) { if (!ret) {
qWarning()<< "load mimeapps list error msg" << error->message; qWarning() << "load mimeapps list error msg" << error->message;
info = g_app_info_get_default_for_type(mimeType.toUtf8().constData(), false); info = g_app_info_get_default_for_type(mimeType.toUtf8().constData(), false);
g_error_free(error); g_error_free(error);
}
else { } else {
gchar *desktopApp = g_key_file_get_string(keyfile, "Default Applications", mimeType.toUtf8(), &error); gchar *desktopApp = g_key_file_get_string(keyfile, "Default Applications", mimeType.toUtf8(), &error);
if (NULL != desktopApp) { if (NULL == desktopApp) {
info = (GAppInfo*)g_desktop_app_info_new(desktopApp);
g_free (desktopApp);
}
else {
info = g_app_info_get_default_for_type(mimeType.toUtf8().constData(), false); info = g_app_info_get_default_for_type(mimeType.toUtf8().constData(), false);
} else {
info = (GAppInfo *) g_desktop_app_info_new(desktopApp);
g_free(desktopApp);
} }
} }
g_key_file_free (keyfile); g_key_file_free(keyfile);
if(G_IS_APP_INFO(info)) { bool success = false;
bool isSuccess(false); if (G_IS_APP_INFO(info)) {
QDBusInterface * appLaunchInterface = new QDBusInterface(KYLIN_APP_MANAGER_NAME, GList *files = g_list_alloc();
KYLIN_APP_MANAGER_PATH, g_list_append(files, file);
KYLIN_APP_MANAGER_INTERFACE,
QDBusConnection::sessionBus()); success = g_app_info_launch(info, files, nullptr, nullptr);
if(!appLaunchInterface->isValid()) {
qWarning() << qPrintable(QDBusConnection::sessionBus().lastError().message()); g_list_free(files);
isSuccess = false;
}
else {
appLaunchInterface->setTimeout(10000);
QDBusReply<bool> reply = appLaunchInterface->call("LaunchDefaultAppWithUrl", path);
if(reply.isValid()) {
isSuccess = reply;
}
else {
qWarning() << "recentfile used appmanager dbus called failed!";
isSuccess = false;
}
}
if(appLaunchInterface) {
delete appLaunchInterface;
}
appLaunchInterface = NULL;
if (!isSuccess){
QDesktopServices::openUrl(path);
}
} }
if (!success) {
QDesktopServices::openUrl(fileUrl);
}
g_object_unref(file);
g_object_unref(info); g_object_unref(info);
} }
} // UkuiMenu } // UkuiMenu
#include "recent-file-extension.moc"

View File

@ -19,14 +19,15 @@
#ifndef UKUI_MENU_RECENT_FILE_EXTENSION_H #ifndef UKUI_MENU_RECENT_FILE_EXTENSION_H
#define UKUI_MENU_RECENT_FILE_EXTENSION_H #define UKUI_MENU_RECENT_FILE_EXTENSION_H
#include <QThread> class QThread;
#include <QDBusInterface>
#include <QAbstractListModel>
#include "menu-extension-iface.h" #include "menu-extension-iface.h"
namespace UkuiMenu { namespace UkuiMenu {
class RecentFilesModel;
class RecentFileProvider;
class RecentFile class RecentFile
{ {
public: public:
@ -37,52 +38,6 @@ public:
QString infoId; QString infoId;
}; };
class RecentFileProvider : public QObject
{
Q_OBJECT
public:
explicit RecentFileProvider(QObject *parent = nullptr);
void dataProcess(QVector<RecentFile> &recentFiles);
public Q_SLOT:
void getRecentData();
void openFileByGFile(const QString &path);
Q_SIGNALS:
void dataLoadCompleted(QVector<RecentFile> recentFiles);
};
class RecentFilesModel : public QAbstractListModel
{
Q_OBJECT
public:
enum RoleMessage {
UriRole = Qt::UserRole,
NameRole = Qt::UserRole + 1,
IconRole = Qt::UserRole + 2,
};
explicit RecentFilesModel(QObject *parent = nullptr);
int rowCount(const QModelIndex &parent) const override;
QVariant data(const QModelIndex &index, int role) const override;
QHash<int, QByteArray> roleNames() const override;
QString getInfoId(const int &index);
QStringList getAllInfoId();
Q_INVOKABLE void updateData();
public Q_SLOT:
void updateRecentFiles(QVector<RecentFile> recentFiles);
private:
QVector<RecentFile> m_recentFileData;
Q_SIGNALS:
void updateRecentData();
};
class RecentFileExtension : public MenuExtensionIFace class RecentFileExtension : public MenuExtensionIFace
{ {
Q_OBJECT Q_OBJECT
@ -100,14 +55,11 @@ public:
private: private:
QVector<RecentFile> m_recentFile; QVector<RecentFile> m_recentFile;
QVariantMap m_data; QVariantMap m_data;
QDBusInterface *m_appManagerDbusInterface = nullptr;
RecentFilesModel *m_recentFilesModel = nullptr; RecentFilesModel *m_recentFilesModel = nullptr;
QThread *m_recentFilesProviderThread = nullptr; QThread *m_recentFilesProviderThread = nullptr;
RecentFileProvider *m_recentFileProvider = nullptr; RecentFileProvider *m_recentFileProvider = nullptr;
QDBusInterface *m_fileManagerDbusInterface = nullptr;
void initFileDbus(); void openFile(const QString& fileUrl);
bool openFile(const QString& desktopFile);
void creatMenu(const QString &path, const int &index); void creatMenu(const QString &path, const int &index);
Q_SIGNALS: Q_SIGNALS:

View File

@ -22,7 +22,8 @@
#include <QDebug> #include <QDebug>
#include <QDBusReply> #include <QDBusReply>
#include <QDBusInterface> #include <QDBusConnection>
#include <QDBusMessage>
#include <QProcess> #include <QProcess>
#define KYLIN_APP_MANAGER_NAME "com.kylin.AppManager" #define KYLIN_APP_MANAGER_NAME "com.kylin.AppManager"
@ -38,13 +39,7 @@ AppManager *AppManager::instance()
AppManager::AppManager(QObject *parent) : QObject(parent) AppManager::AppManager(QObject *parent) : QObject(parent)
{ {
m_appManagerDbusInterface = new QDBusInterface(KYLIN_APP_MANAGER_NAME,
KYLIN_APP_MANAGER_PATH,
KYLIN_APP_MANAGER_INTERFACE,
QDBusConnection::sessionBus());
if (!m_appManagerDbusInterface) {
qWarning() << "appManager dbus does not exists.";
}
} }
bool AppManager::launchApp(const QString &desktopFilePath) bool AppManager::launchApp(const QString &desktopFilePath)
@ -62,7 +57,7 @@ bool AppManager::launchApp(const QString &desktopFilePath)
return false; return false;
} }
return QProcess::startDetached(cmd); return QProcess::startDetached(cmd, QStringList());
} }
bool AppManager::launchBinaryApp(const QString &app, const QString &args) bool AppManager::launchBinaryApp(const QString &app, const QString &args)
@ -77,16 +72,15 @@ bool AppManager::launchBinaryApp(const QString &app, const QString &args)
return true; return true;
} }
return QProcess::startDetached(cmd); return QProcess::startDetached(app, {args});
} }
bool AppManager::launchAppWithDBus(const QString &app) bool AppManager::launchAppWithDBus(const QString &app)
{ {
if (!m_appManagerDbusInterface) { QDBusMessage message = QDBusMessage::createMethodCall(KYLIN_APP_MANAGER_NAME, KYLIN_APP_MANAGER_PATH, KYLIN_APP_MANAGER_INTERFACE, "LaunchApp");
return false; message << app;
}
QDBusReply<bool> status = m_appManagerDbusInterface->call("LaunchApp", app); QDBusReply<bool> status(QDBusConnection::sessionBus().call(message));
return status.value(); return status.value();
} }

View File

@ -41,9 +41,6 @@ private:
bool launchAppWithDBus(const QString &app); bool launchAppWithDBus(const QString &app);
static QString parseDesktopFile(const QString &desktopFilePath); static QString parseDesktopFile(const QString &desktopFilePath);
private:
QDBusInterface *m_appManagerDbusInterface = nullptr;
Q_SIGNALS: Q_SIGNALS:
void request(UkuiMenuApplication::Command command); void request(UkuiMenuApplication::Command command);
}; };