Skip to content

Commit

Permalink
refactor: use systemd manager the deepin-anything dbus service
Browse files Browse the repository at this point in the history
feat: enable the disk manager signals(mountAdded/Removed...)

Change-Id: I9fb19e32b21113ff356ba0236656ac9aac3865ab
  • Loading branch information
zccrs committed Mar 7, 2019
1 parent f021b0b commit 9fabe04
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 13 deletions.
2 changes: 2 additions & 0 deletions server/common.pri
@@ -1 +1,3 @@
isEmpty(VERSION): VERSION = 0.0.1

DEFINES += QT_MESSAGELOGCONTEXT
21 changes: 18 additions & 3 deletions server/lib/lftmanager.cpp
Expand Up @@ -796,7 +796,11 @@ QStringList LFTManager::insertFileToLFTBuf(const QByteArray &file)
markLFTFileToDirty(buf);
root_path_list << QString::fromLocal8Bit(get_root_path(buf));
} else {
cWarning() << "Failed:" << i.first << ", result:" << r;
if (r == ERR_NO_MEM) {
cWarning() << "Failed(No Memory):" << i.first;
} else {
cDebug() << "Failed:" << i.first << ", result:" << r;
}
}
}

Expand Down Expand Up @@ -843,7 +847,11 @@ QStringList LFTManager::removeFileFromLFTBuf(const QByteArray &file)
markLFTFileToDirty(buf);
root_path_list << QString::fromLocal8Bit(get_root_path(buf));
} else {
cWarning() << "Failed:" << i.first << ", result:" << r;
if (r == ERR_NO_MEM) {
cWarning() << "Failed(No Memory):" << i.first;
} else {
cDebug() << "Failed:" << i.first << ", result:" << r;
}
}
}

Expand Down Expand Up @@ -898,7 +906,11 @@ QStringList LFTManager::renameFileOfLFTBuf(const QByteArray &oldFile, const QByt
markLFTFileToDirty(buf);
root_path_list << QString::fromLocal8Bit(get_root_path(buf));
} else {
cWarning() << "Failed: result=" << r;
if (r == ERR_NO_MEM) {
cWarning() << "Failed(No Memory)";
} else {
cDebug() << "Failed: result=" << r;
}
}
}

Expand Down Expand Up @@ -1069,6 +1081,9 @@ LFTManager::LFTManager(QObject *parent)
connect(LFTDiskTool::diskManager(), &DFMDiskManager::fileSystemRemoved,
this, &LFTManager::onFSRemoved);

// 监听设备信号
LFTDiskTool::diskManager()->setWatchChanges(true);

QTimer *sync_timer = new QTimer(this);

connect(sync_timer, &QTimer::timeout, this, &LFTManager::_syncAll);
Expand Down
62 changes: 53 additions & 9 deletions server/monitor/plugins/update-lft/main.cpp
Expand Up @@ -26,45 +26,89 @@
#include <QCoreApplication>
#include <QDBusInterface>
#include <QDBusConnection>
#include <QDBusConnectionInterface>
#include <QDBusServiceWatcher>

DAS_BEGIN_NAMESPACE

#define ANYTHING_DBUS_NAME "com.deepin.anything"

class UpdateLFTInterface : public DASInterface
{
Q_OBJECT
public:
UpdateLFTInterface(QObject *parent = nullptr)
: DASInterface(parent)
, interface("com.deepin.anything", "/com/deepin/anything",
"com.deepin.anything", QDBusConnection::systemBus())
{
// 如果更新的目标正在构建索引,将导致dbus调用阻塞,因此需要更长的超时时间
// 此处设置为1个小时
interface.setTimeout(60000 * 60);
if (QDBusConnection::systemBus().interface()->isServiceRegistered(ANYTHING_DBUS_NAME)) {
initInterface();
}

QDBusServiceWatcher *watcher = new QDBusServiceWatcher(this);

watcher->setConnection(QDBusConnection::systemBus());
watcher->setWatchedServices({ANYTHING_DBUS_NAME});
watcher->setWatchMode(QDBusServiceWatcher::WatchForRegistration | QDBusServiceWatcher::WatchForUnregistration);

connect(watcher, &QDBusServiceWatcher::serviceRegistered,
this, &UpdateLFTInterface::initInterface);
connect(watcher, &QDBusServiceWatcher::serviceUnregistered,
this, &UpdateLFTInterface::destoryInterface);
}

void onFileCreate(const QByteArrayList &files) override
{
if (!interface)
return;

for (const QByteArray &f : files) {
interface.call(QDBus::Block, "insertFileToLFTBuf", f);
interface->call(QDBus::Block, "insertFileToLFTBuf", f);
}
}

void onFileDelete(const QByteArrayList &files) override
{
if (!interface)
return;

for (const QByteArray &f : files) {
interface.call(QDBus::Block, "removeFileFromLFTBuf", f);
interface->call(QDBus::Block, "removeFileFromLFTBuf", f);
}
}

void onFileRename(const QList<QPair<QByteArray, QByteArray>> &files) override
{
if (!interface)
return;

for (const QPair<QByteArray, QByteArray> &f : files) {
interface.call(QDBus::Block, "renameFileOfLFTBuf", f.first, f.second);
interface->call(QDBus::Block, "renameFileOfLFTBuf", f.first, f.second);
}
}

private:
QDBusInterface interface;
void initInterface()
{
if (interface)
return;

interface = new QDBusInterface(ANYTHING_DBUS_NAME, "/com/deepin/anything",
ANYTHING_DBUS_NAME, QDBusConnection::systemBus(),
this);
// 如果更新的目标正在构建索引,将导致dbus调用阻塞,因此需要更长的超时时间
// 此处设置为1个小时
interface->setTimeout(60000 * 60);
}

void destoryInterface()
{
if (interface) {
interface->deleteLater();
interface = nullptr;
}
}

QDBusInterface *interface = nullptr;
};

class UpdateLFTPlugin : public DASPlugin
Expand Down
16 changes: 16 additions & 0 deletions server/tool/deepin-anything-tool.service
@@ -0,0 +1,16 @@
[Unit]
Description=Deepin anything tool service
After=dbus.service udisks2.service
Before=deepin-anything-monitor.service

[Service]
Type=dbus
User=root
Group=root
BusName=com.deepin.anything
ExecStart=/usr/bin/deepin-anything-tool --dbus
Restart=on-failure
RestartSec=10

[Install]
WantedBy=multi-user.target
5 changes: 4 additions & 1 deletion server/tool/tool.pro
Expand Up @@ -41,4 +41,7 @@ dbus_config.files = $$PWD/com.deepin.anything.conf

target.path = $$PREFIX/bin

INSTALLS += target dbus_xmls dbus_service dbus_config
systemd_service.files = $${TARGET}.service
systemd_service.path = /lib/systemd/system

INSTALLS += target dbus_xmls dbus_service dbus_config systemd_service

0 comments on commit 9fabe04

Please sign in to comment.