playlistinfo.m
上传用户:riyaled888
上传日期:2009-03-27
资源大小:7338k
文件大小:15k
源码类别:

多媒体

开发平台:

MultiPlatform

  1. /*****************************************************************************
  2.  r playlistinfo.m: MacOS X interface module
  3.  *****************************************************************************
  4.  * Copyright (C) 2002-2004 VideoLAN
  5.  * $Id: playlistinfo.m 8121 2004-07-05 01:27:35Z hartman $
  6.  *
  7.  * Authors: Benjamin Pracht <bigben at videolan dot org>
  8.  *
  9.  * This program is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2 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 General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  22.  *****************************************************************************/
  23. /*****************************************************************************
  24.  * Preamble
  25.  *****************************************************************************/
  26. #include "intf.h"
  27. #include "playlistinfo.h"
  28. #include "playlist.h"
  29. /*****************************************************************************
  30.  * VLCPlaylistInfo Implementation
  31.  *****************************************************************************/
  32. @implementation VLCInfo
  33. - (id)init
  34. {
  35.     self = [super init];
  36.     if( self != nil )
  37.     {
  38.         i_item = -1;
  39.         o_selected = NULL;
  40.     }
  41.     return( self );
  42. }
  43. - (void)dealloc
  44. {
  45.     [o_selected release];
  46.     [super dealloc];
  47. }
  48. - (void)awakeFromNib
  49. {
  50.     [o_info_window setExcludedFromWindowsMenu: TRUE];
  51.     [o_info_window setTitle: _NS("Properties")];
  52.     [o_uri_lbl setStringValue: _NS("URI")];
  53.     [o_title_lbl setStringValue: _NS("Title")];
  54.     [o_author_lbl setStringValue: _NS("Author")];
  55.     [o_btn_ok setTitle: _NS("OK")];
  56.     [o_btn_cancel setTitle: _NS("Cancel")];
  57.     [o_btn_delete_group setTitle: _NS("Delete Group")];
  58.     [o_btn_add_group setTitle: _NS("Add Group")];
  59.     [o_group_lbl setStringValue: _NS("Group")];
  60. }
  61. - (IBAction)togglePlaylistInfoPanel:(id)sender
  62. {
  63.     if( [o_info_window isVisible] )
  64.     {
  65.         [o_info_window orderOut: sender];
  66.     }
  67.     else
  68.     {
  69.         i_item = [[[VLCMain sharedInstance] getPlaylist] selectedPlaylistItem];
  70.         o_selected = [[[VLCMain sharedInstance] getPlaylist] selectedPlaylistItemsList];
  71.         [o_selected retain];
  72.         [self initPanel:sender];
  73.     }
  74. }
  75. - (IBAction)toggleInfoPanel:(id)sender
  76. {
  77.     if( [o_info_window isVisible] )
  78.     {
  79.         [o_info_window orderOut: sender];
  80.     }
  81.     else
  82.     {
  83.         intf_thread_t * p_intf = VLCIntf;
  84.         playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
  85.                                           FIND_ANYWHERE );
  86.         if (p_playlist)
  87.         {
  88.             i_item = p_playlist->i_index;
  89.             o_selected = [NSMutableArray arrayWithObject:
  90.                             [NSNumber numberWithInt:i_item]];
  91.             [o_selected retain];
  92.             vlc_object_release(p_playlist);
  93.         }
  94.         [self initPanel:sender];
  95.     }
  96. }
  97. - (void)initPanel:(id)sender
  98. {
  99.     intf_thread_t * p_intf = VLCIntf;
  100.     playlist_t * p_playlist;
  101.     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
  102.                                           FIND_ANYWHERE );
  103.     if( p_playlist )
  104.     {
  105.         char *psz_temp;
  106.         /*fill uri / title / author info */
  107.         [o_uri_txt setStringValue:
  108.             ([NSString stringWithUTF8String:p_playlist->
  109.                pp_items[i_item]->input.psz_uri] == nil ) ?
  110.             [NSString stringWithCString:p_playlist->
  111.                 pp_items[i_item]->input.psz_uri] :
  112.             [NSString stringWithUTF8String:p_playlist->
  113.                 pp_items[i_item]->input.psz_uri]];
  114.         [o_title_txt setStringValue:
  115.             ([NSString stringWithUTF8String:p_playlist->
  116.                 pp_items[i_item]->input.psz_name] == nil ) ?
  117.             [NSString stringWithCString:p_playlist->
  118.                 pp_items[i_item]->input.psz_name] :
  119.             [NSString stringWithUTF8String:p_playlist->
  120.                 pp_items[i_item]->input.psz_name]];
  121.         psz_temp = playlist_GetInfo( p_playlist, i_item ,_("General"),_("Author") );
  122.         [o_author_txt setStringValue: [NSString stringWithUTF8String: psz_temp]];
  123.         free( psz_temp );
  124.         [[VLCInfoTreeItem rootItem] refresh];
  125.         [o_outline_view reloadData];
  126.         [self createComboBox];
  127.         [self handleGroup:self];
  128.         vlc_object_release( p_playlist );
  129.     }
  130.     [o_info_window makeKeyAndOrderFront: sender];
  131. }
  132. - (IBAction)infoCancel:(id)sender
  133. {
  134.     [o_info_window orderOut: self];
  135. }
  136. - (IBAction)infoOk:(id)sender
  137. {
  138.     int i,i_row,c;
  139.     intf_thread_t * p_intf = VLCIntf;
  140.     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
  141.                                           FIND_ANYWHERE );
  142.     vlc_value_t val;
  143.     NSNumber * o_number;
  144.     if (p_playlist)
  145.     {
  146.         vlc_mutex_lock(&p_playlist->pp_items[i_item]->input.lock);
  147.         p_playlist->pp_items[i_item]->input.psz_uri =
  148.             strdup([[o_uri_txt stringValue] cString]);
  149.         p_playlist->pp_items[i_item]->input.psz_name =
  150.             strdup([[o_title_txt stringValue] cString]);
  151.         playlist_ItemAddInfo(p_playlist->pp_items[i_item],_("General"),_("Author"), [[o_author_txt stringValue] cString]);
  152.         c = (int)[o_selected count];
  153.         if ([[o_group_cbx stringValue] isEqual:
  154.                     [o_group_cbx objectValueOfSelectedItem]])
  155.         {
  156.             for (i = 0 ; i < c ; i++)
  157.             {
  158.                 o_number = [o_selected lastObject];
  159.                 i_row = [o_number intValue];
  160.                 p_playlist->pp_items[i_row]->i_group = p_playlist->
  161.                     pp_groups[[o_group_cbx indexOfSelectedItem]]->i_id;
  162.                 [o_selected removeObject: o_number];
  163.             }
  164.         }
  165.         else
  166.         {
  167.             playlist_group_t * p_group = playlist_CreateGroup( p_playlist,
  168.                 strdup([[o_group_cbx stringValue] cString]));
  169.             if (p_group)
  170.             {
  171.                 for (i = 0 ; i < c ; i++)
  172.                 {
  173.                     o_number = [o_selected lastObject];
  174.                     i_row = [o_number intValue];
  175.                     p_playlist->pp_items[i_row]->i_group = p_group->i_id;
  176.                     [o_selected removeObject: o_number];
  177.                 }
  178.             }
  179.         }
  180.         vlc_mutex_unlock(&p_playlist->pp_items[i_item]->input.lock);
  181.         val.b_bool = VLC_TRUE;
  182.         var_Set( p_playlist,"intf-change",val );
  183.         vlc_object_release ( p_playlist );
  184.     }
  185.     [o_info_window orderOut: self];
  186. }
  187. - (IBAction)handleGroup:(id)sender
  188. {
  189.     intf_thread_t * p_intf = VLCIntf;
  190.     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
  191.                                           FIND_ANYWHERE );
  192.     if (p_playlist)
  193.     {
  194.         if ([[o_group_cbx stringValue] isEqual:
  195.                     [o_group_cbx objectValueOfSelectedItem]])
  196.         {
  197.             [o_group_color setBackgroundColor:[[[VLCMain sharedInstance] getPlaylist]
  198.                 getColor: p_playlist->pp_groups[
  199.                 [o_group_cbx indexOfSelectedItem]]->i_id]];
  200.         }
  201.         else
  202.         {
  203.             [o_group_color setBackgroundColor:[[[VLCMain sharedInstance] getPlaylist]
  204.                 getColor:p_playlist->pp_groups[
  205.                 [o_group_cbx numberOfItems] - 1]->i_id + 1]];
  206.         }
  207.     vlc_object_release(p_playlist);
  208.     }
  209. }
  210. - (IBAction)deleteOutlineGroup:(id)sender
  211. {
  212.     intf_thread_t * p_intf = VLCIntf;
  213.     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
  214.                                           FIND_ANYWHERE );
  215.     if(p_playlist)
  216.     {
  217.         if ([[o_group_cbx stringValue] isEqual:
  218.                     [o_group_cbx objectValueOfSelectedItem]])
  219.         {
  220.             [[[VLCMain sharedInstance] getPlaylist] deleteGroup:p_playlist->pp_groups[
  221.                     [o_group_cbx indexOfSelectedItem]]->i_id];
  222.             [self createComboBox];
  223.             [self handleGroup:self];
  224.             [o_group_cbx reloadData];
  225.         }
  226.         else
  227.         {
  228.             msg_Warn(p_playlist,"Group doesn't exist, cannot delete");
  229.         }
  230.     vlc_object_release(p_playlist);
  231.     }
  232. }
  233. - (IBAction)createOutlineGroup:(id)sender;
  234. {
  235.     intf_thread_t * p_intf = VLCIntf;
  236.     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
  237.                                           FIND_ANYWHERE );
  238.     if(p_playlist)
  239.     {
  240.         playlist_CreateGroup( p_playlist,
  241.                     strdup([[o_group_cbx stringValue] cString]));
  242.         [self createComboBox];
  243.         [o_group_cbx reloadData];
  244.         [[[VLCMain sharedInstance] getPlaylist] playlistUpdated];
  245.         vlc_object_release(p_playlist);
  246.     }
  247. }
  248. -(void)createComboBox
  249. {
  250.     intf_thread_t * p_intf = VLCIntf;
  251.     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
  252.                                           FIND_ANYWHERE );
  253.     int i;
  254.     [o_group_cbx removeAllItems];
  255.     if (p_playlist)
  256.     {
  257.         for (i = 0; i < p_playlist->i_groups ; i++)
  258.         {
  259.             [o_group_cbx addItemWithObjectValue:
  260.                 [NSString stringWithUTF8String:
  261.                 p_playlist->pp_groups[i]->psz_name]];
  262.             if (p_playlist->pp_items[i_item]->i_group == p_playlist
  263.                 ->pp_groups[i]->i_id)
  264.             {
  265.                 [o_group_cbx selectItemAtIndex:i];
  266.             }
  267.         }
  268.     vlc_object_release(p_playlist);
  269.     }
  270. }
  271. - (int)getItem
  272. {
  273.     return i_item;
  274. }
  275. @end
  276. @implementation VLCInfo (NSMenuValidation)
  277. - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
  278. {
  279.     BOOL bEnabled = TRUE;
  280.     intf_thread_t * p_intf = VLCIntf;
  281.     input_thread_t * p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
  282.                                                        FIND_ANYWHERE );
  283.     if( [[o_mi title] isEqualToString: _NS("Info")] )
  284.     {
  285.         if( p_input == NULL )
  286.         {
  287.             bEnabled = FALSE;
  288.         }
  289.     }
  290.     if( p_input ) vlc_object_release( p_input );
  291.     return( bEnabled );
  292. }
  293. @end
  294. @implementation VLCInfo (NSTableDataSource)
  295. - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
  296. {
  297.     return (item == nil) ? [[VLCInfoTreeItem rootItem] numberOfChildren] : [item numberOfChildren];
  298. }
  299. - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
  300.     return ([item numberOfChildren] > 0);
  301. }
  302. - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
  303. {
  304.     return (item == nil) ? [[VLCInfoTreeItem rootItem] childAtIndex:index] : [item childAtIndex:index];
  305. }
  306. - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
  307. {
  308.     if ([[tableColumn identifier] isEqualToString:@"0"])
  309.     {
  310.         return (item == nil) ? @"" : (id)[item getName];
  311.     }
  312.     else
  313.     {
  314.         return (item == nil) ? @"" : (id)[item getValue];
  315.     }
  316. }
  317. @end
  318. @implementation VLCInfoTreeItem
  319. static VLCInfoTreeItem *o_root_item = nil;
  320. #define IsALeafNode ((id)-1)
  321. - (id)initWithName: (NSString *)o_item_name value: (NSString *)o_item_value ID: (int)i_id parent:(VLCInfoTreeItem *)o_parent_item
  322. {
  323.     self = [super init];
  324.     if( self != nil )
  325.     {
  326.         o_name = [o_item_name copy];
  327.         o_value = [o_item_value copy];
  328.         i_object_id = i_id;
  329.         o_parent = o_parent_item;
  330.         i_item = [[[VLCMain sharedInstance] getInfo] getItem];
  331.     }
  332.     return( self );
  333. }
  334. + (VLCInfoTreeItem *)rootItem {
  335.     if (o_root_item == nil) o_root_item = [[VLCInfoTreeItem alloc] initWithName:@"main" value: @"" ID: 0 parent:nil];
  336.     return o_root_item;
  337. }
  338. - (void)dealloc
  339. {
  340.     if (o_children != IsALeafNode) [o_children release];
  341.     [o_name release];
  342.     [super dealloc];
  343. }
  344. /* Creates and returns the array of children
  345.  * Loads children incrementally */
  346. - (NSArray *)children
  347. {
  348.     if (o_children == NULL)
  349.     {
  350.         intf_thread_t * p_intf = VLCIntf;
  351.         playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
  352.                                           FIND_ANYWHERE );
  353.         int i;
  354.         if (p_playlist)
  355.         {
  356.             if (i_item > -1)
  357.             {
  358.                 if (self == o_root_item)
  359.                 {
  360.                     o_children = [[NSMutableArray alloc] initWithCapacity:p_playlist->pp_items[i_item]->input.i_categories];
  361.                     for (i = 0 ; i<p_playlist->pp_items[i_item]->input.i_categories ; i++)
  362.                     {
  363.                         [o_children addObject:[[VLCInfoTreeItem alloc]
  364.                             initWithName: [NSString stringWithUTF8String:
  365.                                 p_playlist->pp_items[i_item]->input.
  366.                                 pp_categories[i]->psz_name]
  367.                             value: @""
  368.                             ID: i
  369.                             parent: self]];
  370.                     }
  371.                 }
  372.                 else if (o_parent == o_root_item)
  373.                 {
  374.                     o_children = [[NSMutableArray alloc] initWithCapacity:
  375.                         p_playlist->pp_items[i_item]->input.
  376.                         pp_categories[i_object_id]->i_infos];
  377.                     for (i = 0 ; i<p_playlist->pp_items[i_item]->input.
  378.                            pp_categories[i_object_id]->i_infos ; i++)
  379.                     {
  380.                         [o_children addObject:[[VLCInfoTreeItem alloc]
  381.                         initWithName: [NSString stringWithUTF8String:
  382.                                 p_playlist->pp_items[i_item]->input.
  383.                                 pp_categories[i_object_id]->
  384.                                 pp_infos[i]->psz_name]
  385.                             value: [NSString stringWithUTF8String:
  386.                                 p_playlist->pp_items[i_item]->input.
  387.                                 pp_categories[i_object_id]->
  388.                                 pp_infos[i]->psz_value]
  389.                             ID: i
  390.                             parent: self]];
  391.                     }
  392.                 }
  393.                 else
  394.                 {
  395.                     o_children = IsALeafNode;
  396.                 }
  397.             }
  398.             vlc_object_release(p_playlist);
  399.         }
  400.     }
  401.     return o_children;
  402. }
  403. - (NSString *)getName
  404. {
  405.     return o_name;
  406. }
  407. - (NSString *)getValue
  408. {
  409.     return o_value;
  410. }
  411. - (VLCInfoTreeItem *)childAtIndex:(int)i_index {
  412.     return [[self children] objectAtIndex:i_index];
  413. }
  414. - (int)numberOfChildren {
  415.     id i_tmp = [self children];
  416.     return (i_tmp == IsALeafNode) ? (-1) : (int)[i_tmp count];
  417. }
  418. /*- (int)selectedPlaylistItem
  419. {
  420.     return i_item;
  421. }
  422. */
  423. - (void)refresh
  424. {
  425.     i_item = [[[VLCMain sharedInstance] getInfo] getItem];
  426.     if (o_children != NULL)
  427.     {
  428.         [o_children release];
  429.         o_children = NULL;
  430.     }
  431. }
  432. @end