summaryrefslogtreecommitdiffstats
path: root/Liaison/PreferencesController.m
blob: 36765053d1246ad70914855e82b9dd6f5151495c (plain)
1
2
3
4
5
6
7
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
36
37
38
39
40
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
150
151
152
153
154
#import "PreferencesController.h"

#import "RenManager.h"

@implementation PreferencesController (ToolbarDelegate)
- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar
{
    return [NSArray arrayWithObjects:
        NSToolbarSeparatorItemIdentifier,
        NSToolbarSpaceItemIdentifier,
        NSToolbarFlexibleSpaceItemIdentifier,
        NSToolbarCustomizeToolbarItemIdentifier,
        @"Network",
        nil];
}

- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar
{
    return [NSArray arrayWithObjects:
        @"Network",
        NSToolbarFlexibleSpaceItemIdentifier,
        NSToolbarSeparatorItemIdentifier,
        NSToolbarCustomizeToolbarItemIdentifier, nil];
}

- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar
     itemForItemIdentifier:(NSString *)itemIdentifier
 willBeInsertedIntoToolbar:(BOOL)flag
{
    NSToolbarItem *item;

    item = [[NSToolbarItem alloc] initWithItemIdentifier: itemIdentifier];
    if ([itemIdentifier isEqualToString: @"Network"]) {
        [item setLabel: @"Network"];
        [item setPaletteLabel: @"Network"];
        [item setToolTip: @"Open network preferences."];
        [item setImage: [NSImage imageNamed: @"Network (Large).tiff"]];
        [item setTarget: self];
        [item setAction: @selector(revealNetworkPane:)];
    }

    return item;
}

- (BOOL)validateToolbarItem: (NSToolbarItem *)theItem
{
    return YES;
}
@end

@implementation PreferencesController
- (void)setupToolbar
{
    NSToolbar *toolbar;

    toolbar = [[[NSToolbar alloc]
             initWithIdentifier: @"prefsToolbar"] autorelease];
    [toolbar setDelegate: self];
    [toolbar setAllowsUserCustomization: YES];
    [toolbar setAutosavesConfiguration: YES];
    [theWindow setToolbar: toolbar];
}

- (void)awakeFromNib
{
    // We don't need this yet, but leave the stubs.
    //[self setupToolbar];
}
    
- (IBAction)applyChanges:(id)sender
{
    Preferences *prefs;
    
    prefs = [Preferences sharedPreferences];

    [prefs setDownloadDirectory: [theDownloadField stringValue]];

    if ([[prefs hostname] isEqualToString:
        [theHostnameField stringValue]] == NO) {
        [prefs setHostname: [theHostnameField stringValue]];
        [[RenManager sharedManager] updateHostname];
    }
    
    if ([theNetworkEnabledButton state] == 1 &&
        [prefs networkEnabled] == NO) {
        [prefs setNetworkEnabled: YES];
        [[RenManager sharedManager] startSharing];
    } else if ([theNetworkEnabledButton state] == 0 &&
               [prefs networkEnabled] == YES) {
        [prefs setNetworkEnabled: NO];
        [[RenManager sharedManager] stopSharing];
    }

    [theWindow close];
}

- (IBAction)toggleNetworkEnabled:(id)sender
{
    if ([sender state] == 1) {
        [theHostnameFieldDescription setEnabled: YES];
        [theHostnameField setEnabled: YES];
    } else {
        [theHostnameFieldDescription setEnabled: NO];
        [theHostnameField setEnabled: NO];
    }
}

- (IBAction)selectDownloadDirectory:(id)sender
{
    NSOpenPanel *openPanel;

    openPanel = [NSOpenPanel openPanel];
    [openPanel setTitle: @"Select Download Directory"];
    [openPanel setAllowsMultipleSelection: NO];
    [openPanel setCanChooseDirectories: YES];
    [openPanel setCanChooseFiles: NO];

    [openPanel beginSheetForDirectory: [theDownloadField stringValue]
                                 file: nil
                                types: nil
                       modalForWindow: theWindow
                        modalDelegate: self
                       didEndSelector: @selector(openPanelDidEnd:returnCode:contextInfo:)
                          contextInfo: nil];
}

- (void)openPanelDidEnd: (NSOpenPanel *)aPanel
             returnCode: (int)rc
            contextInfo: (void *)someContext
{
    if (rc == NSCancelButton)
        return;

    [theDownloadField setStringValue: [[aPanel filenames] objectAtIndex: 0]];
}

- (void)showWindow
{
    Preferences *prefs;
    
    prefs = [Preferences sharedPreferences];
    [theDownloadField setStringValue: [prefs downloadDirectory]];
    [theHostnameField setStringValue: [prefs hostname]];
    [theNetworkEnabledButton setState:
          [prefs networkEnabled] ? 1 : 0];
    [self toggleNetworkEnabled: theNetworkEnabledButton];
    [theWindow makeKeyAndOrderFront: self];
}
@synthesize theWindow;
@synthesize theNetworkEnabledButton;
@synthesize theHostnameFieldDescription;
@synthesize theHostnameField;
@synthesize theDownloadField;
@end