2008-03-07 23:09:33 +01:00
|
|
|
/* UIxListEditor.m - this file is part of SOGo
|
|
|
|
*
|
2012-06-22 01:32:18 +02:00
|
|
|
* Copyright (C) 2008-2012 Inverse inc.
|
2008-03-07 23:09:33 +01:00
|
|
|
*
|
|
|
|
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
|
2011-07-06 21:44:44 +02:00
|
|
|
* Francis Lachapelle <flachapelle@inverse.ca>
|
2008-03-07 23:09:33 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2009-08-24 22:26:52 +02:00
|
|
|
#import <Foundation/NSArray.h>
|
2010-08-25 17:23:51 +02:00
|
|
|
#import <Foundation/NSDictionary.h>
|
2009-08-24 22:26:52 +02:00
|
|
|
#import <Foundation/NSString.h>
|
|
|
|
|
2009-08-24 23:02:47 +02:00
|
|
|
#import <NGObjWeb/WORequest.h>
|
2008-03-07 23:09:33 +01:00
|
|
|
#import <NGObjWeb/NSException+HTTP.h>
|
|
|
|
#import <NGObjWeb/WOResponse.h>
|
|
|
|
|
2009-08-24 22:26:52 +02:00
|
|
|
#import <NGCards/NGVCardReference.h>
|
|
|
|
#import <NGCards/NGVList.h>
|
2008-03-07 23:09:33 +01:00
|
|
|
|
2011-07-06 21:44:44 +02:00
|
|
|
#import <Contacts/SOGoContactGCSEntry.h>
|
2010-08-25 17:23:51 +02:00
|
|
|
#import <Contacts/SOGoContactGCSFolder.h>
|
|
|
|
#import <Contacts/SOGoContactGCSList.h>
|
|
|
|
|
2008-03-07 23:09:33 +01:00
|
|
|
#import "UIxListEditor.h"
|
|
|
|
|
|
|
|
@implementation UIxListEditor
|
|
|
|
|
2010-08-25 17:23:51 +02:00
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
[list release];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id <WOActionResults>) defaultAction
|
|
|
|
{
|
|
|
|
co = [self clientObject];
|
|
|
|
list = [co vList];
|
|
|
|
[list retain];
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
2009-08-24 22:26:52 +02:00
|
|
|
|
2009-09-17 15:21:10 +02:00
|
|
|
- (NSString *) listName
|
2009-08-24 22:26:52 +02:00
|
|
|
{
|
|
|
|
return [list fn];
|
|
|
|
}
|
2010-08-25 17:23:51 +02:00
|
|
|
|
2009-09-17 15:21:10 +02:00
|
|
|
- (void) setListName: (NSString *) newName
|
2009-08-24 22:26:52 +02:00
|
|
|
{
|
|
|
|
[list setFn: newName];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) nickname
|
|
|
|
{
|
|
|
|
return [list nickname];
|
|
|
|
}
|
2010-08-25 17:23:51 +02:00
|
|
|
|
2009-08-24 22:26:52 +02:00
|
|
|
- (void) setNickname: (NSString *) newName
|
|
|
|
{
|
|
|
|
[list setNickname: newName];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) description
|
|
|
|
{
|
|
|
|
return [list description];
|
|
|
|
}
|
2010-08-25 17:23:51 +02:00
|
|
|
|
2009-08-24 22:26:52 +02:00
|
|
|
- (void) setDescription: (NSString *) newDescription
|
|
|
|
{
|
|
|
|
[list setDescription: newDescription];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *) references
|
|
|
|
{
|
2010-08-25 17:23:51 +02:00
|
|
|
NSArray *references;
|
|
|
|
NSMutableArray *referenceDicts;
|
2009-08-24 22:26:52 +02:00
|
|
|
NSMutableDictionary *row;
|
2010-08-25 17:23:51 +02:00
|
|
|
NGVCardReference *ref;
|
|
|
|
NSString *name, *email;
|
|
|
|
int count, max;
|
2009-08-24 22:26:52 +02:00
|
|
|
|
2010-08-25 17:23:51 +02:00
|
|
|
references = [list cardReferences];
|
|
|
|
max = [references count];
|
|
|
|
referenceDicts = [NSMutableArray arrayWithCapacity: max];
|
2009-08-24 22:26:52 +02:00
|
|
|
|
2010-08-25 17:23:51 +02:00
|
|
|
for (count = 0; count < max; count++)
|
2009-08-24 22:26:52 +02:00
|
|
|
{
|
2010-08-25 17:23:51 +02:00
|
|
|
ref = [references objectAtIndex: count];
|
|
|
|
row = [NSMutableDictionary new];
|
|
|
|
email = [ref email];
|
|
|
|
if ([email length] > 0)
|
|
|
|
name = [NSString stringWithFormat: @"%@ <%@>", [ref fn], email];
|
2010-01-22 21:12:21 +01:00
|
|
|
else
|
2010-08-25 17:23:51 +02:00
|
|
|
name = [ref fn];
|
|
|
|
[row setObject: name forKey: @"name"];
|
2009-08-24 22:26:52 +02:00
|
|
|
[row setObject: [ref reference] forKey: @"id"];
|
2010-08-25 17:23:51 +02:00
|
|
|
[referenceDicts addObject: row];
|
|
|
|
[row release];
|
2009-08-24 22:26:52 +02:00
|
|
|
}
|
|
|
|
|
2010-08-25 17:23:51 +02:00
|
|
|
return referenceDicts;
|
2009-08-24 22:26:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setReferencesValue: (NSString *) value
|
|
|
|
{
|
2009-10-28 18:48:22 +01:00
|
|
|
NSDictionary *values;
|
|
|
|
NSArray *references, *initialReferences;
|
|
|
|
NSString *currentReference;
|
2009-08-24 22:26:52 +02:00
|
|
|
int i, count;
|
|
|
|
NGVCardReference *cardReference;
|
2009-10-28 18:48:22 +01:00
|
|
|
SOGoContactGCSFolder *folder;
|
|
|
|
|
|
|
|
references = [value componentsSeparatedByString: @","];
|
|
|
|
if ([references count])
|
2009-08-24 22:26:52 +02:00
|
|
|
{
|
2010-08-25 17:23:51 +02:00
|
|
|
folder = [co container];
|
2009-10-28 18:48:22 +01:00
|
|
|
|
|
|
|
// Remove from the list the cards that were deleted
|
2009-08-24 22:26:52 +02:00
|
|
|
initialReferences = [list cardReferences];
|
|
|
|
count = [initialReferences count];
|
2010-08-25 17:23:51 +02:00
|
|
|
|
2009-08-24 22:26:52 +02:00
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
cardReference = [initialReferences objectAtIndex: i];
|
2009-10-28 18:48:22 +01:00
|
|
|
if (![references containsObject: [cardReference reference]])
|
2009-08-24 22:26:52 +02:00
|
|
|
[list deleteCardReference: cardReference];
|
|
|
|
}
|
2010-08-25 17:23:51 +02:00
|
|
|
|
2009-10-28 18:48:22 +01:00
|
|
|
// Add new cards
|
|
|
|
count = [references count];
|
|
|
|
|
2009-08-24 22:26:52 +02:00
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
2009-10-28 18:48:22 +01:00
|
|
|
currentReference = [references objectAtIndex: i];
|
2012-08-07 14:46:56 +02:00
|
|
|
if (![self cardReferences: [list cardReferences]
|
2009-08-24 22:26:52 +02:00
|
|
|
contain: currentReference])
|
|
|
|
{
|
2009-11-18 23:54:12 +01:00
|
|
|
// Search contact by vCard UID
|
2009-10-28 18:48:22 +01:00
|
|
|
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];
|
|
|
|
}
|
2011-07-06 21:44:44 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Not a valid UID; expect a string formatted as : "email|fullname"
|
|
|
|
NSString *workMail, *fn, *newUID;
|
|
|
|
NSArray *contactInfo;
|
|
|
|
NGVCard *newCard;
|
|
|
|
CardElement *newWorkMail;
|
|
|
|
SOGoContactGCSEntry *newContact;
|
|
|
|
|
|
|
|
contactInfo = [currentReference componentsSeparatedByString: @"|"];
|
|
|
|
if ([contactInfo count] > 1)
|
|
|
|
{
|
|
|
|
workMail = [contactInfo objectAtIndex: 0];
|
|
|
|
fn = [contactInfo objectAtIndex: 1];
|
|
|
|
|
|
|
|
// Create a new vCard
|
|
|
|
newUID = [NSString stringWithFormat: @"%@.vcf", [co globallyUniqueObjectId]];
|
|
|
|
newCard = [NGVCard cardWithUid: newUID];
|
|
|
|
newWorkMail = [CardElement new];
|
|
|
|
[newWorkMail autorelease];
|
|
|
|
[newWorkMail setTag: @"email"];
|
|
|
|
[newWorkMail addType: @"work"];
|
|
|
|
[newCard addChild: newWorkMail];
|
2011-11-14 17:07:04 +01:00
|
|
|
[newWorkMail setSingleValue: workMail forKey: @""];
|
2011-07-06 21:44:44 +02:00
|
|
|
[newCard setFn: fn];
|
|
|
|
|
|
|
|
// Add vCard to current folder
|
|
|
|
newContact = [SOGoContactGCSEntry objectWithName: newUID
|
|
|
|
inContainer: folder];
|
|
|
|
[newContact saveContentString: [newCard versitString]];
|
|
|
|
|
|
|
|
// Create card reference for the list
|
|
|
|
cardReference = [NGVCardReference elementWithTag: @"card"];
|
|
|
|
[cardReference setFn: fn];
|
|
|
|
[cardReference setEmail: workMail];
|
|
|
|
[cardReference setReference: newUID];
|
|
|
|
|
|
|
|
[list addCardReference: cardReference];
|
|
|
|
}
|
|
|
|
}
|
2009-10-28 18:48:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-08-24 22:26:52 +02:00
|
|
|
}
|
2010-08-25 17:23:51 +02:00
|
|
|
|
2009-08-24 22:26:52 +02:00
|
|
|
- (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;
|
|
|
|
}
|
|
|
|
|
2008-03-07 23:09:33 +01:00
|
|
|
- (NSString *) saveURL
|
|
|
|
{
|
|
|
|
return [NSString stringWithFormat: @"%@/saveAsList",
|
2010-08-25 17:23:51 +02:00
|
|
|
[co baseURLInContext: context]];
|
2008-03-07 23:09:33 +01:00
|
|
|
}
|
|
|
|
|
2009-08-24 22:26:52 +02:00
|
|
|
- (BOOL) canCreateOrModify
|
|
|
|
{
|
|
|
|
return ([co isKindOfClass: [SOGoContentObject class]]
|
|
|
|
&& [super canCreateOrModify]);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) shouldTakeValuesFromRequest: (WORequest *) request
|
|
|
|
inContext: (WOContext*) context
|
|
|
|
{
|
|
|
|
NSString *actionName;
|
2010-08-25 17:23:51 +02:00
|
|
|
BOOL rc;
|
2009-08-24 22:26:52 +02:00
|
|
|
|
2010-08-25 17:23:51 +02:00
|
|
|
co = [self clientObject];
|
2009-08-24 22:26:52 +02:00
|
|
|
actionName = [[request requestHandlerPath] lastPathComponent];
|
2010-08-25 17:23:51 +02:00
|
|
|
if ([co isKindOfClass: [SOGoContactGCSList class]]
|
|
|
|
&& [actionName hasPrefix: @"save"])
|
|
|
|
{
|
|
|
|
list = [co vList];
|
|
|
|
[list retain];
|
|
|
|
rc = YES;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
rc = NO;
|
2009-08-24 22:26:52 +02:00
|
|
|
|
2010-08-25 17:23:51 +02:00
|
|
|
return rc;
|
2009-08-24 22:26:52 +02:00
|
|
|
}
|
|
|
|
|
2008-03-07 23:09:33 +01:00
|
|
|
#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)
|
|
|
|
{
|
2009-08-24 22:26:52 +02:00
|
|
|
method = [NSString stringWithFormat:@"%@/%@.vlf/editAsList",
|
2008-03-07 23:09:33 +01:00
|
|
|
[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;
|
|
|
|
}
|
|
|
|
|
2009-08-24 22:26:52 +02:00
|
|
|
- (id <WOActionResults>) saveAction
|
|
|
|
{
|
|
|
|
id result;
|
|
|
|
NSString *jsRefreshMethod;
|
|
|
|
|
|
|
|
if (co)
|
|
|
|
{
|
|
|
|
[co save];
|
|
|
|
if ([[[[self context] request] formValueForKey: @"nojs"] intValue])
|
2010-08-25 17:23:51 +02:00
|
|
|
result = [self redirectToLocation: [self modulePath]];
|
2009-08-24 22:26:52 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-03-07 23:09:33 +01:00
|
|
|
@end
|