sogo/UI/Contacts/UIxListEditor.m
Francis Lachapelle 544a6ca798 See ChangeLog
Monotone-Parent: dbc120865f7cb48891f7b017124443cc19de5f5c
Monotone-Revision: 98e797e237e41e316f67dab068ddf038cdcc0b9a

Monotone-Author: flachapelle@inverse.ca
Monotone-Date: 2010-01-22T20:12:21
Monotone-Branch: ca.inverse.sogo
2010-01-22 20:12:21 +00:00

258 lines
6.5 KiB
Objective-C

/* UIxListEditor.m - this file is part of SOGo
*
* Copyright (C) 2008-2009 Inverse inc.
*
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
*
* 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.
*/
#import <Foundation/NSDictionary.h>
#import <Foundation/NSArray.h>
#import <Foundation/NSString.h>
#import <NGObjWeb/WORequest.h>
#import <NGObjWeb/NSException+HTTP.h>
#import <NGObjWeb/WOResponse.h>
#import <Foundation/NSPropertyList.h>
#import <Contacts/SOGoContactGCSFolder.h>
#import <NGCards/NGVCardReference.h>
#import <NGCards/NGVList.h>
#import "UIxListEditor.h"
@implementation UIxListEditor
- (NSString *) listName
{
return [list fn];
}
- (void) setListName: (NSString *) newName
{
[list setFn: newName];
}
- (NSString *) nickname
{
return [list nickname];
}
- (void) setNickname: (NSString *) newName
{
[list setNickname: newName];
}
- (NSString *) description
{
return [list description];
}
- (void) setDescription: (NSString *) newDescription
{
[list setDescription: newDescription];
}
- (NSArray *) references
{
NSMutableArray *rc;
NSMutableDictionary *row;
id ref;
int i, count;
rc = [NSMutableArray array];
count = [[list cardReferences] count];
for (i = 0; i < count; i++)
{
ref = [[list cardReferences] objectAtIndex: i];
row = [NSMutableDictionary dictionary];
if ([[ref email] length] > 0)
[row setObject: [NSString stringWithFormat: @"%@ <%@>", [ref fn], [ref email]]
forKey: @"name"];
else
[row setObject: [ref fn]
forKey: @"name"];
[row setObject: [ref reference] forKey: @"id"];
[rc addObject: row];
}
return rc;
}
- (void) setReferencesValue: (NSString *) value
{
NSDictionary *values;
NSArray *references, *initialReferences;
NSString *currentReference;
int i, count;
NGVCardReference *cardReference;
SOGoContactGCSFolder *folder;
references = [value componentsSeparatedByString: @","];
if ([references count])
{
folder = [[self clientObject] container];
// Remove from the list the cards that were deleted
initialReferences = [list cardReferences];
count = [initialReferences count];
for (i = 0; i < count; i++)
{
cardReference = [initialReferences objectAtIndex: i];
if (![references containsObject: [cardReference reference]])
[list deleteCardReference: cardReference];
}
// Add new cards
count = [references count];
for (i = 0; i < count; i++)
{
currentReference = [references objectAtIndex: i];
if (![self cardReferences: initialReferences
contain: currentReference])
{
// Search contact by vCard UID
values = [folder lookupContactWithName: currentReference];
if (values)
{
cardReference = [NGVCardReference elementWithTag: @"card"];
[cardReference setFn: [values objectForKey: @"c_cn"]];
[cardReference setEmail: [values objectForKey: @"c_mail"]];
[cardReference setReference: currentReference];
[list addCardReference: cardReference];
}
}
}
}
}
- (BOOL) cardReferences: (NSArray *) references
contain: (NSString *) ref
{
int i, count;
BOOL rc = NO;
count = [references count];
for (i = 0; i < count; i++)
{
if ([ref isEqualToString: [[references objectAtIndex: i] reference]])
{
rc = YES;
break;
}
}
return rc;
}
- (NSString *) saveURL
{
return [NSString stringWithFormat: @"%@/saveAsList",
[[self clientObject] baseURL]];
}
- (BOOL) canCreateOrModify
{
return ([co isKindOfClass: [SOGoContentObject class]]
&& [super canCreateOrModify]);
}
- (id <WOActionResults>) defaultAction
{
co = [self clientObject];
list = [co vList];
if (list)
NSLog (@"Found list");
else
return [NSException exceptionWithHTTPStatus:404 /* Not Found */
reason: @"could not open list"];
return self;
}
- (void) takeValuesFromRequest: (WORequest *) _rq
inContext: (WOContext *) _ctx
{
co = [self clientObject];
list = [co vList];
[super takeValuesFromRequest: _rq inContext: _ctx];
}
- (BOOL) shouldTakeValuesFromRequest: (WORequest *) request
inContext: (WOContext*) context
{
NSString *actionName;
actionName = [[request requestHandlerPath] lastPathComponent];
return ([[self clientObject] isKindOfClass: [SOGoContactGCSList class]]
&& [actionName hasPrefix: @"save"]);
}
#warning Could this be part of a common parent with UIxAppointment/UIxTaskEditor/UIxListEditor ?
- (id) newAction
{
NSString *objectId, *method, *uri;
id <WOActionResults> result;
co = [self clientObject];
objectId = [co globallyUniqueObjectId];
if ([objectId length] > 0)
{
method = [NSString stringWithFormat:@"%@/%@.vlf/editAsList",
[co soURL], objectId];
uri = [self completeHrefForMethod: method];
result = [self redirectToLocation: uri];
}
else
result = [NSException exceptionWithHTTPStatus: 500 /* Internal Error */
reason: @"could not create a unique ID"];
return result;
}
- (id <WOActionResults>) saveAction
{
id result;
NSString *jsRefreshMethod;
if (co)
{
[co save];
if ([[[[self context] request] formValueForKey: @"nojs"] intValue])
result = [self redirectToLocation: [self modulePath]];
else
{
jsRefreshMethod
= [NSString stringWithFormat: @"refreshContacts(\"%@\")",
[co nameInContainer]];
result = [self jsCloseWithRefreshMethod: jsRefreshMethod];
}
}
else
result = [NSException exceptionWithHTTPStatus: 400 /* Bad Request */
reason: @"method cannot be invoked on "
@"the specified object"];
return result;
}
@end