Controller.m 4.6 KB

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