William Bowling is sharing code with you
Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.
Don't show this againadium / Plugins / Purple Service / ESPurpleJabberAccountViewController.m
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 | /*
* 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 "ESPurpleJabberAccountViewController.h"
#import <Adium/AIAccount.h>
#import <Adium/AIContactControllerProtocol.h>
#import <Adium/AIService.h>
#import <Adium/AIContactList.h>
#import <SystemConfiguration/SystemConfiguration.h>
#include <tgmath.h>
#define SERVERFEEDRSSURL @"http://xmpp.org/services/services-full.xml"
@implementation ESPurpleJabberAccountViewController
- (NSString *)nibName{
return @"ESPurpleJabberAccountView";
}
- (void)awakeFromNib
{
[super awakeFromNib];
[checkBox_checkMail setEnabled:NO];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(contactListChanged:)
name:Contact_ListChanged
object:nil];
}
//Configure our controls
- (void)configureForAccount:(AIAccount *)inAccount
{
[super configureForAccount:inAccount];
//Connection security
[checkBox_forceOldSSL setState:[[account preferenceForKey:KEY_JABBER_FORCE_OLD_SSL group:GROUP_ACCOUNT_STATUS] boolValue]];
[checkBox_requireTLS setState:[[account preferenceForKey:KEY_JABBER_REQUIRE_TLS group:GROUP_ACCOUNT_STATUS] boolValue]];
[checkBox_checkCertificates setState:[account preferenceForKey:KEY_JABBER_VERIFY_CERTS group:GROUP_ACCOUNT_STATUS]?[[account preferenceForKey:KEY_JABBER_VERIFY_CERTS group:GROUP_ACCOUNT_STATUS] boolValue]:YES];
[checkBox_allowPlaintext setState:[[account preferenceForKey:KEY_JABBER_ALLOW_PLAINTEXT group:GROUP_ACCOUNT_STATUS] boolValue]];
//Resource
NSString *resource = [account preferenceForKey:KEY_JABBER_RESOURCE group:GROUP_ACCOUNT_STATUS];
if (!resource)
resource = [(NSString*)SCDynamicStoreCopyLocalHostName(NULL) autorelease];
if (!resource)
resource = @"";
[textField_resource setStringValue:resource];
//Connect server
NSString *connectServer = [account preferenceForKey:KEY_JABBER_CONNECT_SERVER group:GROUP_ACCOUNT_STATUS];
[textField_connectServer setStringValue:(connectServer ? connectServer : @"")];
// BOSH server
NSString *boshServer = [account preferenceForKey:KEY_JABBER_BOSH_SERVER group:GROUP_ACCOUNT_STATUS];
[textField_BOSHserver setStringValue:(boshServer ?: @"")];
//Priority
NSNumber *priority = [account preferenceForKey:KEY_JABBER_PRIORITY_AVAILABLE group:GROUP_ACCOUNT_STATUS];
[textField_priorityAvailable setStringValue:(priority ? [priority stringValue] : @"")];
priority = [account preferenceForKey:KEY_JABBER_PRIORITY_AWAY group:GROUP_ACCOUNT_STATUS];
[textField_priorityAway setStringValue:(priority ? [priority stringValue] : @"")];
//File transfer proxies
NSString *ftProxies = [account preferenceForKey:KEY_JABBER_FT_PROXIES group:GROUP_ACCOUNT_STATUS];
[textField_ftProxies setStringValue:ftProxies ?: @""];
//Subscription behavior
NSInteger subbeh = [[account preferenceForKey:KEY_JABBER_SUBSCRIPTION_BEHAVIOR group:GROUP_ACCOUNT_STATUS] integerValue];
[popup_subscriptionBehavior selectItemWithTag:subbeh];
NSString *defaultGroup = [account preferenceForKey:KEY_JABBER_SUBSCRIPTION_GROUP group:GROUP_ACCOUNT_STATUS];
[comboBox_subscriptionGroup setStringValue:(defaultGroup ? defaultGroup : @"")];
//Hide the register button if the account can't register new accounts
[button_register setHidden:![account.service canRegisterNewAccounts]];
//Set hidden flag of the default group combobox
[self subscriptionModeDidChange:nil];
}
//Save controls
- (void)saveConfiguration
{
[super saveConfiguration];
//Connection security
[account setPreference:[NSNumber numberWithBool:[checkBox_forceOldSSL state]]
forKey:KEY_JABBER_FORCE_OLD_SSL group:GROUP_ACCOUNT_STATUS];
[account setPreference:[NSNumber numberWithBool:[checkBox_requireTLS state]]
forKey:KEY_JABBER_REQUIRE_TLS group:GROUP_ACCOUNT_STATUS];
[account setPreference:[NSNumber numberWithBool:[checkBox_checkCertificates state]]
forKey:KEY_JABBER_VERIFY_CERTS group:GROUP_ACCOUNT_STATUS];
[account setPreference:[NSNumber numberWithBool:[checkBox_allowPlaintext state]]
forKey:KEY_JABBER_ALLOW_PLAINTEXT group:GROUP_ACCOUNT_STATUS];
//Resource
[account setPreference:([[textField_resource stringValue] length] ? [textField_resource stringValue] : nil)
forKey:KEY_JABBER_RESOURCE group:GROUP_ACCOUNT_STATUS];
//Connect server
[account setPreference:([[textField_connectServer stringValue] length] ? [textField_connectServer stringValue] : nil)
forKey:KEY_JABBER_CONNECT_SERVER group:GROUP_ACCOUNT_STATUS];
//BOSH server
[account setPreference:([[textField_BOSHserver stringValue] length] ? [textField_BOSHserver stringValue] : nil)
forKey:KEY_JABBER_BOSH_SERVER group:GROUP_ACCOUNT_STATUS];
//FT proxies
[account setPreference:[textField_ftProxies stringValue]
forKey:KEY_JABBER_FT_PROXIES group:GROUP_ACCOUNT_STATUS];
//Priority
[account setPreference:([textField_priorityAvailable integerValue] ? [NSNumber numberWithInteger:[textField_priorityAvailable integerValue]] : nil)
forKey:KEY_JABBER_PRIORITY_AVAILABLE
group:GROUP_ACCOUNT_STATUS];
[account setPreference:([textField_priorityAway integerValue] ? [NSNumber numberWithInteger:[textField_priorityAway integerValue]] : nil)
forKey:KEY_JABBER_PRIORITY_AWAY
group:GROUP_ACCOUNT_STATUS];
//Subscription Behavior
[account setPreference:([[popup_subscriptionBehavior selectedItem] tag] ? [NSNumber numberWithInteger:[[popup_subscriptionBehavior selectedItem] tag]] : nil)
forKey:KEY_JABBER_SUBSCRIPTION_BEHAVIOR
group:GROUP_ACCOUNT_STATUS];
[account setPreference:([[comboBox_subscriptionGroup stringValue] length] ? [comboBox_subscriptionGroup stringValue] : nil)
forKey:KEY_JABBER_SUBSCRIPTION_GROUP group:GROUP_ACCOUNT_STATUS];
}
- (IBAction)subscriptionModeDidChange:(id)sender {
// only show these two when "accept and add to contact list" is selected
NSInteger tag = [[popup_subscriptionBehavior selectedItem] tag];
[textField_subscriptionModeLabel setHidden:tag != 2];
[comboBox_subscriptionGroup setHidden:tag != 2];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[window_registerServer release];
[servers release];
[super dealloc];
}
#pragma mark group combobox datasource
- (void)contactListChanged:(NSNotification*)n {
[comboBox_subscriptionGroup reloadData];
}
- (NSInteger)numberOfItemsInComboBox:(NSComboBox *)aComboBox {
return [adium.contactController.contactList countOfContainedObjects];
}
- (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(NSInteger)index {
return [[adium.contactController.contactList.containedObjects objectAtIndex:index] formattedUID];
}
- (NSUInteger)comboBox:(NSComboBox *)aComboBox indexOfItemWithStringValue:(NSString *)string {
NSArray *groups = adium.contactController.contactList.containedObjects;
NSUInteger i;
for(i = 0;i < [groups count];++i) {
AIListGroup *group = [groups objectAtIndex:i];
if([group.formattedUID isEqualToString:string])
return i;
}
return NSNotFound;
}
- (NSString *)comboBox:(NSComboBox *)aComboBox completedString:(NSString *)string {
for(AIListObject *obj in adium.contactController.contactList) {
if([obj isKindOfClass:[AIListGroup class]] && [obj.formattedUID hasPrefix:string])
return obj.formattedUID;
}
return string;
}
#pragma mark account creation
static NSComparisonResult compareByDistance(id one, id two, void*context) {
NSNumber *dist1obj = [one objectForKey:@"distance"];
NSNumber *dist2obj = [two objectForKey:@"distance"];
if((id)dist2obj == [NSNull null]) {
if((id)dist1obj == [NSNull null])
return NSOrderedSame;
return NSOrderedAscending;
}
if((id)dist1obj == [NSNull null])
return NSOrderedDescending;
CGFloat dist1 = [dist1obj doubleValue];
CGFloat dist2 = [dist2obj doubleValue];
if(fabs(dist1 - dist2) < 0.000001)
return NSOrderedSame;
if(dist1 > dist2)
return NSOrderedDescending;
return NSOrderedAscending;
}
- (IBAction)registerNewAccount:(id)sender {
if(!servers) {
NSError *err = NULL;
NSXMLDocument *serverfeed = [[[NSXMLDocument alloc] initWithContentsOfURL:[NSURL URLWithString:SERVERFEEDRSSURL]
options:0
error:&err] autorelease];
if(err) {
[[NSAlert alertWithError:err] runModal];
} else {
NSXMLElement *root = [serverfeed rootElement];
NSArray *items = [root elementsForName:@"item"];
if(!root || !items || ![[root name] isEqualToString:@"query"]) {
[[NSAlert alertWithMessageText:AILocalizedString(@"Parse Error.",nil)
defaultButton:AILocalizedString(@"OK",nil)
alternateButton:nil
otherButton:nil
informativeTextWithFormat:[NSString stringWithFormat:
AILocalizedString(@"Unable to parse the server list at %@. Please try again later.",nil), SERVERFEEDRSSURL]] runModal];
} else {
MachineLocation loc;
ReadLocation(&loc);
CGFloat latitude = FractToFloat(loc.latitude)*(M_PI/2.0f);
CGFloat longitude = FractToFloat(loc.longitude)*(M_PI/2.0f);
servers = [[NSMutableArray alloc] init];
for (NSXMLElement *item in items) {
NSXMLElement *title = [[item elementsForName:@"domain"] lastObject];
if(!title)
continue;
NSXMLElement *description = [[item elementsForName:@"description"] lastObject];
NSXMLElement *latitudeNode = [[item elementsForName:@"latitude"] lastObject];
NSXMLElement *longitudeNode = [[item elementsForName:@"longitude"] lastObject];
NSString *domain = [[item attributeForName:@"jid"] stringValue];
NSString *homepageStr = [[[item elementsForName:@"homepage"] lastObject] stringValue];
NSURL *homepage = homepageStr?[NSURL URLWithString:homepageStr]:nil;
id distance = [NSNull null];
if (latitudeNode && longitudeNode) {
/* Calculate the distance between the computer and the xmpp server in km
* Note that this assumes that the earth is a perfect sphere
* If it turns out to be flat or doughnut-shaped, this will not work!
*/
CGFloat latitude2 = [[latitudeNode stringValue] doubleValue] * (M_PI/180.0f);
CGFloat longitude2 = [[longitudeNode stringValue] doubleValue] * (M_PI/180.0f);
CGFloat d_lat = sinf((latitude2 - latitude)/2.0);
CGFloat d_long = sinf((longitude2 - longitude)/2.0);
CGFloat a = d_lat*d_lat + cosf(latitude)*cosf(latitude2)*d_long*d_long;
CGFloat c = 2*atan2f(sqrt(a),sqrt(1.0-a));
CGFloat d = 6372.797*c; // mean earth radius
distance = [NSNumber numberWithDouble:d];
}
[(NSMutableArray*)servers addObject:[NSDictionary dictionaryWithObjectsAndKeys:
[title stringValue], @"servername",
(description ? (id)[description stringValue] : (id)[NSNull null]), @"description",
distance, @"distance",
domain, @"domain",
homepage, @"homepage", // might be nil
nil]];
}
[(NSMutableArray*)servers sortUsingFunction:compareByDistance context:nil];
[tableview_servers reloadData];
}
}
}
[NSApp beginSheet:window_registerServer
modalForWindow:[sender window]
modalDelegate:self
didEndSelector:@selector(registrationSheetDidEnd:returnCode:contextInfo:)
contextInfo:NULL];
}
- (void)registrationSheetDidEnd:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
{
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return [servers count];
}
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
id objectValue = [[servers objectAtIndex:row] objectForKey:[tableColumn identifier]];
return ((objectValue && ![objectValue isKindOfClass:[NSNull class]]) ? objectValue : @"");
}
- (void)tableViewSelectionDidChange:(NSNotification *)notification {
NSDictionary *serverInfo = [servers objectAtIndex:[tableview_servers selectedRow]];
NSString *servername = [serverInfo objectForKey:@"domain"];
[textField_registerServerName setStringValue:servername];
[textField_registerServerPort setStringValue:@""];
[textView_serverDescription setString:[serverInfo objectForKey:@"description"]];
[button_serverHomepage setEnabled:[serverInfo objectForKey:@"homepage"] != nil];
}
- (IBAction)visitServerHomepage:(id)sender {
NSDictionary *serverInfo = [servers objectAtIndex:[tableview_servers selectedRow]];
[[NSWorkspace sharedWorkspace] openURL:[serverInfo objectForKey:@"homepage"]];
}
- (IBAction)registerCancel:(id)sender {
[window_registerServer orderOut:nil];
[NSApp endSheet:window_registerServer];
}
- (IBAction)registerRequestAccount:(id)sender {
[[sender window] makeFirstResponder:nil]; // apply all changes
if([[textField_registerServerName stringValue] length] == 0) {
NSBeep();
return;
}
[account setPreference:[NSNumber numberWithInteger:[textField_registerServerPort integerValue]]
forKey:KEY_CONNECT_PORT group:GROUP_ACCOUNT_STATUS];
NSString *newUID;
if ([[textField_accountUID stringValue] length]) {
NSRange atLocation = [[textField_accountUID stringValue] rangeOfString:@"@" options:NSLiteralSearch];
if (atLocation.location == NSNotFound)
newUID = [NSString stringWithFormat:@"%@@%@",[textField_accountUID stringValue], [textField_registerServerName stringValue]];
else
newUID = [NSString stringWithFormat:@"%@@%@",[[textField_accountUID stringValue] substringToIndex:atLocation.location], [textField_registerServerName stringValue]];
} else {
newUID = [NSString stringWithFormat:@"nobody@%@",[textField_registerServerName stringValue]];
}
[account filterAndSetUID:newUID];
[window_registerServer orderOut:nil];
[NSApp endSheet:window_registerServer];
[account performRegisterWithPassword:[textField_password stringValue]];
[self didBeginRegistration];
}
@end
|