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 / AMXMLConsoleController.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 | //
// AMXMLConsoleController.m
// Adium
//
// Created by Andreas Monitzer on 2007-06-06.
// Copyright 2007 Andreas Monitzer. All rights reserved.
//
#import "AMXMLConsoleController.h"
#import <libpurple/jabber.h>
#import <AIUtilities/AIAutoScrollView.h>
#define XML_PREFIX @"<?xml version='1.0' encoding='UTF-8' ?>\n"
@interface AMXMLConsoleController ()
- (void)appendToLog:(NSAttributedString *)astr;
- (PurpleConnection *)gc;
@end;
static void
xmlnode_received_cb(PurpleConnection *gc, xmlnode **packet, gpointer this)
{
AMXMLConsoleController *self = (AMXMLConsoleController *)this;
if (!this || [self gc] != gc)
return;
char *str = xmlnode_to_formatted_str(*packet, NULL);
NSString *sstr = [NSString stringWithUTF8String:str];
if ([sstr hasPrefix:XML_PREFIX])
sstr = [sstr substringFromIndex:[XML_PREFIX length]];
NSAttributedString *astr = [[NSAttributedString alloc] initWithString:sstr
attributes:nil];
[self appendToLog:astr];
[astr release];
g_free(str);
}
static void
xmlnode_sent_cb(PurpleConnection *gc, char **packet, gpointer this)
{
AMXMLConsoleController *self = (AMXMLConsoleController *)this;
xmlnode *node;
if (!this || [self gc] != gc)
return;
node = ((*packet && strlen(*packet) && ((*packet)[0] == '<')) ?
xmlnode_from_str(*packet, -1) :
NULL);
if (!node)
return;
char *str = xmlnode_to_formatted_str(node, NULL);
NSString *sstr = [NSString stringWithUTF8String:str];
if ([sstr hasPrefix:XML_PREFIX])
sstr = [sstr substringFromIndex:[XML_PREFIX length]];
NSAttributedString *astr = [[NSAttributedString alloc] initWithString:sstr
attributes:[NSDictionary dictionaryWithObject:[NSColor blueColor] forKey:NSForegroundColorAttributeName]];
[self appendToLog:astr];
[astr release];
g_free(str);
xmlnode_free(node);
}
@implementation AMXMLConsoleController
- (void)dealloc {
purple_signals_disconnect_by_handle(self);
[super dealloc];
}
- (IBAction)sendXML:(id)sender {
NSData *rawXMLData = [[xmlInjectView string] dataUsingEncoding:NSUTF8StringEncoding];
jabber_prpl_send_raw(gc, [rawXMLData bytes], [rawXMLData length]);
// remove from text field
[xmlInjectView setString:@""];
}
- (IBAction)clearLog:(id)sender {
[xmlLogView setString:@""];
}
- (IBAction)showWindow:(id)sender {
if (!xmlConsoleWindow) {
//Load the window if it's not already loaded
[NSBundle loadNibNamed:@"AMPurpleJabberXMLConsole" owner:self];
if (!xmlConsoleWindow) AILog(@"Unable to load AMPurpleJabberXMLConsole!");
//Connect to the signals for updating the window
PurplePlugin *jabber = purple_find_prpl("prpl-jabber");
if (!jabber) AILog(@"Unable to locate jabber prpl");
purple_signal_connect(jabber, "jabber-receiving-xmlnode", self,
PURPLE_CALLBACK(xmlnode_received_cb), self);
purple_signal_connect(jabber, "jabber-sending-text", self,
PURPLE_CALLBACK(xmlnode_sent_cb), self);
}
[xmlConsoleWindow makeKeyAndOrderFront:sender];
[(AIAutoScrollView *)[xmlLogView enclosingScrollView] setAutoScrollToBottom:YES];
}
- (void)windowWillClose:(NSNotification *)notification
{
xmlConsoleWindow = nil;
//We don't need to watch the signals with the window closed
purple_signals_disconnect_by_handle(self);
}
- (void)close
{
[xmlConsoleWindow close];
}
- (void)appendToLog:(NSAttributedString*)astr {
[[xmlLogView textStorage] appendAttributedString:astr];
}
- (PurpleConnection*)gc {
return gc;
}
- (void)setPurpleConnection:(PurpleConnection *)inGc
{
gc = inGc;
}
@end
|