From 5fbac41742daad29cae1063e70dd91941924bd03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Carlos=20Cuevas=20Albadalejo?= Date: Sat, 12 Jul 2014 14:50:22 +0200 Subject: [PATCH] Preferences integrated into the application. Now we have to actually use them in our application logic. --- res/stevedore.glade | 9 +++-- src/stevedore.py | 90 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 92 insertions(+), 7 deletions(-) diff --git a/res/stevedore.glade b/res/stevedore.glade index ea2e9a6..15653d0 100644 --- a/res/stevedore.glade +++ b/res/stevedore.glade @@ -182,14 +182,14 @@ True - False True 5 5 5 5 - 0.029999999329447746 + False Docker Server Hostname + 1 @@ -217,13 +217,15 @@ True - False True 5 5 5 5 + False Docker Server Port + number + 1 @@ -240,6 +242,7 @@ 0 True True + 1 diff --git a/src/stevedore.py b/src/stevedore.py index 3b7c8ae..fa66a3f 100644 --- a/src/stevedore.py +++ b/src/stevedore.py @@ -20,7 +20,9 @@ from gi.repository import Gtk, Gdk, Gio from pkg_resources import resource_string + from docker_iface import DockerInterface, DockerImage, DockerContainer, DockerNotConnectedException +from prefs import Preferences import time @@ -58,6 +60,9 @@ class MainWindow(Gtk.Application): self.builder.get_object('LogButton').set_sensitive(False) self.builder.get_object('DeleteContainerButton').set_sensitive(False) + # Get the preferences ready + self.preferences = Preferences() + # Add window to the App and show it self.window = self.builder.get_object('MainWindow') self.add_window(self.window) @@ -155,7 +160,7 @@ class MainWindow(Gtk.Application): """ The main window is deleted """ - pass + self.preferences.save_preferences() def on_PreferencesWindow_delete_event(self, obj, event = None): """ @@ -168,7 +173,6 @@ class MainWindow(Gtk.Application): preferences_window = self.builder.get_object('PreferencesWindow') preferences_window.hide() - # TODO: Save the preferences return True def on_PrefsCloseButton_clicked(self, obj, event = None): @@ -180,7 +184,6 @@ class MainWindow(Gtk.Application): """ preferences_window = self.builder.get_object('PreferencesWindow') preferences_window.hide() - # TODO: Save the preferences ### Action callbacks ### @@ -215,8 +218,32 @@ class MainWindow(Gtk.Application): We've been asked for the preferences window """ preferences_window = self.builder.get_object('PreferencesWindow') + localhost_checkbox = self.builder.get_object('LocalCheckBox') + hostname_entry = self.builder.get_object('HostnameEntry') + port_entry = self.builder.get_object('PortEntry') - # TODO: Set up the window to the current preferences before showing it up + # Set up the window to the current preferences before showing it up + if self.preferences['localhost']: + # We're connecting to a local instance + localhost_checkbox.set_active(True) + + hostname_entry.set_text('') + hostname_entry.set_editable(False) + + port_entry.set_text('') + port_entry.set_editable(False) + + else: + # We're connecting to a remote instance + localhost_checkbox.set_active(False) + + hostname_entry.set_text(self.preferences['host']) + hostname_entry.set_editable(True) + + port_entry.set_text(unicode(self.preferences['port'])) + port_entry.set_editable(True) + + # Now we show up our preferences window preferences_window.show() def on_refresh_action_activate(self, obj, event = None): @@ -371,6 +398,61 @@ class MainWindow(Gtk.Application): log_button.set_sensitive(False) delete_container_button.set_sensitive(False) + # Preferences window callbacks + def on_LocalCheckBox_toggled(self, obj, data = None): + """ + The checkbox has been changed + """ + localhost_checkbox = self.builder.get_object('LocalCheckBox') + hostname_entry = self.builder.get_object('HostnameEntry') + port_entry = self.builder.get_object('PortEntry') + + if localhost_checkbox.get_active(): + hostname_entry.set_text('') + hostname_entry.set_editable(False) + + port_entry.set_text('') + port_entry.set_editable(False) + + else: + hostname_entry.set_text(self.preferences['host']) + hostname_entry.set_editable(True) + + port_entry.set_text(unicode(self.preferences['port'])) + port_entry.set_editable(True) + + self.get_preferences_from_window() + + def on_PortEntry_changed(self, obj, data = None): + """ + The port entry has been changed + """ + self.get_preferences_from_window() + + def on_HostnameEntry_changed(self, obj, data = None): + """ + The hostname entry has been changed + """ + self.get_preferences_from_window() + + def get_preferences_from_window(self): + """ + Gets the current Preferences window widgets and saves the options + """ + localhost_checkbox = self.builder.get_object('LocalCheckBox') + hostname_entry = self.builder.get_object('HostnameEntry') + port_entry = self.builder.get_object('PortEntry') + + if localhost_checkbox.get_active(): + self.preferences['localhost'] = True + self.preferences['host'] = 'localhost' + self.preferences['port'] = 0 + + else: + self.preferences['localhost'] = False + self.preferences['host'] = hostname_entry.get_text() + if port_entry.get_text() != '': + self.preferences['port'] = int(port_entry.get_text()) # run main loop