Plugins/General Preferences/ESGeneralPreferences.m
author Zachary West <zacw@adium.im>
Wed Nov 25 22:28:47 2009 -0500 (2009-11-25)
changeset 2820 37f86ef91f64
parent 2392 955aecf2b34c
child 3326 030035dabb86
permissions -rw-r--r--
Code simplification; remove the old preferences, and don't use a static variable to count the lines. Refs #13362.

I can't imagine using a static variable like this wouldn't cause problems, especially with "save chats" enabled; we'd be messing with the same static variable like crazy. I'm betting this explains some weird context problems, so let's see if this helps #13362.
     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  */
    16 
    17 #import <Adium/AIContentControllerProtocol.h>
    18 #import <Adium/AIInterfaceControllerProtocol.h>
    19 #import "AISoundController.h"
    20 #import "ESGeneralPreferences.h"
    21 #import "ESGeneralPreferencesPlugin.h"
    22 #import "PTHotKeyCenter.h"
    23 #import "PTHotKey.h"
    24 #import "SRRecorderControl.h"
    25 #import "PTHotKey.h"
    26 #import "AIMessageHistoryPreferencesWindowController.h"
    27 #import "AIMessageWindowController.h"
    28 #import <Adium/AIServiceIcons.h>
    29 #import <Adium/AIStatusIcons.h>
    30 #import <AIUtilities/AIColorAdditions.h>
    31 #import <AIUtilities/AIFontAdditions.h>
    32 #import <AIUtilities/AIImageAdditions.h>
    33 #import <AIUtilities/AIMenuAdditions.h>
    34 #import <AIUtilities/AIPopUpButtonAdditions.h>
    35 
    36 #define	PREF_GROUP_DUAL_WINDOW_INTERFACE	@"Dual Window Interface"
    37 #define KEY_TABBAR_POSITION					@"Tab Bar Position"
    38 
    39 @interface ESGeneralPreferences ()
    40 - (NSMenu *)tabChangeKeysMenu;
    41 - (NSMenu *)sendKeysMenu;
    42 - (NSMenu *)tabPositionMenu;
    43 @end
    44 
    45 @implementation ESGeneralPreferences
    46 
    47 // XXX in order to edit the nib, you need the ShortcutReporter palette
    48 // You can download it at http://evands.penguinmilitia.net/ShortcutRecorder.palette.zip
    49 // This comes from http://wafflesoftware.net/shortcut/
    50 
    51 + (NSSet *)keyPathsForValuesAffectingChatHistoryDisplayActive
    52 {
    53 	return [NSSet setWithObjects:@"adium.preferenceController.Logging.Enable Logging",
    54 			@"adium.preferenceController.Message Context Display.Display Message Context",
    55 			nil];
    56 }
    57 
    58 //Preference pane properties
    59 - (NSString *)paneIdentifier
    60 {
    61 	return @"General";
    62 }
    63 - (NSString *)paneName{	
    64     return AILocalizedString(@"General","General preferences label");
    65 }
    66 - (NSString *)nibName{
    67     return @"GeneralPreferences";
    68 }
    69 - (NSImage *)paneIcon
    70 {
    71 	return [NSImage imageNamed:@"pref-general" forClass:[self class]];
    72 }
    73 
    74 //Configure the preference view
    75 - (void)viewDidLoad
    76 {
    77 	BOOL			sendOnEnter, sendOnReturn;
    78 
    79 	//Interface
    80     [checkBox_messagesInTabs setState:[[adium.preferenceController preferenceForKey:KEY_TABBED_CHATTING
    81 																				group:PREF_GROUP_INTERFACE] boolValue]];
    82 	[checkBox_arrangeByGroup setState:[[adium.preferenceController preferenceForKey:KEY_GROUP_CHATS_BY_GROUP
    83 																				group:PREF_GROUP_INTERFACE] boolValue]];
    84 	
    85 	// Update Checking
    86 	[checkBox_updatesAutomatic setState:[[NSUserDefaults standardUserDefaults] boolForKey:@"SUEnableAutomaticChecks"]];
    87 	[checkBox_updatesProfileInfo setState:[[NSUserDefaults standardUserDefaults] boolForKey:@"SUSendProfileInfo"]];
    88 	[checkBox_updatesIncludeBetas setState:[[NSUserDefaults standardUserDefaults] boolForKey:@"AIAlwaysUpdateToBetas"]];
    89 
    90 	//Chat Cycling
    91 	[popUp_tabKeys setMenu:[self tabChangeKeysMenu]];
    92 	[popUp_tabKeys selectItemWithTag:[[adium.preferenceController preferenceForKey:KEY_TAB_SWITCH_KEYS
    93 																						 group:PREF_GROUP_CHAT_CYCLING] intValue]];
    94 
    95 	//General
    96 	sendOnEnter = [[adium.preferenceController preferenceForKey:SEND_ON_ENTER
    97 															group:PREF_GROUP_GENERAL] boolValue];
    98 	sendOnReturn = [[adium.preferenceController preferenceForKey:SEND_ON_RETURN
    99 															group:PREF_GROUP_GENERAL] boolValue];
   100 	[popUp_sendKeys setMenu:[self sendKeysMenu]];
   101 	
   102 	if (sendOnEnter && sendOnReturn) {
   103 		[popUp_sendKeys selectItemWithTag:AISendOnBoth];
   104 	} else if (sendOnEnter) {
   105 		[popUp_sendKeys selectItemWithTag:AISendOnEnter];			
   106 	} else if (sendOnReturn) {
   107 		[popUp_sendKeys selectItemWithTag:AISendOnReturn];
   108 	}
   109 
   110 	[popUp_tabPositionMenu setMenu:[self tabPositionMenu]];
   111 	[popUp_tabPositionMenu selectItemWithTag:[[adium.preferenceController preferenceForKey:KEY_TABBAR_POSITION
   112 																								 group:PREF_GROUP_DUAL_WINDOW_INTERFACE] intValue]];
   113 
   114 	//Global hotkey
   115 	TISInputSourceRef currentLayout = TISCopyCurrentKeyboardLayoutInputSource();
   116 	
   117 	if (TISGetInputSourceProperty(currentLayout, kTISPropertyUnicodeKeyLayoutData)) {
   118 		PTKeyCombo *keyCombo = [[[PTKeyCombo alloc] initWithPlistRepresentation:[adium.preferenceController preferenceForKey:KEY_GENERAL_HOTKEY
   119 																														 group:PREF_GROUP_GENERAL]] autorelease];
   120 		[shortcutRecorder setKeyCombo:SRMakeKeyCombo([keyCombo keyCode], [shortcutRecorder carbonToCocoaFlags:[keyCombo modifiers]])];
   121 		[shortcutRecorder setAnimates:YES];
   122 		[shortcutRecorder setStyle:SRGreyStyle];
   123 		
   124 		[label_shortcutRecorder setLocalizedString:AILocalizedString(@"When pressed, this key combination will bring Adium to the front", nil)];
   125 	} else {
   126 		[shortcutRecorder setEnabled:NO];
   127 		
   128 		[label_shortcutRecorder setLocalizedString:AILocalizedString(@"You are using an old-style (rsrc) keyboard layout which Adium does not support.", nil)];
   129 	}
   130 	
   131 	CFRelease(currentLayout);
   132 
   133     [self configureControlDimming];
   134 }
   135 
   136 //Called in response to all preference controls, applies new settings
   137 - (IBAction)changePreference:(id)sender
   138 {
   139     if (sender == popUp_tabKeys) {
   140 		AITabKeys keySelection = [[sender selectedItem] tag];
   141 
   142 		[adium.preferenceController setPreference:[NSNumber numberWithInt:keySelection]
   143 											 forKey:KEY_TAB_SWITCH_KEYS
   144 											  group:PREF_GROUP_CHAT_CYCLING];
   145 		
   146 	} else if (sender == popUp_sendKeys) {
   147 		AISendKeys 	keySelection = [[sender selectedItem] tag];
   148 		BOOL		sendOnEnter = (keySelection == AISendOnEnter || keySelection == AISendOnBoth);
   149 		BOOL		sendOnReturn = (keySelection == AISendOnReturn || keySelection == AISendOnBoth);
   150 		
   151 		[adium.preferenceController setPreference:[NSNumber numberWithInt:sendOnEnter]
   152 											 forKey:SEND_ON_ENTER
   153 											  group:PREF_GROUP_GENERAL];
   154 		[adium.preferenceController setPreference:[NSNumber numberWithInt:sendOnReturn]
   155 											 forKey:SEND_ON_RETURN
   156                                               group:PREF_GROUP_GENERAL];
   157 	} else if (sender == checkBox_updatesAutomatic) {
   158 		[[NSUserDefaults standardUserDefaults] setBool:[sender state] forKey:@"SUEnableAutomaticChecks"];
   159 		[self configureControlDimming];
   160 	} else if (sender == checkBox_updatesProfileInfo) {
   161 		[[NSUserDefaults standardUserDefaults] setBool:[sender state] forKey:@"SUSendProfileInfo"];
   162 	} else if (sender == checkBox_updatesIncludeBetas) {
   163 		[[NSUserDefaults standardUserDefaults] setBool:[sender state] forKey:@"AIAlwaysUpdateToBetas"];
   164 	}
   165 }
   166 
   167 //Dim controls as needed
   168 - (void)configureControlDimming
   169 {
   170 	[checkBox_arrangeByGroup setEnabled:[checkBox_messagesInTabs state]];
   171 	[checkBox_updatesProfileInfo setEnabled:[checkBox_updatesAutomatic state]];
   172 #ifdef BETA_RELEASE
   173 	[checkBox_updatesIncludeBetas setEnabled:NO];
   174 	[checkBox_updatesIncludeBetas setState:NSOnState];
   175 #else
   176 	[checkBox_updatesIncludeBetas setEnabled:[checkBox_updatesAutomatic state]];
   177 #endif
   178 }
   179 
   180 /*!
   181  * @brief Construct our menu by hand for easy localization
   182  */
   183 - (NSMenu *)tabChangeKeysMenu
   184 {
   185 	NSMenu		*menu = [[NSMenu allocWithZone:[NSMenu menuZone]] init];
   186 #define PLACE_OF_INTEREST_SIGN	"\u2318"
   187 #define LEFTWARDS_ARROW			"\u2190"
   188 #define RIGHTWARDS_ARROW		"\u2192"
   189 #define SHIFT_ARROW				"\u21E7"
   190 #define OPTION_KEY				"\u2325"
   191 
   192 	[menu addItemWithTitle:[NSString stringWithFormat:AILocalizedString(@"Arrows (%@ and %@)","Directional arrow keys word"), [NSString stringWithUTF8String:PLACE_OF_INTEREST_SIGN LEFTWARDS_ARROW], [NSString stringWithUTF8String:PLACE_OF_INTEREST_SIGN RIGHTWARDS_ARROW]]
   193 					target:nil
   194 					action:nil
   195 			 keyEquivalent:@""
   196 					   tag:AISwitchArrows];
   197 	
   198 	[menu addItemWithTitle:[NSString stringWithFormat:AILocalizedString(@"Shift + Arrows (%@ and %@)","Shift key word + Directional arrow keys word"), [NSString stringWithUTF8String:SHIFT_ARROW PLACE_OF_INTEREST_SIGN LEFTWARDS_ARROW], [NSString stringWithUTF8String:SHIFT_ARROW PLACE_OF_INTEREST_SIGN RIGHTWARDS_ARROW]]
   199 					target:nil
   200 					action:nil
   201 			 keyEquivalent:@""
   202 					   tag:AISwitchShiftArrows];
   203 	
   204 	[menu addItemWithTitle:[NSString stringWithFormat:AILocalizedString(@"Option + Arrows (%@ and %@)","Option key word + Directional arrow keys word"), [NSString stringWithUTF8String:OPTION_KEY PLACE_OF_INTEREST_SIGN LEFTWARDS_ARROW], [NSString stringWithUTF8String:OPTION_KEY PLACE_OF_INTEREST_SIGN RIGHTWARDS_ARROW]]
   205 					target:nil
   206 					action:nil
   207 			 keyEquivalent:@""
   208 					   tag:AIOptArrows];	
   209 	
   210 	[menu addItemWithTitle:[NSString stringWithFormat:AILocalizedString(@"Brackets (%@ and %@)","Word for [ and ] keys"), [NSString stringWithUTF8String:PLACE_OF_INTEREST_SIGN "["], [NSString stringWithUTF8String:PLACE_OF_INTEREST_SIGN "]"]]
   211 					target:nil
   212 					action:nil
   213 			 keyEquivalent:@""
   214 					   tag:AIBrackets];
   215 	
   216 	[menu addItemWithTitle:[NSString stringWithFormat:AILocalizedString(@"Curly braces (%@ and %@)","Word for { and } keys"), [NSString stringWithUTF8String:PLACE_OF_INTEREST_SIGN "{"], [NSString stringWithUTF8String:PLACE_OF_INTEREST_SIGN "}"]]
   217 					target:nil
   218 					action:nil
   219 			 keyEquivalent:@""
   220 					   tag:AIBraces];
   221 	
   222 	
   223 	return [menu autorelease];		
   224 }
   225 
   226 - (BOOL)shortcutRecorder:(SRRecorderControl *)aRecorder isKeyCode:(signed short)keyCode andFlagsTaken:(unsigned int)flags reason:(NSString **)aReason
   227 {
   228 	return NO;
   229 }
   230 
   231 - (void)shortcutRecorder:(SRRecorderControl *)aRecorder keyComboDidChange:(KeyCombo)newKeyCombo
   232 {
   233 	if (aRecorder == shortcutRecorder) {
   234 		PTKeyCombo *keyCombo = [PTKeyCombo keyComboWithKeyCode:[shortcutRecorder keyCombo].code
   235 													 modifiers:[shortcutRecorder cocoaToCarbonFlags:[shortcutRecorder keyCombo].flags]];
   236 		[adium.preferenceController setPreference:[keyCombo plistRepresentation]
   237 											 forKey:KEY_GENERAL_HOTKEY
   238 											  group:PREF_GROUP_GENERAL];
   239 	}
   240 }
   241 
   242 /*!
   243  * @brief Construct our menu by hand for easy localization
   244  */
   245 - (NSMenu *)sendKeysMenu
   246 {
   247 	NSMenu		*menu = [[NSMenu allocWithZone:[NSMenu menuZone]] init];
   248 
   249 	[menu addItemWithTitle:AILocalizedString(@"Enter","Enter key for sending messages")
   250 					target:nil
   251 					action:nil
   252 			 keyEquivalent:@""
   253 					   tag:AISendOnEnter];
   254 
   255 	[menu addItemWithTitle:AILocalizedString(@"Return","Return key for sending messages")
   256 					target:nil
   257 					action:nil
   258 			 keyEquivalent:@""
   259 					   tag:AISendOnReturn];
   260 
   261 	[menu addItemWithTitle:AILocalizedString(@"Enter and Return","Enter and return key for sending messages")
   262 					target:nil
   263 					action:nil
   264 			 keyEquivalent:@""
   265 					   tag:AISendOnBoth];
   266 
   267 	return [menu autorelease];		
   268 }
   269 
   270 - (NSMenu *)tabPositionMenu
   271 {
   272 	NSMenu		*menu = [[NSMenu allocWithZone:[NSMenu menuZone]] init];
   273 	
   274 	[menu addItemWithTitle:AILocalizedString(@"Top","Position menu item for tabs at the top of the message window")
   275 					target:nil
   276 					action:nil
   277 			 keyEquivalent:@""
   278 					   tag:AdiumTabPositionTop];
   279 	
   280 	[menu addItemWithTitle:AILocalizedString(@"Bottom","Position menu item for tabs at the bottom of the message window")
   281 					target:nil
   282 					action:nil
   283 			 keyEquivalent:@""
   284 					   tag:AdiumTabPositionBottom];
   285 	
   286 	[menu addItemWithTitle:AILocalizedString(@"Left","Position menu item for tabs at the left of the message window")
   287 					target:nil
   288 					action:nil
   289 			 keyEquivalent:@""
   290 					   tag:AdiumTabPositionLeft];
   291 
   292 	[menu addItemWithTitle:AILocalizedString(@"Right","Position menu item for tabs at the right of the message window")
   293 					target:nil
   294 					action:nil
   295 			 keyEquivalent:@""
   296 					   tag:AdiumTabPositionRight];
   297 	
   298 	return [menu autorelease];
   299 }
   300 
   301 - (BOOL)chatHistoryDisplayActive
   302 {
   303 	return ([[adium.preferenceController preferenceForKey:@"Display Message Context" group:@"Message Context Display"] boolValue] &&
   304 			[[adium.preferenceController preferenceForKey:@"Enable Logging" group:@"Logging"] boolValue]);
   305 }
   306 - (void)setChatHistoryDisplayActive:(BOOL)flag
   307 {
   308 	[adium.preferenceController setPreference:[NSNumber	numberWithBool:flag]
   309 	 forKey:@"Display Message Context"
   310 	 group:@"Message Context Display"];
   311 }
   312 
   313 #pragma mark Message history
   314 - (IBAction)configureMessageHistory:(id)sender
   315 {
   316 	[AIMessageHistoryPreferencesWindowController configureMessageHistoryPreferencesOnWindow:[[self view] window]];
   317 }
   318 
   319 @end