Modify version string to post-release version 0.18.101
[gmpc.git] / src / gmpc-profiles.gob
blob112ee2124d026ce057b19a124240b878df0223d6
1 /* Gnome Music Player Client (GMPC)
2  * Copyright (C) 2004-2009 Qball Cow <qball@sarine.nl>
3  * Project homepage: http://gmpc.wikia.com/
4  
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.
20 requires 2.0.0
21 %ht{
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <libmpd/libmpd.h>
25 #include <libmpd/debug_printf.h>
26 #include "config1.h"
28 %privateheader{
29     #include "main.h"
30         #include "plugin.h"
31     #include "playlist3-messages.h"
33 %h{
34   enum {
35         PROFILE_ADDED,
36         PROFILE_REMOVED,
37         PROFILE_COL_CHANGED,
38     PROFILE_CURRENT
39         };
40   enum {
41     PROFILE_COL_NAME,
42     PROFILE_COL_HOSTNAME,
43     PROFILE_COL_PORT,
44     PROFILE_COL_DO_AUTH,
45     PROFILE_COL_PASSWORD,
46     PROFILE_COL_MUSIC_DIRECTORY,
47     PROFILE_NUM_COLS
48   };
50   typedef struct {
51     gchar     *id;
52     gchar     *name;
53     gchar     *hostname;
54     int       port;
55     gboolean  do_auth;
56     gchar     *password;
57     gchar     *music_directory;
58   }Profile;
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 */
70     private 
71     void
72     connect_to_profile_button(self, GtkWidget *button)
73     {
74         gchar *id = g_object_get_data(G_OBJECT(button), "profile-id");
75         if(id)
76         {
77             self_set_current(self, id);
78             mpd_disconnect(connection);
79             connect_to_mpd();
80         }
81     }
82   /* signal with (self, id) */
83   signal last NONE (INT, INT,STRING)
84   void
85   changed(self,const int changed, const int col, const char * id)
86   {
87         if(changed == PROFILE_ADDED)
88     {
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);
96         q_free(message);
98         debug_printf(DEBUG_INFO,"Item %s added\n", id);
99     }
100     else if(changed == PROFILE_REMOVED)
101     {
102                 debug_printf(DEBUG_INFO,"Item %s removed\n", id);
103     }
104         else if(changed == PROFILE_COL_CHANGED)
105         {
106         /* Disabled because of grand spamming charges */
107     /*    
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);
110         q_free(message);
111         */
112         debug_printf(DEBUG_INFO,"Item %s changed col: %i\n", id, col);
113     }
114   }
117   /**
118    * New
119    */
120   public 
121   GmpcProfiles *
122   new(void)
123   {
124     return (GmpcProfiles *)GET_NEW;
125   }
128   /** 
129    * init 
130    * */
131   init(self)
132   {
133     /**
134      * Get Profile
135      */
136     gchar *url = cfg_get_single_value_as_string(config, "connection", "profile-file");
137     if(!url)
138     {
139           url = gmpc_get_user_path("profiles.cfg");
140       cfg_set_single_value_as_string(config, "connection", "profile-file",url);
141     }
142     self->_priv->profiles = cfg_open(url);
143     if(self->_priv->profiles == NULL)
144     {
145       /**
146        * Show gtk error message and quit 
147        */
148       debug_printf(DEBUG_ERROR,"Failed to save/load Profile file:\n%s\n",url);
149       abort();
150     }
151     g_free(url);
152     self_load_from_config(self);
153     if(self->_priv->num_profiles == 0)
154     {
155       self_add_default(self);
156       self_load_from_config(self);
157     }
158   }
159   /**
160    * Add default values
161    */
162   private 
163   void
164   add_default(self)
165   {
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","");
174   }
176   /*********
177    * PROFILE
178    */
179   /* free a profile */
180   public 
181   void
182   free_profile(Profile *profile)
183   {
184     if(profile->name)
185       g_free(profile->name);
186     if(profile->id)
187       g_free(profile->id);
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);
194     g_free(profile);
195   }
197   public 
198   Profile * 
199   new_profile(void)
200   {
201     Profile *retv = g_malloc0(sizeof(*retv));
202     /* Init values */
203     retv->name      = NULL;
204     retv->id        = NULL;
205     retv->password  = NULL;
206     retv->hostname  = NULL;
207     retv->port      = 6600;
208     retv->do_auth   = FALSE; 
209     retv->music_directory = NULL;
211     return retv;
212   }
214   private 
215   void
216   load_from_config(self)
217   {
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)
221     {
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");
237     }
238     cfg_free_multiple(mult);
239   }
241   /**
242    * get profile
243    */
244   private
245   Profile *
246   get_profile(self , const gchar *id(check null))
247   {
248     int i = 0;
249     if(self->_priv->list)
250     {
251       for(i=0;self->_priv->list[i]; i++)
252       {
253         if(!strcmp(self->_priv->list[i]->id, id))
254           return self->_priv->list[i];
255       }
256     } 
257     return NULL;
258   }
260   /**
261    * get hostname 
262    */
263   public 
264   gchar *
265   get_hostname(self,const gchar *id (check null))
266   {
267     Profile *prof = self_get_profile(self, id);
268     if(!prof)
269       return NULL;
270     return prof->hostname;
271   }
272   /**
273    * get name  
274    */
275   public 
276   const gchar *                                 
277   get_name(self, const gchar *id (check null))
278   {
279     Profile *prof = self_get_profile(self, id);
280     if(!prof)
281       return NULL;
282     return prof->name;
283   }
284   /**
285    * get id 
286    */
287   public 
288   const gchar *                                 
289   get_id(self, const gchar *id (check null))
290   {
291     Profile *prof = self_get_profile(self, id);
292     if(!prof)
293       return NULL;
294     return prof->id;
295   }
296   /**
297    * get password 
298    */
299   public 
300   gchar *                                 
301   get_password(self, const gchar *id (check null))
302   {
303     Profile *prof = self_get_profile(self, id);
304     if(!prof)
305       return NULL;
306     return prof->password;
307   }
309   /**
310    * get music directory 
311    */
312   public 
313   gchar *                                 
314   get_music_directory(self, const gchar *id (check null))
315   {
316     Profile *prof = self_get_profile(self, id);
317     if(!prof)
318       return NULL;
319     return prof->music_directory;
320   }
321   /**
322    * get port 
323    */
324   public 
325   int                     
326   get_port(self, const gchar *id (check null))
327   {
328     Profile *prof = self_get_profile(self, id);
329     if(!prof)
330       return -1;
331     return prof->port;
332   }
334   /**
335    * get do_auth 
336    */
337   public 
338   gboolean 
339   get_do_auth(self, const gchar *id (check null))
340   {
341     Profile *prof = self_get_profile(self, id);
342     if(!prof)
343       return FALSE;
344     return prof->do_auth;
345   }
347   /**
348    * Create new item
349    */
350   public 
351   gchar *
352   create_new_item(self, const gchar *id)
353   {
354         return self_create_new_item_with_name(self, id, "New Profile");
355   }
356   public 
357   gchar*
358   create_new_item_with_name(self, const gchar *id, const gchar *name)
359   {
360     Profile *prof = g_malloc0(sizeof(*prof));
361     if(!id)
362       prof->id = g_strdup_printf("%u",g_random_int());
363     else
364       prof->id = g_strdup(id);
365     if(!name)
366         prof->name = g_strdup("New Profile");
367     else
368         prof->name = g_strdup(name);
369     prof->hostname = g_strdup("localhost");
370     prof->password= g_strdup("");
371     prof->port = 6600;
372     prof->do_auth = 0;
373    
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;
386     /* propagate */
387     self_changed(self,PROFILE_ADDED,-1, prof->id);
388     return prof->id;
389   }
390   public
391   void
392   remove_item(self, const gchar *id)
393   {
394     gchar *message = NULL;
395     int i=0;
396     Profile *prof =  self_get_profile(self, id);
397     if(!prof)
398     {
399         debug_printf(DEBUG_WARNING, "Trying to remove not-existing profile: %i\n", id);
400       return;
401     }
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)
407     {
408       for(; self->_priv->list[i] ;i++)
409       {
410         self->_priv->list[i] = self->_priv->list[i+1];
411       }
412       self->_priv->num_profiles--;
413       self->_priv->list = g_realloc(self->_priv->list, (self->_priv->num_profiles+1)*sizeof(Profile *));     
414       
415       cfg_remove_class(self->_priv->profiles, (char *)id);
416       self_free_profile(prof);
417     }
419     /* Display the message */
420     playlist3_show_error_message(message, ERROR_INFO);
421     q_free(message);
423     self_changed(self,PROFILE_REMOVED,-1, id);
424   }
426   /**
427    * GET CURRENT
428    */
429   public
430   gchar *
431   get_current(self)
432   {
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 */
436                 if(!prof)
437                 {
438                         g_free(id);
439             if(self->_priv->list != NULL && self->_priv->list[0])
440             {
441                             self_set_current(self, self->_priv->list[0]->id);
442             } else {
443                 self_add_default(self);    
444                 self_load_from_config(self);
445                 self_changed(self, PROFILE_ADDED, -1,"Default");
446             }
447                         id = cfg_get_single_value_as_string_with_default(config, "connection", "currentprofile", "Default");
448                 }
449                 return id; 
450   }
452   signal last NONE (STRING) 
453   void
454   set_current(self, const gchar *id(check null))
455   {
456     Profile *prof = self_get_profile(self, id);
457     if(prof){
458         cfg_set_single_value_as_string(config, "connection", "currentprofile", (char *)id);
460         self_changed(self, PROFILE_CURRENT,0, id); 
461     }
463   }
464   public
465   GList *
466   get_profiles_ids(self)
467   {
468     GList *list = NULL;
469     int i=0;
470     for(i=0; self->_priv->list[i] ; i++)
471     {
472       list = g_list_append(list,g_strdup(self->_priv->list[i]->id));
473     }
474     return list;
475   }
477   /**
478    * set field
479    */
480   public
481   void
482   set_name(self, const gchar *id (check null), const gchar *value (check null))
483   {
484     Profile *prof = self_get_profile(self, id);
486     if(!prof)
487       return ;                              
488         
489     if((value && prof->name &&  g_utf8_collate(value, prof->name) == 0)|| (value == NULL && prof->name == NULL))
490         return;
492     if(prof->name)
493       g_free(prof->name);
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);
498   }
500   public
501   void
502   set_hostname(self, const gchar *id (check null), const gchar *value (check null))
503   {
504     Profile *prof = self_get_profile(self, id);
505     if(!prof)
506       return ;                              
508     if((value && prof->hostname &&  g_utf8_collate(value, prof->hostname) == 0)|| (value == NULL && prof->hostname == NULL))
509         return;
511     if(prof->hostname)
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);
516   }
517         /**
518          * Set Password
519          */
520   public
521   void
522   set_password(self,const gchar *id (check null), gchar *value)
523   {
524     Profile *prof = self_get_profile(self, id);
525     if(!prof)
526       return ;                              
528     if((value && prof->password &&  g_utf8_collate(value, prof->password) == 0)|| (value == NULL && prof->password == NULL))
529         return;
531     if(prof->password)
532       g_free(prof->password);
533     if(value)
534       prof->password = g_strdup(value);
535     else
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);
539   }
541         /**
542          * Set Port
543          */
544   public
545   void
546   set_port(self, const gchar *id (check null), int value)
547   {
548     Profile *prof = self_get_profile(self, id);
549     if(!prof)
550       return ;                              
551     if(value == prof->port)
552             return;
553     prof->port = value;
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);
556   }
558   /**
559    * Set music directory 
560    */
561   public
562   void
563   set_music_directory(self,const gchar *id (check null), gchar *value)
564   {
565       Profile *prof = self_get_profile(self, id);
566       if(!prof)
567           return ;                              
569       if((value && prof->music_directory &&  g_utf8_collate(value, prof->music_directory) == 0)|| (value == NULL && prof->music_directory == NULL))
570           return;
572       if(prof->music_directory)
573           g_free(prof->music_directory);
574       if(value)
575           prof->music_directory = g_strdup(value);
576       else
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);
580   }
583         /**
584          * Set do auth
585          */
586   public
587   void
588   set_do_auth(self,const gchar *id (check null), int value)
589   {
590     Profile *prof = self_get_profile(self, id);
591     if(!prof)
592       return ;                              
593     if(prof->do_auth == value)
594             return;
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);
598   }
599         /**
600    * Has profile with id
601    */
602         public
603         gboolean        
604         has_profile(self, const gchar *id (check null))
605         {
606                 Profile *prof = self_get_profile(self, id);
607                 if(!prof)
608                         return FALSE;
609                 else 
610                         return TRUE;                                    
612         }
613         override (G:Object)
614         void
615         finalize (G:Object *obj)
616         {
617                 Self *self = GMPC_PROFILES(obj);
618                 if(self->_priv->list)
619                 {
620                         int i = 0;
621                         do{
622                                 Profile * prof = self->_priv->list[i];
623                                 self_free_profile(prof);
624                                 self->_priv->list[i] = NULL;
625                                 i++;
626                         }while(self->_priv->list[i]);
627                         g_free(self->_priv->list);
628                         self->_priv->list = NULL;
629                 }
630                 if(self->_priv->profiles)
631                 {
632                         cfg_close(self->_priv->profiles);
633                         self->_priv->profiles=  NULL;
634                 }
635                 PARENT_HANDLER(obj);
636         }