1/*
2
3  Copyright (c) 2009 MacUIM Project http://code.google.com/p/macuim/
4
5  All rights reserved.
6
7  Redistribution and use in source and binary forms, with or without
8  modification, are permitted provided that the following conditions
9  are met:
10
11  1. Redistributions of source code must retain the above copyright
12     notice, this list of conditions and the following disclaimer.
13  2. Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions and the following disclaimer in the
15     documentation and/or other materials provided with the distribution.
16  3. Neither the name of authors nor the names of its contributors
17     may be used to endorse or promote products derived from this software
18     without specific prior written permission.
19
20  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
21  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
24  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  SUCH DAMAGE.
31
32*/
33
34#import "Debug.h"
35#import "PreferenceController.h"
36#import "CocoaWinController.h"
37#import "MacUIMController.h"
38
39
40static CFStringRef gCandFont;
41static float gCandFontSize;
42static CFIndex gCandTransparency;
43static Boolean gEnableModeTips;
44static Boolean gCandVertical;
45static Boolean gEnableAnnotation;
46
47static PreferenceController *sharedController;
48
49@implementation PreferenceController
50
51+ (id)sharedController
52{
53	if (!sharedController)
54		[[self alloc] init];
55
56	return sharedController;
57}
58
59- (id)init
60{
61	if (sharedController)
62		return sharedController;
63
64	self = [super init];
65	sharedController = self;
66
67	[self loadSetting];
68
69	return self;
70}
71
72- (void)dealloc
73{
74	[super dealloc];
75}
76
77- (void)loadSetting
78{
79	CFPropertyListRef propVal;
80	Boolean dummy;
81
82	gCandVertical =
83		CFPreferencesGetAppBooleanValue(CFSTR(kPrefCandVertical),
84						CFSTR(kAppID), &dummy);
85
86	gCandTransparency =
87		CFPreferencesGetAppIntegerValue(CFSTR(kPrefCandTransparency),
88						CFSTR(kAppID), &dummy);
89
90	propVal = CFPreferencesCopyAppValue(CFSTR(kPrefCandFont),
91					    CFSTR(kAppID));
92	if (propVal && CFGetTypeID(propVal) == CFStringGetTypeID()) {
93		gCandFont = (CFStringRef)propVal;
94	}
95	if (propVal)
96		CFRelease(propVal);
97
98	propVal = CFPreferencesCopyAppValue(CFSTR(kPrefCandFontSize),
99					    CFSTR(kAppID));
100	if (propVal && CFGetTypeID(propVal) == CFNumberGetTypeID()) {
101		CFNumberGetValue((CFNumberRef)propVal,
102				kCFNumberFloatType, &gCandFontSize);
103	}
104	if (propVal)
105		CFRelease(propVal);
106
107	gEnableModeTips =
108		CFPreferencesGetAppBooleanValue(CFSTR(kPrefModeTips),
109						CFSTR(kAppID), &dummy);
110
111	gEnableAnnotation =
112		CFPreferencesGetAppBooleanValue(CFSTR(kPrefAnnotation),
113						CFSTR(kAppID), &dummy);
114
115	propVal = CFPreferencesCopyAppValue(CFSTR(kPrefIM), CFSTR(kAppID));
116	if (propVal && CFGetTypeID(propVal) == CFStringGetTypeID()) {
117		CFStringGetCString((CFStringRef)propVal, imName, BUFSIZ,
118				   kCFStringEncodingMacRoman);
119	} else
120		[self setIMName:kDefaultIM];
121	if (propVal)
122		CFRelease(propVal);
123}
124
125- (void)setIMName:(const char *)str
126{
127	strlcpy(imName, str, BUFSIZ);
128}
129
130- (const char *)imName
131{
132	return imName;
133}
134
135- (CFStringRef)candFont
136{
137	return gCandFont;
138}
139
140- (int)candTransparency
141{
142	return gCandTransparency;
143}
144
145- (float)candFontSize
146{
147	return gCandFontSize;
148}
149
150- (BOOL)enableModeTips
151{
152	return gEnableModeTips ? YES : NO;
153}
154
155- (BOOL)enableAnnotation
156{
157	return gEnableAnnotation ? YES : NO;
158}
159@end
160
161void NotificationCallback(CFNotificationCenterRef inCenter,
162				 void *inObserver,
163				 CFStringRef inName,
164				 const void *inObject,
165				 CFDictionaryRef inUserInfo)
166{
167  CFStringRef im;
168  char imName[BUFSIZ];
169  CFBooleanRef on;
170  CFStringRef fontName;
171  CFNumberRef fontSize;
172  CFNumberRef trans;
173
174  if (!inUserInfo)
175    return;
176
177  imName[0] = '\0';
178  im = CFDictionaryGetValue(inUserInfo, CFSTR(kPrefIM));
179
180#if DEBUG_NOTIFY
181  NSLog(@"NotificationCallback() im='%@'", (NSString *)im);
182#endif
183  if (im)
184    CFStringGetCString(im, imName, BUFSIZ, kCFStringEncodingMacRoman);
185
186
187  if ((on = CFDictionaryGetValue(inUserInfo, CFSTR(kPrefCandVertical))))
188    gCandVertical = on == kCFBooleanTrue ? true : false;
189
190  // candidate font
191  if ((fontName = CFDictionaryGetValue(inUserInfo, CFSTR(kPrefCandFont)))) {
192    if (gCandFont)
193      CFRelease(gCandFont);
194    gCandFont = CFRetain(fontName);
195    if ((fontSize = CFDictionaryGetValue(inUserInfo, CFSTR(kPrefCandFontSize))))
196      CFNumberGetValue(fontSize, kCFNumberFloatType, &gCandFontSize);
197
198    [[CocoaWinController sharedController] setFont:(NSString *)gCandFont
199					   size:gCandFontSize];
200  }
201
202  if ((trans = CFDictionaryGetValue(inUserInfo, CFSTR(kPrefCandTransparency))))
203    CFNumberGetValue(trans, kCFNumberIntType, &gCandTransparency);
204
205  if ((on = CFDictionaryGetValue(inUserInfo, CFSTR(kPrefModeTips))))
206    gEnableModeTips = on == kCFBooleanTrue ? true : false;
207
208  if ((on = CFDictionaryGetValue(inUserInfo, CFSTR(kPrefAnnotation))))
209    gEnableAnnotation = on == kCFBooleanTrue ? true : false;
210
211#if DEBUG_NOTIFY
212  NSLog(@"NotificationCallback() vertical=%s transparency=%s modetips=%s\n",
213              gCandVertical ? "true" : "false",
214              gCandTransparency ? "true" : "false",
215              gEnableModeTips ? "true" : "false");
216#endif
217
218  if (imName[0] != '\0') {
219    [MacUIMController switchIM:imName];
220    [[PreferenceController sharedController] setIMName:imName];
221  }
222}
223