prefs.m 12.6 KB
Newer Older
1
/*****************************************************************************
2
 * prefs.m: MacOS X module for vlc
3
 *****************************************************************************
4
 * Copyright (C) 2002-2003 VideoLAN
hartman's avatar
hartman committed
5
 * $Id: prefs.m,v 1.38 2004/01/30 12:44:21 hartman 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
41

/*****************************************************************************
42
 * VLCPrefs implementation
43
44
45
46
47
48
49
50
51
 *****************************************************************************/
@implementation VLCPrefs

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

    if( self != nil )
    {
hartman's avatar
hartman committed
52
        o_empty_view = [[[NSView alloc] init] retain];
53
54
55
56
57
58
59
    }

    return( self );
}

- (void)dealloc
{
hartman's avatar
hartman committed
60
61
62
    [o_empty_view release];
    [super dealloc];
}
63

hartman's avatar
hartman committed
64
65
66
67
68
69
70
71
72
73
- (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];
74
    [o_prefs_view setRulersVisible: NO];
hartman's avatar
hartman committed
75
76
    [o_prefs_view setDocumentView: o_empty_view];
    [o_tree selectRow:0 byExtendingSelection:NO];
hartman's avatar
hartman committed
77
    [o_tree expandItem:[o_tree itemAtRow:0]];
hartman's avatar
hartman committed
78
}
79

hartman's avatar
hartman committed
80
81
82
83
84
85
86
87
- (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")];
}
88

hartman's avatar
hartman committed
89
90
- (void)showPrefs
{
hartman's avatar
hartman committed
91
92
93
    // show first tree item
    [[o_prefs_view window] center];
    [[o_prefs_view window] makeKeyAndOrderFront:self];
hartman's avatar
hartman committed
94
}
95

hartman's avatar
hartman committed
96
97
- (IBAction)savePrefs: (id)sender
{
hartman's avatar
hartman committed
98
    // walk trough all treeitems and tell them all to save
hartman's avatar
hartman committed
99
100
101
    config_SaveConfigFile( p_intf, NULL );
    [o_prefs_window orderOut:self];
}
102

hartman's avatar
hartman committed
103
104
105
- (IBAction)closePrefs: (id)sender
{
    [o_prefs_window orderOut:self];
106
107
}

hartman's avatar
hartman committed
108
- (IBAction)resetAll: (id)sender
109
{
110
    NSBeginInformationalAlertSheet(_NS("Reset Preferences"), _NS("Cancel"), _NS("Continue"), 
hartman's avatar
hartman committed
111
        nil, o_prefs_window, self, @selector(sheetDidEnd: returnCode: contextInfo:), NULL, nil,
112
        _NS("Beware this will reset your VLC media player preferences.\n"
hartman's avatar
hartman committed
113
114
115
116
117
118
119
120
            "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
121
        // show first config treeitem
hartman's avatar
hartman committed
122
    }
hartman's avatar
hartman committed
123
}
124

hartman's avatar
hartman committed
125
126
127
128
- (IBAction)advancedToggle: (id)sender
{
    b_advanced = !b_advanced;
    [o_advanced_ckb setState: b_advanced];
hartman's avatar
hartman committed
129
    // walk trough all treeitems and set advanced state
hartman's avatar
hartman committed
130
131
132
133
}

- (void)outlineViewSelectionDidChange:(NSNotification *)o_notification
{
hartman's avatar
hartman committed
134
    // a tree item will be shown
hartman's avatar
hartman committed
135
136
}

hartman's avatar
hartman committed
137
- (void)showViewForID:(int)i_id
hartman's avatar
hartman committed
138
{
139
140
}

hartman's avatar
hartman committed
141
@end
142

hartman's avatar
hartman committed
143
@implementation VLCPrefs (NSTableDataSource)
144

hartman's avatar
hartman committed
145
146
- (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
    return (item == nil) ? [[VLCTreeItem rootItem] numberOfChildren] : [item numberOfChildren];
147
148
}

hartman's avatar
hartman committed
149
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
hartman's avatar
hartman committed
150
    return (item == nil) ? YES : ([item numberOfChildren] >= 0);
151
152
}

hartman's avatar
hartman committed
153
154
- (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item {
    return (item == nil) ? [[VLCTreeItem rootItem] childAtIndex:index] : [item childAtIndex:index];
155
156
}

hartman's avatar
hartman committed
157
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
hartman's avatar
hartman committed
158
    return (item == nil) ? @"" : (id)[item name];
159
160
}

hartman's avatar
hartman committed
161
@end
162

hartman's avatar
hartman committed
163
@implementation VLCTreeItem
164

hartman's avatar
hartman committed
165
static VLCTreeItem *o_root_item = nil;
166

hartman's avatar
hartman committed
167
#define IsALeafNode ((id)-1)
168

hartman's avatar
hartman committed
169
- (id)initWithID: (int)i_id parent: (VLCTreeItem *)o_parent_item
170
{
hartman's avatar
hartman committed
171
    self = [super init];
172

hartman's avatar
hartman committed
173
    if( self != nil )
174
    {
hartman's avatar
hartman committed
175
176
        i_object_id = i_id;
        o_parent = o_parent_item;
177
    }
hartman's avatar
hartman committed
178
    return( self );
179
180
}

hartman's avatar
hartman committed
181
+ (VLCTreeItem *)rootItem {
hartman's avatar
hartman committed
182
   if (o_root_item == nil) o_root_item = [[VLCTreeItem alloc] initWithID: ROOT_ID parent:nil];
hartman's avatar
hartman committed
183
184
   return o_root_item;       
}
hartman's avatar
hartman committed
185

hartman's avatar
hartman committed
186
187
- (void)dealloc
{
hartman's avatar
hartman committed
188
189
190
    if( psz_help ) free( psz_help );
    if( psz_section ) free( psz_section );
    if (o_name) [o_name release];
hartman's avatar
hartman committed
191
192
193
    if (o_children != IsALeafNode) [o_children release];
    [super dealloc];
}
194

hartman's avatar
hartman committed
195
196
/* Creates and returns the array of children
 * Loads children incrementally */
hartman's avatar
hartman committed
197
198
199
200
201
202
- (NSArray *)children
{
    if (o_children == NULL)
    {
        intf_thread_t   *p_intf = [NSApp getIntf];
        vlc_list_t      *p_list = NULL;
203
        module_t        *p_module = NULL;
hartman's avatar
hartman committed
204
205
        module_config_t *p_item = NULL;
        int             i_index;
hartman's avatar
hartman committed
206

207
        /* List the modules */
hartman's avatar
hartman committed
208
209
210
        p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
        if( !p_list ) return nil;

hartman's avatar
hartman committed
211
212
213
214
215
216
217
218
219
220
        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 )
221
        {
hartman's avatar
hartman committed
222
            /*
hartman's avatar
hartman committed
223
224
225
226
227
228
             * 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
229
            for( i_index = 0; i_index < p_list->i_count; i_index++ )
230
            {
hartman's avatar
hartman committed
231
232
233
234
                p_module = (module_t *)p_list->p_values[i_index].p_object;
                if( !strcmp( p_module->psz_object_name, "main" ) )
                    break;
            }
235
236
            if( p_module == NULL )
            {
237
                msg_Err( p_intf, "could not find the main module in our preferences" );
238
239
                return nil;
            }
hartman's avatar
hartman committed
240
241
242
243
244
245
246
247
            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
248
                {
hartman's avatar
hartman committed
249
250
                    VLCTreeItem *o_now;

hartman's avatar
hartman committed
251
252
253
                    switch( p_item->i_type )
                    {
                    case CONFIG_HINT_CATEGORY:
hartman's avatar
hartman committed
254
255
256
                        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
257
258
                        break;
                    }
259
                }
hartman's avatar
hartman committed
260
                while( p_item->i_type != CONFIG_HINT_END && p_item++ );
hartman's avatar
hartman committed
261
262
                [o_children retain];
                //[o_children sortUsingSelector:@selector(caseInsensitiveCompare:)];
hartman's avatar
hartman committed
263
264
            }
        }
hartman's avatar
hartman committed
265
        else if( [self objectID] == MODULE_ID )
hartman's avatar
hartman committed
266
        {
hartman's avatar
hartman committed
267
268
269
270
271
272
273
274
275
            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
276
277
278
279
            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
280
                
hartman's avatar
hartman committed
281
282
283
                /* Exclude the main module */
                if( !strcmp( p_module->psz_object_name, "main" ) )
                    continue;
hartman's avatar
hartman committed
284

285
                /* Exclude empty modules */
hartman's avatar
hartman committed
286
287
288
289
290
                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
291
292
293
294
295
296
297
298
                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
299
300
301
302
303
                
                i_total = [o_children count];
                b_found = FALSE;
                
                for( i_counter = 0; i_counter < i_total; i_counter++ )
hartman's avatar
hartman committed
304
                {
hartman's avatar
hartman committed
305
306
                    if( [[[o_children objectAtIndex: i_counter] name] isEqualToString:
                        [NSApp localizedString: p_module->psz_capability]] )
hartman's avatar
hartman committed
307
308
309
310
                    {
                        b_found = TRUE;
                        break;
                    }
311
                }
hartman's avatar
hartman committed
312
                
hartman's avatar
hartman committed
313
                if( !b_found )
314
                {
hartman's avatar
hartman committed
315
316
317
                    VLCTreeItem *o_now = [[VLCTreeItem alloc] initWithID: CAPABILITY_ID parent:self];
                    [o_now setName: [NSApp localizedString: p_module->psz_capability]];
                    [o_children addObject:o_now];
318
                }
319
            }
hartman's avatar
hartman committed
320
321
            [o_children retain];
            //[o_children sortUsingSelector:@selector(caseInsensitiveCompare:)];
322
        }
hartman's avatar
hartman committed
323
        else if( [self objectID] == CAPABILITY_ID )
hartman's avatar
hartman committed
324
        {
hartman's avatar
hartman committed
325
326
            /* add the modules */
            o_children = [[NSMutableArray alloc] initWithCapacity:3];
hartman's avatar
hartman committed
327
328
329
            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
330
                
hartman's avatar
hartman committed
331
332
333
                /* Exclude the main module */
                if( !strcmp( p_module->psz_object_name, "main" ) )
                    continue;
hartman's avatar
hartman committed
334

335
                /* Exclude empty modules */
hartman's avatar
hartman committed
336
337
338
339
340
                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
341
342
343
344
345
346
347
348
                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
349
350
351

                if( [[self name] isEqualToString:
                    [NSApp localizedString: p_module->psz_capability]] )
hartman's avatar
hartman committed
352
                {
hartman's avatar
hartman committed
353
354
355
356
357
                    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
358
359
                }
            }
hartman's avatar
hartman committed
360
            [o_children retain];
hartman's avatar
hartman committed
361
362
363
364
365
366
        }
        else
        {
            /* all the other stuff are leafs */
            o_children = IsALeafNode;
        }
367
    }
hartman's avatar
hartman committed
368
    return o_children;
369
370
}

hartman's avatar
hartman committed
371
- (int)objectID
372
{
hartman's avatar
hartman committed
373
374
    return i_object_id;
}
375

hartman's avatar
hartman committed
376
- (NSString *)name
hartman's avatar
hartman committed
377
{
hartman's avatar
hartman committed
378
    if( o_name ) return o_name;
hartman's avatar
hartman committed
379
}
380

hartman's avatar
hartman committed
381
382
383
384
- (void)setName:(NSString *)a_name;
{
    if( o_name ) [o_name release];
    o_name = [a_name copy];
385
386
}

hartman's avatar
hartman committed
387
- (VLCTreeItem *)childAtIndex:(int)i_index
388
{
hartman's avatar
hartman committed
389
    return [[self children] objectAtIndex:i_index];
390
391
}

hartman's avatar
hartman committed
392
- (int)numberOfChildren
393
{
hartman's avatar
hartman committed
394
395
    id i_tmp = [self children];
    return (i_tmp == IsALeafNode) ? (-1) : (int)[i_tmp count];
396
397
398
}

@end