Controller.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*****************************************************************************
  2. * test: Controller.m
  3. *****************************************************************************
  4. * Copyright (C) 2007-2012 Pierre d'Herbemont and VideoLAN
  5. *
  6. * Authors: Pierre d'Herbemont
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation; either version 2.1 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with this program; if not, write to the Free Software Foundation,
  20. * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  21. *****************************************************************************/
  22. #import "Controller.h"
  23. static void *sleepForMe(void)
  24. {
  25. while (1) sleep(60);
  26. }
  27. @implementation Controller
  28. - (void)awakeFromNib
  29. {
  30. // atexit((void*)sleepForMe); // Only used for memory leak debugging
  31. [NSApp setDelegate:self];
  32. // Allocate a VLCVideoView instance and tell it what area to occupy.
  33. NSRect rect = NSMakeRect(0, 0, 0, 0);
  34. rect.size = [videoHolderView frame].size;
  35. videoView = [[VLCVideoView alloc] initWithFrame:rect];
  36. [videoHolderView addSubview:videoView];
  37. [videoView setAutoresizingMask: NSViewHeightSizable|NSViewWidthSizable];
  38. videoView.fillScreen = YES;
  39. playlist = [[VLCMediaList alloc] init];
  40. [playlist addObserver:self forKeyPath:@"media" options:NSKeyValueObservingOptionNew context:nil];
  41. player = [[VLCMediaPlayer alloc] initWithVideoView:videoView];
  42. mediaIndex = -1;
  43. [playlistOutline registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, NSURLPboardType, nil]];
  44. [playlistOutline setDoubleAction:@selector(changeAndPlay:)];
  45. }
  46. - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
  47. {
  48. }
  49. - (void)applicationWillTerminate:(NSNotification *)aNotification
  50. {
  51. [playlist removeObserver:self forKeyPath:@"media"];
  52. [player pause];
  53. [player setMedia:nil];
  54. [player release];
  55. [playlist release];
  56. [videoView release];
  57. }
  58. - (void)changeAndPlay:(id)sender
  59. {
  60. if ([playlistOutline selectedRow] != mediaIndex)
  61. {
  62. [self setMediaIndex:[playlistOutline selectedRow]];
  63. if (![player isPlaying])
  64. [player play];
  65. }
  66. }
  67. - (void)setMediaIndex:(int)value
  68. {
  69. if ([playlist count] <= 0)
  70. return;
  71. if (value < 0)
  72. value = 0;
  73. if (value > [playlist count] - 1)
  74. value = [playlist count] - 1;
  75. mediaIndex = value;
  76. [player setMedia:[playlist mediaAtIndex:mediaIndex]];
  77. }
  78. - (void)play:(id)sender
  79. {
  80. [self setMediaIndex:mediaIndex+1];
  81. if (![player isPlaying])
  82. {
  83. NSLog(@"%@ length = %@", [playlist mediaAtIndex:mediaIndex], [[playlist mediaAtIndex:mediaIndex] lengthWaitUntilDate:[NSDate dateWithTimeIntervalSinceNow:60]]);
  84. [player play];
  85. }
  86. }
  87. - (void)pause:(id)sender
  88. {
  89. NSLog(@"Sending pause message to media player...");
  90. [player pause];
  91. }
  92. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  93. {
  94. if ([keyPath isEqualToString:@"media"] && object == playlist) {
  95. [playlistOutline reloadData];
  96. }
  97. }
  98. // NSTableView Implementation
  99. - (int)numberOfRowsInTableView:(NSTableView *)tableView
  100. {
  101. return [playlist count];
  102. }
  103. - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn
  104. row:(int)row
  105. {
  106. return [(VLCMedia *)[playlist mediaAtIndex:row].metaDictionary valueForKey:VLCMetaInformationTitle];
  107. }
  108. - (NSDragOperation)tableView:(NSTableView*)tv validateDrop:(id <NSDraggingInfo>)info
  109. proposedRow:(int)row proposedDropOperation:(NSTableViewDropOperation)op
  110. {
  111. return NSDragOperationEvery; /* This is for now */
  112. }
  113. - (BOOL)tableView:(NSTableView *)aTableView acceptDrop:(id <NSDraggingInfo>)info
  114. row:(int)row dropOperation:(NSTableViewDropOperation)operation
  115. {
  116. int i;
  117. NSArray *droppedItems = [[info draggingPasteboard] propertyListForType:NSFilenamesPboardType];
  118. for (i = 0; i < [droppedItems count]; i++)
  119. {
  120. NSString * filename = [droppedItems objectAtIndex:i];
  121. VLCMedia * media = [VLCMedia mediaWithURL:[NSURL fileURLWithPath:filename]];
  122. [playlist addMedia:media];
  123. }
  124. return YES;
  125. }
  126. @end