GmpcProfiles: Port to Vala
[gmpc.git] / src / Widgets / gmpc-profiles.vala
blob935cf69e6393a01442dd5e83148d18808a1fdde1
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;
23 namespace Gmpc
25 private class Profile
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";
41 public enum Action
43 ADDED,
44 REMOVED,
45 COL_CHANGED,
46 CURRENT
49 public enum Column
51 NAME,
52 HOSTNAME,
53 PORT,
54 DO_AUTH,
55 PASSWORD,
56 MUSIC_DIRECTORY,
57 DB_UPDATE_TIME,
58 NUM_COLS
61 private Settings profiles;
62 private GLib.HashTable<string, Profile> list;
65 public
66 uint
67 get_number_of_profiles()
69 return this.list.size();
72 /* Connect on button press event */
73 private
74 void
75 connect_to_profile_button(Gtk.Button button)
77 string id = button.get_data("profile-id");
78 if(id != null)
80 this.set_current(id);
81 MpdInteraction.disconnect();
82 MpdInteraction.connect();
86 public signal void changed(int changed, int col, string id);
87 void
88 changed_internal(Action changed, Column col, string id)
90 switch (changed)
92 case Action.ADDED:
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);
102 break;
103 case Action.REMOVED:
104 GLib.log(LOG_DOMAIN, GLib.LogLevelFlags.LEVEL_DEBUG,"Item %s removed\n", id);
105 break;
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);
111 q_free(message);
113 GLib.log(LOG_DOMAIN, GLib.LogLevelFlags.LEVEL_DEBUG,"Item %s changed col: %i\n", id, col);
114 break;
115 case Action.CURRENT:
116 break;
118 this.changed(changed, col, id);
122 construct
124 this.list = new GLib.HashTable<string, Profile>(GLib.str_hash, GLib.str_equal);
125 string url;
127 * Get Profile
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);
137 Gtk.main_quit();
139 this.load_from_config();
140 if(this.list.size() == 0)
142 this.add_default();
143 this.load_from_config();
148 * Add default values
150 private
151 void
152 add_default()
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", "");
163 private
164 void
165 load_from_config()
167 SettingsList list = this.profiles.get_class_list();
168 for (unowned SettingsList iter = list ; iter != null ; iter = iter.next)
170 Profile prof = new Profile();
172 prof.id = iter.key;
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);
187 * get hostname
189 public
190 string?
191 get_hostname(string id)
193 Profile prof = this.list.lookup(id);
194 if(prof == null)
195 return null;
196 return prof.hostname;
199 * get name
201 public
202 unowned string?
203 get_name(string id)
205 Profile prof = this.list.lookup(id);
206 if(prof == null)
207 return null;
208 return prof.name;
211 * get id
213 public
214 unowned string?
215 get_id(string id)
217 Profile prof = this.list.lookup(id);
218 if(prof == null)
219 return null;
220 return prof.id;
223 * get password
225 public
226 unowned string?
227 get_password(string id)
229 Profile prof = this.list.lookup(id);
230 if(prof == null)
231 return null;
232 return prof.password;
236 * get music directory
238 public
239 unowned string?
240 get_music_directory(string id)
242 Profile prof = this.list.lookup(id);
243 if(prof == null)
244 return null;
245 return prof.music_directory;
248 * get music directory
250 public
252 get_db_update_time(string id)
254 Profile prof = this.list.lookup(id);
255 if(prof == null)
256 return 0;
257 return prof.db_update_time;
260 * get port
262 public
264 get_port(string id)
266 Profile prof = this.list.lookup(id);
267 if(prof == null)
268 return -1;
269 return prof.port;
273 * get do_auth
275 public
276 bool
277 get_do_auth(string id)
279 Profile prof = this.list.lookup(id);
280 if(prof == null)
281 return false;
282 return prof.do_auth;
286 * Create new item
288 public
289 unowned string
290 create_new_item(string id)
292 return this.create_new_item_with_name(id, "New Profile");
295 public
296 unowned string
297 create_new_item_with_name(string? id, string? name)
299 var prof = new Profile();
300 if(id == null)
301 prof.id = GLib.Random.next_int().to_string();
302 else
303 prof.id = id;
304 if(name == null)
305 prof.name = "New Profile";
306 else
307 prof.name = name;
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);
321 /* propagate */
322 this.changed_internal(Action.ADDED, Column.NUM_COLS, prof.id);
323 return prof.id;
326 public
327 void
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);
334 return;
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);
348 * GET CURRENT
350 public
351 string
352 get_current()
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 */
357 if(prof == null)
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);
364 } else {
365 this.add_default();
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");
371 return id;
374 public signal void profile_changed(string id);
375 public void
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);
387 public
388 GLib.List<weak string>
389 get_profiles_ids()
391 return this.list.get_keys();
395 * set field
397 public
398 void
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;
405 prof.name = @value;
407 this.profiles.set_string(id, "name", prof.name);
408 this.changed_internal(Action.COL_CHANGED, Column.NAME, id);
411 public
412 void
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);
426 * Set Password
428 public
429 void
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);
443 * Set Port
445 public
446 void
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;
453 prof.port = @value;
455 this.profiles.set_int(id, "portnumber",prof.port);
456 this.changed_internal(Action.COL_CHANGED, Column.PORT, id);
460 * Set music directory
462 public
463 void
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);
477 public
478 void
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);
492 * Set do auth
494 public
495 void
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
511 public
512 bool
513 has_profile(string id)
515 return ( this.list.lookup(id) != null );
518 public
519 void
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);