Make plugin translatable
[gmpc-fullscreeninfo.git] / src / plugin.c
blobe5f0f7214e0c2bfdd8ea41a65e10cfb116a813ff
1 /* gmpc-fullscreen_info (GMPC plugin)
2 * Copyright (C) 2006-2009 Qball Cow <qball@sarine.nl>
3 * Project homepage: http://gmpcwiki.sarine.nl/
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 #include <stdio.h>
21 #include <string.h>
22 #include <config.h>
23 #include <glib.h>
24 #include <glib/gi18n-lib.h>
25 #include <gmpc/plugin.h>
26 #include <gmpc/gmpc-metaimage.h>
27 #include <gmpc/gmpc-connection.h>
28 #include <gmpc/gmpc-plugin.h>
29 #include <gdk/gdkkeysyms.h>
30 #include <config.h>
32 typedef struct _FullscreenInfoPlugin
34 GmpcPluginBase parent_instance;
35 } FullscreenInfoPlugin ;
37 typedef struct _FullscreenInfoPluginClass
39 GmpcPluginBaseClass parent_class;
40 } FullscreenInfoPluginClass;
42 extern GmpcConnection *gmpcconn;
44 gulong signal_handler = 0;
46 GtkWidget *buttons[4] = {NULL, };
47 GtkWidget *label = NULL;
48 /**
49 * Get/Set enabled functions.
51 static gboolean fullscreen_info_get_enabled(GmpcPluginBase *plug)
53 return cfg_get_single_value_as_int_with_default(config, "fullscreen_info-plugin", "enable", TRUE);
55 static void fullscreen_info_set_enabled(GmpcPluginBase *plug, int enabled)
57 cfg_set_single_value_as_int(config, "fullscreen_info-plugin", "enable", enabled);
61 static void gfi_button_press_event(GtkWidget *widevent, GdkEventButton *event, GtkWidget *window)
63 gtk_widget_destroy(window);
64 if(signal_handler)
65 g_signal_handler_disconnect(G_OBJECT(gmpcconn), signal_handler);
66 signal_handler = 0;
68 static gboolean gfi_window_fullscreen(GtkWidget *window)
70 gtk_window_fullscreen(GTK_WINDOW(window));
71 return FALSE;
73 static void gfi_size_allocate(GtkWidget *window, GtkAllocation *alloc, GtkWidget *image)
75 g_idle_add((GSourceFunc)gfi_window_fullscreen, window);
78 static void gfi_size_allocate_mi(GtkWidget *window, GtkAllocation *alloc, GtkWidget *image){
80 gmpc_metaimage_set_size(GMPC_METAIMAGE(image), (alloc->width < alloc->height)?alloc->width:alloc->height);
81 gmpc_metaimage_reload_image(GMPC_METAIMAGE(image));
83 static void play_pause(MpdObj *mi)
85 if(mpd_player_get_state(mi) == MPD_PLAYER_PLAY){
86 mpd_player_pause(mi);
87 }else{
88 mpd_player_play(mi);
91 static void status_changed(GmpcConnection *gmpcconn, MpdObj *mi, ChangedStatusType what, gpointer data)
93 if(what&MPD_CST_STATE){
94 GtkWidget *child = gtk_bin_get_child(GTK_BIN(buttons[2]));
96 if(mpd_player_get_state(connection) == MPD_PLAYER_PLAY){
97 gtk_image_set_from_stock(GTK_IMAGE(child),GTK_STOCK_MEDIA_PAUSE,GTK_ICON_SIZE_DIALOG);
98 }else{
99 gtk_image_set_from_stock(GTK_IMAGE(child),GTK_STOCK_MEDIA_PLAY,GTK_ICON_SIZE_DIALOG);
102 if(what&(MPD_CST_SONGID|MPD_CST_SONGPOS|MPD_CST_PLAYLIST|MPD_CST_STATE))
104 char buffer[512];
105 mpd_Song *song = mpd_playlist_get_current_song(mi);
106 if(song && mpd_player_get_state(mi) != MPD_PLAYER_STOP)
108 mpd_song_markup_escaped(buffer, 512, C_("header title markup","<span size='xx-large'>[[%title% - &[%artist%]]|%shortfile%]</span>"),song);
110 else{
111 snprintf(buffer, 512, "<span size='xx-large'>%s</span>", _("Gnome Music Player Client"));
113 gtk_label_set_markup(GTK_LABEL(label), buffer);
117 static void gfi_fullscreen(void)
119 gchar *temp;
120 GtkWidget *popup = gtk_window_new(GTK_WINDOW_TOPLEVEL);
121 GtkWidget *metaimage = gmpc_metaimage_new(META_ALBUM_ART);
122 GtkWidget *vbox = gtk_vbox_new(FALSE, 6);
123 GtkWidget *hbox = gtk_hbox_new(TRUE, 6);
124 gtk_widget_set_has_tooltip(metaimage, FALSE);
126 gtk_window_set_keep_above(GTK_WINDOW(popup), TRUE);
127 /* Setup the meta image */
128 gmpc_metaimage_set_no_cover_icon(GMPC_METAIMAGE(metaimage),(char *)"gmpc");
129 gmpc_metaimage_set_squared(GMPC_METAIMAGE(metaimage), TRUE);
130 gmpc_metaimage_set_connection(GMPC_METAIMAGE(metaimage), connection);
131 gmpc_metaimage_update_cover(GMPC_METAIMAGE(metaimage), connection,MPD_CST_SONGID, NULL);
132 g_signal_connect(G_OBJECT(metaimage), "button-press-event", G_CALLBACK(gfi_button_press_event), popup);
135 gtk_container_add(GTK_CONTAINER(popup), vbox);
136 gtk_window_set_resizable(GTK_WINDOW(popup), TRUE);
138 g_signal_connect(G_OBJECT(popup), "size-allocate", G_CALLBACK(gfi_size_allocate), metaimage);
140 g_signal_connect(G_OBJECT(metaimage), "size-allocate", G_CALLBACK(gfi_size_allocate_mi), metaimage);
144 * Main title label
146 label = gtk_label_new("");
147 temp = g_markup_printf_escaped("<span size='xx-large'>%s</span>", _("Gnome Music Player Client"));
148 gtk_label_set_markup(GTK_LABEL(label), temp);
149 g_free(temp);
150 gtk_label_set_ellipsize(GTK_LABEL(label), PANGO_ELLIPSIZE_END);
151 gtk_widget_ensure_style(label);
152 gtk_widget_modify_fg(label, GTK_STATE_NORMAL, &(label->style->white));
154 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE,0);
156 gtk_box_pack_start(GTK_BOX(vbox), metaimage, TRUE, TRUE, 0);
158 * Control buttons */
159 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE,0);
163 /* Prev */
164 buttons[0] = gtk_button_new();
165 gtk_container_add(GTK_CONTAINER(buttons[0]),
166 gtk_image_new_from_stock(GTK_STOCK_MEDIA_PREVIOUS, GTK_ICON_SIZE_DIALOG));
167 gtk_button_set_relief(GTK_BUTTON(buttons[0]), GTK_RELIEF_NONE);
168 gtk_box_pack_start(GTK_BOX(hbox), buttons[0], FALSE, TRUE, 0);
169 g_signal_connect_swapped(G_OBJECT(buttons[0]), "clicked", G_CALLBACK(mpd_player_prev), connection);
170 /* STOP */
171 buttons[1] = gtk_button_new();
172 gtk_container_add(GTK_CONTAINER(buttons[1]),
173 gtk_image_new_from_stock(GTK_STOCK_MEDIA_STOP, GTK_ICON_SIZE_DIALOG));
174 gtk_button_set_relief(GTK_BUTTON(buttons[1]), GTK_RELIEF_NONE);
175 gtk_box_pack_start(GTK_BOX(hbox), buttons[1], FALSE, TRUE, 0);
177 g_signal_connect_swapped(G_OBJECT(buttons[1]), "clicked", G_CALLBACK(mpd_player_stop), connection);
179 /* Play/Pause */
180 buttons[2] = gtk_button_new();
181 gtk_container_add(GTK_CONTAINER(buttons[2]),
182 gtk_image_new_from_stock(GTK_STOCK_MEDIA_PLAY, GTK_ICON_SIZE_DIALOG));
183 gtk_button_set_relief(GTK_BUTTON(buttons[2]), GTK_RELIEF_NONE);
184 gtk_box_pack_start(GTK_BOX(hbox), buttons[2], FALSE, TRUE, 0);
186 g_signal_connect_swapped(G_OBJECT(buttons[2]), "clicked", G_CALLBACK(play_pause), connection);
188 /* Next */
189 buttons[3] = gtk_button_new();
190 gtk_container_add(GTK_CONTAINER(buttons[3]),
191 gtk_image_new_from_stock(GTK_STOCK_MEDIA_NEXT, GTK_ICON_SIZE_DIALOG));
192 gtk_button_set_relief(GTK_BUTTON(buttons[3]), GTK_RELIEF_NONE);
193 gtk_box_pack_start(GTK_BOX(hbox), buttons[3], FALSE, TRUE, 0);
195 g_signal_connect_swapped(G_OBJECT(buttons[3]), "clicked", G_CALLBACK(mpd_player_next), connection);
197 gtk_widget_ensure_style(popup);
198 gtk_widget_modify_bg(popup, GTK_STATE_NORMAL, &(popup->style->black));
201 status_changed(gmpcconn, connection, MPD_CST_STATE|MPD_CST_SONGPOS, NULL);
202 signal_handler = g_signal_connect(G_OBJECT(gmpcconn), "status-changed", G_CALLBACK(status_changed), NULL);
203 gtk_widget_show_all(popup);
206 static int gfi_add_tool_menu(GmpcPluginToolMenuIface *plug, GtkMenu *menu)
208 GtkWidget *item = NULL;
210 item = gtk_image_menu_item_new_with_label(_("Fullscreen Info"));
211 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item),
212 gtk_image_new_from_icon_name("playlist-browser", GTK_ICON_SIZE_MENU));
213 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
214 gtk_widget_add_accelerator(GTK_WIDGET(item), "activate", gtk_menu_get_accel_group(GTK_MENU(menu)), GDK_F11, 0, GTK_ACCEL_VISIBLE);
215 g_signal_connect(G_OBJECT(item), "activate",
216 G_CALLBACK(gfi_fullscreen), NULL);
217 return 1;
221 * Gobject plugin
223 static void fullscreen_info_plugin_class_init (FullscreenInfoPluginClass *klass);
224 GType fullscreen_info_plugin_get_type(void);
226 static int *fullscreen_info_plugin_get_version(GmpcPluginBase *plug, int *length)
228 static int version[3] = {PLUGIN_MAJOR_VERSION,PLUGIN_MINOR_VERSION,PLUGIN_MICRO_VERSION};
229 if(length) *length = 3;
230 return (int *)version;
233 static const char *fullscreen_info_plugin_get_name(GmpcPluginBase *plug)
235 return N_("Fullscreen info plugin");
237 static GObject *fullscreen_info_plugin_constructor(GType type, guint n_construct_properties, GObjectConstructParam * construct_properties) {
238 FullscreenInfoPluginClass * klass;
239 FullscreenInfoPlugin *self;
240 GObjectClass * parent_class;
241 klass = (g_type_class_peek (fullscreen_info_plugin_get_type()));
242 parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
243 self = (FullscreenInfoPlugin *) parent_class->constructor (type, n_construct_properties, construct_properties);
245 /* Setup textdomain */
246 bindtextdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
247 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
249 /* Make it an internal plugin */
250 GMPC_PLUGIN_BASE(self)->translation_domain = GETTEXT_PACKAGE;
251 GMPC_PLUGIN_BASE(self)->plugin_type = GMPC_PLUGIN_NO_GUI;
253 return G_OBJECT(self);
257 static void fullscreen_info_plugin_class_init (FullscreenInfoPluginClass *klass)
259 G_OBJECT_CLASS(klass)->constructor = fullscreen_info_plugin_constructor;
260 /* Connect plugin functions */
261 GMPC_PLUGIN_BASE_CLASS(klass)->get_version = fullscreen_info_plugin_get_version;
262 GMPC_PLUGIN_BASE_CLASS(klass)->get_name = fullscreen_info_plugin_get_name;
264 GMPC_PLUGIN_BASE_CLASS(klass)->get_enabled = fullscreen_info_get_enabled;
265 GMPC_PLUGIN_BASE_CLASS(klass)->set_enabled = fullscreen_info_set_enabled;
268 static void fullscreen_info_plugin_gmpc_plugin_tool_menu_iface_interface_init (GmpcPluginToolMenuIfaceIface * iface) {
269 iface->tool_menu_integration = gfi_add_tool_menu;
272 GType fullscreen_info_plugin_get_type(void) {
273 static GType fullscreen_info_plugin_type_id = 0;
274 if(fullscreen_info_plugin_type_id == 0) {
275 static const GTypeInfo info = {
276 .class_size = sizeof(FullscreenInfoPluginClass),
277 .class_init = (GClassInitFunc)fullscreen_info_plugin_class_init,
278 .instance_size = sizeof(FullscreenInfoPlugin),
279 .n_preallocs = 0
282 static const GInterfaceInfo iface_info = { (GInterfaceInitFunc) fullscreen_info_plugin_gmpc_plugin_tool_menu_iface_interface_init,
283 (GInterfaceFinalizeFunc) NULL, NULL};
285 fullscreen_info_plugin_type_id = g_type_register_static(GMPC_PLUGIN_TYPE_BASE, "FullscreenInfosPlugin", &info, 0);
287 g_type_add_interface_static (fullscreen_info_plugin_type_id, GMPC_PLUGIN_TYPE_TOOL_MENU_IFACE, &iface_info);
289 return fullscreen_info_plugin_type_id;
292 G_MODULE_EXPORT GType plugin_get_type(void)
294 return fullscreen_info_plugin_get_type();