data,ui: Remove MediaExport's title widgets
[profile/ivi/rygel.git] / src / ui / rygel-media-export-pref-section.vala
1 /*
2  * Copyright (C) 2009 Nokia Corporation.
3  *
4  * Author: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
5  *                               <zeeshan.ali@nokia.com>
6  *
7  * This file is part of Rygel.
8  *
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.
13  *
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.
18  *
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.
22  */
23 using Gtk;
24 using Gee;
25
26 public class Rygel.MediaExportPrefSection : PreferencesSection {
27     const string ENABLED_CHECK = "-enabled-checkbutton";
28     const string NAME = "MediaExport";
29     const string URIS_KEY = "uris";
30     const string URIS_LABEL = URIS_KEY + "-label";
31     const string URIS_TEXTVIEW = URIS_KEY + "-treeview";
32     const string URIS_LISTSTORE = URIS_KEY + "-liststore";
33     const string URIS_DIALOG = URIS_KEY + "-dialog";
34     const string ADD_BUTTON = "add-button";
35     const string REMOVE_BUTTON = "remove-button";
36     const string CLEAR_BUTTON = "clear-button";
37
38     private CheckButton enabled_check;
39     private ArrayList<Widget> widgets; // All widgets in this section
40
41     private TreeView treeview;
42     private ListStore liststore;
43     private FileChooserDialog dialog;
44
45     public MediaExportPrefSection (Builder            builder,
46                                    WritableUserConfig config) {
47         base (config, NAME);
48
49         this.widgets = new ArrayList<Widget> ();
50
51         this.enabled_check = (CheckButton) builder.get_object (name.down () +
52                                                                ENABLED_CHECK);
53         assert (this.enabled_check != null);
54
55         try {
56             this.enabled_check.active = config.get_enabled (name);
57         } catch (GLib.Error err) {
58             this.enabled_check.active = false;
59         }
60
61         this.enabled_check.toggled.connect (this.on_enabled_check_toggled);
62
63         this.treeview = (TreeView) builder.get_object (URIS_TEXTVIEW);
64         assert (this.treeview != null);
65         this.liststore = (ListStore) builder.get_object (URIS_LISTSTORE);
66         assert (this.liststore != null);
67         this.dialog = (FileChooserDialog) builder.get_object (URIS_DIALOG);
68         assert (this.dialog != null);
69
70         treeview.insert_column_with_attributes (-1,
71                                                 "paths",
72                                                 new CellRendererText (),
73                                                 "text",
74                                                 0,
75                                                 null);
76         this.widgets.add (this.treeview);
77
78         try {
79             var uris = config.get_string_list (this.name, URIS_KEY);
80             foreach (var uri in uris) {
81                 TreeIter iter;
82
83                 this.liststore.append (out iter);
84                 this.liststore.set (iter, 0, uri, -1);
85             }
86         } catch (GLib.Error err) {} // Nevermind
87
88         this.dialog.set_current_folder (Environment.get_home_dir ());
89         this.dialog.show_hidden = false;
90
91         var button = (Button) builder.get_object (ADD_BUTTON);
92         button.clicked.connect (this.on_add_button_clicked);
93         this.widgets.add (button);
94
95         button = (Button) builder.get_object (REMOVE_BUTTON);
96         button.clicked.connect (this.on_remove_button_clicked);
97         this.widgets.add (button);
98
99         button = (Button) builder.get_object (CLEAR_BUTTON);
100         button.clicked.connect (this.on_clear_button_clicked);
101         this.widgets.add (button);
102
103         var label = (Label) builder.get_object (URIS_LABEL);
104         assert (label != null);
105         this.widgets.add (label);
106
107         // Initialize the sensitivity of all widgets
108         this.reset_widgets_sensitivity ();
109     }
110
111     public override void save () {
112         this.config.set_bool (this.name,
113                               UserConfig.ENABLED_KEY,
114                               this.enabled_check.active);
115
116         TreeIter iter;
117         var uri_list = new ArrayList<string> ();
118
119         if (this.liststore.get_iter_first (out iter)) {
120             do {
121                 string uri;
122
123                 this.liststore.get (iter, 0, out uri, -1);
124                 uri_list.add (uri);
125             } while (this.liststore.iter_next (ref iter));
126         }
127
128         this.config.set_string_list (this.name, URIS_KEY, uri_list);
129     }
130
131     private void reset_widgets_sensitivity () {
132         this.title_entry.sensitive = this.enabled_check.active;
133
134         foreach (var widget in this.widgets) {
135             widget.sensitive = enabled_check.active;
136         }
137     }
138
139     private void on_enabled_check_toggled (ToggleButton enabled_check) {
140         this.reset_widgets_sensitivity ();
141     }
142
143     private void on_add_button_clicked (Button button) {
144         if (this.dialog.run () == ResponseType.OK) {
145             TreeIter iter;
146
147             var dirs = this.dialog.get_files ();
148
149             foreach (var dir in dirs) {
150                 string path = dir.get_path ();
151
152                 if (path == null) {
153                     path = dir.get_uri ();
154                 }
155
156                 this.liststore.append (out iter);
157                 this.liststore.set (iter, 0, path, -1);
158             }
159         }
160
161         this.dialog.hide ();
162     }
163
164     private void on_remove_button_clicked (Button button) {
165         var selection = this.treeview.get_selection ();
166         var rows = selection.get_selected_rows (null);
167
168         // First get permanent references to rows
169         var row_refs = new ArrayList<TreeRowReference> ();
170         foreach (var row in rows) {
171             row_refs.add (new TreeRowReference (this.liststore, row));
172         }
173
174         // Now we can safely remove rows
175         foreach (var row_ref in row_refs) {
176            TreeIter iter;
177
178            var path = row_ref.get_path ();
179            this.liststore.get_iter (out iter, path);
180
181            this.liststore.remove (iter);
182         }
183     }
184
185     private void on_clear_button_clicked (Button button) {
186         this.liststore.clear ();
187     }
188 }