2020-10-27 21:15:57 +03:00
|
|
|
//
|
|
|
|
// Copyright 2020 elementary, Inc. (https://elementary.io)
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
public class Gala.AppCache : GLib.Object {
|
|
|
|
public signal void changed ();
|
|
|
|
|
|
|
|
private const int DEFAULT_TIMEOUT_SECONDS = 3;
|
|
|
|
|
2022-10-11 03:47:37 +03:00
|
|
|
private GLib.HashTable<unowned string, unowned string> startup_wm_class_to_id;
|
|
|
|
private GLib.HashTable<unowned string, GLib.DesktopAppInfo> id_to_app;
|
2020-10-27 21:15:57 +03:00
|
|
|
|
|
|
|
private GLib.AppInfoMonitor app_info_monitor;
|
|
|
|
|
|
|
|
private uint queued_update_id = 0;
|
|
|
|
|
|
|
|
construct {
|
2022-10-11 03:47:37 +03:00
|
|
|
startup_wm_class_to_id = new GLib.HashTable<unowned string, unowned string> (str_hash, str_equal);
|
|
|
|
id_to_app = new GLib.HashTable<unowned string, GLib.DesktopAppInfo> (str_hash, str_equal);
|
2020-10-27 21:15:57 +03:00
|
|
|
|
|
|
|
app_info_monitor = GLib.AppInfoMonitor.@get ();
|
|
|
|
app_info_monitor.changed.connect (queue_cache_update);
|
|
|
|
|
2021-01-17 18:35:59 +03:00
|
|
|
rebuild_cache.begin ();
|
2020-10-27 21:15:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private void queue_cache_update () {
|
|
|
|
if (queued_update_id != 0) {
|
|
|
|
GLib.Source.remove (queued_update_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
queued_update_id = GLib.Timeout.add_seconds (DEFAULT_TIMEOUT_SECONDS, () => {
|
2021-01-17 18:35:59 +03:00
|
|
|
rebuild_cache.begin ((obj, res) => {
|
|
|
|
rebuild_cache.end (res);
|
|
|
|
changed ();
|
2020-10-27 21:15:57 +03:00
|
|
|
|
2021-01-17 18:35:59 +03:00
|
|
|
queued_update_id = 0;
|
|
|
|
});
|
2020-10-27 21:15:57 +03:00
|
|
|
|
|
|
|
return GLib.Source.REMOVE;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-01-17 18:35:59 +03:00
|
|
|
private async void rebuild_cache () {
|
|
|
|
SourceFunc callback = rebuild_cache.callback;
|
2020-10-27 21:15:57 +03:00
|
|
|
|
2021-01-17 18:35:59 +03:00
|
|
|
new Thread<void> ("rebuild_cache", () => {
|
|
|
|
lock (startup_wm_class_to_id) {
|
2022-10-11 03:47:37 +03:00
|
|
|
startup_wm_class_to_id.remove_all ();
|
|
|
|
id_to_app.remove_all ();
|
2020-10-27 21:15:57 +03:00
|
|
|
|
2021-01-17 18:35:59 +03:00
|
|
|
var app_infos = GLib.AppInfo.get_all ();
|
2020-10-27 21:15:57 +03:00
|
|
|
|
2021-01-17 18:35:59 +03:00
|
|
|
foreach (unowned GLib.AppInfo app in app_infos) {
|
|
|
|
unowned string id = app.get_id ();
|
|
|
|
unowned string? startup_wm_class = ((GLib.DesktopAppInfo)app).get_startup_wm_class ();
|
2020-10-27 21:15:57 +03:00
|
|
|
|
2021-01-17 18:35:59 +03:00
|
|
|
id_to_app[id] = (GLib.DesktopAppInfo)app;
|
|
|
|
|
|
|
|
if (startup_wm_class == null) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-10-27 21:15:57 +03:00
|
|
|
|
2022-10-11 03:47:37 +03:00
|
|
|
unowned var old_id = startup_wm_class_to_id[startup_wm_class];
|
2021-01-17 18:35:59 +03:00
|
|
|
if (old_id == null || id == startup_wm_class) {
|
|
|
|
startup_wm_class_to_id[startup_wm_class] = id;
|
|
|
|
}
|
|
|
|
}
|
2020-10-27 21:15:57 +03:00
|
|
|
}
|
2021-01-17 18:35:59 +03:00
|
|
|
|
|
|
|
Idle.add ((owned)callback);
|
|
|
|
});
|
|
|
|
|
|
|
|
yield;
|
2020-10-27 21:15:57 +03:00
|
|
|
}
|
|
|
|
|
2022-10-11 03:47:37 +03:00
|
|
|
public unowned GLib.DesktopAppInfo? lookup_id (string? id) {
|
2020-10-27 21:15:57 +03:00
|
|
|
if (id == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return id_to_app[id];
|
|
|
|
}
|
|
|
|
|
|
|
|
public GLib.DesktopAppInfo? lookup_startup_wmclass (string? wm_class) {
|
|
|
|
if (wm_class == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-10-11 03:47:37 +03:00
|
|
|
unowned var id = startup_wm_class_to_id[wm_class];
|
2020-10-27 21:15:57 +03:00
|
|
|
if (id == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return id_to_app[id];
|
|
|
|
}
|
|
|
|
}
|