排除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 <QTranslator>
#include <QCoreApplication>
#include <QDBusConnection>
#include <QDBusMessage>
#include <QThread>
#include <QAbstractListModel>
#include "recent-file-extension.h"
@ -39,6 +43,50 @@
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 最近文件获取工具
class GVFSRecentFileData
{
@ -68,9 +116,9 @@ void GVFSRecentFileData::fileMonitor(RecentFileProvider *p_recentFileProvider)
{
GError *error = nullptr;
s_recentFileMonitor = g_file_monitor_directory(GVFSRecentFileData::s_recentFileRootDir,
G_FILE_MONITOR_NONE,
nullptr,
&error);
G_FILE_MONITOR_NONE,
nullptr,
&error);
if (error) {
qWarning() << "recentFile monitor creat error";
@ -149,7 +197,8 @@ GVFSRecentFileData::enumerateFinish(GFile *file, GAsyncResult *res, RecentFilePr
}
GAsyncReadyCallback
GVFSRecentFileData::parseRecentFiles(GFileEnumerator *enumerator, GAsyncResult *res, RecentFileProvider *p_recentFileProvider)
GVFSRecentFileData::parseRecentFiles(GFileEnumerator *enumerator, GAsyncResult *res,
RecentFileProvider *p_recentFileProvider)
{
GError *error = nullptr;
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);
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) {
auto iconNameIterator = iconNames;
while(*iconNameIterator) {
if(QIcon::hasThemeIcon(*iconNameIterator)) {
while (*iconNameIterator) {
if (QIcon::hasThemeIcon(*iconNameIterator)) {
recentFile.icon = "image://appicon/" + QString(*iconNameIterator);
break;
} else {
@ -229,15 +278,15 @@ RecentFileExtension::RecentFileExtension(QObject *parent) : MenuExtensionIFace(p
QCoreApplication::installTranslator(translator);
}
qRegisterMetaType<UkuiMenu::RecentFilesModel*>("RecentFilesModel*");
qRegisterMetaType<UkuiMenu::RecentFilesModel *>("RecentFilesModel*");
qRegisterMetaType<QVector<RecentFile> >("QVector<RecentFile>");
m_recentFilesProviderThread = new QThread(this);
m_recentFilesModel = new RecentFilesModel(this);
m_recentFileProvider = new RecentFileProvider();
if ((m_recentFilesProviderThread == nullptr) || (m_recentFilesModel == nullptr) || (m_recentFileProvider == nullptr)) {
qWarning() << "recentfile construction error" ;
if (!m_recentFilesProviderThread || !m_recentFilesModel || !m_recentFileProvider) {
qWarning() << "recentfile construction error";
return;
}
@ -245,12 +294,13 @@ RecentFileExtension::RecentFileExtension(QObject *parent) : MenuExtensionIFace(p
m_recentFileProvider->moveToThread(m_recentFilesProviderThread);
connect(this, &RecentFileExtension::loadRecentFiles, m_recentFileProvider, &RecentFileProvider::getRecentData);
connect(m_recentFilesModel, &RecentFilesModel::updateRecentData, m_recentFileProvider, &RecentFileProvider::getRecentData);
connect(m_recentFileProvider, &RecentFileProvider::dataLoadCompleted, m_recentFilesModel, &RecentFilesModel::updateRecentFiles);
connect(m_recentFilesModel, &RecentFilesModel::updateRecentData, m_recentFileProvider,
&RecentFileProvider::getRecentData);
connect(m_recentFileProvider, &RecentFileProvider::dataLoadCompleted, m_recentFilesModel,
&RecentFilesModel::updateRecentFiles);
connect(this, &RecentFileExtension::openFileASync, m_recentFileProvider, &RecentFileProvider::openFileByGFile);
m_data.insert("recentFilesModel", QVariant::fromValue(m_recentFilesModel));
initFileDbus();
GVFSRecentFileData::fileMonitor(m_recentFileProvider);
Q_EMIT loadRecentFiles();
@ -305,40 +355,24 @@ void RecentFileExtension::receive(QVariantMap data)
creatMenu(path, index);
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,
KYLIN_APP_MANAGER_PATH,
KYLIN_APP_MANAGER_INTERFACE,
QDBusConnection::sessionBus());
QDBusMessage message = QDBusMessage::createMethodCall(KYLIN_APP_MANAGER_NAME, KYLIN_APP_MANAGER_PATH, KYLIN_APP_MANAGER_INTERFACE, "LaunchDefaultAppWithUrl");
message << fileUrl;
m_fileManagerDbusInterface = new QDBusInterface(FREEDESKTOP_FILEMANAGER_NAME,
FREEDESKTOP_FILEMANAGER_PATH,
FREEDESKTOP_FILEMANAGER_INTERFACE,
QDBusConnection::sessionBus());
auto watcher = new QDBusPendingCallWatcher(QDBusConnection::sessionBus().asyncCall(message), this);
connect(watcher, &QDBusPendingCallWatcher::finished, this, [fileUrl, this] (QDBusPendingCallWatcher *self) {
QDBusPendingReply<bool> reply = *self;
if (reply.isError() || !reply.value()) {
Q_EMIT openFileASync(fileUrl);
}
if (!m_appManagerDbusInterface) {
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;
}
self->deleteLater();
});
}
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"));
connect(&open, &QAction::triggered, this, [this, path]() {
if (!openFile(path)) {
Q_EMIT openFileASync(path);
}
});
openFile(path);
});
connect(&remove, &QAction::triggered, this, [this, index]() {
GVFSRecentFileData::removeRecentFileByInfoId(m_recentFilesModel->getInfoId(index));
});
});
connect(&clear, &QAction::triggered, this, [this]() {
QStringList infoIdList = m_recentFilesModel->getAllInfoId();
for (const QString &infoId : infoIdList) {
GVFSRecentFileData::removeRecentFileByInfoId(infoId);
}
});
});
connect(&directory, &QAction::triggered, this, [this, path]() {
if (!m_fileManagerDbusInterface) { return; }
QStringList pathList;
pathList.append(path);
m_fileManagerDbusInterface->call("ShowFolders", pathList, "arg");
});
QDBusMessage message = QDBusMessage::createMethodCall(FREEDESKTOP_FILEMANAGER_NAME,
FREEDESKTOP_FILEMANAGER_PATH,
FREEDESKTOP_FILEMANAGER_INTERFACE, "ShowFolders");
message << QStringList(path);
QDBusConnection::sessionBus().asyncCall(message);
});
menu.addAction(&open);
menu.addSeparator();
@ -468,19 +501,16 @@ void RecentFileProvider::getRecentData()
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) {
return;
}
GFileInfo *fileInfo = g_file_query_info(file,
"standard::*," "time::*," "access::*," "mountable::*," "metadata::*," "trash::*," G_FILE_ATTRIBUTE_ID_FILE,
G_FILE_QUERY_INFO_NONE,
nullptr,
nullptr);
GFileInfo *fileInfo = g_file_query_info(file, "standard::*," G_FILE_ATTRIBUTE_ID_FILE, G_FILE_QUERY_INFO_NONE, nullptr, nullptr);
if (!fileInfo) {
g_object_unref(file);
return;
}
@ -492,62 +522,49 @@ void RecentFileProvider::openFileByGFile(const QString &path)
}
GError *error = NULL;
GAppInfo *info = NULL;
GAppInfo *info = NULL;
QString mimeAppsListPath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + "/.config/mimeapps.list";
GKeyFile *keyfile = g_key_file_new();
gboolean ret = g_key_file_load_from_file(keyfile, mimeAppsListPath.toUtf8(), G_KEY_FILE_NONE, &error);
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);
g_error_free(error);
}
else {
} else {
gchar *desktopApp = g_key_file_get_string(keyfile, "Default Applications", mimeType.toUtf8(), &error);
if (NULL != desktopApp) {
info = (GAppInfo*)g_desktop_app_info_new(desktopApp);
g_free (desktopApp);
}
else {
if (NULL == desktopApp) {
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 isSuccess(false);
QDBusInterface * appLaunchInterface = new QDBusInterface(KYLIN_APP_MANAGER_NAME,
KYLIN_APP_MANAGER_PATH,
KYLIN_APP_MANAGER_INTERFACE,
QDBusConnection::sessionBus());
if(!appLaunchInterface->isValid()) {
qWarning() << qPrintable(QDBusConnection::sessionBus().lastError().message());
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);
}
bool success = false;
if (G_IS_APP_INFO(info)) {
GList *files = g_list_alloc();
g_list_append(files, file);
success = g_app_info_launch(info, files, nullptr, nullptr);
g_list_free(files);
}
if (!success) {
QDesktopServices::openUrl(fileUrl);
}
g_object_unref(file);
g_object_unref(info);
}
} // UkuiMenu
#include "recent-file-extension.moc"

View File

@ -19,14 +19,15 @@
#ifndef UKUI_MENU_RECENT_FILE_EXTENSION_H
#define UKUI_MENU_RECENT_FILE_EXTENSION_H
#include <QThread>
#include <QDBusInterface>
#include <QAbstractListModel>
class QThread;
#include "menu-extension-iface.h"
namespace UkuiMenu {
class RecentFilesModel;
class RecentFileProvider;
class RecentFile
{
public:
@ -37,52 +38,6 @@ public:
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
{
Q_OBJECT
@ -100,14 +55,11 @@ public:
private:
QVector<RecentFile> m_recentFile;
QVariantMap m_data;
QDBusInterface *m_appManagerDbusInterface = nullptr;
RecentFilesModel *m_recentFilesModel = nullptr;
QThread *m_recentFilesProviderThread = nullptr;
RecentFileProvider *m_recentFileProvider = nullptr;
QDBusInterface *m_fileManagerDbusInterface = nullptr;
void initFileDbus();
bool openFile(const QString& desktopFile);
void openFile(const QString& fileUrl);
void creatMenu(const QString &path, const int &index);
Q_SIGNALS:

View File

@ -22,7 +22,8 @@
#include <QDebug>
#include <QDBusReply>
#include <QDBusInterface>
#include <QDBusConnection>
#include <QDBusMessage>
#include <QProcess>
#define KYLIN_APP_MANAGER_NAME "com.kylin.AppManager"
@ -38,13 +39,7 @@ AppManager *AppManager::instance()
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)
@ -62,7 +57,7 @@ bool AppManager::launchApp(const QString &desktopFilePath)
return false;
}
return QProcess::startDetached(cmd);
return QProcess::startDetached(cmd, QStringList());
}
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 QProcess::startDetached(cmd);
return QProcess::startDetached(app, {args});
}
bool AppManager::launchAppWithDBus(const QString &app)
{
if (!m_appManagerDbusInterface) {
return false;
}
QDBusMessage message = QDBusMessage::createMethodCall(KYLIN_APP_MANAGER_NAME, KYLIN_APP_MANAGER_PATH, KYLIN_APP_MANAGER_INTERFACE, "LaunchApp");
message << app;
QDBusReply<bool> status = m_appManagerDbusInterface->call("LaunchApp", app);
QDBusReply<bool> status(QDBusConnection::sessionBus().call(message));
return status.value();
}

View File

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