2 * Copyright (C) 2009 Nokia Corporation.
4 * Author: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
5 * <zeeshan.ali@nokia.com>
7 * This file is part of Rygel.
9 * Rygel is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * Rygel is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 public class Rygel.MediaExportPrefSection : PreferencesSection {
27 const string ENABLED_CHECK = "-enabled-checkbutton";
28 const string TITLE_LABEL = "-title-label";
29 const string TITLE_ENTRY = "-title-entry";
30 const string NAME = "MediaExport";
31 const string URIS_KEY = "uris";
32 const string URIS_LABEL = URIS_KEY + "-label";
33 const string URIS_TEXTVIEW = URIS_KEY + "-treeview";
34 const string URIS_LISTSTORE = URIS_KEY + "-liststore";
35 const string URIS_DIALOG = URIS_KEY + "-dialog";
36 const string ADD_BUTTON = "add-button";
37 const string REMOVE_BUTTON = "remove-button";
38 const string CLEAR_BUTTON = "clear-button";
40 private CheckButton enabled_check;
41 private Entry title_entry;
43 private ArrayList<Widget> widgets; // All widgets in this section
45 private TreeView treeview;
46 private ListStore liststore;
47 private FileChooserDialog dialog;
49 public MediaExportPrefSection (Builder builder,
50 WritableUserConfig config) {
53 this.widgets = new ArrayList<Widget> ();
55 this.enabled_check = (CheckButton) builder.get_object (name.down () +
57 assert (this.enabled_check != null);
58 this.title_entry = (Entry) builder.get_object (name.down () +
60 assert (this.title_entry != null);
61 var title_label = (Label) builder.get_object (name.down () +
63 assert (title_label != null);
64 this.widgets.add (title_label);
67 this.enabled_check.active = config.get_enabled (name);
68 } catch (GLib.Error err) {
69 this.enabled_check.active = false;
74 title = config.get_title (name);
75 } catch (GLib.Error err) {
79 title = title.replace ("@REALNAME@", "%n");
80 title = title.replace ("@USERNAME@", "%u");
81 title = title.replace ("@HOSTNAME@", "%h");
82 this.title_entry.set_text (title);
84 this.enabled_check.toggled.connect (this.on_enabled_check_toggled);
86 this.treeview = (TreeView) builder.get_object (URIS_TEXTVIEW);
87 assert (this.treeview != null);
88 this.liststore = (ListStore) builder.get_object (URIS_LISTSTORE);
89 assert (this.liststore != null);
90 this.dialog = (FileChooserDialog) builder.get_object (URIS_DIALOG);
91 assert (this.dialog != null);
93 treeview.insert_column_with_attributes (-1,
95 new CellRendererText (),
99 this.widgets.add (this.treeview);
102 var uris = config.get_string_list (this.name, URIS_KEY);
103 foreach (var uri in uris) {
106 this.liststore.append (out iter);
107 this.liststore.set (iter, 0, uri, -1);
109 } catch (GLib.Error err) {} // Nevermind
111 this.dialog.set_current_folder (Environment.get_home_dir ());
112 this.dialog.show_hidden = false;
114 var button = (Button) builder.get_object (ADD_BUTTON);
115 button.clicked.connect (this.on_add_button_clicked);
116 this.widgets.add (button);
118 button = (Button) builder.get_object (REMOVE_BUTTON);
119 button.clicked.connect (this.on_remove_button_clicked);
120 this.widgets.add (button);
122 button = (Button) builder.get_object (CLEAR_BUTTON);
123 button.clicked.connect (this.on_clear_button_clicked);
124 this.widgets.add (button);
126 var label = (Label) builder.get_object (URIS_LABEL);
127 assert (label != null);
128 this.widgets.add (label);
130 // Initialize the sensitivity of all widgets
131 this.reset_widgets_sensitivity ();
134 public override void save () {
135 this.config.set_bool (this.name,
136 UserConfig.ENABLED_KEY,
137 this.enabled_check.active);
139 var title = this.title_entry.get_text ().replace ("%n", "@REALNAME@");
140 title = title.replace ("%u", "@USERNAME@");
141 title = title.replace ("%h", "@HOSTNAME@");
142 this.config.set_string (this.name, UserConfig.TITLE_KEY, title);
145 var uri_list = new ArrayList<string> ();
147 if (this.liststore.get_iter_first (out iter)) {
151 this.liststore.get (iter, 0, out uri, -1);
153 } while (this.liststore.iter_next (ref iter));
156 this.config.set_string_list (this.name, URIS_KEY, uri_list);
159 private void reset_widgets_sensitivity () {
160 this.title_entry.sensitive = this.enabled_check.active;
162 foreach (var widget in this.widgets) {
163 widget.sensitive = enabled_check.active;
167 private void on_enabled_check_toggled (ToggleButton enabled_check) {
168 this.reset_widgets_sensitivity ();
171 private void on_add_button_clicked (Button button) {
172 if (this.dialog.run () == ResponseType.OK) {
175 var dirs = this.dialog.get_files ();
177 foreach (var dir in dirs) {
178 string path = dir.get_path ();
181 path = dir.get_uri ();
184 this.liststore.append (out iter);
185 this.liststore.set (iter, 0, path, -1);
192 private void on_remove_button_clicked (Button button) {
193 var selection = this.treeview.get_selection ();
194 var rows = selection.get_selected_rows (null);
196 // First get permanent references to rows
197 var row_refs = new ArrayList<TreeRowReference> ();
198 foreach (var row in rows) {
199 row_refs.add (new TreeRowReference (this.liststore, row));
202 // Now we can safely remove rows
203 foreach (var row_ref in row_refs) {
206 var path = row_ref.get_path ();
207 this.liststore.get_iter (out iter, path);
209 this.liststore.remove (iter);
213 private void on_clear_button_clicked (Button button) {
214 this.liststore.clear ();