feat: add filesystem watcher for reloading applications
Signed-off-by: ComixHe <heyuming@deepin.org>
This commit is contained in:
parent
16d8e21598
commit
47d5cb05fe
@ -19,7 +19,7 @@ int main(int argc, char *argv[])
|
|||||||
parser.setApplicationDescription("Identify in what capacity the process is running.");
|
parser.setApplicationDescription("Identify in what capacity the process is running.");
|
||||||
parser.addHelpOption();
|
parser.addHelpOption();
|
||||||
parser.addVersionOption();
|
parser.addVersionOption();
|
||||||
parser.addPositionalArgument("PIDs", "PIDs to identify.", "[pid1 pid2 pid3 ...]");
|
parser.addPositionalArgument("PIDs", "PIDs to identify.", "[pid1,pid2,pid3 ...]");
|
||||||
|
|
||||||
parser.process(app);
|
parser.process(app);
|
||||||
auto PIDs = parser.positionalArguments();
|
auto PIDs = parser.positionalArguments();
|
||||||
@ -27,13 +27,14 @@ int main(int argc, char *argv[])
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto slice = PIDs.first().split(' ', Qt::SkipEmptyParts);
|
auto slice = PIDs.first().split(',', Qt::SkipEmptyParts);
|
||||||
QList<pid_t> PIDList{};
|
QList<pid_t> PIDList{};
|
||||||
std::transform(slice.cbegin(), slice.cend(), std::back_inserter(PIDList), [](const QString &pid) {
|
std::transform(slice.cbegin(), slice.cend(), std::back_inserter(PIDList), [](const QString &pid) {
|
||||||
bool ok{true};
|
bool ok{true};
|
||||||
auto result = pid.toInt(&ok);
|
auto result = pid.toInt(&ok);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
qFatal() << "failed to convert:" << pid;
|
qCritical() << "syntax error:" << pid;
|
||||||
|
std::terminate();
|
||||||
}
|
}
|
||||||
|
|
||||||
return static_cast<pid_t>(result);
|
return static_cast<pid_t>(result);
|
||||||
@ -43,7 +44,8 @@ int main(int argc, char *argv[])
|
|||||||
std::for_each(PIDList.cbegin(), PIDList.cend(), [&con](pid_t pid) {
|
std::for_each(PIDList.cbegin(), PIDList.cend(), [&con](pid_t pid) {
|
||||||
auto pidfd = pidfd_open(pid, 0);
|
auto pidfd = pidfd_open(pid, 0);
|
||||||
if (pidfd == -1) {
|
if (pidfd == -1) {
|
||||||
qFatal() << "failed to open pidfd:" << std::strerror(errno);
|
qCritical() << "failed to open pidfd:" << std::strerror(errno) << "skip.";
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto msg = QDBusMessage::createMethodCall(
|
auto msg = QDBusMessage::createMethodCall(
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
#include <QHash>
|
#include <QHash>
|
||||||
#include <QDBusMessage>
|
#include <QDBusMessage>
|
||||||
#include <QStringBuilder>
|
#include <QStringBuilder>
|
||||||
|
#include <chrono>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
ApplicationManager1Service::~ApplicationManager1Service() = default;
|
ApplicationManager1Service::~ApplicationManager1Service() = default;
|
||||||
@ -24,6 +25,8 @@ ApplicationManager1Service::ApplicationManager1Service(std::unique_ptr<Identifie
|
|||||||
: m_identifier(std::move(ptr))
|
: m_identifier(std::move(ptr))
|
||||||
, m_storage(std::move(storage))
|
, m_storage(std::move(storage))
|
||||||
{
|
{
|
||||||
|
using namespace std::chrono_literals;
|
||||||
|
m_reloadTimer.setInterval(1s);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ApplicationManager1Service::initService(QDBusConnection &connection) noexcept
|
void ApplicationManager1Service::initService(QDBusConnection &connection) noexcept
|
||||||
@ -61,6 +64,12 @@ void ApplicationManager1Service::initService(QDBusConnection &connection) noexce
|
|||||||
std::terminate();
|
std::terminate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
connect(&m_watcher, &QFileSystemWatcher::directoryChanged, this, &ApplicationManager1Service::ReloadApplications);
|
||||||
|
auto unhandled = m_watcher.addPaths(getDesktopFileDirs());
|
||||||
|
for (const auto &dir : unhandled) {
|
||||||
|
qCritical() << "couldn't watch directory:" << dir;
|
||||||
|
}
|
||||||
|
|
||||||
auto &dispatcher = SystemdSignalDispatcher::instance();
|
auto &dispatcher = SystemdSignalDispatcher::instance();
|
||||||
|
|
||||||
connect(&dispatcher, &SystemdSignalDispatcher::SystemdUnitNew, this, &ApplicationManager1Service::addInstanceToApplication);
|
connect(&dispatcher, &SystemdSignalDispatcher::SystemdUnitNew, this, &ApplicationManager1Service::addInstanceToApplication);
|
||||||
@ -604,6 +613,13 @@ void ApplicationManager1Service::updateApplication(const QSharedPointer<Applicat
|
|||||||
|
|
||||||
void ApplicationManager1Service::ReloadApplications()
|
void ApplicationManager1Service::ReloadApplications()
|
||||||
{
|
{
|
||||||
|
if (m_reloadTimer.isActive()) {
|
||||||
|
qInfo() << "reloadTimer is running, ignore...";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_reloadTimer.start();
|
||||||
|
|
||||||
const auto &desktopFileDirs = getDesktopFileDirs();
|
const auto &desktopFileDirs = getDesktopFileDirs();
|
||||||
|
|
||||||
auto apps = m_applicationList.keys();
|
auto apps = m_applicationList.keys();
|
||||||
|
@ -12,6 +12,8 @@
|
|||||||
#include <QScopedPointer>
|
#include <QScopedPointer>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <QMap>
|
#include <QMap>
|
||||||
|
#include <QFileSystemWatcher>
|
||||||
|
#include <QTimer>
|
||||||
#include "applicationmanagerstorage.h"
|
#include "applicationmanagerstorage.h"
|
||||||
#include "dbus/jobmanager1service.h"
|
#include "dbus/jobmanager1service.h"
|
||||||
#include "dbus/mimemanager1service.h"
|
#include "dbus/mimemanager1service.h"
|
||||||
@ -71,6 +73,8 @@ private:
|
|||||||
QScopedPointer<JobManager1Service> m_jobManager{nullptr};
|
QScopedPointer<JobManager1Service> m_jobManager{nullptr};
|
||||||
QStringList m_hookElements;
|
QStringList m_hookElements;
|
||||||
QStringList m_systemdPathEnv;
|
QStringList m_systemdPathEnv;
|
||||||
|
QFileSystemWatcher m_watcher;
|
||||||
|
QTimer m_reloadTimer;
|
||||||
QMap<QDBusObjectPath, QSharedPointer<ApplicationService>> m_applicationList;
|
QMap<QDBusObjectPath, QSharedPointer<ApplicationService>> m_applicationList;
|
||||||
|
|
||||||
void scanMimeInfos() noexcept;
|
void scanMimeInfos() noexcept;
|
||||||
|
Loading…
Reference in New Issue
Block a user