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

Revision 11205, 12.4 kB (checked in by topia, 5 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 Clovery.iTunesHelper.COMWrap.Interfaces;
7
8namespace Clovery.iTunesHelper.COMWrap
9{
10    /// <summary>
11    /// base class of some factorizable classes to represent iTunes object.
12    /// </summary>
13    internal class ITFactory : IITFactory
14    {
15        /// <summary>
16        /// Points to this sessions' iTunes application object.
17        /// </summary>
18        protected internal readonly ITunesApp _app;
19
20        /// <summary>
21        /// Points to internal iTunes object.
22        /// </summary>
23        protected internal object _internal;
24
25        /// <summary>
26        /// Base Initializer.
27        /// </summary>
28        /// <param name="_internal">iTunes internal object</param>
29        /// <param name="app">iTunes application object</param>
30        protected internal ITFactory(object _internal, ITunesApp app)
31        {
32            this._app = app;
33            this._internal = _internal;
34        }
35
36        /// <summary>
37        /// Base initializer.
38        /// </summary>
39        /// <param name="app">iTunes application object</param>
40        public ITFactory(ITunesApp app)
41        {
42            this._app = app;
43            this._internal = null;
44        }
45
46        /// <summary>
47        /// Returns self-wrapped iTunes object factory.
48        /// </summary>
49        public IITunesWrappedObjectFactory factory
50        {
51            get { return this._app.factory; }
52        }
53       
54        public object __internalObject
55        {
56            get { return this._internal; }
57        }
58
59        public bool Attached
60        {
61            get { return this._internal != null; }
62        }
63    }
64
65    internal class ITObject : ITFactory, IITObject
66    {
67        protected internal readonly int _playlistID;
68        protected internal readonly int _sourceID;
69        protected internal readonly int _TrackDatabaseID;
70        protected internal readonly int _trackID;
71        protected internal string _name;
72
73        protected internal ITObject(iTunesLib.IITObject _internal, ITunesApp app) : base(_internal, app)
74        {
75            _internal.GetITObjectIDs(out this._sourceID, out this._playlistID, out this._trackID,
76                                     out this._TrackDatabaseID);
77            this._name = _internal.Name;
78        }
79
80        protected internal ITObject(int sourceID, int playlistID, int trackID, int TrackDatabaseID, ITunesApp app) : base(app)
81        {
82            this._sourceID = sourceID;
83            this._playlistID = playlistID;
84            this._trackID = trackID;
85            this._TrackDatabaseID = TrackDatabaseID;
86        }
87
88        ~ITObject ()
89        {
90            this._internal = null;
91        }
92
93        protected iTunesLib.IITObject _asIITObject
94        {
95            get
96            {
97                if (this._internal == null)
98                {
99                    throw new InvalidCastException("this object cannot be called with abstract ITObject");
100                }
101                return (iTunesLib.IITObject) this._internal;
102            }
103        }
104
105        public void GetITObjectIDs(out int sourceID, out int playlistID, out int trackID, out int databaseID)
106        {
107            //this._asIITObject.GetITObjectIDs(out sourceID, out playlistID, out trackID, out databaseID);
108            sourceID = this.sourceID;
109            playlistID = this.playlistID;
110            trackID = this.trackID;
111            databaseID = this.TrackDatabaseID;
112        }
113
114        public string Name
115        {
116            get
117            {
118                try
119                {
120                    string name = this._asIITObject.Name;
121                    this._name = name;
122                    return name;
123                }
124                catch (InvalidCastException)
125                {
126                    return this._name;
127                }
128            }
129
130            set
131            {
132                this._asIITObject.Name = value;
133                this._name = value;
134            }
135        }
136
137        public int Index
138        {
139            get { return this._asIITObject.Index; }
140        }
141
142        public int sourceID
143        {
144            get { return this._sourceID; }
145        }
146
147        public int playlistID
148        {
149            get { return this._playlistID; }
150        }
151
152        public int trackID
153        {
154            get { return this._trackID; }
155        }
156
157        public int TrackDatabaseID
158        {
159            get { return this._TrackDatabaseID; }
160        }
161
162        ///<summary>
163        ///Indicates whether the current object is equal to another object of the same type.
164        ///</summary>
165        ///
166        ///<returns>
167        ///true if the current object is equal to the other parameter; otherwise, false.
168        ///</returns>
169        ///
170        ///<param name="other">An object to compare with this object.</param>
171        public bool Equals(IITObject other)
172        {
173            if (other == null) return false;
174            if (this._sourceID != other.sourceID) return false;
175            if (this._playlistID != other.playlistID) return false;
176            if (this._trackID != other.trackID) return false;
177            if (this._TrackDatabaseID != other.TrackDatabaseID) return false;
178            return true;
179        }
180
181        ///<summary>
182        ///Indicates whether the current object is equal to another object of the same type.
183        ///</summary>
184        ///
185        ///<returns>
186        ///true if the current object is equal to the other parameter; otherwise, false.
187        ///</returns>
188        ///
189        ///<param name="other">An object to compare with this object.</param>
190        public override bool Equals(object other)
191        {
192            if (ReferenceEquals(this, other)) return true;
193            if (other == null) return false;
194            return Equals(other as IITObject);
195        }
196
197        public static bool operator ==(ITObject a, ITObject b)
198        {
199            if (ReferenceEquals(a, b)) return true;
200            if (((object) a == null) || ((object) b == null))
201            {
202                return false;
203            }
204
205            return a.Equals(b);
206        }
207
208        public static bool operator !=(ITObject a, ITObject b)
209        {
210            return !(a == b);
211        }
212
213        public override int GetHashCode()
214        {
215            int result = this._sourceID;
216            result = 29*result + this._playlistID;
217            result = 29*result + this._trackID;
218            result = 29*result + this._TrackDatabaseID;
219            return result;
220        }
221
222        public IITObject Retrive()
223        {
224            return this._app.GetITObjectByID(this.sourceID, this.playlistID, this.trackID, this.TrackDatabaseID);
225        }
226
227        public void Invalidate()
228        {
229            this._name = this._asIITObject.Name;
230        }
231    }
232
233    internal class ParentStringIndexerReferrer<TCollection, TItem> : IIndexedCollection<string, TItem>
234        where TCollection : IIndexedCollection<string, TItem>
235    {
236        protected internal readonly TCollection _parent;
237
238        internal ParentStringIndexerReferrer(TCollection _parent)
239        {
240            this._parent = _parent;
241        }
242
243        public TItem this[string Name]
244        {
245            get { return this._parent[Name]; }
246        }
247    }
248
249    internal abstract class ITBaseCollection<TItem> : ITFactory, IITBaseCollection<TItem>
250        where TItem : class
251    {
252        protected ITBaseCollection(object _internal, ITunesApp app) : base(_internal, app)
253        {
254        }
255
256        public abstract int Count { get; }
257
258        ///<summary>
259        ///Returns an enumerator that iterates through the collection.
260        ///</summary>
261        ///
262        ///<returns>
263        ///A <see cref="T:System.Collections.Generic.IEnumerator`1"></see> that can be used to iterate through the collection.
264        ///</returns>
265        ///<filterpriority>1</filterpriority>
266        public IEnumerator<TItem> GetEnumerator()
267        {
268            return this._GetEnumerator();
269        }
270
271        ///<summary>
272        ///Returns an enumerator that iterates through a collection.
273        ///</summary>
274        ///
275        ///<returns>
276        ///An <see cref="T:System.Collections.IEnumerator"></see> object that can be used to iterate through the collection.
277        ///</returns>
278        ///<filterpriority>2</filterpriority>
279        IEnumerator IEnumerable.GetEnumerator()
280        {
281            return this._GetEnumerator();
282        }
283
284        protected internal abstract IEnumerator<TItem> _GetEnumerator();
285    }
286
287    internal class ITBaseEnumerator<TITunesItem, TItem, TBaseCollection> : IEnumerator<TItem>
288        where TBaseCollection : IITBaseCollection<TItem>, ItemCreatableCollection<TITunesItem, TItem>
289        where TItem : class
290    {
291        protected internal readonly TBaseCollection _collection;
292        protected internal readonly IEnumerator _internal;
293        protected TItem _current;
294
295        internal ITBaseEnumerator(IEnumerator internalEnumerator, TBaseCollection baseCollection)
296        {
297            this._internal = internalEnumerator;
298            this._collection = baseCollection;
299        }
300
301        ///<summary>
302        ///Advances the enumerator to the next element of the collection.
303        ///</summary>
304        ///
305        ///<returns>
306        ///true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.
307        ///</returns>
308        ///
309        ///<exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception><filterpriority>2</filterpriority>
310        public bool MoveNext()
311        {
312            this._current = null;
313            return this._internal.MoveNext();
314        }
315
316        ///<summary>
317        ///Sets the enumerator to its initial position, which is before the first element in the collection.
318        ///</summary>
319        ///
320        ///<exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception><filterpriority>2</filterpriority>
321        public void Reset()
322        {
323            this._internal.Reset();
324        }
325
326        ///<summary>
327        ///Gets the current element in the collection.
328        ///</summary>
329        ///
330        ///<returns>
331        ///The current element in the collection.
332        ///</returns>
333        ///
334        ///<exception cref="T:System.InvalidOperationException">The enumerator is positioned before the first element of the collection or after the last element. </exception><filterpriority>2</filterpriority>
335        object IEnumerator.Current
336        {
337            get { return this.GetCurrent(); }
338        }
339
340        ///<summary>
341        ///Gets the element in the collection at the current position of the enumerator.
342        ///</summary>
343        ///
344        ///<returns>
345        ///The element in the collection at the current position of the enumerator.
346        ///</returns>
347        ///
348        TItem IEnumerator<TItem>.Current
349        {
350            get { return this.GetCurrent(); }
351        }
352
353        ///<summary>
354        ///Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
355        ///</summary>
356        ///<filterpriority>2</filterpriority>
357        public void Dispose()
358        {
359        }
360
361        protected TItem GetCurrent()
362        {
363            if (this._current == null)
364            {
365                this._current = this._GetCurrent();
366            }
367
368            return this._current;
369        }
370
371        protected TItem _GetCurrent()
372        {
373            return this._collection.CreateItem((TITunesItem) this._internal.Current);
374        }
375    }
376
377    public class EventAttachStatusChangedEventArgs : EventArgs
378    {
379        private readonly bool newState;
380
381
382        public EventAttachStatusChangedEventArgs(bool newState)
383        {
384            this.newState = newState;
385        }
386
387        public bool NewState
388        {
389            get { return this.newState; }
390        }
391
392        public override string ToString()
393        {
394            return String.Format("{0}({1})", this.GetType(), this.NewState);
395        }
396    }
397}
Note: See TracBrowser for help on using the browser.