root/trunk/PrefController.m @ 41

Revision 41, 14.6 kB (checked in by ngerakines, 3 years ago)

* Put into place the real icons that Wiley sent over
* Fixed the prefs on login
* Removed the TypePadSuper class
* General code cleanup

Line 
1#import "PrefController.h"
2
3@implementation PrefController
4
5// init
6// 1
7- (id)init {
8// NSLog(@"PrefController - init -- called \n");
9    if (self = [super init]) {
10        [self setToolbar:[[NSToolbar alloc] initWithIdentifier:@"preferencePanes"]];
11        [self setItems:[[NSMutableDictionary alloc] init]];
12                [self setMyCollections:[[NSMutableDictionary alloc] init]];
13        [self setAllowedPanels:[[NSMutableArray alloc] init]];
14        [self setService:[[NSString alloc] init]];
15    }
16    return self;
17}
18
19// 2
20- (void)awakeFromNib {
21        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowWillClose:) name:@"NSWindowWillCloseNotification" object:myWindow];
22       
23        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
24        // [self setService:[userDefaults objectForKey:@"service type"]];
25        [self setService:@"None"];
26
27        // Set the general pref view control values
28        [enable_growl setObjectValue:[userDefaults objectForKey:@"enable_growl"]];
29        [save_login setObjectValue:[userDefaults objectForKey:@"save_login"]];
30       
31        TypePad *tp = [[[TypePad alloc] init] autorelease];
32        Vox *vx = [[[Vox alloc] init] autorelease];
33        if ([[tp accounts] count] > 0) {
34                [self setService:@"TypePad"];
35        }
36        if ([[vx accounts] count] > 0) {
37                [self setService:@"Vox"];
38        }
39       
40       
41        if ([[self service] isEqualToString:@"None"]) {
42                [self init_generic];
43        } else {
44                if ([[self service] isEqualToString:@"TypePad"]) {
45                        [self init_typepad];
46                }
47                if ([[self service] isEqualToString:@"Vox"]) {
48                        [self init_vox];
49                }
50        }
51
52    toolbar = [[NSToolbar alloc] initWithIdentifier:@"preferencePanes"];
53    [toolbar setDelegate:self];
54    [toolbar setAllowsUserCustomization:NO];
55    [toolbar setAutosavesConfiguration:NO];
56    [myWindow setToolbar:toolbar];
57    [toolbar release];
58
59    [myWindow center];
60
61    [self switchViews:nil];
62}
63
64/* dealloc */
65- (void) dealloc {
66    [toolbar release]; toolbar = nil;
67    [items release]; items = nil;
68    [allowedPanels release]; allowedPanels = nil;
69    [service release]; service = nil;
70        [myCollections release]; myCollections = nil;
71    [super dealloc];
72}
73
74- (void) init_generic {
75        [items setObject:[self newItem:@"General" image:@"General"] forKey:@"General"];
76        allowedPanels = [NSArray arrayWithObjects:@"General", NSToolbarFlexibleSpaceItemIdentifier, nil];
77}
78
79- (void) init_typepad {
80        [items setObject:[self newItem:@"General" image:@"General"] forKey:@"General"];
81        [items setObject:[self newItem:@"Image" image:@"Weblog"] forKey:@"Image"];
82        [items setObject:[self newItem:@"Text" image:@"Gallery"] forKey:@"Text"];
83        allowedPanels = [NSArray arrayWithObjects:@"General", @"Image", @"Text", NSToolbarFlexibleSpaceItemIdentifier, nil];
84
85        [tp_image_dropdown removeAllItems];
86        [tp_text_dropdown removeAllItems];
87
88        id key;
89        TypePad *tp = [[[TypePad alloc] init] autorelease];
90        // TypePadAccount *uacc = [[tp accounts] objectForKey:[self defaultaccount]];
91
92        // set the items that need blogs
93        // NSArray *blogs = [tp defaultAccountWeblogs];
94        NSEnumerator *blog_enumerator = [[tp defaultAccountWeblogs] objectEnumerator];
95        while (key = [blog_enumerator nextObject]) {
96                TypePadWeblog *tpobj = key;
97                [myCollections setObject:[tpobj weblogID] forKey:[NSString stringWithFormat:@"Weblog: %@", [tpobj weblogTitle]]];
98                [tp_image_dropdown addItemWithTitle:[NSString stringWithFormat:@"Weblog: %@", [tpobj weblogTitle]]];
99                [tp_text_dropdown addItemWithTitle:[NSString stringWithFormat:@"Weblog: %@", [tpobj weblogTitle]]];
100        }
101       
102        // set the items that need galleries - defaultAccountGalleries
103        // NSArray *galleries = [tp defaultAccountGalleries];
104        NSEnumerator *gallery_enumerator = [[tp defaultAccountGalleries] objectEnumerator];
105        while (key = [gallery_enumerator nextObject]) {
106                TypePadGallery *tpobj = key;
107                [myCollections setObject:[tpobj galleryID] forKey:[NSString stringWithFormat:@"Gallery: %@", [tpobj galleryTitle]]];
108                [tp_image_dropdown addItemWithTitle:[NSString stringWithFormat:@"Gallery: %@", [tpobj galleryTitle]]];
109        }
110       
111        if ([tp_image_dropdown numberOfItems] == 0) {
112                [tp_image_dropdown setState:NSOffState];
113        }
114        if ([tp_text_dropdown numberOfItems] == 0) {
115                [tp_text_dropdown setState:NSOffState];
116        }
117
118        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
119        if ([userDefaults objectForKey:@"image destination id"]) {
120                if ([[userDefaults objectForKey:@"image destination type"] isEqualToString:@"1"]) {
121                        [tp_image_dropdown selectItemWithTitle:[NSString stringWithFormat:@"Weblog: %@", [userDefaults objectForKey:@"image destination title"]]];
122                }
123                if ([[userDefaults objectForKey:@"image destination type"] isEqualToString:@"2"]) {
124                        [tp_image_dropdown selectItemWithTitle:[NSString stringWithFormat:@"Gallery: %@", [userDefaults objectForKey:@"image destination title"]]];
125                }
126        }
127        if ([userDefaults objectForKey:@"text destination id"]) {
128                if ([[userDefaults objectForKey:@"text destination type"] isEqualToString:@"1"]) {
129                        [tp_text_dropdown selectItemWithTitle:[NSString stringWithFormat:@"Weblog: %@", [userDefaults objectForKey:@"text destination title"]]];
130                }
131        }
132
133}
134
135- (void) init_vox {
136        [items setObject:[self newItem:@"General" image:@"General"] forKey:@"General"];
137        [items setObject:[self newItem:@"Audio" image:@"Weblog"] forKey:@"Audio"];
138        [items setObject:[self newItem:@"Image" image:@"Gallery"] forKey:@"Image"];
139        [items setObject:[self newItem:@"Text" image:@"Weblog"] forKey:@"Text"];
140        allowedPanels = [NSArray arrayWithObjects:@"General", @"Audio", @"Image", @"Text", NSToolbarFlexibleSpaceItemIdentifier, nil];
141       
142        [vx_image_dropdown removeAllItems];
143        [vx_text_dropdown removeAllItems];
144       
145        id key;
146        Vox *vx = [[[Vox alloc] init] autorelease];
147        // TypePadAccount *uacc = [[tp accounts] objectForKey:[self defaultaccount]];
148       
149        // set the items that need blogs
150        // NSArray *blogs = [tp defaultAccountWeblogs];
151        NSEnumerator *col_enumerator = [[vx defaultAccountCollections] objectEnumerator];
152        while (key = [col_enumerator nextObject]) {
153                VoxCollection *voxcol = key;
154                [myCollections setObject:[voxcol collectionID] forKey:[NSString stringWithFormat:@"Collection: %@", [voxcol collectionTitle]]];
155// NSLog(@"Mapping collection '%@' to id '%@'", [voxcol collectionTitle], [voxcol collectionID]);
156                [vx_image_dropdown addItemWithTitle:[NSString stringWithFormat:@"Collection: %@", [voxcol collectionTitle]]];
157                [vx_text_dropdown addItemWithTitle:[NSString stringWithFormat:@"Collection: %@", [voxcol collectionTitle]]];
158        }
159        if ([vx_image_dropdown numberOfItems] == 0) {
160                [vx_image_dropdown setState:NSOffState];
161        }
162        if ([vx_text_dropdown numberOfItems] == 0) {
163                [vx_text_dropdown setState:NSOffState];
164        }
165       
166        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
167        if ([userDefaults objectForKey:@"vox image destination id"]) {
168                if ([[userDefaults objectForKey:@"vox image destination type"] isEqualToString:@"1"]) {
169                        [tp_image_dropdown selectItemWithTitle:[NSString stringWithFormat:@"Collection: %@", [userDefaults objectForKey:@"vox image destination title"]]];
170                }
171        }
172        if ([userDefaults objectForKey:@"vox text destination id"]) {
173                if ([[userDefaults objectForKey:@"vox text destination type"] isEqualToString:@"1"]) {
174                        [tp_text_dropdown selectItemWithTitle:[NSString stringWithFormat:@"Collection: %@", [userDefaults objectForKey:@"vox text destination title"]]];
175                }
176        }
177}
178
179- (NSToolbarItem *) newItem:(NSString *) name image:(NSString *) image {
180        NSToolbarItem *item = [[[NSToolbarItem alloc] initWithItemIdentifier:name] autorelease];
181    [item setPaletteLabel:name];
182    [item setLabel:name];
183    [item setToolTip:@""];
184    [item setImage:[[NSImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:image ofType:@"tiff"]]];
185    [item setTarget:self];
186    [item setAction:@selector(switchViews:)];
187        return item;
188}
189
190- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag {
191    return [items objectForKey:itemIdentifier];
192}
193
194- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)theToolbar {
195    return [self toolbarDefaultItemIdentifiers:theToolbar];
196}
197
198- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)theToolbar {
199    return [self allowedPanels];
200}
201
202- (NSArray *)toolbarSelectableItemIdentifiers: (NSToolbar *)toolbar {
203    return [items allKeys];
204}
205
206- (void)switchViews:(NSToolbarItem *)item {
207       
208    NSString *sender;
209    if (item == nil){
210        sender = @"General";
211        [toolbar setSelectedItemIdentifier:sender];
212    } else {
213        sender = [item label];
214    }
215    NSView *prefsView;
216    [myWindow setTitle:sender];
217    if([sender isEqualToString:@"General"]){
218        prefsView = PrefsGeneral;
219    }else if([sender isEqualToString:@"Audio"]){
220        prefsView = PrefsVoxAudio;
221    }else if([sender isEqualToString:@"Image"]){
222        prefsView = [[self service] isEqualToString:@"TypePad"] ? PrefsTypePadImage : PrefsVoxImage;
223    }else if([sender isEqualToString:@"Text"]){
224        prefsView = [[self service] isEqualToString:@"TypePad"] ? PrefsTypePadText : PrefsVoxText;
225    }
226       
227        NSView *tempView = [[NSView alloc] initWithFrame:[[myWindow contentView] frame]];
228        [myWindow setContentView:tempView];
229        [tempView release];
230       
231        NSRect newFrame = [myWindow frame];
232        newFrame.size.height = [prefsView frame].size.height + ([myWindow frame].size.height - [[myWindow contentView] frame].size.height);
233        newFrame.size.width = [prefsView frame].size.width;
234        newFrame.origin.y += ([[myWindow contentView] frame].size.height - [prefsView frame].size.height);
235       
236        [myWindow setShowsResizeIndicator:NO];
237        [myWindow setFrame:newFrame display:YES animate:YES];
238        [myWindow setContentView:prefsView];
239}
240
241- (void) windowWillClose:(id) sender {
242// NSLog(@"Window closed\n");
243        [self save_generic];
244        if ([[self service] isEqualToString:@"TypePad"]) {
245                [self save_typepad];
246        }
247        if ([[self service] isEqualToString:@"Vox"]) {
248                [self save_vox];
249        }
250}
251
252/* toolbar */
253- (NSToolbar *) toolbar { return toolbar; }
254
255/* -setToolbar: */
256- (void) setToolbar: (NSToolbar *) Toolbar {
257    //NSLog(@"in -setToolbar:, old value of toolbar: %@, changed to: %@", toolbar, Toolbar);
258    if (toolbar != Toolbar) {
259        [toolbar autorelease];
260        toolbar = [Toolbar retain];
261    }
262}
263
264/* items */
265- (NSMutableDictionary *) items { return items; }
266
267/* -setItems: */
268- (void) setItems: (NSMutableDictionary *) Items {
269    //NSLog(@"in -setItems:, old value of items: %@, changed to: %@", items, Items);
270    if (items != Items) {
271        [items autorelease];
272        items = [Items retain];
273    }
274}
275
276/* allowedPanels */
277- (NSMutableArray *) allowedPanels { return allowedPanels; }
278
279/* -setAllowedPanels: */
280- (void) setAllowedPanels: (NSMutableArray *) AllowedPanels {
281    //NSLog(@"in -setAllowedPanels:, old value of allowedPanels: %@, changed to: %@", allowedPanels, AllowedPanels);
282    if (allowedPanels != AllowedPanels) {
283        [allowedPanels autorelease];
284        allowedPanels = [AllowedPanels retain];
285    }
286}
287
288/* service */
289- (NSString *) service { return service; }
290
291/* -setService: */
292- (void) setService: (NSString *) Service {
293    //NSLog(@"in -setService:, old value of service: %@, changed to: %@", service, Service);
294    if (service != Service) {
295        [service autorelease];
296        service = [Service retain];
297    }
298}
299
300/* myCollections */
301- (NSMutableDictionary *) myCollections { return myCollections; }
302
303/* -setMyCollections: */
304- (void) setMyCollections: (NSMutableDictionary *) MyCollections {
305    //NSLog(@"in -setMyCollections:, old value of myCollections: %@, changed to: %@", myCollections, MyCollections);
306    if (myCollections != MyCollections) {
307        [myCollections autorelease];
308        myCollections = [MyCollections retain];
309    }
310}
311
312- (void) save_generic {
313        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
314        [userDefaults setObject:[enable_growl objectValue] forKey:@"enable_growl"];
315        [userDefaults setObject:[save_login objectValue] forKey:@"save_login"];
316}
317
318- (void) save_typepad {
319// NSLog(@"- (void) save_typepad -- called\n");
320        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
321        if ([tp_image_dropdown numberOfItems] > 0) {
322                NSArray *image_target = [self parseTargetType:[tp_image_dropdown titleOfSelectedItem]];
323                if ([image_target count] > 0) {
324                        [userDefaults setObject:[image_target objectAtIndex:0] forKey:@"image destination type"];
325                        [userDefaults setObject:[image_target objectAtIndex:1] forKey:@"image destination id"];
326                        [userDefaults setObject:[image_target objectAtIndex:2] forKey:@"image destination title"];
327                }
328        }
329        if ([tp_text_dropdown numberOfItems] > 0) {
330                NSArray *text_target = [self parseTargetType:[tp_text_dropdown titleOfSelectedItem]];
331                if ([text_target count] > 0) {
332                        [userDefaults setObject:[text_target objectAtIndex:0] forKey:@"text destination type"];
333                        [userDefaults setObject:[text_target objectAtIndex:1] forKey:@"text destination id"];
334                        [userDefaults setObject:[text_target objectAtIndex:2] forKey:@"text destination title"];
335                }
336        }
337        [userDefaults synchronize];
338}
339
340- (void) save_vox {
341        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
342        if ([vx_image_dropdown numberOfItems] > 0) {
343                NSArray *image_target = [self parseTargetType:[vx_image_dropdown titleOfSelectedItem]];
344                if ([image_target count] > 0) {
345                        [userDefaults setObject:[image_target objectAtIndex:0] forKey:@"vox image destination type"];
346                        [userDefaults setObject:[image_target objectAtIndex:1] forKey:@"vox image destination id"];
347                        [userDefaults setObject:[image_target objectAtIndex:2] forKey:@"vox image destination title"];
348                }
349        }
350        if ([vx_text_dropdown numberOfItems] > 0) {
351                NSArray *text_target = [self parseTargetType:[vx_text_dropdown titleOfSelectedItem]];
352                if ([text_target count] > 0) {
353                        [userDefaults setObject:[text_target objectAtIndex:0] forKey:@"vox text destination type"];
354                        [userDefaults setObject:[text_target objectAtIndex:1] forKey:@"vox text destination id"];
355                        [userDefaults setObject:[text_target objectAtIndex:2] forKey:@"vox text destination title"];
356                }
357        }
358        [userDefaults synchronize];
359}
360
361- (NSArray *) parseTargetType: (NSString *) title {
362        NSMutableArray *ret = [[[NSMutableArray alloc] initWithCapacity:3] autorelease];
363        if ([title rangeOfString:@"Weblog: "].location != NSNotFound) {
364                [ret addObject:@"1"];
365                [ret addObject:[myCollections objectForKey:title]];
366                [ret addObject:[title substringFromIndex:8]];
367        }
368        if ([title rangeOfString:@"Gallery: "].location != NSNotFound) {
369                [ret addObject:@"2"];
370                [ret addObject:[myCollections objectForKey:title]];
371                [ret addObject:[title substringFromIndex:9]];
372        }
373        if ([title rangeOfString:@"TypeList: "].location != NSNotFound) {
374                [ret addObject:@"3"];
375                [ret addObject:[myCollections objectForKey:title]];
376                [ret addObject:[title substringFromIndex:10]];
377        }
378        if ([title rangeOfString:@"Collection: "].location != NSNotFound) {
379                [ret addObject:@"1"];
380                [ret addObject:[myCollections objectForKey:title]];
381                [ret addObject:[title substringFromIndex:12]];
382        }
383        return ret;
384}
385
386@end
Note: See TracBrowser for help on using the browser.