5
// Neil Loknath <neil.loknath@gmail.com>
7
// Copyright (C) 2009 Neil Loknath
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:
17
// The above copyright notice and this permission notice shall be
18
// included in all copies or substantial portions of the Software.
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.
30
using System.Threading;
31
using System.Collections.Generic;
33
using Banshee.ServiceStack;
35
using Banshee.Telepathy.API;
36
using Banshee.Telepathy.API.Dispatchables;
38
using Hyena.Data.Sqlite;
42
namespace Banshee.Telepathy.DBus
44
public class PlaylistProvider : BaseProvider, IPlaylistProvider
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;
52
public PlaylistProvider (DBusActivity activity, int id) : base ()
56
myindex = instance_count;
59
//myservice = service;
60
this.activity = activity;
64
public string GetName ()
66
string name = ServiceManager.DbConnection.Query<string> (
67
"SELECT Name FROM CorePlaylists WHERE PlaylistID = ?", Id
74
public override void GetChunk (long timestamp, int sequence_num)
77
IDictionary <string, object> [] dict = {};
79
lock (timestamp_lock) {
80
if (timestamp == CurrentTimestamp && buffer != null) {
81
if (buffer.ContainsKey (sequence_num)) {
82
dict = buffer[sequence_num];
87
OnSingleChunkReady (ObjectPath, dict, timestamp, sequence_num);
91
public override void GetChunks (int chunk_size)
93
// mark as critical region to prevent consecutive calls from mucking things up
98
lock (timestamp_lock) {
100
buffer = new Dictionary <int, IDictionary <string, object> []> ();
102
timestamp = DateTime.Now.Ticks;
103
CurrentTimestamp = timestamp;
106
int total = ServiceManager.DbConnection.Query<int> (
107
"SELECT COUNT(*) FROM CorePlaylistEntries WHERE PlaylistID = ?", Id
111
IDictionary <string, object> [] empty = {}; // d-bus no like nulls
112
OnChunkReady (ObjectPath, empty, timestamp, 1, 0);
116
chunk_size = chunk_size < 1 ? 100 : chunk_size; // default chunk_size
118
HyenaDataReader reader = new HyenaDataReader (ServiceManager.DbConnection.Query (
119
"SELECT TrackID FROM CorePlaylistEntries WHERE PlaylistID = ?", Id)
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;
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];
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));
138
buffer.Add (sequence_num, dict);
141
OnChunkReady (ObjectPath, dict, timestamp, sequence_num++, total);
147
void IPlaylistProvider.GetChunks (int chunk_size)
149
ThreadPool.QueueUserWorkItem ( delegate { GetChunks (chunk_size); } );
152
void IPlaylistProvider.Destroy ()
154
activity.UnRegisterDBusObject (ObjectPath);
158
protected override void Dispose (bool disposing)
161
base.Dispose (disposing);
164
public static string BusName {
165
get { return "org.bansheeproject.Banshee"; }
168
public string ObjectPath {
169
get { return "/org/bansheeproject/PlaylistProvider_" + myindex; }