1 /* Gnome Music Player Client (GMPC)
2 * Copyright (C) 2004-2012 Qball Cow <qball@gmpclient.org>
3 * Project homepage: http://gmpclient.org/
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.
25 #include <libmpd/libmpd.h>
31 #include "playlist3-messages.h"
32 #define LOG_DOMAIN "Profiles"
47 PROFILE_COL_MUSIC_DIRECTORY,
48 PROFILE_COL_DB_UPDATE_TIME,
59 gchar *music_directory;
64 class Gmpc:Profiles from G:Object
66 private config_obj *profiles = {NULL};
67 private Profile **list = {NULL};
68 private int num_profiles = {0};
73 get_number_of_profiles(self)
75 return self->_priv->num_profiles;
78 /* Connect on button press event */
81 connect_to_profile_button(self, GtkWidget *button)
83 gchar *id = g_object_get_data(G_OBJECT(button), "profile-id");
86 self_set_current(self, id);
87 mpd_disconnect(connection);
91 /* signal with (self, id) */
92 signal last NONE (INT, INT,STRING)
94 changed(self,const int changed, const int col, const char * id)
96 if(changed == PROFILE_ADDED)
98 GtkWidget *connect_button = gtk_button_new_from_stock(GTK_STOCK_CONNECT);
99 gchar *message = g_markup_printf_escaped("<b>%s:</b> '%s'", _("Added profile"),self_get_name(self,id));
100 playlist3_show_error_message(message, ERROR_INFO);
102 g_object_set_data_full(G_OBJECT(connect_button), "profile-id", g_strdup(id), (GDestroyNotify)g_free);
103 g_signal_connect_swapped(G_OBJECT(connect_button), "clicked", G_CALLBACK(self_connect_to_profile_button), self);
104 playlist3_error_add_widget(connect_button);
107 g_log(LOG_DOMAIN, G_LOG_LEVEL_DEBUG,"Item %s added\n", id);
109 else if(changed == PROFILE_REMOVED)
111 g_log(LOG_DOMAIN, G_LOG_LEVEL_DEBUG,"Item %s removed\n", id);
113 else if(changed == PROFILE_COL_CHANGED)
115 /* Disabled because of grand spamming charges */
117 gchar *message = g_markup_printf_escaped("<b>%s:</b> '%s'", _("Changed profile"),self_get_name(self,id));
118 playlist3_show_error_message(message, ERROR_INFO);
121 g_log(LOG_DOMAIN, G_LOG_LEVEL_DEBUG,"Item %s changed col: %i\n", id, col);
133 return (GmpcProfiles *)GET_NEW;
146 url = gmpc_get_user_path("profiles.cfg");
147 self->_priv->profiles = cfg_open(url);
148 if(self->_priv->profiles == NULL)
151 * Show gtk error message and quit
153 g_log(LOG_DOMAIN, G_LOG_LEVEL_ERROR,"Failed to save/load Profile file:\n%s\n",url);
157 self_load_from_config(self);
158 if(self->_priv->num_profiles == 0)
160 self_add_default(self);
161 self_load_from_config(self);
171 cfg_set_single_value_as_string(config, "connection", "currentprofile", "Default");
172 cfg_set_single_value_as_string(self->_priv->profiles, "Default", "hostname", "localhost");
173 cfg_set_single_value_as_string(self->_priv->profiles, "Default", "name", "Default");
174 cfg_set_single_value_as_string(self->_priv->profiles, "Default", "password","");
175 cfg_set_single_value_as_int(self->_priv->profiles, "Default", "portnumber",6600);
176 cfg_set_single_value_as_int(self->_priv->profiles, "Default", "useauth",FALSE);
178 cfg_set_single_value_as_string(self->_priv->profiles, "Default", "music directory","");
187 free_profile(Profile *profile)
190 g_free(profile->name);
193 if(profile->hostname)
194 g_free(profile->hostname);
195 if(profile->password)
196 g_free(profile->password);
197 if(profile->music_directory)
198 g_free(profile->music_directory);
200 profile->db_update_time = 0;
208 Profile *retv = g_malloc0(sizeof(*retv));
212 retv->password = NULL;
213 retv->hostname = NULL;
215 retv->do_auth = FALSE;
216 retv->music_directory = NULL;
217 retv->db_update_time = 0;
224 load_from_config(self)
226 conf_mult_obj *iter,*mult = cfg_get_class_list(self->_priv->profiles);
227 /* free the old list */
228 for(iter = mult;iter;iter = iter->next)
230 Profile *prof = self_new_profile();
232 self->_priv->num_profiles++;
233 self->_priv->list = g_realloc(self->_priv->list, (self->_priv->num_profiles+1)*sizeof(Profile *));
234 self->_priv->list[self->_priv->num_profiles] = NULL;
235 self->_priv->list[self->_priv->num_profiles-1] = prof;
237 prof->id = g_strdup(iter->key);
238 prof->name = cfg_get_single_value_as_string(self->_priv->profiles, prof->id, "name");
239 prof->hostname = cfg_get_single_value_as_string(self->_priv->profiles, prof->id, "hostname");
240 prof->password = cfg_get_single_value_as_string(self->_priv->profiles, prof->id, "password");
241 prof->port = cfg_get_single_value_as_int(self->_priv->profiles, prof->id, "portnumber");
242 prof->do_auth = cfg_get_single_value_as_int(self->_priv->profiles, prof->id, "useauth");
244 prof->music_directory = cfg_get_single_value_as_string(self->_priv->profiles, prof->id, "music directory");
245 prof->db_update_time = cfg_get_single_value_as_int_with_default(self->_priv->profiles, prof->id, "db update time",0);
247 cfg_free_multiple(mult);
255 get_profile(self , const gchar *id(check null))
258 if(self->_priv->list)
260 for(i=0;self->_priv->list[i]; i++)
262 if(!strcmp(self->_priv->list[i]->id, id))
263 return self->_priv->list[i];
274 get_hostname(self,const gchar *id (check null))
276 Profile *prof = self_get_profile(self, id);
279 return prof->hostname;
286 get_name(self, const gchar *id (check null))
288 Profile *prof = self_get_profile(self, id);
298 get_id(self, const gchar *id (check null))
300 Profile *prof = self_get_profile(self, id);
310 get_password(self, const gchar *id (check null))
312 Profile *prof = self_get_profile(self, id);
315 return prof->password;
319 * get music directory
323 get_music_directory(self, const gchar *id (check null))
325 Profile *prof = self_get_profile(self, id);
328 return prof->music_directory;
331 * get music directory
335 get_db_update_time(self, const gchar *id (check null))
337 Profile *prof = self_get_profile(self, id);
340 return prof->db_update_time;
347 get_port(self, const gchar *id (check null))
349 Profile *prof = self_get_profile(self, id);
360 get_do_auth(self, const gchar *id (check null))
362 Profile *prof = self_get_profile(self, id);
365 return prof->do_auth;
373 create_new_item(self, const gchar *id)
375 return self_create_new_item_with_name(self, id, "New Profile");
379 create_new_item_with_name(self, const gchar *id, const gchar *name)
381 Profile *prof = g_malloc0(sizeof(*prof));
383 prof->id = g_strdup_printf("%u",g_random_int());
385 prof->id = g_strdup(id);
387 prof->name = g_strdup("New Profile");
389 prof->name = g_strdup(name);
390 prof->hostname = g_strdup("localhost");
391 prof->password= g_strdup("");
395 /* safe this to the config file */
396 cfg_set_single_value_as_string(self->_priv->profiles, prof->id, "name", prof->name);
397 cfg_set_single_value_as_string(self->_priv->profiles, prof->id, "hostname", prof->hostname);
398 cfg_set_single_value_as_string(self->_priv->profiles, prof->id, "password", prof->password);
399 cfg_set_single_value_as_int(self->_priv->profiles, prof->id, "portnumber", prof->port);
400 cfg_set_single_value_as_int(self->_priv->profiles, prof->id, "useauth", prof->do_auth);
401 cfg_set_single_value_as_string(self->_priv->profiles, prof->id, "music directory", prof->music_directory);
403 cfg_get_single_value_as_int_with_default(self->_priv->profiles, prof->id, "db update time",(int)(prof->db_update_time));
405 self->_priv->num_profiles++;
406 self->_priv->list = g_realloc(self->_priv->list, (self->_priv->num_profiles+1)*sizeof(Profile *));
407 self->_priv->list[self->_priv->num_profiles] = NULL;
408 self->_priv->list[self->_priv->num_profiles-1] = prof;
410 self_changed(self,PROFILE_ADDED,-1, prof->id);
415 remove_item(self, const gchar *id)
417 gchar *message = NULL;
419 Profile *prof = self_get_profile(self, id);
422 g_log(LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Trying to remove not-existing profile: %s\n", id);
425 /* Generate removal message before the actual profile is destroyed */
426 message = g_markup_printf_escaped("<b>%s:</b> '%s'", _("Removed profile"),self_get_name(self,id));
428 for(i=0; self->_priv->list[i] && self->_priv->list[i] != prof;i++);
429 if(self->_priv->list[i] == prof)
431 for(; self->_priv->list[i] ;i++)
433 self->_priv->list[i] = self->_priv->list[i+1];
435 self->_priv->num_profiles--;
436 self->_priv->list = g_realloc(self->_priv->list, (self->_priv->num_profiles+1)*sizeof(Profile *));
438 cfg_remove_class(self->_priv->profiles, (char *)id);
439 self_free_profile(prof);
442 /* Display the message */
443 playlist3_show_error_message(message, ERROR_INFO);
446 self_changed(self,PROFILE_REMOVED,-1, id);
456 gchar *id = cfg_get_single_value_as_string_with_default(config, "connection", "currentprofile", "Default");
457 Profile *prof = self_get_profile(self, id);
458 /* if not available get the first one */
462 if(self->_priv->list != NULL && self->_priv->list[0])
464 self_set_current(self, self->_priv->list[0]->id);
466 self_add_default(self);
467 self_load_from_config(self);
468 self_changed(self, PROFILE_ADDED, -1,"Default");
470 id = cfg_get_single_value_as_string_with_default(config, "connection", "currentprofile", "Default");
475 signal last NONE (STRING)
477 set_current(self, const gchar *id(check null))
479 Profile *prof = self_get_profile(self, id);
481 cfg_set_single_value_as_string(config, "connection", "currentprofile", (char *)id);
483 self_changed(self, PROFILE_CURRENT,0, id);
489 get_profiles_ids(self)
493 for(i=0; self->_priv->list[i] ; i++)
495 list = g_list_append(list,g_strdup(self->_priv->list[i]->id));
505 set_name(self, const gchar *id (check null), const gchar *value (check null))
507 Profile *prof = self_get_profile(self, id);
512 if((value && prof->name && g_utf8_collate(value, prof->name) == 0)|| (value == NULL && prof->name == NULL))
518 prof->name = g_strdup(value);
519 cfg_set_single_value_as_string(self->_priv->profiles, (char *)id, "name",prof->name);
520 self_changed(self,PROFILE_COL_CHANGED, PROFILE_COL_NAME,id);
525 set_hostname(self, const gchar *id (check null), const gchar *value (check null))
527 Profile *prof = self_get_profile(self, id);
531 if((value && prof->hostname && g_utf8_collate(value, prof->hostname) == 0)|| (value == NULL && prof->hostname == NULL))
535 g_free(prof->hostname);
536 prof->hostname = g_strdup(value);
537 cfg_set_single_value_as_string(self->_priv->profiles, (char *)id, "hostname",prof->hostname);
538 self_changed(self,PROFILE_COL_CHANGED, PROFILE_COL_HOSTNAME,id);
545 set_password(self,const gchar *id (check null), gchar *value)
547 Profile *prof = self_get_profile(self, id);
551 if((value && prof->password && g_utf8_collate(value, prof->password) == 0)|| (value == NULL && prof->password == NULL))
555 g_free(prof->password);
557 prof->password = g_strdup(value);
559 prof->password = g_strdup("");
560 cfg_set_single_value_as_string(self->_priv->profiles, (char *)id, "password",prof->password);
561 self_changed(self,PROFILE_COL_CHANGED, PROFILE_COL_PASSWORD,id);
569 set_port(self, const gchar *id (check null), int value)
571 Profile *prof = self_get_profile(self, id);
574 if(value == prof->port)
577 cfg_set_single_value_as_int(self->_priv->profiles, (char *)id, "portnumber",prof->port);
578 self_changed(self,PROFILE_COL_CHANGED, PROFILE_COL_PORT,id);
582 * Set music directory
586 set_music_directory(self,const gchar *id (check null), gchar *value)
588 Profile *prof = self_get_profile(self, id);
592 if((value && prof->music_directory && g_utf8_collate(value, prof->music_directory) == 0)|| (value == NULL && prof->music_directory == NULL))
595 if(prof->music_directory)
596 g_free(prof->music_directory);
598 prof->music_directory = g_strdup(value);
600 prof->music_directory = g_strdup("");
601 cfg_set_single_value_as_string(self->_priv->profiles, (char *)id, "music directory",prof->music_directory);
602 self_changed(self,PROFILE_COL_CHANGED, PROFILE_COL_MUSIC_DIRECTORY,id);
608 set_db_update_time(self, const gchar *id (check null), int value)
611 Profile *prof = self_get_profile(self, id);
615 if(value == prof->db_update_time) return;
617 cfg_set_single_value_as_int(self->_priv->profiles, (char *)id, "db update time",(int)value);
618 self_changed(self,PROFILE_COL_CHANGED, PROFILE_COL_DB_UPDATE_TIME,id);
625 set_do_auth(self,const gchar *id (check null), int value)
627 Profile *prof = self_get_profile(self, id);
630 if(prof->do_auth == value)
632 prof->do_auth = value;
633 cfg_set_single_value_as_int(self->_priv->profiles,(char *)id, "useauth",prof->do_auth);
634 self_changed(self,PROFILE_COL_CHANGED, PROFILE_COL_DO_AUTH,id);
637 * Has profile with id
641 has_profile(self, const gchar *id (check null))
643 Profile *prof = self_get_profile(self, id);
652 finalize (G:Object *obj)
654 Self *self = GMPC_PROFILES(obj);
655 if(self->_priv->list)
659 Profile * prof = self->_priv->list[i];
660 self_free_profile(prof);
661 self->_priv->list[i] = NULL;
663 }while(self->_priv->list[i]);
664 g_free(self->_priv->list);
665 self->_priv->list = NULL;
667 if(self->_priv->profiles)
669 cfg_close(self->_priv->profiles);
670 self->_priv->profiles= NULL;
677 set_profile_from_name(self, const gchar *name (check null))
679 GList *iter, *items = gmpc_profiles_get_profiles_ids(gmpc_profiles);
680 for (iter = g_list_first(items); iter; iter = g_list_next(iter))
682 const char *pname = gmpc_profiles_get_name(gmpc_profiles,
683 (const gchar *)iter->data);
685 g_utf8_collate(name, pname) == 0)
687 connection_set_current_profile((const gchar *)iter->data);
691 g_list_foreach(items, (GFunc) g_free, NULL);