@interface UIxMailToSelection : UIxComponent
{
- NSArray *to;
- NSArray *cc;
- NSArray *bcc;
+ NSArray *to;
+ NSArray *cc;
+ NSArray *bcc;
+ id item;
+ id address;
+ NSArray *addressList;
+ int currentIndex;
}
- (void)setTo:(NSArray *)_to;
- (void)setBcc:(NSArray *)_bcc;
- (NSArray *)bcc;
+- (NSArray *)properlySplitAddresses:(NSArray *)_addresses;
+
- (NSArray *)getAddressesOfType:(NSString *)_type
fromFormValues:(NSArray *)_values;
@end
#include "common.h"
+#include <NGMail/NGMail.h>
@implementation UIxMailToSelection
+static NSArray *headers = nil;
+
++ (void)initialize {
+ static BOOL didInit = NO;
+ if(didInit)
+ return;
+
+ didInit = YES;
+ headers = [[NSArray alloc] initWithObjects:@"to", @"cc", @"bcc", nil];
+}
+
+- (id)init {
+ self = [super init];
+ if(self) {
+ self->currentIndex = 0;
+ }
+ return self;
+}
+
- (void)dealloc {
- [self->to release];
- [self->cc release];
- [self->bcc release];
- [super dealloc];
+ [self->to release];
+ [self->cc release];
+ [self->bcc release];
+
+ [self->item release];
+ [self->address release];
+ [self->addressList release];
+ [super dealloc];
}
/* notifications */
+- (void)awake {
+ [super awake];
+ NSLog(@"%s", __PRETTY_FUNCTION__);
+}
+
- (void)sleep {
- [super sleep];
+ [super sleep];
}
/* accessors */
- (void)setTo:(NSArray *)_to {
- ASSIGN(self->to, _to);
+ _to = [self properlySplitAddresses:_to];
+ ASSIGN(self->to, _to);
}
- (NSArray *)to {
- return self->to;
+ return self->to;
}
- (void)setCc:(NSArray *)_cc {
- ASSIGN(self->cc, _cc);
+ _cc = [self properlySplitAddresses:_cc];
+ ASSIGN(self->cc, _cc);
}
- (NSArray *)cc {
- return self->cc;
+ return self->cc;
}
- (void)setBcc:(NSArray *)_bcc {
- ASSIGN(self->bcc, _bcc);
+ _bcc = [self properlySplitAddresses:_bcc];
+ ASSIGN(self->bcc, _bcc);
}
- (NSArray *)bcc {
- return self->bcc;
+ return self->bcc;
+}
+
+- (void)setAddressList:(NSArray *)_addressList {
+ ASSIGN(self->addressList, _addressList);
+}
+- (NSArray *)addressList {
+ return self->addressList;
+}
+
+- (void)setAddress:(id)_address {
+ ASSIGN(self->address, _address);
+}
+- (id)address {
+ return self->address;
+}
+
+- (void)setItem:(id)_item {
+ ASSIGN(self->item, _item);
+}
+- (id)item {
+ return self->item;
+}
+
+- (NSArray *)addressLists {
+ NSMutableArray *ma;
+
+ ma = [NSMutableArray arrayWithCapacity:3];
+ if(self->to)
+ [ma addObject:self->to];
+ if(self->cc)
+ [ma addObject:self->cc];
+ if(self->bcc)
+ [ma addObject:self->bcc];
+ return ma;
+}
+
+- (NSArray *)headers {
+ return headers;
+}
+
+- (NSString *)currentHeader {
+ if(self->addressList == self->to)
+ return @"to";
+ else if(self->addressList == self->cc)
+ return @"cc";
+ return @"bcc";
+}
+
+/* identifiers */
+
+- (NSString *)currentRowId {
+ if(currentIndex == 0)
+ return @"addr_proto";
+ return [NSString stringWithFormat:@"row_%d", self->currentIndex];
+}
+
+- (NSString *)currentPopUpId {
+ return [NSString stringWithFormat:@"popup_%d", self->currentIndex];
+}
+
+- (NSString *)currentAddressId {
+ return [NSString stringWithFormat:@"addr_%d", self->currentIndex];
+}
+
+- (NSString *)nextId {
+ self->currentIndex++;
+ return @"";
+}
+
+/* address handling */
+
+- (NSArray *)properlySplitAddresses:(NSArray *)_addresses {
+ NSString *addrs;
+ NGMailAddressParser *parser;
+ NSArray *result;
+ NSMutableArray *ma;
+ unsigned i, count;
+
+ if(!_addresses || [_addresses count] == 0)
+ return nil;
+
+ /* create one huge string, then split it using the parser */
+ addrs = [_addresses componentsJoinedByString:@","];
+ parser = [NGMailAddressParser mailAddressParserWithString:addrs];
+ result = [parser parseAddressList];
+ if(result == nil) {
+ [self debugWithFormat:@"Couldn't parse given addresses:%@", _addresses];
+ return _addresses;
+ }
+
+ count = [result count];
+ ma = [NSMutableArray arrayWithCapacity:count];
+ for(i = 0; i < count; i++) {
+ NGMailAddress *addr;
+ NSMutableString *s;
+ BOOL hasName = NO;
+
+ s = [[NSMutableString alloc] init];
+ addr = [result objectAtIndex:i];
+ if([addr displayName]) {
+ [s appendString:[addr displayName]];
+ [s appendString:@" "];
+ hasName = YES;
+ }
+ if(hasName)
+ [s appendString:@"<"];
+ [s appendString:[addr address]];
+ if(hasName)
+ [s appendString:@">"];
+ [ma addObject:s];
+ }
+ return ma;
}
/* handling requests */
- (NSArray *)getAddressesOfType:(NSString *)_type
fromFormValues:(NSArray *)_values
{
- unsigned i, count;
- NSMutableArray *result;
+ unsigned i, count;
+ NSMutableArray *result;
+
+ count = [_values count];
+ result = [NSMutableArray arrayWithCapacity:count];
+ for (i = 0; i < count; i++) {
+ NSString *s;
- count = [_values count];
- result = [NSMutableArray arrayWithCapacity:count];
- for (i = 0; i < count; i++) {
- NSString *s;
-
- s = [_values objectAtIndex:i];
- if ([s length] == 0)
- continue;
- }
- return result;
+ s = [_values objectAtIndex:i];
+ if ([s length] == 0)
+ continue;
+ }
+ return result;
}
- (void)takeValuesFromRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
- /* OK, we have a special form value processor */
- NSArray *tmp;
-
- [self debugWithFormat:@"Note: will take values ..."];
-
- NSLog(@"formValues: %@", [_rq formValues]);
-
- tmp = [self getAddressesOfType:@"to"
- fromFormValues:[_rq formValuesForKey:@"to"]];
- [self setTo:tmp];
- tmp = [self getAddressesOfType:@"cc"
- fromFormValues:[_rq formValuesForKey:@"cc"]];
- [self setCc:tmp];
- tmp = [self getAddressesOfType:@"bcc"
- fromFormValues:[_rq formValuesForKey:@"bcc"]];
- [self setBcc:tmp];
+ /* OK, we have a special form value processor */
+ NSArray *tmp;
+
+ [self debugWithFormat:@"Note: will take values ..."];
+
+ NSLog(@"%s formValues: %@",
+ __PRETTY_FUNCTION__,
+ [_rq formValues]);
+
+ tmp = [self getAddressesOfType:@"to"
+ fromFormValues:[_rq formValuesForKey:@"to"]];
+ [self setTo:tmp];
+ tmp = [self getAddressesOfType:@"cc"
+ fromFormValues:[_rq formValuesForKey:@"cc"]];
+ [self setCc:tmp];
+ tmp = [self getAddressesOfType:@"bcc"
+ fromFormValues:[_rq formValuesForKey:@"bcc"]];
+ [self setBcc:tmp];
}
/* response generation */
- (NSString *)jsCode {
- static NSString *script = \
- @"var currentIndex = 0;\n"
- @"\n"
- @"function hasAddress(email) {\n"
- @" var e = document.getElementById(email);\n"
- @" if(e)\n"
- @" return true;\n"
- @" return false;\n"
- @"}\n"
- @"\n"
- @"function rememberAddress(email) {\n"
- @" var list, e;\n"
- @" \n"
- @" list = document.getElementById(\"addr_addresses\");\n"
- @" e = document.createElement(\"span\");\n"
- @" e.id = email;\n"
- @" list.appendChild(e);\n"
- @"}\n"
- @"\n"
- @"function addAddress(type, cn, dn, email, uid, sn) {\n"
- @" var shouldAddRow, s;\n"
- @" \n"
- @" shouldAddRow = true;\n"
- @" s = cn + \" <\" + email + \">\";\n"
- @"\n"
- @" if(this.hasAddress(email))\n"
- @" return;\n"
- @"\n"
- @" this.rememberAddress(email);\n"
- @"\n"
- @" var e = document.getElementById(\"addr_0\");\n"
- @" if(e.value == '') {\n"
- @" e.value = s;\n"
- @" shouldAddRow = false;\n"
- @" }\n"
- @" if(shouldAddRow) {\n"
- @" this.fancyAddRow(false, s);\n"
- @" }\n"
- @"}\n"
- @"\n"
- @"function fancyAddRow(shouldEdit, text) {\n"
- @" var table = document.getElementById(\"addr_table\").childNodes[1];\n"
- @" var lastChild = document.getElementById(\"addr_lastentry\");\n"
- @" var proto, row, select, input;\n"
- @"\n"
- @" currentIndex++;\n"
- @"\n"
- @" proto = document.getElementById(\"addr_proto\");\n"
- @" row = proto.cloneNode(true);\n"
- @" row.id = \"\";\n"
- @"\n"
- @" select = row.childNodes[1].childNodes[1];\n"
- @" select.name = \"popup_\" + currentIndex;\n"
- @" input = row.childNodes[3].childNodes[1];\n"
- @" input.name = \"addr_\" + currentIndex;\n"
- @" input.id = \"addr_\" + currentIndex;\n"
- @" input.value = text;\n"
- @"\n"
- @" table.insertBefore(row, lastChild);\n"
- @" if(shouldEdit) {\n"
- @" input.focus();\n"
- @" input.select();\n"
- @" }\n"
- @"}\n"
- @"\n";
- return script;
+ static NSString *script = \
+ @"var currentIndex = %d;\n"
+ @"\n"
+ @"function hasAddress(email) {\n"
+ @" var e = document.getElementById(email);\n"
+ @" if(e)\n"
+ @" return true;\n"
+ @" return false;\n"
+ @"}\n"
+ @"\n"
+ @"function rememberAddress(email) {\n"
+ @" var list, e;\n"
+ @" \n"
+ @" list = document.getElementById(\"addr_addresses\");\n"
+ @" e = document.createElement(\"span\");\n"
+ @" e.id = email;\n"
+ @" list.appendChild(e);\n"
+ @"}\n"
+ @"\n"
+ @"function addAddress(type, cn, dn, email, uid, sn) {\n"
+ @" var shouldAddRow, s;\n"
+ @" \n"
+ @" shouldAddRow = true;\n"
+ @" s = cn + \" <\" + email + \">\";\n"
+ @"\n"
+ @" if(this.hasAddress(email))\n"
+ @" return;\n"
+ @"\n"
+ @" this.rememberAddress(email);\n"
+ @"\n"
+ @" var e = document.getElementById(\"addr_0\");\n"
+ @" if(e.value == '') {\n"
+ @" e.value = s;\n"
+ @" shouldAddRow = false;\n"
+ @" }\n"
+ @" if(shouldAddRow) {\n"
+ @" this.fancyAddRow(false, s);\n"
+ @" }\n"
+ @"}\n"
+ @"\n"
+ @"function fancyAddRow(shouldEdit, text) {\n"
+ @" var table = document.getElementById(\"addr_table\").childNodes[1];\n"
+ @" var lastChild = document.getElementById(\"addr_lastentry\");\n"
+ @" var proto, row, select, input;\n"
+ @"\n"
+ @" currentIndex++;\n"
+ @"\n"
+ @" proto = document.getElementById(\"addr_proto\");\n"
+ @" row = proto.cloneNode(true);\n"
+ @" row.id = \"\";\n"
+ @"\n"
+ @" select = row.childNodes[1].childNodes[1];\n"
+ @" select.name = \"popup_\" + currentIndex;\n"
+ @" input = row.childNodes[3].childNodes[1];\n"
+ @" input.name = \"addr_\" + currentIndex;\n"
+ @" input.id = \"addr_\" + currentIndex;\n"
+ @" input.value = text;\n"
+ @"\n"
+ @" table.insertBefore(row, lastChild);\n"
+ @" if(shouldEdit) {\n"
+ @" input.focus();\n"
+ @" input.select();\n"
+ @" }\n"
+ @"}\n"
+ @"\n";
+ return [NSString stringWithFormat:
+ script,
+ self->currentIndex];
}
@end