Remove Gmpc.MpdData.Treeview,
[gmpc.git] / src / Widgets / gmpc-metadata-similarsongs.vala
blob1ed982d1926c3ee0bd0f57b92dffb406fddf687e
1 /* Gnome Music Player Client (GMPC)
2 * Copyright (C) 2004-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.
21 using Config;
22 using Gtk;
23 using Gmpc;
25 private const bool use_transition_mdss = Gmpc.use_transition;
26 private const string some_unique_name_mdss = Config.VERSION;
28 public class Gmpc.MetaData.Widgets.SimilarSongs : Gtk.Alignment
30 private MPD.Song? song = null;
31 private Gtk.Widget pchild = null;
32 private uint idle_add = 0;
33 ~SimilarSongs ()
35 if(this.idle_add > 0)
37 GLib.Source.remove(this.idle_add);
38 this.idle_add = 0;
42 public SimilarSongs (MPD.Song song)
44 this.song = song.copy();
45 this.set(0.0f, 0.0f, 1.0f, 0.0f);
47 private void add_clicked(Gtk.Widget item)
49 Gtk.TreeView tree = (Gtk.TreeView)this.pchild;
51 var sel = tree.get_selection();
52 Gtk.TreeModel model = null;
53 Gtk.TreeIter iter;
54 List<Gtk.TreePath> list = sel.get_selected_rows(out model);
55 foreach(Gtk.TreePath path in list)
57 if(model.get_iter(out iter, path))
59 unowned MPD.Song? song = null;
60 model.get(iter, 0, out song, -1);
61 if(song != null)
63 MPD.PlayQueue.queue_add_song(server, song.file);
67 MPD.PlayQueue.queue_commit(server);
70 private void play_clicked(Gtk.Widget item)
72 Gtk.TreeView tree = (Gtk.TreeView)this.pchild;
74 var sel = tree.get_selection();
75 Gtk.TreeModel model = null;
76 Gtk.TreeIter iter;
77 List<Gtk.TreePath> list = sel.get_selected_rows(out model);
78 if(list != null)
80 Gtk.TreePath path = list.data;
81 if(model.get_iter(out iter, path))
83 unowned MPD.Song? song = null;
84 model.get(iter, 0, out song, -1);
85 if(song != null)
87 Gmpc.MpdInteraction.play_path(song.file);
92 private void replace_clicked(Gtk.Widget item)
94 bool found = false;
95 Gtk.TreeView tree = (Gtk.TreeView)this.pchild;
96 var sel = tree.get_selection();
97 Gtk.TreeModel model = null;
98 Gtk.TreeIter iter;
99 List<Gtk.TreePath> list = sel.get_selected_rows(out model);
100 foreach(Gtk.TreePath path in list)
102 if(model.get_iter(out iter, path))
104 unowned MPD.Song? song = null;
105 model.get(iter, 0, out song, -1);
106 if(song != null)
108 MPD.PlayQueue.queue_add_song(server, song.file);
109 found = true;
113 if(found)
115 MPD.PlayQueue.clear(server);
116 MPD.PlayQueue.queue_commit(server);
117 MPD.Player.play(server);
121 this.play_clicked(item);
123 private Gmpc.MetaData.Item copy = null;
124 MPD.Data.Item item = null;
125 private unowned List <unowned string> current = null;
126 private bool update_sim_song()
128 if(current == null)
130 current = copy.get_text_list();
131 pchild = new Gtk.ProgressBar();
132 this.add(pchild);
133 this.show_all();
135 ((Gtk.ProgressBar)pchild).pulse();
136 if(current != null)
138 string entry = current.data;
139 if(entry != null)
141 var split = entry.split("::",2);
142 if(split.length == 2)
144 MPD.Database.search_start(server, false);
145 var art_split = split[0].split(" ");
146 foreach(string artist in art_split)
148 MPD.Database.search_add_constraint(server, MPD.Tag.Type.ARTIST, artist);
151 MPD.Database.search_add_constraint(server, MPD.Tag.Type.TITLE, split[1]);
152 var data = MPD.Database.search_commit(server);
153 if(data != null)
155 item.concatenate((owned)data);
159 current = current.next;
160 if(current != null) return true;
162 this.pchild.destroy();
163 if(item != null)
165 var model = new Gmpc.MpdData.Model();
166 item.remove_duplicate_songs();
167 model.set_mpd_data((owned)item);
168 Gmpc.DataView tree = new Gmpc.DataView("similar-song", Gmpc.DataView.ViewType.SONG_LIST);
169 tree.set_model(model);
170 this.add(tree);
172 this.pchild = tree;
174 else
176 var label = new Gtk.Label(_("Unavailable"));
177 label.set_alignment(0.0f, 0.0f);
178 this.add(label);
179 this.pchild = label;
182 copy = null;
183 this.idle_add = 0;
185 this.show_all();
186 return false;
188 private void metadata_changed(Meta.Watcher gmw2, MPD.Song song, Gmpc.MetaData.Type type, Gmpc.MetaData.Result result, Gmpc.MetaData.Item? met)
190 if(this.song.artist.collate(song.artist)!=0) return;
191 if(type != Gmpc.MetaData.Type.SONG_SIMILAR) return;
193 if(this.pchild != null) this.pchild.destroy();
195 if(result == Gmpc.MetaData.Result.FETCHING)
197 var label = new Gtk.Label(_("Fetching .. "));
198 label.set_alignment(0.0f, 0.0f);
199 this.add(label);
200 this.pchild = label;
202 else if (result == Gmpc.MetaData.Result.UNAVAILABLE)
204 var label = new Gtk.Label(_("Unavailable"));
205 label.set_alignment(0.0f, 0.0f);
206 this.add(label);
207 this.pchild = label;
209 else
211 if(met.is_text_list())
213 this.copy = met.dup_steal();
214 this.idle_add = GLib.Idle.add(this.update_sim_song);
215 return;
217 else
219 var label = new Gtk.Label(_("Unavailable"));
220 label.set_alignment(0.0f, 0.0f);
221 this.add(label);
222 this.pchild = label;
225 this.show_all();
228 public void update()
230 MetaData.Item item = null;
231 metawatcher.data_changed.connect(metadata_changed);
232 Gmpc.MetaData.Result gm_result = MetaData.get_path(song, Gmpc.MetaData.Type.SONG_SIMILAR,out item);
233 this.metadata_changed(metawatcher, this.song, Gmpc.MetaData.Type.SONG_SIMILAR, gm_result, item);