-
Notifications
You must be signed in to change notification settings - Fork 0
/
TLJimBos.m
300 lines (255 loc) · 9.84 KB
/
TLJimBos.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
//
// TLJimBos.m
// Mercatalog
//
// Created by Nathan Vander Wilt on 1/20/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "TLJimBos.h"
#import "NSApplication+TLLicense.h"
#import <CommonCrypto/CommonDigest.h>
const NSUInteger TLJimBos_MaximumUseCount = 10;
static NSString* const TLJimBos_StorePage = @"http://calftrail.com/store";
static NSString* const TLJimBos_ContactAddress = @"support@calftrail.com";
static NSString* const TLJimBos_ContactSubject = @"Geotagalog feedback";
static NSString* const TLJimBos_ContactBody = @"";
static NSString* const TLJimBos_UserNameKey = @"com.calftrail.mercatalog.username";
static NSString* const TLJimBos_ProductKey = @"com.calftrail.mercatalog.productkey";
NSString* const TLJimBosAppRegisteredNotification = @"TLJimBosAppRegistered";
@interface TLJimBos ()
- (BOOL)isRegistered;
- (BOOL)checkKey:(NSString*)licenseKey;
- (NSString*)emailAddressFromKey:(NSString*)licenseKey;
@end
@implementation TLJimBos
+ (id)sharedRegistrar {
static TLJimBos* sharedRegistrar = nil;
if (!sharedRegistrar) {
sharedRegistrar = [TLJimBos new];
// http://www.cocoabuilder.com/archive/message/cocoa/2008/8/10/215295
[[NSAppleEventManager sharedAppleEventManager] setEventHandler:sharedRegistrar
andSelector:@selector(handleURLEvent:reply:)
forEventClass:kInternetEventClass
andEventID:kAEGetURL];
[[NSNotificationCenter defaultCenter] addObserver:sharedRegistrar selector:@selector(registeredNew:)
name:TLLicenseApplicationRegistered object:nil];
}
return sharedRegistrar;
}
- (void)dealloc {
[registrationWindow release];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSAppleEventManager sharedAppleEventManager] removeEventHandlerForEventClass:kInternetEventClass andEventID:kAEGetURL];
[super dealloc];
}
- (void)handleURLEvent:(NSAppleEventDescriptor *)event reply:(NSAppleEventDescriptor *)replyEvent {
(void)replyEvent;
NSString* urlString = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
NSURL* url = [NSURL URLWithString:urlString];
//NSLog(@"Received URL: %@\n", url);
if (![[url scheme] isEqualToString:@"x-com-calftrail-license-tagalog"]) {
return;
}
NSString* licenseKey = [[url resourceSpecifier] stringByReplacingOccurrencesOfString:@"//" withString:@""];
NSString* userName = NSFullUserName();
BOOL registered = [self registerApplication:licenseKey user:userName];
if (registered && registrationWindow) {
[self closeRegistrationWindow:self];
}
if (registered) {
[self showRegistrationWindow:self];
}
else {
NSAlert* registrationError = [[NSAlert new] autorelease];
[registrationError setAlertStyle:NSWarningAlertStyle];
[registrationError setMessageText:@"Automatic registration failed!"];
[registrationError setInformativeText:
@"Geotagalog could not be registered through the link you just clicked. "
@"You can try registering using the manual window, or contact us if you are having trouble. "
@"Sorry for the inconvenience."];
(void)[registrationError addButtonWithTitle:@"OK"];
NSButton* contact = [registrationError addButtonWithTitle:@"Contact Calf Trail"];
[contact setBezelStyle:NSRoundRectBezelStyle];
NSInteger button = [registrationError runModal];
if (button == NSAlertSecondButtonReturn) {
[self contactCalfTrail:self];
}
else {
[self showRegistrationWindow:self];
}
}
}
- (IBAction)contactCalfTrail:(id)sender {
(void)sender;
//http://www.ietf.org/rfc/rfc2368
NSString* mailString = [NSString stringWithFormat:@"mailto:%@?subject=%@&body=%@",
TLJimBos_ContactAddress, TLJimBos_ContactSubject, TLJimBos_ContactBody];
NSString* encodedMailString = [mailString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL* mailURL = [NSURL URLWithString:encodedMailString];
[[NSWorkspace sharedWorkspace] openURL:mailURL];
}
- (IBAction)buyMercatalog:(id)sender {
(void)sender;
NSURL* storeURL = [NSURL URLWithString:TLJimBos_StorePage];
[[NSWorkspace sharedWorkspace] openURL:storeURL];
}
- (BOOL)continueExportDemoPrompt {
NSAssert(![self isRegistered], @"Don't demo prompt when already registered!");
NSAlert* demoInfo = [[NSAlert new] autorelease];
[demoInfo setAlertStyle:NSWarningAlertStyle];
[demoInfo setMessageText:@"Thanks for trying Geotagalog!"];
NSString* info = [NSString stringWithString:
@"You are using a demo version of Geotagalog, which limits the amount of photos you may export. "
@"To remove this limitation, you will need to purchase a registration code."];
[demoInfo setInformativeText:info];
(void)[demoInfo addButtonWithTitle:@"Export 24 photos"];
NSButton* buy = [demoInfo addButtonWithTitle:@"Purchase"];
[buy setBezelStyle:NSRoundRectBezelStyle];
NSButton* contact = [demoInfo addButtonWithTitle:@"Contact Calf Trail"];
[contact setBezelStyle:NSRoundRectBezelStyle];
NSInteger button = [demoInfo runModal];
if (button == NSAlertFirstButtonReturn) {
// use demo
return YES;
}
else if (button == NSAlertSecondButtonReturn) {
[self buyMercatalog:self];
}
else if (button == NSAlertThirdButtonReturn) {
[self contactCalfTrail:self];
}
return NO;
}
- (IBAction)showRegistrationWindow:(id)sender {
(void)sender;
NSString* userName = [[NSUserDefaults standardUserDefaults] objectForKey:TLJimBos_UserNameKey];
if ([self isRegistered]) {
[NSBundle loadNibNamed:@"ThankYou" owner:self];
[nameField setStringValue:userName];
NSString* licenseKey = [[NSUserDefaults standardUserDefaults] objectForKey:TLJimBos_ProductKey];
NSString* emailAddress = [self emailAddressFromKey:licenseKey];
[emailField setStringValue:emailAddress];
}
else {
[NSBundle loadNibNamed:@"Registration" owner:self];
if (!userName) {
userName = NSFullUserName();
}
[nameField setStringValue:userName];
}
[registrationWindow makeKeyAndOrderFront:self];
}
- (IBAction)registerByForm:(id)sender {
(void)sender;
NSString* licenseKey = [registrationCode stringValue];
NSString* userName = [nameField stringValue];
BOOL registered = [self registerApplication:licenseKey user:userName];
if (!registered) {
NSBeep();
[registerButton setEnabled:NO];
}
else {
[self closeRegistrationWindow:self];
[self showRegistrationWindow:self];
}
}
- (void)controlTextDidChange:(NSNotification*)changeNotification {
if ([changeNotification object] != registrationCode) return;
NSString* licenseKey = [registrationCode stringValue];
if ([self checkKey:licenseKey]) {
[registerButton setEnabled:YES];
}
else {
[registerButton setEnabled:NO];
}
}
- (void)cleanupWindow {
registrationWindow = nil;
nameField = nil;
emailField = nil;
registrationCode = nil;
registerButton = nil;
}
- (void)windowWillClose:(NSNotification *)notification {
(void)notification;
[self cleanupWindow];
}
- (IBAction)closeRegistrationWindow:(id)sender {
(void)sender;
[registrationWindow close];
}
- (NSString*)createLicenseKeyWithEmail:(NSString*)email productCode:(NSString*)productCode secret:(NSString*)secret {
NSString* stringToHash = [NSString stringWithFormat:@"%@%@%@", email, productCode, secret];
const char* cStringToHash = [stringToHash UTF8String];
unsigned char hash[CC_SHA1_DIGEST_LENGTH];
CC_SHA1(cStringToHash, (CC_LONG)strlen(cStringToHash), hash);
NSString* hashString = [NSString stringWithFormat:
@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
hash[0], hash[1], hash[2], hash[3],
hash[4], hash[5], hash[6], hash[7],
hash[8], hash[9], hash[10], hash[11],
hash[12], hash[13], hash[14], hash[15],
hash[16], hash[17], hash[18], hash[19], hash[20] ];
return [NSString stringWithFormat:@"%@%@%@",
email,
@"@@",
hashString];
}
- (NSString*)emailAddressFromKey:(NSString*)licenseKey {
NSArray* sections = [licenseKey componentsSeparatedByString:@"@@"];
NSString* emailAddress = nil;
if ([sections count]) {
emailAddress = [sections objectAtIndex:0];
}
return emailAddress;
}
- (BOOL)licenseKeyIsValid:(NSString*)licenseKey productCode:(NSString*)productCode secret:(NSString*)secret {
BOOL result = NO;
NSString* emailAddress = [self emailAddressFromKey:licenseKey];
if (emailAddress) {
NSString* generatedLicense = [self createLicenseKeyWithEmail:emailAddress
productCode:productCode
secret:secret];
if ([generatedLicense isEqualToString:licenseKey]) {
result = YES;
}
}
return result;
}
- (BOOL)checkKey:(NSString*)licenseKey {
if (!licenseKey) return NO;
static NSString* const productCode = @"tagalog-1.0";
static NSString* const secret = @"pleasesupportourworkandfamilies";
NSString* extraSecret = [NSString stringWithFormat:@"%c%c%c%s%c", 'C', 'T', 'S', "llc", '1'];
NSString* fullSecret = [secret stringByAppendingString:extraSecret];
return [self licenseKeyIsValid:licenseKey productCode:productCode secret:fullSecret];
}
- (BOOL)isRegistered {
NSString* licenseKey = [[NSUserDefaults standardUserDefaults] objectForKey:TLJimBos_ProductKey];
BOOL oldKey = [self checkKey:licenseKey];
return oldKey || [NSApp tl_registrationIsValid];
}
- (BOOL)registerApplication:(NSString*)licenseKey
user:(NSString*)userName
{
[[NSUserDefaults standardUserDefaults] setObject:userName forKey:TLJimBos_UserNameKey];
[[NSUserDefaults standardUserDefaults] setObject:licenseKey forKey:TLJimBos_ProductKey];
BOOL saved = [[NSUserDefaults standardUserDefaults] synchronize];
if (!saved) {
NSLog(@"Could not save user defaults O_o");
return NO;
}
BOOL valid = [self isRegistered];
if (!valid) {
[[NSUserDefaults standardUserDefaults] removeObjectForKey:TLJimBos_ProductKey];
}
else {
[[NSNotificationCenter defaultCenter] postNotificationName:TLJimBosAppRegisteredNotification object:self];
}
return valid;
}
- (void)registeredNew:(NSNotification*)aNotification {
(void)aNotification;
[[NSNotificationCenter defaultCenter] postNotificationName:TLJimBosAppRegisteredNotification object:self];
}
@end