From c5e4a06c6c7f0749a03722e6d76f20da528a0d15 Mon Sep 17 00:00:00 2001 From: houchengqiu Date: Fri, 24 Nov 2023 10:37:20 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=80=82=E9=85=8D=E5=9B=BE=E5=83=8F?= =?UTF-8?q?=E5=88=86=E7=B1=BBDBus=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 适配图像分类DBus接口 Log: 适配图像分类DBus接口 --- src/album/albumview/albumview.cpp | 2 +- src/album/albumview/leftlistview.cpp | 6 +-- src/album/imageengine/imageenginethread.cpp | 2 +- src/album/utils/classifyutils.cpp | 21 +++++---- src/album/utils/classifyutils.h | 52 +++++++++++++++++++-- 5 files changed, 67 insertions(+), 16 deletions(-) diff --git a/src/album/albumview/albumview.cpp b/src/album/albumview/albumview.cpp index cef50d64..34b80119 100644 --- a/src/album/albumview/albumview.cpp +++ b/src/album/albumview/albumview.cpp @@ -471,7 +471,7 @@ void AlbumView::initRightView() //初始化最近删除列表 initTrashWidget(); //初始化图片分类列表 - if (Classifyutils::GetInstance()->isLoaded()) { + if (Classifyutils::GetInstance()->isDBusExist()) { initClassWidget(); } else { m_pClassWidget = new DWidget(); diff --git a/src/album/albumview/leftlistview.cpp b/src/album/albumview/leftlistview.cpp index 42f03393..65aa8bd1 100644 --- a/src/album/albumview/leftlistview.cpp +++ b/src/album/albumview/leftlistview.cpp @@ -117,7 +117,7 @@ void LeftListView::initUI() m_pPhotoLibListView->setItemDelegate(itemDelegate0); m_pPhotoLibListView->setFixedWidth(LEFT_VIEW_WIDTH_180); - if (Classifyutils::GetInstance()->isLoaded()) + if (Classifyutils::GetInstance()->isDBusExist()) m_pPhotoLibListView->setFixedHeight(160); else m_pPhotoLibListView->setFixedHeight(120); @@ -145,7 +145,7 @@ void LeftListView::initUI() m_pPhotoLibListView->setItemWidget(pListWidgetItem2, pAlbumLeftTabItem2); // 图片分类 - if (Classifyutils::GetInstance()->isLoaded()) { + if (Classifyutils::GetInstance()->isDBusExist()) { QListWidgetItem *pListWidgetItem3 = new QListWidgetItem(m_pPhotoLibListView); pListWidgetItem3->setSizeHint(QSize(LEFT_VIEW_LISTITEM_WIDTH_160, LEFT_VIEW_LISTITEM_HEIGHT_40)); AlbumLeftTabItem *pAlbumLeftTabItem3 = new AlbumLeftTabItem(COMMON_STR_CLASS, -1); @@ -290,7 +290,7 @@ void LeftListView::updatePhotoListView() m_pPhotoLibListView->setItemWidget(pListWidgetItem2, pAlbumLeftTabItem2); // 图片分类 - if (Classifyutils::GetInstance()->isLoaded()) { + if (Classifyutils::GetInstance()->isDBusExist()) { QListWidgetItem *pListWidgetItem3 = new QListWidgetItem(m_pPhotoLibListView); pListWidgetItem3->setSizeHint(QSize(LEFT_VIEW_LISTITEM_WIDTH_160, LEFT_VIEW_LISTITEM_HEIGHT_40)); diff --git a/src/album/imageengine/imageenginethread.cpp b/src/album/imageengine/imageenginethread.cpp index d3ee0589..e41fa85b 100644 --- a/src/album/imageengine/imageenginethread.cpp +++ b/src/album/imageengine/imageenginethread.cpp @@ -57,7 +57,7 @@ DBImgInfo getDBInfo(const QString &srcpath, bool isVideo) auto mds = getAllMetaData(srcpath); QString value = mds.value("DateTimeOriginal"); dbi.itemType = ItemTypePic; - if (Classifyutils::GetInstance()->isLoaded()) { + if (Classifyutils::GetInstance()->isDBusExist()) { if (srcfi.exists() && utils::base::isSupportClassify(srcpath)) dbi.className = Classifyutils::GetInstance()->imageClassify(srcpath.toStdString().c_str()); else diff --git a/src/album/utils/classifyutils.cpp b/src/album/utils/classifyutils.cpp index e5f15f57..1c1c1341 100644 --- a/src/album/utils/classifyutils.cpp +++ b/src/album/utils/classifyutils.cpp @@ -6,6 +6,7 @@ #include #include +const QString ImageClassifyDBusServicePath = "/usr/share/dbus-1/system-services/com.deepin.imageclassify.service"; Classifyutils *Classifyutils::m_pInstance = nullptr; Classifyutils *Classifyutils::GetInstance() { @@ -17,22 +18,26 @@ Classifyutils *Classifyutils::GetInstance() QString Classifyutils::imageClassify(const QString &path) { - if (!imageClassifyFunc) + if (!isDBusExist()) return ""; - return imageClassifyFunc(path.toStdString().c_str()); + return m_dbus->imageClassify(path); } -bool Classifyutils::isLoaded() +bool Classifyutils::isDBusExist() { - return imageClassifyFunc; + return m_bDBusExist; } Classifyutils::Classifyutils() { - QLibrary library("libimageclassify.so"); - imageClassifyFunc = reinterpret_cast(library.resolve("getImageClassification")); + m_dbus = new DaemonImageClassifyInterface(this); + if (!m_dbus->isValid() && !m_dbus->lastError().message().isEmpty()) { + qCritical() << "dbus com.deepin.logviewer isValid false error:" << m_dbus->lastError() << m_dbus->lastError().message(); + } + + QFileInfo fi(ImageClassifyDBusServicePath); + m_bDBusExist = fi.exists(); - if (!imageClassifyFunc) - return; + qDebug() << "dbus com.deepin.logviewer isValid true"; } diff --git a/src/album/utils/classifyutils.h b/src/album/utils/classifyutils.h index 1ff19563..30596f34 100644 --- a/src/album/utils/classifyutils.h +++ b/src/album/utils/classifyutils.h @@ -5,18 +5,64 @@ #ifndef CLASSIFYUTILS_H #define CLASSIFYUTILS_H #include +#include +/* + * Proxy class for interface com.deepin.logviewer + */ +class DaemonImageClassifyInterface : public QDBusAbstractInterface +{ + Q_OBJECT +public: + static inline const char *staticInterfaceName() + { return "com.deepin.imageclassify"; } + static inline const char *staticServiceName() + { return "com.deepin.imageclassify"; } + static inline const char *staticObjectPath() + { return "/com/deepin/imageclassify"; } + +public: + DaemonImageClassifyInterface(QObject *parent = nullptr) + : QDBusAbstractInterface(staticServiceName(), staticObjectPath(), staticInterfaceName(), QDBusConnection::systemBus(), parent) + { + + } + + ~DaemonImageClassifyInterface() + { -class Classifyutils + } + +public Q_SLOTS: // METHODS + inline QDBusPendingReply imageClassify(const QString &filePath) + { + QList argumentList; + argumentList << QVariant::fromValue(filePath); + return asyncCallWithArgumentList(QStringLiteral("imageClassify"), argumentList); + } + +Q_SIGNALS: // SIGNALS +}; + +namespace com { +namespace deepin { +typedef ::DaemonImageClassifyInterface imageclassify; +} +} // namespace com + +class Classifyutils : public QObject { + Q_OBJECT + public: static Classifyutils *GetInstance(); QString imageClassify(const QString &path); - bool isLoaded(); + bool isDBusExist(); private : static Classifyutils *m_pInstance; Classifyutils(); - const char* (*imageClassifyFunc)(const char*) = nullptr; + bool m_bDBusExist{false}; + DaemonImageClassifyInterface *m_dbus {nullptr}; }; #endif // CLASSIFYUTILS_H