File: entry_row.py

package info (click to toggle)
secrets 12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,616 kB
  • sloc: python: 6,838; xml: 7; makefile: 4
file content (244 lines) | stat: -rw-r--r-- 7,897 bytes parent folder | download
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# SPDX-License-Identifier: GPL-3.0-only
from __future__ import annotations

import typing
from gettext import gettext as _

from gi.repository import Adw, GLib, GObject, Gtk

from gsecrets.safe_element import EntryColor, SafeEntry

if typing.TYPE_CHECKING:
    from gsecrets.unlocked_database import UnlockedDatabase


@Gtk.Template(resource_path="/org/gnome/World/Secrets/gtk/entry_row.ui")
class EntryRow(Adw.Bin):
    __gtype_name__ = "EntryRow"

    _prefix_stack = Gtk.Template.Child()
    _entry_icon = Gtk.Template.Child()
    _selection_checkbox = Gtk.Template.Child()
    _entry_copy_otp_button = Gtk.Template.Child()
    _entry_copy_user_button = Gtk.Template.Child()
    _entry_copy_pass_button = Gtk.Template.Child()

    _safe_entry = None

    title = GObject.Property(type=str, default="")
    subtitle = GObject.Property(type=str, default="")

    def __init__(self, database: UnlockedDatabase) -> None:
        super().__init__()

        self._signals = GObject.SignalGroup.new(SafeEntry)
        self._bindings = GObject.BindingGroup.new()

        self._bindings.bind(
            "icon-name",
            self._entry_icon,
            "icon-name",
            GObject.BindingFlags.SYNC_CREATE,
        )
        self._bindings.bind(
            "selected",
            self._selection_checkbox,
            "active",
            GObject.BindingFlags.BIDIRECTIONAL | GObject.BindingFlags.SYNC_CREATE,
        )
        self._bindings.bind(
            "sensitive",
            self,
            "sensitive",
            GObject.BindingFlags.BIDIRECTIONAL | GObject.BindingFlags.SYNC_CREATE,
        )

        self._signals.connect_closure(
            "notify::name",
            self._on_entry_name_changed,
            False,
        )
        self._signals.connect_closure(
            "notify::username",
            self._on_entry_username_changed,
            False,
        )
        self._signals.connect_closure(
            "notify::password",
            self._on_entry_password_changed,
            False,
        )
        self._signals.connect_closure(
            "notify::otp",
            self._on_entry_opt_token_changed,
            False,
        )
        self._signals.connect_closure(
            "notify::color",
            self._on_entry_color_changed,
            False,
        )
        self._signals.connect_closure(
            "notify::expired",
            self._on_entry_notify_expired,
            False,
        )
        self._signals.connect_closure(
            "notify::selected",
            self._on_selected_notify,
            False,
        )

        self.unlocked_database = database
        self.db_manager = database.database_manager

        self.unlocked_database.connect(
            "notify::selection-mode",
            self._on_selection_mode_notify,
        )

    @GObject.Property(type=SafeEntry)
    def safe_entry(self):
        return self._safe_entry

    @safe_entry.setter  # type: ignore
    def safe_entry(self, element):
        self._safe_entry = element

        self._signals.props.target = element
        self._bindings.props.source = element

        self._on_entry_name_changed(element, None)
        self._on_entry_username_changed(element, None)
        self._on_entry_password_changed(element, None)
        self._on_entry_opt_token_changed(element, None)
        self._on_entry_color_changed(element, None)
        self._on_entry_notify_expired(element, None)
        self._on_selection_mode_notify(self.unlocked_database, None)

    def _on_entry_notify_expired(self, safe_entry, _gparam):
        if safe_entry.expired and safe_entry.props.expires:
            self.add_css_class("strikethrough")
        else:
            self.remove_css_class("strikethrough")

    @Gtk.Template.Callback()
    def _on_entry_row_button_pressed(
        self,
        gesture: Gtk.GestureClick,
        _n_press: int,
        _event_x: float,
        _event_y: float,
    ) -> None:
        # pylint: disable=too-many-arguments
        gesture.set_state(Gtk.EventSequenceState.CLAIMED)
        db_view: UnlockedDatabase = self.unlocked_database
        db_view.start_database_lock_timer()

        if db_view.props.selection_mode:
            selected = self._safe_entry.selected  # type: ignore
            self._safe_entry.selected = not selected  # type: ignore
        else:
            db_view.props.selection_mode = True
            self._safe_entry.selected = True  # type: ignore

    def _on_selected_notify(self, entry, _pspec):
        self.unlocked_database.start_database_lock_timer()

        if entry.props.selected:
            self.unlocked_database.add_selection(entry)
        else:
            self.unlocked_database.remove_selection(entry)

    @Gtk.Template.Callback()
    def on_entry_copy_pass_button_clicked(self, _button):
        self.unlocked_database.send_to_clipboard(
            self._safe_entry.props.password,
            _("Password copied"),
        )

    @Gtk.Template.Callback()
    def on_entry_copy_user_button_clicked(self, _button):
        self.unlocked_database.send_to_clipboard(
            self._safe_entry.props.username,
            _("Username copied"),
        )

    @Gtk.Template.Callback()
    def on_entry_copy_otp_button_clicked(self, _button):
        if token := self._safe_entry.otp_token():
            self.unlocked_database.send_to_clipboard(
                token,
                _("One-Time Password copied"),
            )

    def _on_entry_name_changed(
        self,
        safe_entry: SafeEntry,
        _value: GObject.ParamSpec,
    ) -> None:
        entry_name = GLib.markup_escape_text(safe_entry.props.name)
        if entry_name:
            self.remove_css_class("italic-title")
            self.props.title = entry_name
        else:
            self.add_css_class("italic-title")
            self.props.title = _("Title not Specified")

    def _on_entry_username_changed(
        self,
        safe_entry: SafeEntry,
        _value: GObject.ParamSpec,
    ) -> None:
        entry_username = GLib.markup_escape_text(safe_entry.props.username)
        self.props.subtitle = entry_username
        self._entry_copy_user_button.set_visible(bool(entry_username))

    def _on_entry_password_changed(
        self,
        safe_entry: SafeEntry,
        _value: GObject.ParamSpec,
    ) -> None:
        entry_password = GLib.markup_escape_text(safe_entry.props.password)
        self._entry_copy_pass_button.set_visible(bool(entry_password))

    def _on_entry_opt_token_changed(
        self,
        safe_entry: SafeEntry,
        _value: GObject.ParamSpec,
    ) -> None:
        entry_otp_token = safe_entry.otp_token()
        if entry_otp_token:
            self._entry_copy_otp_button.props.visible = True
        else:
            self._entry_copy_otp_button.props.visible = False

    def _on_entry_color_changed(
        self,
        safe_entry: SafeEntry,
        _value: GObject.ParamSpec,
    ) -> None:
        # Clear current style
        for color in EntryColor:
            css_class = color.css_class()
            self._entry_icon.remove_css_class(css_class)

        color = safe_entry.props.color
        css_class = EntryColor(color).css_class()
        self._entry_icon.add_css_class(css_class)

    def _on_selection_mode_notify(self, unlocked_db, _pspec):
        selection_mode = unlocked_db.props.selection_mode

        if selection_mode:
            visible_child = self._selection_checkbox
        else:
            visible_child = self._entry_icon

        self._prefix_stack.props.visible_child = visible_child

    @Gtk.Template.Callback()
    def _on_long_press_gesture_pressed(self, gesture, _x, _y):
        gesture.set_state(Gtk.EventSequenceState.CLAIMED)
        self.unlocked_database.props.selection_mode = True
        self._safe_entry.props.selected = not self._safe_entry.props.selected