Philipp Hörist pushed to branch master at gajim / gajim
Commits:
-
a3d7d6a3
by mjk at 2022-11-24T19:43:00+00:00
5 changed files:
- gajim/common/preview.py
- gajim/common/preview_helpers.py
- gajim/gtk/chat_list_row.py
- gajim/gtk/chat_stack.py
- gajim/gtk/preview.py
Changes:
| ... | ... | @@ -282,12 +282,11 @@ def is_previewable(self, |
| 282 | 282 | if not self._accept_uri(urlparts, uri, additional_data):
|
| 283 | 283 | return False
|
| 284 | 284 | |
| 285 | - if uri.startswith('geo:'):
|
|
| 285 | + if urlparts.scheme == 'geo':
|
|
| 286 | 286 | try:
|
| 287 | 287 | split_geo_uri(uri)
|
| 288 | 288 | except Exception as err:
|
| 289 | - log.error(uri)
|
|
| 290 | - log.error(err)
|
|
| 289 | + log.info('Bad geo URI %s: %s', uri, err)
|
|
| 291 | 290 | return False
|
| 292 | 291 | |
| 293 | 292 | return True
|
| ... | ... | @@ -47,6 +47,7 @@ |
| 47 | 47 | |
| 48 | 48 | from gajim.common.helpers import sanitize_filename
|
| 49 | 49 | from gajim.common.i18n import _
|
| 50 | +from gajim.common.i18n import P_
|
|
| 50 | 51 | |
| 51 | 52 | log = logging.getLogger('gajim.c.preview_helpers')
|
| 52 | 53 | |
| ... | ... | @@ -382,6 +383,26 @@ def split_geo_uri(uri: str) -> Coords: |
| 382 | 383 | return Coords(location=location, lat=lat, lon=lon)
|
| 383 | 384 | |
| 384 | 385 | |
| 386 | +def format_geo_coords(lat: float, lon: float) -> str:
|
|
| 387 | + def fmt(f: float) -> str:
|
|
| 388 | + i = int(round(f * 3600.0))
|
|
| 389 | + seconds = i % 60
|
|
| 390 | + i //= 60
|
|
| 391 | + minutes = i % 60
|
|
| 392 | + i //= 60
|
|
| 393 | + degrees = i
|
|
| 394 | + return '%d°%02d′%02d′′' % (degrees, minutes, seconds)
|
|
| 395 | + if lat >= 0:
|
|
| 396 | + slat = P_('positive latitude', '%sN') % fmt(lat)
|
|
| 397 | + else:
|
|
| 398 | + slat = P_('negative latitude', '%sS') % fmt(-lat)
|
|
| 399 | + if lon >= 0:
|
|
| 400 | + slon = P_('positive longitude', '%sE') % fmt(lon)
|
|
| 401 | + else:
|
|
| 402 | + slon = P_('negative longitude', '%sW') % fmt(-lon)
|
|
| 403 | + return '%s %s' % (slat, slon)
|
|
| 404 | + |
|
| 405 | + |
|
| 385 | 406 | def filename_from_uri(uri: str) -> str:
|
| 386 | 407 | urlparts = urlparse(unquote(uri))
|
| 387 | 408 | path = Path(urlparts.path)
|
| ... | ... | @@ -389,7 +410,7 @@ def filename_from_uri(uri: str) -> str: |
| 389 | 410 | |
| 390 | 411 | |
| 391 | 412 | def aes_decrypt(key: bytes, iv: bytes, payload: bytes) -> bytes:
|
| 392 | - # Use AES128 GCM with the given key and iv to decrypt the payload
|
|
| 413 | + # Use AES256 GCM with the given key and iv to decrypt the payload
|
|
| 393 | 414 | data = payload[:-16]
|
| 394 | 415 | tag = payload[-16:]
|
| 395 | 416 | decryptor = Cipher(algorithms.AES(key),
|
| ... | ... | @@ -447,8 +468,6 @@ def guess_simple_file_type(file_path: str, |
| 447 | 468 | if mime_type == 'application/octet-stream':
|
| 448 | 469 | mime_type = ''
|
| 449 | 470 | icon = get_icon_for_mime_type(mime_type)
|
| 450 | - if file_path.startswith('geo:'):
|
|
| 451 | - return icon, _('Location')
|
|
| 452 | 471 | if mime_type.startswith('audio/'):
|
| 453 | 472 | return icon, _('Audio File')
|
| 454 | 473 | if mime_type.startswith('image/'):
|
| ... | ... | @@ -18,6 +18,7 @@ |
| 18 | 18 | from typing import Any
|
| 19 | 19 | |
| 20 | 20 | import pickle
|
| 21 | +from urllib.parse import urlparse
|
|
| 21 | 22 | |
| 22 | 23 | from gi.repository import Gdk
|
| 23 | 24 | from gi.repository import Gio
|
| ... | ... | @@ -39,6 +40,8 @@ |
| 39 | 40 | from gajim.common.helpers import get_uf_relative_time
|
| 40 | 41 | from gajim.common.helpers import message_needs_highlight
|
| 41 | 42 | from gajim.common.helpers import AdditionalDataDict
|
| 43 | +from gajim.common.preview_helpers import split_geo_uri
|
|
| 44 | +from gajim.common.preview_helpers import format_geo_coords
|
|
| 42 | 45 | from gajim.common.preview_helpers import filename_from_uri
|
| 43 | 46 | from gajim.common.preview_helpers import guess_simple_file_type
|
| 44 | 47 | from gajim.common.types import ChatContactT
|
| ... | ... | @@ -252,9 +255,16 @@ def set_message_text(self, |
| 252 | 255 | icon = Gio.Icon.new_for_string(icon_name)
|
| 253 | 256 | if additional_data is not None:
|
| 254 | 257 | if app.preview_manager.is_previewable(text, additional_data):
|
| 255 | - file_name = filename_from_uri(text)
|
|
| 256 | - icon, file_type = guess_simple_file_type(text)
|
|
| 257 | - text = f'{file_type} ({file_name})'
|
|
| 258 | + scheme = urlparse(text).scheme
|
|
| 259 | + if scheme == 'geo':
|
|
| 260 | + location = split_geo_uri(text)
|
|
| 261 | + icon = Gio.Icon.new_for_string('mark-location')
|
|
| 262 | + text = format_geo_coords(
|
|
| 263 | + float(location.lat), float(location.lon))
|
|
| 264 | + else:
|
|
| 265 | + file_name = filename_from_uri(text)
|
|
| 266 | + icon, file_type = guess_simple_file_type(text)
|
|
| 267 | + text = f'{file_type} ({file_name})'
|
|
| 258 | 268 | |
| 259 | 269 | text = GLib.markup_escape_text(text)
|
| 260 | 270 | if text.startswith('/me') and nickname is not None:
|
| ... | ... | @@ -20,6 +20,7 @@ |
| 20 | 20 | import sys
|
| 21 | 21 | import logging
|
| 22 | 22 | import time
|
| 23 | +from urllib.parse import urlparse
|
|
| 23 | 24 | |
| 24 | 25 | from gi.repository import Gdk
|
| 25 | 26 | from gi.repository import GLib
|
| ... | ... | @@ -40,6 +41,8 @@ |
| 40 | 41 | from gajim.common.modules.contacts import BareContact
|
| 41 | 42 | from gajim.common.modules.contacts import GroupchatContact
|
| 42 | 43 | from gajim.common.modules.contacts import GroupchatParticipant
|
| 44 | +from gajim.common.preview_helpers import split_geo_uri
|
|
| 45 | +from gajim.common.preview_helpers import format_geo_coords
|
|
| 43 | 46 | from gajim.common.structs import OutgoingMessage
|
| 44 | 47 | from gajim.common.types import ChatContactT
|
| 45 | 48 | from gajim.gui.dialogs import ErrorDialog
|
| ... | ... | @@ -389,8 +392,11 @@ def _issue_notification(self, event: events.MessageReceived) -> None: |
| 389 | 392 | is_previewable = app.preview_manager.is_previewable(
|
| 390 | 393 | text, additional_data)
|
| 391 | 394 | if is_previewable:
|
| 392 | - if text.startswith('geo:'):
|
|
| 393 | - text = _('Location')
|
|
| 395 | + scheme = urlparse(text).scheme
|
|
| 396 | + if scheme == 'geo':
|
|
| 397 | + location = split_geo_uri(text)
|
|
| 398 | + text = format_geo_coords(
|
|
| 399 | + float(location.lat), float(location.lon))
|
|
| 394 | 400 | else:
|
| 395 | 401 | file_name = preview_helpers.filename_from_uri(text)
|
| 396 | 402 | _icon, file_type = preview_helpers.guess_simple_file_type(text)
|
| ... | ... | @@ -33,6 +33,7 @@ |
| 33 | 33 | from gajim.common.i18n import _
|
| 34 | 34 | from gajim.common.preview import Preview
|
| 35 | 35 | from gajim.common.preview_helpers import split_geo_uri
|
| 36 | +from gajim.common.preview_helpers import format_geo_coords
|
|
| 36 | 37 | from gajim.common.preview_helpers import contains_audio_streams
|
| 37 | 38 | from gajim.common.preview_helpers import get_icon_for_mime_type
|
| 38 | 39 | from gajim.common.types import GdkPixbufType
|
| ... | ... | @@ -136,12 +137,10 @@ def update(self, preview: Preview, data: Optional[GdkPixbufType]) -> None: |
| 136 | 137 | self._ui.download_button.hide()
|
| 137 | 138 | |
| 138 | 139 | location = split_geo_uri(preview.uri)
|
| 139 | - self._ui.file_size.set_text(_('Lat: %s Lon: %s') % (
|
|
| 140 | - round(float(location.lat), 5),
|
|
| 141 | - round(float(location.lon), 5)))
|
|
| 140 | + text = format_geo_coords(float(location.lat), float(location.lon))
|
|
| 141 | + self._ui.file_size.set_text(text)
|
|
| 142 | 142 | self._ui.image_button.set_tooltip_text(
|
| 143 | - _('Location at Lat: %s Lon: %s') % (
|
|
| 144 | - location.lat, location.lon))
|
|
| 143 | + _('Location at %s') % text)
|
|
| 145 | 144 | self._ui.preview_box.set_size_request(160, -1)
|
| 146 | 145 | return
|
| 147 | 146 |