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 / AMPurpleJabberServiceDiscoveryBrowserController.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 364 365 366 367 368 369 | //
// AMPurpleJabberServiceDiscoveryBrowserController.m
// Adium
//
// Created by Evan Schoenberg on 9/5/07.
//
#import "AMPurpleJabberServiceDiscoveryBrowserController.h"
#import "AMPurpleJabberNode.h"
#import <libpurple/jabber.h>
#import <Adium/DCJoinChatWindowController.h>
#import "DCPurpleJabberJoinChatViewController.h"
@implementation AMPurpleJabberServiceDiscoveryBrowserController
extern void jabber_adhoc_execute(JabberStream *js, JabberAdHocCommands *cmd);
static NSImage *downloadprogress = nil;
static NSImage *det_triangle_opened = nil;
static NSImage *det_triangle_closed = nil;
- (id)initWithAccount:(AIAccount*)_account purpleConnection:(PurpleConnection *)_gc node:(AMPurpleJabberNode *)_node
{
if ((self = [super initWithWindowNibName:@"AMPurpleJabberDiscoveryBrowser"])) {
account = _account;
gc = _gc;
//Load the window immediately
[self window];
node = [_node retain];
[node addDelegate:self];
if (![node items])
[node fetchItems];
if (![node identities])
[node fetchInfo];
[[self window] makeKeyAndOrderFront:nil];
[self retain];
[outlineview setTarget:self];
[outlineview setDoubleAction:@selector(openService:)];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[node release];
[super dealloc];
}
- (NSString *)adiumFrameAutosaveName
{
return @"Jabber Service Discovery Browser";
}
- (void)windowDidLoad
{
[[self window] setTitle:AILocalizedString(@"Service Discovery Browser", "Window title for the service discovery browser")];
[label_service setLocalizedString:AILocalizedString(@"Service:", nil)];
[label_node setLocalizedString:AILocalizedString(@"Node:", nil)];
[[[outlineview tableColumnWithIdentifier:@"name"] headerCell] setStringValue:AILocalizedString(@"Name", "Name table column header for the service discovery browser")];
[[[outlineview tableColumnWithIdentifier:@"jid"] headerCell] setStringValue:AILocalizedString(@"JID", "JID (Jabber ID) table column header for the service discovery browser. This may not need to be localized.")];
[[[outlineview tableColumnWithIdentifier:@"category"] headerCell] setStringValue:AILocalizedString(@"Category", "Category table column header for the service discovery browser")];
[super windowDidLoad];
}
- (IBAction)openService:(id)sender
{
NSInteger row = [outlineview clickedRow];
if (row != -1) {
AMPurpleJabberNode *item = [outlineview itemAtRow:row];
NSArray *identities = [item identities];
if (!identities)
return;
NSDictionary *identity;
for (identity in identities) {
if ([[identity objectForKey:@"category"] isEqualToString:@"gateway"])
#warning This should not be called outside libpurple!
jabber_register_gateway((JabberStream*)gc->proto_data, [[item jid] UTF8String]);
else if ([[identity objectForKey:@"category"] isEqualToString:@"conference"]) {
DCJoinChatWindowController *jcwc = [DCJoinChatWindowController showJoinChatWindow];
[jcwc configureForAccount:account];
NSRange atsign = [[item jid] rangeOfString:@"@"];
if (atsign.location == NSNotFound)
[(DCPurpleJabberJoinChatViewController*)[jcwc joinChatViewController] setServer:[item jid]];
else {
[(DCPurpleJabberJoinChatViewController*)[jcwc joinChatViewController] setServer:[[item jid] substringFromIndex:atsign.location+1]];
[(DCPurpleJabberJoinChatViewController*)[jcwc joinChatViewController] setRoomName:[[item jid] substringToIndex:atsign.location]];
}
} else if ([[identity objectForKey:@"category"] isEqualToString:@"directory"]) {
#warning This should not be called outside libpurple!
jabber_user_search((JabberStream*)gc->proto_data, [[item jid] UTF8String]);
} else if ([[identity objectForKey:@"category"] isEqualToString:@"automation"] &&
[[identity objectForKey:@"type"] isEqualToString:@"command-node"]) {
JabberAdHocCommands cmd;
cmd.jid = (char*)[[item jid] UTF8String];
cmd.node = (char*)[[item node] UTF8String];
cmd.name = (char*)[[item name] UTF8String];
#warning This should not be called outside libpurple!
jabber_adhoc_execute(gc->proto_data, &cmd);
}
}
}
}
- (IBAction)performCommand:(id)sender {
AMPurpleJabberNode *commandnode = [sender representedObject];
JabberAdHocCommands cmd;
cmd.jid = (char*)[[commandnode jid] UTF8String];
cmd.node = (char*)[[commandnode node] UTF8String];
cmd.name = (char*)[[commandnode name] UTF8String];
#warning This should not be called outside libpurple!
jabber_adhoc_execute(gc->proto_data, &cmd);
}
- (NSMenu *)outlineView:(NSOutlineView *)outlineView menuForEvent:(NSEvent *)theEvent
{
NSMenu *menu = nil;
NSInteger row = [outlineView rowAtPoint:[outlineView convertPoint:[theEvent locationInWindow]
fromView:nil]];
if (row != -1) {
id item = [outlineView itemAtRow:row];
NSArray *commands = [(AMPurpleJabberNode*)item commands];
if (commands) {
menu = [[[NSMenu alloc] initWithTitle:@""] autorelease];
AMPurpleJabberNode *command;
for (command in commands) {
NSMenuItem *mitem = [[NSMenuItem alloc] initWithTitle:[command name]
action:@selector(performCommand:)
keyEquivalent:@""];
[mitem setTarget:self];
[mitem setRepresentedObject:command];
[menu addItem:mitem];
[mitem release];
}
}
}
return menu;
}
- (IBAction)changeServiceName:(id)sender {
[node release];
node = [[AMPurpleJabberNode alloc] initWithJID:[servicename stringValue] node:([[nodename stringValue] length]>0)?[nodename stringValue]:nil name:nil connection:gc];
[node addDelegate:self];
[node fetchInfo];
[outlineview reloadData];
}
- (void)windowWillClose:(NSNotification *)notification
{
[self release];
[super windowWillClose:notification];
}
- (void)jabberNodeGotItems:(AMPurpleJabberNode*)node {
[outlineview reloadData];
}
- (void)jabberNodeGotInfo:(AMPurpleJabberNode*)node {
[outlineview reloadData];
}
#pragma mark Outline View
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
{
if (!item)
return node;
return [[item items] objectAtIndex:index];
}
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
{
if (![item items]) {
// unknown
return YES;
}
return [[item items] count] > 0;
}
- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item
{
return [item identities] != NULL;
}
- (void)outlineViewSelectionDidChange:(NSNotification *)notification {
if ([outlineview selectedRow] != -1) {
AMPurpleJabberNode *selection = [outlineview itemAtRow:[outlineview selectedRow]];
if (![selection features])
[selection fetchInfo];
[servicename setStringValue:[selection jid]];
[nodename setStringValue:[selection node]?[selection node]:@""];
}
}
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
{
if (!item)
return 1;
return [[item items] count];
}
- (void)outlineViewItemWillExpand:(NSNotification *)notification
{
AMPurpleJabberNode *item = [[notification userInfo] objectForKey:@"NSObject"];
if (![item items])
[item fetchItems];
}
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
{
NSDictionary *style = [NSDictionary dictionaryWithObject:[item identities]?[NSColor blackColor]:[NSColor grayColor] forKey:NSForegroundColorAttributeName];
NSString *identifier = [tableColumn identifier];
if ([identifier isEqualToString:@"jid"])
return [[[NSAttributedString alloc] initWithString:[item jid] attributes:style] autorelease];
else if ([identifier isEqualToString:@"name"]) {
if ([item node]) {
if ([item name])
return [[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@ (%@)",[item name],[item node]] attributes:style] autorelease];
return [[[NSAttributedString alloc] initWithString:[item node] attributes:style] autorelease];
}
if ([item node])
return [[[NSAttributedString alloc] initWithString:[item name] attributes:style] autorelease];
// try to guess a name when there's none supplied
NSRange slashsign = [[item jid] rangeOfString:@"/"];
if (slashsign.location != NSNotFound)
return [[[NSAttributedString alloc] initWithString:[[item jid] substringFromIndex:slashsign.location+1] attributes:style] autorelease];
NSRange atsign = [[item jid] rangeOfString:@"@"];
if (atsign.location != NSNotFound)
return [[[NSAttributedString alloc] initWithString:[[item jid] substringToIndex:atsign.location] attributes:style] autorelease];
if ([[item identities] count] > 0) {
NSDictionary *identity = [[item identities] objectAtIndex:0];
id name = [identity objectForKey:@"name"];
if (name != [NSNull null] && [name length] > 0)
return [[[NSAttributedString alloc] initWithString:[identity objectForKey:@"name"] attributes:style] autorelease];
}
return [[[NSAttributedString alloc] initWithString:AILocalizedString(@"(unknown)",nil) attributes:style] autorelease];
} else if ([identifier isEqualToString:@"category"]) {
if (![item identities])
[[[NSAttributedString alloc] initWithString:AILocalizedString(@"Fetching...",nil) attributes:style] autorelease];
NSMutableArray *identities = [[NSMutableArray alloc] init];
NSEnumerator *e = [[item identities] objectEnumerator];
NSDictionary *identity;
while ((identity = [e nextObject]))
[identities addObject:[NSString stringWithFormat:@"%@ (%@)",[identity objectForKey:@"category"],[identity objectForKey:@"type"]]];
NSString *result = [identities componentsJoinedByString:@", "];
[identities release];
return [[[NSAttributedString alloc] initWithString:result attributes:style] autorelease];
} else
return @"???";
}
- (NSString *)outlineView:(NSOutlineView *)ov toolTipForCell:(NSCell *)cell rect:(NSRectPointer)rect tableColumn:(NSTableColumn *)tc item:(id)item mouseLocation:(NSPoint)mouseLocation {
NSArray *identities = [item identities];
if (!identities)
return nil;
NSMutableArray *result = [NSMutableArray array];
NSDictionary *identity;
for (identity in identities) {
if ([[identity objectForKey:@"category"] isEqualToString:@"gateway"])
[result addObject:[NSString stringWithFormat:AILocalizedString(@"%@; double-click to register.","XMPP service discovery browser gateway tooltip"),[identity objectForKey:@"name"]]];
else if ([[identity objectForKey:@"category"] isEqualToString:@"conference"])
[result addObject:AILocalizedString(@"Conference service; double-click to join",nil)];
else if ([[identity objectForKey:@"category"] isEqualToString:@"directory"])
[result addObject:AILocalizedString(@"Directory service; double-click to search",nil)];
else if ([[identity objectForKey:@"category"] isEqualToString:@"automation"] &&
[[identity objectForKey:@"type"] isEqualToString:@"command-node"])
[result addObject:AILocalizedString(@"Ad-Hoc command; double-click to execute",nil)];
}
if ([[item commands] count] > 0)
[result addObject:AILocalizedString(@"This node provides ad-hoc commands. Open the context menu to access them.",nil)];
if ([result count] == 0)
[result addObject:AILocalizedString(@"This node does not provide any services accessible to this program.",nil)];
return [result componentsJoinedByString:@"\n"];
}
- (void)outlineView:(NSOutlineView *)outlineView willDisplayOutlineCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item {
BOOL expanded = [outlineView isItemExpanded:item];
if (expanded && [item items] == nil) {
if (!downloadprogress)
downloadprogress = [[NSImage alloc] initWithContentsOfFile:[[NSBundle bundleForClass:[self class]] pathForResource:@"downloadprogress" ofType:@"png"]];
NSSize imgsize = [downloadprogress size];
NSImage *img = [[NSImage alloc] initWithSize:imgsize];
NSAffineTransform *transform = [NSAffineTransform transform];
[transform translateXBy:imgsize.width/2.0 yBy:imgsize.height/2.0];
NSTimeInterval intv = [NSDate timeIntervalSinceReferenceDate];
intv -= floor(intv); // only get the fractional part
[transform rotateByRadians:2.0*M_PI * (1.0-intv)];
[transform translateXBy:-imgsize.width/2.0 yBy:-imgsize.height/2.0];
[img lockFocus];
[transform set];
[downloadprogress drawInRect:NSMakeRect(0.0,0.0,imgsize.width,imgsize.height) fromRect:NSMakeRect(0.0,0.0,imgsize.width,imgsize.height)
operation:NSCompositeSourceOver fraction:1.0];
[[NSAffineTransform transform] set];
[img unlockFocus];
[cell setImage:img];
[img release];
NSInvocation *inv = [[NSInvocation invocationWithMethodSignature:[outlineView methodSignatureForSelector:@selector(setNeedsDisplayInRect:)]] retain];
[inv setSelector:@selector(setNeedsDisplayInRect:)];
NSRect rect = [outlineView rectOfRow:[outlineView rowForItem:item]];
[inv setArgument:&rect atIndex:2];
[inv performSelector:@selector(invokeWithTarget:) withObject:outlineView afterDelay:0.1];
} else {
if (expanded) {
if (!det_triangle_opened) {
det_triangle_opened = [[NSImage alloc] initWithSize:NSMakeSize(13.0,13.0)];
NSButtonCell *triangleCell = [[NSButtonCell alloc] initImageCell:nil];
[triangleCell setButtonType:NSOnOffButton];
[triangleCell setBezelStyle:NSDisclosureBezelStyle];
[triangleCell setState:NSOnState];
[det_triangle_opened lockFocus];
[triangleCell drawWithFrame:NSMakeRect(0.0,0.0,13.0,13.0) inView:outlineView];
[det_triangle_opened unlockFocus];
[triangleCell release];
}
[cell setImage:det_triangle_opened];
} else {
if (!det_triangle_closed) {
det_triangle_closed = [[NSImage alloc] initWithSize:NSMakeSize(13.0,13.0)];
NSButtonCell *triangleCell = [[NSButtonCell alloc] initImageCell:nil];
[triangleCell setButtonType:NSOnOffButton];
[triangleCell setBezelStyle:NSDisclosureBezelStyle];
[triangleCell setIntegerValue:NSOffState];
[det_triangle_closed lockFocus];
[triangleCell drawWithFrame:NSMakeRect(0.0,0.0,13.0,13.0) inView:outlineView];
[det_triangle_closed unlockFocus];
[triangleCell release];
}
[cell setImage:det_triangle_closed];
}
}
}
@end
|