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