Merge branch 'master', remote-tracking branch 'home/master'
[gmpc.git] / src / vala / gmpc-url-fetching-gui.vala
blob167ca17e123c02ce7ad24ec8adf07b61221e1919
1 /* Gnome Music Player Client (GMPC)
2 * Copyright (C) 2004-2011 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.
20 using GLib;
21 using Gtk;
22 using Gdk;
25 private const bool use_transition_ufg = Gmpc.use_transition;
26 namespace Gmpc.UrlFetching
28 public class Gui : GLib.Object
30 private Gtk.Builder builder = new Gtk.Builder();
31 private ParseUrl parse_callback = null;
32 private ValidateUrl validate_callback = null;
33 private GLib.DestroyNotify destroy_cb;
35 private enum State {
36 NORMAL,
37 PROCESSING,
38 ERROR,
39 DONE
42 /* Called when an url needs to be validated.
43 * returns bool is successfull
45 public delegate bool ValidateUrl(Gui gui, string url);
47 /* Function you needs to parse setting */
48 public delegate void ParseUrl (Gui gui, string? url);
50 private void add_url_dialog_response( int response_id)
52 if(response_id == 1) {
53 unowned Gtk.Entry entry = (Gtk.Entry)this.builder.get_object("url_entry");
54 string url = entry.get_text();
55 this.parse_callback(this, url);
56 return;
58 else
60 this.parse_callback(this, null);
62 this.destroy_cb(this);
64 private void url_entry_changed(Gtk.Editable editable)
66 var add_button = (Gtk.Button) builder.get_object("add_button");
67 var text = ((Gtk.Entry)editable).get_text();
68 if(text != null && this.validate_callback != null && this.validate_callback(this, text)) {
69 add_button.sensitive = true;
70 }else{
71 add_button.sensitive = false;
75 ~Gui ()
77 if(builder != null)
79 var dialog = (Gtk.Dialog) builder.get_object("add_url_dialog");
80 if(dialog != null) {
81 dialog.destroy();
86 public Gui (ParseUrl parse_callback, ValidateUrl validate_callback, GLib.DestroyNotify destroy_cb)
88 this.parse_callback = parse_callback;
89 this.validate_callback = validate_callback;
90 this.destroy_cb = destroy_cb;
92 try {
93 this.builder.add_from_file(Gmpc.data_path("gmpc-add-url.ui"));
94 }catch (GLib.Error e)
96 GLib.error("Failed to load GtkBuilder file: %s", e.message);
98 var dialog = (Gtk.Dialog) builder.get_object("add_url_dialog");
99 dialog.set_transient_for(Gmpc.Playlist.get_window());
100 dialog.show();
102 var entry = (Gtk.Entry) builder.get_object("url_entry");
103 /* Connect by hand as connect_signals fails utterly */
104 dialog.response.connect(add_url_dialog_response);
105 entry.changed.connect(url_entry_changed);
109 private State state_counter = State.NORMAL;
112 private void sensitive(bool state)
114 if(this.builder == null) return;
115 var entry = (Gtk.Entry) builder.get_object("url_entry");
116 entry.sensitive = state;
118 var add_button = (Gtk.Button) builder.get_object("add_button");
119 add_button.sensitive = state;
121 var close_button = (Gtk.Button) builder.get_object("close_button");
122 close_button.sensitive = state;
124 var progress = (Gtk.ProgressBar) builder.get_object("url_progress");
125 if(!state){
126 progress.show();
128 else
129 progress.hide();
131 /* Tell the dialog that we started processing stuff.
132 * This should make the window insensitive */
133 public void set_processing()
135 this.state_counter = State.PROCESSING;
137 sensitive(false);
140 /* Set progress
141 * This can only be set after set_processing
142 * double -1 is a pulse.
144 public void set_progress(double progress)
146 GLib.log("GUFG", GLib.LogLevelFlags.LEVEL_DEBUG, "Set progress: %f", progress);
147 if(this.state_counter != State.PROCESSING) return;
150 var progressw = (Gtk.ProgressBar) builder.get_object("url_progress");
151 if(progress < 0) progressw.pulse();
152 else progressw.set_fraction(progress);
156 /* Tell the dialog dialog we successfully parsed the pls. */
157 public void set_completed()
159 GLib.log("GUFG", GLib.LogLevelFlags.LEVEL_DEBUG, "Completed");
160 this.state_counter = State.DONE;
162 sensitive(true);
163 /* for now, destroy the window */
164 this.destroy_cb(this);
166 /* Set error, this also undo's set_processing*/
167 public void set_error(string error_message)
169 GLib.log("GUFG", GLib.LogLevelFlags.LEVEL_DEBUG, "Error: %s", error_message);
170 this.state_counter = State.ERROR;
172 sensitive(true);
177 /* vim: set noexpandtab ts=4 sw=4 sts=4 tw=120: */