Controller.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #import "Controller.h"
  2. static void *sleepForMe(void)
  3. {
  4. while (1) sleep(60);
  5. }
  6. @implementation Controller
  7. - (void)awakeFromNib
  8. {
  9. // atexit((void*)sleepForMe); // Only used for memory leak debugging
  10. [NSApp setDelegate:self];
  11. // Allocate a VLCVideoView instance and tell it what area to occupy.
  12. NSRect rect = NSMakeRect(0, 0, 0, 0);
  13. rect.size = [videoHolderView frame].size;
  14. videoView = [[VLCVideoView alloc] initWithFrame:rect];
  15. [videoHolderView addSubview:videoView];
  16. [videoView setAutoresizingMask: NSViewHeightSizable|NSViewWidthSizable];
  17. playlist = [[VLCMediaList alloc] init];
  18. [playlist addObserver:self forKeyPath:@"media" options:NSKeyValueObservingOptionNew context:nil];
  19. player = [[VLCMediaPlayer alloc] initWithVideoView:videoView];
  20. mediaIndex = -1;
  21. [playlistOutline registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, NSURLPboardType, nil]];
  22. [playlistOutline setDoubleAction:@selector(changeAndPlay:)];
  23. }
  24. - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
  25. {
  26. }
  27. - (void)applicationWillTerminate:(NSNotification *)aNotification
  28. {
  29. [playlist removeObserver:self forKeyPath:@"media"];
  30. [player pause];
  31. [player setMedia:nil];
  32. [player release];
  33. [playlist release];
  34. [videoView release];
  35. }
  36. - (void)changeAndPlay:(id)sender
  37. {
  38. if ([playlistOutline selectedRow] != mediaIndex)
  39. {
  40. [self setMediaIndex:[playlistOutline selectedRow]];
  41. if (![player isPlaying])
  42. [player play];
  43. }
  44. }
  45. - (void)setMediaIndex:(int)value
  46. {
  47. if ([playlist count] <= 0)
  48. return;
  49. if (value < 0)
  50. value = 0;
  51. if (value > [playlist count] - 1)
  52. value = [playlist count] - 1;
  53. mediaIndex = value;
  54. [player setMedia:[playlist mediaAtIndex:mediaIndex]];
  55. }
  56. - (void)play:(id)sender
  57. {
  58. [self setMediaIndex:mediaIndex+1];
  59. if (![player isPlaying])
  60. [player play];
  61. }
  62. - (void)pause:(id)sender
  63. {
  64. NSLog(@"Sending pause message to media player...");
  65. [player pause];
  66. }
  67. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  68. {
  69. if ([keyPath isEqualToString:@"media"] && object == playlist) {
  70. [playlistOutline reloadData];
  71. }
  72. }
  73. // NSTableView Implementation
  74. - (int)numberOfRowsInTableView:(NSTableView *)tableView
  75. {
  76. return [playlist count];
  77. }
  78. - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn
  79. row:(int)row
  80. {
  81. return [(VLCMedia *)[playlist mediaAtIndex:row].metaDictionary valueForKey:VLCMetaInformationTitle];
  82. }
  83. - (NSDragOperation)tableView:(NSTableView*)tv validateDrop:(id <NSDraggingInfo>)info
  84. proposedRow:(int)row proposedDropOperation:(NSTableViewDropOperation)op
  85. {
  86. return NSDragOperationEvery; /* This is for now */
  87. }
  88. - (BOOL)tableView:(NSTableView *)aTableView acceptDrop:(id <NSDraggingInfo>)info
  89. row:(int)row dropOperation:(NSTableViewDropOperation)operation
  90. {
  91. int i;
  92. NSArray *droppedItems = [[info draggingPasteboard] propertyListForType:NSFilenamesPboardType];
  93. for (i = 0; i < [droppedItems count]; i++)
  94. {
  95. NSString * filename = [droppedItems objectAtIndex:i];
  96. VLCMedia * media = [VLCMedia mediaWithURL:[NSURL fileURLWithPath:filename]];
  97. NSLog(@"%@ length = %@", media, [media lengthWaitUntilDate:[NSDate dateWithTimeIntervalSinceNow:60]]);
  98. [playlist addMedia:media];
  99. }
  100. return YES;
  101. }
  102. @end