VLCMediaListAspect.m 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*****************************************************************************
  2. * VLCMediaList.m: VLC.framework VLCMediaList implementation
  3. *****************************************************************************
  4. * Copyright (C) 2007 Pierre d'Herbemont
  5. * Copyright (C) 2007 the VideoLAN team
  6. * $Id$
  7. *
  8. * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  23. *****************************************************************************/
  24. #import "VLCMediaListAspect.h"
  25. #import "VLCLibrary.h"
  26. #import "VLCEventManager.h"
  27. #import "VLCLibVLCBridging.h"
  28. #include <vlc/vlc.h>
  29. #include <vlc/libvlc.h>
  30. // TODO: Documentation
  31. @interface VLCMediaListAspect (Private)
  32. /* Initializers */
  33. - (void)initInternalMediaListView;
  34. - (void)mediaListViewItemAdded:(NSDictionary *)args;
  35. - (void)mediaListViewItemRemoved:(NSNumber *)index;
  36. @end
  37. @implementation VLCMediaListAspectNode
  38. @synthesize media;
  39. @synthesize children;
  40. - (BOOL)isLeaf
  41. {
  42. return self.children == NULL;
  43. }
  44. -(void)dealloc
  45. {
  46. [self.children release];
  47. [super dealloc];
  48. }
  49. @end
  50. @implementation VLCMediaListAspect (KeyValueCodingCompliance)
  51. /* For the @"media" key */
  52. - (int) countOfMedia
  53. {
  54. return [cachedNode count];
  55. }
  56. - (id) objectInMediaAtIndex:(int)i
  57. {
  58. return [[cachedNode objectAtIndex:i] media];
  59. }
  60. /* For the @"node" key */
  61. - (int) countOfNode
  62. {
  63. return [cachedNode count];
  64. }
  65. - (id) objectInNodeAtIndex:(int)i
  66. {
  67. return [cachedNode objectAtIndex:i];
  68. }
  69. @end
  70. /* libvlc event callback */
  71. static void HandleMediaListViewItemAdded(const libvlc_event_t *event, void *user_data)
  72. {
  73. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  74. id self = user_data;
  75. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  76. withMethod:@selector(mediaListViewItemAdded:)
  77. withArgumentAsObject:[NSDictionary dictionaryWithObjectsAndKeys:
  78. [VLCMedia mediaWithLibVLCMediaDescriptor:event->u.media_list_item_added.item], @"media",
  79. [NSNumber numberWithInt:event->u.media_list_item_added.index], @"index",
  80. nil]];
  81. [pool release];
  82. }
  83. static void HandleMediaListViewItemDeleted( const libvlc_event_t * event, void * user_data)
  84. {
  85. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  86. id self = user_data;
  87. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  88. withMethod:@selector(mediaListViewItemRemoved:)
  89. withArgumentAsObject:[NSNumber numberWithInt:event->u.media_list_item_deleted.index]];
  90. [pool release];
  91. }
  92. @implementation VLCMediaListAspect
  93. - (void)dealloc
  94. {
  95. // Release allocated memory
  96. libvlc_media_list_view_release(p_mlv);
  97. [cachedNode release];
  98. [parentMediaList release];
  99. [super dealloc];
  100. }
  101. - (VLCMedia *)mediaAtIndex:(int)index
  102. {
  103. libvlc_exception_t p_e;
  104. libvlc_exception_init( &p_e );
  105. libvlc_media_descriptor_t *p_md = libvlc_media_list_view_item_at_index( p_mlv, index, &p_e );
  106. quit_on_exception( &p_e );
  107. // Returns local object for media descriptor, searchs for user data first. If not found it creates a
  108. // new cocoa object representation of the media descriptor.
  109. return [VLCMedia mediaWithLibVLCMediaDescriptor:p_md];
  110. }
  111. - (VLCMediaListAspect *)childrenAtIndex:(int)index
  112. {
  113. libvlc_exception_t p_e;
  114. libvlc_exception_init( &p_e );
  115. libvlc_media_list_view_t *p_sub_mlv = libvlc_media_list_view_children_at_index( p_mlv, index, &p_e );
  116. quit_on_exception( &p_e );
  117. if( !p_sub_mlv )
  118. return nil;
  119. // Returns local object for media descriptor, searchs for user data first. If not found it creates a
  120. // new cocoa object representation of the media descriptor.
  121. return [VLCMediaListAspect mediaListAspectWithLibVLCMediaListView:p_sub_mlv];
  122. }
  123. - (VLCMediaListAspectNode *)nodeAtIndex:(int)index
  124. {
  125. VLCMediaListAspectNode * node = [[[VLCMediaListAspectNode alloc] init] autorelease];
  126. [node setMedia:[self mediaAtIndex: index]];
  127. return node;
  128. }
  129. - (int)count
  130. {
  131. libvlc_exception_t p_e;
  132. libvlc_exception_init( &p_e );
  133. int result = libvlc_media_list_view_count( p_mlv, &p_e );
  134. quit_on_exception( &p_e );
  135. return result;
  136. }
  137. - (VLCMediaList *)parentMediaList
  138. {
  139. return [[parentMediaList retain] autorelease];
  140. }
  141. @end
  142. @implementation VLCMediaListAspect (LibVLCBridging)
  143. + (id)mediaListAspectWithLibVLCMediaListView:(libvlc_media_list_view_t *)p_new_mlv;
  144. {
  145. return [[[VLCMediaListAspect alloc] initWithLibVLCMediaListView:p_new_mlv] autorelease];
  146. }
  147. - (id)initWithLibVLCMediaListView:(libvlc_media_list_view_t *)p_new_mlv;
  148. {
  149. if( self = [super init] )
  150. {
  151. p_mlv = p_new_mlv;
  152. libvlc_media_list_view_retain(p_mlv);
  153. libvlc_media_list_t * p_mlist = libvlc_media_list_view_parent_media_list(p_mlv, NULL);
  154. parentMediaList = [[VLCMediaList mediaListWithLibVLCMediaList: p_mlist] retain];
  155. libvlc_media_list_release( p_mlist );
  156. //libvlc_media_list_lock(p_mlv->p_mlist);
  157. cachedNode = [[NSMutableArray alloc] initWithCapacity:libvlc_media_list_view_count(p_mlv, NULL)];
  158. int i, count = libvlc_media_list_view_count(p_mlv, NULL);
  159. for( i = 0; i < count; i++ )
  160. {
  161. libvlc_media_descriptor_t * p_md = libvlc_media_list_view_item_at_index(p_mlv, i, NULL);
  162. libvlc_media_list_view_t * p_sub_mlv = libvlc_media_list_view_children_at_index(p_mlv, i, NULL);
  163. VLCMediaListAspectNode * node = [[[VLCMediaListAspectNode alloc] init] autorelease];
  164. [node setMedia:[VLCMedia mediaWithLibVLCMediaDescriptor: p_md]];
  165. [node setChildren: p_sub_mlv ? [VLCMediaListAspect mediaListAspectWithLibVLCMediaListView: p_sub_mlv] : nil];
  166. [cachedNode addObject:node];
  167. libvlc_media_descriptor_release(p_md);
  168. if( p_sub_mlv ) libvlc_media_list_view_release(p_sub_mlv);
  169. }
  170. [self initInternalMediaListView];
  171. //libvlc_media_list_unlock(p_mlv->p_mlist);
  172. }
  173. return self;
  174. }
  175. - (libvlc_media_list_view_t *)libVLCMediaListView
  176. {
  177. return (libvlc_media_list_view_t *)p_mlv;
  178. }
  179. @end
  180. @implementation VLCMediaListAspect (Private)
  181. - (void)initInternalMediaListView
  182. {
  183. libvlc_exception_t e;
  184. libvlc_exception_init( &e );
  185. libvlc_event_manager_t *p_em = libvlc_media_list_event_manager( p_mlv, &e );
  186. /* Add internal callback */
  187. libvlc_event_attach( p_em, libvlc_MediaListViewItemAdded, HandleMediaListViewItemAdded, self, &e );
  188. libvlc_event_attach( p_em, libvlc_MediaListViewItemDeleted, HandleMediaListViewItemDeleted, self, &e );
  189. quit_on_exception( &e );
  190. }
  191. - (void)mediaListViewItemAdded:(NSDictionary *)args
  192. {
  193. int index = [[args objectForKey:@"index"] intValue];
  194. VLCMedia * media = [args objectForKey:@"media"];
  195. VLCMediaListAspectNode * node = [[[VLCMediaListAspectNode alloc] init] autorelease];
  196. [node setMedia:media];
  197. [node setChildren:[self childrenAtIndex:index]];
  198. [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:index] forKey:@"media"];
  199. [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:index] forKey:@"node"];
  200. [cachedNode insertObject:node atIndex:index];
  201. [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:index] forKey:@"node"];
  202. [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:index] forKey:@"media"];
  203. }
  204. - (void)mediaListViewItemRemoved:(NSNumber *)index
  205. {
  206. [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"media"];
  207. [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"node"];
  208. [cachedNode removeObjectAtIndex:[index intValue]];
  209. [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"node"];
  210. [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"media"];
  211. }
  212. @end