-
Notifications
You must be signed in to change notification settings - Fork 12
/
RequestController.m
186 lines (147 loc) · 5.07 KB
/
RequestController.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
#import "RequestController.h"
#import "RosterController.h"
#import "XMPP.h"
@implementation RequestController
- (id)init
{
if(self = [super init])
{
jids = [[NSMutableArray alloc] init];
jidIndex = -1;
}
return self;
}
- (void)awakeFromNib
{
[xmppClient addDelegate:self];
NSRect visibleFrame = [[window screen] visibleFrame];
NSRect windowFrame = [window frame];
NSPoint windowPosition;
windowPosition.x = visibleFrame.origin.x + visibleFrame.size.width - windowFrame.size.width - 5;
windowPosition.y = visibleFrame.origin.y + visibleFrame.size.height - windowFrame.size.height - 5;
[window setFrameOrigin:windowPosition];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[jids release];
[super dealloc];
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// NSWindow Delegate Methods
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)windowWillClose:(NSNotification *)notification
{
// User chose to ignore requests by closing the window
[jids removeAllObjects];
jidIndex = -1;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Helper Methods
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)nextRequest
{
NSLog(@"RequestController: nextRequest");
if(++jidIndex < [jids count])
{
XMPPJID *jid = [jids objectAtIndex:jidIndex];
[jidField setStringValue:[jid bare]];
[xofyField setStringValue:[NSString stringWithFormat:@"%i of %i", (jidIndex+1), [jids count]]];
}
else
{
[jids removeAllObjects];
jidIndex = -1;
[window close];
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// XMPPClient Delegate Methods
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)xmppClient:(XMPPClient *)sender didReceiveBuddyRequest:(XMPPJID *)jid
{
if(![jids containsObject:jid])
{
[jids addObject:jid];
if([jids count] == 1)
{
jidIndex = 0;
[jidField setStringValue:[jid bare]];
[xofyField setHidden:YES];
[window setAlphaValue:0.85];
[window makeKeyAndOrderFront:self];
}
else
{
[xofyField setStringValue:[NSString stringWithFormat:@"%i of %i", (jidIndex+1), [jids count]]];
[xofyField setHidden:NO];
}
}
}
- (void)xmppClientDidUpdateRoster:(XMPPClient *)sender
{
// Often times XMPP servers send presence requests prior to sending the roster.
// That is, after you authenticate, they immediately send you presence requests,
// meaning that we receive them before we've had a chance to request and receive our roster.
// The result is that we may not know, upon receiving a presence request,
// if we've already requested this person to be our buddy.
// We make up for that by fixing our mistake as soon as possible.
NSArray *roster = [xmppClient sortedUsersByAvailabilityName];
// Remember: Our roster contains only those users we've added.
// If the server tries to include buddies that we haven't added, but have asked to subscribe to us,
// the xmpp client filters them out.
int i;
for(i = 0; i < [roster count]; i++)
{
XMPPUser *user = [roster objectAtIndex:i];
int index = [jids indexOfObject:[user jid]];
if(index != NSNotFound)
{
// Now we may be getting a notification of an updated roster due to an accept/reject we just sent.
// The simplest way to check is if the index isn't pointing to a jid we've already processed.
if(index >= jidIndex)
{
NSLog(@"Auto-accepting buddy request, since they already accepted us");
[sender acceptBuddyRequest:[user jid]];
[jids removeObjectAtIndex:index];
// We need to calll nextRequest, but we want jidIndex to remain at it's current jid
if(index >= jidIndex)
{
// Subtract 1, because nextRequest will immediately add 1
jidIndex = jidIndex - 1;
}
else
{
// Subtract 2, because the current jid will go down 1
// and because nextRequest will immediately add 1
jidIndex = jidIndex - 2;
}
[self nextRequest];
}
}
}
}
- (void)xmppClientDidDisconnect:(XMPPClient *)sender
{
// We can't accept or reject any requests when we're disconnected from the server.
// We may as well close the window.
[jids removeAllObjects];
jidIndex = -1;
[window close];
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Interface Builder Methods
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (IBAction)accept:(id)sender
{
XMPPJID *jid = [jids objectAtIndex:jidIndex];
[xmppClient acceptBuddyRequest:jid];
[self nextRequest];
}
- (IBAction)reject:(id)sender
{
XMPPJID *jid = [jids objectAtIndex:jidIndex];
[xmppClient rejectBuddyRequest:jid];
[self nextRequest];
}
@end