Use initialize, not load, wherever possible.
[adiumx.git] / Source / ESAddressBookIntegrationAdvancedPreferences.m
bloba6e765e5b4f3230edfd333435f7df4fc5c33c1f2
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 "ESAddressBookIntegrationAdvancedPreferences.h"
18 #import "ESAddressBookIntegrationPlugin.h"
19 #import <Adium/AIContactControllerProtocol.h>
20 #import <Adium/AIPreferenceControllerProtocol.h>
21 #import <Adium/AILocalizationTextField.h>
22 #import <AIUtilities/AIDictionaryAdditions.h>
23 #import <AIUtilities/AIImageAdditions.h>
24 #import <AIUtilities/AIMenuAdditions.h>
26 #define ADDRESS_BOOK_FIRST_OPTION                                       AILocalizedString(@"First","Address Book Name display style, e.g. Evan")
27 #define ADDRESS_BOOK_FIRST_LAST_OPTION                          AILocalizedString(@"First Last","Address Book Name display style, e.g. Evan Schoenberg")
28 #define ADDRESS_BOOK_LAST_FIRST_OPTION                          AILocalizedString(@"Last, First","Address Book Name display style, e.g. Schoenberg, Evan")
29 #define ADDRESS_BOOK_LAST_FIRST_NO_COMMA_OPTION         AILocalizedString(@"Last First", "Address Book Name display style, e.g. Schoenberg Evan")
30 #define ADDRESS_BOOK_FIRST_MIDDLE_OPTION                        AILocalizedString(@"First Middle", "Address Book Name display style, e.g. Evan Dreskin")
31 #define ADDRESS_BOOK_FIRST_MIDDLE_LAST_OPTION           AILocalizedString(@"First Middle Last", "Address Book Name display style, e.g. Evan Dreskin Schoenberg")
32 #define ADDRESS_BOOK_LAST_FIRST_MIDDLE_OPTION           AILocalizedString(@"Last, First Middle", "Address Book Name display style, e.g. Schoenberg, Evan Dreskin")
33 #define ADDRESS_BOOK_LAST_FIRST_MIDDLE_NO_COMMA_OPTION  AILocalizedString(@"Last First Middle", "Address Book Name display style, e.g. Schoenberg Evan Dreskin")
35 @interface ESAddressBookIntegrationAdvancedPreferences (PRIVATE)
36 - (void)preferencesChanged:(NSNotification *)notification;
37 - (void)configureFormatMenu;
38 - (IBAction)changeFormat:(id)sender;
39 @end
41 /*!
42  * @class ESAddressBookIntegrationAdvancedPreferences
43  * @brief Provide advanced preferences for the address book integration
44  */
45 @implementation ESAddressBookIntegrationAdvancedPreferences
47 /*!
48  * @brief Label
49  */
50 - (NSString *)label{
51     return AILocalizedString(@"Address Book",nil);
53 /*!
54  * @brief Nib name
55  */
56 - (NSString *)nibName{
57     return @"AddressBookPrefs";
59 /*!
60  * @brief Image for advanced preferences
61  */
62 - (NSImage *)image{
63         return [NSImage imageNamed:@"AddressBook" forClass:[self class]];
66 /*!
67  * @brief Configure the preference view
68  */
69 - (void)viewDidLoad
71         [self configureFormatMenu];
72         
73         [label_formatNamesAs setLocalizedString:AILocalizedString(@"Format name as:", "Format name as: [popup menu of choices like 'First, Last']")];
74         [label_names setLocalizedString:AILocalizedString(@"Names",nil)];
75         [label_images setLocalizedString:AILocalizedString(@"Images",nil)];
76         [label_contacts setLocalizedString:AILocalizedString(@"Contacts",nil)];
77         
78         [checkBox_enableImport setLocalizedString:AILocalizedString(@"Import my contacts' names from the Address Book",nil)];
79         [checkBox_useNickName setLocalizedString:AILocalizedString(@"Use nickname if available",nil)];
80         [checkBox_useMiddleName setLocalizedString:AILocalizedString(@"Use middle name if available",nil)];
81         [checkBox_useABImages setLocalizedString:AILocalizedString(@"Use Address Book images as contacts' icons",nil)];
82         [checkBox_preferABImages setLocalizedString:AILocalizedString(@"Even if the contact already has a contact icon",nil)];
83         [checkBox_syncAutomatic setLocalizedString:AILocalizedString(@"Overwrite Address Book images with contacts' icons",nil)];
84         [checkBox_metaContacts setLocalizedString:AILocalizedString(@"Combine contacts listed on a single card",nil)];  
86         [checkBox_enableImport setState:[[[adium preferenceController] preferenceForKey:KEY_AB_ENABLE_IMPORT
87                                                                                                                                                           group:PREF_GROUP_ADDRESSBOOK] boolValue]];
88         [popUp_formatMenu selectItemAtIndex:[popUp_formatMenu indexOfItemWithTag:[[[adium preferenceController] preferenceForKey:KEY_AB_DISPLAYFORMAT
89                                                                                                                                                                                                                                            group:PREF_GROUP_ADDRESSBOOK] intValue]]];
90         [checkBox_useNickName setState:[[[adium preferenceController] preferenceForKey:KEY_AB_USE_NICKNAME
91                                                                                                                                                          group:PREF_GROUP_ADDRESSBOOK] boolValue]];
92         [checkBox_useMiddleName setState:[[[adium preferenceController] preferenceForKey:KEY_AB_USE_MIDDLE
93                                                                                                                                                            group:PREF_GROUP_ADDRESSBOOK] boolValue]];
94         [checkBox_syncAutomatic setState:[[[adium preferenceController] preferenceForKey:KEY_AB_IMAGE_SYNC
95                                                                                                                                                            group:PREF_GROUP_ADDRESSBOOK] boolValue]];
96         [checkBox_useABImages setState:[[[adium preferenceController] preferenceForKey:KEY_AB_USE_IMAGES
97                                                                                                                                                          group:PREF_GROUP_ADDRESSBOOK] boolValue]];
98         [checkBox_enableNoteSync setState:[[[adium preferenceController] preferenceForKey:KEY_AB_NOTE_SYNC
99                                                                                                                                                                 group:PREF_GROUP_ADDRESSBOOK] boolValue]];
100         [checkBox_preferABImages setState:[[[adium preferenceController] preferenceForKey:KEY_AB_PREFER_ADDRESS_BOOK_IMAGES
101                                                                                                                                                                 group:PREF_GROUP_ADDRESSBOOK] boolValue]];
102         [checkBox_metaContacts setState:[[[adium preferenceController] preferenceForKey:KEY_AB_CREATE_METACONTACTS
103                                                                                                                                                           group:PREF_GROUP_ADDRESSBOOK] boolValue]];
104         
105         [self configureControlDimming];
109  * @brief Deallocate
110  */
111 - (void)dealloc
113         [super dealloc];
117  * @brief Configure control dimming
118  */
119 - (void)configureControlDimming
121         BOOL            enableImport = [[[adium preferenceController] preferenceForKey:KEY_AB_ENABLE_IMPORT
122                                                                                                                                                          group:PREF_GROUP_ADDRESSBOOK] boolValue];
123         BOOL            useImages = [[[adium preferenceController] preferenceForKey:KEY_AB_USE_IMAGES
124                                                                                                                                                   group:PREF_GROUP_ADDRESSBOOK] boolValue];
125         
126         //Use Nick Name and the format menu are irrelevent if importing of names is not enabled
127         [checkBox_useNickName setEnabled:enableImport]; 
128         [popUp_formatMenu setEnabled:enableImport];
129         [checkBox_useMiddleName setEnabled:enableImport];
131         //We will not allow image syncing if AB images are preferred
132         //so disable the control and uncheck the box to indicate this to the user
133         //dchoby98: why are image import and export linked?
134         //[checkBox_syncAutomatic setEnabled:!preferABImages];
135         //if (preferABImages)
136         //      [checkBox_syncAutomatic setState:NSOffState];
137         
138         //Disable the image priority checkbox if we aren't using images
139         [checkBox_preferABImages setEnabled:useImages];
143  * @brief Configure the menu of name formats
144  */
145 - (void)configureFormatMenu
147     NSMenu                      *choicesMenu = [[[NSMenu allocWithZone:[NSMenu menuZone]] init] autorelease];
148     NSMenuItem          *menuItem;
149         NSString                *firstTitle, *firstLastTitle, *lastFirstTitle, *lastFirstNoCommaTitle;
150         
151         BOOL                    useMiddleName = [[[adium preferenceController] preferenceForKey:KEY_AB_USE_MIDDLE
152                                                                                                                                                           group:PREF_GROUP_ADDRESSBOOK] boolValue];
153         
154         //If the use middle name preference is set, we use the menu titles that include a middle name
155         if (useMiddleName) {
156                 firstTitle = ADDRESS_BOOK_FIRST_MIDDLE_OPTION;
157                 firstLastTitle = ADDRESS_BOOK_FIRST_MIDDLE_LAST_OPTION;
158                 lastFirstTitle = ADDRESS_BOOK_LAST_FIRST_MIDDLE_OPTION;
159                 lastFirstNoCommaTitle = ADDRESS_BOOK_LAST_FIRST_MIDDLE_NO_COMMA_OPTION;
160                 
161         //Otherwise we use the standard menu titles
162         } else {
163                 firstTitle = ADDRESS_BOOK_FIRST_OPTION;
164                 firstLastTitle = ADDRESS_BOOK_FIRST_LAST_OPTION;
165                 lastFirstTitle = ADDRESS_BOOK_LAST_FIRST_OPTION;
166                 lastFirstNoCommaTitle = ADDRESS_BOOK_LAST_FIRST_NO_COMMA_OPTION;
167                 
168         }
169         
170         menuItem = [[[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:firstTitle
171                                                                                                                                          target:self
172                                                                                                                                          action:@selector(changeFormat:)
173                                                                                                                           keyEquivalent:@""] autorelease];
174     [menuItem setTag:First];
175     [choicesMenu addItem:menuItem];
176         
177     menuItem = [[[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:firstLastTitle
178                                                                                                                                          target:self
179                                                                                                                                          action:@selector(changeFormat:)
180                                                                                                                           keyEquivalent:@""] autorelease];
181     [menuItem setTag:FirstLast];
182     [choicesMenu addItem:menuItem];
183     
184     menuItem = [[[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:lastFirstTitle
185                                                                                                                                          target:self
186                                                                                                                                          action:@selector(changeFormat:)
187                                                                                                                           keyEquivalent:@""] autorelease];
188     [menuItem setTag:LastFirst];
189     [choicesMenu addItem:menuItem];
190         
191         menuItem = [[[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:lastFirstNoCommaTitle
192                                                                                                                                          target:self
193                                                                                                                                          action:@selector(changeFormat:)
194                                                                                                                           keyEquivalent:@""] autorelease];
195         [menuItem setTag:LastFirstNoComma];
196         [choicesMenu addItem:menuItem];
197         
198     [popUp_formatMenu setMenu:choicesMenu];
199         
200     NSRect oldFrame = [popUp_formatMenu frame];
201     [popUp_formatMenu setFrameOrigin:oldFrame.origin];
202         [[self view] setNeedsDisplay:YES];
206  * @brief Save changed name format preference
207  */
208 - (IBAction)changeFormat:(id)sender
210         [[adium preferenceController] setPreference:[NSNumber numberWithInt:[sender tag]]
211                                             forKey:KEY_AB_DISPLAYFORMAT
212                                             group:PREF_GROUP_ADDRESSBOOK];
216  * @brief Save changed preference
217  */
218 - (IBAction)changePreference:(id)sender
220     if (sender == checkBox_syncAutomatic) {
221         [[adium preferenceController] setPreference:[NSNumber numberWithBool:([sender state]==NSOnState)]
222                                              forKey:KEY_AB_IMAGE_SYNC
223                                               group:PREF_GROUP_ADDRESSBOOK];
224                 
225     } else if (sender == checkBox_useABImages) {
226         [[adium preferenceController] setPreference:[NSNumber numberWithBool:([sender state]==NSOnState)]
227                                              forKey:KEY_AB_USE_IMAGES
228                                               group:PREF_GROUP_ADDRESSBOOK];
229                 
230     } else if (sender == checkBox_useNickName) {
231         [[adium preferenceController] setPreference:[NSNumber numberWithBool:([sender state]==NSOnState)]
232                                              forKey:KEY_AB_USE_NICKNAME
233                                               group:PREF_GROUP_ADDRESSBOOK];
234                 
235         } else if (sender == checkBox_useMiddleName) {
236                 [[adium preferenceController] setPreference:[NSNumber numberWithBool:([sender state] == NSOnState)]
237                                                                                          forKey:KEY_AB_USE_MIDDLE
238                                                                                           group:PREF_GROUP_ADDRESSBOOK];
239                 //Update the format menu to reflect the use of middle names
240                 [self configureFormatMenu];
241                 
242     } else if (sender == checkBox_enableImport) {
243         [[adium preferenceController] setPreference:[NSNumber numberWithBool:([sender state] == NSOnState)]
244                                              forKey:KEY_AB_ENABLE_IMPORT
245                                               group:PREF_GROUP_ADDRESSBOOK];
246                 
247     } else if (sender == checkBox_preferABImages) {
248         [[adium preferenceController] setPreference:[NSNumber numberWithBool:([sender state] == NSOnState)]
249                                              forKey:KEY_AB_PREFER_ADDRESS_BOOK_IMAGES
250                                               group:PREF_GROUP_ADDRESSBOOK];
251                 
252     } else if (sender == checkBox_enableNoteSync) {
253         [[adium preferenceController] setPreference:[NSNumber numberWithBool:([sender state] == NSOnState)]
254                                              forKey:KEY_AB_NOTE_SYNC
255                                               group:PREF_GROUP_ADDRESSBOOK];
256                 
257     } else if (sender == checkBox_metaContacts) {
258                 BOOL shouldCreateMetaContacts = ([sender state] == NSOnState);
259                 
260                 if (shouldCreateMetaContacts) {
261                         [[adium preferenceController] setPreference:[NSNumber numberWithBool:YES]
262                                                                                                  forKey:KEY_AB_CREATE_METACONTACTS
263                                                                                                   group:PREF_GROUP_ADDRESSBOOK];                
264                         
265                 } else {
266                         NSBeginAlertSheet(nil,
267                                                           AILocalizedString(@"Unconsolidate all metacontacts",nil),
268                                                           AILocalizedString(@"Cancel",nil), nil,
269                                                           [[self view] window], self,
270                                                           @selector(sheetDidEnd:returnCode:contextInfo:), NULL,
271                                                           NULL,
272                                                           AILocalizedString(@"Disabling automatic contact consolidation will also unconsolidate all existing metacontacts, including any created manually.  You will need to recreate any manually-created metacontacts if you proceed.",nil));
273                 }
274         }
276     [self configureControlDimming];
279 - (void)sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
281         if (returnCode == NSAlertDefaultReturn) {
282                 //If we now shouldn't create metaContacts, clear 'em all... not pretty, but effective.
284                 //Delay to the next run loop to give better UI responsiveness
285                 [[adium contactController] performSelector:@selector(clearAllMetaContactData)
286                                                                                 withObject:nil
287                                                                                 afterDelay:0.0001];
288                 
289                 
290                 [[adium preferenceController] setPreference:[NSNumber numberWithBool:NO]
291                                              forKey:KEY_AB_CREATE_METACONTACTS
292                                               group:PREF_GROUP_ADDRESSBOOK];            
293         } else {
294                 //Put the checkbox back
295                 [checkBox_metaContacts setState:![checkBox_metaContacts state]];
296         }
299 @end