ExtensionsManager.m 11.4 KB
Newer Older
1 2 3 4 5 6
/*****************************************************************************
 * extensions_manager.cpp: Extensions manager for Cocoa
 ****************************************************************************
 * Copyright (C) 2009-2012 VideoLAN and authors
 * $Id$
 *
7
 * Authors: Brendon Justin <brendonjustin@gmail.com>,
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
 *          Jean-Philippe André < jpeg # videolan.org >
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#import "ExtensionsManager.h"

#import "ExtensionsDialogProvider.h"

#import <vlc_modules.h>
#import "assert.h"

#define MENU_MAP(a,e) ((uint32_t)( (((uint16_t)a) << 16) | ((uint16_t)e) ))
#define MENU_GET_ACTION(a) ( (uint16_t)( ((uint32_t)a) >> 16 ) )
#define MENU_GET_EXTENSION(a) ( (uint16_t)( ((uint32_t)a) & 0xFFFF ) )

36
@implementation ExtensionsManager
37 38 39

static ExtensionsManager* instance = nil;

40
@synthesize isUnloading = b_unloading;
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

+ (ExtensionsManager *)getInstance:( intf_thread_t *)_p_intf
{
    if( !instance )
        instance = [[[ExtensionsManager alloc] initWithIntf:_p_intf] autorelease];
    return instance;
}

- (id)initWithIntf:(intf_thread_t *)_p_intf
{
    if ((self = [super init]))
    {
        p_intf = _p_intf;
        p_extensions_manager = NULL;
        p_edp = NULL;

        p_extDict = [[NSMutableDictionary alloc] init];

        b_unloading = false;
        b_failed = false;

        delegate = nil;
    }

    return self;
}

/** Get the extensions_manager_t if it is loaded and hold the object */
- (extensions_manager_t *)getManager
{
    if( !p_extensions_manager )
        return NULL;

    vlc_object_hold( p_extensions_manager );
    return p_extensions_manager;
}

- (void)buildMenu:(NSMenu *)extMenu
{
    assert( extMenu != nil );
    if( ![self isLoaded] )
    {
        // This case can happen: do nothing
        return;
    }

    vlc_mutex_lock( &p_extensions_manager->lock );

    extension_t *p_ext = NULL;
    int i_ext = 0;
    FOREACH_ARRAY( p_ext, p_extensions_manager->extensions )
    {
        bool b_Active = extension_IsActivated( p_extensions_manager, p_ext );

95
        NSString *titleString = [NSString stringWithCString:p_ext->psz_title
96
                                                   encoding:NSUTF8StringEncoding];
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118

        if( b_Active && extension_HasMenu( p_extensions_manager, p_ext ) )
        {
            NSMenu *submenu = [[NSMenu alloc] initWithTitle:titleString];
            NSMenuItem *submenuItem = [extMenu addItemWithTitle:titleString
                                                         action:nil
                                                  keyEquivalent:@""];

            [extMenu setSubmenu:submenu forItem:submenuItem];
            [submenu release];

            char **ppsz_titles = NULL;
            uint16_t *pi_ids = NULL;
            size_t i_num = 0;

            if( extension_GetMenu( p_extensions_manager, p_ext,
                                   &ppsz_titles, &pi_ids ) == VLC_SUCCESS )
            {
                for( int i = 0; ppsz_titles[i] != NULL; ++i )
                {
                    ++i_num;
                    titleString = [NSString stringWithCString:ppsz_titles[i]
119
                                                     encoding:NSUTF8StringEncoding];
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
                    NSMenuItem *menuItem = [submenu addItemWithTitle:titleString
                                                              action:@selector(triggerMenu:)
                                                       keyEquivalent:@""];
                    [menuItem setTarget:self];
                    menuItem.tag = MENU_MAP(pi_ids[i], i_ext);

                    free( ppsz_titles[i] );
                }
                if( !i_num )
                {
                    NSMenuItem *menuItem = [submenu addItemWithTitle:@"Empty"
                                                              action:@selector(triggerMenu:)
                                                       keyEquivalent:@""];
                    [menuItem setEnabled:NO];
                }
                free( ppsz_titles );
                free( pi_ids );
            }
            else
            {
                msg_Warn( p_intf, "Could not get menu for extension '%s'",
                          p_ext->psz_title );
                NSMenuItem *menuItem = [submenu addItemWithTitle:@"Empty"
                                                          action:@selector(triggerMenu:)
                                                   keyEquivalent:@""];
                [menuItem setEnabled:NO];
            }

            [submenu addItem:[NSMenuItem separatorItem]];

150 151
            NSMenuItem *deactivateItem = [submenu addItemWithTitle:@"Deactivate"
                                                            action:@selector(triggerMenu:)
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
                                                     keyEquivalent:@""];
            [deactivateItem setTarget:self];
            deactivateItem.tag = MENU_MAP(0, i_ext);
        }
        else
        {
            NSMenuItem *menuItem = [extMenu addItemWithTitle:titleString
                                                     action:@selector(triggerMenu:)
                                              keyEquivalent:@""];
            [menuItem setTarget:self];

            if( !extension_TriggerOnly( p_extensions_manager, p_ext ) )
            {
                if ( b_Active )
                    [menuItem setState:NSOnState];
            }
            menuItem.tag = MENU_MAP(0, i_ext);
        }
        i_ext++;
    }
    FOREACH_END()

    vlc_mutex_unlock( &p_extensions_manager->lock );
}

- (BOOL)loadExtensions
{
    if( !p_extensions_manager )
    {
        p_extensions_manager = ( extensions_manager_t* )
                    vlc_object_create( p_intf, sizeof( extensions_manager_t ) );
        if( !p_extensions_manager )
        {
            b_failed = true;
            [delegate extensionsUpdated];
            return false;
        }

        p_extensions_manager->p_module =
                module_need( p_extensions_manager, "extension", NULL, false );

        if( !p_extensions_manager->p_module )
        {
            msg_Err( p_intf, "Unable to load extensions module" );
            vlc_object_release( p_extensions_manager );
            p_extensions_manager = NULL;
            b_failed = true;
            [delegate extensionsUpdated];
            return false;
        }

        /* Initialize dialog provider */
        p_edp = [ExtensionsDialogProvider sharedInstance:p_intf];
        [p_edp retain];

        if( !p_edp )
        {
            msg_Err( p_intf, "Unable to create dialogs provider for extensions" );
            module_unneed( p_extensions_manager,
                           p_extensions_manager->p_module );
            vlc_object_release( p_extensions_manager );
            p_extensions_manager = NULL;
            b_failed = true;
            [delegate extensionsUpdated];
            return false;
        }
        b_unloading = false;
    }
    b_failed = false;
    [delegate extensionsUpdated];
    return true;
}

- (void)unloadExtensions
{
    if( !p_extensions_manager )
        return;
    b_unloading = true;
    [p_edp release];
    module_unneed( p_extensions_manager, p_extensions_manager->p_module );
    vlc_object_release( p_extensions_manager );
    p_extensions_manager = NULL;
}

- (void)reloadExtensions
{
    [self unloadExtensions];
    [self loadExtensions];

    if (delegate)
        [delegate extensionsUpdated];
}

- (void)triggerMenu:(id)sender
{
    uint32_t identifier = [(NSMenuItem *)sender tag];

    uint16_t i_ext = MENU_GET_EXTENSION( identifier );
    uint16_t i_action = MENU_GET_ACTION( identifier );

    vlc_mutex_lock( &p_extensions_manager->lock );

    if( (int) i_ext > p_extensions_manager->extensions.i_size )
    {
        msg_Dbg( p_intf, "can't trigger extension with wrong id %d",
                 (int) i_ext );
        return;
    }

    extension_t *p_ext = ARRAY_VAL( p_extensions_manager->extensions, i_ext );
    assert( p_ext != NULL);

    vlc_mutex_unlock( &p_extensions_manager->lock );

    if( i_action == 0 )
    {
        msg_Dbg( p_intf, "activating or triggering extension '%s', id %d",
                 p_ext->psz_title, i_ext );

        if( extension_TriggerOnly( p_extensions_manager, p_ext ) )
        {
            extension_Trigger( p_extensions_manager, p_ext );
        }
        else
        {
            if( !extension_IsActivated( p_extensions_manager, p_ext ) )
                extension_Activate( p_extensions_manager, p_ext );
            else
                extension_Deactivate( p_extensions_manager, p_ext );
        }
    }
    else
    {
        msg_Dbg( p_intf, "triggering extension '%s', on menu with id = 0x%x",
                 p_ext->psz_title, i_action );

        extension_TriggerMenu( p_extensions_manager, p_ext, i_action );
    }
}

- (void)inputChanged:(input_thread_t *)p_input
{
    //This is unlikely, but can happen if no extension modules can be loaded.
    if ( p_extensions_manager == NULL )
        return ;
    vlc_mutex_lock( &p_extensions_manager->lock );

    extension_t *p_ext;
    FOREACH_ARRAY( p_ext, p_extensions_manager->extensions )
    {
        if( extension_IsActivated( p_extensions_manager, p_ext ) )
        {
            extension_SetInput( p_extensions_manager, p_ext, p_input );
        }
    }
    FOREACH_END()

    vlc_mutex_unlock( &p_extensions_manager->lock );
}

- (void)playingChanged:(int)state
{
    //This is unlikely, but can happen if no extension modules can be loaded.
    if ( p_extensions_manager == NULL )
        return ;
    vlc_mutex_lock( &p_extensions_manager->lock );

    extension_t *p_ext;
    FOREACH_ARRAY( p_ext, p_extensions_manager->extensions )
    {
        if( extension_IsActivated( p_extensions_manager, p_ext ) )
        {
            extension_PlayingChanged( p_extensions_manager, p_ext, state );
        }
    }
    FOREACH_END()

    vlc_mutex_unlock( &p_extensions_manager->lock );
}

- (void)metaChanged:(input_item_t *)p_input
{
    //This is unlikely, but can happen if no extension modules can be loaded.
    if ( p_extensions_manager == NULL )
        return ;
    vlc_mutex_lock( &p_extensions_manager->lock );
    extension_t *p_ext;
    FOREACH_ARRAY( p_ext, p_extensions_manager->extensions )
    {
        if( extension_IsActivated( p_extensions_manager, p_ext ) )
        {
            extension_MetaChanged( p_extensions_manager, p_ext );
        }
    }
    FOREACH_END()
    vlc_mutex_unlock( &p_extensions_manager->lock );
}

- (void)dealloc
{
    msg_Dbg( p_intf, "Killing extension dialog provider" );
    [ExtensionsDialogProvider killInstance];

355 356
    if( p_extensions_manager )
        vlc_object_release( p_extensions_manager );
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372

    [p_extDict release];

    [super dealloc];
}

- (BOOL)isLoaded
{
    return p_extensions_manager != NULL;
}

- (BOOL)cannotLoad
{
    return b_unloading || b_failed;
}

373
@end