Re-add rating.
[gmpc.git] / src / Widgets / gmpc-rating.vala
blob7d43ff8cad89d57c8bf30bba877eea1134c1b0be
1 /* Gnome Music Player Client (GMPC)
2 * Copyright (C) 2004-2014 Qball Cow <qball@sarine.nl>
3 * Project homepage: http://gmpc.wikia.com/
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;
23 using Cairo;
24 using MPD;
25 using Gmpc;
29 public class Gmpc.Rating : Gtk.Frame
31 private const bool use_transition = Gmpc.use_transition;
32 private unowned MPD.Server server = null;
33 private MPD.Song song = null;
34 private Gtk.Image[] rat;
35 private Gtk.HBox box;
36 public Gtk.EventBox event_box;
37 private int rating = -1;
38 static int id = id+1;
40 private ulong status_changed_id = 0;
42 ~Rating() {
43 if (this.status_changed_id > 0 && GLib.SignalHandler.is_connected(gmpcconn,
44 this.status_changed_id)) {
45 GLib.SignalHandler.disconnect(gmpcconn, this.status_changed_id);
46 this.status_changed_id = 0;
50 public bool button_press_event_callback(Gtk.Widget wid, Gdk.EventButton event)
52 if(event.type == Gdk.EventType.BUTTON_PRESS)
54 if(event.button == 1)
56 Gtk.Allocation ns;
57 this.event_box.get_allocation(out ns);
58 int width = ns.width;
59 int button = (int)((((event.x)/(double)width)+0.15)*10);
60 MPD.Sticker.Song.set(this.server, this.song.file, "rating", button.to_string());
61 this.set_rating(button/2);
65 return false;
68 private void status_changed ( MPD.Server server, MPD.Status.Changed what,Gmpc.Connection conn)
70 if(((what&MPD.Status.Changed.STICKER) != 0))
72 this.update();
76 public Rating (MPD.Server server, MPD.Song song) {
77 this.server = server;
78 this.song = song.copy();
79 this.update();
80 this.status_changed_id = GLib.Signal.connect_swapped(gmpcconn, "status_changed",
81 (GLib.Callback)status_changed, this);
84 construct {
85 int i;
86 this.shadow_type = Gtk.ShadowType.NONE;
88 this.box = new Gtk.HBox(true,6);
89 this.rat = new Gtk.Image[5];
90 this.event_box = new Gtk.EventBox();
91 this.event_box.visible_window = false;
93 for(i=0;i<5;i++) {
94 // Copy value.
95 var curr_entry = i+1;
96 this.rat[i] = new Gtk.Image.from_icon_name("rating", Gtk.IconSize.MENU);
97 this.box.pack_start(this.rat[i], false, false, 0);
100 this.event_box.add(this.box);
101 this.event_box.button_press_event.connect(button_press_event_callback);
102 this.add(this.event_box);
103 this.show_all();
107 public void set_rating(int rating)
109 int i=0;
110 if(rating != this.rating)
112 for(i=0;i<5;i++)
114 this.rat[i].sensitive = i<rating;
116 this.rating = rating;
119 public void update()
121 var value = MPD.Sticker.Song.get(this.server,this.song.file, "rating");
122 if(value == null) {
123 this.set_rating(0);
124 } else {
125 this.set_rating(value.to_int()/2);