Allow localization of the Alert Text label in the Display an Alert action
[adiumx.git] / Source / AICLPreferences.m
blobca345bfc9920e9a082d1c1d9eb1ad41d43453d6c
1 /* 
2  * Adium is the legal property of its developers, whose names are listed in the copyright file included
3  * with this source distribution.
4  * 
5  * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6  * General Public License as published by the Free Software Foundation; either version 2 of the License,
7  * or (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
10  * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
11  * Public License for more details.
12  * 
13  * You should have received a copy of the GNU General Public License along with this program; if not,
14  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
15  */
17 #import "AICLPreferences.h"
18 #import "AIListLayoutWindowController.h"
19 #import "AIListThemePreviewCell.h"
20 #import "AIListThemeWindowController.h"
21 #import "AISCLViewPlugin.h"
22 #import <AIUtilities/AIFileManagerAdditions.h>
23 #import <AIUtilities/AIImageTextCell.h>
24 #import <AIUtilities/ESImageAdditions.h>
25 #import <Adium/AIAbstractListController.h>
27 //Handles the interface interaction, and sets preference values
28 //The outline view plugin is responsible for reading & setting the preferences, as well as observing changes in them
30 @interface AICLPreferences (PRIVATE)
31 - (void)configureView;
32 - (void)changeFont:(id)sender;
33 - (void)showFont:(NSFont *)inFont inField:(NSTextField *)inTextField;
34 - (void)configureControlDimming;
36 - (void)updateLayouts;
37 - (void)updateThemes;
38 - (void)updateSelectedLayoutAndTheme;
40 - (void)applySet:(NSDictionary *)setDictionary toPreferenceGroup:(NSString *)preferenceGroup;
41 - (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(int)row;
42 @end
44 @implementation AICLPreferences
46 //Preference pane properties
47 - (PREFERENCE_CATEGORY)category{
48     return AIPref_Advanced;
50 - (NSString *)label{
51     return @"General Appearance";
53 - (NSString *)nibName{
54     return @"AICLPrefView";
57 //Configures our view for the current preferences
58 - (void)viewDidLoad
60         AIImageTextCell *dataCell;
62         currentLayoutName = [@"Default" retain];
63         currentThemeName = [@"Default" retain];
64         [self updateLayouts];
65         [self updateThemes];
67         //Observe for installation of new themes/layouts
68         [[adium notificationCenter] addObserver:self
69                                                                    selector:@selector(xtrasChanged:)
70                                                                            name:Adium_Xtras_Changed
71                                                                          object:nil];
72         [[adium preferenceController] registerPreferenceObserver:self forGroup:PREF_GROUP_CONTACT_LIST];
73         
74         //Images
75         layoutStandard = [[NSImage imageNamed:@"style-standard" forClass:[self class]] retain];
76         layoutBorderless = [[NSImage imageNamed:@"style-borderless" forClass:[self class]] retain];
77         layoutMockie = [[NSImage imageNamed:@"style-mockie" forClass:[self class]] retain];
78         layoutPillows = [[NSImage imageNamed:@"style-pillows" forClass:[self class]] retain];
79         
80         //
81         [button_themeEdit setTitle:@"Edit"];
82         [button_layoutEdit setTitle:@"Edit"];
83         
84         //
85         dataCell = [[[AIImageTextCell alloc] init] autorelease];
86     [dataCell setFont:[NSFont systemFontOfSize:12]];
87         [dataCell setIgnoresFocus:YES];
88         [dataCell setDrawsGradientHighlight:YES];
89         [[tableView_layout tableColumnWithIdentifier:@"name"] setDataCell:dataCell];
90         
91         dataCell = [[[AIGradientCell alloc] init] autorelease];
92     [dataCell setFont:[NSFont systemFontOfSize:12]];
93         [dataCell setIgnoresFocus:YES]; 
94         [dataCell setDrawsGradientHighlight:YES];
95         [[tableView_layout tableColumnWithIdentifier:@"preview"] setDataCell:dataCell];
97         dataCell = [[[AIImageTextCell alloc] init] autorelease];
98     [dataCell setFont:[NSFont systemFontOfSize:12]];
99         [dataCell setIgnoresFocus:YES];
100         [dataCell setDrawsGradientHighlight:YES];
101         [[tableView_theme tableColumnWithIdentifier:@"name"] setDataCell:dataCell];
102         
103         //
104     [tableView_layout setTarget:self];
105         [tableView_layout setDoubleAction:@selector(editLayout:)];
106     [tableView_theme setTarget:self];
107         [tableView_theme setDoubleAction:@selector(editTheme:)];
110 //Preference view is closing
111 - (void)viewWillClose
113         [layoutStandard release]; layoutStandard = nil;
114         [layoutBorderless release]; layoutBorderless = nil;
115         [layoutMockie release]; layoutMockie = nil;
116         [layoutPillows release]; layoutPillows = nil;
117         
118         [[adium notificationCenter] removeObserver:self];
119         [[adium preferenceController] unregisterPreferenceObserver:self];
120         
121         [AISCLViewPlugin resetXtrasCache];
124 //Installed xtras have changed
125 - (void)xtrasChanged:(NSNotification *)notification
127         if(notification == nil || [[notification object] caseInsensitiveCompare:LIST_LAYOUT_EXTENSION] == 0){
128                 [AISCLViewPlugin resetXtrasCache];
129                 [self updateLayouts];
130                 
131         }else if(notification == nil || [[notification object] caseInsensitiveCompare:LIST_THEME_EXTENSION] == 0){
132                 [AISCLViewPlugin resetXtrasCache];
133                 [self updateThemes];
134         }
137 //Selected theme/layout changed
138 - (void)preferencesChangedForGroup:(NSString *)group key:(NSString *)key
139                                                         object:(AIListObject *)object preferenceDict:(NSDictionary *)prefDict firstTime:(BOOL)firstTime
141         [self updateSelectedLayoutAndTheme];
144 //Update our list of available layouts
145 - (void)updateLayouts
147         [layoutArray release];
148         layoutArray = [[AISCLViewPlugin availableLayoutSets] retain];
149         [tableView_layout reloadData];
150         [self updateSelectedLayoutAndTheme];
153 //Update our list of available themes
154 - (void)updateThemes
156         [themeArray release];
157         themeArray = [[AISCLViewPlugin availableThemeSets] retain];
158         [tableView_theme reloadData];
159         [self updateSelectedLayoutAndTheme];
162 //Update the selected table rows to match our preferences
163 - (void)updateSelectedLayoutAndTheme
165         NSEnumerator    *enumerator;
166         NSDictionary    *dict;
167         
168         [currentLayoutName release];
169         [currentThemeName release];
170         
171         currentLayoutName = [[[adium preferenceController] preferenceForKey:KEY_LIST_LAYOUT_NAME group:PREF_GROUP_CONTACT_LIST] retain];
172         currentThemeName = [[[adium preferenceController] preferenceForKey:KEY_LIST_THEME_NAME group:PREF_GROUP_CONTACT_LIST] retain];
173         
174         enumerator = [layoutArray objectEnumerator];
175         while((dict = [enumerator nextObject])){
176                 if([[dict objectForKey:@"name"] isEqualToString:currentLayoutName]){
177                         [tableView_layout selectRow:[layoutArray indexOfObject:dict] byExtendingSelection:NO];
178                 }
179         }
180         
181         enumerator = [themeArray objectEnumerator];
182         while((dict = [enumerator nextObject])){
183                 if([[dict objectForKey:@"name"] isEqualToString:currentThemeName]){
184                         [tableView_theme selectRow:[themeArray indexOfObject:dict] byExtendingSelection:NO];
185                 }
186         }
187         
188         [self configureControlDimming];
191 - (void)configureControlDimming
193         [button_layoutEdit setEnabled:([layoutArray count] > 0)];
194         [button_layoutDelete setEnabled:([layoutArray count] > 1)];
196         [button_themeEdit setEnabled:([themeArray count] > 0)];
197         [button_themeDelete setEnabled:([themeArray count] > 1)];
201 //Editing --------------------------------------------------------------------------------------------------------------
202 #pragma mark Editing
203 //Create new layout or theme
204 - (IBAction)spawnLayout:(id)sender
206         [AIListLayoutWindowController listLayoutOnWindow:[[self view] window]
207                                                                                         withName:[NSString stringWithFormat:@"%@ Copy",currentLayoutName]];
209 - (IBAction)spawnTheme:(id)sender
211         [AIListThemeWindowController listThemeOnWindow:[[self view] window]
212                                                                                   withName:[NSString stringWithFormat:@"%@ Copy",currentThemeName]];
215 //Edit a layout or theme
216 - (IBAction)editTheme:(id)sender
218         [AIListThemeWindowController listThemeOnWindow:[[self view] window] withName:currentThemeName];
220 - (IBAction)editLayout:(id)sender
222         [AIListLayoutWindowController listLayoutOnWindow:[[self view] window] withName:currentLayoutName];
225 //Delete a layout
226 - (IBAction)deleteLayout:(id)sender
228         if([layoutArray count] > 1){
229                 NSDictionary    *selected = [layoutArray objectAtIndex:[tableView_layout selectedRow]];
230                 NSBeginAlertSheet(AILocalizedString(@"Delete Layout",nil), 
231                                                   AILocalizedString(@"Delete",nil), 
232                                                   AILocalizedString(@"Cancel",nil),
233                                                   @"",
234                                                   [[self view] window],
235                                                   self,
236                                                   @selector(deleteLayoutSheetDidEnd:returnCode:contextInfo:),
237                                                   nil,
238                                                   selected,
239                                                   AILocalizedString(@"Delete the layout \"%@\" from %@?",nil), 
240                                                   [selected objectForKey:@"name"],
241                                                   [selected objectForKey:@"path"]);
242         }
244 - (void)deleteLayoutSheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(NSDictionary *)contextInfo
246     if(returnCode == NSAlertDefaultReturn && contextInfo){
247                 NSString *path = [contextInfo objectForKey:@"path"];
248                 if(path){
249                         [[NSFileManager defaultManager] trashFileAtPath:path];
250                         [[adium notificationCenter] postNotificationName:Adium_Xtras_Changed object:LIST_LAYOUT_EXTENSION];
251                         [self tableView:tableView_layout shouldSelectRow:[tableView_layout selectedRow]];
252                 }
253         }
256 //Delete a theme
257 - (IBAction)deleteTheme:(id)sender
259         if([themeArray count] > 1){
260                 NSDictionary    *selected = [themeArray objectAtIndex:[tableView_theme selectedRow]];
261                 NSBeginAlertSheet(AILocalizedString(@"Delete Theme",nil), 
262                                                   AILocalizedString(@"Delete",nil), 
263                                                   AILocalizedString(@"Cancel",nil),
264                                                   @"",
265                                                   [[self view] window],
266                                                   self,
267                                                   @selector(deleteThemeSheetDidEnd:returnCode:contextInfo:),
268                                                   nil,
269                                                   selected,
270                                                   AILocalizedString(@"Delete the theme \"%@\" from %@?",nil), 
271                                                   [selected objectForKey:@"name"],
272                                                   [selected objectForKey:@"path"]);
273         }
275 - (void)deleteThemeSheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(NSDictionary *)contextInfo
277     if(returnCode == NSAlertDefaultReturn && contextInfo){
278                 NSString *path = [contextInfo objectForKey:@"path"];
279                 if(path){
280                         [[NSFileManager defaultManager] trashFileAtPath:path];
281                         [[adium notificationCenter] postNotificationName:Adium_Xtras_Changed object:LIST_THEME_EXTENSION];
282                         [self tableView:tableView_theme shouldSelectRow:[tableView_theme selectedRow]];
283                 }
284         }
288 //Table Delegate -------------------------------------------------------------------------------------------------------
289 #pragma mark Table Delegate
290 - (int)numberOfRowsInTableView:(NSTableView *)tableView
292         if(tableView == tableView_layout){
293                 return [layoutArray count];
294         }else{
295                 return [themeArray count];
296         }
299 - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row
301         NSString        *column = [tableColumn identifier];
302         
303         if(tableView == tableView_layout){
304                 NSDictionary    *layoutDict = [layoutArray objectAtIndex:row];
305                 
306                 if([column isEqualToString:@"name"]){
307                         return [layoutDict objectForKey:@"name"];
308                 }else if([column isEqualToString:@"preview"]){
309                         return @"-";
310                 }
311         }else if(tableView == tableView_theme){
312                 NSDictionary    *themeDict = [themeArray objectAtIndex:row];
313                 
314                 if([column isEqualToString:@"type"]){
315                         return @"-";
316                 }else if([column isEqualToString:@"name"]){
317                         return [themeDict objectForKey:@"name"];
318                 }else if([column isEqualToString:@"preview"]){
319                         return @"-";
320                 }
321         }
323         return @"-";
326 - (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(int)row
328         if(tableView == tableView_layout){
329                 NSDictionary    *layoutDict = [layoutArray objectAtIndex:row];
330                 [[adium preferenceController] setPreference:[layoutDict objectForKey:@"name"]
331                                                                                          forKey:KEY_LIST_LAYOUT_NAME
332                                                                                           group:PREF_GROUP_CONTACT_LIST];
333                 
334         }else if(tableView == tableView_theme){
335                 NSDictionary    *themeDict = [themeArray objectAtIndex:row];
336                 [[adium preferenceController] setPreference:[themeDict objectForKey:@"name"]
337                                                                                          forKey:KEY_LIST_THEME_NAME
338                                                                                           group:PREF_GROUP_CONTACT_LIST];
339                 
340         }
341         
342         [self updateSelectedLayoutAndTheme];
343         
344         return YES;
347 - (void)tableView:(NSTableView *)tableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn row:(int)row
349 //      NSString        *column = [tableColumn identifier];
351 //      if(tableView == tableView_layout){
352 //              if([column isEqualToString:@"name"]){
353 //                      NSImage *image = nil;
354 //                      switch([[[[layoutArray objectAtIndex:row] objectForKey:@"preferences"] objectForKey:KEY_LIST_LAYOUT_WINDOW_STYLE] intValue]){
355 //                              case WINDOW_STYLE_STANDARD: image = layoutStandard; break;
356 //                              case WINDOW_STYLE_BORDERLESS: image = layoutBorderless; break;
357 //                              case WINDOW_STYLE_MOCKIE: image = layoutMockie; break;
358 //                              case WINDOW_STYLE_PILLOWS: image = layoutPillows; break;
359 //                              case WINDOW_STYLE_PILLOWS_FITTED: image = layoutPillows; break;
360 //                      }
361 //                      [cell setImage:image];
362 //              }else{
363 //                      [cell setImage:nil];
364 //              }
365 //      }else if(tableView == tableView_theme){
366 //              if([column isEqualToString:@"preview"]){
367 //                      [cell setThemeDict:[[themeArray objectAtIndex:row] objectForKey:@"preferences"]];
368 //              }               
369 //      }
372 @end