2 // DCPurpleSIPEJoinChatViewController.m
5 // Copyright (C) 2015 SIPE Project <http://sipe.sourceforge.net/>
7 // Created by Michael Lamb on 02/10/12.
8 // Copyright 2012 Michael Lamb. All rights reserved.
11 #import "DCPurpleSIPEJoinChatViewController.h"
12 #import <Adium/AIChatControllerProtocol.h>
13 #import "DCJoinChatWindowController.h"
14 #import <Adium/AIAccount.h>
15 #import <ESDebugAILog.h>
16 #import "CBPurpleAccount.h"
19 @implementation DCPurpleSIPEJoinChatViewController
25 room_dict = [[NSMutableDictionary alloc] init];
26 combo_rooms.usesDataSource = YES;
27 combo_rooms.completes = YES;
28 combo_rooms.dataSource = self;
37 if (room_list != NULL) {
38 purple_roomlist_unref(room_list);
45 - (void)configureForAccount:(AIAccount *)inAccount
47 [super configureForAccount:inAccount];
49 [(DCJoinChatWindowController *)delegate setJoinChatEnabled:YES];
53 if (room_list == NULL) {
54 // we want to run that code only once (configureForAccount is called twice actually)
55 CBPurpleAccount *pinAccount = (CBPurpleAccount*)inAccount;
56 room_list = purple_roomlist_get_list(pinAccount.purpleAccount->gc);
58 purple_roomlist_ref(room_list);
59 [progress_fetch startAnimation:self];
60 // start a timer to control when the fetching is done
61 timer = [NSTimer scheduledTimerWithTimeInterval:0.5
63 selector:@selector(checkForRoomlistFetchCompletion:)
67 [progress_fetch setHidden:YES];
68 AILog(@"(DCPurpleSIPEJoinChatViewController) Can't fetch room list.");
73 - (void)joinChatWithAccount:(AIAccount *)inAccount
76 NSInteger idx = [combo_rooms indexOfSelectedItem];
78 if (idx >= 0 && [room_dict count]) {
80 NSString *key = [[room_dict allKeys] objectAtIndex:idx];
83 uri = [room_dict valueForKey:key];
86 uri = [combo_rooms stringValue];
88 if (uri && [uri length]) {
89 NSRange res = [uri rangeOfString:@"ma-chan://" options:NSCaseInsensitiveSearch];
91 if (res.location != 0) {
92 NSAlert *alert = [[NSAlert alloc] init];
94 [alert setMessageText:@"Invalid room URI"];
95 [alert setInformativeText:[combo_rooms toolTip]];
96 [alert addButtonWithTitle:@"Ok"];
102 [self doJoinChatWithName:[NSString stringWithFormat:@"%@",uri]
104 chatCreationInfo:[NSDictionary dictionaryWithObjectsAndKeys:
108 withInvitationMessage:nil];
112 AILog(@"(DCPurpleSIPEJoinChatViewController) No URI specified.");
115 // TODO: allow creation of OCS "Conference" (different from group-chat)
116 // Add a text field (UI should have radio buttons to enable/disable
117 // create a PurpleBuddy* based off of the username entered in the text field
119 // sipe_core_buddy_new_chat(PURPLE_BUDDY_TO_SIPE_CORE_PUBLIC, purple_buddy_get_name(buddy));
120 // which should kick off the Adium code to open a chat window.
124 - (void)checkForRoomlistFetchCompletion:(NSTimer*) aTimer
126 if (room_list && purple_roomlist_get_in_progress(room_list) == FALSE) {
127 [progress_fetch stopAnimation:self];
128 [progress_fetch setHidden:YES];
132 // finally copy the list into our dict
133 for (GList *rooms = room_list->rooms; rooms != NULL; rooms = rooms->next) {
134 PurpleRoomlistRoom *room = rooms->data;
135 gchar *roomName = room->name;
136 gchar *uriStr = room->fields->data;
137 if (roomName != NULL && uriStr != NULL) {
138 NSString *nameStr = [NSString stringWithUTF8String:roomName];
139 if ([room_dict objectForKey:nameStr] == nil) {
140 [room_dict setObject:[NSString stringWithUTF8String:uriStr] forKey:nameStr];
145 purple_roomlist_unref(room_list);
150 #pragma mark NSComboBoxDataSource
152 - (NSInteger) numberOfItemsInComboBox:(NSComboBox*) aComboBox
154 return [room_dict count];
156 - (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(NSInteger)index
158 NSArray* keys = [room_dict allKeys];
159 return [keys objectAtIndex:index];
163 - (NSString *)comboBox:(NSComboBox *)aComboBox completedString:(NSString *)uncompletedString
165 if ([room_dict count] == 0 || uncompletedString == nil) {
168 NSArray *keys = [room_dict allKeys];
169 for (NSString *key in keys) {
170 NSRange res = [key rangeOfString:uncompletedString options:NSCaseInsensitiveSearch];
171 if (res.location != NSNotFound && res.location == 0) {
179 - (NSUInteger)comboBox:(NSComboBox *)aComboBox indexOfItemWithStringValue:(NSString *)aString
181 if ([room_dict count] == 0) {
184 NSArray *keys = [room_dict allKeys];
185 return [keys indexOfObjectIdenticalTo:aString];
190 - (NSString *)nibName
192 return @"DCPurpleSIPEJoinChatView";