File: MyUserPrefsController.m

package info (click to toggle)
lynkeos.app 3.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 15,740 kB
  • sloc: objc: 36,412; ansic: 684; cpp: 148; sh: 68; makefile: 21
file content (227 lines) | stat: -rw-r--r-- 6,681 bytes parent folder | download
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
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
// 
//  Lynkeos
//  $Id: MyUserPrefsController.m 585 2018-09-08 21:30:37Z j-etienne $
//
//  Created by Jean-Etienne LAMIAUD on Sun Feb 8 2004.
//  Copyright (c) 2003-2013. Jean-Etienne LAMIAUD
//
// 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
#include <objc/runtime.h>

#include "LynkeosPreferences.h"
#include "MyPluginsController.h"

#include "MyUserPrefsController.h"

//! Preferences toolbar identifier
static NSString * const K_PREF_TOOLBAR = @"Preferences toolbar";
//! Prefix for toolbar items
static NSString * const toolbarIdentPrefix = @"LynkeosPrefToolbarItem_";

//! MyUserPrefsController singleton instance
static MyUserPrefsController *instancePointer = nil;

/*!
 * @abstract Private methods for MyUserPrefsController
 */
@interface MyUserPrefsController(Private)
//! Set the active preference pane
- (void) activatePreferencePane: (id) sender ;
@end

@implementation MyUserPrefsController(Private)
- (void) activatePreferencePane: (id) sender
{
   NSInteger tag = [sender tag];
   NSArray *prefList = [[MyPluginsController defaultPluginController]
                                                           getPreferencesPanes];
   // Retrieve the preferences controller
   Class c = [prefList objectAtIndex:tag];
   id <LynkeosPreferences> pref = [c getPreferenceInstance];
   NSView *newView = [pref getPreferencesView];

   // Access the NSView and insert it in the window
   [_prefView setDocumentView:newView];
}
@end

@implementation MyUserPrefsController

- (id) init
{
   NSAssert( instancePointer == nil, 
             @"Creation of more than 1 MyUserPrefsController object" );

   if ( (self = [super init]) != nil )
   {
      _user = [NSUserDefaults standardUserDefaults];

      _toolbar = [[NSToolbar alloc] initWithIdentifier:K_PREF_TOOLBAR];
      [_toolbar setAllowsUserCustomization:YES];
      [_toolbar setAutosavesConfiguration:YES];
      [_toolbar setDelegate:self];

      instancePointer = self;
   }

   return( self );
}

- (void) awakeFromNib
{
   [_panel setToolbar:_toolbar];

   [_prefView setDocumentView:_generalPrefsView];
   [_toolbar setSelectedItemIdentifier:@"LynkeosPrefToolbarItem_MyGeneralPrefs"];
}

// Toolbar
- (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
{
   if ( toolbar != _toolbar )
      return( nil );

   // Toolbar items identifiers are derived from their class name
   NSMutableArray *items = [NSMutableArray array];
   NSEnumerator *prefList = [[[MyPluginsController defaultPluginController]
                                         getPreferencesPanes] objectEnumerator];
   Class p;

   while ( (p = [prefList nextObject]) != nil )
   {
      NSString *ident = [toolbarIdentPrefix stringByAppendingString:
                                                                 [p className]];
      [items addObject:ident];
   }

   return( items );
}

- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar
{
   if ( toolbar != _toolbar )
      return( nil );

   NSMutableArray *allowed =
      (NSMutableArray*)[self toolbarSelectableItemIdentifiers:toolbar];

   [allowed addObjectsFromArray:
      [NSArray arrayWithObjects:
         NSToolbarCustomizeToolbarItemIdentifier,
         NSToolbarFlexibleSpaceItemIdentifier,
         NSToolbarSpaceItemIdentifier,
         NSToolbarSeparatorItemIdentifier, nil]];

   return( allowed );
}

- (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar
{
   return( [self toolbarSelectableItemIdentifiers:toolbar] );
}

- (NSToolbarItem *) toolbar:(NSToolbar *)toolbar
      itemForItemIdentifier:(NSString *)itemIdentifier
  willBeInsertedIntoToolbar:(BOOL)flag
{
   if ( toolbar != _toolbar )
      return( nil );

   NSToolbarItem *item = nil;

   // Retrieve the class from the identifier
   if ([itemIdentifier hasPrefix:toolbarIdentPrefix])
   {
      NSString *className = [itemIdentifier substringFromIndex:
                                                   [toolbarIdentPrefix length]];
      Class c = objc_getClass( [className UTF8String] );

      if ( c != nil )
      {
         NSUInteger tag = [[[MyPluginsController defaultPluginController]
                                          getPreferencesPanes] indexOfObject:c];

         NSAssert( tag != NSNotFound,
                   @"Tollbar item not in preferences class list" );

         // Initialize the item
         item = [[[NSToolbarItem alloc] initWithItemIdentifier:
                                                   itemIdentifier] autorelease];

         NSString *title, *tip;
         NSImage *icon;
         [c getPreferenceTitle:&title icon:&icon tip:&tip];

         [item setLabel:title];
         [item setPaletteLabel:title];

         [item setToolTip:tip];
         [item setImage:icon];

         [item setTag:tag];
         [item setTarget:self];
         [item setAction:@selector(activatePreferencePane:)];
      }
   }

   return item;
}

+ (MyUserPrefsController*) getUserPref { return instancePointer; }

- (IBAction)resetPrefs:(id)sender
{
   NSEnumerator *prefList = [[[MyPluginsController defaultPluginController]
      getPreferencesPanes] objectEnumerator];
   Class p;

   while ( (p = [prefList nextObject]) != nil )
      [[p getPreferenceInstance] resetPreferences:_user];
}

- (IBAction)applyChanges:(id)sender
{
   NSEnumerator *prefList = [[[MyPluginsController defaultPluginController]
      getPreferencesPanes] objectEnumerator];
   Class p;

   while ( (p = [prefList nextObject]) != nil )
      [[p getPreferenceInstance] savePreferences:_user];
}

- (IBAction)cancelChanges:(id)sender
{
   NSEnumerator *prefList = [[[MyPluginsController defaultPluginController]
      getPreferencesPanes] objectEnumerator];
   Class p;

   while ( (p = [prefList nextObject]) != nil )
      [[p getPreferenceInstance] revertPreferences];
   [_panel orderOut:sender];
}

- (IBAction)confirmChanges:(id)sender
{
   [self applyChanges:sender];
   [_panel orderOut:sender];
}

- (IBAction) showPrefs :(id)sender
{
   [_panel makeKeyAndOrderFront:self];
}

@end