Release 1.25.0 -- Buddy Idle Time, RTF
[siplcs.git] / src / adium / DCPurpleSIPEJoinChatViewController.m
blobb1de79f7f9f60569c78c374ae01969a4b1481ccf
1 //
2 //  DCPurpleSIPEJoinChatViewController.m
3 //  SIPEAdiumPlugin
4 //
5 //  Copyright (C) 2015 SIPE Project <http://sipe.sourceforge.net/>
6 //
7 //  Created by Michael Lamb on 02/10/12.
8 //  Copyright 2012 Michael Lamb. All rights reserved.
9 //
11 #import "DCPurpleSIPEJoinChatViewController.h"
12 #import <Adium/AIChatControllerProtocol.h>
13 #import <Adium/DCJoinChatWindowController.h>
14 #import <Adium/AIAccount.h>
15 #import <Adium/ESDebugAILog.h>
16 #import <AdiumLibPurple/CBPurpleAccount.h>
17 #import "roomlist.h"
19 @implementation DCPurpleSIPEJoinChatViewController
21 - (id)init
23     self = [super init];
24     if (self) {
25         room_dict = [[NSMutableDictionary alloc] init];
26         combo_rooms.usesDataSource = YES;
27         combo_rooms.completes  = YES;
28         combo_rooms.dataSource = self;
29     }
30     return self;
33 - (void)dealloc
35     [timer invalidate];
36     timer = nil;
37     if (room_list != NULL) {
38         purple_roomlist_unref(room_list);
39         room_list = NULL;
40     }
41     [room_dict release];
42     [super dealloc];
45 - (void)configureForAccount:(AIAccount *)inAccount
47     [super configureForAccount:inAccount];
48     if ( delegate ) {
49         [(DCJoinChatWindowController *)delegate setJoinChatEnabled:YES];
50     }
52     // get room list
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);
57         if (room_list) {
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
62                                                      target:self
63                                                    selector:@selector(checkForRoomlistFetchCompletion:)
64                                                    userInfo:nil
65                                                     repeats:YES];
66         } else {
67             [progress_fetch setHidden:YES];
68             AILog(@"(DCPurpleSIPEJoinChatViewController) Can't fetch room list.");
69         }
70     }
73 - (void)joinChatWithAccount:(AIAccount *)inAccount
75     NSString *uri = nil;
76     NSInteger idx = [combo_rooms indexOfSelectedItem];
78     if (idx >= 0 && [room_dict count]) {
79         // get selected entry
80         NSString *key = [[room_dict allKeys] objectAtIndex:idx];
82         if (key)
83             uri = [room_dict valueForKey:key];
85     } else
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"];
97             [alert runModal];
98             [alert release];
100         } else {
102             [self doJoinChatWithName:[NSString stringWithFormat:@"%@",uri]
103                            onAccount:inAccount
104                     chatCreationInfo:[NSDictionary dictionaryWithObjectsAndKeys:
105                                       uri, @"uri",
106                                       nil]
107                     invitingContacts:nil
108                withInvitationMessage:nil];
110         }
111     } else {
112         AILog(@"(DCPurpleSIPEJoinChatViewController) No URI specified.");
113     }
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
118     // then call:
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];
129         [timer invalidate];
130         timer = nil;
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];
141                 }
142             }
143         }
145         purple_roomlist_unref(room_list);
146         room_list = NULL;
147     }
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];
162 // String completion
163 - (NSString *)comboBox:(NSComboBox *)aComboBox completedString:(NSString *)uncompletedString
165     if ([room_dict count] == 0 || uncompletedString == nil) {
166         return @"";
167     }
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) {
172             return key;
173         }
174     }
176     return @"";
179 - (NSUInteger)comboBox:(NSComboBox *)aComboBox indexOfItemWithStringValue:(NSString *)aString
181     if ([room_dict count] == 0) {
182         return NSNotFound;
183     }
184     NSArray *keys = [room_dict allKeys];
185     return [keys indexOfObjectIdenticalTo:aString];
188 #pragma mark
190 - (NSString *)nibName
192     return @"DCPurpleSIPEJoinChatView";
195 @end