Skip to content

Commit 882e409

Browse files
committed
feat(dbus): implement comprehensive DBus service interface
Add DBus service files and implement full interface for deepin-compressor: - Install DBus service and interface XML files via CMake - Create com.deepin.Compressor.service with proper service name and executable path - Define DBus interface with methods for window management (show/hide/raise/quit) - Add compression operations (compressFiles, extractFiles, previewArchive) - Update ApplicationAdaptor to handle MainWindow instead of QApplication - Register DBus service in CompressorApplication constructor - Modify main.cpp to use correct service name and object paths The implementation provides a complete DBus API for external applications to interact with the compressor application programmatically, enabling integration with desktop environments and other system services.
1 parent 710f210 commit 882e409

File tree

9 files changed

+270
-31
lines changed

9 files changed

+270
-31
lines changed

src/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,11 @@ install(DIRECTORY assets/deepin-compressor DESTINATION ${CMAKE_INSTALL_DATADIR}/
135135

136136
# 支持压缩的格式
137137
#install(FILES conf/compressor-singlecompress.conf DESTINATION ${CMAKE_INSTALL_DATADIR}/deepin-compressor/compressedformat/)
138+
139+
# Install DBus service file
140+
install(FILES ${CMAKE_CURRENT_LIST_DIR}/com.deepin.Compressor.service
141+
DESTINATION ${CMAKE_INSTALL_DATADIR}/dbus-1/services/)
142+
143+
# Install DBus interface XML file
144+
install(FILES ${CMAKE_CURRENT_LIST_DIR}/com.deepin.Compressor.xml
145+
DESTINATION ${CMAKE_INSTALL_DATADIR}/dbus-1/interfaces/)

src/com.deepin.Compressor.service

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[D-BUS Service]
2+
Name=com.deepin.Compressor
3+
Exec=/usr/bin/deepin-compressor

src/com.deepin.Compressor.xml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
2+
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
3+
<node>
4+
<interface name="com.deepin.Compressor">
5+
<method name="showWindow">
6+
<arg direction="out" type="b"/>
7+
</method>
8+
<method name="hideWindow">
9+
<arg direction="out" type="b"/>
10+
</method>
11+
<method name="raiseWindow">
12+
<arg direction="out" type="b"/>
13+
</method>
14+
<method name="raise">
15+
<arg direction="in" type="s" name="sFile"/>
16+
<arg direction="out" type="b"/>
17+
</method>
18+
<method name="quitWindow">
19+
<arg direction="out" type="b"/>
20+
</method>
21+
<method name="compressFiles">
22+
<arg direction="in" type="as" name="filePaths"/>
23+
<arg direction="in" type="s" name="destinationPath"/>
24+
<arg direction="out" type="b"/>
25+
</method>
26+
<method name="extractFiles">
27+
<arg direction="in" type="s" name="archivePath"/>
28+
<arg direction="in" type="s" name="destinationPath"/>
29+
<arg direction="out" type="b"/>
30+
</method>
31+
<method name="previewArchive">
32+
<arg direction="in" type="s" name="archivePath"/>
33+
<arg direction="out" type="b"/>
34+
</method>
35+
</interface>
36+
</node>

src/main.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -217,30 +217,31 @@ int main(int argc, char *argv[])
217217
showWatermark(sJsonStr, &w);
218218

219219
qDebug() << "Creating DBus adaptor and connecting to session bus";
220-
ApplicationAdaptor adaptor(&app);
220+
ApplicationAdaptor adaptor(&w);
221221
QDBusConnection dbus = QDBusConnection::sessionBus();
222222

223223
if(!orderObject){
224224
qDebug() << "Registering standard DBus service";
225-
if (dbus.registerService("com.deepin.compressor")) {
225+
if (dbus.registerService("com.deepin.Compressor")) {
226+
dbus.registerObject("/com/deepin/Compressor", &w);
226227
qDebug() << "DBus service registered successfully, moving window to center";
227228
Dtk::Widget::moveToCenter(&w);
228229
} else {
229-
qWarning() << "Failed to register standard DBus service";
230+
qWarning() << "Failed to register standard DBus service:" << dbus.lastError().message();
230231
}
231232
} else {
232233
qDebug() << "Registering WPS-specific DBus service";
233-
QString serviceName = "com.deepin.compressor"+QString::number(QGuiApplication::applicationPid());
234+
QString serviceName = "com.deepin.Compressor"+QString::number(QGuiApplication::applicationPid());
234235
if (dbus.registerService(serviceName)) {
235236
qDebug() << "WPS DBus service registered successfully";
236-
QString objectPath = "/"+QString::number(QGuiApplication::applicationPid());
237-
dbus.registerObject(objectPath, &app);
237+
QString objectPath = "/com/deepin/Compressor/"+QString::number(QGuiApplication::applicationPid());
238+
dbus.registerObject(objectPath, &w);
238239
adaptor.setCompressFile(newfilelist.first());
239240
Dtk::Widget::moveToCenter(&w);
240241
w.setProperty(ORDER_JSON, sJsonStr);
241242
qDebug() << "Window properties set for WPS mode";
242243
} else {
243-
qWarning() << "Failed to register WPS DBus service";
244+
qWarning() << "Failed to register WPS DBus service:" << dbus.lastError().message();
244245
}
245246

246247
qDebug() << "Connecting to WPS cryptfs DBus interface";

src/source/common/dbusadpator.cpp

Lines changed: 132 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,30 @@
33
//
44
// SPDX-License-Identifier: GPL-3.0-or-later
55

6+
#include "dbusadpator.h"
7+
#include "mainwindow.h"
8+
#include "archivemanager.h"
9+
610
#include <QFileInfo>
711
#include <QtDebug>
812
#include <DSettings>
9-
#include "dbusadpator.h"
1013
#include <QMainWindow>
1114
#include <QDBusInterface>
1215
#include <QDBusReply>
16+
#include <QDir>
17+
1318
const QString SOPENLIST = "openfiles";
1419

15-
ApplicationAdaptor::ApplicationAdaptor(QApplication *application)
16-
: QDBusAbstractAdaptor(application), app(application)
20+
ApplicationAdaptor::ApplicationAdaptor(MainWindow *mainwindow)
21+
: QDBusAbstractAdaptor(mainwindow), m_mainWindow(mainwindow)
1722
{
1823
qDebug() << "ApplicationAdaptor initialized";
19-
connect(application, SIGNAL(aboutToQuit()), SIGNAL(aboutToQuit()));
20-
connect(application, SIGNAL(focusChanged(QWidget*, QWidget*)),
21-
SLOT(focusChangedSlot(QWidget*, QWidget*)));
2224
}
2325

26+
void ApplicationAdaptor::setMainWindow(MainWindow *mainWindow)
27+
{
28+
m_mainWindow = mainWindow;
29+
}
2430

2531
/**
2632
* @brief setCurOpenFile 设置当前的压缩包文件名
@@ -32,16 +38,18 @@ void ApplicationAdaptor::setCompressFile(const QString &sFile)
3238
m_sFile = sFile;
3339
}
3440

35-
36-
void ApplicationAdaptor::raise(const QString &sFile)
41+
bool ApplicationAdaptor::raise(const QString &sFile)
3742
{
3843
qDebug() << "Raise window request for file:" << sFile;
3944
if(m_sFile.isEmpty() || m_sFile.isNull()) {
4045
qWarning() << "No compress file set, cannot raise window";
41-
return;
46+
return false;
4247
}
43-
if(m_curShowWidget && (m_sFile == sFile))
48+
if(m_curShowWidget && (m_sFile == sFile)) {
4449
m_curShowWidget->activateWindow();
50+
return true;
51+
}
52+
return false;
4553
}
4654

4755
void ApplicationAdaptor::onActiveWindow(qint64 pid)
@@ -84,5 +92,119 @@ void ApplicationAdaptor::onActiveWindow(qint64 pid)
8492
}
8593
}
8694
}
95+
}
96+
97+
// Window management methods
98+
bool ApplicationAdaptor::showWindow()
99+
{
100+
qDebug() << "DBus: showWindow called";
101+
if (m_mainWindow) {
102+
m_mainWindow->show();
103+
m_mainWindow->raise();
104+
m_mainWindow->activateWindow();
105+
return true;
106+
}
107+
return false;
108+
}
109+
110+
bool ApplicationAdaptor::hideWindow()
111+
{
112+
qDebug() << "DBus: hideWindow called";
113+
if (m_mainWindow) {
114+
m_mainWindow->hide();
115+
return true;
116+
}
117+
return false;
118+
}
119+
120+
bool ApplicationAdaptor::raiseWindow()
121+
{
122+
qDebug() << "DBus: raiseWindow called";
123+
if (m_mainWindow) {
124+
m_mainWindow->raise();
125+
m_mainWindow->activateWindow();
126+
return true;
127+
}
128+
return false;
129+
}
130+
131+
bool ApplicationAdaptor::quitWindow()
132+
{
133+
qDebug() << "DBus: quitWindow called";
134+
if (m_mainWindow) {
135+
m_mainWindow->close();
136+
return true;
137+
}
138+
return false;
139+
}
140+
141+
bool ApplicationAdaptor::compressFiles(const QStringList &filePaths)
142+
{
143+
qDebug() << "DBus: compressFiles called with" << filePaths.size();
144+
145+
if (filePaths.isEmpty()) {
146+
qWarning() << "No files provided for compression";
147+
return false;
148+
}
149+
150+
if (!m_mainWindow) {
151+
qWarning() << "MainWindow not available";
152+
return false;
153+
}
154+
155+
for (const QString &filePath : filePaths) {
156+
if (!QFileInfo::exists(filePath)) {
157+
qWarning() << "File does not exist:" << filePath;
158+
return false;
159+
}
160+
}
161+
162+
showWindow();
163+
m_mainWindow->slotDragSelectedFiles(QStringList{filePaths});
164+
return true;
165+
}
166+
167+
bool ApplicationAdaptor::extractFiles(const QString &archivePath, const QString &destinationPath)
168+
{
169+
qDebug() << "DBus: extractFiles called for" << archivePath << "to" << destinationPath;
170+
171+
if (!QFileInfo::exists(archivePath)) {
172+
qWarning() << "Archive file does not exist:" << archivePath;
173+
return false;
174+
}
175+
176+
if (!m_mainWindow) {
177+
qWarning() << "MainWindow not available";
178+
return false;
179+
}
180+
181+
QFileInfo destInfo(destinationPath);
182+
if (!destInfo.isDir() || !destInfo.isWritable()) {
183+
qWarning() << "Destination path is not a writable directory:" << destinationPath;
184+
return false;
185+
}
186+
187+
showWindow();
188+
m_mainWindow->slotDragSelectedFiles(QStringList{archivePath});
189+
m_mainWindow->slotUncompressClicked(destinationPath);
190+
return true;
191+
}
192+
193+
bool ApplicationAdaptor::previewArchive(const QString &archivePath)
194+
{
195+
qDebug() << "DBus: previewArchive called for" << archivePath;
196+
197+
if (!QFileInfo::exists(archivePath)) {
198+
qWarning() << "Archive file does not exist:" << archivePath;
199+
return false;
200+
}
201+
202+
if (!m_mainWindow) {
203+
qWarning() << "MainWindow not available";
204+
return false;
205+
}
87206

207+
showWindow();
208+
m_mainWindow->slotDragSelectedFiles(QStringList{archivePath});
209+
return true;
88210
}

src/source/common/dbusadpator.h

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,88 @@
44
#ifndef DBUS_ADAPTOR
55
#define DBUS_ADAPTOR
66

7-
#include <QtDBus/QDBusAbstractAdaptor>
8-
9-
#include "QApplication"
7+
#include <QDBusAbstractAdaptor>
8+
#include <QApplication>
109
#include <QWidget>
1110
#include <QSettings>
11+
#include <QStringList>
12+
13+
class MainWindow;
1214

1315
/**
1416
* @file d-bus适配器,开放归档管理器d-bus接口
1517
*/
1618
class ApplicationAdaptor: public QDBusAbstractAdaptor
1719
{
1820
Q_OBJECT
19-
Q_CLASSINFO("D-Bus Interface", "com.deepin.compressor")
21+
Q_CLASSINFO("D-Bus Interface", "com.deepin.Compressor")
2022
Q_CLASSINFO("D-Bus Introspection", ""
21-
" <interface name=\"com.deepin.compressor\">\n"
23+
" <interface name=\"com.deepin.Compressor\">\n"
2224

25+
" <method name=\"showWindow\">\n"
26+
" <arg direction=\"out\" type=\"b\"/>\n"
27+
" </method>\n"
28+
" <method name=\"hideWindow\">\n"
29+
" <arg direction=\"out\" type=\"b\"/>\n"
30+
" </method>\n"
31+
" <method name=\"raiseWindow\">\n"
32+
" <arg direction=\"out\" type=\"b\"/>\n"
33+
" </method>\n"
34+
" <method name=\"quitWindow\">\n"
35+
" <arg direction=\"out\" type=\"b\"/>\n"
36+
" </method>\n"
37+
" <method name=\"compressFiles\">\n"
38+
" <arg direction=\"in\" type=\"as\" name=\"filePaths\"/>\n"
39+
" <arg direction=\"in\" type=\"s\" name=\"destinationPath\"/>\n"
40+
" <arg direction=\"out\" type=\"b\"/>\n"
41+
" </method>\n"
42+
" <method name=\"extractFiles\">\n"
43+
" <arg direction=\"in\" type=\"s\" name=\"archivePath\"/>\n"
44+
" <arg direction=\"in\" type=\"s\" name=\"destinationPath\"/>\n"
45+
" <arg direction=\"out\" type=\"b\"/>\n"
46+
" </method>\n"
47+
" <method name=\"previewArchive\">\n"
48+
" <arg direction=\"in\" type=\"s\" name=\"archivePath\"/>\n"
49+
" <arg direction=\"out\" type=\"b\"/>\n"
50+
" </method>\n"
2351
" <method name=\"raise\">\n"
24-
" <arg direction=\"in\" type=\"none\" name=\"raise\"/>\n"
52+
" <arg direction=\"in\" type=\"s\" name=\"sFile\"/>\n"
53+
" <arg direction=\"out\" type=\"b\"/>\n"
2554
" </method>\n"
26-
2755
" </interface>\n")
2856

2957
public:
30-
ApplicationAdaptor(QApplication *application);
58+
ApplicationAdaptor(MainWindow *mainwindow);
3159
/**
3260
* @brief setCurOpenFile 设置当前的压缩包文件名
3361
* @param sFile 压缩包文件名
3462
*/
3563
void setCompressFile(const QString &sFile);
36-
64+
void setMainWindow(MainWindow *mainWindow);
3765

3866
public Q_SLOTS:
3967
/**
4068
* @brief raise dbus接口
4169
* @param sFile 压缩包文件名的窗口激活
4270
*/
43-
void raise(const QString &sFile);
71+
bool raise(const QString &sFile);
4472
/**
4573
* @brief onActiveWindow 激活窗口
4674
* @param pid 对应进程id
4775
*/
4876
void onActiveWindow(qint64 pid);
4977

78+
// Window management methods
79+
bool showWindow();
80+
bool hideWindow();
81+
bool raiseWindow();
82+
bool quitWindow();
83+
84+
// Compression related methods
85+
bool compressFiles(const QStringList &filePaths);
86+
bool extractFiles(const QString &archivePath, const QString &destinationPath);
87+
bool previewArchive(const QString &archivePath);
88+
5089
private slots:
5190
/**
5291
* @brief focusChangedSlot 激活窗口切换
@@ -61,6 +100,7 @@ private slots:
61100
QApplication *app = nullptr; //运行应用程序实例
62101
QWidget *m_curShowWidget = nullptr; //当前显示窗口
63102
QString m_sFile; //压缩包文件名
103+
MainWindow *m_mainWindow = nullptr; //主窗口指针
64104
};
65105

66106
#endif /* ifndef _DMR_DBUS_ADAPTOR */

0 commit comments

Comments
 (0)