1 /* Gnome Music Player Client (GMPC)
2 * Copyright (C) 2004-2012 Qball Cow <qball@gmpclient.org>
3 * Copyright (C) 2012 Quentin "Sardem FF7" Glidic
4 * Project homepage: http://gmpclient.org/
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 private const string some_unique_name_prfs
= Config
.VERSION
;
27 public string id
= null;
28 public string name
= null;
29 public string hostname
= "localhost";
30 public int port
= 6600;
31 public bool do_auth
= false;
32 public string password
= "";
33 public string music_directory
= null;
34 public int db_update_time
= 0;
37 public class Profiles
: GLib
.Object
39 const string LOG_DOMAIN
= "Profiles";
61 private Settings profiles
;
62 private GLib
.HashTable
<string, Profile
> list
;
67 get_number_of_profiles()
69 return this
.list
.size();
72 /* Connect on button press event */
75 connect_to_profile_button(Gtk
.Button button
)
77 string id
= button
.get_data("profile-id");
81 MpdInteraction
.disconnect();
82 MpdInteraction
.connect();
86 public signal void changed(int changed
, int col
, string id
);
88 changed_internal(Action changed
, Column col
, string id
)
93 var connect_button
= new Gtk
.Button
.from_stock(Gtk
.Stock
.CONNECT
);
94 string message
= GLib
.Markup
.printf_escaped("<b>%s:</b> '%s'", _("Added profile"), this
.get_name(id
));
95 Gmpc
.Messages
.show(message
, Gmpc
.Messages
.Level
.INFO
);
97 connect_button
.set_data_full("profile-id", id
, GLib
.free
);
98 connect_button
.clicked
.connect(this
.connect_to_profile_button
);
99 Gmpc
.Messages
.add_widget(connect_button
);
101 GLib
.log(LOG_DOMAIN
, GLib
.LogLevelFlags
.LEVEL_DEBUG
,"Item %s added\n", id
);
104 GLib
.log(LOG_DOMAIN
, GLib
.LogLevelFlags
.LEVEL_DEBUG
,"Item %s removed\n", id
);
106 case Action
.COL_CHANGED
:
107 /* Disabled because of grand spamming charges */
109 stringmessage = GLib.Markup.printf_escaped("<b>%s:</b> '%s'", _("Changed profile"),this.get_name(id));
110 Gmpc.Messages.show(message, ERROR_INFO);
113 GLib
.log(LOG_DOMAIN
, GLib
.LogLevelFlags
.LEVEL_DEBUG
,"Item %s changed col: %i\n", id
, col
);
118 this
.changed(changed
, col
, id
);
124 this
.list
= new GLib
.HashTable
<string, Profile
>(GLib
.str_hash
, GLib
.str_equal
);
129 url
= Gmpc
.user_path("profiles.cfg");
130 this
.profiles
= new Gmpc
.Settings
.from_file(url
);
131 if(this
.profiles
== null)
134 * Show gtk error message and quit
136 GLib
.log(LOG_DOMAIN
, GLib
.LogLevelFlags
.LEVEL_ERROR
,"Failed to save/load Profile file:\n%s\n",url
);
139 this
.load_from_config();
140 if(this
.list
.size() == 0)
143 this
.load_from_config();
154 Gmpc
.config
.set_string("connection", "currentprofile", "Default");
155 this
.profiles
.set_string("Default", "hostname", "localhost");
156 this
.profiles
.set_string("Default", "name", "Default");
157 this
.profiles
.set_string("Default", "password", "");
158 this
.profiles
.set_int("Default", "portnumber", 6600);
159 this
.profiles
.set_bool("Default", "useauth", false);
160 this
.profiles
.set_string("Default", "music directory", "");
167 SettingsList list
= this
.profiles
.get_class_list();
168 for (unowned SettingsList iter
= list
; iter
!= null ; iter
= iter
.next
)
170 Profile prof
= new
Profile();
173 prof
.name
= this
.profiles
.get_string(prof
.id
, "name");
174 prof
.hostname
= this
.profiles
.get_string(prof
.id
, "hostname");
175 prof
.password
= this
.profiles
.get_string(prof
.id
, "password");
176 prof
.port
= this
.profiles
.get_int(prof
.id
, "portnumber");
177 prof
.do_auth
= this
.profiles
.get_bool(prof
.id
, "useauth");
179 prof
.music_directory
= this
.profiles
.get_string(prof
.id
, "music directory");
180 prof
.db_update_time
= this
.profiles
.get_int_with_default(prof
.id
, "db update time", 0);
182 this
.list
.insert(prof
.id
, prof
);
191 get_hostname(string id
)
193 Profile prof
= this
.list
.lookup(id
);
196 return prof
.hostname
;
205 Profile prof
= this
.list
.lookup(id
);
217 Profile prof
= this
.list
.lookup(id
);
227 get_password(string id
)
229 Profile prof
= this
.list
.lookup(id
);
232 return prof
.password
;
236 * get music directory
240 get_music_directory(string id
)
242 Profile prof
= this
.list
.lookup(id
);
245 return prof
.music_directory
;
248 * get music directory
252 get_db_update_time(string id
)
254 Profile prof
= this
.list
.lookup(id
);
257 return prof
.db_update_time
;
266 Profile prof
= this
.list
.lookup(id
);
277 get_do_auth(string id
)
279 Profile prof
= this
.list
.lookup(id
);
290 create_new_item(string id
)
292 return this
.create_new_item_with_name(id
, "New Profile");
297 create_new_item_with_name(string? id
, string? name
)
299 var prof
= new
Profile();
301 prof
.id
= GLib
.Random
.next_int().to_string();
305 prof
.name
= "New Profile";
309 /* safe this to the config file */
310 this
.profiles
.set_string(prof
.id
, "name", prof
.name
);
311 this
.profiles
.set_string(prof
.id
, "hostname", prof
.hostname
);
312 this
.profiles
.set_string(prof
.id
, "password", prof
.password
);
313 this
.profiles
.set_int(prof
.id
, "portnumber", prof
.port
);
314 this
.profiles
.set_bool(prof
.id
, "useauth", prof
.do_auth
);
315 this
.profiles
.set_string(prof
.id
, "music directory", prof
.music_directory
);
317 this
.profiles
.get_int_with_default(prof
.id
, "db update time",(int)(prof
.db_update_time
));
319 this
.list
.insert(prof
.id
, prof
);
322 this
.changed_internal(Action
.ADDED
, Column
.NUM_COLS
, prof
.id
);
328 remove_item(string id
)
330 string message
= null;
331 if(!this
.has_profile(id
))
333 GLib
.log(LOG_DOMAIN
, GLib
.LogLevelFlags
.LEVEL_WARNING
, "Trying to remove not-existing profile: %s\n", id
);
336 /* Generate removal message before the actual profile is destroyed */
337 message
= GLib
.Markup
.printf_escaped("<b>%s:</b> '%s'", _("Removed profile"),this
.get_name(id
));
339 this
.list
.remove(id
);
341 /* Display the message */
342 Gmpc
.Messages
.show(message
, Gmpc
.Messages
.Level
.INFO
);
344 this
.changed_internal(Action
.REMOVED
, Column
.NUM_COLS
, id
);
354 var id
= Gmpc
.config
.get_string_with_default("connection", "currentprofile", "Default");
355 Profile prof
= this
.list
.lookup(id
);
356 /* if not available get the first one */
359 if(this
.list
.size() > 0)
361 var iter
= GLib
.HashTableIter
<string, Profile
>(this
.list
);
362 iter
.next(out id
, out prof
);
363 this
.set_current(id
);
366 this
.load_from_config();
367 this
.changed_internal(Action
.ADDED
, Column
.NUM_COLS
, "Default");
369 id
= Gmpc
.config
.get_string_with_default("connection", "currentprofile", "Default");
374 public signal void profile_changed(string id
);
376 set_current(string id
)
378 if(this
.has_profile(id
))
380 Gmpc
.config
.set_string("connection", "currentprofile", id
);
382 this
.changed_internal(Action
.CURRENT
, 0, id
);
384 this
.profile_changed(id
);
388 GLib
.List
<weak string>
391 return this
.list
.get_keys();
399 set_name(string id
, string @value
)
401 Profile prof
= this
.list
.lookup(id
);
402 if(prof
== null) return;
404 if(@value
== prof
.name
) return;
407 this
.profiles
.set_string(id
, "name", prof
.name
);
408 this
.changed_internal(Action
.COL_CHANGED
, Column
.NAME
, id
);
413 set_hostname(string id
, string @value
)
415 Profile prof
= this
.list
.lookup(id
);
416 if(prof
== null) return;
418 if(@value
== prof
.hostname
) return;
419 prof
.hostname
= @value
;
421 this
.profiles
.set_string(id
, "hostname", prof
.hostname
);
422 this
.changed_internal(Action
.COL_CHANGED
, Column
.HOSTNAME
, id
);
430 set_password(string id
, string? @value
)
432 Profile prof
= this
.list
.lookup(id
);
433 if(prof
== null) return;
435 if(@value
== prof
.password
) return;
436 prof
.password
= (@value
!= null) ? @value
: "";
438 this
.profiles
.set_string(id
, "password", prof
.password
);
439 this
.changed_internal(Action
.COL_CHANGED
, Column
.PASSWORD
, id
);
447 set_port(string id
, int @value
)
449 Profile prof
= this
.list
.lookup(id
);
450 if(prof
== null) return;
452 if(value
== prof
.port
) return;
455 this
.profiles
.set_int(id
, "portnumber",prof
.port
);
456 this
.changed_internal(Action
.COL_CHANGED
, Column
.PORT
, id
);
460 * Set music directory
464 set_music_directory(string id
, string @value
)
466 Profile prof
= this
.list
.lookup(id
);
467 if(prof
== null) return;
469 if(@value
== prof
.music_directory
) return;
470 prof
.music_directory
= (@value
!= null) ? @value
: "";
472 this
.profiles
.set_string(id
, "music directory", prof
.music_directory
);
473 this
.changed_internal(Action
.COL_CHANGED
, Column
.MUSIC_DIRECTORY
,id
);
479 set_db_update_time(string id
, int @value
)
481 Profile prof
= this
.list
.lookup(id
);
482 if(prof
== null) return;
484 if(@value
== prof
.db_update_time
) return;
485 prof
.db_update_time
= @value
;
487 this
.profiles
.set_int(id
, "db update time", @value
);
488 this
.changed_internal(Action
.COL_CHANGED
, Column
.DB_UPDATE_TIME
, id
);
496 set_do_auth(string id
, bool @value
)
498 Profile prof
= this
.list
.lookup(id
);
499 if(prof
== null) return;
501 if(prof
.do_auth
== @value
) return;
502 prof
.do_auth
= @value
;
504 this
.profiles
.set_bool(id
, "useauth", prof
.do_auth
);
505 this
.changed_internal(Action
.COL_CHANGED
, Column
.DO_AUTH
, id
);
509 * Has profile with id
513 has_profile(string id
)
515 return ( this
.list
.lookup(id
) != null );
520 set_profile_from_name(string name
)
522 var prof
= this
.list
.find((id
, prof
) => {
523 return (name
== prof
.name
);
525 MpdInteraction
.set_current_profile(prof
.id
);