1 /* Gnome Music Player Client (GMPC)
2 * Copyright (C) 2004-2009 Qball Cow <qball@sarine.nl>
3 * Project homepage: http://gmpc.wikia.com/
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <libmpd/libmpd.h>
25 #include <libmpd/debug_printf.h>
31 #include "playlist3-messages.h"
46 PROFILE_COL_MUSIC_DIRECTORY,
57 gchar *music_directory;
62 class Gmpc:Profiles from G:Object
64 private config_obj *profiles = {NULL};
65 private Profile **list = {NULL};
66 private int num_profiles = {0};
69 /* Connect on button press event */
72 connect_to_profile_button(self, GtkWidget *button)
74 gchar *id = g_object_get_data(G_OBJECT(button), "profile-id");
77 self_set_current(self, id);
78 mpd_disconnect(connection);
82 /* signal with (self, id) */
83 signal last NONE (INT, INT,STRING)
85 changed(self,const int changed, const int col, const char * id)
87 if(changed == PROFILE_ADDED)
89 GtkWidget *connect_button = gtk_button_new_from_stock(GTK_STOCK_CONNECT);
90 gchar *message = g_markup_printf_escaped("<b>%s:</b> '%s'", _("Added profile"),self_get_name(self,id));
91 playlist3_show_error_message(message, ERROR_INFO);
93 g_object_set_data_full(G_OBJECT(connect_button), "profile-id", g_strdup(id), (GDestroyNotify)g_free);
94 g_signal_connect_swapped(G_OBJECT(connect_button), "clicked", G_CALLBACK(self_connect_to_profile_button), self);
95 playlist3_error_add_widget(connect_button);
98 debug_printf(DEBUG_INFO,"Item %s added\n", id);
100 else if(changed == PROFILE_REMOVED)
102 debug_printf(DEBUG_INFO,"Item %s removed\n", id);
104 else if(changed == PROFILE_COL_CHANGED)
106 /* Disabled because of grand spamming charges */
108 gchar *message = g_markup_printf_escaped("<b>%s:</b> '%s'", _("Changed profile"),self_get_name(self,id));
109 playlist3_show_error_message(message, ERROR_INFO);
112 debug_printf(DEBUG_INFO,"Item %s changed col: %i\n", id, col);
124 return (GmpcProfiles *)GET_NEW;
136 gchar *url = cfg_get_single_value_as_string(config, "connection", "profile-file");
139 url = gmpc_get_user_path("profiles.cfg");
140 cfg_set_single_value_as_string(config, "connection", "profile-file",url);
142 self->_priv->profiles = cfg_open(url);
143 if(self->_priv->profiles == NULL)
146 * Show gtk error message and quit
148 debug_printf(DEBUG_ERROR,"Failed to save/load Profile file:\n%s\n",url);
152 self_load_from_config(self);
153 if(self->_priv->num_profiles == 0)
155 self_add_default(self);
156 self_load_from_config(self);
166 cfg_set_single_value_as_string(config, "connection", "currentprofile", "Default");
167 cfg_set_single_value_as_string(self->_priv->profiles, "Default", "hostname", "localhost");
168 cfg_set_single_value_as_string(self->_priv->profiles, "Default", "name", "Default");
169 cfg_set_single_value_as_string(self->_priv->profiles, "Default", "password","");
170 cfg_set_single_value_as_int(self->_priv->profiles, "Default", "portnumber",6600);
171 cfg_set_single_value_as_int(self->_priv->profiles, "Default", "useauth",FALSE);
173 cfg_set_single_value_as_string(self->_priv->profiles, "Default", "music directory","");
182 free_profile(Profile *profile)
185 g_free(profile->name);
188 if(profile->hostname)
189 g_free(profile->hostname);
190 if(profile->password)
191 g_free(profile->password);
192 if(profile->music_directory)
193 g_free(profile->music_directory);
201 Profile *retv = g_malloc0(sizeof(*retv));
205 retv->password = NULL;
206 retv->hostname = NULL;
208 retv->do_auth = FALSE;
209 retv->music_directory = NULL;
216 load_from_config(self)
218 conf_mult_obj *iter,*mult = cfg_get_class_list(self->_priv->profiles);
219 /* free the old list */
220 for(iter = mult;iter;iter = iter->next)
222 Profile *prof = self_new_profile();
224 self->_priv->num_profiles++;
225 self->_priv->list = g_realloc(self->_priv->list, (self->_priv->num_profiles+1)*sizeof(Profile *));
226 self->_priv->list[self->_priv->num_profiles] = NULL;
227 self->_priv->list[self->_priv->num_profiles-1] = prof;
229 prof->id = g_strdup(iter->key);
230 prof->name = cfg_get_single_value_as_string(self->_priv->profiles, prof->id, "name");
231 prof->hostname = cfg_get_single_value_as_string(self->_priv->profiles, prof->id, "hostname");
232 prof->password = cfg_get_single_value_as_string(self->_priv->profiles, prof->id, "password");
233 prof->port = cfg_get_single_value_as_int(self->_priv->profiles, prof->id, "portnumber");
234 prof->do_auth = cfg_get_single_value_as_int(self->_priv->profiles, prof->id, "useauth");
236 prof->music_directory = cfg_get_single_value_as_string(self->_priv->profiles, prof->id, "music directory");
238 cfg_free_multiple(mult);
246 get_profile(self , const gchar *id(check null))
249 if(self->_priv->list)
251 for(i=0;self->_priv->list[i]; i++)
253 if(!strcmp(self->_priv->list[i]->id, id))
254 return self->_priv->list[i];
265 get_hostname(self,const gchar *id (check null))
267 Profile *prof = self_get_profile(self, id);
270 return prof->hostname;
277 get_name(self, const gchar *id (check null))
279 Profile *prof = self_get_profile(self, id);
289 get_id(self, const gchar *id (check null))
291 Profile *prof = self_get_profile(self, id);
301 get_password(self, const gchar *id (check null))
303 Profile *prof = self_get_profile(self, id);
306 return prof->password;
310 * get music directory
314 get_music_directory(self, const gchar *id (check null))
316 Profile *prof = self_get_profile(self, id);
319 return prof->music_directory;
326 get_port(self, const gchar *id (check null))
328 Profile *prof = self_get_profile(self, id);
339 get_do_auth(self, const gchar *id (check null))
341 Profile *prof = self_get_profile(self, id);
344 return prof->do_auth;
352 create_new_item(self, const gchar *id)
354 return self_create_new_item_with_name(self, id, "New Profile");
358 create_new_item_with_name(self, const gchar *id, const gchar *name)
360 Profile *prof = g_malloc0(sizeof(*prof));
362 prof->id = g_strdup_printf("%u",g_random_int());
364 prof->id = g_strdup(id);
366 prof->name = g_strdup("New Profile");
368 prof->name = g_strdup(name);
369 prof->hostname = g_strdup("localhost");
370 prof->password= g_strdup("");
374 /* safe this to the config file */
375 cfg_set_single_value_as_string(self->_priv->profiles, prof->id, "name", prof->name);
376 cfg_set_single_value_as_string(self->_priv->profiles, prof->id, "hostname", prof->hostname);
377 cfg_set_single_value_as_string(self->_priv->profiles, prof->id, "password", prof->password);
378 cfg_set_single_value_as_int(self->_priv->profiles, prof->id, "portnumber", prof->port);
379 cfg_set_single_value_as_int(self->_priv->profiles, prof->id, "useauth", prof->do_auth);
380 cfg_set_single_value_as_string(self->_priv->profiles, prof->id, "music directory", prof->music_directory);
382 self->_priv->num_profiles++;
383 self->_priv->list = g_realloc(self->_priv->list, (self->_priv->num_profiles+1)*sizeof(Profile *));
384 self->_priv->list[self->_priv->num_profiles] = NULL;
385 self->_priv->list[self->_priv->num_profiles-1] = prof;
387 self_changed(self,PROFILE_ADDED,-1, prof->id);
392 remove_item(self, const gchar *id)
394 gchar *message = NULL;
396 Profile *prof = self_get_profile(self, id);
399 debug_printf(DEBUG_WARNING, "Trying to remove not-existing profile: %i\n", id);
402 /* Generate removal message before the actual profile is destroyed */
403 message = g_markup_printf_escaped("<b>%s:</b> '%s'", _("Removed profile"),self_get_name(self,id));
405 for(i=0; self->_priv->list[i] && self->_priv->list[i] != prof;i++);
406 if(self->_priv->list[i] == prof)
408 for(; self->_priv->list[i] ;i++)
410 self->_priv->list[i] = self->_priv->list[i+1];
412 self->_priv->num_profiles--;
413 self->_priv->list = g_realloc(self->_priv->list, (self->_priv->num_profiles+1)*sizeof(Profile *));
415 cfg_remove_class(self->_priv->profiles, (char *)id);
416 self_free_profile(prof);
419 /* Display the message */
420 playlist3_show_error_message(message, ERROR_INFO);
423 self_changed(self,PROFILE_REMOVED,-1, id);
433 gchar *id = cfg_get_single_value_as_string_with_default(config, "connection", "currentprofile", "Default");
434 Profile *prof = self_get_profile(self, id);
435 /* if not available get the first one */
439 if(self->_priv->list != NULL && self->_priv->list[0])
441 self_set_current(self, self->_priv->list[0]->id);
443 self_add_default(self);
444 self_load_from_config(self);
445 self_changed(self, PROFILE_ADDED, -1,"Default");
447 id = cfg_get_single_value_as_string_with_default(config, "connection", "currentprofile", "Default");
452 signal last NONE (STRING)
454 set_current(self, const gchar *id(check null))
456 Profile *prof = self_get_profile(self, id);
458 cfg_set_single_value_as_string(config, "connection", "currentprofile", (char *)id);
460 self_changed(self, PROFILE_CURRENT,0, id);
466 get_profiles_ids(self)
470 for(i=0; self->_priv->list[i] ; i++)
472 list = g_list_append(list,g_strdup(self->_priv->list[i]->id));
482 set_name(self, const gchar *id (check null), const gchar *value (check null))
484 Profile *prof = self_get_profile(self, id);
489 if((value && prof->name && g_utf8_collate(value, prof->name) == 0)|| (value == NULL && prof->name == NULL))
495 prof->name = g_strdup(value);
496 cfg_set_single_value_as_string(self->_priv->profiles, (char *)id, "name",prof->name);
497 self_changed(self,PROFILE_COL_CHANGED, PROFILE_COL_NAME,id);
502 set_hostname(self, const gchar *id (check null), const gchar *value (check null))
504 Profile *prof = self_get_profile(self, id);
508 if((value && prof->hostname && g_utf8_collate(value, prof->hostname) == 0)|| (value == NULL && prof->hostname == NULL))
512 g_free(prof->hostname);
513 prof->hostname = g_strdup(value);
514 cfg_set_single_value_as_string(self->_priv->profiles, (char *)id, "hostname",prof->hostname);
515 self_changed(self,PROFILE_COL_CHANGED, PROFILE_COL_HOSTNAME,id);
522 set_password(self,const gchar *id (check null), gchar *value)
524 Profile *prof = self_get_profile(self, id);
528 if((value && prof->password && g_utf8_collate(value, prof->password) == 0)|| (value == NULL && prof->password == NULL))
532 g_free(prof->password);
534 prof->password = g_strdup(value);
536 prof->password = g_strdup("");
537 cfg_set_single_value_as_string(self->_priv->profiles, (char *)id, "password",prof->password);
538 self_changed(self,PROFILE_COL_CHANGED, PROFILE_COL_PASSWORD,id);
546 set_port(self, const gchar *id (check null), int value)
548 Profile *prof = self_get_profile(self, id);
551 if(value == prof->port)
554 cfg_set_single_value_as_int(self->_priv->profiles, (char *)id, "portnumber",prof->port);
555 self_changed(self,PROFILE_COL_CHANGED, PROFILE_COL_PORT,id);
559 * Set music directory
563 set_music_directory(self,const gchar *id (check null), gchar *value)
565 Profile *prof = self_get_profile(self, id);
569 if((value && prof->music_directory && g_utf8_collate(value, prof->music_directory) == 0)|| (value == NULL && prof->music_directory == NULL))
572 if(prof->music_directory)
573 g_free(prof->music_directory);
575 prof->music_directory = g_strdup(value);
577 prof->music_directory = g_strdup("");
578 cfg_set_single_value_as_string(self->_priv->profiles, (char *)id, "music directory",prof->music_directory);
579 self_changed(self,PROFILE_COL_CHANGED, PROFILE_COL_MUSIC_DIRECTORY,id);
588 set_do_auth(self,const gchar *id (check null), int value)
590 Profile *prof = self_get_profile(self, id);
593 if(prof->do_auth == value)
595 prof->do_auth = value;
596 cfg_set_single_value_as_int(self->_priv->profiles,(char *)id, "useauth",prof->do_auth);
597 self_changed(self,PROFILE_COL_CHANGED, PROFILE_COL_DO_AUTH,id);
600 * Has profile with id
604 has_profile(self, const gchar *id (check null))
606 Profile *prof = self_get_profile(self, id);
615 finalize (G:Object *obj)
617 Self *self = GMPC_PROFILES(obj);
618 if(self->_priv->list)
622 Profile * prof = self->_priv->list[i];
623 self_free_profile(prof);
624 self->_priv->list[i] = NULL;
626 }while(self->_priv->list[i]);
627 g_free(self->_priv->list);
628 self->_priv->list = NULL;
630 if(self->_priv->profiles)
632 cfg_close(self->_priv->profiles);
633 self->_priv->profiles= NULL;