1 /*
2  *  Copyright (C) 2009 Marc Pavot <marc.pavot@gmail.com>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2, or (at your option)
7  *  any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  */
19 
20 #include "preferences/ario-playlist-preferences.h"
21 #include <config.h>
22 #include <gtk/gtk.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <time.h>
26 #include <glib/gi18n.h>
27 #include "preferences/ario-preferences.h"
28 #include "lib/gtk-builder-helpers.h"
29 #include "lib/ario-conf.h"
30 #include "ario-debug.h"
31 #include "playlist/ario-playlist-manager.h"
32 
33 static void ario_playlist_preferences_sync_playlist (ArioPlaylistPreferences *playlist_preferences);
34 G_MODULE_EXPORT void ario_playlist_preferences_track_toogled_cb (GtkCheckButton *butt,
35                                                                  ArioPlaylistPreferences *playlist_preferences);
36 G_MODULE_EXPORT void ario_playlist_preferences_title_toogled_cb (GtkCheckButton *butt,
37                                                                  ArioPlaylistPreferences *playlist_preferences);
38 G_MODULE_EXPORT void ario_playlist_preferences_artist_toogled_cb (GtkCheckButton *butt,
39                                                                   ArioPlaylistPreferences *playlist_preferences);
40 G_MODULE_EXPORT void ario_playlist_preferences_album_toogled_cb (GtkCheckButton *butt,
41                                                                  ArioPlaylistPreferences *playlist_preferences);
42 G_MODULE_EXPORT void ario_playlist_preferences_genre_toogled_cb (GtkCheckButton *butt,
43                                                                  ArioPlaylistPreferences *playlist_preferences);
44 G_MODULE_EXPORT void ario_playlist_preferences_duration_toogled_cb (GtkCheckButton *butt,
45                                                                     ArioPlaylistPreferences *playlist_preferences);
46 G_MODULE_EXPORT void ario_playlist_preferences_file_toogled_cb (GtkCheckButton *butt,
47                                                                 ArioPlaylistPreferences *playlist_preferences);
48 G_MODULE_EXPORT void ario_playlist_preferences_date_toogled_cb (GtkCheckButton *butt,
49                                                                 ArioPlaylistPreferences *playlist_preferences);
50 G_MODULE_EXPORT void ario_playlist_preferences_disc_toogled_cb (GtkCheckButton *butt,
51                                                                 ArioPlaylistPreferences *playlist_preferences);
52 G_MODULE_EXPORT void ario_playlist_preferences_autoscroll_toogled_cb (GtkCheckButton *butt,
53                                                                       ArioPlaylistPreferences *playlist_preferences);
54 G_MODULE_EXPORT void ario_playlist_preferences_playlist_mode_changed_cb (GtkComboBox *combobox,
55                                                                          ArioPlaylistPreferences *playlist_preferences);
56 G_MODULE_EXPORT void ario_playlist_preferences_doubleclick_changed_cb (GtkComboBox *combobox,
57                                                                        ArioPlaylistPreferences *playlist_preferences);
58 
59 struct ArioPlaylistPreferencesPrivate
60 {
61         GtkWidget *track_checkbutton;
62         GtkWidget *title_checkbutton;
63         GtkWidget *artist_checkbutton;
64         GtkWidget *album_checkbutton;
65         GtkWidget *genre_checkbutton;
66         GtkWidget *duration_checkbutton;
67         GtkWidget *file_checkbutton;
68         GtkWidget *date_checkbutton;
69         GtkWidget *disc_checkbutton;
70         GtkWidget *autoscroll_checkbutton;
71 
72         GtkWidget *playlist_combobox;
73         GtkWidget *doubleclick_combobox;
74         GtkWidget *vbox;
75         GtkWidget *config;
76 };
77 
78 #define ARIO_PLAYLIST_PREFERENCES_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TYPE_ARIO_PLAYLIST_PREFERENCES, ArioPlaylistPreferencesPrivate))
G_DEFINE_TYPE(ArioPlaylistPreferences,ario_playlist_preferences,GTK_TYPE_BOX)79 G_DEFINE_TYPE (ArioPlaylistPreferences, ario_playlist_preferences, GTK_TYPE_BOX)
80 
81 static void
82 ario_playlist_preferences_class_init (ArioPlaylistPreferencesClass *klass)
83 {
84         ARIO_LOG_FUNCTION_START;
85         g_type_class_add_private (klass, sizeof (ArioPlaylistPreferencesPrivate));
86 }
87 
88 static void
ario_playlist_preferences_init(ArioPlaylistPreferences * playlist_preferences)89 ario_playlist_preferences_init (ArioPlaylistPreferences *playlist_preferences)
90 {
91         ARIO_LOG_FUNCTION_START;
92         playlist_preferences->priv = ARIO_PLAYLIST_PREFERENCES_GET_PRIVATE (playlist_preferences);
93 }
94 
95 GtkWidget *
ario_playlist_preferences_new(void)96 ario_playlist_preferences_new (void)
97 {
98         ARIO_LOG_FUNCTION_START;
99         ArioPlaylistPreferences *playlist_preferences;
100         GtkBuilder *builder;
101         GtkListStore *list_store;
102         GtkTreeIter iter;
103         GSList *playlist_modes;
104         ArioPlaylistMode *playlist_mode;
105 
106         playlist_preferences = g_object_new (TYPE_ARIO_PLAYLIST_PREFERENCES, NULL);
107 
108         g_return_val_if_fail (playlist_preferences->priv != NULL, NULL);
109 
110         gtk_orientable_set_orientation (GTK_ORIENTABLE (playlist_preferences), GTK_ORIENTATION_VERTICAL);
111 
112         builder = gtk_builder_helpers_new (UI_PATH "playlist-prefs.ui",
113                                            playlist_preferences);
114 
115         playlist_preferences->priv->track_checkbutton =
116                 GTK_WIDGET (gtk_builder_get_object (builder, "track_checkbutton"));
117         playlist_preferences->priv->title_checkbutton =
118                 GTK_WIDGET (gtk_builder_get_object (builder, "title_checkbutton"));
119         playlist_preferences->priv->artist_checkbutton =
120                 GTK_WIDGET (gtk_builder_get_object (builder, "artist_checkbutton"));
121         playlist_preferences->priv->album_checkbutton =
122                 GTK_WIDGET (gtk_builder_get_object (builder, "album_checkbutton"));
123         playlist_preferences->priv->genre_checkbutton =
124                 GTK_WIDGET (gtk_builder_get_object (builder, "genre_checkbutton"));
125         playlist_preferences->priv->duration_checkbutton =
126                 GTK_WIDGET (gtk_builder_get_object (builder, "duration_checkbutton"));
127         playlist_preferences->priv->file_checkbutton =
128                 GTK_WIDGET (gtk_builder_get_object (builder, "file_checkbutton"));
129         playlist_preferences->priv->date_checkbutton =
130                 GTK_WIDGET (gtk_builder_get_object (builder, "date_checkbutton"));
131         playlist_preferences->priv->disc_checkbutton =
132                 GTK_WIDGET (gtk_builder_get_object (builder, "disc_checkbutton"));
133         playlist_preferences->priv->autoscroll_checkbutton =
134                 GTK_WIDGET (gtk_builder_get_object (builder, "autoscroll_checkbutton"));
135         playlist_preferences->priv->playlist_combobox =
136                 GTK_WIDGET (gtk_builder_get_object (builder, "playlist_combobox"));
137         playlist_preferences->priv->doubleclick_combobox =
138                 GTK_WIDGET (gtk_builder_get_object (builder, "doubleclick_combobox"));
139         playlist_preferences->priv->vbox =
140                 GTK_WIDGET (gtk_builder_get_object (builder, "vbox"));
141         list_store =
142                 GTK_LIST_STORE (gtk_builder_get_object (builder, "liststore"));
143 
144         gtk_builder_helpers_boldify_label (builder, "playlist_label");
145         gtk_builder_helpers_boldify_label (builder, "mode_label");
146         gtk_builder_helpers_boldify_label (builder, "doubleclick_label");
147 
148         playlist_modes = ario_playlist_manager_get_modes (ario_playlist_manager_get_instance ());
149         for (; playlist_modes; playlist_modes = g_slist_next (playlist_modes)) {
150                 playlist_mode = playlist_modes->data;
151                 gtk_list_store_append (list_store, &iter);
152                 gtk_list_store_set (list_store, &iter,
153                                     0, ario_playlist_mode_get_name (playlist_mode),
154                                     1, ario_playlist_mode_get_id (playlist_mode),
155                                     -1);
156         }
157 
158         ario_playlist_preferences_sync_playlist (playlist_preferences);
159 
160         gtk_box_pack_start (GTK_BOX (playlist_preferences), GTK_WIDGET (gtk_builder_get_object (builder, "playlist_vbox")), TRUE, TRUE, 0);
161 
162         g_object_unref (builder);
163 
164         return GTK_WIDGET (playlist_preferences);
165 }
166 
167 static void
ario_playlist_preferences_sync_playlist(ArioPlaylistPreferences * playlist_preferences)168 ario_playlist_preferences_sync_playlist (ArioPlaylistPreferences *playlist_preferences)
169 {
170         ARIO_LOG_FUNCTION_START;
171         const gchar *id;
172         int i = 0;
173         GSList *playlist_modes;
174         ArioPlaylistMode *playlist_mode;
175 
176         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (playlist_preferences->priv->track_checkbutton),
177                                       ario_conf_get_boolean (PREF_TRACK_COLUMN_VISIBLE, PREF_TRACK_COLUMN_VISIBLE_DEFAULT));
178 
179         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (playlist_preferences->priv->title_checkbutton),
180                                       ario_conf_get_boolean (PREF_TITLE_COLUMN_VISIBLE, PREF_TITLE_COLUMN_VISIBLE_DEFAULT));
181 
182         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (playlist_preferences->priv->artist_checkbutton),
183                                       ario_conf_get_boolean (PREF_ARTIST_COLUMN_VISIBLE, PREF_ARTIST_COLUMN_VISIBLE_DEFAULT));
184 
185         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (playlist_preferences->priv->album_checkbutton),
186                                       ario_conf_get_boolean (PREF_ALBUM_COLUMN_VISIBLE, PREF_ALBUM_COLUMN_VISIBLE_DEFAULT));
187 
188         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (playlist_preferences->priv->genre_checkbutton),
189                                       ario_conf_get_boolean (PREF_GENRE_COLUMN_VISIBLE, PREF_GENRE_COLUMN_VISIBLE_DEFAULT));
190 
191         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (playlist_preferences->priv->duration_checkbutton),
192                                       ario_conf_get_boolean (PREF_DURATION_COLUMN_VISIBLE, PREF_DURATION_COLUMN_VISIBLE_DEFAULT));
193 
194         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (playlist_preferences->priv->file_checkbutton),
195                                       ario_conf_get_boolean (PREF_FILE_COLUMN_VISIBLE, PREF_FILE_COLUMN_VISIBLE_DEFAULT));
196 
197         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (playlist_preferences->priv->date_checkbutton),
198                                       ario_conf_get_boolean (PREF_DATE_COLUMN_VISIBLE, PREF_DATE_COLUMN_VISIBLE_DEFAULT));
199 
200         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (playlist_preferences->priv->disc_checkbutton),
201                                       ario_conf_get_boolean (PREF_DISC_COLUMN_VISIBLE, PREF_DISC_COLUMN_VISIBLE_DEFAULT));
202 
203         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (playlist_preferences->priv->autoscroll_checkbutton),
204                                       ario_conf_get_boolean (PREF_PLAYLIST_AUTOSCROLL, PREF_PLAYLIST_AUTOSCROLL_DEFAULT));
205 
206         id = ario_conf_get_string (PREF_PLAYLIST_MODE, PREF_PLAYLIST_MODE_DEFAULT);
207         playlist_modes = ario_playlist_manager_get_modes (ario_playlist_manager_get_instance ());
208         for (; playlist_modes; playlist_modes = g_slist_next (playlist_modes)) {
209                 playlist_mode = playlist_modes->data;
210                 if (!strcmp (ario_playlist_mode_get_id (playlist_mode), id)) {
211                         gtk_combo_box_set_active (GTK_COMBO_BOX (playlist_preferences->priv->playlist_combobox), i);
212                         break;
213                 }
214                 ++i;
215         }
216 
217         gtk_combo_box_set_active (GTK_COMBO_BOX (playlist_preferences->priv->doubleclick_combobox),
218                                   ario_conf_get_integer (PREF_DOUBLECLICK_BEHAVIOR, PREF_DOUBLECLICK_BEHAVIOR_DEFAULT));
219 }
220 
221 void
ario_playlist_preferences_playlist_mode_changed_cb(GtkComboBox * combobox,ArioPlaylistPreferences * playlist_preferences)222 ario_playlist_preferences_playlist_mode_changed_cb (GtkComboBox *combobox,
223                                                     ArioPlaylistPreferences *playlist_preferences)
224 {
225         ARIO_LOG_FUNCTION_START;
226         GtkTreeIter iter;
227         gchar *id;
228         ArioPlaylistMode *playlist_mode;
229 
230         gtk_combo_box_get_active_iter (combobox, &iter);
231 
232         gtk_tree_model_get (gtk_combo_box_get_model (combobox), &iter,
233                             1, &id, -1);
234 
235         ario_conf_set_string (PREF_PLAYLIST_MODE, id);
236 
237         if (playlist_preferences->priv->config) {
238                 gtk_container_remove (GTK_CONTAINER (playlist_preferences->priv->vbox),
239                                       playlist_preferences->priv->config);
240                 playlist_preferences->priv->config = NULL;
241         }
242         playlist_mode = ario_playlist_manager_get_mode_from_id (ario_playlist_manager_get_instance (),
243                                                                 id);
244         if (playlist_mode) {
245                 playlist_preferences->priv->config = ario_playlist_mode_get_config (playlist_mode);
246                 if (playlist_preferences->priv->config) {
247                         gtk_box_pack_end (GTK_BOX (playlist_preferences->priv->vbox),
248                                           playlist_preferences->priv->config,
249                                           TRUE, TRUE, 0);
250                         gtk_widget_show_all (playlist_preferences->priv->config);
251                 }
252         }
253         g_free (id);
254 }
255 
ario_playlist_preferences_doubleclick_changed_cb(GtkComboBox * combobox,ArioPlaylistPreferences * playlist_preferences)256 void ario_playlist_preferences_doubleclick_changed_cb (GtkComboBox *combobox,
257                                                        ArioPlaylistPreferences *playlist_preferences)
258 {
259         ARIO_LOG_FUNCTION_START;
260         int i;
261 
262         i = gtk_combo_box_get_active (GTK_COMBO_BOX (combobox));
263 
264         ario_conf_set_integer (PREF_DOUBLECLICK_BEHAVIOR, i);
265 }
266 
267 void
ario_playlist_preferences_track_toogled_cb(GtkCheckButton * butt,ArioPlaylistPreferences * playlist_preferences)268 ario_playlist_preferences_track_toogled_cb (GtkCheckButton *butt,
269                                             ArioPlaylistPreferences *playlist_preferences)
270 {
271         ARIO_LOG_FUNCTION_START;
272         ario_conf_set_boolean (PREF_TRACK_COLUMN_VISIBLE,
273                                gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (butt)));
274 }
275 
276 void
ario_playlist_preferences_title_toogled_cb(GtkCheckButton * butt,ArioPlaylistPreferences * playlist_preferences)277 ario_playlist_preferences_title_toogled_cb (GtkCheckButton *butt,
278                                             ArioPlaylistPreferences *playlist_preferences)
279 {
280         ARIO_LOG_FUNCTION_START;
281         ario_conf_set_boolean (PREF_TITLE_COLUMN_VISIBLE,
282                                gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (butt)));
283 }
284 
285 void
ario_playlist_preferences_artist_toogled_cb(GtkCheckButton * butt,ArioPlaylistPreferences * playlist_preferences)286 ario_playlist_preferences_artist_toogled_cb (GtkCheckButton *butt,
287                                              ArioPlaylistPreferences *playlist_preferences)
288 {
289         ARIO_LOG_FUNCTION_START;
290         ario_conf_set_boolean (PREF_ARTIST_COLUMN_VISIBLE,
291                                gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (butt)));
292 }
293 
294 void
ario_playlist_preferences_album_toogled_cb(GtkCheckButton * butt,ArioPlaylistPreferences * playlist_preferences)295 ario_playlist_preferences_album_toogled_cb (GtkCheckButton *butt,
296                                             ArioPlaylistPreferences *playlist_preferences)
297 {
298         ARIO_LOG_FUNCTION_START;
299         ario_conf_set_boolean (PREF_ALBUM_COLUMN_VISIBLE,
300                                gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (butt)));
301 }
302 
303 void
ario_playlist_preferences_genre_toogled_cb(GtkCheckButton * butt,ArioPlaylistPreferences * playlist_preferences)304 ario_playlist_preferences_genre_toogled_cb (GtkCheckButton *butt,
305                                             ArioPlaylistPreferences *playlist_preferences)
306 {
307         ARIO_LOG_FUNCTION_START;
308         ario_conf_set_boolean (PREF_GENRE_COLUMN_VISIBLE,
309                                gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (butt)));
310 }
311 
312 void
ario_playlist_preferences_duration_toogled_cb(GtkCheckButton * butt,ArioPlaylistPreferences * playlist_preferences)313 ario_playlist_preferences_duration_toogled_cb (GtkCheckButton *butt,
314                                                ArioPlaylistPreferences *playlist_preferences)
315 {
316         ARIO_LOG_FUNCTION_START;
317         ario_conf_set_boolean (PREF_DURATION_COLUMN_VISIBLE,
318                                gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (butt)));
319 }
320 
321 void
ario_playlist_preferences_file_toogled_cb(GtkCheckButton * butt,ArioPlaylistPreferences * playlist_preferences)322 ario_playlist_preferences_file_toogled_cb (GtkCheckButton *butt,
323                                            ArioPlaylistPreferences *playlist_preferences)
324 {
325         ARIO_LOG_FUNCTION_START;
326         ario_conf_set_boolean (PREF_FILE_COLUMN_VISIBLE,
327                                gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (butt)));
328 }
329 
330 void
ario_playlist_preferences_date_toogled_cb(GtkCheckButton * butt,ArioPlaylistPreferences * playlist_preferences)331 ario_playlist_preferences_date_toogled_cb (GtkCheckButton *butt,
332                                            ArioPlaylistPreferences *playlist_preferences)
333 {
334         ARIO_LOG_FUNCTION_START;
335         ario_conf_set_boolean (PREF_DATE_COLUMN_VISIBLE,
336                                gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (butt)));
337 }
338 
339 void
ario_playlist_preferences_disc_toogled_cb(GtkCheckButton * butt,ArioPlaylistPreferences * playlist_preferences)340 ario_playlist_preferences_disc_toogled_cb (GtkCheckButton *butt,
341                                            ArioPlaylistPreferences *playlist_preferences)
342 {
343         ARIO_LOG_FUNCTION_START;
344         ario_conf_set_boolean (PREF_DISC_COLUMN_VISIBLE,
345                                gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (butt)));
346 }
347 
348 void
ario_playlist_preferences_autoscroll_toogled_cb(GtkCheckButton * butt,ArioPlaylistPreferences * playlist_preferences)349 ario_playlist_preferences_autoscroll_toogled_cb (GtkCheckButton *butt,
350                                                  ArioPlaylistPreferences *playlist_preferences)
351 {
352         ARIO_LOG_FUNCTION_START;
353         ario_conf_set_boolean (PREF_PLAYLIST_AUTOSCROLL,
354                                gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (butt)));
355 }
356 
357 
358