~karol-bedkowski/do-plugins/putty-fix-324282_load_parameter

« back to all changes in this revision

Viewing changes to Banshee/src/Banshee.cs

  • Committer: Alex Launi
  • Date: 2009-01-23 02:23:36 UTC
  • mfrom: (276.4.12 bansheeplugin)
  • Revision ID: alex.launi@gmail.com-20090123022336-z0hf1ycf7pvg5i0d
Add full Banshee plugin

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
 
39
39
        public class Banshee
40
40
        {
41
 
                static BansheeDbus bus;
42
 
 
43
 
                static Banshee ()
44
 
                {
45
 
                        bus = new BansheeDbus ();
46
 
                }
47
 
 
48
 
                public static bool Playing {
49
 
                        get { return bus.Playing; }
 
41
                static BansheeDBus bus;
 
42
                static BansheeIndexer indexer;
 
43
                
 
44
                static Banshee()
 
45
                {
 
46
                        bus = new BansheeDBus ();
 
47
                        indexer = new BansheeIndexer ();
 
48
                }
 
49
 
 
50
                public static void Index ()
 
51
                {
 
52
                        indexer.Start ();
 
53
                }
 
54
 
 
55
                public static bool IsPlaying {
 
56
                        get { return bus.IsPlaying (); }
 
57
                }
 
58
 
 
59
                public static void Enqueue (MediaItem item)
 
60
                {
 
61
                        Services.Application.RunOnThread (() => bus.Enqueue (LoadMedia (item)));
50
62
                }
51
63
 
52
64
                public static void Play ()
53
65
                {
54
66
                        Services.Application.RunOnThread (bus.Play);
55
67
                }
 
68
                
 
69
                public static void Play (MediaItem item)
 
70
                {
 
71
                        Services.Application.RunOnThread (() => bus.Play (LoadMedia (item)));
 
72
                }
56
73
 
57
74
                public static void Pause ()
58
75
                {
68
85
                {
69
86
                        Services.Application.RunOnThread (bus.Previous);
70
87
                }
 
88
 
 
89
                public static PlaybackShuffleMode ShuffleMode {
 
90
                        set { bus.ShuffleMode = value; }
 
91
                }
 
92
 
 
93
                public static IEnumerable<IMediaFile> LoadMedia (MediaItem item)
 
94
                {
 
95
                        if (item is MusicItem)
 
96
                                return LoadSongsFor (item as MusicItem);
 
97
                        else if (item is PodcastItem)
 
98
                                return LoadPodcastsFor (item as PodcastItem);
 
99
                        else if (item is VideoItem)
 
100
                                return indexer.Videos as IEnumerable<IMediaFile>;
 
101
                        else
 
102
                                return Enumerable.Empty<IMediaFile> ();
 
103
                }
 
104
 
 
105
                public static void LoadVideos (out List<VideoItem> videos)
 
106
                {
 
107
                        videos = new List<VideoItem> (indexer.Videos);
 
108
                }
 
109
                
 
110
                public static List<IMediaFile> SearchMedia (string pattern)
 
111
                {
 
112
                        List<IMediaFile> results = new List<IMediaFile> ();
 
113
                        
 
114
                        results.AddRange (indexer.Songs.Where (item => ContainsMatch (item, pattern)).Cast<IMediaFile> ());
 
115
                        results.AddRange (indexer.Videos.Where (item => ContainsMatch (item, pattern)).Cast<IMediaFile> ());
 
116
                        results.AddRange (indexer.Podcasts.Where (item => ContainsMatch (item, pattern)).Cast<IMediaFile> ());
 
117
 
 
118
                        return results;
 
119
                }
 
120
 
 
121
                public static void LoadPodcasts (out List<PodcastItem> podcastsOut)
 
122
                {
 
123
                        Dictionary<string, PodcastItem> publishers;
 
124
                        
 
125
                        podcastsOut = new List<PodcastItem> ();
 
126
                        publishers = new Dictionary<string, PodcastItem> ();
 
127
                
 
128
                        foreach (PodcastPodcastItem podcast in indexer.Podcasts) {
 
129
                                if (!publishers.ContainsKey (podcast.Artist))
 
130
                                        publishers[podcast.Artist] = new PodcastPublisherItem (
 
131
                                                podcast.Artist, podcast.Year, podcast.Cover);
 
132
                        }
 
133
                        
 
134
                        podcastsOut.AddRange (publishers.Values);
 
135
                }
 
136
                
 
137
                public static void LoadAlbumsAndArtists (out List<AlbumMusicItem> albumsOut, out List<ArtistMusicItem> artistsOut)
 
138
                {
 
139
                        Dictionary<string, AlbumMusicItem>  albums;
 
140
                        Dictionary<string, ArtistMusicItem> artists;
 
141
                        
 
142
                        albumsOut = new List<AlbumMusicItem> ();
 
143
                        artistsOut = new List<ArtistMusicItem> ();
 
144
                        
 
145
                        albums = new Dictionary<string, AlbumMusicItem> ();
 
146
                        artists = new Dictionary<string,ArtistMusicItem> ();
 
147
                        
 
148
                        foreach (SongMusicItem song in indexer.Songs) {
 
149
                                if (!artists.ContainsKey (song.Artist))
 
150
                                        artists[song.Artist] = new ArtistMusicItem (song.Artist, song.Cover);
 
151
                                        
 
152
                                if (!albums.ContainsKey (song.Album))
 
153
                                        albums[song.Album] = new AlbumMusicItem (song.Album, song.Artist, song.Year,
 
154
                                                song.Cover);
 
155
                        }
 
156
                        
 
157
                        albumsOut.AddRange (albums.Values);
 
158
                        artistsOut.AddRange (artists.Values);
 
159
                }
 
160
 
 
161
                public static IEnumerable<AlbumMusicItem> LoadAlbumsFor (ArtistMusicItem artist, IEnumerable<AlbumMusicItem> albums)
 
162
                {
 
163
                        return albums.Where (album => album.Artist.Contains (artist.Name));
 
164
                }
 
165
 
 
166
                static List<IMediaFile> LoadSongsFor (MusicItem item)
 
167
                {
 
168
                        SortedList<string, IMediaFile> albumSongs;
 
169
                        string key;
 
170
                        
 
171
                        if (item is SongMusicItem) {
 
172
                                List<IMediaFile> single = new List<IMediaFile> ();
 
173
                                single.Add (item as SongMusicItem);
 
174
                                return single;
 
175
                        }
 
176
                        
 
177
                        albumSongs = new SortedList<string, IMediaFile> ();
 
178
                        foreach (SongMusicItem song in indexer.Songs) {
 
179
                                switch (item.GetType ().Name) {
 
180
                                case "AlbumMusicItem":
 
181
                                        if (item.Name != song.Album) continue;
 
182
                                        break;
 
183
                                case "ArtistMusicItem":
 
184
                                        if (item.Name != song.Artist) continue;
 
185
                                        break;
 
186
                                }
 
187
                                
 
188
                                key = string.Format ("{0}-{1}-{2}", song.Album, song.Track, song.Path);
 
189
                                try {
 
190
                                        if (albumSongs.ContainsKey (key)) continue;
 
191
                                        albumSongs.Add (key, song);
 
192
                                } catch (Exception e) {
 
193
                                        Log.Error ("{0} : {1}", key, e.Message);
 
194
                                }
 
195
                        }
 
196
                        
 
197
                        return new List<IMediaFile> (albumSongs.Values);
 
198
                }
 
199
 
 
200
                static IEnumerable<IMediaFile> LoadPodcastsFor (PodcastItem item)
 
201
                {                       
 
202
                        if (item is PodcastPodcastItem) {
 
203
                                yield return item as IMediaFile;
 
204
                        }
 
205
                        
 
206
                        foreach (PodcastPodcastItem pc in indexer.Podcasts) {
 
207
                                if ((item as PodcastPublisherItem).Name != pc.Artist) continue;
 
208
                                
 
209
                                yield return pc;
 
210
                        }
 
211
                }
 
212
 
 
213
 
 
214
                static bool ContainsMatch (MediaItem item, string pattern)
 
215
                {
 
216
                        return ContainsMatch (item, p => PropertyInfoMatchesPattern (item, p, pattern));
 
217
                }
 
218
 
 
219
                static bool ContainsMatch (MediaItem item, Func<PropertyInfo, bool> predicate)
 
220
                {
 
221
                        return item.GetType ().GetProperties ().Any (predicate);
 
222
                }
 
223
 
 
224
                static bool PropertyInfoMatchesPattern (MediaItem item, PropertyInfo info, string pattern)
 
225
                {
 
226
                        return (info.Name != "File" && (info.GetValue (item, null).ToString ().Contains (pattern)));
 
227
                }
71
228
        }
72
229
}