18#include "xdgwatcher.h"
23#include <QStandardPaths>
26XdgWatcher::XdgWatcher(QObject* parent)
28 m_watcher(new QFileSystemWatcher(this))
30 connect(m_watcher, &QFileSystemWatcher::directoryChanged,
this, &XdgWatcher::onDirectoryChanged);
31 connect(m_watcher, &QFileSystemWatcher::fileChanged,
this, &XdgWatcher::onFileChanged);
33 const auto paths = QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation);
34 for (
const auto &path: paths) {
35 const auto qdir = QDir(path);
41 m_watcher->addPath(path);
44 const auto files = qdir.entryInfoList(QDir::Files);
45 for (
const auto &file: files) {
46 if (file.suffix() ==
"desktop") {
47 const auto path = file.absoluteFilePath();
48 m_watcher->addPath(path);
49 m_registry.insert(path, getAppId(file));
56const QString XdgWatcher::stripAppIdVersion(
const QString rawAppID)
const {
57 auto appIdComponents = rawAppID.split(
"_");
58 appIdComponents.removeLast();
59 return appIdComponents.join(
"_");
64const QString XdgWatcher::toStandardAppId(
const QFileInfo fileInfo)
const {
65 const auto paths = QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation);
66 for (
const auto &path: paths) {
67 if (fileInfo.absolutePath() == path) {
70 if (fileInfo.absolutePath().contains(path)) {
71 auto fileStr = fileInfo.absoluteFilePath();
72 fileStr.replace(path,
"");
73 fileStr.replace(
"/",
"-");
74 fileStr.replace(
".desktop",
"");
78 return fileInfo.completeBaseName();
81const QString XdgWatcher::getAppId(
const QFileInfo fileInfo)
const {
86 QFile qFile(fileInfo.absoluteFilePath());
87 qFile.open(QIODevice::ReadOnly);
88 QTextStream fileStream(&qFile);
90 while (fileStream.readLineInto(&line)) {
91 if (line.startsWith(
"X-Lomiri-Application-ID=")) {
92 auto rawAppID = line.replace(
"X-Lomiri-Application-ID=",
"");
94 return stripAppIdVersion(rawAppID);
100 return toStandardAppId(fileInfo);
104void XdgWatcher::onDirectoryChanged(
const QString &path) {
105 const auto files = QDir(path).entryInfoList(QDir::Files);
106 const auto watchedFiles = m_watcher->files();
107 for (
const auto &file: files) {
108 const auto appPath = file.absoluteFilePath();
109 if (file.suffix() ==
"desktop" && !watchedFiles.contains(appPath)) {
110 m_watcher->addPath(appPath);
112 const auto appId = getAppId(file);
113 m_registry.insert(appPath, appId);
114 Q_EMIT appAdded(appId);
119void XdgWatcher::onFileChanged(
const QString &path) {
120 const auto watchedFiles = m_watcher->files();
121 if (watchedFiles.contains(path)) {
123 Q_EMIT appInfoChanged(m_registry.value(path));
127 Q_EMIT appRemoved(m_registry.take(path));