* Adium is the legal property of its developers, whose names are listed in the copyright file included * with this source distribution. * This program 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 of the License, * or (at your option) any later version. * This program 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; if not, * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #import "AMPurpleJabberFormGenerator.h" @interface AMPurpleJabberFormField (protectedMethods) - (id)initWithXML:(xmlnode*)xml; @implementation AMPurpleJabberFormField /* factory method pattern */ + (AMPurpleJabberFormField*)fieldForXML:(xmlnode*)xml { const char *fieldtype = xmlnode_get_attrib(xml,"type"); if(!strcmp(fieldtype,"boolean")) class = [AMPurpleJabberFormFieldBoolean class]; else if(!strcmp(fieldtype,"fixed")) class = [AMPurpleJabberFormFieldFixed class]; else if(!strcmp(fieldtype,"hidden")) class = [AMPurpleJabberFormFieldHidden class]; else if(!strcmp(fieldtype,"jid-multi")) class = [AMPurpleJabberFormFieldJidMulti class]; else if(!strcmp(fieldtype,"jid-single")) class = [AMPurpleJabberFormFieldJidSingle class]; else if(!strcmp(fieldtype,"list-multi")) class = [AMPurpleJabberFormFieldListMulti class]; else if(!strcmp(fieldtype,"list-single")) class = [AMPurpleJabberFormFieldListSingle class]; else if(!strcmp(fieldtype,"text-multi")) class = [AMPurpleJabberFormFieldTextMulti class]; else if(!strcmp(fieldtype,"text-private")) class = [AMPurpleJabberFormFieldTextPrivate class]; else if(!strcmp(fieldtype,"text-single")) class = [AMPurpleJabberFormFieldTextSingle class]; return [[[class alloc] initWithXML:xml] autorelease]; - (id)initWithXML:(xmlnode*)xml { if((self = [self init])) { if(xmlnode_get_child(xml,"required")) const char *labelstr = xmlnode_get_attrib(xml,"label"); [self setLabel:[NSString stringWithUTF8String:labelstr]]; const char *varstr = xmlnode_get_attrib(xml,"var"); [self setVariable:[NSString stringWithUTF8String:varstr]]; xmlnode *descnode = xmlnode_get_child(xml,"desc"); const char *descstr = xmlnode_get_data(descnode); [self setDescription:[NSString stringWithUTF8String:descstr]]; - (void)setRequired:(BOOL)_required { - (void)setLabel:(NSString*)_label { - (void)setVariable:(NSString*)_var { - (void)setDescription:(NSString*)_desc { xmlnode *xml_result = xmlnode_new("field"); xmlnode_set_attrib(xml_result,"label",[label UTF8String]); xmlnode_set_attrib(xml_result,"var",[var UTF8String]); xmlnode_new_child(xml_result,"required"); xmlnode_insert_data(xmlnode_new_child(xml_result,"desc"), [desc UTF8String], -1); @implementation AMPurpleJabberFormFieldBoolean - (id)initWithXML:(xmlnode*)xml { if((self = [super initWithXML:xml])) { xmlnode *valuenode = xmlnode_get_child(xml,"value"); const char *v = xmlnode_get_data(valuenode); if(v && (!strcmp(v, "1") || !strcmp(v, "true"))) - (void)setBoolValue:(BOOL)_value { xmlnode *xml_result = [super xml]; xmlnode_set_attrib(xml_result,"type","boolean"); xmlnode_insert_data(xmlnode_new_child(xml_result,"value"),"1",-1); @implementation AMPurpleJabberFormFieldFixed - (id)initWithXML:(xmlnode*)xml { if((self = [super initWithXML:xml])) { xmlnode *valuenode = xmlnode_get_child(xml,"value"); const char *valstr = xmlnode_get_data(valuenode); [self setStringValue:[NSString stringWithUTF8String:valstr]]; - (void)setStringValue:(NSString*)_value { - (NSString*)stringValue { xmlnode *xml_result = [super xml]; xmlnode_set_attrib(xml_result,"type","fixed"); xmlnode_insert_data(xmlnode_new_child(xml_result,"value"),[value UTF8String],-1); @implementation AMPurpleJabberFormFieldHidden - (id)initWithXML:(xmlnode*)xml { if((self = [super initWithXML:xml])) { xmlnode *valuenode = xmlnode_get_child(xml,"value"); const char *valstr = xmlnode_get_data(valuenode); [self setStringValue:[NSString stringWithUTF8String:valstr]]; - (void)setStringValue:(NSString*)_value { - (NSString*)stringValue { xmlnode *xml_result = [super xml]; xmlnode_set_attrib(xml_result,"type","hidden"); xmlnode_insert_data(xmlnode_new_child(xml_result,"value"),[value UTF8String],-1); @implementation AMPurpleJabberFormFieldJidMulti - (id)initWithXML:(xmlnode*)xml { if((self = [super initWithXML:xml])) { NSMutableArray *values = [NSMutableArray array]; for(valuenode = xml->child; valuenode; valuenode = valuenode->next) { if(valuenode->type == XMLNODE_TYPE_TAG && !strcmp(valuenode->name, "value")) { const char *content = xmlnode_get_data(valuenode); [values addObject:[NSString stringWithUTF8String:content]]; - (void)setJIDs:(NSArray*)_jids { xmlnode *xml_result = [super xml]; xmlnode_set_attrib(xml_result,"type","jid-multi"); xmlnode_insert_data(xmlnode_new_child(xml_result,"value"),[jid UTF8String],-1); @implementation AMPurpleJabberFormFieldJidSingle - (id)initWithXML:(xmlnode*)xml { if((self = [super initWithXML:xml])) { xmlnode *value = xmlnode_get_child(xml,"value"); const char *valstr = xmlnode_get_data(value); [self setJID:[NSString stringWithUTF8String:valstr]]; - (void)setJID:(NSString*)_jid { xmlnode *xml_result = [super xml]; xmlnode_set_attrib(xml_result,"type","jid-single"); xmlnode_insert_data(xmlnode_new_child(xml_result,"value"),[jid UTF8String],-1); @implementation AMPurpleJabberFormFieldListMulti - (id)initWithXML:(xmlnode*)xml { if((self = [super initWithXML:xml])) { NSMutableArray *newvalues = [NSMutableArray array]; for(valuenode = xml->child; valuenode; valuenode = valuenode->next) { if(valuenode->type == XMLNODE_TYPE_TAG && !strcmp(valuenode->name, "value")) { const char *content = xmlnode_get_data(valuenode); [newvalues addObject:[NSString stringWithUTF8String:content]]; [self setStringValues:newvalues]; NSMutableArray *newoptions = [NSMutableArray array]; for(option = xml->child; option; option = option->next) { if(option->type == XMLNODE_TYPE_TAG && !strcmp(option->name, "option")) { const char *labelstr = xmlnode_get_attrib(option,"label"); xmlnode *lvaluenode = xmlnode_get_child(option,"value"); const char *valuestr = xmlnode_get_data(lvaluenode); [newoptions addObject:[NSDictionary dictionaryWithObjectsAndKeys: [NSString stringWithUTF8String:valuestr], @"value", labelstr?[NSString stringWithUTF8String:labelstr]:nil, @"label", [self setOptions:newoptions]; - (void)setOptions:(NSArray*)_options { options = [_options copy]; - (void)setStringValues:(NSArray*)_values { - (NSArray*)stringValues { xmlnode *xml_result = [super xml]; xmlnode_set_attrib(xml_result,"type","list-multi"); for(NSDictionary *option in options) { xmlnode *optnode = xmlnode_new_child(xml_result,"option"); xmlnode_set_attrib(optnode,"label",[[option objectForKey:@"label"] UTF8String]); xmlnode_insert_data(xmlnode_new_child(optnode,"value"),[[option objectForKey:@"value"] UTF8String],-1); xmlnode_insert_data(xmlnode_new_child(xml_result,"value"),[value UTF8String],-1); @implementation AMPurpleJabberFormFieldListSingle - (id)initWithXML:(xmlnode*)xml { if((self = [super initWithXML:xml])) { xmlnode *valuenode = xmlnode_get_child(xml,"value"); const char *valstr = xmlnode_get_data(valuenode); [self setStringValue:[NSString stringWithUTF8String:valstr]]; NSMutableArray *newoptions = [NSMutableArray array]; for(option = xml->child; option; option = option->next) { if(option->type == XMLNODE_TYPE_TAG && !strcmp(option->name, "option")) { const char *labelstr = xmlnode_get_attrib(option,"label"); xmlnode *lvaluenode = xmlnode_get_child(option,"value"); const char *valuestr = xmlnode_get_data(lvaluenode); [newoptions addObject:[NSDictionary dictionaryWithObjectsAndKeys: [NSString stringWithUTF8String:valuestr], @"value", labelstr?[NSString stringWithUTF8String:labelstr]:nil, @"label", [self setOptions:newoptions]; - (void)setStringValue:(NSString*)_value { - (NSString*)stringValue { - (void)setOptions:(NSArray*)_options { options = [_options copy]; xmlnode *xml_result = [super xml]; xmlnode_set_attrib(xml_result,"type","list-single"); xmlnode *optnode = xmlnode_new_child(xml_result,"option"); xmlnode_set_attrib(optnode,"label",[[option objectForKey:@"label"] UTF8String]); xmlnode_insert_data(xmlnode_new_child(optnode,"value"),[[option objectForKey:@"value"] UTF8String],-1); xmlnode_insert_data(xmlnode_new_child(xml_result,"value"),[value UTF8String],-1); @implementation AMPurpleJabberFormFieldTextMulti - (id)initWithXML:(xmlnode*)xml { if((self = [super initWithXML:xml])) { NSMutableArray *values = [NSMutableArray array]; for(valuenode = xml->child; valuenode; valuenode = valuenode->next) { if(valuenode->type == XMLNODE_TYPE_TAG && !strcmp(valuenode->name, "value")) { const char *content = xmlnode_get_data(valuenode); [values addObject:[NSString stringWithUTF8String:content]]; [self setStringValue:[values componentsJoinedByString:@"\n"]]; - (void)setStringValue:(NSString*)_value { - (NSString*)stringValue { xmlnode *xml_result = [super xml]; xmlnode_set_attrib(xml_result,"type","text-multi"); for (NSString *line in [value componentsSeparatedByString:@"\n"]) xmlnode_insert_data(xmlnode_new_child(xml_result,"value"),[line UTF8String],-1); @implementation AMPurpleJabberFormFieldTextSingle - (id)initWithXML:(xmlnode*)xml { if((self = [super initWithXML:xml])) { xmlnode *valuenode = xmlnode_get_child(xml,"value"); const char *valstr = xmlnode_get_data(valuenode); [self setStringValue:[NSString stringWithUTF8String:valstr]]; - (void)setStringValue:(NSString*)_value { - (NSString*)stringValue { xmlnode *xml_result = [super xml]; xmlnode_set_attrib(xml_result,"type","text-single"); xmlnode_insert_data(xmlnode_new_child(xml_result,"value"),[value UTF8String],-1); @implementation AMPurpleJabberFormFieldTextPrivate - (id)initWithXML:(xmlnode*)xml { if((self = [super initWithXML:xml])) { xmlnode *valuenode = xmlnode_get_child(xml,"value"); const char *valstr = xmlnode_get_data(valuenode); [self setStringValue:[NSString stringWithUTF8String:valstr]]; - (void)setStringValue:(NSString*)_value { - (NSString*)stringValue { xmlnode *xml_result = [super xml]; xmlnode_set_attrib(xml_result,"type","text-private"); xmlnode_insert_data(xmlnode_new_child(xml_result,"value"),[value UTF8String],-1); @implementation AMPurpleJabberFormGenerator - (id)initWithType:(enum AMPurpleJabberFormType)_type { if((self = [super init])) { fields = [[NSMutableArray alloc] init]; - (id)initWithXML:(xmlnode*)xml { if((self = [super init])) { // verify that this is really a jabber:x:data if(xml->type != XMLNODE_TYPE_TAG || strcmp(xml->name, "x")) { const char *xmlns = xmlnode_get_namespace(xml); if(!xmlns || strcmp(xmlns,"jabber:x:data")) { const char *typestr = xmlnode_get_attrib(xml,"type"); if(!strcmp(typestr, "form")) else if(!strcmp(typestr, "submit")) else if(!strcmp(typestr, "cancel")) else if(!strcmp(typestr, "result")) else { /* unknown form type */ xmlnode *titlenode = xmlnode_get_child(xml,"title"); const char *titlestr = xmlnode_get_data(titlenode); [self setTitle:[NSString stringWithUTF8String:titlestr]]; xmlnode *instructionsnode = xmlnode_get_child(xml,"instructions"); const char *instructionsstr = xmlnode_get_data(instructionsnode); [self setInstructions:[NSString stringWithUTF8String:instructionsstr]]; fields = [[NSMutableArray alloc] init]; for(field = xml->child; field; field = field->next) { if(field->type == XMLNODE_TYPE_TAG && !strcmp(field->name,"field")) { AMPurpleJabberFormField *fieldobj = [AMPurpleJabberFormField fieldForXML:field]; [self addField:fieldobj]; - (void)setTitle:(NSString*)_title { - (void)setInstructions:(NSString*)_instructions { instructions = [_instructions copy]; - (NSString*)instructions { - (enum AMPurpleJabberFormType)type { - (void)addField:(AMPurpleJabberFormField*)field { [fields addObject:field]; - (void)removeField:(AMPurpleJabberFormField*)field { [fields removeObject:field]; xmlnode *xml = xmlnode_new("x"); xmlnode_set_namespace(xml,"jabber:x:data"); xmlnode_set_attrib(xml,"type","form"); xmlnode_set_attrib(xml,"type","submit"); xmlnode_set_attrib(xml,"type","cancel"); xmlnode_set_attrib(xml,"type","result"); xmlnode_insert_data(xmlnode_new_child(xml,"title"),[title UTF8String],-1); xmlnode_insert_data(xmlnode_new_child(xml,"instructions"),[instructions UTF8String],-1); AMPurpleJabberFormField *field; xmlnode *fieldxml = [field xml]; xmlnode_insert_child(xml,fieldxml);