diff --git a/tryton/common/common.py b/tryton/common/common.py index 6cb1c56fde..83fefe9e7a 100644 --- a/tryton/common/common.py +++ b/tryton/common/common.py @@ -433,7 +433,11 @@ def file_selection(title, filename='', win.set_icon(TRYTON_ICON) win.set_current_folder(CONFIG['client.default_path']) if filename: - win.set_current_name(filename) + if action in (gtk.FILE_CHOOSER_ACTION_SAVE, + gtk.FILE_CHOOSER_ACTION_CREATE_FOLDER): + win.set_current_name(filename) + else: + win.set_filename(filename) win.set_select_multiple(multi) win.set_default_response(gtk.RESPONSE_OK) if filters is not None: diff --git a/tryton/gui/window/view_form/view/form_gtk/binary.py b/tryton/gui/window/view_form/view/form_gtk/binary.py index 2f6a4b471f..4683caef5a 100644 --- a/tryton/gui/window/view_form/view/form_gtk/binary.py +++ b/tryton/gui/window/view_form/view/form_gtk/binary.py @@ -5,7 +5,7 @@ import gettext import os import tempfile from tryton.common import common -from tryton.common import file_selection, Tooltips, file_open +from tryton.common import file_selection, Tooltips, file_open, slugify from interface import WidgetInterface _ = gettext.gettext @@ -78,6 +78,8 @@ class Binary(WidgetInterface): self.tooltips.set_tip(self.but_remove, _('Clear')) self.widget.pack_start(self.but_remove, expand=False, fill=False) + self.last_open_file = None + self.tooltips.enable() @property @@ -108,7 +110,12 @@ class Binary(WidgetInterface): return self.wid_size.grab_focus() def sig_new(self, widget=None): - filename = file_selection(_('Open...')) + filename = '' + if self.last_open_file: + last_id, last_filename = self.last_open_file + if last_id == self.record.id: + filename = last_filename + filename = file_selection(_('Open...'), filename=filename) if filename and self.field: self.field.set_client(self.record, open(filename, 'rb').read()) if self.filename_field: @@ -119,10 +126,11 @@ class Binary(WidgetInterface): if not self.filename_field: return dtemp = tempfile.mkdtemp(prefix='tryton_') - filename = self.filename_field.get(self.record).replace( - os.sep, '_').replace(os.altsep or os.sep, '_') + filename = self.filename_field.get(self.record) if not filename: return + root, ext = os.path.splitext(filename) + filename = ''.join([slugify(root), os.extsep, slugify(ext)]) file_path = os.path.join(dtemp, filename) with open(file_path, 'wb') as fp: fp.write(self.field.get_data(self.record)) @@ -130,6 +138,7 @@ class Binary(WidgetInterface): if type_: type_ = type_[1:] file_open(file_path, type_) + self.last_open_file = (self.record.id, file_path) def sig_save_as(self, widget=None): filename = '' diff --git a/tryton/gui/window/view_form/view/form_gtk/image.py b/tryton/gui/window/view_form/view/form_gtk/image.py index dc30c039b9..ba5bdd0df4 100644 --- a/tryton/gui/window/view_form/view/form_gtk/image.py +++ b/tryton/gui/window/view_form/view/form_gtk/image.py @@ -5,7 +5,7 @@ import glib import gettext import os import tempfile -from tryton.common import file_selection, Tooltips, file_open +from tryton.common import file_selection, Tooltips, file_open, slugify from tryton.config import PIXMAPS_DIR from interface import WidgetInterface import urllib @@ -141,8 +141,11 @@ class Image(WidgetInterface): if not self.filename_field: return dtemp = tempfile.mkdtemp(prefix='tryton_') - filename = self.filename_field.get(self.record).replace( - os.sep, '_').replace(os.altsep or os.sep, '_') + filename = self.filename_field.get(self.record) + if not filename: + return + root, ext = os.path.splitext(filename) + filename = ''.join([slugify(root), os.extsep, slugify(ext)]) file_path = os.path.join(dtemp, filename) with open(file_path, 'wb') as fp: fp.write(self.field.get_data(self.record)) diff --git a/tryton/gui/window/view_form/view/list_gtk/parser.py b/tryton/gui/window/view_form/view/list_gtk/parser.py index 7e483b31bd..a7e3f9957a 100644 --- a/tryton/gui/window/view_form/view/list_gtk/parser.py +++ b/tryton/gui/window/view_form/view/list_gtk/parser.py @@ -18,7 +18,7 @@ from tryton.gui.window.win_form import WinForm from tryton.gui.window.view_form.screen import Screen import tryton.rpc as rpc from tryton.common import COLORS, node_attributes, \ - file_selection, file_open + file_selection, file_open, slugify import tryton.common as common from tryton.common.cellrendererbutton import CellRendererButton from tryton.common.cellrendererdate import CellRendererDate @@ -606,6 +606,7 @@ class Binary(Char): self.renderer.connect('open', self.open_binary) self.renderer.connect('save', self.save_binary) self.renderer.connect('clear', self.clear_binary) + self.last_open_file = None def get_textual_value(self, record): pass @@ -638,8 +639,13 @@ class Binary(Char): cell.set_property('editable', not readonly) def new_binary(self, renderer, path): - filename = file_selection(_('Open...')) record, field = self._get_record_field(path) + filename = '' + if self.last_open_file: + last_id, last_filename = self.last_open_file + if last_id == record.id: + filename = last_filename + filename = file_selection(_('Open...'), filename=filename) if filename: field.set_client(record, open(filename, 'rb').read()) if self.filename: @@ -652,10 +658,11 @@ class Binary(Char): dtemp = tempfile.mkdtemp(prefix='tryton_') record, field = self._get_record_field(path) filename_field = record.group.fields.get(self.filename) - filename = filename_field.get(record).replace( - os.sep, '_').replace(os.altsep or os.sep, '_') + filename = filename_field.get(record) if not filename: return + root, ext = os.path.splitext(filename) + filename = ''.join([slugify(root), os.extsep, slugify(ext)]) file_path = os.path.join(dtemp, filename) with open(file_path, 'wb') as fp: fp.write(field.get_data(record)) @@ -663,6 +670,7 @@ class Binary(Char): if type_: type_ = type_[1:] file_open(file_path, type_) + self.last_open_file = (record.id, file_path) def save_binary(self, renderer, path): filename = ''