VLCMediaListAspect.m 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. [super dealloc];
  99. }
  100. - (VLCMedia *)mediaAtIndex:(int)index
  101. {
  102. libvlc_exception_t p_e;
  103. libvlc_exception_init( &p_e );
  104. libvlc_media_descriptor_t *p_md = libvlc_media_list_view_item_at_index( p_mlv, index, &p_e );
  105. quit_on_exception( &p_e );
  106. // Returns local object for media descriptor, searchs for user data first. If not found it creates a
  107. // new cocoa object representation of the media descriptor.
  108. return [VLCMedia mediaWithLibVLCMediaDescriptor:p_md];
  109. }
  110. - (VLCMediaListAspect *)childrenAtIndex:(int)index
  111. {
  112. libvlc_exception_t p_e;
  113. libvlc_exception_init( &p_e );
  114. libvlc_media_list_view_t *p_sub_mlv = libvlc_media_list_view_children_at_index( p_mlv, index, &p_e );
  115. quit_on_exception( &p_e );
  116. if( !p_sub_mlv )
  117. return nil;
  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 [VLCMediaListAspect mediaListAspectWithLibVLCMediaListView:p_sub_mlv];
  121. }
  122. - (VLCMediaListAspectNode *)nodeAtIndex:(int)index
  123. {
  124. VLCMediaListAspectNode * node = [[[VLCMediaListAspectNode alloc] init] autorelease];
  125. [node setMedia:[self mediaAtIndex: index]];
  126. return node;
  127. }
  128. - (int)count
  129. {
  130. libvlc_exception_t p_e;
  131. libvlc_exception_init( &p_e );
  132. int result = libvlc_media_list_view_count( p_mlv, &p_e );
  133. quit_on_exception( &p_e );
  134. return result;
  135. }
  136. @end
  137. @implementation VLCMediaListAspect (LibVLCBridging)
  138. + (id)mediaListAspectWithLibVLCMediaListView:(libvlc_media_list_view_t *)p_new_mlv;
  139. {
  140. return [[[VLCMediaListAspect alloc] initWithLibVLCMediaListView:p_new_mlv] autorelease];
  141. }
  142. - (id)initWithLibVLCMediaListView:(libvlc_media_list_view_t *)p_new_mlv;
  143. {
  144. if( self = [super init] )
  145. {
  146. p_mlv = p_new_mlv;
  147. libvlc_media_list_view_retain(p_mlv);
  148. //libvlc_media_list_lock(p_mlv->p_mlist);
  149. cachedNode = [[NSMutableArray alloc] initWithCapacity:libvlc_media_list_view_count(p_mlv, NULL)];
  150. int i, count = libvlc_media_list_view_count(p_mlv, NULL);
  151. for( i = 0; i < count; i++ )
  152. {
  153. libvlc_media_descriptor_t * p_md = libvlc_media_list_view_item_at_index(p_mlv, i, NULL);
  154. libvlc_media_list_view_t * p_sub_mlv = libvlc_media_list_view_children_at_index(p_mlv, i, NULL);
  155. VLCMediaListAspectNode * node = [[[VLCMediaListAspectNode alloc] init] autorelease];
  156. [node setMedia:[VLCMedia mediaWithLibVLCMediaDescriptor: p_md]];
  157. [node setChildren: p_sub_mlv ? [VLCMediaListAspect mediaListAspectWithLibVLCMediaListView: p_sub_mlv] : nil];
  158. [cachedNode addObject:node];
  159. libvlc_media_descriptor_release(p_md);
  160. if( p_sub_mlv ) libvlc_media_list_view_release(p_sub_mlv);
  161. }
  162. [self initInternalMediaListView];
  163. //libvlc_media_list_unlock(p_mlv->p_mlist);
  164. }
  165. return self;
  166. }
  167. - (libvlc_media_list_view_t *)libVLCMediaListView
  168. {
  169. return (libvlc_media_list_view_t *)p_mlv;
  170. }
  171. @end
  172. @implementation VLCMediaListAspect (Private)
  173. - (void)initInternalMediaListView
  174. {
  175. libvlc_exception_t e;
  176. libvlc_exception_init( &e );
  177. libvlc_event_manager_t *p_em = libvlc_media_list_event_manager( p_mlv, &e );
  178. /* Add internal callback */
  179. libvlc_event_attach( p_em, libvlc_MediaListViewItemAdded, HandleMediaListViewItemAdded, self, &e );
  180. libvlc_event_attach( p_em, libvlc_MediaListViewItemDeleted, HandleMediaListViewItemDeleted, self, &e );
  181. quit_on_exception( &e );
  182. }
  183. - (void)mediaListViewItemAdded:(NSDictionary *)args
  184. {
  185. int index = [[args objectForKey:@"index"] intValue];
  186. VLCMedia * media = [args objectForKey:@"media"];
  187. VLCMediaListAspectNode * node = [[[VLCMediaListAspectNode alloc] init] autorelease];
  188. [node setMedia:media];
  189. [node setChildren:[self childrenAtIndex:index]];
  190. [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:index] forKey:@"media"];
  191. [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:index] forKey:@"node"];
  192. [cachedNode insertObject:node atIndex:index];
  193. [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:index] forKey:@"node"];
  194. [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:index] forKey:@"media"];
  195. }
  196. - (void)mediaListViewItemRemoved:(NSNumber *)index
  197. {
  198. [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"media"];
  199. [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"node"];
  200. [cachedNode removeObjectAtIndex:[index intValue]];
  201. [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"node"];
  202. [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:[index intValue]] forKey:@"media"];
  203. }
  204. @end