File: Revert-recent_files_menu-Dont-always-check-for-existance.patch

package info (click to toggle)
secrets 11.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,744 kB
  • sloc: python: 6,509; xml: 7; makefile: 4
file content (149 lines) | stat: -rw-r--r-- 5,155 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
From: =?utf-8?q?Jeremy_B=C3=ADcha?= <jeremy.bicha@canonical.com>
Date: Sat, 12 Apr 2025 19:30:17 -0400
Subject: Revert "recent_files_menu: Dont always check for existance"

This reverts commit 94e2d362772d1fb180e1525904ff09d3113f0413.

asyncio requires pygobject 3.52
---
 gsecrets/application.py       |  4 ----
 gsecrets/recent_files_menu.py | 27 +++++++++++++++++++++++++++
 gsecrets/recent_manager.py    | 41 ++++++-----------------------------------
 3 files changed, 33 insertions(+), 39 deletions(-)

diff --git a/gsecrets/application.py b/gsecrets/application.py
index ba73619..62920c8 100644
--- a/gsecrets/application.py
+++ b/gsecrets/application.py
@@ -9,7 +9,6 @@ from gi.events import GLibEventLoopPolicy
 from gi.repository import Adw, Gio, GLib, Gtk, GtkSource
 
 from gsecrets import const
-from gsecrets.recent_manager import RecentManager
 from gsecrets.widgets.mod import load_widgets
 from gsecrets.widgets.window import Window
 
@@ -52,9 +51,6 @@ class Application(Adw.Application):
 
         load_widgets()
 
-        recents = RecentManager()
-        self.create_asyncio_task(recents.clean_non_existent())
-
     def do_open(self, gfile_list, _n_files, _hint):  # pylint: disable=arguments-differ
         for gfile in gfile_list:
             if not gfile.query_exists():
diff --git a/gsecrets/recent_files_menu.py b/gsecrets/recent_files_menu.py
index 21b40af..9fcfffa 100644
--- a/gsecrets/recent_files_menu.py
+++ b/gsecrets/recent_files_menu.py
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: GPL-3.0-only
 from __future__ import annotations
 
+import logging
 from gettext import gettext as _
 
 from gi.repository import Gio
@@ -19,12 +20,38 @@ class RecentFilesMenu:
         self.is_empty = True
 
         recents = RecentManager()
+        to_remove = []
 
+        attribute = Gio.FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
         for item in reversed(recents):
+            if not item.query_exists():
+                uri = item.get_uri()
+                logging.info("Ignoring nonexistent recent file: %s", uri)
+                to_remove.append(item)
+                continue
+
+            file_info = item.query_info(
+                attribute,
+                Gio.FileQueryInfoFlags.NONE,
+                None,
+            )
+            if content_type := file_info.get_attribute_as_string(attribute):
+                mime_type = Gio.content_type_get_mime_type(content_type)
+            else:
+                mime_type = "application/octet-stream"
+
+            if mime_type != "application/x-keepass2":
+                continue
+
             basename = item.get_basename()
             path = item.get_path()
             self.section.append(basename, f"win.open_database::{path}")
             self.is_empty = False
 
+        # We remove items after we are done iterating to avoid undefined
+        # behaviour.
+        for item in to_remove:
+            recents.remove_item(item)
+
         self.menu.append_section(_("Recent Files"), self.section)
         self.menu.freeze()
diff --git a/gsecrets/recent_manager.py b/gsecrets/recent_manager.py
index f1097dd..99600ff 100644
--- a/gsecrets/recent_manager.py
+++ b/gsecrets/recent_manager.py
@@ -1,8 +1,7 @@
 # SPDX-License-Identifier: GPL-3.0-only
-import logging
 from collections import deque
 
-from gi.repository import Gio, GLib, GObject
+from gi.repository import Gio, GObject
 
 from gsecrets.const import APP_ID
 
@@ -39,12 +38,11 @@ class RecentManager(GObject.Object):
         self._save_items()
         self.emit(self.changed)
 
-    def _remove_items(self, items: list[Gio.File]) -> None:
-        for item in items:
-            self.recents.remove(item)
-
-        if bool(items):
-            self._save_items()
+    def remove_item(self, item: Gio.File) -> None:
+        if found := next(
+            (f for f in self.recents if f.get_uri() == item.get_uri()), None
+        ):
+            self.recents.remove(found)
             self.emit(self.changed)
 
     def is_empty(self) -> bool:
@@ -70,30 +68,3 @@ class RecentManager(GObject.Object):
     def _on_settings_changed(self, _settings, _key):
         self._load_items()
         self.emit(self.changed)
-
-    async def clean_non_existent(self) -> None:
-        to_remove = []
-
-        for item in self.recents:
-            try:
-                await item.query_info_async(
-                    Gio.FILE_ATTRIBUTE_STANDARD_TYPE,
-                    Gio.FileQueryInfoFlags.NONE,
-                    GLib.PRIORITY_DEFAULT,
-                    None,
-                )
-            except GLib.Error as err:
-                file_exist = not err.matches(
-                    Gio.io_error_quark(), Gio.IOErrorEnum.NOT_FOUND
-                )
-                if file_exist:
-                    logging.exception("Could not get file info")
-                else:
-                    uri = item.get_uri()
-                    logging.info("Ignoring nonexistent recent file: %s", uri)
-
-                to_remove.append(item)
-
-        # We remove items after we are done iterating to avoid undefined
-        # behaviour.
-        self._remove_items(to_remove)