prefs.m 14.3 KB
Newer Older
1
/*****************************************************************************
2
 * prefs.m: MacOS X module for vlc
3
 *****************************************************************************
4
 * Copyright (C) 2002-2003 VideoLAN
Eric Petit's avatar
Eric Petit committed
5
 * $Id: prefs.m,v 1.39 2004/02/02 08:50:41 titer Exp $
6
 *
7
 * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
hartman's avatar
hartman committed
8
 *	    Derk-Jan Hartman <hartman at videolan.org>
9
10
11
12
13
 *
 * 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.
14
 *
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdlib.h>                                      /* malloc(), free() */
#include <string.h>

31
32
#include "intf.h"
#include "prefs.h"
hartman's avatar
hartman committed
33
34
35
36
37
38
39
#include "misc.h"
#include <vlc_help.h>

#define ROOT_ID 1241
#define GENERAL_ID 1242
#define MODULE_ID 1243
#define CAPABILITY_ID 1244
40

Eric Petit's avatar
Eric Petit committed
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
/*****************************************************************************
 * GetCapabilityHelp: Display the help for one capability.
 *****************************************************************************/
static char * GetCapabilityHelp( char *psz_capability, int i_type)
{
    if( psz_capability == NULL) return "";

    if( !strcasecmp(psz_capability,"access") )
        return i_type == 1 ? ACCESS_TITLE : ACCESS_HELP;
    if( !strcasecmp(psz_capability,"audio filter") )
        return i_type == 1 ? AUDIO_FILTER_TITLE : AUDIO_FILTER_HELP;
    if( !strcasecmp(psz_capability,"audio output") )
        return i_type == 1 ? AOUT_TITLE : AOUT_HELP;
    if( !strcasecmp(psz_capability,"audio encoder") )
        return i_type == 1 ? AOUT_ENC_TITLE : AOUT_ENC_HELP;
    if( !strcasecmp(psz_capability,"chroma") )
        return i_type == 1 ? CHROMA_TITLE : CHROMA_HELP;
    if( !strcasecmp(psz_capability,"decoder") )
        return i_type == 1 ? DECODER_TITLE : DECODER_HELP;
    if( !strcasecmp(psz_capability,"demux") )
        return i_type == 1 ? DEMUX_TITLE : DEMUX_HELP;
    if( !strcasecmp(psz_capability,"interface") )
        return i_type == 1 ? INTERFACE_TITLE : INTERFACE_HELP;
    if( !strcasecmp(psz_capability,"sout access") )
        return i_type == 1 ? SOUT_TITLE : SOUT_HELP;
    if( !strcasecmp(psz_capability,"subtitle demux") )
        return i_type == 1 ? SUBTITLE_DEMUX_TITLE : SUBTITLE_DEMUX_HELP;
    if( !strcasecmp(psz_capability,"text renderer") )
        return i_type == 1 ? TEXT_TITLE : TEXT_HELP;
    if( !strcasecmp(psz_capability,"video output") )
        return i_type == 1 ? VOUT__TITLE : VOUT_HELP;
    if( !strcasecmp(psz_capability,"video filter") )
        return i_type == 1 ? VIDEO_FILTER_TITLE : VIDEO_FILTER_HELP;

    return " ";
}

78
/*****************************************************************************
79
 * VLCPrefs implementation
80
81
82
83
84
85
86
87
88
 *****************************************************************************/
@implementation VLCPrefs

- (id)init
{
    self = [super init];

    if( self != nil )
    {
hartman's avatar
hartman committed
89
        o_empty_view = [[[NSView alloc] init] retain];
90
91
92
93
94
95
96
    }

    return( self );
}

- (void)dealloc
{
hartman's avatar
hartman committed
97
98
99
    [o_empty_view release];
    [super dealloc];
}
100

hartman's avatar
hartman committed
101
102
103
104
105
106
107
108
109
110
- (void)awakeFromNib
{
    p_intf = [NSApp getIntf];
    b_advanced = config_GetInt( p_intf, "advanced" );

    [self initStrings];
    [o_advanced_ckb setState: b_advanced];
    [o_prefs_view setBorderType: NSGrooveBorder];
    [o_prefs_view setHasVerticalScroller: YES];
    [o_prefs_view setDrawsBackground: NO];
111
    [o_prefs_view setRulersVisible: NO];
hartman's avatar
hartman committed
112
113
    [o_prefs_view setDocumentView: o_empty_view];
    [o_tree selectRow:0 byExtendingSelection:NO];
hartman's avatar
hartman committed
114
    [o_tree expandItem:[o_tree itemAtRow:0]];
hartman's avatar
hartman committed
115
}
116

hartman's avatar
hartman committed
117
118
119
120
121
122
123
124
- (void)initStrings
{
    [o_prefs_window setTitle: _NS("Preferences")];
    [o_save_btn setTitle: _NS("Save")];
    [o_cancel_btn setTitle: _NS("Cancel")];
    [o_reset_btn setTitle: _NS("Reset All")];
    [o_advanced_ckb setTitle: _NS("Advanced")];
}
125

hartman's avatar
hartman committed
126
127
- (void)showPrefs
{
hartman's avatar
hartman committed
128
129
130
    // show first tree item
    [[o_prefs_view window] center];
    [[o_prefs_view window] makeKeyAndOrderFront:self];
hartman's avatar
hartman committed
131
}
132

hartman's avatar
hartman committed
133
134
- (IBAction)savePrefs: (id)sender
{
hartman's avatar
hartman committed
135
    // walk trough all treeitems and tell them all to save
hartman's avatar
hartman committed
136
137
138
    config_SaveConfigFile( p_intf, NULL );
    [o_prefs_window orderOut:self];
}
139

hartman's avatar
hartman committed
140
141
142
- (IBAction)closePrefs: (id)sender
{
    [o_prefs_window orderOut:self];
143
144
}

hartman's avatar
hartman committed
145
- (IBAction)resetAll: (id)sender
146
{
147
    NSBeginInformationalAlertSheet(_NS("Reset Preferences"), _NS("Cancel"), _NS("Continue"), 
hartman's avatar
hartman committed
148
        nil, o_prefs_window, self, @selector(sheetDidEnd: returnCode: contextInfo:), NULL, nil,
149
        _NS("Beware this will reset your VLC media player preferences.\n"
hartman's avatar
hartman committed
150
151
152
153
154
155
156
157
            "Are you sure you want to continue?") );
}

- (void)sheetDidEnd:(NSWindow *)o_sheet returnCode:(int)i_return contextInfo:(void *)o_context
{
    if( i_return == NSAlertAlternateReturn )
    {
        config_ResetAll( p_intf );
hartman's avatar
hartman committed
158
        // show first config treeitem
hartman's avatar
hartman committed
159
    }
hartman's avatar
hartman committed
160
}
161

hartman's avatar
hartman committed
162
163
164
165
- (IBAction)advancedToggle: (id)sender
{
    b_advanced = !b_advanced;
    [o_advanced_ckb setState: b_advanced];
hartman's avatar
hartman committed
166
    // walk trough all treeitems and set advanced state
hartman's avatar
hartman committed
167
168
169
170
}

- (void)outlineViewSelectionDidChange:(NSNotification *)o_notification
{
hartman's avatar
hartman committed
171
    // a tree item will be shown
hartman's avatar
hartman committed
172
173
}

hartman's avatar
hartman committed
174
- (void)showViewForID:(int)i_id
hartman's avatar
hartman committed
175
{
176
177
}

hartman's avatar
hartman committed
178
@end
179

hartman's avatar
hartman committed
180
@implementation VLCPrefs (NSTableDataSource)
181

hartman's avatar
hartman committed
182
183
- (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
    return (item == nil) ? [[VLCTreeItem rootItem] numberOfChildren] : [item numberOfChildren];
184
185
}

hartman's avatar
hartman committed
186
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
hartman's avatar
hartman committed
187
    return (item == nil) ? YES : ([item numberOfChildren] >= 0);
188
189
}

hartman's avatar
hartman committed
190
191
- (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item {
    return (item == nil) ? [[VLCTreeItem rootItem] childAtIndex:index] : [item childAtIndex:index];
192
193
}

hartman's avatar
hartman committed
194
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
hartman's avatar
hartman committed
195
    return (item == nil) ? @"" : (id)[item name];
196
197
}

hartman's avatar
hartman committed
198
@end
199

hartman's avatar
hartman committed
200
@implementation VLCTreeItem
201

hartman's avatar
hartman committed
202
static VLCTreeItem *o_root_item = nil;
203

hartman's avatar
hartman committed
204
#define IsALeafNode ((id)-1)
205

hartman's avatar
hartman committed
206
- (id)initWithID: (int)i_id parent: (VLCTreeItem *)o_parent_item
207
{
hartman's avatar
hartman committed
208
    self = [super init];
209

hartman's avatar
hartman committed
210
    if( self != nil )
211
    {
hartman's avatar
hartman committed
212
213
        i_object_id = i_id;
        o_parent = o_parent_item;
214
    }
hartman's avatar
hartman committed
215
    return( self );
216
217
}

hartman's avatar
hartman committed
218
+ (VLCTreeItem *)rootItem {
hartman's avatar
hartman committed
219
   if (o_root_item == nil) o_root_item = [[VLCTreeItem alloc] initWithID: ROOT_ID parent:nil];
hartman's avatar
hartman committed
220
221
   return o_root_item;       
}
hartman's avatar
hartman committed
222

hartman's avatar
hartman committed
223
224
- (void)dealloc
{
hartman's avatar
hartman committed
225
226
227
    if( psz_help ) free( psz_help );
    if( psz_section ) free( psz_section );
    if (o_name) [o_name release];
hartman's avatar
hartman committed
228
229
230
    if (o_children != IsALeafNode) [o_children release];
    [super dealloc];
}
231

hartman's avatar
hartman committed
232
233
/* Creates and returns the array of children
 * Loads children incrementally */
hartman's avatar
hartman committed
234
235
236
237
238
239
- (NSArray *)children
{
    if (o_children == NULL)
    {
        intf_thread_t   *p_intf = [NSApp getIntf];
        vlc_list_t      *p_list = NULL;
240
        module_t        *p_module = NULL;
hartman's avatar
hartman committed
241
242
        module_config_t *p_item = NULL;
        int             i_index;
hartman's avatar
hartman committed
243

244
        /* List the modules */
hartman's avatar
hartman committed
245
246
247
        p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
        if( !p_list ) return nil;

hartman's avatar
hartman committed
248
249
250
251
252
253
254
255
256
257
        if( [self objectID] == ROOT_ID )
        {
            /* Create the General Settings and Modules items */
            o_children = [[NSMutableArray alloc] initWithCapacity:2];
            o_name = @"root";
            [o_children addObject:[[VLCTreeItem alloc] initWithID: GENERAL_ID parent:self]];
            [o_children addObject:[[VLCTreeItem alloc] initWithID: MODULE_ID parent:self]];
            [o_children retain];
        }
        else if( [self objectID] == GENERAL_ID )
258
        {
hartman's avatar
hartman committed
259
            /*
hartman's avatar
hartman committed
260
261
262
263
264
265
             * Build a tree of the main options
             */
            o_name = [_NS("General Settings") copy];
            psz_help = strdup( GENERAL_HELP );
            psz_section = strdup( GENERAL_TITLE );
            
hartman's avatar
hartman committed
266
            for( i_index = 0; i_index < p_list->i_count; i_index++ )
267
            {
hartman's avatar
hartman committed
268
269
270
271
                p_module = (module_t *)p_list->p_values[i_index].p_object;
                if( !strcmp( p_module->psz_object_name, "main" ) )
                    break;
            }
272
273
            if( p_module == NULL )
            {
274
                msg_Err( p_intf, "could not find the main module in our preferences" );
275
276
                return nil;
            }
hartman's avatar
hartman committed
277
278
279
280
281
282
283
284
            if( i_index < p_list->i_count )
            {
                /* Enumerate config categories and store a reference so we can
                 * generate their config panel them when it is asked by the user. */
                p_item = p_module->p_config;
                o_children = [[NSMutableArray alloc] initWithCapacity:10];

                if( p_item ) do
285
                {
hartman's avatar
hartman committed
286
287
                    VLCTreeItem *o_now;

hartman's avatar
hartman committed
288
289
290
                    switch( p_item->i_type )
                    {
                    case CONFIG_HINT_CATEGORY:
hartman's avatar
hartman committed
291
292
293
                        o_now = [[VLCTreeItem alloc] initWithID: p_module->i_object_id parent:self];
                        [o_now setName: [[NSApp localizedString: p_item->psz_text] retain]];
                        [o_children addObject: o_now];
hartman's avatar
hartman committed
294
295
                        break;
                    }
296
                }
hartman's avatar
hartman committed
297
                while( p_item->i_type != CONFIG_HINT_END && p_item++ );
hartman's avatar
hartman committed
298
299
                [o_children retain];
                //[o_children sortUsingSelector:@selector(caseInsensitiveCompare:)];
hartman's avatar
hartman committed
300
301
            }
        }
hartman's avatar
hartman committed
302
        else if( [self objectID] == MODULE_ID )
hartman's avatar
hartman committed
303
        {
hartman's avatar
hartman committed
304
305
306
307
308
309
310
311
312
            int i_counter;
            int i_total;
            BOOL b_found;

            /* Build a list of the capabilities */
            o_name = [_NS("Modules") copy];
            psz_help = strdup( PLUGIN_HELP );
            psz_section = strdup( PLUGIN_TITLE );
            
hartman's avatar
hartman committed
313
314
315
316
            o_children = [[NSMutableArray alloc] initWithCapacity:10];
            for( i_index = 0; i_index < p_list->i_count; i_index++ )
            {
                p_module = (module_t *)p_list->p_values[i_index].p_object;
hartman's avatar
hartman committed
317
                
hartman's avatar
hartman committed
318
319
320
                /* Exclude the main module */
                if( !strcmp( p_module->psz_object_name, "main" ) )
                    continue;
hartman's avatar
hartman committed
321

322
                /* Exclude empty modules */
hartman's avatar
hartman committed
323
324
325
326
327
                if( p_module->b_submodule )
                    p_item = ((module_t *)p_module->p_parent)->p_config;
                else
                    p_item = p_module->p_config;
                
hartman's avatar
hartman committed
328
329
330
331
332
333
334
335
                if( !p_item ) continue;
                do
                {
                    if( p_item->i_type & CONFIG_ITEM )
                        break;
                }
                while( p_item->i_type != CONFIG_HINT_END && p_item++ );
                if( p_item->i_type == CONFIG_HINT_END ) continue;
hartman's avatar
hartman committed
336
337
338
339
340
                
                i_total = [o_children count];
                b_found = FALSE;
                
                for( i_counter = 0; i_counter < i_total; i_counter++ )
hartman's avatar
hartman committed
341
                {
hartman's avatar
hartman committed
342
343
                    if( [[[o_children objectAtIndex: i_counter] name] isEqualToString:
                        [NSApp localizedString: p_module->psz_capability]] )
hartman's avatar
hartman committed
344
345
346
347
                    {
                        b_found = TRUE;
                        break;
                    }
348
                }
hartman's avatar
hartman committed
349
                
hartman's avatar
hartman committed
350
                if( !b_found )
351
                {
hartman's avatar
hartman committed
352
353
354
                    VLCTreeItem *o_now = [[VLCTreeItem alloc] initWithID: CAPABILITY_ID parent:self];
                    [o_now setName: [NSApp localizedString: p_module->psz_capability]];
                    [o_children addObject:o_now];
355
                }
356
            }
hartman's avatar
hartman committed
357
358
            [o_children retain];
            //[o_children sortUsingSelector:@selector(caseInsensitiveCompare:)];
359
        }
hartman's avatar
hartman committed
360
        else if( [self objectID] == CAPABILITY_ID )
hartman's avatar
hartman committed
361
        {
hartman's avatar
hartman committed
362
363
            /* add the modules */
            o_children = [[NSMutableArray alloc] initWithCapacity:3];
hartman's avatar
hartman committed
364
365
366
            for( i_index = 0; i_index < p_list->i_count; i_index++ )
            {
                p_module = (module_t *)p_list->p_values[i_index].p_object;
hartman's avatar
hartman committed
367
                
hartman's avatar
hartman committed
368
369
370
                /* Exclude the main module */
                if( !strcmp( p_module->psz_object_name, "main" ) )
                    continue;
hartman's avatar
hartman committed
371

372
                /* Exclude empty modules */
hartman's avatar
hartman committed
373
374
375
376
377
                if( p_module->b_submodule )
                    p_item = ((module_t *)p_module->p_parent)->p_config;
                else
                    p_item = p_module->p_config;
                
hartman's avatar
hartman committed
378
379
380
381
382
383
384
385
                if( !p_item ) continue;
                do
                {
                    if( p_item->i_type & CONFIG_ITEM )
                        break;
                }
                while( p_item->i_type != CONFIG_HINT_END && p_item++ );
                if( p_item->i_type == CONFIG_HINT_END ) continue;
hartman's avatar
hartman committed
386
387
388

                if( [[self name] isEqualToString:
                    [NSApp localizedString: p_module->psz_capability]] )
hartman's avatar
hartman committed
389
                {
hartman's avatar
hartman committed
390
391
392
393
394
                    VLCTreeItem *o_now = [[VLCTreeItem alloc] initWithID: p_module->i_object_id parent:self];
                    psz_help = strdup(GetCapabilityHelp( p_module->psz_capability, 1));
                    psz_section = strdup(GetCapabilityHelp( p_module->psz_capability, 2));
                    [o_now setName: [[NSApp localizedString:p_module->psz_object_name] retain]];
                    [o_children addObject:o_now];
hartman's avatar
hartman committed
395
396
                }
            }
hartman's avatar
hartman committed
397
            [o_children retain];
hartman's avatar
hartman committed
398
399
400
401
402
403
        }
        else
        {
            /* all the other stuff are leafs */
            o_children = IsALeafNode;
        }
404
    }
hartman's avatar
hartman committed
405
    return o_children;
406
407
}

hartman's avatar
hartman committed
408
- (int)objectID
409
{
hartman's avatar
hartman committed
410
411
    return i_object_id;
}
412

hartman's avatar
hartman committed
413
- (NSString *)name
hartman's avatar
hartman committed
414
{
hartman's avatar
hartman committed
415
    if( o_name ) return o_name;
hartman's avatar
hartman committed
416
}
417

hartman's avatar
hartman committed
418
419
420
421
- (void)setName:(NSString *)a_name;
{
    if( o_name ) [o_name release];
    o_name = [a_name copy];
422
423
}

hartman's avatar
hartman committed
424
- (VLCTreeItem *)childAtIndex:(int)i_index
425
{
hartman's avatar
hartman committed
426
    return [[self children] objectAtIndex:i_index];
427
428
}

hartman's avatar
hartman committed
429
- (int)numberOfChildren
430
{
hartman's avatar
hartman committed
431
432
    id i_tmp = [self children];
    return (i_tmp == IsALeafNode) ? (-1) : (int)[i_tmp count];
433
434
435
}

@end