diff --git a/.gitignore b/.gitignore index f8446a72..03cba814 100644 --- a/.gitignore +++ b/.gitignore @@ -103,3 +103,4 @@ debian/deepin-anything-server.debhelper.log src/kernelmod/.Module.symvers.cmd src/kernelmod/.modules.order.cmd src/kernelmod/.vfs_monitor.mod.cmd +.pms/ diff --git a/assets/org.deepin.anything.json b/assets/org.deepin.anything.json index b81e78c2..35639ab8 100644 --- a/assets/org.deepin.anything.json +++ b/assets/org.deepin.anything.json @@ -3,90 +3,92 @@ "version": "1.0", "contents": { "indexing_paths": { - "value": ["$HOME"], + "value": [ + "$HOME" + ], "serial": 0, - "flags":[], + "flags": [], "name": "Indexing Paths", "name[zh_CN]": "索引路径", "description": "Indexing Paths", - "description[zh_CN]":"索引路径", + "description[zh_CN]": "索引路径", "permissions": "readwrite", "visibility": "public" }, "blacklist_paths": { "value": [], "serial": 0, - "flags":[], + "flags": [], "name": "Blacklist Paths", "name[zh_CN]": "黑名单路径", "description": "Blacklist Paths", - "description[zh_CN]":"黑名单路径", + "description[zh_CN]": "黑名单路径", "permissions": "readwrite", "visibility": "public" }, "app_file_suffix": { "value": "desktop", "serial": 0, - "flags":[], + "flags": [], "name": "App File Suffix", "name[zh_CN]": "应用文件后缀", "description": "App File Suffix", - "description[zh_CN]":"应用文件后缀", + "description[zh_CN]": "应用文件后缀", "permissions": "readwrite", "visibility": "public" }, "archive_file_suffix": { "value": "7z;ace;arj;bz2;cab;gz;gzip;jar;r00;r01;r02;r03;r04;r05;r06;r07;r08;r09;r10;r11;r12;r13;r14;r15;r16;r17;r18;r19;r20;r21;r22;r23;r24;r25;r26;r27;r28;r29;rar;tar;tgz;z;zip", "serial": 0, - "flags":[], + "flags": [], "name": "Archive File Suffix", "name[zh_CN]": "归档文件后缀", "description": "Archive File Suffix", - "description[zh_CN]":"归档文件后缀", + "description[zh_CN]": "归档文件后缀", "permissions": "readwrite", "visibility": "public" }, "audio_file_suffix": { "value": "aac;ac3;aif;aifc;aiff;au;cda;dts;fla;flac;it;m1a;m2a;m3u;m4a;mid;midi;mka;mod;mp2;mp3;mpa;ogg;opus;ra;rmi;spc;rmi;snd;umx;voc;wav;wma;xm;ape", "serial": 0, - "flags":[], + "flags": [], "name": "Audio File Suffix", "name[zh_CN]": "音频文件后缀", "description": "Audio File Suffix", - "description[zh_CN]":"音频文件后缀", + "description[zh_CN]": "音频文件后缀", "permissions": "readwrite", "visibility": "public" }, "doc_file_suffix": { "value": "c;chm;cpp;csv;cxx;doc;docm;docx;dot;dotm;dotx;h;hpp;htm;html;hxx;ini;java;lua;mht;mhtml;ods;odt;odp;pdf;potx;potm;ppam;ppsm;ppsx;pps;ppt;pptm;pptx;rtf;sldm;sldx;thmx;txt;vsd;wpd;wps;wri;xlam;xls;xlsb;xlsm;xlsx;xltm;xltx;xml;latex;wpt;md;odg;dps;sh;xhtml;dhtml;shtm;shtml;json;css;yaml;bat;js;sql;uof;ofd;log;tex", "serial": 0, - "flags":[], + "flags": [], "name": "Doc File Suffix", "name[zh_CN]": "文档文件后缀", "description": "Doc File Suffix", - "description[zh_CN]":"文档文件后缀", + "description[zh_CN]": "文档文件后缀", "permissions": "readwrite", "visibility": "public" }, "pic_file_suffix": { "value": "ani;avif;avifs;bmp;bw;dci;eps;epsf;epsi;exr;gif;heic;heif;heis;heix;icns;ico;jpe;jpeg;jpg;kra;mng;ora;pcx;pic;png;psd;raf;ras;raw;rgb;rgba;sgi;svg;svgz;tga;tif;tiff;wbmp;webp;wmf;xcf;cr2;crw;nef;dng;arw;orf;rw2;pef;srw;mrw;x3f;dcr;kdc;3fr;nrw;mef;iiq;sr2;erf;mos;rwl", "serial": 0, - "flags":[], + "flags": [], "name": "Pic File Suffix", "name[zh_CN]": "图片文件后缀", "description": "Pic File Suffix", - "description[zh_CN]":"图片文件后缀", + "description[zh_CN]": "图片文件后缀", "permissions": "readwrite", "visibility": "public" }, "video_file_suffix": { "value": "3g2;3gp;3gp2;3gpp;amr;amv;asf;asx;avi;bdmv;bik;d2v;divx;drc;dsa;dsm;dss;dsv;evo;f4v;flc;fli;flic;flv;hdmov;ifo;ivf;m1v;m2p;m2t;m2ts;m2v;m4b;m4p;m4v;mkv;mp2v;mp4;mp4v;mpe;mpeg;mpg;mpls;mpv2;mpv4;mov;mts;ogm;ogv;pss;pva;qt;ram;ratdvd;rm;rmm;rmvb;roq;rpm;smil;smk;swf;tp;tpr;ts;vob;vp6;webm;wm;wmp;wmv", "serial": 0, - "flags":[], + "flags": [], "name": "Video File Suffix", "name[zh_CN]": "视频文件后缀", "description": "Video File Suffix", - "description[zh_CN]":"视频文件后缀", + "description[zh_CN]": "视频文件后缀", "permissions": "readwrite", "visibility": "public" }, @@ -133,6 +135,17 @@ "description[zh_CN]": "触发 updating 的事件数", "permissions": "readwrite", "visibility": "public" + }, + "idle_exit_timeout_minutes": { + "value": 10, + "serial": 0, + "flags": [], + "name": "Idle Exit Timeout Minutes", + "name[zh_CN]": "空闲退出超时时间(分钟)", + "description": "Minutes of D-Bus inactivity before the index service auto-exits (0 = never exit)", + "description[zh_CN]": "索引服务空闲多少分钟后自动退出(0 = 永不退出)", + "permissions": "readwrite", + "visibility": "public" } } } diff --git a/src/index/CMakeLists.txt b/src/index/CMakeLists.txt index 92a6edc9..4619930c 100644 --- a/src/index/CMakeLists.txt +++ b/src/index/CMakeLists.txt @@ -67,6 +67,7 @@ set(INDEX_SOURCES dbus/ocrindexdbus.cpp dbus/filenameindexdbus.cpp serviceentry.cpp + idleexitmanager.cpp ) list(TRANSFORM INDEX_SOURCES PREPEND ${CMAKE_CURRENT_SOURCE_DIR}/) file(GLOB_RECURSE INDEX_HEADERS CONFIGURE_DEPENDS "*.h") diff --git a/src/index/idleexitmanager.cpp b/src/index/idleexitmanager.cpp new file mode 100644 index 00000000..87abaf69 --- /dev/null +++ b/src/index/idleexitmanager.cpp @@ -0,0 +1,84 @@ +// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: GPL-3.0-or-later + +#include "idleexitmanager.h" +#include "serviceentry.h" + +#include +#include +#include +#include +#include + +// qDBusAddSpyHook is a Qt DBus internal function exported from libQt6DBus. +// It registers a callback invoked for every D-Bus message that flows +// through the connection (both incoming and outgoing). The callback +// runs on the main thread, so touching the QTimer directly is safe. +// +// The symbol is not declared in any public Qt6 header, so we declare +// it here. It is a C++ function (not extern "C"), so the linker must +// resolve the mangled symbol _Z15qDBusAddSpyHookPFvRK12QDBusMessageE +// that libQt6DBus exports. Using extern "C" would search for the +// unmangled name and fail at link time. +Q_DECL_IMPORT void qDBusAddSpyHook(void (*)(const QDBusMessage &)); + +namespace anything_index { + +static constexpr char kAnythingAppId[] = "org.deepin.anything"; +static constexpr char kAnythingSchema[] = "org.deepin.anything"; +static constexpr char kIdleExitTimeoutKey[] = "idle_exit_timeout_minutes"; +static constexpr int kDefaultIdleExitTimeoutMinutes = 10; + +static QTimer *g_idleTimer = nullptr; + +static void idleSpyHook(const QDBusMessage &) +{ + if (g_idleTimer) + g_idleTimer->start(); +} + +void startIdleExitManager() +{ + int timeoutMinutes = kDefaultIdleExitTimeoutMinutes; + + const std::unique_ptr dconfig( + Dtk::Core::DConfig::create(QString::fromLatin1(kAnythingAppId), + QString::fromLatin1(kAnythingSchema))); + if (dconfig && dconfig->isValid()) { + timeoutMinutes = dconfig->value(QString::fromLatin1(kIdleExitTimeoutKey), + kDefaultIdleExitTimeoutMinutes).toInt(); + if (timeoutMinutes < 0) { + qWarning() << "deepin-anything-index: invalid idle_exit_timeout_minutes" + << timeoutMinutes << ", using default" + << kDefaultIdleExitTimeoutMinutes; + timeoutMinutes = kDefaultIdleExitTimeoutMinutes; + } + } else { + qWarning() << "deepin-anything-index: failed to load dconfig for idle exit" + "timeout, using default" << kDefaultIdleExitTimeoutMinutes + << "minutes"; + } + + if (timeoutMinutes == 0) { + qInfo() << "deepin-anything-index: idle exit disabled (timeout = 0)"; + return; + } + + g_idleTimer = new QTimer; + g_idleTimer->setSingleShot(true); + g_idleTimer->setInterval(timeoutMinutes * 60 * 1000); + + QObject::connect(g_idleTimer, &QTimer::timeout, []() { + qInfo() << "deepin-anything-index: idle timeout reached," + "exiting to release resources (D-Bus activation will" + "restart on next request)"; + unregisterIndexServices(); + QCoreApplication::quit(); + }); + + qDBusAddSpyHook(idleSpyHook); + g_idleTimer->start(); +} + +} // namespace anything_index diff --git a/src/index/idleexitmanager.h b/src/index/idleexitmanager.h new file mode 100644 index 00000000..1b545485 --- /dev/null +++ b/src/index/idleexitmanager.h @@ -0,0 +1,30 @@ +// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef IDLEEXITMANAGER_H +#define IDLEEXITMANAGER_H + +namespace anything_index { + +// Registers a D-Bus spy hook that restarts an idle timer on every D-Bus +// message. When the timer expires (no D-Bus activity for the configured +// number of minutes), unregisters index services and quits the application. +// +// The idle timeout is read from the dconfig key "idle_exit_timeout_minutes" +// in the org.deepin.anything schema (default: 10 minutes). A value of 0 +// disables the auto-exit feature entirely. +// +// The service is D-Bus-activated (its .service file points at +// deepin-anything-index), so exiting when idle is safe: the next D-Bus +// call to any index service name will automatically restart the process. +// +// dde-file-manager's AbstractIndexController::keepBackendAlive polls +// IsEnabled every 5 minutes while indexes are enabled, which resets the +// timer and keeps the service alive. When all indexes are disabled the +// keepalive stops, and the service exits after the idle timeout. +void startIdleExitManager(); + +} // namespace anything_index + +#endif // IDLEEXITMANAGER_H diff --git a/src/index/main.cpp b/src/index/main.cpp index 9ab3ae54..7cc5f2c3 100644 --- a/src/index/main.cpp +++ b/src/index/main.cpp @@ -3,6 +3,7 @@ // SPDX-License-Identifier: GPL-3.0-or-later #include "serviceentry.h" +#include "idleexitmanager.h" #include "utils/trustclient.h" #include @@ -132,6 +133,14 @@ int main(int argc, char *argv[]) return 1; } + // Exit the process after a configurable idle timeout (dconfig key + // idle_exit_timeout_minutes, default 10 min). The service + // is D-Bus-activated, so the next request to any index service name + // restarts it automatically. dde-file-manager's keepBackendAlive polls + // IsEnabled every 5 minutes while indexes are enabled, which resets the + // idle timer and keeps the service alive. + anything_index::startIdleExitManager(); + // Bind the index lifecycle to the daemon: watch the daemon's D-Bus name // (org.deepin.Anything) on the system bus for unregistration. When the // daemon stops or crashes its bus name is released — at that point we