]> err.no Git - scalable-opengroupware.org/blob - UI/PreferencesUI/UIxPreferences.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1151 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / PreferencesUI / UIxPreferences.m
1 /* UIxPreferences.m - this file is part of SOGo
2  *
3  * Copyright (C) 2007 Inverse groupe conseil
4  *
5  * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
6  *
7  * This file is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * This file is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #import <Foundation/NSCalendarDate.h>
24 #import <Foundation/NSString.h>
25 #import <Foundation/NSTimeZone.h>
26 #import <Foundation/NSUserDefaults.h>
27
28 #import <NGObjWeb/WOContext.h>
29 #import <NGObjWeb/WORequest.h>
30
31 #import <SoObjects/SOGo/SOGoUser.h>
32
33 #import "UIxPreferences.h"
34
35 #warning this class is not finished
36 /* remaining:
37    default event length
38    default snooze length
39    refresh calendar every X minutes
40    workweek = from -> to
41    identities */
42
43 static BOOL defaultsRead = NO;
44 static BOOL shouldDisplayPasswordChange = NO;
45
46 @implementation UIxPreferences
47
48 + (void) initialize
49 {
50   NSUserDefaults *ud;
51
52   if (!defaultsRead)
53     {
54       ud = [NSUserDefaults standardUserDefaults];
55       shouldDisplayPasswordChange
56         = [ud boolForKey: @"SOGoUIxUserCanChangePassword"];
57       defaultsRead = YES;
58     }
59 }
60
61 - (id) init
62 {
63   NSDictionary *locale;
64   
65   if ((self = [super init]))
66     {
67       item = nil;
68       hours = nil;
69       ASSIGN (user, [context activeUser]);
70       ASSIGN (userDefaults, [user userDefaults]);
71       ASSIGN (today, [NSCalendarDate date]);
72       locale = [context valueForKey: @"locale"];
73       ASSIGN (daysOfWeek,
74               [locale objectForKey: NSWeekDayNameArray]);
75     }
76
77   return self;
78 }
79
80 - (void) dealloc
81 {
82   [today release];
83   [item release];
84   [user release];
85   [userDefaults release];
86   [hours release];
87   [daysOfWeek release];
88   [super dealloc];
89 }
90
91 - (void) setItem: (NSString *) newItem
92 {
93   ASSIGN (item, newItem);
94 }
95
96 - (NSString *) item
97 {
98   return item;
99 }
100
101 - (NSString *) inTheOffice
102 {
103   NSString *inTheOffice;
104
105   inTheOffice = [userDefaults objectForKey: @"InTheOffice"];
106
107   return ((!inTheOffice || [inTheOffice boolValue])
108           ? @"YES" : @"NO");
109 }
110
111 - (void) setInTheOffice: (NSString *) newValue
112 {
113   [userDefaults setObject: newValue forKey: @"InTheOffice"];
114 }
115
116 - (void) setAutoReplyText: (NSString *) newAutoReplyText
117 {
118   [userDefaults setObject: newAutoReplyText forKey: @"AutoReplyText"];
119 }
120
121 - (NSString *) autoReplyText
122 {
123   return [userDefaults objectForKey: @"AutoReplyText"];
124 }
125
126 - (NSArray *) timeZonesList
127 {
128   return [NSTimeZone knownTimeZoneNames];
129 }
130
131 - (NSString *) userTimeZone
132 {
133   return [[user timeZone] timeZoneName];
134 }
135
136 - (void) setUserTimeZone: (NSString *) newUserTimeZone
137 {
138   [userDefaults setObject: newUserTimeZone forKey: @"TimeZone"];
139 }
140
141 - (NSArray *) shortDateFormatsList
142 {
143   NSMutableArray *shortDateFormatsList = nil;
144   NSString *key, *currentFormat;
145   unsigned int nbr;
146   BOOL done;
147
148   shortDateFormatsList = [NSMutableArray array];
149
150   nbr = 0;
151   done = NO;
152   while (!done)
153     {
154       key = [NSString stringWithFormat: @"shortDateFmt_%d", nbr];
155       currentFormat = [self labelForKey: key];
156       if ([currentFormat length] > 0)
157         {
158           [shortDateFormatsList addObject: currentFormat];
159           nbr++;
160         }
161       else
162         done = YES;
163     }
164
165   return shortDateFormatsList;
166 }
167
168 - (NSString *) itemShortDateFormatText
169 {
170   return [today descriptionWithCalendarFormat: item
171                 locale: [context valueForKey: @"locale"]];
172 }
173
174 - (NSString *) userShortDateFormat
175 {
176   return [userDefaults objectForKey: @"ShortDateFormat"];
177 }
178
179 - (void) setUserShortDateFormat: (NSString *) newFormat
180 {
181   [userDefaults setObject: newFormat forKey: @"ShortDateFormat"];
182 }
183
184 - (NSArray *) longDateFormatsList
185 {
186   NSMutableArray *longDateFormatsList = nil;
187   NSString *key, *currentFormat;
188   unsigned int nbr;
189   BOOL done;
190
191   longDateFormatsList = [NSMutableArray array];
192
193   nbr = 0;
194   done = NO;
195   while (!done)
196     {
197       key = [NSString stringWithFormat: @"longDateFmt_%d", nbr];
198       currentFormat = [self labelForKey: key];
199       if ([currentFormat length] > 0)
200         {
201           [longDateFormatsList addObject: currentFormat];
202           nbr++;
203         }
204       else
205         done = YES;
206     }
207
208   return longDateFormatsList;
209 }
210
211 - (NSString *) itemLongDateFormatText
212 {
213   return [today descriptionWithCalendarFormat: item
214                 locale: [context valueForKey: @"locale"]];
215 }
216
217 - (NSString *) userLongDateFormat
218 {
219   return [userDefaults objectForKey: @"LongDateFormat"];
220 }
221
222 - (void) setUserLongDateFormat: (NSString *) newFormat
223 {
224   [userDefaults setObject: newFormat forKey: @"LongDateFormat"];
225 }
226
227 - (NSArray *) timeFormatsList
228 {
229   NSMutableArray *timeFormatsList = nil;
230   NSString *key, *currentFormat;
231   unsigned int nbr;
232   BOOL done;
233
234   timeFormatsList = [NSMutableArray array];
235
236   nbr = 0;
237   done = NO;
238   while (!done)
239     {
240       key = [NSString stringWithFormat: @"timeFmt_%d", nbr];
241       currentFormat = [self labelForKey: key];
242       if ([currentFormat length] > 0)
243         {
244           [timeFormatsList addObject: currentFormat];
245           nbr++;
246         }
247       else
248         done = YES;
249     }
250
251   return timeFormatsList;
252 }
253
254 - (NSString *) itemTimeFormatText
255 {
256   return [today descriptionWithCalendarFormat: item
257                 locale: [context valueForKey: @"locale"]];
258 }
259
260 - (NSString *) userTimeFormat
261 {
262   return [userDefaults objectForKey: @"TimeFormat"];
263 }
264
265 - (void) setUserTimeFormat: (NSString *) newFormat
266 {
267   [userDefaults setObject: newFormat forKey: @"TimeFormat"];
268 }
269
270 - (NSArray *) daysList
271 {
272   NSMutableArray *daysList;
273   unsigned int currentDay;
274
275   daysList = [NSMutableArray new];
276   [daysList autorelease];
277   for (currentDay = 0; currentDay < 7; currentDay++)
278     [daysList addObject: [NSString stringWithFormat: @"%d", currentDay]];
279
280   return daysList;
281 }
282
283 - (NSString *) itemWeekStartDay
284 {
285   return [daysOfWeek objectAtIndex: [item intValue]];
286 }
287
288 - (NSString *) userWeekStartDay
289 {
290   return [userDefaults objectForKey: @"WeekStartDay"];
291 }
292
293 - (void) setUserWeekStartDay: (NSString *) newDay
294 {
295   [userDefaults setObject: newDay forKey: @"WeekStartDay"];
296 }
297
298 - (NSArray *) hoursList
299 {
300   unsigned int currentHour;
301
302   if (!hours)
303     {
304       hours = [[NSMutableArray alloc] initWithCapacity: 24];
305       for (currentHour = 0; currentHour < 24; currentHour++)
306         [hours addObject: [NSString stringWithFormat: @"%.2d:00",
307                                     currentHour]];
308     }
309
310   return hours;
311 }
312
313 - (NSString *) userDayStartTime
314 {
315   NSString *time;
316
317   time = [userDefaults objectForKey: @"DayStartTime"];
318   if (!time)
319     time = @"08:00";
320
321   return time;
322 }
323
324 - (void) setUserDayStartTime: (NSString *) newTime
325 {
326   [userDefaults setObject: newTime forKey: @"DayStartTime"];
327 }
328
329 - (NSString *) userDayEndTime
330 {
331   NSString *time;
332
333   time = [userDefaults objectForKey: @"DayEndTime"];
334   if (!time)
335     time = @"18:00";
336
337   return time;
338 }
339
340 - (void) setUserDayEndTime: (NSString *) newTime
341 {
342   [userDefaults setObject: newTime forKey: @"DayEndTime"];
343 }
344
345 - (NSArray *) firstWeekList
346 {
347   return [NSArray arrayWithObjects: SOGoWeekStartHideWeekNumbers,
348                   SOGoWeekStartJanuary1,
349                   SOGoWeekStartFirst4DayWeek,
350                   SOGoWeekStartFirstFullWeek, nil];
351 }
352
353 - (NSString *) itemFirstWeekText
354 {
355   return [self labelForKey: [NSString stringWithFormat: @"firstWeekOfYear_%@",
356                                       item]];
357 }
358
359 - (NSString *) userFirstWeek
360 {
361   return [userDefaults objectForKey: @"FirstWeek"];
362 }
363
364 - (void) setUserFirstWeek: (NSString *) newFirstWeek
365 {
366   [userDefaults setObject: newFirstWeek forKey: @"FirstWeek"];
367 }
368
369 - (NSString *) reminderEnabled
370 {
371   NSString *reminderEnabled;
372
373   reminderEnabled = [userDefaults objectForKey: @"ReminderEnabled"];
374
375   return ((!reminderEnabled || [reminderEnabled boolValue])
376           ? @"YES" : @"NO");
377 }
378
379 - (void) setReminderEnabled: (NSString *) newValue
380 {
381   [userDefaults setObject: newValue forKey: @"ReminderEnabled"];
382 }
383
384 - (NSString *) remindWithASound
385 {
386   NSString *remindWithASound;
387
388   remindWithASound = [userDefaults objectForKey: @"RemindWithASound"];
389
390   return ((!remindWithASound || [remindWithASound boolValue])
391           ? @"YES" : @"NO");
392 }
393
394 - (void) setRemindWithASound: (NSString *) newValue
395 {
396   [userDefaults setObject: newValue forKey: @"RemindWithASound"];
397 }
398
399 - (NSArray *) reminderTimesList
400 {
401   return [NSArray arrayWithObjects: @"0000", @"0005", @"0010", @"0015",
402                   @"0030", @"0100", @"0200", @"0400", @"0800", @"1200",
403                   @"2400", @"4800", nil];
404 }
405
406 - (NSString *) itemReminderTimeText
407 {
408   return [self labelForKey:
409                  [NSString stringWithFormat: @"reminderTime_%@", item]];
410 }
411
412 - (NSString *) userReminderTime
413 {
414   return [userDefaults objectForKey: @"ReminderTime"];
415 }
416
417 - (void) setReminderTime: (NSString *) newTime
418 {
419   [userDefaults setObject: newTime forKey: @"ReminderTime"];
420 }
421
422 - (id <WOActionResults>) defaultAction
423 {
424   id <WOActionResults> results;
425   WORequest *request;
426
427   request = [context request];
428   if ([[request method] isEqualToString: @"POST"])
429     {
430       [userDefaults synchronize];
431       results = [self jsCloseWithRefreshMethod: nil];
432     }
433   else
434     results = self;
435
436   return results;
437 }
438
439 - (BOOL) shouldTakeValuesFromRequest: (WORequest *) request
440                            inContext: (WOContext*) context
441 {
442   return [[request method] isEqualToString: @"POST"];
443 }
444
445 - (BOOL) shouldDisplayPasswordChange
446 {
447   return shouldDisplayPasswordChange;
448 }
449
450 @end