~ubuntu-branches/debian/jessie/banshee-community-extensions/jessie

« back to all changes in this revision

Viewing changes to .pc/0001-Use-DBus-instead-of-NDesk.DBus.patch/src/Telepathy/Banshee.Telepathy/Banshee.Telepathy.DBus/PlaylistProvider.cs

  • Committer: Package Import Robot
  • Author(s): Chow Loong Jin
  • Date: 2011-09-20 18:45:46 UTC
  • mfrom: (1.2.9 upstream) (5.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20110920184546-3ahue2qplydc4t0e
Tags: 2.2.0-1
* [4940fab] Imported Upstream version 2.2.0
  + Notable bug fixes:
    - Karaoke: Fix crash when switching to Now Playing
    - Lyrics: Fix crash when switching to Now Playing

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
// PlaylistProvider.cs
3
 
//
4
 
// Author:
5
 
//   Neil Loknath <neil.loknath@gmail.com>
6
 
//
7
 
// Copyright (C) 2009 Neil Loknath
8
 
//
9
 
// Permission is hereby granted, free of charge, to any person obtaining
10
 
// a copy of this software and associated documentation files (the
11
 
// "Software"), to deal in the Software without restriction, including
12
 
// without limitation the rights to use, copy, modify, merge, publish,
13
 
// distribute, sublicense, and/or sell copies of the Software, and to
14
 
// permit persons to whom the Software is furnished to do so, subject to
15
 
// the following conditions:
16
 
//
17
 
// The above copyright notice and this permission notice shall be
18
 
// included in all copies or substantial portions of the Software.
19
 
//
20
 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
 
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
 
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23
 
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24
 
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25
 
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26
 
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
 
//
28
 
 
29
 
using System;
30
 
using System.Threading;
31
 
using System.Collections.Generic;
32
 
 
33
 
using Banshee.ServiceStack;
34
 
 
35
 
using Banshee.Telepathy.API;
36
 
using Banshee.Telepathy.API.Dispatchables;
37
 
 
38
 
using Hyena.Data.Sqlite;
39
 
 
40
 
using NDesk.DBus;
41
 
 
42
 
namespace Banshee.Telepathy.DBus
43
 
{
44
 
    public class PlaylistProvider : BaseProvider, IPlaylistProvider
45
 
    {
46
 
        private static object class_lock = new object ();
47
 
        private static int instance_count = 0;
48
 
        private int myindex = 0;
49
 
        //private MetadataProviderService myservice;
50
 
        private DBusActivity activity;
51
 
 
52
 
        public PlaylistProvider (DBusActivity activity, int id) : base ()
53
 
        {
54
 
            lock (class_lock) {
55
 
                instance_count++;
56
 
                myindex = instance_count;
57
 
            }
58
 
 
59
 
            //myservice = service;
60
 
            this.activity = activity;
61
 
            Id = id;
62
 
        }
63
 
 
64
 
        public string GetName ()
65
 
        {
66
 
            string name = ServiceManager.DbConnection.Query<string> (
67
 
                "SELECT Name FROM CorePlaylists WHERE PlaylistID = ?", Id
68
 
            );
69
 
 
70
 
            return name ?? "";
71
 
 
72
 
        }
73
 
 
74
 
        public override void GetChunk (long timestamp, int sequence_num)
75
 
        {
76
 
 
77
 
            IDictionary <string, object> [] dict = {};
78
 
 
79
 
            lock (timestamp_lock) {
80
 
                if (timestamp == CurrentTimestamp && buffer != null) {
81
 
                    if (buffer.ContainsKey (sequence_num)) {
82
 
                        dict =  buffer[sequence_num];
83
 
                    }
84
 
                }
85
 
            }
86
 
 
87
 
            OnSingleChunkReady (ObjectPath, dict, timestamp, sequence_num);
88
 
 
89
 
        }
90
 
 
91
 
        public override void GetChunks (int chunk_size)
92
 
        {
93
 
            // mark as critical region to prevent consecutive calls from mucking things up
94
 
            lock (payload_lock) {
95
 
 
96
 
                long timestamp;
97
 
 
98
 
                lock (timestamp_lock) {
99
 
                    if (UseBuffer) {
100
 
                        buffer = new Dictionary <int, IDictionary <string, object> []> ();
101
 
                    }
102
 
                    timestamp = DateTime.Now.Ticks;
103
 
                    CurrentTimestamp = timestamp;
104
 
                }
105
 
 
106
 
                int total = ServiceManager.DbConnection.Query<int> (
107
 
                    "SELECT COUNT(*) FROM CorePlaylistEntries WHERE PlaylistID = ?", Id
108
 
                );
109
 
 
110
 
                if (total == 0) {
111
 
                    IDictionary <string, object> [] empty = {};         // d-bus no like nulls
112
 
                    OnChunkReady (ObjectPath, empty, timestamp, 1, 0);
113
 
                    return;
114
 
                }
115
 
 
116
 
                chunk_size = chunk_size < 1 ? 100 : chunk_size;         // default chunk_size
117
 
 
118
 
                HyenaDataReader reader = new HyenaDataReader (ServiceManager.DbConnection.Query (
119
 
                    "SELECT TrackID FROM CorePlaylistEntries WHERE PlaylistID = ?", Id)
120
 
                );
121
 
 
122
 
                // deliver data asynchronously via signal in chunks of chunk_size
123
 
                // this should make things look like they are happening quickly over our tube
124
 
                int sequence_num = 1;
125
 
 
126
 
                for (int i = 0; i < total; i += chunk_size) {
127
 
                    int dict_size = (total - i) < chunk_size ? (total - i) : chunk_size;
128
 
                    IDictionary <string, object> [] dict  = new Dictionary <string, object> [dict_size];
129
 
 
130
 
                    for (int j = 0; j < dict.Length; j++) {
131
 
                        dict[j] = new Dictionary <string, object> ();
132
 
                        if (reader.Read ()) {
133
 
                            dict[j].Add ("TrackID", reader.Get <int> (0));
134
 
                        }
135
 
                    }
136
 
 
137
 
                    if (UseBuffer) {
138
 
                        buffer.Add (sequence_num, dict);
139
 
                    }
140
 
 
141
 
                    OnChunkReady (ObjectPath, dict, timestamp, sequence_num++, total);
142
 
 
143
 
                }
144
 
            }
145
 
        }
146
 
 
147
 
        void IPlaylistProvider.GetChunks (int chunk_size)
148
 
        {
149
 
            ThreadPool.QueueUserWorkItem ( delegate { GetChunks (chunk_size); } );
150
 
        }
151
 
 
152
 
        void IPlaylistProvider.Destroy ()
153
 
        {
154
 
            activity.UnRegisterDBusObject (ObjectPath);
155
 
            Dispose ();
156
 
        }
157
 
 
158
 
        protected override void Dispose (bool disposing)
159
 
        {
160
 
            //instance_count--;
161
 
            base.Dispose (disposing);
162
 
        }
163
 
 
164
 
        public static string BusName {
165
 
            get { return "org.bansheeproject.Banshee"; }
166
 
        }
167
 
 
168
 
        public string ObjectPath {
169
 
            get { return "/org/bansheeproject/PlaylistProvider_" + myindex; }
170
 
        }
171
 
    }
172
 
}