Controller.m 4.6 KB

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