/* Copyright (C) 2000-2003 Max Berger This file is part of libsyncml, written for the OpenGroupware project (OGo) OGo is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. OGo 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with OGo; see the file COPYING. If not, write to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ // $Id$ #import "ABSAppController.h" #import "URLPOSTRequestApple.h" #import #import "AddressBookSyncSessionHandler.h" #import @implementation ABSAppController - (void) _saveDefaults { [[NSUserDefaults standardUserDefaults]setObject:[connectTo stringValue] forKey:@"URI"]; [[NSUserDefaults standardUserDefaults]setObject:[usernameLine stringValue] forKey:@"Name"]; [[NSUserDefaults standardUserDefaults]synchronize]; } - (void)awakeFromNib { NSString *string; string = [[NSUserDefaults standardUserDefaults]objectForKey:@"URI"]; if (string) [connectTo setStringValue:string]; string = [[NSUserDefaults standardUserDefaults]objectForKey:@"Name"]; if (string) [usernameLine setStringValue:string]; } - (IBAction)quitButton:(id)sender { [self _saveDefaults]; [[NSApplication sharedApplication] terminate:self]; } - (IBAction)syncButton:(id)sender { NSData *recData = nil; NSData *answer = nil; URLPOSTRequest *req; NSURLResponse *resp = nil; NSError *err = nil; NSAutoreleasePool *subpool = nil; AddressBookSessionHandler* adbsessionhandler; SyncML *syncml; NSURL *url; BOOL firstTime = YES; NSAlert *alert; int syncType; [self _saveDefaults]; syncType = [[typeOfSync selectedCell]tag]; NSLog(@"SyncType %u",syncType); switch (syncType) { case SYNCML_SYNC_ALERT_TWO_WAY: case SYNCML_SYNC_ALERT_SLOW_SYNC: alert = [NSAlert alertWithMessageText:@"Experimental Feature" defaultButton:@"No" alternateButton:@"Yes" otherButton:nil informativeTextWithFormat:@"Warning: This is an experimental feature! Do NOT use it! Continue?"]; break; case SYNCML_SYNC_ALERT_REFRESH_FROM_SERVER: alert = [NSAlert alertWithMessageText:@"Overwrite Addressbook" defaultButton:@"No" alternateButton:@"Yes" otherButton:nil informativeTextWithFormat:@"Warning: This WILL delete all your current AddressBook entries! Continue?"]; break; } [alert setAlertStyle:NSCriticalAlertStyle]; if ([alert runModal]==NSAlertDefaultReturn) return; [progressIndicator setMaxValue:100.0]; [progressIndicator setMinValue:0.0]; [progressIndicator setDoubleValue:0.0]; [progressIndicator startAnimation:self]; [progressIndicator displayIfNeeded]; [errorMessage setStringValue:@"No Error"]; url = [NSURL URLWithString:[connectTo stringValue]]; req = nil; adbsessionhandler = [[AddressBookSessionHandler alloc]initWithUser:[usernameLine stringValue] pass:[passwordLine stringValue] syncType:syncType errorLine:errorMessage]; syncml = [[SyncML alloc]initWithSessionHandler:adbsessionhandler]; answer = [syncml createInitialSyncPackageTo:@"www.syncserver.org"]; [answer retain]; [progressIndicator setDoubleValue:10.0]; [progressIndicator displayIfNeeded]; while ( (err==nil) && ([answer length]>0) ) { subpool = [[NSAutoreleasePool alloc]init]; req = [[URLPOSTRequest alloc]initWithURL:url]; [req setPostData:answer]; [syncml release]; syncml = nil; [answer release]; answer = nil; recData = [NSURLConnection sendSynchronousRequest:req returningResponse:&resp error:&err]; [req release]; req = nil; [progressIndicator incrementBy:10.0]; [progressIndicator displayIfNeeded]; if ( [recData length]>0 ) { syncml = [[SyncML alloc]initWithSessionHandler:adbsessionhandler]; answer = [syncml processSyncMessage:recData messageForInvalid:NO]; [progressIndicator incrementBy:10.0]; [progressIndicator displayIfNeeded]; if (([answer length]==0)&&(firstTime)){ [errorMessage setStringValue:@"Invalid SyncML packet received! (check URL)"]; } } if (err!=nil) { [errorMessage setStringValue:[err description]]; } [answer retain]; [subpool release]; subpool = nil; firstTime = NO; } [syncml release]; syncml = nil; [statusMessage setStringValue:[NSString stringWithFormat:@"Received %u Contacts",[adbsessionhandler addCount]]]; [adbsessionhandler release]; adbsessionhandler = nil; [answer release]; answer = nil; [progressIndicator setDoubleValue:90.0]; [progressIndicator displayIfNeeded]; [[ABAddressBook sharedAddressBook]save]; [progressIndicator setDoubleValue:100.0]; [progressIndicator stopAnimation:self]; [progressIndicator displayIfNeeded]; } @end