2014-07-04 16:18:56 +02:00
|
|
|
/* UIxDatePicker.h - this file is part of SOGo
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009-2014 Inverse inc.
|
|
|
|
*
|
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2007-07-12 17:37:14 +02:00
|
|
|
#import <Foundation/NSValue.h>
|
|
|
|
#import <Foundation/NSCalendarDate.h>
|
2010-01-06 20:21:15 +01:00
|
|
|
#import <Foundation/NSTimeZone.h>
|
2007-07-12 17:37:14 +02:00
|
|
|
|
2010-01-06 20:21:15 +01:00
|
|
|
#import <NGObjWeb/WOContext+SoObjects.h>
|
2007-07-12 17:37:14 +02:00
|
|
|
#import <NGObjWeb/WORequest.h>
|
|
|
|
#import <NGExtensions/NSObject+Logs.h>
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2010-01-06 20:21:15 +01:00
|
|
|
#import <SOGo/SOGoUser.h>
|
|
|
|
#import <SOGo/SOGoUserDefaults.h>
|
|
|
|
|
2009-01-07 22:01:16 +01:00
|
|
|
#import "UIxDatePicker.h"
|
2006-06-15 21:34:10 +02:00
|
|
|
|
|
|
|
@implementation UIxDatePicker
|
|
|
|
|
2006-10-19 16:37:13 +02:00
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
2014-07-04 16:18:56 +02:00
|
|
|
dateID = nil;
|
|
|
|
day = nil;
|
|
|
|
year = nil;
|
|
|
|
label = nil;
|
2006-10-19 16:37:13 +02:00
|
|
|
isDisabled = NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2014-07-04 16:18:56 +02:00
|
|
|
- (void) dealloc
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
[self->dateID release];
|
|
|
|
[self->day release];
|
|
|
|
[self->month release];
|
|
|
|
[self->year release];
|
|
|
|
[self->label release];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Accessors */
|
|
|
|
|
2014-07-04 16:18:56 +02:00
|
|
|
- (void) setDateID: (NSString *) _dateID
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
ASSIGNCOPY(self->dateID, _dateID);
|
|
|
|
}
|
2014-07-04 16:18:56 +02:00
|
|
|
- (NSString *) dateID
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
return self->dateID;
|
|
|
|
}
|
|
|
|
|
2014-07-04 16:18:56 +02:00
|
|
|
- (void) setDay: (id) _day
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
ASSIGN(self->day, _day);
|
|
|
|
}
|
2014-07-04 16:18:56 +02:00
|
|
|
- (id) day
|
|
|
|
{
|
|
|
|
return self->day;
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
2014-07-04 16:18:56 +02:00
|
|
|
|
|
|
|
- (void) setMonth: (id) _month
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
ASSIGN(self->month, _month);
|
|
|
|
}
|
2014-07-04 16:18:56 +02:00
|
|
|
- (id) month
|
|
|
|
{
|
|
|
|
return self->month;
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
2014-07-04 16:18:56 +02:00
|
|
|
|
|
|
|
- (void) setYear: (id) _year
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
ASSIGN(self->year, _year);
|
|
|
|
}
|
2014-07-04 16:18:56 +02:00
|
|
|
- (id) year
|
|
|
|
{
|
|
|
|
return self->year;
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2014-07-04 16:18:56 +02:00
|
|
|
- (void) setLabel: (NSString *) _label
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
ASSIGNCOPY(self->label, _label);
|
|
|
|
}
|
2014-07-04 16:18:56 +02:00
|
|
|
- (NSString *) label
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
return self->label;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* formats */
|
|
|
|
|
2014-07-04 16:18:56 +02:00
|
|
|
- (BOOL) useISOFormats
|
|
|
|
{
|
|
|
|
NSNumber *useISOFormats;
|
2006-06-15 21:34:10 +02:00
|
|
|
WOContext *ctx;
|
|
|
|
|
2014-07-04 16:18:56 +02:00
|
|
|
ctx = [self context];
|
2006-06-15 21:34:10 +02:00
|
|
|
useISOFormats = [ctx valueForKey:@"useISOFormats"];
|
2014-07-04 16:18:56 +02:00
|
|
|
if (!useISOFormats)
|
|
|
|
{
|
|
|
|
NSArray *languages;
|
|
|
|
|
|
|
|
languages = [ctx resourceLookupLanguages];
|
|
|
|
if (languages && [languages count] > 0)
|
|
|
|
{
|
|
|
|
if ([[languages objectAtIndex:0] isEqualToString:@"French"])
|
|
|
|
{
|
|
|
|
useISOFormats = [NSNumber numberWithBool:NO];
|
|
|
|
}
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
if (!useISOFormats)
|
2014-07-04 16:18:56 +02:00
|
|
|
useISOFormats = [NSNumber numberWithBool: YES];
|
|
|
|
|
|
|
|
[ctx takeValue: useISOFormats forKey:@"useISOFormats"];
|
|
|
|
}
|
|
|
|
|
2006-06-15 21:34:10 +02:00
|
|
|
return [useISOFormats boolValue];
|
|
|
|
}
|
2006-10-26 18:35:51 +02:00
|
|
|
|
|
|
|
- (NSString *) formattedDateString
|
|
|
|
{
|
2009-08-20 19:50:37 +02:00
|
|
|
char buf[22];
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2014-07-04 16:18:56 +02:00
|
|
|
if ([self useISOFormats])
|
|
|
|
{
|
|
|
|
sprintf(buf, "%04d-%02d-%02d",
|
|
|
|
[[self year] intValue],
|
|
|
|
[[self month] intValue],
|
|
|
|
[[self day] intValue]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sprintf(buf, "%02d/%02d/%04d",
|
|
|
|
[[self day] intValue],
|
|
|
|
[[self month] intValue],
|
|
|
|
[[self year] intValue]);
|
2009-08-20 19:50:37 +02:00
|
|
|
}
|
|
|
|
return [NSString stringWithCString:buf];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2006-08-19 00:46:45 +02:00
|
|
|
- (NSString *) dateFormat
|
|
|
|
{
|
2009-08-20 19:50:37 +02:00
|
|
|
return [self useISOFormats] ? @"%Y-%m-%d" : @"%d/%m/%Y";
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2006-08-19 00:46:45 +02:00
|
|
|
- (NSString *) jsDateFormat
|
|
|
|
{
|
2009-08-20 19:50:37 +02:00
|
|
|
return [self useISOFormats] ? @"yyyy-mm-dd" : @"dd/mm/yyyy";
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2006-10-12 18:45:32 +02:00
|
|
|
- (void) takeValuesFromRequest: (WORequest *) _rq
|
2014-07-04 16:18:56 +02:00
|
|
|
inContext: (WOContext *) _ctx
|
2006-10-12 18:45:32 +02:00
|
|
|
{
|
2010-01-06 20:21:15 +01:00
|
|
|
NSInteger dateTZOffset, userTZOffset;
|
2011-02-14 19:02:39 +01:00
|
|
|
NSTimeZone *systemTZ, *userTZ;
|
2010-01-06 20:21:15 +01:00
|
|
|
SOGoUserDefaults *ud;
|
2014-07-04 16:18:56 +02:00
|
|
|
NSString *dateString;
|
|
|
|
NSCalendarDate *d;
|
2006-06-15 21:34:10 +02:00
|
|
|
|
|
|
|
dateString = [_rq formValueForKey:[self dateID]];
|
2014-07-04 16:18:56 +02:00
|
|
|
|
|
|
|
if (dateString == nil)
|
|
|
|
{
|
|
|
|
[self debugWithFormat:@"got no date string!"];
|
|
|
|
return;
|
|
|
|
}
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2014-07-04 16:18:56 +02:00
|
|
|
d = [NSCalendarDate dateWithString: dateString
|
|
|
|
calendarFormat: [self dateFormat]];
|
|
|
|
if (!d)
|
|
|
|
{
|
|
|
|
[self warnWithFormat: @"Could not parse dateString: '%@'",
|
2006-06-15 21:34:10 +02:00
|
|
|
dateString];
|
2014-07-04 16:18:56 +02:00
|
|
|
}
|
2006-10-19 16:37:13 +02:00
|
|
|
|
2010-01-06 20:21:15 +01:00
|
|
|
/* we must adjust the date timezone because "dateWithString:..." uses the
|
|
|
|
system timezone, which can be different from the user's. */
|
|
|
|
ud = [[_ctx activeUser] userDefaults];
|
2011-02-14 19:02:39 +01:00
|
|
|
systemTZ = [d timeZone];
|
|
|
|
dateTZOffset = [systemTZ secondsFromGMTForDate: d];
|
2010-01-06 20:21:15 +01:00
|
|
|
userTZ = [ud timeZone];
|
2011-02-14 19:02:39 +01:00
|
|
|
userTZOffset = [userTZ secondsFromGMTForDate: d];
|
2010-01-06 20:21:15 +01:00
|
|
|
if (dateTZOffset != userTZOffset)
|
|
|
|
d = [d dateByAddingYears: 0 months: 0 days: 0
|
|
|
|
hours: 0 minutes: 0
|
|
|
|
seconds: (dateTZOffset - userTZOffset)];
|
|
|
|
[d setTimeZone: userTZ];
|
|
|
|
|
2014-07-04 16:18:56 +02:00
|
|
|
[self setDay: [NSNumber numberWithInt:[d dayOfMonth]]];
|
|
|
|
[self setMonth: [NSNumber numberWithInt:[d monthOfYear]]];
|
2006-06-15 21:34:10 +02:00
|
|
|
[self setYear: [NSNumber numberWithInt:[d yearOfCommonEra]]];
|
|
|
|
|
2014-07-04 16:18:56 +02:00
|
|
|
[super takeValuesFromRequest: _rq inContext: _ctx];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2006-10-19 16:37:13 +02:00
|
|
|
- (void) setDisabled: (BOOL) disabled
|
|
|
|
{
|
|
|
|
isDisabled = disabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) disabled
|
|
|
|
{
|
|
|
|
return isDisabled;
|
|
|
|
}
|
|
|
|
|
2006-06-15 21:34:10 +02:00
|
|
|
@end /* UIxDatePicker */
|