VLCMediaListAspect.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. @end
  35. @implementation VLCMediaListAspect (KeyValueCodingCompliance)
  36. /* For the @"Media" key */
  37. - (int) countOfMedia
  38. {
  39. return [self count];
  40. }
  41. - (id) objectInMediaAtIndex:(int)i
  42. {
  43. return [self mediaAtIndex:i];
  44. }
  45. @end
  46. /* libvlc event callback */
  47. static void HandleMediaListViewItemAdded(const libvlc_event_t *event, void *user_data)
  48. {
  49. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  50. id self = user_data;
  51. int index = event->u.media_list_view_item_added.index;
  52. [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:index] forKey:@"Media"];
  53. [pool release];
  54. }
  55. static void HandleMediaListViewWillAddItem(const libvlc_event_t *event, void *user_data)
  56. {
  57. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  58. id self = user_data;
  59. int index = event->u.media_list_view_will_add_item.index;
  60. [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:[NSIndexSet indexSetWithIndex:index] forKey:@"Media"];
  61. [pool release];
  62. }
  63. static void HandleMediaListViewItemDeleted( const libvlc_event_t * event, void * user_data)
  64. {
  65. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  66. id self = user_data;
  67. int index = event->u.media_list_view_will_add_item.index;
  68. [self didChange:NSKeyValueChangeRemoval valuesAtIndexes:[NSIndexSet indexSetWithIndex:index] forKey:@"Media"];
  69. [pool release];
  70. }
  71. static void HandleMediaListViewWillDeleteItem(const libvlc_event_t *event, void *user_data)
  72. {
  73. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  74. id self = user_data;
  75. int index = event->u.media_list_view_will_add_item.index;
  76. [self willChange:NSKeyValueChangeRemoval valuesAtIndexes:[NSIndexSet indexSetWithIndex:index] forKey:@"Media"];
  77. [pool release];
  78. }
  79. @implementation VLCMediaListAspect
  80. - (void)dealloc
  81. {
  82. // Release allocated memory
  83. libvlc_media_list_view_release(p_mlv);
  84. [super dealloc];
  85. }
  86. - (VLCMedia *)mediaAtIndex:(int)index
  87. {
  88. libvlc_exception_t p_e;
  89. libvlc_exception_init( &p_e );
  90. libvlc_media_descriptor_t *p_md = libvlc_media_list_view_item_at_index( p_mlv, index, &p_e );
  91. quit_on_exception( &p_e );
  92. // Returns local object for media descriptor, searchs for user data first. If not found it creates a
  93. // new cocoa object representation of the media descriptor.
  94. return [VLCMedia mediaWithLibVLCMediaDescriptor:p_md];
  95. }
  96. - (int)count
  97. {
  98. libvlc_exception_t p_e;
  99. libvlc_exception_init( &p_e );
  100. int result = libvlc_media_list_view_count( p_mlv, &p_e );
  101. quit_on_exception( &p_e );
  102. return result;
  103. }
  104. @end
  105. @implementation VLCMediaListAspect (LibVLCBridging)
  106. + (id)mediaListAspectWithLibVLCMediaListView:(libvlc_media_list_view_t *)p_new_mlv;
  107. {
  108. return [[[VLCMediaListAspect alloc] initWithLibVLCMediaListView:p_new_mlv] autorelease];
  109. }
  110. - (id)initWithLibVLCMediaListView:(libvlc_media_list_view_t *)p_new_mlv;
  111. {
  112. if( self = [super init] )
  113. {
  114. p_mlv = p_new_mlv;
  115. libvlc_media_list_view_retain(p_mlv);
  116. [self initInternalMediaListView];
  117. }
  118. return self;
  119. }
  120. - (libvlc_media_list_view_t *)libVLCMediaListView
  121. {
  122. return (libvlc_media_list_view_t *)p_mlv;
  123. }
  124. @end
  125. @implementation VLCMediaListAspect (Private)
  126. - (void)initInternalMediaListView
  127. {
  128. libvlc_exception_t e;
  129. libvlc_exception_init( &e );
  130. libvlc_event_manager_t *p_em = libvlc_media_list_event_manager( p_mlv, &e );
  131. /* Add internal callback */
  132. libvlc_event_attach( p_em, libvlc_MediaListViewItemAdded, HandleMediaListViewItemAdded, self, &e );
  133. libvlc_event_attach( p_em, libvlc_MediaListViewWillAddItem, HandleMediaListViewWillAddItem, self, &e );
  134. libvlc_event_attach( p_em, libvlc_MediaListViewItemDeleted, HandleMediaListViewItemDeleted, self, &e );
  135. libvlc_event_attach( p_em, libvlc_MediaListViewWillDeleteItem, HandleMediaListViewWillDeleteItem, self, &e );
  136. quit_on_exception( &e );
  137. }
  138. @end