Tighten top bar metadata browser a bit
[gmpc.git] / src / tray-icon2.c
blobf9ef5d6f37466e0b1b16133fe68f27435d908175
1 #include <gtk/gtk.h>
2 #include <glade/glade.h>
3 #include <libmpd/libmpd.h>
4 #include "main.h"
5 #include "plugin.h"
6 #include "gmpc-clicklabel.h"
7 #include "misc.h"
10 /* name of config field */
11 #define TRAY_ICON2_ID "tray-icon2"
13 GtkStatusIcon *tray_icon2_gsi = NULL;
14 /**
15 * Tooltip
17 GtkWidget *tray_icon2_tooltip = NULL;
18 GtkWidget *tray_icon2_tooltip_pb = NULL;
19 guint tray_icon2_tooltip_timeout = 0;
20 enum{
21 TI2_AT_TOOLTIP,
22 TI2_AT_UPPER_LEFT,
23 TI2_AT_UPPER_RIGHT,
24 TI2_AT_LOWER_LEFT,
25 TI2_AT_LOWER_RIGHT,
26 TI2_AT_NUM_OPTIONS
28 /**
29 * Preferences
31 static GladeXML *tray_icon2_preferences_xml = NULL;
32 void popup_timeout_changed(void);
33 void popup_position_changed(GtkComboBox *om);
34 void popup_enable_toggled(GtkToggleButton *but);
35 void tray_enable_toggled(GtkToggleButton *but);
36 void tray_icon2_preferences_pm_combo_changed(GtkComboBox *cm, gpointer data);
37 /**
38 * Tray icon
41 gboolean tray_icon2_get_available(void)
43 if(tray_icon2_gsi) {
44 if(gtk_status_icon_is_embedded(tray_icon2_gsi) &&
45 gtk_status_icon_get_visible(tray_icon2_gsi)) {
46 return TRUE;
49 return FALSE;
51 /**
52 * click on the tray icon
54 static void tray_icon2_activate(GtkStatusIcon *gsi, gpointer user_data)
56 pl3_toggle_hidden();
58 /**
59 * Right mouse press on tray icon
61 static void tray_icon2_populate_menu(GtkStatusIcon *gsi,guint button, guint activate_time, gpointer user_data)
63 GtkWidget *item;
64 GtkWidget *menu = gtk_menu_new();
66 if(mpd_check_connected(connection))
68 if(mpd_server_check_command_allowed(connection, "play"))
70 if(mpd_player_get_state(connection) == MPD_STATUS_STATE_PLAY)
72 item = gtk_image_menu_item_new_from_stock(GTK_STOCK_MEDIA_PAUSE, NULL);
73 }else{
74 item = gtk_image_menu_item_new_from_stock(GTK_STOCK_MEDIA_PLAY, NULL);
76 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
77 g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(play_song), NULL);
80 item = gtk_image_menu_item_new_from_stock(GTK_STOCK_MEDIA_STOP, NULL);
81 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
82 g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(stop_song), NULL);
84 item = gtk_image_menu_item_new_from_stock(GTK_STOCK_MEDIA_NEXT, NULL);
85 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
86 g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(next_song), NULL);
89 item = gtk_image_menu_item_new_from_stock(GTK_STOCK_MEDIA_PREVIOUS, NULL);
90 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
91 g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(prev_song), NULL);
92 item = gtk_separator_menu_item_new();
93 gtk_menu_shell_append(GTK_MENU_SHELL(menu),item);
96 else
98 item = gtk_image_menu_item_new_from_stock(GTK_STOCK_CONNECT, NULL);
99 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
100 g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(connect_to_mpd), NULL);
101 item = gtk_separator_menu_item_new();
102 gtk_menu_shell_append(GTK_MENU_SHELL(menu),item);
104 item = gtk_image_menu_item_new_with_mnemonic(_("Pla_ylist"));
105 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item),
106 gtk_image_new_from_stock("gtk-justify-fill", GTK_ICON_SIZE_MENU));
107 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
108 g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(create_playlist3), NULL);
111 item = gtk_separator_menu_item_new();
112 gtk_menu_shell_append(GTK_MENU_SHELL(menu),item);
114 item = gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT,NULL);
115 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
116 g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(main_quit), NULL);
117 gtk_widget_show_all(menu);
118 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, button, activate_time);
121 static void tray_icon2_embedded_changed(GtkStatusIcon *icon,GParamSpec *arg1, gpointer data)
123 if(gtk_status_icon_is_embedded(icon))
125 /* the status icon just got embedded */
127 else
129 /* the widget isn't embedded anymore */
130 create_playlist3();
134 * Initialize the tray icon
136 static void tray_icon2_init(void)
139 if(cfg_get_single_value_as_int_with_default(config, TRAY_ICON2_ID, "enable", TRUE))
141 tray_icon2_gsi = gtk_status_icon_new_from_icon_name ("gmpc-tray-disconnected");
142 /* connect the (sparse) signals */
143 g_signal_connect(G_OBJECT(tray_icon2_gsi), "popup-menu", G_CALLBACK(tray_icon2_populate_menu), NULL);
144 g_signal_connect(G_OBJECT(tray_icon2_gsi), "activate", G_CALLBACK(tray_icon2_activate), NULL);
145 g_signal_connect(G_OBJECT(tray_icon2_gsi), "notify::embedded", G_CALLBACK(tray_icon2_embedded_changed), NULL);
149 * Destroy and cleanup
151 static void tray_icon2_destroy(void)
153 if(tray_icon2_gsi)
155 g_object_unref(tray_icon2_gsi);
159 * Get enabled
161 static gboolean tray_icon2_get_enabled(void)
163 return cfg_get_single_value_as_int_with_default(config, TRAY_ICON2_ID, "enable", TRUE);
166 * Set Disabled
168 static void tray_icon2_set_enabled(int enabled)
170 cfg_set_single_value_as_int(config, TRAY_ICON2_ID, "enable", enabled);
171 if(enabled) {
172 if(!tray_icon2_gsi) {
173 tray_icon2_init();
174 } else {
175 gtk_status_icon_set_visible(GTK_STATUS_ICON(tray_icon2_gsi), TRUE);
177 } else {
178 if(tray_icon2_gsi) {
179 gtk_status_icon_set_visible(GTK_STATUS_ICON(tray_icon2_gsi), FALSE);
184 * TOOLTIP
187 static gboolean has_buttons = FALSE;
188 static GtkWidget *play_button = NULL;
190 static gboolean tray_icon2_tooltip_destroy(void)
192 tray_icon2_tooltip_pb = NULL;
193 gtk_widget_destroy(tray_icon2_tooltip);
194 tray_icon2_tooltip = NULL;
195 /* remove timeout, this is for when it doesn't get called from inside the timeout */
196 if(tray_icon2_tooltip_timeout)
198 g_source_remove(tray_icon2_tooltip_timeout);
200 tray_icon2_tooltip_timeout = 0;
201 has_buttons = FALSE;
202 /* remove the timeout */
203 return FALSE;
206 static gboolean tray_icon2_tooltip_button_press_event(GtkWidget *box, GdkEventButton *event, GtkWidget *vbox)
208 if(event->button == 3 && !has_buttons)
210 GtkWidget *hbox,*button;
211 int state = cfg_get_single_value_as_int_with_default(config, TRAY_ICON2_ID, "tooltip-timeout", 5);
212 if(tray_icon2_tooltip_timeout)
214 g_source_remove(tray_icon2_tooltip_timeout);
216 tray_icon2_tooltip_timeout = g_timeout_add_seconds(state*2, (GSourceFunc)tray_icon2_tooltip_destroy, NULL);
218 has_buttons = TRUE;
220 hbox = gtk_hbox_new(TRUE, 6);
221 /* prev */
222 button = gtk_button_new();
223 gtk_button_set_image(GTK_BUTTON(button), gtk_image_new_from_stock(GTK_STOCK_MEDIA_PREVIOUS,GTK_ICON_SIZE_BUTTON));
224 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
225 g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(prev_song), NULL);
226 /* stop */
227 button = gtk_button_new();
228 gtk_button_set_image(GTK_BUTTON(button), gtk_image_new_from_stock(GTK_STOCK_MEDIA_STOP,GTK_ICON_SIZE_BUTTON));
229 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
230 g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(stop_song), NULL);
231 /* pause/play */
232 state = mpd_player_get_state(connection);
233 if(state != MPD_PLAYER_PLAY)
235 play_button = button = gtk_button_new();
236 gtk_button_set_image(GTK_BUTTON(button), gtk_image_new_from_stock(GTK_STOCK_MEDIA_PLAY,GTK_ICON_SIZE_BUTTON));
237 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
238 g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(play_song), NULL);
240 else
242 play_button = button = gtk_button_new();
243 gtk_button_set_image(GTK_BUTTON(button), gtk_image_new_from_stock(GTK_STOCK_MEDIA_PAUSE,GTK_ICON_SIZE_BUTTON));
245 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
246 g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(play_song), NULL);
248 /* next */
249 button = gtk_button_new();
250 gtk_button_set_image(GTK_BUTTON(button), gtk_image_new_from_stock(GTK_STOCK_MEDIA_NEXT,GTK_ICON_SIZE_BUTTON));
252 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
253 g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(next_song), NULL);
255 gtk_widget_show_all(hbox);
256 gtk_box_pack_end(GTK_BOX(vbox), hbox, FALSE, TRUE,0);
258 return TRUE;
260 tray_icon2_tooltip_destroy();
261 return TRUE;
265 void tray_icon2_create_tooltip(void)
267 int x=0,y=0,monitor;
268 GdkScreen *screen;
269 GtkWidget *pl3_win = glade_xml_get_widget(pl3_xml, "pl3_win");
270 GtkWidget *hbox = NULL;
271 GtkWidget *vbox = NULL;
272 GtkWidget *label = NULL;
273 GtkWidget *event = NULL;
274 GtkWidget *coverimg = NULL;
275 mpd_Song *song = NULL;
276 int state;
277 int x_offset = cfg_get_single_value_as_int_with_default(config, TRAY_ICON2_ID, "x-offset", 0);
278 int y_offset = cfg_get_single_value_as_int_with_default(config, TRAY_ICON2_ID, "y-offset", 0);
282 * if the tooltip still exists destroy it...
284 if(tray_icon2_tooltip)
286 tray_icon2_tooltip_destroy();
288 if(cfg_get_single_value_as_int_with_default(config, TRAY_ICON2_ID, "show-tooltip", 1) == 0)
289 return;
290 /* If gmpc is fullscreen, don't show the tooltip */
291 if(pl3_window_is_fullscreen())
292 return;
294 * Creat the tootlip window
296 tray_icon2_tooltip = gtk_window_new(GTK_WINDOW_POPUP);
298 /* causes the border */
299 gtk_widget_modify_bg(GTK_WIDGET(tray_icon2_tooltip), GTK_STATE_NORMAL, &(pl3_win->style->black));
300 gtk_container_set_border_width(GTK_CONTAINER(tray_icon2_tooltip),1);
301 gtk_window_set_default_size(GTK_WINDOW(tray_icon2_tooltip), 300,-1);
302 gtk_window_set_transient_for(GTK_WINDOW(tray_icon2_tooltip), GTK_WINDOW(pl3_win));
305 * Tooltip exists from 2 parts..
306 * ------------------
307 * | 1 | |
308 * | | 2 |
309 * ------------------
311 hbox = gtk_hbox_new(FALSE,0);
312 gtk_container_add(GTK_CONTAINER(tray_icon2_tooltip), hbox);
316 * In first box we have image/coverart
317 * Re-use the gmpc-metaimage widget
320 coverimg = gmpc_metaimage_new(META_ALBUM_ART);
322 gmpc_metaimage_set_connection(GMPC_METAIMAGE(coverimg), connection);
323 gmpc_metaimage_set_size(GMPC_METAIMAGE(coverimg), 80);
324 gmpc_metaimage_set_no_cover_icon(GMPC_METAIMAGE(coverimg),"gmpc");
326 * Force an update if mpd is playing
328 state = mpd_player_get_state(connection);
329 if(state == MPD_PLAYER_PLAY || state == MPD_PLAYER_PAUSE) {
330 gmpc_metaimage_update_cover(GMPC_METAIMAGE(coverimg), connection, MPD_CST_SONGID,NULL);
331 } else {
332 gmpc_metaimage_set_cover_na(GMPC_METAIMAGE(coverimg));
335 * Pack the widget in a eventbox so we can set background color
337 event = gtk_event_box_new();
338 gtk_widget_set_size_request(event, 86,86);
339 gtk_widget_modify_bg(GTK_WIDGET(event), GTK_STATE_NORMAL, &(pl3_win->style->bg[GTK_STATE_SELECTED]));
340 gtk_container_add(GTK_CONTAINER(event), coverimg);
341 gtk_box_pack_start(GTK_BOX(hbox), event, FALSE,TRUE,0);
345 * Right (2) view
348 * Create white background label box
350 event = gtk_event_box_new();
351 vbox = gtk_vbox_new(FALSE, 0);
352 g_signal_connect(G_OBJECT(hbox), "button-press-event", G_CALLBACK(tray_icon2_tooltip_button_press_event), vbox);
354 gtk_widget_modify_bg(GTK_WIDGET(event), GTK_STATE_NORMAL, &(pl3_win->style->light[GTK_STATE_NORMAL]));
355 gtk_container_set_border_width(GTK_CONTAINER(vbox),3);
356 gtk_container_add(GTK_CONTAINER(event), vbox);
357 gtk_box_pack_start(GTK_BOX(hbox), event, TRUE,TRUE,0);
359 * If there is a song, show show song info
361 song = mpd_playlist_get_current_song(connection);
362 if(song)
364 int size_offset = cfg_get_single_value_as_int_with_default(config, TRAY_ICON2_ID, "size-offset", 1024);
365 size_offset = (size_offset < 100)?1024:size_offset;
367 /** Artist label */
368 if(song->title || song->file || song->name)
370 char buffer[256];
371 gchar *test = g_strdup_printf("<span size='%i' weight='bold'>[%%title%%|%%shortfile%%][ (%%name%%)]</span>", 14*size_offset);
372 mpd_song_markup_escaped(buffer, 256,test,song);
373 q_free(test);
374 label = gtk_label_new("");
375 gtk_label_set_ellipsize(GTK_LABEL(label), PANGO_ELLIPSIZE_END);
376 gtk_misc_set_alignment(GTK_MISC(label), 0,0.5);
377 gtk_label_set_markup(GTK_LABEL(label), buffer);
378 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE,FALSE,0);
380 if(song->artist)
382 char buffer[256];
383 gchar *test = g_strdup_printf("<span size='%i'>%%artist%%</span>", 10*size_offset);
384 label = gtk_label_new("");
385 gtk_label_set_ellipsize(GTK_LABEL(label), PANGO_ELLIPSIZE_END);
386 mpd_song_markup_escaped(buffer, 256,test,song);
387 q_free(test);
388 gtk_label_set_markup(GTK_LABEL(label), buffer);
389 gtk_misc_set_alignment(GTK_MISC(label), 0,0.5);
391 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE,FALSE,0);
393 if(song->album)
395 char buffer[256];
396 gchar *test = g_strdup_printf("<span size='%i'>%%album%%[ (%%year%%)]</span>", 8*size_offset);
397 label = gtk_label_new("");
398 gtk_label_set_ellipsize(GTK_LABEL(label), PANGO_ELLIPSIZE_END);
399 mpd_song_markup_escaped(buffer, 256,test,song);
400 q_free(test);
401 gtk_misc_set_alignment(GTK_MISC(label), 0,0.5);
402 gtk_label_set_markup(GTK_LABEL(label), buffer);
403 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE,FALSE,0);
405 tray_icon2_tooltip_pb = gtk_progress_bar_new();
406 /* Update the progressbar */
408 gchar *str;
409 int totalTime = mpd_status_get_total_song_time(connection);
410 int elapsedTime = mpd_status_get_elapsed_song_time(connection);
411 gdouble progress = elapsedTime/(gdouble)MAX(totalTime,1);
412 str = g_strdup_printf("%02i:%02i/%02i:%02i", elapsedTime/60, elapsedTime%60,
413 totalTime/60,totalTime%60);
414 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(tray_icon2_tooltip_pb), RANGE(0,1,progress));
415 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(tray_icon2_tooltip_pb), str);
416 q_free(str);
420 gtk_box_pack_start(GTK_BOX(vbox), tray_icon2_tooltip_pb, TRUE,FALSE,0);
421 } else {
422 label = gtk_label_new("");
423 gtk_label_set_markup(GTK_LABEL(label), _("<span size='large'>Gnome Music Player Client</span>"));
424 gtk_label_set_ellipsize(GTK_LABEL(label), PANGO_ELLIPSIZE_END);
425 gtk_box_pack_start(GTK_BOX(vbox), label, TRUE,TRUE,0);
429 * Position the popup
431 state = cfg_get_single_value_as_int_with_default(config,TRAY_ICON2_ID, "tooltip-position", TI2_AT_TOOLTIP);
432 if(state == TI2_AT_TOOLTIP && tray_icon2_get_available())
435 GdkRectangle rect, rect2;
436 GtkOrientation orientation;
437 if(gtk_status_icon_get_geometry(tray_icon2_gsi, &screen, &rect, &orientation))
439 monitor = gdk_screen_get_monitor_at_point(screen, rect.x, rect.y);
440 gdk_screen_get_monitor_geometry(screen, monitor, &rect2);
441 /* Get Y */
442 y= rect.y+rect.height+5-rect2.y+y_offset;
443 /* if the lower part falls off the screen, move it up */
444 if((y+95) > rect2.height) {
445 y = rect.y - 95 - 5;
447 if(y < 0) y =0;
449 /* Get X */
450 x = rect.x - 300/2-rect2.x+x_offset;
451 if((x+300) > rect2.width){
452 if(orientation == GTK_ORIENTATION_VERTICAL) {
453 x = rect2.width+-300-rect.width-5;
454 } else {
455 x = rect2.width - 300;
458 if(x<0) {
459 if(orientation == GTK_ORIENTATION_VERTICAL) {
460 x = rect.width+5;
461 } else {
462 x = 0;
466 gtk_window_move(GTK_WINDOW(tray_icon2_tooltip), rect2.x+x,rect2.y+y);
467 } else
468 if (state == TI2_AT_UPPER_LEFT)
470 GdkRectangle rect2;
471 screen =gtk_widget_get_screen(pl3_win);
473 monitor = gdk_screen_get_monitor_at_window(screen, pl3_win->window);
474 gdk_screen_get_monitor_geometry(screen, monitor, &rect2);
475 gtk_window_move(GTK_WINDOW(tray_icon2_tooltip), rect2.x+5+x_offset,rect2.y+5+y_offset);
476 } else if (state == TI2_AT_UPPER_RIGHT) {
477 GdkRectangle rect2;
478 screen =gtk_widget_get_screen(pl3_win);
480 monitor = gdk_screen_get_monitor_at_window(screen, pl3_win->window);
481 gdk_screen_get_monitor_geometry(screen, monitor, &rect2);
482 /** Set Y = 0; */
483 y = rect2.y+5;
484 /** X is upper right - width */
485 x = rect2.x+rect2.width-5-300;
486 gtk_window_move(GTK_WINDOW(tray_icon2_tooltip), x+x_offset,y+y_offset);
487 } else if (state == TI2_AT_LOWER_LEFT)
489 GdkRectangle rect2;
490 screen =gtk_widget_get_screen(pl3_win);
492 monitor = gdk_screen_get_monitor_at_window(screen, pl3_win->window);
493 gdk_screen_get_monitor_geometry(screen, monitor, &rect2);
494 /** Set Y = window height - size; */
495 y = rect2.y+rect2.height-5-95;
496 /** X =5 */
497 x = rect2.x+ 5;
498 gtk_window_move(GTK_WINDOW(tray_icon2_tooltip), x+x_offset,y+y_offset);
499 } else
501 GdkRectangle rect2;
502 screen =gtk_widget_get_screen(pl3_win);
504 monitor = gdk_screen_get_monitor_at_window(screen, pl3_win->window);
505 gdk_screen_get_monitor_geometry(screen, monitor, &rect2);
506 /** Set Y = window height - size; */
507 y = rect2.y+rect2.height-5-95;
508 /** X =window width - width */
509 x = rect2.x+rect2.width-5-300;
510 gtk_window_move(GTK_WINDOW(tray_icon2_tooltip), x+x_offset,y+y_offset);
513 gtk_widget_show_all(tray_icon2_tooltip);
517 * Destroy it after 5 seconds
519 state = cfg_get_single_value_as_int_with_default(config, TRAY_ICON2_ID, "tooltip-timeout", 5);
520 tray_icon2_tooltip_timeout = g_timeout_add_seconds(state, (GSourceFunc)tray_icon2_tooltip_destroy, NULL);
523 static void tray_icon2_status_changed(MpdObj *mi, ChangedStatusType what, void *userdata)
525 char buffer[256];
526 mpd_Song *song = mpd_playlist_get_current_song(connection);
527 if(what&(MPD_CST_SONGID))
529 /**
530 * If enabled by user, show the tooltip.
531 * But only if playing or paused.
534 if(cfg_get_single_value_as_int_with_default(config, TRAY_ICON2_ID, "show-tooltip", TRUE))
536 int state = mpd_player_get_state(connection);
537 if(state == MPD_PLAYER_PLAY || state == MPD_PLAYER_PAUSE)
539 tray_icon2_create_tooltip();
542 if(tray_icon2_gsi)
544 mpd_song_markup(buffer, 256,"[%name%: ][%title%|%shortfile%][ - %artist%]",song);
545 gtk_status_icon_set_tooltip(tray_icon2_gsi,buffer);
549 /* update the progress bar if available */
550 if(what&MPD_CST_ELAPSED_TIME)
552 if(tray_icon2_tooltip && tray_icon2_tooltip_pb)
554 int totalTime = mpd_status_get_total_song_time(connection);
555 int elapsedTime = mpd_status_get_elapsed_song_time(connection);
556 gdouble progress = elapsedTime/(gdouble)MAX(totalTime,1);
557 gchar*label = g_strdup_printf("%02i:%02i/%02i:%02i", elapsedTime/60, elapsedTime%60,
558 totalTime/60,totalTime%60);
559 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(tray_icon2_tooltip_pb), RANGE(0,1,progress));
560 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(tray_icon2_tooltip_pb), label);
561 q_free(label);
564 if(tray_icon2_gsi == NULL)
565 return;
568 if(what&MPD_CST_STATE)
570 int state = mpd_player_get_state(connection);
571 if(state == MPD_PLAYER_PLAY){
572 gtk_status_icon_set_from_icon_name(tray_icon2_gsi, "gmpc-tray-play");
574 mpd_song_markup(buffer, 256,"[%name%: ][%title%|%shortfile%][ - %artist%]",song);
575 gtk_status_icon_set_tooltip(tray_icon2_gsi,buffer);
576 if(has_buttons)
578 gtk_button_set_image(GTK_BUTTON(play_button), gtk_image_new_from_stock(GTK_STOCK_MEDIA_PAUSE, GTK_ICON_SIZE_BUTTON));
580 } else if(state == MPD_PLAYER_PAUSE){
581 gtk_status_icon_set_from_icon_name(tray_icon2_gsi, "gmpc-tray-pause");
582 gtk_status_icon_set_tooltip(tray_icon2_gsi,_("Gnome Music Player Client"));
583 if(has_buttons)
585 gtk_button_set_image(GTK_BUTTON(play_button), gtk_image_new_from_stock(GTK_STOCK_MEDIA_PLAY, GTK_ICON_SIZE_BUTTON));
587 } else {
588 gtk_status_icon_set_from_icon_name(tray_icon2_gsi, "gmpc-tray");
589 gtk_status_icon_set_tooltip(tray_icon2_gsi,_("Gnome Music Player Client"));
590 if(has_buttons)
592 gtk_button_set_image(GTK_BUTTON(play_button), gtk_image_new_from_stock(GTK_STOCK_MEDIA_PLAY, GTK_ICON_SIZE_BUTTON));
598 * Show right icon when (dis)connected
600 static void tray_icon2_connection_changed(MpdObj *mi, int connect,void *user_data)
602 if(tray_icon2_gsi == NULL)
603 return;
605 if(connect) {
606 tray_icon2_status_changed(mi, MPD_CST_STATE,NULL);
607 } else {
608 gtk_status_icon_set_from_icon_name(tray_icon2_gsi, "gmpc-tray-disconnected");
613 * PREFERENCES
617 void tray_enable_toggled(GtkToggleButton *but)
619 debug_printf(DEBUG_INFO,"tray-icon.c: changing tray icon %i\n", gtk_toggle_button_get_active(but));
620 cfg_set_single_value_as_int(config, TRAY_ICON2_ID, "enable", (int)gtk_toggle_button_get_active(but));
621 if(cfg_get_single_value_as_int_with_default(config, TRAY_ICON2_ID, "enable", 1)) {
622 tray_icon2_set_enabled(TRUE);
623 } else {
624 tray_icon2_set_enabled(FALSE);
628 /* this sets all the settings in the notification area preferences correct */
629 static void tray_update_settings(void)
631 gtk_toggle_button_set_active((GtkToggleButton *)
632 glade_xml_get_widget(tray_icon2_preferences_xml, "ck_tray_enable"),
633 cfg_get_single_value_as_int_with_default(config, TRAY_ICON2_ID, "enable", DEFAULT_TRAY_ICON_ENABLE));
636 void popup_enable_toggled(GtkToggleButton *but)
638 cfg_set_single_value_as_int(config, TRAY_ICON2_ID, "show-tooltip", gtk_toggle_button_get_active(but));
642 void popup_position_changed(GtkComboBox *om)
644 cfg_set_single_value_as_int(config, TRAY_ICON2_ID, "tooltip-position", gtk_combo_box_get_active(om));
647 void popup_timeout_changed(void)
649 cfg_set_single_value_as_int(config, TRAY_ICON2_ID, "tooltip-timeout",
650 gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(glade_xml_get_widget(tray_icon2_preferences_xml, "popup_timeout"))));
653 static void update_popup_settings(void)
655 gtk_toggle_button_set_active((GtkToggleButton *)
656 glade_xml_get_widget(tray_icon2_preferences_xml, "ck_popup_enable"),
657 cfg_get_single_value_as_int_with_default(config, TRAY_ICON2_ID, "show-tooltip", 1));
658 gtk_combo_box_set_active(GTK_COMBO_BOX(glade_xml_get_widget(tray_icon2_preferences_xml, "om_popup_position")),
659 cfg_get_single_value_as_int_with_default(config, TRAY_ICON2_ID, "tooltip-position", 0));
660 gtk_spin_button_set_value(GTK_SPIN_BUTTON(glade_xml_get_widget(tray_icon2_preferences_xml, "popup_timeout")),
661 cfg_get_single_value_as_int_with_default(config, TRAY_ICON2_ID, "tooltip-timeout", 5));
664 static void tray_icon2_preferences_destroy(GtkWidget *container)
666 if(tray_icon2_preferences_xml) {
667 GtkWidget *vbox = glade_xml_get_widget(tray_icon2_preferences_xml, "tray-pref-vbox");
668 gtk_container_remove(GTK_CONTAINER(container),vbox);
669 g_object_unref(tray_icon2_preferences_xml);
670 tray_icon2_preferences_xml = NULL;
673 void tray_icon2_preferences_pm_combo_changed(GtkComboBox *cm, gpointer data)
675 int level = gtk_combo_box_get_active(cm);
676 cfg_set_single_value_as_int(config, "Default","min-error-level", level);
678 static void tray_icon2_preferences_construct(GtkWidget *container)
680 gchar *path = gmpc_get_full_glade_path("gmpc.glade");
681 tray_icon2_preferences_xml = glade_xml_new(path, "tray-pref-vbox",NULL);
683 if(tray_icon2_preferences_xml) {
684 GtkWidget *vbox = glade_xml_get_widget(tray_icon2_preferences_xml, "tray-pref-vbox");
685 gtk_container_add(GTK_CONTAINER(container),vbox);
686 tray_update_settings();
687 update_popup_settings();
688 glade_xml_signal_autoconnect(tray_icon2_preferences_xml);
690 gtk_combo_box_set_active(GTK_COMBO_BOX(glade_xml_get_widget(tray_icon2_preferences_xml, "pm-combo")),
691 cfg_get_single_value_as_int_with_default(config, "Default","min-error-level", ERROR_INFO));
693 gmpcPrefPlugin tray_icon2_preferences = {
694 tray_icon2_preferences_construct,
695 tray_icon2_preferences_destroy
698 gmpcPlugin tray_icon2_plug = {
699 .name = N_("Notification"),
700 .version = {0,0,0},
701 .plugin_type = GMPC_INTERNALL,
702 .init = tray_icon2_init,
703 .destroy = tray_icon2_destroy,
704 .mpd_status_changed = tray_icon2_status_changed,
705 .mpd_connection_changed = tray_icon2_connection_changed,
706 .set_enabled = tray_icon2_set_enabled,
707 .get_enabled = tray_icon2_get_enabled,
708 .pref = &tray_icon2_preferences