VLCMediaListAspect.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 HandleMediaListViewItemDeleted( 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_item_deleted.index;
  60. [self didChange:NSKeyValueChangeRemoval valuesAtIndexes:[NSIndexSet indexSetWithIndex:index] forKey:@"Media"];
  61. [pool release];
  62. }
  63. @implementation VLCMediaListAspect
  64. - (void)dealloc
  65. {
  66. // Release allocated memory
  67. libvlc_media_list_view_release(p_mlv);
  68. [cachedMedia release];
  69. [super dealloc];
  70. }
  71. - (VLCMedia *)mediaAtIndex:(int)index
  72. {
  73. libvlc_exception_t p_e;
  74. libvlc_exception_init( &p_e );
  75. libvlc_media_descriptor_t *p_md = libvlc_media_list_view_item_at_index( p_mlv, index, &p_e );
  76. quit_on_exception( &p_e );
  77. // Returns local object for media descriptor, searchs for user data first. If not found it creates a
  78. // new cocoa object representation of the media descriptor.
  79. return [VLCMedia mediaWithLibVLCMediaDescriptor:p_md];
  80. }
  81. - (int)count
  82. {
  83. libvlc_exception_t p_e;
  84. libvlc_exception_init( &p_e );
  85. int result = libvlc_media_list_view_count( p_mlv, &p_e );
  86. quit_on_exception( &p_e );
  87. return result;
  88. }
  89. @end
  90. @implementation VLCMediaListAspect (LibVLCBridging)
  91. + (id)mediaListAspectWithLibVLCMediaListView:(libvlc_media_list_view_t *)p_new_mlv;
  92. {
  93. return [[[VLCMediaListAspect alloc] initWithLibVLCMediaListView:p_new_mlv] autorelease];
  94. }
  95. - (id)initWithLibVLCMediaListView:(libvlc_media_list_view_t *)p_new_mlv;
  96. {
  97. if( self = [super init] )
  98. {
  99. p_mlv = p_new_mlv;
  100. libvlc_media_list_view_retain(p_mlv);
  101. //libvlc_media_list_lock(p_mlist);
  102. cachedMedia = [[NSMutableArray alloc] initWithCapacity:libvlc_media_list_view_count(p_mlv, NULL)];
  103. int i, count = libvlc_media_list_view_count(p_mlv, NULL);
  104. for( i = 0; i < count; i++ )
  105. {
  106. libvlc_media_descriptor_t * p_md = libvlc_media_list_view_item_at_index(p_mlv, i, NULL);
  107. [cachedMedia addObject:[VLCMedia mediaWithLibVLCMediaDescriptor: p_md]];
  108. libvlc_media_descriptor_release(p_md);
  109. }
  110. [self initInternalMediaListView];
  111. //libvlc_media_list_unlock(p_mlist);
  112. }
  113. return self;
  114. }
  115. - (libvlc_media_list_view_t *)libVLCMediaListView
  116. {
  117. return (libvlc_media_list_view_t *)p_mlv;
  118. }
  119. @end
  120. @implementation VLCMediaListAspect (Private)
  121. - (void)initInternalMediaListView
  122. {
  123. libvlc_exception_t e;
  124. libvlc_exception_init( &e );
  125. libvlc_event_manager_t *p_em = libvlc_media_list_event_manager( p_mlv, &e );
  126. /* Add internal callback */
  127. libvlc_event_attach( p_em, libvlc_MediaListViewItemAdded, HandleMediaListViewItemAdded, self, &e );
  128. libvlc_event_attach( p_em, libvlc_MediaListViewItemDeleted, HandleMediaListViewItemDeleted, self, &e );
  129. quit_on_exception( &e );
  130. }
  131. @end