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 / AMPurpleSearchResultsController.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 | //
// AMPurpleSearchResultsController.m
// Adium
//
// Created by Andreas Monitzer on 2007-06-25.
// Copyright 2007 Andreas Monitzer. All rights reserved.
//
#import "AMPurpleSearchResultsController.h"
@implementation AMPurpleSearchResultsController
- (id)initWithPurpleConnection:(PurpleConnection*)_gc title:(NSString*)title primaryText:(NSString*)primary secondaryText:(NSString*)secondary searchResults:(PurpleNotifySearchResults*)results userData:(gpointer)_user_data {
if((self = [super initWithWindowNibName:@"AMPurpleSearchResultsWindow"])) {
[[self window] setTitle:title?title:AILocalizedString(@"Search Results",nil)];
[textfield_primary setStringValue:primary?primary:@""];
[textfield_secondary setStringValue:secondary?secondary:@""];
gc = _gc;
purpleresults = results;
// add the action buttons
CGFloat offset = [buttonview frame].size.width - 20.0f;
searchButtons = [[NSMutableDictionary alloc] init];
GList *but;
for(but = results->buttons; but; but = g_list_next(but)) {
PurpleNotifySearchButton *button = but->data;
NSString *title = nil;
switch(button->type) {
case PURPLE_NOTIFY_BUTTON_LABELED:
if(button->label)
title = [NSString stringWithUTF8String:button->label];
break;
case PURPLE_NOTIFY_BUTTON_CONTINUE:
title = AILocalizedString(@"Continue",nil);
break;
case PURPLE_NOTIFY_BUTTON_ADD:
title = AILocalizedString(@"Add",nil);
break;
case PURPLE_NOTIFY_BUTTON_INFO:
title = AILocalizedString(@"Info",nil);
break;
case PURPLE_NOTIFY_BUTTON_IM:
title = AILocalizedString(@"Send Message",nil);
break;
case PURPLE_NOTIFY_BUTTON_JOIN:
title = AILocalizedString(@"Join",nil);
break;
case PURPLE_NOTIFY_BUTTON_INVITE:
title = AILocalizedString(@"Invite",nil);
break;
}
NSButton *newbutton = [[NSButton alloc] initWithFrame:NSMakeRect(0.0f, 0.0f, 100.0f, 100.0f)];
[newbutton setTitle:title];
[[newbutton cell] setControlSize:NSRegularControlSize];
[newbutton setTarget:self];
[newbutton setAction:@selector(invokeAction:)];
[newbutton setBordered:YES];
[newbutton setBezelStyle:NSRoundedBezelStyle];
[newbutton setImagePosition:NSNoImage];
[newbutton setAlignment:NSCenterTextAlignment];
[[newbutton cell] setControlTint:NSDefaultControlTint];
[newbutton setButtonType:NSMomentaryPushInButton];
// determine ideal size
NSSize minsize = [[newbutton cell] cellSize];
offset -= minsize.width + 20.0f;
[buttonview addSubview:newbutton];
[newbutton setFrame:NSMakeRect(offset, 0.0f, minsize.width + 20.0f, minsize.height)];
[newbutton setAutoresizingMask:NSViewMinXMargin | NSViewMinYMargin];
[searchButtons setObject:[NSValue valueWithPointer:button] forKey:[NSValue valueWithNonretainedObject:newbutton]];
[newbutton release];
offset -= 20.0f;
}
// remove all columns (it's not possible to create a table without any columns in IB
while([tableview numberOfColumns] > 0)
[tableview removeTableColumn:[[tableview tableColumns] objectAtIndex:0]];
// add the ones we need
NSUInteger index = 0;
GList *column;
for(column = results->columns; column; column = g_list_next(column)) {
PurpleNotifySearchColumn *scol = column->data;
NSTableColumn *tcol = [[NSTableColumn alloc] initWithIdentifier:[NSNumber numberWithUnsignedInteger:index++]];
if(scol->title)
[[tcol headerCell] setStringValue:[NSString stringWithUTF8String:scol->title]];
[tableview addTableColumn:tcol];
[tcol release];
}
// convert the rows
searchResults = [[NSMutableArray alloc] init];
GList *row;
for(row = results->rows; row; row = g_list_next(row)) {
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
NSUInteger col = 0;
[searchResults addObject:dict];
GList *cell;
for(cell = row->data; cell; cell = g_list_next(cell)) {
const char *text = cell->data;
if(text)
[dict setObject:[NSString stringWithUTF8String:text] forKey:[NSNumber numberWithUnsignedInteger:col++]];
}
[dict release];
}
[tableview reloadData];
[tableview sizeToFit];
[self showWindow:nil];
[self tableViewSelectionDidChange:nil];
}
return [self retain]; // will be released in -purpleRequestClose when we're done
}
- (void)dealloc {
[searchButtons release];
[searchResults release];
[super dealloc];
}
- (void)addResults:(PurpleNotifySearchResults*)results {
GList *row;
for(row = results->rows; row; row = g_list_next(row)) {
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
NSUInteger col = 0;
[searchResults addObject:dict];
GList *cell;
for(cell = row->data; cell; cell = g_list_next(cell)) {
const char *text = cell->data;
if(text)
[dict setObject:[NSString stringWithUTF8String:text] forKey:[NSNumber numberWithUnsignedInteger:col++]];
}
[dict release];
}
[tableview reloadData];
[tableview sizeToFit];
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return [searchResults count];
}
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
return [[searchResults objectAtIndex:row] objectForKey:[tableColumn identifier]];
}
- (BOOL)tableView:(NSTableView *)tableView shouldEditTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
return NO;
}
- (void)tableViewSelectionDidChange:(NSNotification *)notification {
BOOL enabled = [tableview numberOfSelectedRows] > 0;
for (NSValue *buttonval in searchButtons) {
[(NSButton *)[buttonval nonretainedObjectValue] setEnabled:enabled];
}
}
- (BOOL)windowShouldClose:(id)sender {
purple_notify_close(PURPLE_NOTIFY_SEARCHRESULTS, self);
return windowIsClosing;
}
- (IBAction)invokeAction:(id)sender {
PurpleNotifySearchButton *button = [[searchButtons objectForKey:[NSValue valueWithNonretainedObject:sender]] pointerValue];
if(!button) {
NSBeep();
return;
}
NSInteger row = [tableview selectedRow];
GList *rowptr = NULL;
if(row != -1)
rowptr = g_list_nth_data(purpleresults->rows,row);
button->callback(gc, rowptr, user_data);
}
@end
|