-
Notifications
You must be signed in to change notification settings - Fork 0
/
SMSWindowController.m
330 lines (303 loc) · 9.43 KB
/
SMSWindowController.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
//
// SMSWindowController.m
// smsPlugin
//
// Created by Hugh Cole-Baker on 14/09/2009.
// Copyright (c) 2009 Hugh Cole-Baker
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#import "SMSWindowController.h"
#import "BluetoothPhoneInfo.h"
#import "BluetoothCommandChannel.h"
#import "SMSSender.h"
@implementation SMSWindowController
//initialize our member variables and load the nib file for our sheet
- (SMSWindowController*)initWithNib:(NSNib*)nib
{
self = [super init];
if(self)
{
currentPhoneNumber = nil;
if(![nib instantiateNibWithOwner:self topLevelObjects:&nibObjects])
{
NSLog(@"Error: could not instantiate nib");
return nil;
}
[nibObjects makeObjectsPerformSelector:@selector(release)];
[nibObjects retain];
}
return self;
}
- (void)runSheetForPhone:(NSString *)phoneNumber
{
if(currentPhoneNumber) [currentPhoneNumber autorelease];
currentPhoneNumber = phoneNumber;
[currentPhoneNumber retain];
[NSApp beginSheet:sendingPane modalForWindow:[NSApp mainWindow]
modalDelegate:self didEndSelector:NULL contextInfo:nil];
[self checkSavedTerminal];
}
//Checks if the terminal name that is saved in user defaults is present and selects it if so,
//otherwise erases the setting in defaults.
- (void)checkSavedTerminal
{
NSString* savedTerminal = [[NSUserDefaults standardUserDefaults] stringForKey:kBluetoothSMSSerialPort];
if(savedTerminal)
{
NSArray* list = [self list];
NSUInteger portsCount = [list count];
BOOL found = NO;
NSUInteger i;
for (i = 0; i < portsCount; ++i)
{
//compare name
if([[[list objectAtIndex:i] name] isEqualToString:savedTerminal])
{
[phonePopUp selectItemAtIndex:i];
BluetoothPhoneInfo* selectedPhone = [list objectAtIndex:i];
found = YES;
if(phone)
{
[phone release];
}
phone = [[SMSSender alloc] initWithDevice:selectedPhone.dev serviceUUID:selectedPhone.uuid];
phone.delegate = self;
break;
}
}
if(!found)
{
[phonePopUp selectItemAtIndex:0];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:kBluetoothSMSSerialPort];
}
}
}
//This method is called when the content of the text message field is updated
//and it recalculates the character/sms count.
- (void)controlTextDidChange:(NSNotification *)aNotification
{
int count = [SMSSender messageSize:[messageText stringValue]];
if([longMessages state] == NSOnState)
{
[messageLabel setStringValue:[NSString stringWithFormat:@"Message Text (%d characters, using %d messages):",[[messageText stringValue] length],count]];
}
else
{
if(count > 1)
{
//this shouldn't happen as the formatter should prevent it
//NSLog(@"Warning: count exceeded 1!");
[messageText setStringValue:[[messageText stringValue] substringToIndex:160]];
}
[messageLabel setStringValue:[NSString stringWithFormat:@"Message Text (%d characters):",[[messageText stringValue] length]]];
}
}
- (void)testFailed:(NSString*)message
{
[statusSpinner stopAnimation:self];
[statusLabel setStringValue:[NSString stringWithFormat:@"Test failed: %@",message]];
[testingPhoneName release];
testingPhoneName = nil;
[self checkSavedTerminal];
}
- (void)testPassed
{
[statusSpinner stopAnimation:self];
[statusLabel setStringValue:@"Test succeeded"];
[[NSUserDefaults standardUserDefaults] setObject:testingPhoneName forKey:kBluetoothSMSSerialPort];
[testingPhoneName release];
testingPhoneName = nil;
}
- (void)sendFailed:(NSString*)message
{
[statusSpinner stopAnimation:self];
[statusLabel setStringValue:[NSString stringWithFormat:@"Failed: %@",message]];
[self enableControls];
}
- (void)sendSucceeded
{
[statusSpinner stopAnimation:self];
[statusLabel setStringValue:@"Sent successfully"];
[NSTimer scheduledTimerWithTimeInterval:2
target:self
selector:@selector(delayedHideSheet)
userInfo:nil
repeats:NO];
}
- (void)statusUpdated:(NSString*)message
{
[statusLabel setStringValue:message];
}
//cleanup and hide the sheet
- (IBAction)cancel:(id)sender
{
[sendingPane orderOut:nil];
[NSApp endSheet:sendingPane];
[nibObjects release];
}
// Action fired when the user selects a port, it tests the port
- (IBAction)selectPhone:(id)sender
{
if(![[phonePopUp titleOfSelectedItem] isEqualToString:CHOOSE_PORT_STRING])
{
BluetoothPhoneInfo* selectedPhone = [[popupController selectedObjects] objectAtIndex:0];
if(phone)
{
[phone release];
}
if(testingPhoneName)
{
[testingPhoneName release];
}
phone = [[SMSSender alloc] initWithDevice:selectedPhone.dev serviceUUID:selectedPhone.uuid];
testingPhoneName = selectedPhone.name;
[testingPhoneName retain];
phone.delegate = self;
[statusLabel setHidden:NO];
IOReturn status = [phone test];
if(status == kIOReturnSuccess)
{
[statusLabel setStringValue:[NSString stringWithFormat:@"Testing %@ ...", selectedPhone.name]];
[statusSpinner startAnimation:sender];
}
else
{
[statusLabel setStringValue:@"Failed to open for testing"];
}
}
else if([[phonePopUp titleOfSelectedItem] isEqualToString:CHOOSE_PORT_STRING])
{
if(phone)
{
[phone release];
phone = nil;
}
if(testingPhoneName)
{
[testingPhoneName release];
testingPhoneName = nil;
}
[statusLabel setHidden:YES];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:kBluetoothSMSSerialPort];
}
}
//Action fired when the user clicks send
- (IBAction)send:(id)sender
{
NSString* savedTerminal = [[NSUserDefaults standardUserDefaults] stringForKey:kBluetoothSMSSerialPort];
[statusLabel setHidden:NO];
if(savedTerminal != nil && (![savedTerminal isEqualToString:@""]))
{
[self disableControls];
[statusLabel setStringValue:@"Sending..."];
[phone sendSMS:[messageText stringValue] to:currentPhoneNumber withReceipt:([receiptRequested state] == NSOnState)];
[statusSpinner startAnimation:sender];
}
else
{
[statusLabel setStringValue:@"Error: choose a valid port first!"];
}
}
- (IBAction)longMessagesChanged:(id)sender
{
[self controlTextDidChange:nil];
}
//disable the controls while send is in progress
- (void)disableControls
{
[messageLabel setEnabled:NO];
[messageText setEnabled:NO];
[sendLabel setEnabled:NO];
[phonePopUp setEnabled:NO];
[longMessages setEnabled:NO];
[receiptRequested setEnabled:NO];
[statusLabel setEnabled:NO];
[sendButton setEnabled:NO];
}
- (void)enableControls
{
[messageLabel setEnabled:YES];
[messageText setEnabled:YES];
[sendLabel setEnabled:YES];
[phonePopUp setEnabled:YES];
[longMessages setEnabled:YES];
[receiptRequested setEnabled:YES];
[statusLabel setEnabled:YES];
[sendButton setEnabled:YES];
}
//called to hide the sheet after a successful send
- (void)delayedHideSheet
{
[sendingPane orderOut:nil];
[NSApp endSheet:sendingPane];
[nibObjects release];
}
- (void)dealloc
{
if(phone)
{
[phone release];
phone = nil;
}
if(testingPhoneName)
{
[testingPhoneName release];
phone = nil;
}
if(currentPhoneNumber)
{
[currentPhoneNumber release];
phone = nil;
}
[super dealloc];
}
//List the tty devices available
- (NSArray*)list
{
BluetoothPhoneInfo* dummy = [[[BluetoothPhoneInfo alloc] init] autorelease];
dummy.dev = nil;
dummy.uuid = nil;
dummy.name = CHOOSE_PORT_STRING;
NSArray* phones = [NSArray arrayWithObject:dummy];
return [phones arrayByAddingObjectsFromArray:[BluetoothCommandChannel listServices]];
}
@end
//The SMSTextFieldFormatter is a custom formatter for the text field,
//which doesn't permit the length to exceed the SMS length if long messages
//are disabled.
@implementation SMSTextFieldFormatter
- (NSString *)stringForObjectValue:(id)object {
return (NSString *)object;
}
- (BOOL)getObjectValue:(id *)object forString:(NSString *)string errorDescription:(NSString **)error {
*object = string;
return YES;
}
- (BOOL)isPartialStringValid:(NSString *)partialString newEditingString:(NSString **)newString errorDescription:(NSString **)error {
if([longMessages state] == NSOffState && [SMSSender messageSize:partialString] > 1)
{
*newString = nil;
return NO;
}
return YES;
}
- (NSAttributedString *)attributedStringForObjectValue:(id)anObject withDefaultAttributes:(NSDictionary *)attributes {
return nil;
}
@end