VLCMediaListAspect.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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:(NSArray *)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:[NSArray arrayWithObject:[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. if( ownHisMediaList )
  99. [parentMediaList release];
  100. [super dealloc];
  101. }
  102. - (NSString *)description
  103. {
  104. NSMutableString * content = [NSMutableString string];
  105. int i;
  106. for( i = 0; i < [self count]; i++)
  107. {
  108. [content appendFormat:@"%@\n", [self mediaAtIndex: i]];
  109. }
  110. return [NSString stringWithFormat:@"<%@ %p> {\n%@}", [self className], self, content];
  111. }
  112. - (VLCMedia *)mediaAtIndex:(int)index
  113. {
  114. libvlc_exception_t p_e;
  115. libvlc_exception_init( &p_e );
  116. libvlc_media_descriptor_t * p_md = libvlc_media_list_view_item_at_index( p_mlv, index, &p_e );
  117. catch_exception( &p_e );
  118. // Returns local object for media descriptor, searchs for user data first. If not found it creates a
  119. // new cocoa object representation of the media descriptor.
  120. return [VLCMedia mediaWithLibVLCMediaDescriptor:p_md];
  121. }
  122. - (VLCMediaListAspect *)childrenAtIndex:(int)index
  123. {
  124. libvlc_exception_t p_e;
  125. libvlc_exception_init( &p_e );
  126. libvlc_media_list_view_t * p_sub_mlv = libvlc_media_list_view_children_at_index( p_mlv, index, &p_e );
  127. catch_exception( &p_e );
  128. if( !p_sub_mlv )
  129. return nil;
  130. // Returns local object for media descriptor, searchs for user data first. If not found it creates a
  131. // new cocoa object representation of the media descriptor.
  132. return [VLCMediaListAspect mediaListAspectWithLibVLCMediaListView:p_sub_mlv];
  133. }
  134. - (VLCMediaListAspectNode *)nodeAtIndex:(int)index
  135. {
  136. VLCMediaListAspectNode * node = [[[VLCMediaListAspectNode alloc] init] autorelease];
  137. [node setMedia:[self mediaAtIndex: index]];
  138. return node;
  139. }
  140. - (int)count
  141. {
  142. libvlc_exception_t p_e;
  143. libvlc_exception_init( &p_e );
  144. int result = libvlc_media_list_view_count( p_mlv, &p_e );
  145. catch_exception( &p_e );
  146. return result;
  147. }
  148. - (VLCMediaList *)parentMediaList
  149. {
  150. return parentMediaList;
  151. }
  152. @end
  153. @implementation VLCMediaListAspect (LibVLCBridging)
  154. + (id)mediaListAspectWithLibVLCMediaListView:(libvlc_media_list_view_t *)p_new_mlv
  155. {
  156. return [[[VLCMediaListAspect alloc] initWithLibVLCMediaListView:p_new_mlv andMediaList:nil] autorelease];
  157. }
  158. + (id)mediaListAspectWithLibVLCMediaListView:(libvlc_media_list_view_t *)p_new_mlv andMediaList:(VLCMediaList *)mediaList;
  159. {
  160. return [[[VLCMediaListAspect alloc] initWithLibVLCMediaListView:p_new_mlv andMediaList:mediaList] autorelease];
  161. }
  162. - (id)initWithLibVLCMediaListView:(libvlc_media_list_view_t *)p_new_mlv andMediaList:(VLCMediaList *)mediaList;
  163. {
  164. if( self = [super init] )
  165. {
  166. p_mlv = p_new_mlv;
  167. libvlc_media_list_view_retain(p_mlv);
  168. /* parentMediaList isn't retained, because we need a mediaList to exists, and not the contrary */
  169. parentMediaList = mediaList;
  170. ownHisMediaList = NO;
  171. if( !parentMediaList )
  172. {
  173. /* We have to create it then */
  174. libvlc_media_list_view_retain(p_mlv);
  175. libvlc_media_list_t * p_mlist = libvlc_media_list_view_parent_media_list(p_mlv, NULL);
  176. parentMediaList = [[VLCMediaList mediaListWithLibVLCMediaList: p_mlist] retain];
  177. libvlc_media_list_release( p_mlist );
  178. /* This is an exception, and we owns it here */
  179. ownHisMediaList = YES;
  180. }
  181. cachedNode = [[NSMutableArray alloc] initWithCapacity:libvlc_media_list_view_count(p_mlv, NULL)];
  182. libvlc_media_list_t * p_mlist;
  183. p_mlist = libvlc_media_list_view_parent_media_list( p_mlv, NULL );
  184. libvlc_media_list_lock( p_mlist );
  185. int i, count = libvlc_media_list_view_count(p_mlv, NULL);
  186. for( i = 0; i < count; i++ )
  187. {
  188. libvlc_media_descriptor_t * p_md = libvlc_media_list_view_item_at_index(p_mlv, i, NULL);
  189. libvlc_media_list_view_t * p_sub_mlv = libvlc_media_list_view_children_at_index(p_mlv, i, NULL);
  190. VLCMediaListAspectNode * node = [[[VLCMediaListAspectNode alloc] init] autorelease];
  191. [node setMedia:[VLCMedia mediaWithLibVLCMediaDescriptor: p_md]];
  192. [node setChildren: p_sub_mlv ? [VLCMediaListAspect mediaListAspectWithLibVLCMediaListView: p_sub_mlv] : nil];
  193. [cachedNode addObject:node];
  194. libvlc_media_descriptor_release(p_md);
  195. if( p_sub_mlv ) libvlc_media_list_view_release(p_sub_mlv);
  196. }
  197. [self initInternalMediaListView];
  198. libvlc_media_list_unlock( p_mlist );
  199. libvlc_media_list_release( p_mlist );
  200. }
  201. return self;
  202. }
  203. - (libvlc_media_list_view_t *)libVLCMediaListView
  204. {
  205. return (libvlc_media_list_view_t *)p_mlv;
  206. }
  207. @end
  208. @implementation VLCMediaListAspect (Private)
  209. - (void)initInternalMediaListView
  210. {
  211. libvlc_exception_t e;
  212. libvlc_exception_init( &e );
  213. libvlc_event_manager_t * p_em = libvlc_media_list_event_manager( p_mlv, &e );
  214. /* Add internal callback */
  215. libvlc_event_attach( p_em, libvlc_MediaListViewItemAdded, HandleMediaListViewItemAdded, self, &e );
  216. libvlc_event_attach( p_em, libvlc_MediaListViewItemDeleted, HandleMediaListViewItemDeleted, self, &e );
  217. catch_exception( &e );
  218. }
  219. - (void)mediaListViewItemAdded:(NSArray *)arrayOfArgs
  220. {
  221. NSAssert([NSThread isMainThread], @"We are not on main thread");
  222. /* We hope to receive index in a nide range, that could change one day */
  223. int start = [[[arrayOfArgs objectAtIndex: 0] objectForKey:@"index"] intValue];
  224. int end = [[[arrayOfArgs objectAtIndex: [arrayOfArgs count]-1] objectForKey:@"index"] intValue];
  225. NSRange range = NSMakeRange(start, end-start);
  226. [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range] forKey:@"media"];
  227. [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range] forKey:@"node"];
  228. for( NSDictionary * args in arrayOfArgs )
  229. {
  230. int index = [[args objectForKey:@"index"] intValue];
  231. VLCMedia * media = [args objectForKey:@"media"];
  232. VLCMediaListAspectNode * node = [[[VLCMediaListAspectNode alloc] init] autorelease];
  233. [node setMedia:media];
  234. [node setChildren:[self childrenAtIndex:index]];
  235. /* Sanity check */
  236. if( index && index >= [cachedNode count] )
  237. index = [cachedNode count] - 1;
  238. [cachedNode insertObject:node atIndex:index];
  239. }
  240. [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range] forKey:@"node"];
  241. [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndexesInRange:range] forKey:@"media"];
  242. }
  243. - (void)mediaListViewItemRemoved:(NSNumber *)index
  244. {
  245. [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"media"];
  246. [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"node"];
  247. [cachedNode removeObjectAtIndex:[index intValue]];
  248. [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"node"];
  249. [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"media"];
  250. }
  251. @end