Mercurial > audlegacy
comparison src/audacious/ui_lastfm.c @ 3183:bcf611cf5ae2 trunk
lastfm radio GUI
author | Cristi Magherusan <majeru@atheme-project.org> |
---|---|
date | Sat, 28 Jul 2007 01:27:59 +0300 |
parents | 49ce30748980 |
children | 1fef0ac7395e |
comparison
equal
deleted
inserted
replaced
3180:384cb84c968f | 3183:bcf611cf5ae2 |
---|---|
1 #include <glib.h> | 1 #include <gtk/gtk.h> |
2 #include <glib/gi18n.h> | |
3 #include <gdk/gdkkeysyms.h> | |
4 #include "configdb.h" | |
5 #include "playback.h" | |
6 | |
7 #define DEBUG 1 | |
8 typedef struct { | |
9 gchar *username, | |
10 *session; | |
11 }last_fm; | |
12 | |
13 typedef struct | |
14 { | |
15 gchar *artist, | |
16 *title; | |
17 } | |
18 track_data; | |
19 | |
20 last_fm *login_data=NULL; | |
21 Playlist *current_playlist=NULL; | |
22 TitleInput *info = NULL; | |
23 GtkWidget *lastfm_url_entry, *lastfm_label,*artist_label,*title_label,*album_label, *gui_window=NULL; | |
24 | |
25 gpointer open_url(gpointer url) | |
26 { | |
27 gchar*s=g_new0(gchar,20); | |
28 VFSFile* fd = vfs_fopen((gchar*)url,"r"); | |
29 if(fd) | |
30 { | |
31 vfs_fgets(s,20,fd); | |
32 g_print("Got data: '%s'\n",s); | |
33 vfs_fclose(fd); | |
34 } | |
35 g_free(s); | |
36 g_free(url); | |
37 return NULL; | |
38 } | |
39 void command(gchar *comm) | |
40 { | |
41 /* get the session from mowgli*/ | |
42 if(login_data->session) | |
43 { | |
44 g_free(login_data->session); | |
45 login_data->session=NULL; | |
46 } | |
47 login_data->session = g_strdup(mowgli_global_storage_get("lastfm_session_id")); | |
48 gchar *url=g_strdup_printf("http://ws.audioscrobbler.com/radio/control.php?session=%s&command=%s&debug=0",login_data->session,comm); | |
49 g_thread_create(open_url,url,FALSE,NULL); | |
50 if(!g_str_has_prefix(comm,"love")) | |
51 g_thread_create((gpointer)playback_initiate,NULL,FALSE,NULL); | |
52 return; | |
53 | |
54 } | |
55 static void change_track_data_cb(gpointer track, gpointer unused) | |
56 { | |
57 gchar **inf, | |
58 **t, | |
59 *alb, | |
60 *tr; | |
61 tr=((track_data*)track)->title; | |
62 | |
63 if(tr==NULL) | |
64 return ; | |
65 #if DEBUG | |
66 g_print("New Track: %s \n",tr); | |
67 #endif | |
68 if(g_str_has_prefix(tr,"lastfm://")) | |
69 return; | |
70 | |
71 if(g_strrstr(tr,"Neighbour")!=NULL) | |
72 { | |
73 gchar *temp=g_strdup_printf("lastfm://user/%s/neighbours", login_data->username); | |
74 gtk_entry_set_text(GTK_ENTRY(lastfm_url_entry),temp); | |
75 g_free(temp); | |
76 } | |
77 if(g_strrstr(tr,"Personal")!=NULL) | |
78 { | |
79 gchar *temp=g_strdup_printf("lastfm://user/%s/personal", login_data->username); | |
80 gtk_entry_set_text(GTK_ENTRY(lastfm_url_entry),temp); | |
81 g_free(temp); | |
82 } | |
83 | |
84 inf = g_strsplit(tr," - ",2); | |
85 if(inf[0]==NULL || inf[1]==NULL) | |
86 return; | |
87 gchar* artist_markup=g_strdup_printf("<b>Artist:</b> %s" ,inf[0]); | |
88 gtk_label_set_markup(GTK_LABEL(artist_label),artist_markup); | |
89 g_free(artist_markup); | |
90 | |
91 t = g_strsplit(inf[1], g_strrstr(inf[1],"("),2); | |
92 alb = g_strdup(mowgli_global_storage_get("lastfm_album")); | |
93 | |
94 gchar* title_markup=g_strdup_printf("<b>Title:</b> %s" ,t[0]); | |
95 gtk_label_set_markup( GTK_LABEL(title_label),title_markup); | |
96 g_free(title_markup); | |
97 | |
98 gchar* album_markup=g_strdup_printf("<b>Album:</b> %s" ,alb); | |
99 gtk_label_set_markup( GTK_LABEL(album_label),album_markup); | |
100 g_free(album_markup); | |
101 | |
102 g_strfreev(inf); | |
103 g_strfreev(t); | |
104 g_free(alb); | |
105 g_free(tr); | |
106 | |
107 return ; | |
108 } | |
109 | |
110 static void show_login_error_dialog(void) | |
111 { | |
112 const gchar *markup = | |
113 N_("<b><big>Couldn't find your lastfm login data.</big></b>\n\n" | |
114 "Check if your Scrobbler's plugin login settings are configured properly.\n"); | |
115 | |
116 GtkWidget *dialog = | |
117 gtk_message_dialog_new_with_markup(NULL, | |
118 GTK_DIALOG_DESTROY_WITH_PARENT, | |
119 GTK_MESSAGE_ERROR, | |
120 GTK_BUTTONS_OK, | |
121 _(markup)); | |
122 gtk_dialog_run(GTK_DIALOG(dialog)); | |
123 gtk_widget_destroy(dialog); | |
124 } | |
125 | |
126 | |
127 | |
128 void init_last_fm(void) | |
129 { | |
130 login_data = g_new0(last_fm,1); | |
131 login_data->username = NULL; | |
132 login_data->session = NULL; | |
133 ConfigDb *cfg = NULL; | |
134 | |
135 if ((cfg = bmp_cfg_db_open()) != NULL) | |
136 { | |
137 login_data->username=NULL; | |
138 bmp_cfg_db_get_string(cfg, "audioscrobbler","username", | |
139 &login_data->username); | |
140 if(login_data->username==NULL) | |
141 show_login_error_dialog(); | |
142 g_free(cfg); | |
143 } | |
144 | |
145 current_playlist = g_new0(Playlist,1); | |
146 current_playlist = playlist_get_active(); | |
147 track_data * tr=g_new0(track_data,1); | |
148 tr->title =NULL; | |
149 hook_associate( "playlist set info" , change_track_data_cb ,tr); | |
150 | |
151 } | |
152 | |
153 /*event callback functions*/ | |
154 | |
155 gboolean love_press_callback(GtkWidget *love) | |
156 { | |
157 command("love"); | |
158 return FALSE; | |
159 } | |
160 | |
161 gboolean skip_press_callback(GtkWidget *skip) | |
162 { | |
163 command("skip"); | |
164 return FALSE; | |
165 } | |
166 gboolean ban_press_callback(GtkWidget *ban) | |
167 { | |
168 command("ban"); | |
169 return FALSE; | |
170 } | |
171 | |
172 | |
173 gboolean add_press_callback(GtkWidget *love) | |
174 { | |
175 gchar *text=NULL; | |
176 gint poz=0; | |
177 text = g_strdup(gtk_entry_get_text(GTK_ENTRY(lastfm_url_entry))); | |
178 poz = playlist_get_length(current_playlist); | |
179 playlist_add_url(current_playlist, text); | |
180 sleep(1); | |
181 playlist_set_position(current_playlist, poz); | |
182 playback_initiate(); | |
183 g_free(text); | |
184 return FALSE; | |
185 } | |
186 | |
187 gboolean neighbours_press_callback(GtkWidget *love) | |
188 { | |
189 gchar *temp=g_strdup_printf("lastfm://user/%s/neighbours", login_data->username); | |
190 gtk_entry_set_text(GTK_ENTRY(lastfm_url_entry),temp); | |
191 g_free(temp); | |
192 return FALSE; | |
193 } | |
194 | |
195 gboolean personal_press_callback(GtkWidget *love) | |
196 { | |
197 gchar *per=g_strdup_printf("lastfm://user/%s/personal", login_data->username); | |
198 gtk_entry_set_text(GTK_ENTRY(lastfm_url_entry),per); | |
199 g_free(per); | |
200 return FALSE; | |
201 } | |
202 | |
203 gboolean delete_window_callback(GtkWidget *window) | |
204 { | |
205 gtk_widget_destroy(window); | |
206 window=NULL; | |
207 return FALSE; | |
208 } | |
209 static gboolean keypress_callback(GtkWidget * widget, GdkEventKey * event, gpointer data) | |
210 { | |
211 switch (event->keyval) | |
212 { | |
213 case GDK_Escape: | |
214 gtk_widget_hide_all(widget); | |
215 break; | |
216 default: | |
217 return FALSE; | |
218 } | |
219 return TRUE; | |
220 } | |
221 | |
222 | |
223 GtkWidget *ui_lastfm_create(void) | |
224 { | |
225 GtkWidget *window; | |
226 GtkWidget *box1,*box2,*vboxw,*vbox1,*vbox2,*v_hbox1,*v_hbox2,*labelbox; | |
227 GtkWidget *love,*ban,*skip,*add,*neighbours,*personal; | |
228 | |
229 window = gtk_window_new (GTK_WINDOW_TOPLEVEL); | |
230 gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER); | |
231 gtk_window_set_title(GTK_WINDOW(window), N_("Audacious last.fm radio tuner")); | |
232 lastfm_label = gtk_label_new_with_mnemonic(N_("Station:")); | |
233 title_label = gtk_label_new(NULL); | |
234 artist_label = gtk_label_new(NULL); | |
235 album_label = gtk_label_new(NULL); | |
236 lastfm_url_entry = gtk_entry_new(); | |
237 gtk_editable_set_editable (GTK_EDITABLE(lastfm_url_entry),TRUE); | |
238 gchar* artist_markup=g_strdup("<b>Artist:</b>"); | |
239 gtk_label_set_markup(GTK_LABEL(artist_label),artist_markup); | |
240 g_free(artist_markup); | |
241 | |
242 gchar* title_markup=g_strdup("<b>Title:</b>"); | |
243 gtk_label_set_markup( GTK_LABEL(title_label),title_markup); | |
244 g_free(title_markup); | |
245 | |
246 gchar* album_markup=g_strdup_printf("<b>Album:</b>"); | |
247 gtk_label_set_markup( GTK_LABEL(album_label),album_markup); | |
248 g_free(album_markup); | |
249 | |
250 love = gtk_button_new_with_label (N_("Love")); | |
251 ban = gtk_button_new_with_label (N_("Ban")); | |
252 skip = gtk_button_new_with_label (N_("Skip")); | |
253 add = gtk_button_new_with_label (N_("Tune in")); | |
254 | |
255 neighbours = gtk_button_new_with_label(N_("Neighbours' radio")); | |
256 personal = gtk_button_new_with_label(N_("Personal radio")); | |
257 | |
258 box1 = gtk_hbox_new(FALSE,1); | |
259 box2 = gtk_hbox_new(FALSE,1); | |
260 | |
261 vboxw = gtk_vbox_new(FALSE,1); | |
262 | |
263 vbox1 = gtk_vbox_new(FALSE,1); | |
264 vbox2 = gtk_vbox_new(FALSE,1); | |
265 | |
266 v_hbox1 = gtk_vbox_new(FALSE,1); | |
267 v_hbox2 = gtk_hbox_new(FALSE,1); | |
268 labelbox = gtk_vbox_new(FALSE,1); | |
269 | |
270 gtk_misc_set_alignment(GTK_MISC(artist_label),0,0); | |
271 gtk_misc_set_alignment(GTK_MISC(title_label),0,0); | |
272 gtk_misc_set_alignment(GTK_MISC(album_label),0,0); | |
273 | |
274 gtk_box_pack_start(GTK_BOX(vboxw),box1,FALSE,TRUE,1); | |
275 gtk_box_pack_end(GTK_BOX(vboxw),box2,TRUE,TRUE,1); | |
276 | |
277 gtk_box_pack_start(GTK_BOX(box1),lastfm_label,FALSE,TRUE,1); | |
278 gtk_box_pack_start(GTK_BOX(box1),lastfm_url_entry,TRUE,TRUE,1); | |
279 gtk_box_pack_start(GTK_BOX(box1),add,FALSE,TRUE,1); | |
280 | |
281 gtk_box_set_spacing(GTK_BOX(box1),2); | |
282 gtk_container_set_border_width(GTK_CONTAINER(box1),2); | |
283 | |
284 gtk_box_pack_start(GTK_BOX(box2),vbox1,FALSE,TRUE,1); | |
285 gtk_box_pack_start(GTK_BOX(box2),vbox2,TRUE,TRUE,1); | |
286 | |
287 gtk_box_pack_start(GTK_BOX(vbox1),neighbours,FALSE,TRUE,3); | |
288 gtk_box_pack_start(GTK_BOX(vbox1),personal,FALSE,TRUE,3); | |
289 gtk_box_set_spacing(GTK_BOX(vbox1),2); | |
290 gtk_container_set_border_width(GTK_CONTAINER(vbox1),2); | |
291 | |
292 gtk_box_pack_start(GTK_BOX(vbox2),v_hbox1,TRUE,TRUE,1); | |
293 gtk_box_pack_start(GTK_BOX(vbox2),v_hbox2,TRUE,TRUE,1); | |
294 | |
295 gtk_box_pack_start(GTK_BOX(v_hbox1),artist_label,TRUE,TRUE,1); | |
296 gtk_box_pack_start(GTK_BOX(v_hbox1),title_label,TRUE,TRUE,1); | |
297 gtk_box_pack_start(GTK_BOX(v_hbox1),album_label,TRUE,TRUE,1); | |
298 gtk_box_set_spacing(GTK_BOX(v_hbox1),2); | |
299 gtk_container_set_border_width(GTK_CONTAINER(v_hbox1),2); | |
300 | |
301 | |
302 gtk_box_pack_start(GTK_BOX(v_hbox2),love,TRUE,TRUE,1); | |
303 gtk_box_pack_start(GTK_BOX(v_hbox2),skip,TRUE,TRUE,1); | |
304 gtk_box_pack_start(GTK_BOX(v_hbox2),ban,TRUE,TRUE,1); | |
305 gtk_box_set_spacing(GTK_BOX(v_hbox1),2); | |
306 gtk_container_set_border_width(GTK_CONTAINER(v_hbox1),2); | |
307 | |
308 | |
309 | |
310 gtk_container_add (GTK_CONTAINER (window), vboxw); | |
311 | |
312 g_signal_connect (G_OBJECT (love), "button_press_event",G_CALLBACK (love_press_callback), NULL); | |
313 g_signal_connect (G_OBJECT (add), "button_press_event",G_CALLBACK (add_press_callback), NULL); | |
314 g_signal_connect (G_OBJECT (ban), "button_press_event",G_CALLBACK (ban_press_callback), NULL); | |
315 g_signal_connect (G_OBJECT (skip), "button_press_event",G_CALLBACK (skip_press_callback), NULL); | |
316 g_signal_connect (G_OBJECT (neighbours), "button_press_event",G_CALLBACK (neighbours_press_callback), NULL); | |
317 g_signal_connect (G_OBJECT (personal), "button_press_event",G_CALLBACK (personal_press_callback), NULL); | |
318 g_signal_connect (G_OBJECT (window), "delete_event",G_CALLBACK (gtk_widget_hide_all), window); | |
319 g_signal_connect (G_OBJECT (window), "key_press_event",G_CALLBACK(keypress_callback), NULL); | |
320 | |
321 gtk_widget_set_size_request(GTK_WIDGET(window),400,124); | |
322 gtk_window_set_resizable(GTK_WINDOW(window),FALSE); | |
323 gtk_widget_show_all(gui_window); | |
324 return window; | |
325 } | |
326 | |
327 static void no_lastfm_plugin_dialog(void) | |
328 { | |
329 const gchar *markup = | |
330 N_("<b><big>The lastfm radio plugin could not be found.</big></b>\n\n" | |
331 "Check if the AudioScrobbler plugin was compiled in\n"); | |
332 | |
333 GtkWidget *dialog = | |
334 gtk_message_dialog_new_with_markup(NULL, | |
335 GTK_DIALOG_DESTROY_WITH_PARENT, | |
336 GTK_MESSAGE_ERROR, | |
337 GTK_BUTTONS_OK, | |
338 _(markup)); | |
339 gtk_dialog_run(GTK_DIALOG(dialog)); | |
340 gtk_widget_destroy(dialog); | |
341 } | |
342 | |
343 | |
344 | |
2 void ui_lastfm(void) | 345 void ui_lastfm(void) |
3 { | 346 { |
4 g_print("lastfm gui stub\n"); | 347 /*if(!mowgli_global_storage_get("lastfm_loaded")) |
5 } | 348 { |
349 no_lastfm_plugin_dialog(); | |
350 return; | |
351 } | |
352 */ | |
353 init_last_fm(); | |
354 if(!gui_window) | |
355 { | |
356 if((gui_window = ui_lastfm_create())!=NULL); | |
357 gtk_widget_show_all(gui_window); | |
358 #if 0 /*here should be set the artist, title and album labels at the first run, because it isn't captured by the hook*/ | |
359 gchar* current_title = playlist_get_songtitle(current_playlist,playlist_get_position(current_playlist)); | |
360 if(current_title) | |
361 change_track_data_cb(current_title,NULL); | |
362 #endif | |
363 } | |
364 else | |
365 gtk_widget_show_all(gui_window); | |
366 | |
367 } | |
368 |