3
3
# SPDX-FileContributor: kramo
4
4
5
5
from collections .abc import Iterator
6
- from typing import Any
6
+ from typing import Any , Self
7
7
8
8
from gi .repository import Gdk , GdkPixbuf , Gio , GLib , GObject
9
9
10
10
from openemail import app
11
11
from openemail .core import client , model
12
12
from openemail .core .client import WriteError , user
13
13
from openemail .core .crypto import KeyPair
14
- from openemail .core .model import Address
14
+ from openemail .core .model import Address , User
15
15
16
16
from . import Notifier
17
17
@@ -149,6 +149,8 @@ class Profile(GObject.Object):
149
149
_name : str | None = None
150
150
_image : Gdk .Paintable | None = None
151
151
152
+ _user : Self | None = None
153
+
152
154
def set_from_profile (self , profile : model .Profile | None ) -> None :
153
155
"""Set the properties of `self` from `profile`."""
154
156
self ._profile = profile
@@ -215,13 +217,26 @@ def image(self, image: Gdk.Paintable | None) -> None:
215
217
self ._image = image
216
218
self .has_image = bool (image )
217
219
218
- @staticmethod
219
- def of (address : Address ) -> "Profile" :
220
- """Get the profile associated with `address`."""
221
- from .store import profiles # noqa: PLC0415
220
+ @classmethod
221
+ def of (cls , user : Address | User , / ) -> "Profile" :
222
+ """Get the profile associated with `user`.
223
+
224
+ If `user` is a User object instead of an Address,
225
+ returns a `Profile` object that always represents the data of
226
+ the currently logged in user, even after a relogin.
227
+ """
228
+ match user :
229
+ case Address ():
230
+ from .store import profiles # noqa: PLC0415
231
+
232
+ (profile := profiles [user ]).address = str (user )
233
+ return profile
222
234
223
- (profile := profiles [address ]).address = str (address )
224
- return profile
235
+ case User ():
236
+ if not cls ._user :
237
+ cls ._user = cls ()
238
+
239
+ return cls ._user
225
240
226
241
def value_of (self , ident : str ) -> Any :
227
242
"""Get the value of the field identified by `ident` in `self`."""
@@ -244,10 +259,10 @@ def set_receives_broadcasts(self, value: bool) -> None:
244
259
245
260
async def refresh () -> None :
246
261
"""Update the profile of the user by fetching new data remotely."""
247
- from . store import user_profile # noqa: PLC0415
248
-
249
- user_profile . updating = True
250
- user_profile . set_from_profile ( profile := await client . fetch_profile ( user . address ) )
262
+ Profile . of ( client . user ). updating = True
263
+ Profile . of ( client . user ). set_from_profile (
264
+ profile := await client . fetch_profile ( user . address )
265
+ )
251
266
252
267
if profile :
253
268
user .signing_keys = KeyPair (
@@ -262,15 +277,15 @@ async def refresh() -> None:
262
277
)
263
278
264
279
try :
265
- user_profile .image = Gdk .Texture .new_from_bytes (
280
+ Profile . of ( client . user ) .image = Gdk .Texture .new_from_bytes (
266
281
GLib .Bytes .new (await client .fetch_profile_image (user .address ))
267
282
)
268
283
except GLib .Error :
269
- user_profile .image = None
284
+ Profile . of ( client . user ) .image = None
270
285
271
- Profile .of (user .address ).image = user_profile .image
286
+ Profile .of (user .address ).image = Profile . of ( client . user ) .image
272
287
Profile .of (user .address ).set_from_profile (profile )
273
- user_profile .updating = False
288
+ Profile . of ( client . user ) .updating = False
274
289
275
290
276
291
async def update (values : dict [str , str ]) -> None :
0 commit comments