Small fix in open_uri.
[gmpc.git] / src / Widgets / gmpc-song-links.vala
blob4f1e09fd00733c459933c223752e7a37dc19802a
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.
20 using Config;
21 using GLib;
22 using Gtk;
23 using Gdk;
24 using Cairo;
25 using MPD;
26 using Gmpc;
28 private const bool use_transition_sl = Gmpc.use_transition;
30 static bool initialized = false;
31 public class Gmpc.MetaData.Widgets.SongLinks: Gtk.Frame
33 private const string some_unique_name = Config.VERSION;
34 public enum Type {
35 ARTIST,
36 ALBUM,
37 SONG
39 private Type type = Type.ARTIST;
40 private MPD.Song? song = null;
42 ~SongLinks()
46 static void open_uri_function(Gtk.LinkButton but, string uri)
48 Gmpc.open_uri(uri);
51 public SongLinks(Type type, MPD.Song song)
53 debug("Links created");
54 this.type = type;
55 this.song = song.copy();
56 var event = new Gtk.EventBox();
57 var label = new Gtk.Label("");
58 event.add(label);
59 event.visible_window = false;
60 this.label_widget = event;
61 label.set_markup("<b>%s:</b>".printf(_("Web Links")));
62 this.shadow = Gtk.ShadowType.NONE;
64 parse_uris();
66 if(!initialized) {
67 Gtk.LinkButton.set_uri_hook(open_uri_function);
68 initialized = true;
72 private void parse_uris()
74 var child = this.get_child();
75 if(child != null) child.destroy();
77 var file = new GLib.KeyFile();
78 var path = Gmpc.user_path("weblinks.list");
79 if(! FileUtils.test(path, FileTest.EXISTS))
81 path = Gmpc.data_path("weblinks.list");
82 if(! FileUtils.test(path, FileTest.EXISTS))
84 return;
87 try {
88 file.load_from_file(path, GLib.KeyFileFlags.NONE);
90 catch (Error e) {
91 return;
94 var ali = new Gtk.Alignment(0.0f, 0.0f, 0.0f,0.0f);
95 ali.set_padding(8,8,12,6);
96 this.add(ali);
98 var vbox = new Gtk.VBox(false, 0);
99 ali.add(vbox);
101 var groups = file.get_groups();
102 foreach(string entry in groups)
104 try{
105 string typestr = file.get_string(entry,"type");
106 string uri = file.get_string(entry, "url");
108 Type type;
109 switch(typestr) {
110 case "artist":
111 type = Type.ARTIST;
112 if(this.song.artist != null)
113 uri = uri.replace("%ARTIST%", Gmpc.AsyncDownload.escape_uri(this.song.artist));
114 break;
115 case "album":
116 type = Type.ALBUM;
118 if(this.song.album != null)
119 uri = uri.replace("%ALBUM%", Gmpc.AsyncDownload.escape_uri(this.song.album));
121 if(this.song.artist != null)
122 uri = uri.replace("%ARTIST%", Gmpc.AsyncDownload.escape_uri(this.song.artist));
123 break;
124 case "song":
125 default:
126 type = Type.SONG;
128 if(this.song.title != null)
129 uri = uri.replace("%TITLE%", Gmpc.AsyncDownload.escape_uri(this.song.title));
131 if(this.song.album != null)
132 uri = uri.replace("%ALBUM%", Gmpc.AsyncDownload.escape_uri(this.song.album));
134 if(this.song.artist != null)
135 uri = uri.replace("%ARTIST%", Gmpc.AsyncDownload.escape_uri(this.song.artist));
136 break;
138 try{
139 string sar = file.get_string(entry, "search-and-replace");
140 if(sar != null) {
141 string[] s = sar.split("::");
142 if(s.length == 2){
143 try{
144 var regex = new GLib.Regex (s[0]);
145 uri = regex.replace_literal(uri,-1,0, s[1]);
146 } catch (GLib.RegexError e) {
147 GLib.debug("Failed to compile regex: '%s'\n", e.message);
151 }catch(Error e) {
155 if((int)type <= (int)this.type)
157 var label = new Gtk.LinkButton(uri);
158 label.set_label(_("Lookup %s on %s").printf(_(typestr),entry));
159 label.set_alignment(0.0f, 0.5f);
160 vbox.pack_start(label, false, true, 0);
162 }catch(Error e){
163 GLib.error("Failed to get entry from %s: '%s'\n", path, e.message);