# Path: hamster_gtk/helpers.py
# def _u(string):
#     """
#     Return passed string as a text instance.
# 
#     This helper is particularly useful to wrap Gtk 'string return values' as
#     they depend on the used python version. On python 2 Gtk will return utf-8
#     encoded bytestrings while on python 3 it will return 'unicode strings'.
# 
#     The reason we have to use this and not ``six.u`` is that six does not make
#     assumptions about the encoding and hence can not be used to safely decode
#     non ascii bytestrings into unicode.
# 
#     Args:
#         string (str): A string instance. On ``py2`` this will be a bytestring,
#             on ``py3`` a 'unicode string'.
# 
#     Returns:
#         text_type: Returns a 'unicode text type'. Under ``py2`` that means a
#         ``unicode`` instance, under ``py3`` this will be a ``str`` instance.
#     """
#     if six.PY2:
#         return string.decode('utf-8')
#     else:
#         return string
# 
# def get_parent_window(widget):
#     """
#     Reliably determine parent window of a widget.
# 
#     Just using :meth:`Gtk.Widget.get_toplevel` would return the widget itself
#     if it had no parent window.
# 
#     On the other hand using :meth:`Gtk.Widget.get_ancestor` would return only
#     the closest :class:`Gtk.Window` in the hierarchy.
# 
#     https://developer.gnome.org/gtk3/unstable/GtkWidget.html#gtk-widget-get-toplevel
#     """
#     toplevel = widget.get_toplevel()
#     if not toplevel.is_toplevel():
#         toplevel = None
# 
#     return toplevel
# 
# Path: hamster_gtk/preferences/widgets/config_widget.py
# class ConfigWidget(object):
#     """
#     Abstract class defining unified setter/getter methods for accessing widget values.
# 
#     Depending on a particular widget, the way a value is set/retrieved varies while the semantics
#     are rather constant. This class provides a consistent interface to be implemented by our custom
#     widgets, which should help in making the access to their values cleaner and more concise.
#     """
# 
#     def get_config_value(self):
#         """Get widget value."""
#         raise NotImplementedError
# 
#     def set_config_value(self, value):
#         """Set widget value."""
#         raise NotImplementedError


from gettext import gettext as _
from gi.repository import Gtk
from six import text_type
from hamster_gtk.helpers import _u, get_parent_window
from .config_widget import ConfigWidget
        self.connect('mnemonic-activate', self._on_mnemonic_activate)

    def get_config_value(self):
        """
        Return the selected path.

        Returns:
            six.text_type: Selected file path.
        """
        return _u(self._entry.get_text())

    def set_config_value(self, path):
        """
        Select given file path.

        Args:
            path (six.text_type): Path to be selected
        """
        self._entry.set_text(text_type(path))

    def _on_choose_clicked(self, widget):
        """Open a dialog to select path and update entry widget with it."""
        toplevel = get_parent_window(self)

        dialog = Gtk.FileChooserDialog(_("Please choose a directory"), toplevel,
            Gtk.FileChooserAction.SAVE, (_("_Cancel"), Gtk.ResponseType.CANCEL,
                                         _("_Save"), Gtk.ResponseType.OK))
        dialog.set_filename(self.get_config_value())
        response = dialog.run()
        if response == Gtk.ResponseType.OK:

# Path: hamster_gtk/helpers.py
# def _u(string):
#     """
#     Return passed string as a text instance.
# 
#     This helper is particularly useful to wrap Gtk 'string return values' as
#     they depend on the used python version. On python 2 Gtk will return utf-8
#     encoded bytestrings while on python 3 it will return 'unicode strings'.
# 
#     The reason we have to use this and not ``six.u`` is that six does not make
#     assumptions about the encoding and hence can not be used to safely decode
#     non ascii bytestrings into unicode.
# 
#     Args:
#         string (str): A string instance. On ``py2`` this will be a bytestring,
#             on ``py3`` a 'unicode string'.
# 
#     Returns:
#         text_type: Returns a 'unicode text type'. Under ``py2`` that means a
#         ``unicode`` instance, under ``py3`` this will be a ``str`` instance.
#     """
#     if six.PY2:
#         return string.decode('utf-8')
#     else:
#         return string
# 
# def get_parent_window(widget):
#     """
#     Reliably determine parent window of a widget.
# 
#     Just using :meth:`Gtk.Widget.get_toplevel` would return the widget itself
#     if it had no parent window.
# 
#     On the other hand using :meth:`Gtk.Widget.get_ancestor` would return only
#     the closest :class:`Gtk.Window` in the hierarchy.
# 
#     https://developer.gnome.org/gtk3/unstable/GtkWidget.html#gtk-widget-get-toplevel
#     """
#     toplevel = widget.get_toplevel()
#     if not toplevel.is_toplevel():
#         toplevel = None
# 
#     return toplevel
# 
# Path: hamster_gtk/preferences/widgets/config_widget.py
# class ConfigWidget(object):
#     """
#     Abstract class defining unified setter/getter methods for accessing widget values.
# 
#     Depending on a particular widget, the way a value is set/retrieved varies while the semantics
#     are rather constant. This class provides a consistent interface to be implemented by our custom
#     widgets, which should help in making the access to their values cleaner and more concise.
#     """
# 
#     def get_config_value(self):
#         """Get widget value."""
#         raise NotImplementedError
# 
#     def set_config_value(self, value):
#         """Set widget value."""
#         raise NotImplementedError


# Path: hamster_gtk/preferences/widgets/combo_file_chooser.py
from gettext import gettext as _
from gi.repository import Gtk
from six import text_type
from hamster_gtk.helpers import _u, get_parent_window
from .config_widget import ConfigWidget

        self.connect('mnemonic-activate', self._on_mnemonic_activate)

    def get_config_value(self):
        """
        Return the selected path.

        Returns:
            six.text_type: Selected file path.
        """
        return _u(self._entry.get_text())

    def set_config_value(self, path):
        """
        Select given file path.

        Args:
            path (six.text_type): Path to be selected
        """
        self._entry.set_text(text_type(path))

    def _on_choose_clicked(self, widget):
        """Open a dialog to select path and update entry widget with it."""
        toplevel = get_parent_window(self)

        dialog = Gtk.FileChooserDialog(_("Please choose a directory"), toplevel,
            Gtk.FileChooserAction.SAVE, (_("_Cancel"), Gtk.ResponseType.CANCEL,
                                         _("_Save"), Gtk.ResponseType.OK))
        dialog.set_filename(self.get_config_value())
        response = dialog.run()
        if response == Gtk.ResponseType.OK:

            self._entry.set_text(_u(dialog.get_filename()))