From 4a7f432091c90cc5f7f24e8eb294cee7fe50500a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Thu, 26 Jan 2012 12:46:15 +0100 Subject: [PATCH] Remove "Files Actions" and instead use xdg issue2265 review223002 --- doc/usage.rst | 18 ------ tryton/common/common.py | 98 +++++++++++++------------------- tryton/config.py | 25 -------- tryton/gui/main.py | 15 ----- tryton/gui/window/__init__.py | 1 - tryton/gui/window/fileactions.py | 88 ---------------------------- 6 files changed, 38 insertions(+), 207 deletions(-) delete mode 100644 tryton/gui/window/fileactions.py diff --git a/doc/usage.rst b/doc/usage.rst index 2caabd13bc..a4ee6ef68d 100644 --- a/doc/usage.rst +++ b/doc/usage.rst @@ -581,24 +581,6 @@ Tabs Position * Right * Bottom -.. _Menu-Options-File_Actions: - -File Actions...: - Opens a dialog to set up file types for print and open actions. - Use ``"%s"`` as a placeholder for the document name. - - * Provided file types: - - - ODT file: Open Office Writer Document - - PDF file: Adobes(TM) Portable Document Format - - PNG file: Portable Network Graphics format - - TXT file: Pure text file - - * Provided actions - - - Open: Setting up program system call which opens the specific file type. - - Print: Setting up program system call printing the specific file type. - .. _Menu-Options-Email: Email...: diff --git a/tryton/common/common.py b/tryton/common/common.py index 8e14be3fc7..01d607a5c4 100644 --- a/tryton/common/common.py +++ b/tryton/common/common.py @@ -404,47 +404,7 @@ def file_selection(title, filename='', return filenames def file_open(filename, type, print_p=False): - if os.name == 'nt': - operation = 'open' - if print_p: - operation = 'print' - try: - os.startfile(os.path.normpath(filename), operation) - except WindowsError: - save_name = file_selection(_('Save As...'), - action=gtk.FILE_CHOOSER_ACTION_SAVE) - if save_name: - file_p = open(filename, 'rb') - save_p = open(save_name, 'wb+') - save_p.write(file_p.read()) - save_p.close() - file_p.close() - return - elif sys.platform == 'darwin': - pid = os.fork() - if not pid: - pid = os.fork() - if not pid: - try: - os.execv('/usr/bin/open', ['/usr/bin/open', filename]) - except OSError: - sys.exit(0) - time.sleep(0.1) - sys.exit(0) - os.waitpid(pid, 0) - return - cmd = '' - if isinstance(CONFIG['client.actions'], basestring): - CONFIG['client.actions'] = safe_eval(CONFIG['client.actions']) - if type in CONFIG['client.actions']: - if print_p: - cmd = CONFIG['client.actions'][type][1] - else: - cmd = CONFIG['client.actions'][type][0] - if not cmd: - #TODO add dialog box - pass - if not cmd: + def save(): save_name = file_selection(_('Save As...'), action=gtk.FILE_CHOOSER_ACTION_SAVE) if save_name: @@ -453,25 +413,26 @@ def file_open(filename, type, print_p=False): save_p.write(file_p.read()) save_p.close() file_p.close() - return - cmd = cmd % filename - args = shlex.split(str(cmd)) - prog = find_in_path(args[0]) - args[0] = os.path.basename(args[0]) - if print_p: - os.spawnv(os.P_WAIT, prog, args) - return - pid = os.fork() - if not pid: - pid = os.fork() - if not pid: - try: - os.execv(prog, args) - except OSError: - sys.exit(0) - time.sleep(0.1) - sys.exit(0) - os.waitpid(pid, 0) + + if os.name == 'nt': + operation = 'open' + if print_p: + operation = 'print' + try: + os.startfile(os.path.normpath(filename), operation) + except WindowsError: + save() + elif sys.platform == 'darwin': + try: + subprocess.Popen(['/usr/bin/open', filename]) + except OSError: + save() + else: + try: + subprocess.Popen(['xdg-open', filename]) + except OSError: + save() + def mailto(to=None, cc=None, subject=None, body=None, attachment=None): if CONFIG['client.email']: @@ -485,6 +446,23 @@ def mailto(to=None, cc=None, subject=None, body=None, attachment=None): args = shlex.split(str(cmd)) subprocess.Popen(args) return + if os.name != 'nt' and sys.platform != 'darwin': + args = ['xdg-email', '--utf8'] + if cc: + args.extend(['--cc', cc]) + if subject: + args.extend(['--subject', subject]) + if body: + args.extend(['--body', body]) + if attachment: + args.extend(['--attach', attachment]) + if to: + args.append(to) + try: + subprocess.Popen(args) + return + except OSError: + pass #http://www.faqs.org/rfcs/rfc2368.html url = "mailto:" if to: diff --git a/tryton/config.py b/tryton/config.py index 4683034d29..5ca577833b 100644 --- a/tryton/config.py +++ b/tryton/config.py @@ -29,20 +29,6 @@ def get_config_dir(): if not os.path.isdir(get_config_dir()): os.makedirs(get_config_dir(), 0700) -def find_path(progs, args): - if os.name == 'nt': - return '' - if sys.platform == 'darwin': - return '' - paths = [x for x in os.environ['PATH'].split(':') - if os.path.isdir(x)] - for dir in paths: - for prog in progs: - val = os.path.join(dir, prog) - if os.path.isfile(val) or os.path.islink(val): - return val + ' ' + args - return '' - class ConfigManager(object): "Config manager" @@ -78,17 +64,6 @@ class ConfigManager(object): 'client.default_path': get_home_dir(), 'client.lang': locale.getdefaultlocale()[0], 'client.language_direction': 'ltr', - 'client.actions': { - 'odt': {0: find_path(['ooffice', 'ooffice2', 'libreoffice'], '"%s"'), - 1: find_path(['ooffice', 'ooffice2', 'libreoffice'], '-p "%s"')}, - 'txt': {0: find_path(['ooffice', 'ooffice2', 'libreoffice'], '"%s"'), - 1: find_path(['ooffice', 'ooffice2', 'libreoffice'], '-p "%s"')}, - 'pdf': {0: find_path(['evince', 'xpdf', 'gpdf', - 'kpdf', 'epdfview', 'acroread'], '"%s"'), 1: ''}, - 'png': {0: find_path(['feh', 'display', 'qiv', 'eye'], '"%s"'), 1: ''}, - 'csv': {0: find_path(['ooffice', 'ooffice2', 'libreoffice'], '"%s"'), - 1: find_path(['ooffice', 'ooffice2', 'libreoffice'], '-p "%s"')}, - }, 'client.email': '', 'client.can_change_accelerators': False, 'client.limit': 1000, diff --git a/tryton/gui/main.py b/tryton/gui/main.py index e50b6c48a7..1207cf9807 100644 --- a/tryton/gui/main.py +++ b/tryton/gui/main.py @@ -23,7 +23,6 @@ from tryton.exceptions import TrytonServerError, TrytonError from tryton.gui.window import Window from tryton.gui.window.preference import Preference from tryton.gui.window import Limit -from tryton.gui.window import FileActions from tryton.gui.window import Email from tryton.gui.window.dblogin import DBLogin from tryton.gui.window.dbcreate import DBCreate @@ -189,11 +188,6 @@ class Main(object): self.sig_statusbar_show() - if os.name == 'nt' or sys.platform == 'darwin': - # Disable actions, on win32 we use os.startfile - # and on mac we use /usr/bin/open - self.menuitem_actions.set_sensitive(False) - # Adding a timer the check to requests gobject.timeout_add(5 * 60 * 1000, self.request_set) _MAIN.append(self) @@ -631,12 +625,6 @@ class Main(object): menuitem_limit.set_accel_path('/Options/Search Limit') menu_options.add(menuitem_limit) - menuitem_actions = gtk.MenuItem(_('File _Actions...')) - self.menuitem_actions = menuitem_actions - menuitem_actions.connect('activate', self.sig_file_actions) - menuitem_actions.set_accel_path('/Options/File Actions') - menu_options.add(menuitem_actions) - menuitem_email = gtk.MenuItem(_('_Email...')) self.menuitem_email = menuitem_email menuitem_email.connect('activate', self.sig_email) @@ -817,9 +805,6 @@ class Main(object): def sig_limit(self, widget): Limit().run() - def sig_file_actions(self, widget): - FileActions().run() - def sig_email(self, widget): Email().run() diff --git a/tryton/gui/window/__init__.py b/tryton/gui/window/__init__.py index 3558718664..d0d616fc01 100644 --- a/tryton/gui/window/__init__.py +++ b/tryton/gui/window/__init__.py @@ -1,6 +1,5 @@ #This file is part of Tryton. The COPYRIGHT file at the top level of #this repository contains the full copyright notices and license terms. from window import * -from fileactions import * from email import * from limit import * diff --git a/tryton/gui/window/fileactions.py b/tryton/gui/window/fileactions.py deleted file mode 100644 index 403365662a..0000000000 --- a/tryton/gui/window/fileactions.py +++ /dev/null @@ -1,88 +0,0 @@ -#This file is part of Tryton. The COPYRIGHT file at the top level of -#this repository contains the full copyright notices and license terms. -"File actions" -import gtk -import gettext -from tryton.config import TRYTON_ICON, CONFIG -from tryton.common import safe_eval, get_toplevel_window - -_ = gettext.gettext - - -class FileActions(object): - "File actions window" - - def __init__(self): - self.parent= get_toplevel_window() - self.win = gtk.Dialog(_('File Actions'), self.parent, - gtk.DIALOG_MODAL|gtk.DIALOG_DESTROY_WITH_PARENT, - (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, - gtk.STOCK_OK, gtk.RESPONSE_OK)) - self.win.set_default_response(gtk.RESPONSE_OK) - self.win.set_icon(TRYTON_ICON) - self.win.set_has_separator(True) - self.win.vbox.pack_start(gtk.Label( - _('Edit Files Actions')), expand=False, fill=True) - self.win.vbox.pack_start(gtk.HSeparator()) - self.treeview = gtk.TreeView() - - self.model = gtk.ListStore(str, str, str, str) - self.treeview.set_model(self.model) - - for index, text in enumerate((_('File Type'), _('Open'), _('Print'))): - renderer = gtk.CellRendererText() - if index != 0: - renderer.set_property('editable', True) - renderer.connect('edited', self._sig_edited) - column = gtk.TreeViewColumn(text, renderer, text=index + 1) - column.set_resizable(True) - self.treeview.append_column(column) - - i = 1 - if isinstance(CONFIG['client.actions'], basestring): - CONFIG['client.actions'] = safe_eval(CONFIG['client.actions']) - extensions = CONFIG['client.actions'].keys() - extensions.sort() - for extension in extensions: - iter = self.model.append() - self.model.set(iter, 0, extension, - 1, _('%s file') % extension.upper(), - 2, CONFIG['client.actions'][extension][0], - 3, CONFIG['client.actions'][extension][1]) - - scroll = gtk.ScrolledWindow() - scroll.add(self.treeview) - scroll.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) - viewport= gtk.Viewport() - viewport.set_shadow_type(gtk.SHADOW_ETCHED_IN) - viewport.set_size_request(400, 200) - viewport.add(scroll) - self.win.vbox.pack_start(viewport, expand=True, fill=True) - - label = gtk.Label(_('Use "%s" as a placeholder for the file name')) - label.set_alignment(0.0, 0.5) - label.set_padding(0, 10) - self.win.vbox.pack_start(label, expand=False, fill=True) - self.win.show_all() - - def run(self): - "Run the window" - res = self.win.run() - if res == gtk.RESPONSE_OK: - config = {} - for extension, _, cmd_open, cmd_print in self.model: - config[extension] = { - 0: cmd_open, - 1: cmd_print, - } - CONFIG['client.actions'] = config - CONFIG.save() - self.parent.present() - self.win.destroy() - return res - - def _sig_edited(self, cell, path, new_text): - iter = self.model.get_iter_from_string(path) - (path, column) = self.treeview.get_cursor() - column_id = self.treeview.get_columns().index(column) - self.model.set(iter, column_id + 1, new_text)