Philipp Hörist pushed to branch master at gajim / gajim


Commits:
235f2a80 by Philipp Hörist at 2019-11-09T09:46:51Z
Refactor opening files

- Rename method
- Catch and log all exceptions

- - - - -


4 changed files:

- gajim/chat_control.py
- gajim/common/helpers.py
- gajim/gtk/filetransfer.py
- gajim/gtk/preferences.py


Changes:

=====================================
gajim/chat_control.py
=====================================
@@ -47,6 +47,7 @@ from gajim.common.helpers import AdditionalDataDict
 from gajim.common.helpers import open_uri
 from gajim.common.helpers import geo_provider_from_location
 from gajim.common.helpers import event_filter
+from gajim.common.helpers import open_file
 from gajim.common.contacts import GC_Contact
 from gajim.common.const import AvatarSize
 from gajim.common.const import KindConstant
@@ -1449,7 +1450,7 @@ class ChatControl(ChatControlBase):
     def _on_open_ft_folder(self, widget, file_props):
         path = os.path.split(file_props.file_name)[0]
         if os.path.exists(path) and os.path.isdir(path):
-            helpers.launch_file_manager(path)
+            open_file(path)
         ev = self._get_file_props_event(file_props, 'file-completed')
         if ev:
             app.events.remove_events(self.account, self.contact.jid, event=ev)


=====================================
gajim/common/helpers.py
=====================================
@@ -619,17 +619,6 @@ def get_contact_dict_for_account(account):
             contacts_dict[name] = contact
     return contacts_dict
 
-def launch_file_manager(path_to_open):
-    if os.name == 'nt':
-        try:
-            os.startfile(path_to_open) # if pywin32 is installed we open
-        except Exception:
-            pass
-    else:
-        if not path_to_open.startswith('file://'):
-            path_to_open = 'file://' + path_to_open
-        Gio.AppInfo.launch_default_for_uri(path_to_open)
-
 def play_sound(event):
     if not app.config.get('sounds_on'):
         return
@@ -1475,6 +1464,18 @@ def event_filter(filter_):
     return event_filter_decorator
 
 
+def catch_execptions(func):
+    @wraps(func)
+    def func_wrapper(self, *args, **kwargs):
+        try:
+            result = func(self, *args, **kwargs)
+        except Exception as error:
+            log.exception(error)
+            return None
+        return result
+    return func_wrapper
+
+
 def parse_uri_actions(uri):
     uri = uri[5:]
     if '?' not in uri:
@@ -1531,12 +1532,13 @@ def parse_uri(uri):
     return URI(type=URIType.WEB, data=uri)
 
 
+@catch_execptions
 def open_uri(uri, account=None):
     if not isinstance(uri, URI):
         uri = parse_uri(uri)
 
     if uri.type == URIType.FILE:
-        launch_file_manager(uri.data)
+        open_file(uri.data)
 
     elif uri.type == URIType.MAIL:
         uri = 'mailto:%s' % uri.data
@@ -1573,6 +1575,16 @@ def open_uri(uri, account=None):
         log.warning('Cant open URI: %s', uri)
 
 
+@catch_execptions
+def open_file(path):
+    if os.name == 'nt':
+        os.startfile(path)
+    else:
+        if not path.startswith('file://'):
+            path = 'file://' + path
+        Gio.AppInfo.launch_default_for_uri(path)
+
+
 def geo_provider_from_location(lat, lon):
     return ('https://www.openstreetmap.org/?'
             'mlat=%s&mlon=%s&zoom=16') % (lat, lon)


=====================================
gajim/gtk/filetransfer.py
=====================================
@@ -36,6 +36,7 @@ from gajim.common import app
 from gajim.common import helpers
 from gajim.common.i18n import _
 from gajim.common.file_props import FilesProp
+from gajim.common.helpers import open_file
 from gajim.common.modules.bytestream import (is_transfer_active,
                                              is_transfer_paused,
                                              is_transfer_stopped)
@@ -216,7 +217,7 @@ class FileTransfersWindow:
                 return
             path = os.path.split(file_props.file_name)[0]
             if os.path.exists(path) and os.path.isdir(path):
-                helpers.launch_file_manager(path)
+                open_file(path)
             self._ui.transfers_list.get_selection().unselect_all()
 
         if file_props.type_ == 'r':
@@ -1004,7 +1005,7 @@ class FileTransfersWindow:
             return
         path = os.path.split(file_props.file_name)[0]
         if os.path.exists(path) and os.path.isdir(path):
-            helpers.launch_file_manager(path)
+            open_file(path)
 
     def _on_cancel_menuitem_activate(self, widget):
         self._on_cancel_button_clicked(widget)


=====================================
gajim/gtk/preferences.py
=====================================
@@ -26,6 +26,7 @@ from gajim.common import idle
 from gajim.common.nec import NetworkEvent
 from gajim.common.i18n import _
 from gajim.common.i18n import ngettext
+from gajim.common.helpers import open_file
 
 from gajim import message_control
 
@@ -1081,8 +1082,7 @@ class Preferences(Gtk.ApplicationWindow):
         app.set_debug_mode(widget.get_active())
 
     def _on_debug_folder_clicked(self, _widget):
-        debug_folder = configpaths.get('DEBUG')
-        helpers.launch_file_manager(debug_folder)
+        open_file(configpaths.get('DEBUG'))
 
     # Advanced Config Editor (ACE)
     def on_open_advanced_editor_button_clicked(self, _widget):



View it on GitLab: 
https://dev.gajim.org/gajim/gajim/commit/235f2a805ee1068c71f65075c8b1191b8e2f5a87

-- 
View it on GitLab: 
https://dev.gajim.org/gajim/gajim/commit/235f2a805ee1068c71f65075c8b1191b8e2f5a87
You're receiving this email because of your account on dev.gajim.org.


_______________________________________________
Commits mailing list
Commits@gajim.org
https://lists.gajim.org/cgi-bin/listinfo/commits

Reply via email to