2
* Copyright (C) 2013 Kylin Ltd
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License version 3 as
6
* published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Authored by Weihua Zhang <fly2high@126.com>
17
* Authored by liuxing <liuxingcs@yeah.net>
18
* Modified Information:
19
* Rewrite perform_search to search China music from music.baidu.com
23
* Copyright (C) 2011 Canonical Ltd
25
* This program is free software: you can redistribute it and/or modify
26
* it under the terms of the GNU General Public License version 3 as
27
* published by the Free Software Foundation.
29
* This program is distributed in the hope that it will be useful,
30
* but WITHOUT ANY WARRANTY; without even the implied warranty of
31
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32
* GNU General Public License for more details.
34
* You should have received a copy of the GNU General Public License
35
* along with this program. If not, see <http://www.gnu.org/licenses/>.
37
* Authored by Alex Launi <alex.launi@canonical.com>
43
namespace Unity.MusicLens
46
static Application? app = null;
47
static MusicBaiduScopeProxy? daemon = null;
50
public class MusicBaiduScopeProxy : SimpleScope
52
private MusicBaiduCollection collection;
53
private Unity.MusicPreview? music_preview;
55
public MusicBaiduScopeProxy ()
59
scope = new Unity.Scope ("/com/canonical/unity/scope/musicbaidu");
60
scope.search_in_global = true;
61
scope.activate_uri.connect (activate);
62
scope.preview_uri.connect (preview);
66
collection = new MusicBaiduCollection ();
72
catch (GLib.IOError e)
74
stdout.printf (e.message);
79
protected override int num_results_without_search { get { return 100;
82
protected override int num_results_global_search { get { return 20;
85
protected override int num_results_lens_search { get { return 50;
90
public Unity.ActivationResponse activate (string uri)
92
/* launch the music baidu streaming client */
96
if(uri.has_prefix ("song://"))
98
AppInfo.launch_default_for_uri (uri.substring (7), null);//strip off "song://" from the uri
100
else if(uri.has_prefix ("album://"))
102
AppInfo.launch_default_for_uri (uri.substring (8), null);//strip off "album://" from the uri
107
warning ("Failed to open uri %s. %s", uri, e.message);
109
return new Unity.ActivationResponse (Unity.HandledType.HIDE_DASH);
112
public Unity.ActivationResponse download_album (string uri)
114
return activate (uri);
117
public Unity.Preview preview (string uri)
119
music_preview = null;
121
SList<Track> tracks = null;
124
if(uri.has_prefix ("album://"))
126
collection.get_album_details(uri, out album, out tracks,null);
128
else if(uri.has_prefix ("song://"))
130
collection.get_song_details (uri, out album, out tracks,null);
136
GLib.Icon? icon_file = null;
140
var artwork_path = album.artwork_path;
141
icon_file = new GLib.FileIcon(File.new_for_uri (artwork_path));
142
music_preview = new Unity.MusicPreview (album.title, album.artist, icon_file);
147
foreach (Track track in tracks)
149
TrackMetadata tm = new TrackMetadata ();
151
tm.track_no = i++; //FIXME: u1ms search doesn't provide track numbers *yet*, this will change soon
152
tm.title = track.title;
153
tm.length = track.duration;
154
music_preview.add_track (tm);
158
GLib.Icon? icon = new GLib.FileIcon (File.new_for_path (Config.DATADIR + "/icons/unity-icon-theme/places/svg/service-u1.svg"));
159
var download_action = new Unity.PreviewAction ("download_album", _("Download"), icon);
161
download_action.activated.connect (download_album);
162
music_preview.add_action (download_action);
164
return music_preview;
167
public override async void perform_search (LensSearch search,
168
SearchType search_type,
169
owned List<FilterParser> filters,
170
int max_results = -1,
171
Cancellable? cancellable = null)
173
if (is_search_empty (search))
177
{ debug ("model has %u rows before search", search.results_model.get_n_rows ());
178
yield collection.search (search,
183
debug ("model has %u rows after search", search.results_model.get_n_rows ());
187
warning ("Failed to search for '%s': %s", search.search_string, e.message);
193
/* Check if a given well known DBus is owned.
194
* WARNING: This does sync IO! */
195
public static bool dbus_name_has_owner (string name)
200
DBusConnection bus = Bus.get_sync (BusType.SESSION);
201
Variant result = bus.call_sync ("org.freedesktop.DBus",
202
"/org/freedesktop/dbus",
203
"org.freedesktop.DBus",
205
new Variant ("(s)", name),
206
new VariantType ("(b)"),
207
DBusCallFlags.NO_AUTO_START,
209
result.get ("(b)", out has_owner);
214
warning ("Unable to decide whether '%s' is running: %s", name, e.message);
220
public static int main (string[] args)
222
GLib.Environment.set_prgname ("unity-musicbaidu-daemon");
224
/* Sort up locale to get translations but also sorting and
225
* punctuation right */
226
GLib.Intl.textdomain (Config.PACKAGE);
227
GLib.Intl.bindtextdomain (Config.PACKAGE, Config.LOCALEDIR);
228
GLib.Intl.bind_textdomain_codeset (Config.PACKAGE, "UTF-8");
229
GLib.Intl.setlocale(GLib.LocaleCategory.ALL, "");
231
/* Workaround for https://bugzilla.gnome.org/show_bug.cgi?id=640714
232
* GApplication.register() call owns our DBus name in a sync manner
233
* making it race against GDBus' worker thread to export our
234
* objects on the bus before/after owning our name and receiving
235
* method calls on our objects (which may not yet be up!)*/
236
if (dbus_name_has_owner ("com.canonical.Unity.Scope.MusicBaidu"))
238
print ("Another instance of the UbuntuOne Music Daemon " +
239
"already appears to be running.\nBailing out.\n");
243
/* Now register our DBus objects *before* acquiring the name!
244
* See above for reasons */
245
daemon = new MusicBaiduScopeProxy ();
247
/* Use GApplication directly for single instance app functionality */
248
app = new Application ("com.canonical.Unity.Scope.MusicBaidu",
249
ApplicationFlags.IS_SERVICE);
256
/* FIXME: We get this error if another daemon is already running,
257
* but it uses a generic error so we can't detect this reliably... */
258
print ("Failed to start music daemon: %s\n", e.message);
262
if (app.get_is_remote ())
264
print ("Another instance of the Unity Music Daemon " +
265
"already appears to be running.\nBailing out.\n");
269
/* Hold()ing the app makes sure the GApplication doesn't exit */