root/lang/csharp/iTunesCOMWrap/trunk/iTunesCOMWrap/Application.cs @ 11205

Revision 11205, 21.1 kB (checked in by topia, 7 years ago)

* trivial fixes.
* add TODO.

  • Property svn:eol-style set to CRLF
  • Property svn:keywords set to Id URL Date Rev Author
Line 
1// $Id$
2using System;
3using System.Collections;
4using System.Collections.Generic;
5using System.Linq;
6using System.Runtime.InteropServices;
7using Clovery.iTunesHelper.COMWrap.Interfaces;
8using iTunes = iTunesLib;
9
10namespace Clovery.iTunesHelper.COMWrap
11{
12    public class ITunesApp : IITFactory
13    {
14        private readonly IITunesWrappedObjectFactory _objectFactory;
15        protected internal object _internal;
16        public IIndexedCollection<IPlaylist, bool> CanSetShuffle = new CanSetShufflePlaylistCollection();
17        public IIndexedCollection<IPlaylist, bool> CanSetSongRepeat = new CanSetSongRepeatPlaylistCollection();
18
19        internal ITunesApp(iTunes.iTunesApp _internal, bool useCache)
20        {
21            this._internal = _internal;
22            if (!this._asIiTunes.CheckVersion(
23                     (int) SupportedVersion.MajorVersion,
24                     (int) SupportedVersion.MinorVersion))
25                throw new NotSupportedException("not supported iTunes version");
26
27            if (useCache)
28            {
29                this._objectFactory = new ITunesWrappedCachedObjectFactory(this);
30                this.AttachEvents();
31            }
32            else
33            {
34                this._objectFactory = new ITunesWrappedObjectFactory(this);
35            }
36        }
37
38        public ITunesApp(bool useCache)
39            : this(new iTunes.iTunesAppClass(), useCache)
40        {
41        }
42
43        public ITunesApp() : this(false)
44        {
45        }
46
47        protected internal iTunes.IiTunes _asIiTunes
48        {
49            get { return (iTunes.IiTunes) this._internal; }
50        }
51
52        protected internal iTunes._IiTunesEvents_Event _asIiTunesEvents_Event
53        {
54            get { return (iTunes._IiTunesEvents_Event) this._internal; }
55        }
56
57        public object __internalObject
58        {
59            get { return this._internal; }
60        }
61
62        public bool Attached
63        {
64            get { return this._internal != null; }
65        }
66
67        public bool EventAttached { get; private set; }
68
69        public void AttachEvents()
70        {
71            if (this.EventAttached) return;
72            this.EventAttached = true;
73            this._asIiTunesEvents_Event.OnDatabaseChangedEvent += this._onITDatabaseChangedEvent;
74            this._asIiTunesEvents_Event.OnPlayerPlayEvent += this._onPlayerPlayEvent;
75            this._asIiTunesEvents_Event.OnPlayerStopEvent += this._onPlayerStopEvent;
76            this._asIiTunesEvents_Event.OnPlayerPlayingTrackChangedEvent += this._onPlayerPlayingTrackChangedEvent;
77            this._asIiTunesEvents_Event.OnCOMCallsDisabledEvent += this._onCOMCallsDisabledEvent;
78            this._asIiTunesEvents_Event.OnCOMCallsEnabledEvent += this._onCOMCallsEnabledEvent;
79            this._asIiTunesEvents_Event.OnQuittingEvent += this._onQuittingEvent;
80            this._asIiTunesEvents_Event.OnAboutToPromptUserToQuitEvent += this._onAbortToPromptUserToQuitEvent;
81            this._asIiTunesEvents_Event.OnSoundVolumeChangedEvent += this._onSoundVolumeChangedEvent;
82            var e = this.EventAttachStatusChangedEvent;
83            if (e != null) e(this, new EventAttachStatusChangedEventArgs(this.EventAttached));
84        }
85
86        public void DetachEvents()
87        {
88            if (!this.EventAttached) return;
89            this.EventAttached = false;
90            this._asIiTunesEvents_Event.OnDatabaseChangedEvent -= this._onITDatabaseChangedEvent;
91            this._asIiTunesEvents_Event.OnPlayerPlayEvent -= this._onPlayerPlayEvent;
92            this._asIiTunesEvents_Event.OnPlayerStopEvent -= this._onPlayerStopEvent;
93            this._asIiTunesEvents_Event.OnPlayerPlayingTrackChangedEvent -= this._onPlayerPlayingTrackChangedEvent;
94            this._asIiTunesEvents_Event.OnCOMCallsDisabledEvent -= this._onCOMCallsDisabledEvent;
95            this._asIiTunesEvents_Event.OnCOMCallsEnabledEvent -= this._onCOMCallsEnabledEvent;
96            this._asIiTunesEvents_Event.OnQuittingEvent -= this._onQuittingEvent;
97            this._asIiTunesEvents_Event.OnAboutToPromptUserToQuitEvent -= this._onAbortToPromptUserToQuitEvent;
98            this._asIiTunesEvents_Event.OnSoundVolumeChangedEvent -= this._onSoundVolumeChangedEvent;
99            var e = this.EventAttachStatusChangedEvent;
100            if (e != null) e(this, new EventAttachStatusChangedEventArgs(this.EventAttached));
101        }
102
103        public ISourceCollection Sources
104        {
105            get { return this.factory.CreateSourceCollection(this._asIiTunes.Sources); }
106        }
107
108        public IEncoderCollection Encoders
109        {
110            get { return this.factory.CreateEncoderCollection(this._asIiTunes.Encoders); }
111        }
112
113        public IEQPresetCollection EQPresets
114        {
115            get { return this.factory.CreateEQPresetCollection(this._asIiTunes.EQPresets); }
116        }
117
118        public IVisualCollection Visuals
119        {
120            get { return this.factory.CreateVisualCollection(this._asIiTunes.Visuals); }
121        }
122
123        public IWindowCollection Windows
124        {
125            get { return this.factory.CreateWindowCollection(this._asIiTunes.Windows); }
126        }
127
128        public int SoundVolume
129        {
130            get { return this._asIiTunes.SoundVolume; }
131            set { this._asIiTunes.SoundVolume = value; }
132        }
133
134        public bool Mute
135        {
136            get { return this._asIiTunes.Mute; }
137            set { this._asIiTunes.Mute = value; }
138        }
139
140        public PlayerState PlayerState
141        {
142            get { return (PlayerState) this._asIiTunes.PlayerState; }
143        }
144
145        public int PlayerPosition
146        {
147            get { return this._asIiTunes.PlayerPosition; }
148            set { this._asIiTunes.PlayerPosition = value; }
149        }
150
151        public IEncoder CurrentEncoder
152        {
153            get { return this.factory.CreateEncoder(this._asIiTunes.CurrentEncoder); }
154            set { this._asIiTunes.CurrentEncoder = value.__asIITEncoder; }
155        }
156
157        public bool VisualsEnabled
158        {
159            get { return this._asIiTunes.VisualsEnabled; }
160            set { this._asIiTunes.VisualsEnabled = value; }
161        }
162
163        public bool FullScreenVisuals
164        {
165            get { return this._asIiTunes.FullScreenVisuals; }
166            set { this._asIiTunes.FullScreenVisuals = value; }
167        }
168
169        public VisualSize VisualSize
170        {
171            get { return (VisualSize) this._asIiTunes.VisualSize; }
172            set { this._asIiTunes.VisualSize = (iTunes.ITVisualSize) value; }
173        }
174
175        public IVisual CurrentVisual
176        {
177            get { return this.factory.CreateVisual(this._asIiTunes.CurrentVisual); }
178            set { this._asIiTunes.CurrentVisual = value.__asIITVisual; }
179        }
180
181        public bool EQEnabled
182        {
183            get { return this._asIiTunes.EQEnabled; }
184            set { this._asIiTunes.EQEnabled = value; }
185        }
186
187        public IEQPreset CurrentEQPreset
188        {
189            get { return this.factory.CreateEQPreset(this._asIiTunes.CurrentEQPreset); }
190            set { this._asIiTunes.CurrentEQPreset = value.__asIITEQPreset; }
191        }
192
193        public string CurrentStreamTitle
194        {
195            get { return this._asIiTunes.CurrentStreamTitle; }
196        }
197
198        public string CurrentStreamURL
199        {
200            get { return this._asIiTunes.CurrentStreamURL; }
201        }
202
203        public IBrowserWindow BrowserWindow
204        {
205            get { return this.factory.CreateBrowserWindow(this._asIiTunes.BrowserWindow); }
206        }
207
208        public IWindow EQWindow
209        {
210            get { return this.factory.CreateWindow(this._asIiTunes.EQWindow); }
211        }
212
213        public ISource LibrarySource
214        {
215            get { return this.factory.CreateSource(this._asIiTunes.LibrarySource); }
216        }
217
218        public ILibraryPlaylist LibraryPlaylist
219        {
220            get { return this.factory.CreateLibraryPlaylist(this._asIiTunes.LibraryPlaylist); }
221        }
222
223        public ITrack CurrentTrack
224        {
225            get { return this.factory.CreateTrack(this._asIiTunes.CurrentTrack); }
226        }
227
228        public IPlaylist CurrentPlaylist
229        {
230            get { return this.factory.CreatePlaylist(this._asIiTunes.CurrentPlaylist); }
231        }
232
233        public ITrackCollection SelectedTracks
234        {
235            get { return this.factory.CreateTrackCollection(this._asIiTunes.SelectedTracks); }
236        }
237
238        public string Version
239        {
240            get { return this._asIiTunes.Version; }
241        }
242
243        public bool AppCommandMessageProcessingEnabled
244        {
245            get { return this._asIiTunes.AppCommandMessageProcessingEnabled; }
246            set { this._asIiTunes.AppCommandMessageProcessingEnabled = value; }
247        }
248
249        public bool ForceToForegroundOnDialog
250        {
251            get { return this._asIiTunes.ForceToForegroundOnDialog; }
252            set { this._asIiTunes.ForceToForegroundOnDialog = value; }
253        }
254
255        public IConvertOperationStatusWithEvents ConvertOperationStatus
256        {
257            get { return this.factory.CreateConvertOperationStatus(this._asIiTunes.ConvertOperationStatus); }
258        }
259
260        public bool SoundVolumeControlEnabled
261        {
262            get { return this._asIiTunes.SoundVolumeControlEnabled; }
263        }
264
265        public string LibraryXMLPath
266        {
267            get { return this._asIiTunes.LibraryXMLPath; }
268        }
269
270        public IITunesWrappedObjectFactory factory
271        {
272            get { return this._objectFactory; }
273        }
274
275        // ~ITunesApp()
276        // {
277        //     //this.DetachEvents();
278        // }
279
280        private void _onITDatabaseChangedEvent(object deletedObjectIDs, object changedObjectIDs)
281        {
282            var deletedObjects = new List<IITObject>();
283            if (deletedObjectIDs != null)
284            {
285                var ObjectIDs = deletedObjectIDs as object[,];
286                if (ObjectIDs != null)
287                {
288                    for (int i = ObjectIDs.GetLowerBound(0); i <= ObjectIDs.GetUpperBound(0); i++)
289                        deletedObjects.Add(
290                            this.factory.GetCacheOrAbstractObject<ITObject>(
291                                (int) ObjectIDs[i, 0], (int) ObjectIDs[i, 1],
292                                (int) ObjectIDs[i, 2], (int) ObjectIDs[i, 3]));
293                }
294            }
295            var changedObjects = new List<IITObject>();
296            if (changedObjectIDs != null)
297            {
298                var ObjectIDs = changedObjectIDs as object[,];
299                if (ObjectIDs != null)
300                {
301                    for (int i = ObjectIDs.GetLowerBound(0); i <= ObjectIDs.GetUpperBound(0); i++)
302                        changedObjects.Add(
303                            this.GetITObjectByID(
304                                (int) ObjectIDs[i, 0], (int) ObjectIDs[i, 1],
305                                (int) ObjectIDs[i, 2], (int) ObjectIDs[i, 3]));
306                }
307            }
308            var e = this.DatabaseChangedEvent;
309            if (e != null)
310                e(this,
311                  new DatabaseChangedEventArgs(deletedObjects.ToArray(), changedObjects.ToArray()));
312        }
313
314        private void _onPlayerPlayEvent(object track)
315        {
316            var e = this.PlayerPlayEvent;
317            if (e != null) e(this, new TrackEventArgs(this.factory.CreateTrack(track as iTunes.IITTrack)));
318        }
319
320        private void _onPlayerStopEvent(object track)
321        {
322            var e = this.PlayerStopEvent;
323            if (e != null) e(this, new TrackEventArgs(this.factory.CreateTrack(track as iTunes.IITTrack)));
324        }
325
326        private void _onPlayerPlayingTrackChangedEvent(object track)
327        {
328            var e = this.PlayerPlayingTrackChangedEvent;
329            if (e != null)
330                e(this, new TrackEventArgs(this.factory.CreateTrack(track as iTunes.IITTrack)));
331        }
332
333        private void _onCOMCallsDisabledEvent(iTunes.ITCOMDisabledReason reason)
334        {
335            var e = this.COMCallsDisabledEvent;
336            if (e != null) e(this, new COMCallsDisabledEventArgs((COMDisabledReason) reason));
337        }
338
339        private void _onCOMCallsEnabledEvent()
340        {
341            var e = this.COMCallsEnabledEvent;
342            if (e != null) e(this, EventArgs.Empty);
343        }
344
345        private void _onQuittingEvent()
346        {
347            var e = this.QuittingEvent;
348            if (e != null) e(this, EventArgs.Empty);
349        }
350
351        private void _onAbortToPromptUserToQuitEvent()
352        {
353            var e = this.AbortToPromptUserToQuitEvent;
354            if (e != null) e(this, EventArgs.Empty);
355        }
356
357        private void _onSoundVolumeChangedEvent(int newVolume)
358        {
359            var e = this.SoundVolumeChangedEvent;
360            if (e != null) e(this, new SoundVolumeChangedEventArgs(newVolume));
361        }
362
363        static private object _convertToITObjects(IITInternalObject track)
364        {
365            return track.__internalObject;
366        }
367
368        public event EventHandler<DatabaseChangedEventArgs> DatabaseChangedEvent;
369        public event EventHandler<TrackEventArgs> PlayerPlayEvent;
370        public event EventHandler<TrackEventArgs> PlayerStopEvent;
371        public event EventHandler<TrackEventArgs> PlayerPlayingTrackChangedEvent;
372        public event EventHandler<COMCallsDisabledEventArgs> COMCallsDisabledEvent;
373        public event EventHandler<EventArgs> COMCallsEnabledEvent;
374        public event EventHandler<EventArgs> QuittingEvent;
375        public event EventHandler<EventArgs> AbortToPromptUserToQuitEvent;
376        public event EventHandler<SoundVolumeChangedEventArgs> SoundVolumeChangedEvent;
377        public event EventHandler<EventAttachStatusChangedEventArgs> EventAttachStatusChangedEvent;
378
379        public void BackTrack()
380        {
381            this._asIiTunes.BackTrack();
382        }
383
384        public void FastForward()
385        {
386            this._asIiTunes.FastForward();
387        }
388
389        public void NextTrack()
390        {
391            this._asIiTunes.NextTrack();
392        }
393
394        public void Pause()
395        {
396            this._asIiTunes.Pause();
397        }
398
399        public void Play()
400        {
401            this._asIiTunes.Play();
402        }
403
404        public void PlayFile(string filePath)
405        {
406            this._asIiTunes.PlayFile(filePath);
407        }
408
409        public void PlayPause()
410        {
411            this._asIiTunes.PlayPause();
412        }
413
414        public void PreviousTrack()
415        {
416            this._asIiTunes.PreviousTrack();
417        }
418
419        public void Resume()
420        {
421            this._asIiTunes.Resume();
422        }
423
424        public void Rewind()
425        {
426            this._asIiTunes.Rewind();
427        }
428
429        public void Stop()
430        {
431            this._asIiTunes.Stop();
432        }
433
434        public IConvertOperationStatusWithEvents ConvertFile(string filePath)
435        {
436            return this.factory.CreateConvertOperationStatus(this._asIiTunes.ConvertFile2(filePath));
437        }
438
439        public IConvertOperationStatusWithEvents ConvertFiles(string[] filePaths)
440        {
441            object real = filePaths;
442            return this.factory.CreateConvertOperationStatus(this._asIiTunes.ConvertFiles2(ref real));
443        }
444
445        public IConvertOperationStatusWithEvents ConvertTrack(ITrack iTrackToConvert)
446        {
447            object real = _convertToITObjects(iTrackToConvert);
448            return this.factory.CreateConvertOperationStatus(this._asIiTunes.ConvertTrack2(ref real));
449        }
450
451        public IConvertOperationStatusWithEvents ConvertTracks(ITrack[] iTracksToConvert)
452        {
453            object real =
454                iTracksToConvert.ToList().ConvertAll(
455                    new Converter<ITrack, object>(_convertToITObjects)).ToArray();
456            return this.factory.CreateConvertOperationStatus(this._asIiTunes.ConvertTracks2(ref real));
457        }
458
459        public bool CheckVersion(int majorVersion, int minorVersion)
460        {
461            return this._asIiTunes.CheckVersion(majorVersion, minorVersion);
462        }
463
464        public IITObject GetITObjectByID(int sourceID, int playlistID, int trackID, int databaseID)
465        {
466            try
467            {
468                return this.factory.CreateObject(
469                    this._asIiTunes.GetITObjectByID(sourceID, playlistID, trackID, databaseID));
470            } catch (COMException)
471            {
472                return null;
473            }
474        }
475
476        public IPlaylist CreatePlaylist(string playlistName)
477        {
478            return this.factory.CreatePlaylist(this._asIiTunes.CreatePlaylist(playlistName));
479        }
480
481        public void OpenURL(string URL)
482        {
483            this._asIiTunes.OpenURL(URL);
484        }
485
486        public void GotoMusicStoreHomePage()
487        {
488            this._asIiTunes.GotoMusicStoreHomePage();
489        }
490
491        public void UpdateIPod()
492        {
493            this._asIiTunes.UpdateIPod();
494        }
495
496        public void Authorize(int numElems, ref object data, ref string names)
497        {
498            this._asIiTunes.Authorize(numElems, ref data, ref names);
499        }
500
501        public void Quit()
502        {
503            this._asIiTunes.Quit();
504        }
505
506        public void SetOptions(int options)
507        {
508            this._asIiTunes.SetOptions(options);
509        }
510
511        public IEQPreset CreateEQPreset(string eqPresetName)
512        {
513            return this.factory.CreateEQPreset(this._asIiTunes.CreateEQPreset(eqPresetName));
514        }
515
516        public IPlaylist CreatePlaylistInSource(string playlistName, ISource iSource)
517        {
518            return iSource.CreatePlaylist(playlistName);
519        }
520
521        public void GetPlayerButtonsState(out bool previousEnabled, out PlayButtonState playPauseStopState,
522                                          out bool nextEnabled)
523        {
524            iTunes.ITPlayButtonState tempPlayPauseStopState;
525            this._asIiTunes.GetPlayerButtonsState(out previousEnabled, out tempPlayPauseStopState, out nextEnabled);
526            playPauseStopState = (PlayButtonState) tempPlayPauseStopState;
527        }
528
529        public void PlayerButtonClicked(PlayerButton playerButton, int playerButtonModifierKeys)
530        {
531            this._asIiTunes.PlayerButtonClicked((iTunes.ITPlayerButton) playerButton, playerButtonModifierKeys);
532        }
533
534        public void SubscribeToPodcast(string URL)
535        {
536            this._asIiTunes.SubscribeToPodcast(URL);
537        }
538
539        public void UpdatePodcastFeeds()
540        {
541            this._asIiTunes.UpdatePodcastFeeds();
542        }
543
544        public IPlaylist CreateFolder(string folderName)
545        {
546            return this.factory.CreatePlaylist(this._asIiTunes.CreateFolder(folderName));
547        }
548
549        public IPlaylist CreateFolderInSource(string folderName, ISource iSource)
550        {
551            return iSource.CreateFolder(folderName);
552        }
553    }
554
555    internal class CanSetShufflePlaylistCollection : IIndexedCollection<IPlaylist, bool>
556    {
557        public bool this[IPlaylist Playlist]
558        {
559            get { return Playlist.CanSetShuffle; }
560        }
561    }
562
563    internal class CanSetSongRepeatPlaylistCollection : IIndexedCollection<IPlaylist, bool>
564    {
565        public bool this[IPlaylist Playlist]
566        {
567            get { return Playlist.CanSetSongRepeat; }
568        }
569    }
570
571    public class DatabaseChangedEventArgs : EventArgs
572    {
573        private readonly IITObject[] changedObjects;
574        private readonly IITObject[] deletedObjects;
575
576        public DatabaseChangedEventArgs(IITObject[] deletedObjects, IITObject[] changedObjects)
577        {
578            this.deletedObjects = deletedObjects;
579            this.changedObjects = changedObjects;
580        }
581
582        public IITObject[] DeletedObjects
583        {
584            get { return this.deletedObjects; }
585        }
586
587        public IITObject[] ChangedObjects
588        {
589            get { return this.changedObjects; }
590        }
591    }
592
593    public class TrackEventArgs : EventArgs
594    {
595        private readonly ITrack track;
596
597        public TrackEventArgs(ITrack track)
598        {
599            this.track = track;
600        }
601
602        public ITrack Track
603        {
604            get { return this.track; }
605        }
606    }
607
608    public class COMCallsDisabledEventArgs : EventArgs
609    {
610        private readonly COMDisabledReason reason;
611
612        public COMCallsDisabledEventArgs(COMDisabledReason reason)
613        {
614            this.reason = reason;
615        }
616
617        public COMDisabledReason Reason
618        {
619            get { return this.reason; }
620        }
621    }
622
623    public class SoundVolumeChangedEventArgs : EventArgs
624    {
625        private readonly int newVolume;
626
627        public SoundVolumeChangedEventArgs(int newVolume)
628        {
629            this.newVolume = newVolume;
630        }
631
632        public int NewVolume
633        {
634            get { return this.newVolume; }
635        }
636    }
637}
Note: See TracBrowser for help on using the browser.