diff options
Diffstat (limited to 'grc/gui/Dialogs.py')
-rw-r--r-- | grc/gui/Dialogs.py | 80 |
1 files changed, 64 insertions, 16 deletions
diff --git a/grc/gui/Dialogs.py b/grc/gui/Dialogs.py index 631dc0fd98..f2941250a8 100644 --- a/grc/gui/Dialogs.py +++ b/grc/gui/Dialogs.py @@ -21,7 +21,11 @@ import pygtk pygtk.require('2.0') import gtk -from . import Utils, Actions, Constants +import sys +from distutils.spawn import find_executable + + +from . import Utils, Actions, Constants, Messages class SimpleTextDisplay(gtk.TextView): @@ -237,19 +241,63 @@ def MissingXTermDialog(xterm): def ChooseEditorDialog(): - file_dialog = gtk.FileChooserDialog( - 'Open a Data File...', None, - gtk.FILE_CHOOSER_ACTION_OPEN, - ('gtk-cancel', gtk.RESPONSE_CANCEL, 'gtk-open', gtk.RESPONSE_OK) + # Give the option to either choose an editor or use the default + # Always return true/false so the caller knows it was successful + buttons = ( + 'Choose Editor', gtk.RESPONSE_YES, + 'Use Default', gtk.RESPONSE_NO, + gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL ) - file_dialog.set_select_multiple(False) - file_dialog.set_local_only(True) - file_dialog.set_current_folder('/usr/bin') - response = file_dialog.run() - - if response == gtk.RESPONSE_OK: - file_path = file_dialog.get_filename() - Constants.prefs.set_string('grc', 'editor', file_path) - Constants.prefs.save() - Constants.EDITOR = file_path - file_dialog.destroy() + response = MessageDialogHelper( + gtk.MESSAGE_QUESTION, gtk.BUTTONS_NONE, 'Choose Editor', + 'Would you like to choose the editor to use?', gtk.RESPONSE_YES, buttons + ) + + # Handle the inital default/choose/cancel response + # User wants to choose the editor to use + if response == gtk.RESPONSE_YES: + file_dialog = gtk.FileChooserDialog( + 'Select an Editor...', None, + gtk.FILE_CHOOSER_ACTION_OPEN, + ('gtk-cancel', gtk.RESPONSE_CANCEL, 'gtk-open', gtk.RESPONSE_OK) + ) + file_dialog.set_select_multiple(False) + file_dialog.set_local_only(True) + file_dialog.set_current_folder('/usr/bin') + try: + if file_dialog.run() == gtk.RESPONSE_OK: + file_path = file_dialog.get_filename() + Constants.prefs.set_string('grc', 'editor', file_path) + Constants.prefs.save() + Constants.EDITOR = file_path + file_dialog.destroy() + return file_path + finally: + file_dialog.destroy() + + # Go with the default editor + elif response == gtk.RESPONSE_NO: + # Determine the platform + try: + process = None + if sys.platform.startswith('linux'): + process = find_executable('xdg-open') + elif sys.platform.startswith('darwin'): + process = find_executable('open') + if process is None: + raise ValueError("Can't find default editor executable") + # Save + Constants.prefs.set_string('grc', 'editor', process) + Constants.prefs.save() + Constants.EDITOR = process + return process + except Exception: + Messages.send('>>> Unable to load the default editor. Please choose an editor.\n') + # Just reset of the constant and force the user to select an editor the next time + Constants.prefs.set_string('grc', 'editor', '') + Constants.prefs.save() + Constants.EDITOR = "" + return + + Messages.send('>>> No editor selected.\n') + return |