From 0ec058c208f4f4cf05855164648177ec666894ea Mon Sep 17 00:00:00 2001 From: "Zeeshan Ali (Khattak)" Date: Mon, 27 Apr 2009 23:45:55 +0300 Subject: [PATCH] Port the code to use dynamic UI Port the code to use Glade3-created dynamic UI. --- src/ui/rygel-general-pref-page.vala | 40 ++++++++++----- src/ui/rygel-plugin-pref-page.vala | 54 +++++++++++++-------- src/ui/rygel-preferences-dialog.vala | 60 ++++++++++++----------- src/ui/rygel-preferences-page.vala | 94 +----------------------------------- 4 files changed, 95 insertions(+), 153 deletions(-) diff --git a/src/ui/rygel-general-pref-page.vala b/src/ui/rygel-general-pref-page.vala index 744fdb9..9ee10e0 100644 --- a/src/ui/rygel-general-pref-page.vala +++ b/src/ui/rygel-general-pref-page.vala @@ -23,18 +23,34 @@ using Gtk; public class Rygel.GeneralPrefPage : PreferencesPage { - public GeneralPrefPage (Configuration config) { - base (config, "General", "general"); + const string IP_ENTRY = "ip-entry"; + const string PORT_SPINBUTTON = "port-spinbutton"; - this.add_string_pref (Configuration.IP_KEY, - "IP", - this.config.host_ip, - "The IP to advertise the UPnP MediaServer on"); - this.add_int_pref (Configuration.PORT_KEY, - "Port", - this.config.port, - uint16.MIN, - uint16.MAX, - "The port to advertise the UPnP MediaServer on"); + private Entry ip_entry; + private SpinButton port_spin; + + public GeneralPrefPage (Builder builder, + Configuration config) throws Error { + base (config, "general"); + + this.ip_entry = (Entry) builder.get_object (IP_ENTRY); + assert (this.ip_entry != null); + this.port_spin = (SpinButton) builder.get_object (PORT_SPINBUTTON); + assert (this.port_spin != null); + + if (config.host_ip != null) { + this.ip_entry.set_text (config.host_ip); + } + this.port_spin.set_value (config.port); + } + + public override void save () { + this.config.set_string (this.section, + Configuration.IP_KEY, + this.ip_entry.get_text ()); + + this.config.set_int (this.section, + Configuration.PORT_KEY, + (int) this.port_spin.get_value ()); } } diff --git a/src/ui/rygel-plugin-pref-page.vala b/src/ui/rygel-plugin-pref-page.vala index 8daa760..e54f423 100644 --- a/src/ui/rygel-plugin-pref-page.vala +++ b/src/ui/rygel-plugin-pref-page.vala @@ -23,29 +23,43 @@ using Gtk; public class Rygel.PluginPrefPage : PreferencesPage { - public PluginPrefPage (Configuration config, - string section) { - base (config, section, section); + const string ENABLED_CHECK = "-enabled-checkbutton"; + const string TITLE_ENTRY = "-title-entry"; + const string UDN_ENTRY = "-udn-entry"; + + private CheckButton enabled_check; + private Entry title_entry; + private Entry udn_entry; - var enabled = config.get_enabled (section); - var title = config.get_title (section); - var udn = config.get_udn (section); + public PluginPrefPage (Builder builder, + Configuration config, + string section) { + base (config, section); - this.add_boolean_pref (Configuration.ENABLED_KEY, - "Enabled", - enabled, - "Enable/Disable this plugin"); + this.enabled_check = (CheckButton) builder.get_object (section.down () + + ENABLED_CHECK); + assert (this.enabled_check != null); + this.title_entry = (Entry) builder.get_object (section.down () + + TITLE_ENTRY); + assert (this.title_entry != null); + this.udn_entry = (Entry) builder.get_object (section.down () + + UDN_ENTRY); + assert (this.udn_entry != null); - this.add_string_pref (Configuration.TITLE_KEY, - "Title", - title, - "This is the name that will appear on the " + - "client UIs to"); + this.enabled_check.active = config.get_enabled (section); + this.title_entry.set_text (config.get_title (section)); + this.udn_entry.set_text (config.get_udn (section)); + } - this.add_string_pref (Configuration.UDN_KEY, - "UDN", - udn, - "The Unique Device Name (UDN) for this plugin." + - " Usually, there is no need to change this."); + public override void save () { + this.config.set_bool (this.section, + Configuration.ENABLED_KEY, + this.enabled_check.active); + this.config.set_string (this.section, + Configuration.TITLE_KEY, + this.title_entry.get_text ()); + this.config.set_string (this.section, + Configuration.UDN_KEY, + this.udn_entry.get_text ()); } } diff --git a/src/ui/rygel-preferences-dialog.vala b/src/ui/rygel-preferences-dialog.vala index 2e414cc..1f2ba86 100644 --- a/src/ui/rygel-preferences-dialog.vala +++ b/src/ui/rygel-preferences-dialog.vala @@ -21,42 +21,44 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ using Gtk; +using Gee; +using CStuff; -public class Rygel.PreferencesDialog : Dialog { - Notebook notebook; +public class Rygel.PreferencesDialog : GLib.Object { + const string UI_FILE = BuildConfig.DATA_DIR + "/rygel-preferences.ui"; + const string DIALOG = "preferences-dialog"; - public PreferencesDialog () { - this.title = "Rygel Preferences"; + Builder builder; + Dialog dialog; + ArrayList pages; + public PreferencesDialog () throws Error { var config = new Configuration (); - this.notebook = new Notebook (); - this.add_pref_page (new GeneralPrefPage (config)); - this.add_pref_page (new PluginPrefPage (config, "Tracker")); - this.add_pref_page (new PluginPrefPage (config, "DVB")); - this.add_pref_page (new PluginPrefPage (config, "Test")); + this.builder = new Builder (); - this.vbox.add (this.notebook); + this.builder.add_from_file (UI_FILE); - this.add_button (STOCK_APPLY, ResponseType.APPLY); - this.add_button (STOCK_CANCEL, ResponseType.REJECT); - this.add_button (STOCK_OK, ResponseType.ACCEPT); + this.dialog = (Dialog) this.builder.get_object (DIALOG); + assert (this.dialog != null); - this.response += this.on_response; - this.delete_event += (dialog, event) => { + this.pages = new ArrayList (); + this.pages.add (new GeneralPrefPage (this.builder, config)); + this.pages.add (new PluginPrefPage (this.builder, config, "Tracker")); + this.pages.add (new PluginPrefPage (this.builder, config, "DVB")); + this.pages.add (new PluginPrefPage (this.builder, config, "Test")); + + this.dialog.response += this.on_response; + this.dialog.delete_event += (dialog, event) => { Gtk.main_quit (); return false; }; - this.show_all (); - } + this.dialog.show_all (); - private void add_pref_page (PreferencesPage page) { - var label = new Label (page.title); - this.notebook.append_page (page, label); } - private void on_response (PreferencesDialog dialog, int response_id) { + private void on_response (Dialog dialog, int response_id) { switch (response_id) { case ResponseType.REJECT: Gtk.main_quit (); @@ -72,12 +74,8 @@ public class Rygel.PreferencesDialog : Dialog { } private void apply_settings () { - foreach (var child in this.notebook.get_children ()) { - if (!(child is PreferencesPage)) { - break; - } - - ((PreferencesPage) child).save (); + foreach (var page in this.pages) { + page.save (); } } @@ -88,9 +86,13 @@ public class Rygel.PreferencesDialog : Dialog { public static int main (string[] args) { Gtk.init (ref args); - var dialog = new PreferencesDialog (); + try { + var dialog = new PreferencesDialog (); - dialog.run (); + dialog.run (); + } catch (Error err) { + error ("Failed to create preferences dialog: %s\n", err.message); + } return 0; } diff --git a/src/ui/rygel-preferences-page.vala b/src/ui/rygel-preferences-page.vala index fea55c5..d4c08fe 100644 --- a/src/ui/rygel-preferences-page.vala +++ b/src/ui/rygel-preferences-page.vala @@ -22,106 +22,16 @@ */ using Gtk; -public class Rygel.PreferencesPage : VBox { +public abstract class Rygel.PreferencesPage : GLib.Object { protected Configuration config; - public string title; public string section; public PreferencesPage (Configuration config, - string title, string section) { this.section = section; - this.title = title; - this.config = config; } - protected void add_string_pref (string name, - string title, - string? current_value, - string tooltip) { - var entry = new Entry (); - - if (current_value != null) { - entry.set_text (current_value); - } - - this.add_pref_widget (name, title, entry, tooltip); - } - - protected void add_int_pref (string name, - string title, - int current_value, - int min, - int max, - string tooltip) { - var adjustment = new Adjustment (current_value, - min, - max, - 1.0, - 10.0, - 10.0); - - var spin = new SpinButton (adjustment, 1.0, 0); - - this.add_pref_widget (name, title, spin, tooltip); - } - - protected void add_boolean_pref (string name, - string title, - bool current_value, - string tooltip) { - var check = new CheckButton (); - - check.active = current_value; - - this.add_pref_widget (name, title, check, tooltip); - } - - private void add_pref_widget (string name, - string title, - Widget widget, - string tooltip) { - var hbox = new HBox (true, 6); - - var label = new Label (title); - - hbox.add (label); - hbox.add (widget); - - hbox.set_tooltip_text (tooltip); - widget.set_name (name); - - this.add (hbox); - } - - public void save () { - foreach (var child in this.get_children ()) { - if (!(child is HBox)) { - break; - } - - var hbox = (HBox) child; - - foreach (var widget in hbox.get_children ()) { - if (widget is SpinButton) { - var name = widget.get_name (); - var number = ((SpinButton) widget).get_value (); - - this.config.set_int (this.section, name, (int) number); - } else if (widget is Entry) { - var name = widget.get_name (); - var text = ((Entry) widget).get_text (); - - this.config.set_string (this.section, name, text); - } else if (widget is CheckButton) { - var name = widget.get_name (); - var active = ((CheckButton) widget).get_active (); - - this.config.set_bool (this.section, name, active); - } - } - } - } + public abstract void save (); } -- 2.7.4