Update vala version.
[gmpc.git] / src / Widgets / gmpc-metadata-backdrop.vala
blob702b2df11157090c05e01ea4fedf9878e3a712a7
1 /* Gnome Music Player Client
2 * Copyright (C) 2011-2012 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.
19 using GLib;
20 using Gmpc;
22 private const string log_domain_mdbd = "Gmpc.Widgets.MetaData.Backdrop";
23 namespace Gmpc
25 namespace MetaData.Widgets
27 class Backdrop : Gtk.EventBox
29 private string song_checksum = null;
30 private MPD.Song? cur_song = null;
31 private Gmpc.MetaData.Type cur_type = Gmpc.MetaData.Type.BACKDROP_ART;
32 /* */
33 private Gdk.Pixbuf pb = null;
34 private ModificationType mod_type = (ModificationType)
35 config.get_int_with_default(
36 "Backdrop", "alterations",2);
38 // Image loading
39 private Gmpc.PixbufLoaderAsync? loader = null;
40 private void set_from_item (Gmpc.MetaData.Item? item)
42 pb = null;
43 if(item == null || song_checksum == null)
45 this.queue_draw();
46 return;
48 if (item.content_type == Gmpc.MetaData.ContentType.RAW)
50 Gtk.Allocation req;
51 int width;
52 this.get_allocation(out req);
53 width = int.max(req.width, 400);
54 log(log_domain_mdbd, GLib.LogLevelFlags.LEVEL_DEBUG,
55 "Getting image with size: %u", width);
56 if(loader == null)
57 loader = new PixbufLoaderAsync();
58 loader.pixbuf_update.connect((source, pixbuf)=>
60 this.pb = pixbuf;
61 log (log_domain_mdbd,GLib.LogLevelFlags.LEVEL_DEBUG,
62 "Updating background");
63 this.queue_draw();
64 });
65 unowned uchar[] data = item.get_raw();
66 loader.set_from_raw(data, width,-1, mod_type, item.md5sum);
69 /**
70 * @param The #MPD.Song to set the background for or NULL to clear.
72 * Set the background for song, or clear the background.
74 public void set_song(MPD.Song? song)
76 cur_song = song.copy();
77 if(song == null)
79 song_checksum = null;
80 set_from_item(null);
81 return;
83 // Same artist/bg do not update.
84 if(song_checksum == Gmpc.Misc.song_checksum_type(song, cur_type))
85 return;
87 song_checksum = Gmpc.Misc.song_checksum_type(song, cur_type);
88 Gmpc.MetaData.Item item = null;
89 var a = Gmpc.MetaData.get_path(song, cur_type,out item); //metawatcher.query(song, cur_type, out item);
90 if(a == Gmpc.MetaData.Result.AVAILABLE)
92 this.set_from_item(item);
94 else
96 this.set_from_item(null);
99 /**
100 * @param type The type of backdrop (artist or album image)
101 * Create backdrop widget of type.
102 * @return a new #Backdrop (of type #Gtk.Eventbox)
104 public Backdrop(Gmpc.MetaData.Type type)
106 assert(type == Gmpc.MetaData.Type.BACKDROP_ART ||
107 type == Gmpc.MetaData.Type.ARTIST_ART||
108 type == Gmpc.MetaData.Type.ALBUM_ART);
109 cur_type = type;
112 this.realize.connect((source)=>
114 source.get_window().set_back_pixmap(null, true);
117 // Set visible window
118 this.set_visible_window(true);
119 // Set paintable
120 this.set_app_paintable(true);
121 // Watch changes */
122 metawatcher.data_changed.connect((csong, type, result, met) =>
124 if(song_checksum != null &&
125 type == cur_type &&
126 song_checksum == Gmpc.Misc.song_checksum_type(csong,cur_type))
128 if(met != null && result == Gmpc.MetaData.Result.AVAILABLE)
130 this.set_from_item(met);
132 else
134 this.set_from_item(null);
139 // Add expose event
140 this.draw.connect(container_expose);
142 this.button_press_event.connect(button_press_event_callback);
144 private bool button_press_event_callback(Gdk.EventButton event)
146 if(event.button != 3) return false;
147 if(cur_song == null) return false;
148 if(cur_song.artist == null) return false;
149 var menu = new Gtk.Menu();
151 var item = new Gtk.ImageMenuItem.with_label(_("Refresh backdrop"));
152 item.set_image(new Gtk.Image.from_stock("gtk-refresh", Gtk.IconSize.MENU));
153 item.activate.connect((source)=>
155 MetaData.Item mitem = null;
156 stdout.printf("Push backdrop update\n");
157 var a = MetaData.get_path(cur_song, cur_type|Gmpc.MetaData.Type.QUERY_NO_CACHE, out mitem);
158 //metawatcher.query(cur_song, cur_type|Gmpc.MetaData.Type.QUERY_NO_CACHE, out mitem);
159 if(a == Gmpc.MetaData.Result.AVAILABLE)
161 this.set_from_item(mitem);
163 else {
164 this.set_from_item(null);
167 menu.append(item);
168 /* Add selector */
169 item = new Gtk.ImageMenuItem.with_label(_("Metadata selector"));
170 item.set_image(new Gtk.Image.from_stock("gtk-edit", Gtk.IconSize.MENU));
171 item.activate.connect((source)=>
173 new Gmpc.MetaData.EditWindow(cur_song, cur_type);
175 menu.append(item);
176 menu.show_all();
177 menu.popup(null, null, null, event.button, event.time);
178 return true;
181 * Draw the background. (only exposed part)
183 private bool container_expose(Gtk.Widget ev, Cairo.Context gc)
185 Gtk.Allocation alloc;
186 ev.get_allocation(out alloc);
187 gc.rectangle(alloc.x, alloc.y, alloc.width, alloc.height);
188 /* If there is an background image set. */
189 if(pb != null)
191 gc.set_source_rgb(0.0,0.0,0.0);
192 gc.fill_preserve();
193 Gdk.cairo_set_source_pixbuf(gc, pb,0.0,0.0);
194 gc.fill();
196 /* no image set */
197 else
199 gc.set_source_rgb(0.0,0.0,0.0);
200 gc.fill();
202 return false;