2008-03-07 23:09:33 +01:00
|
|
|
/* UIxListEditor.m - this file is part of SOGo
|
|
|
|
*
|
2009-10-17 19:54:14 +02:00
|
|
|
* Copyright (C) 2008-2009 Inverse inc.
|
2008-03-07 23:09:33 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2009-08-24 22:26:52 +02:00
|
|
|
#import <Foundation/NSDictionary.h>
|
|
|
|
#import <Foundation/NSArray.h>
|
|
|
|
#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 <Foundation/NSPropertyList.h>
|
2008-03-07 23:09:33 +01:00
|
|
|
|
|
|
|
#import <Contacts/SOGoContactGCSFolder.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
|
|
|
|
|
|
|
#import "UIxListEditor.h"
|
|
|
|
|
|
|
|
@implementation UIxListEditor
|
|
|
|
|
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];
|
|
|
|
}
|
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];
|
|
|
|
}
|
|
|
|
- (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];
|
|
|
|
[row setObject: [NSString stringWithFormat: @"%@ <%@>", [ref fn], [ref email]]
|
|
|
|
forKey: @"name"];
|
|
|
|
[row setObject: [ref reference] forKey: @"id"];
|
|
|
|
[rc addObject: row];
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (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
|
|
|
{
|
2009-10-28 18:48:22 +01:00
|
|
|
folder = [[self clientObject] container];
|
|
|
|
|
|
|
|
// Remove from the list the cards that were deleted
|
2009-08-24 22:26:52 +02:00
|
|
|
initialReferences = [list cardReferences];
|
|
|
|
count = [initialReferences count];
|
2009-10-28 18:48:22 +01: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];
|
|
|
|
}
|
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];
|
2009-08-24 22:26:52 +02:00
|
|
|
if (![self cardReferences: initialReferences
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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",
|
|
|
|
[[self clientObject] baseURL]];
|
|
|
|
}
|
|
|
|
|
2009-08-24 22:26:52 +02:00
|
|
|
- (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"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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])
|
2009-11-10 22:39:40 +01: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
|