~pishuilu1128/unity-china-music-scope/unity-china-music-scope

« back to all changes in this revision

Viewing changes to src/musicbaidu-daemon.vala

  • Committer: Package Import Robot
  • Author(s): whzhang-kylin
  • Date: 2012-12-13 09:29:34 UTC
  • Revision ID: package-import@ubuntu.com-20121213092934-0z4qywr7i1fp6nsy
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2013 Kylin Ltd
 
3
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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/>.
 
15
 *
 
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
 
20
 */
 
21
 
 
22
/*
 
23
 * Copyright (C) 2011 Canonical Ltd
 
24
 *
 
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.
 
28
 *
 
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.
 
33
 *
 
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/>.
 
36
 *
 
37
 * Authored by Alex Launi <alex.launi@canonical.com>
 
38
 *
 
39
 */
 
40
using GLib;
 
41
using Config;
 
42
 
 
43
namespace Unity.MusicLens
 
44
{
 
45
 
 
46
static Application? app = null;
 
47
static MusicBaiduScopeProxy? daemon = null;
 
48
 
 
49
 
 
50
public class MusicBaiduScopeProxy : SimpleScope
 
51
{
 
52
    private MusicBaiduCollection collection;
 
53
    private Unity.MusicPreview? music_preview;
 
54
 
 
55
    public MusicBaiduScopeProxy ()
 
56
    {
 
57
        base ();
 
58
 
 
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);
 
63
 
 
64
        base.initialize ();
 
65
 
 
66
        collection = new MusicBaiduCollection ();
 
67
 
 
68
        try
 
69
        {
 
70
            scope.export ();
 
71
        }
 
72
        catch (GLib.IOError e)
 
73
        {
 
74
            stdout.printf (e.message);
 
75
        }
 
76
 
 
77
    }
 
78
 
 
79
    protected override int num_results_without_search { get { return 100;
 
80
                                                            }
 
81
                                                      }
 
82
    protected override int num_results_global_search { get { return 20;
 
83
                                                           }
 
84
                                                     }
 
85
    protected override int num_results_lens_search { get { return 50;
 
86
                                                         }
 
87
                                                   }
 
88
 
 
89
 
 
90
    public Unity.ActivationResponse activate (string uri)
 
91
    {
 
92
        /* launch the music baidu streaming client */
 
93
        try
 
94
        {
 
95
 
 
96
            if(uri.has_prefix ("song://"))
 
97
            {
 
98
                AppInfo.launch_default_for_uri (uri.substring (7), null);//strip off "song://" from the uri
 
99
            }
 
100
            else if(uri.has_prefix ("album://"))
 
101
            {
 
102
                AppInfo.launch_default_for_uri (uri.substring (8), null);//strip off "album://" from the uri
 
103
            }
 
104
        }
 
105
        catch (GLib.Error e)
 
106
        {
 
107
            warning ("Failed to open uri %s. %s", uri, e.message);
 
108
        }
 
109
        return new Unity.ActivationResponse (Unity.HandledType.HIDE_DASH);
 
110
    }
 
111
 
 
112
    public Unity.ActivationResponse download_album (string uri)
 
113
    {
 
114
        return activate (uri);
 
115
    }
 
116
 
 
117
    public Unity.Preview preview (string uri)
 
118
    {
 
119
        music_preview = null;
 
120
        Album album = null;
 
121
        SList<Track> tracks = null;
 
122
 
 
123
 
 
124
        if(uri.has_prefix ("album://"))
 
125
        {
 
126
            collection.get_album_details(uri,  out album, out tracks,null);
 
127
        }
 
128
        else if(uri.has_prefix ("song://"))
 
129
        {
 
130
            collection.get_song_details (uri, out album, out tracks,null);
 
131
        }
 
132
 
 
133
        if (album != null)
 
134
        {
 
135
 
 
136
            GLib.Icon? icon_file = null;
 
137
            int i = 1;
 
138
 
 
139
 
 
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);
 
143
 
 
144
 
 
145
            if (tracks != null)
 
146
            {
 
147
                foreach (Track track in tracks)
 
148
                {
 
149
                    TrackMetadata tm = new TrackMetadata ();
 
150
                    tm.uri = track.uri;
 
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);
 
155
                }
 
156
            }
 
157
 
 
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);
 
160
 
 
161
            download_action.activated.connect (download_album);
 
162
            music_preview.add_action (download_action);
 
163
        }
 
164
        return music_preview;
 
165
    }
 
166
 
 
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)
 
172
    {
 
173
        if (is_search_empty (search))
 
174
            return;
 
175
 
 
176
        try
 
177
        {   debug ("model has %u rows before search", search.results_model.get_n_rows ());
 
178
            yield collection.search (search,
 
179
                                     search_type,
 
180
                                     (owned) filters,
 
181
                                     max_results,
 
182
                                     cancellable);
 
183
           debug ("model has %u rows after search", search.results_model.get_n_rows ());
 
184
        }
 
185
        catch (IOError e)
 
186
        {
 
187
            warning ("Failed to search for '%s': %s", search.search_string, e.message);
 
188
        }
 
189
    }
 
190
 
 
191
}
 
192
 
 
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)
 
196
{
 
197
    try
 
198
    {
 
199
        bool has_owner;
 
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",
 
204
                                        "NameHasOwner",
 
205
                                        new Variant ("(s)", name),
 
206
                                        new VariantType ("(b)"),
 
207
                                        DBusCallFlags.NO_AUTO_START,
 
208
                                        -1);
 
209
        result.get ("(b)", out has_owner);
 
210
        return has_owner;
 
211
    }
 
212
    catch (Error e)
 
213
    {
 
214
        warning ("Unable to decide whether '%s' is running: %s", name, e.message);
 
215
    }
 
216
    return false;
 
217
}
 
218
 
 
219
 
 
220
public static int main (string[] args)
 
221
{
 
222
    GLib.Environment.set_prgname ("unity-musicbaidu-daemon");
 
223
 
 
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, "");
 
230
 
 
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"))
 
237
    {
 
238
        print ("Another instance of the UbuntuOne Music Daemon " +
 
239
               "already appears to be running.\nBailing out.\n");
 
240
        return 2;
 
241
    }
 
242
 
 
243
    /* Now register our DBus objects *before* acquiring the name!
 
244
     * See above for reasons */
 
245
    daemon = new MusicBaiduScopeProxy ();
 
246
 
 
247
    /* Use GApplication directly for single instance app functionality */
 
248
    app = new Application ("com.canonical.Unity.Scope.MusicBaidu",
 
249
                           ApplicationFlags.IS_SERVICE);
 
250
    try
 
251
    {
 
252
        app.register ();
 
253
    }
 
254
    catch (Error e)
 
255
    {
 
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);
 
259
        return 1;
 
260
    }
 
261
 
 
262
    if (app.get_is_remote ())
 
263
    {
 
264
        print ("Another instance of the Unity Music Daemon " +
 
265
               "already appears to be running.\nBailing out.\n");
 
266
        return 2;
 
267
    }
 
268
 
 
269
    /* Hold()ing the app makes sure the GApplication doesn't exit */
 
270
    app.hold();
 
271
    return app.run ();
 
272
}
 
273
 
 
274
} /* namespace */