Controller.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. videoView.fillScreen = YES;
  18. playlist = [[VLCMediaList alloc] init];
  19. [playlist addObserver:self forKeyPath:@"media" options:NSKeyValueObservingOptionNew context:nil];
  20. player = [[VLCMediaPlayer alloc] initWithVideoView:videoView];
  21. mediaIndex = -1;
  22. [playlistOutline registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, NSURLPboardType, nil]];
  23. [playlistOutline setDoubleAction:@selector(changeAndPlay:)];
  24. }
  25. - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
  26. {
  27. }
  28. - (void)applicationWillTerminate:(NSNotification *)aNotification
  29. {
  30. [playlist removeObserver:self forKeyPath:@"media"];
  31. [player pause];
  32. [player setMedia:nil];
  33. [player release];
  34. [playlist release];
  35. [videoView release];
  36. }
  37. - (void)changeAndPlay:(id)sender
  38. {
  39. if ([playlistOutline selectedRow] != mediaIndex)
  40. {
  41. [self setMediaIndex:[playlistOutline selectedRow]];
  42. if (![player isPlaying])
  43. [player play];
  44. }
  45. }
  46. - (void)setMediaIndex:(int)value
  47. {
  48. if ([playlist count] <= 0)
  49. return;
  50. if (value < 0)
  51. value = 0;
  52. if (value > [playlist count] - 1)
  53. value = [playlist count] - 1;
  54. mediaIndex = value;
  55. [player setMedia:[playlist mediaAtIndex:mediaIndex]];
  56. }
  57. - (void)play:(id)sender
  58. {
  59. [self setMediaIndex:mediaIndex+1];
  60. if (![player isPlaying])
  61. {
  62. NSLog(@"%@ length = %@", [playlist mediaAtIndex:mediaIndex], [[playlist mediaAtIndex:mediaIndex] lengthWaitUntilDate:[NSDate dateWithTimeIntervalSinceNow:60]]);
  63. [player play];
  64. }
  65. }
  66. - (void)pause:(id)sender
  67. {
  68. NSLog(@"Sending pause message to media player...");
  69. [player pause];
  70. }
  71. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  72. {
  73. if ([keyPath isEqualToString:@"media"] && object == playlist) {
  74. [playlistOutline reloadData];
  75. }
  76. }
  77. // NSTableView Implementation
  78. - (int)numberOfRowsInTableView:(NSTableView *)tableView
  79. {
  80. return [playlist count];
  81. }
  82. - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn
  83. row:(int)row
  84. {
  85. return [(VLCMedia *)[playlist mediaAtIndex:row].metaDictionary valueForKey:VLCMetaInformationTitle];
  86. }
  87. - (NSDragOperation)tableView:(NSTableView*)tv validateDrop:(id <NSDraggingInfo>)info
  88. proposedRow:(int)row proposedDropOperation:(NSTableViewDropOperation)op
  89. {
  90. return NSDragOperationEvery; /* This is for now */
  91. }
  92. - (BOOL)tableView:(NSTableView *)aTableView acceptDrop:(id <NSDraggingInfo>)info
  93. row:(int)row dropOperation:(NSTableViewDropOperation)operation
  94. {
  95. int i;
  96. NSArray *droppedItems = [[info draggingPasteboard] propertyListForType:NSFilenamesPboardType];
  97. for (i = 0; i < [droppedItems count]; i++)
  98. {
  99. NSString * filename = [droppedItems objectAtIndex:i];
  100. VLCMedia * media = [VLCMedia mediaWithURL:[NSURL fileURLWithPath:filename]];
  101. [playlist addMedia:media];
  102. }
  103. return YES;
  104. }
  105. @end