-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQPAppController.m
271 lines (224 loc) · 9.99 KB
/
QPAppController.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
/*
Copyright (c) 2009, Stefan Lange-Hegermann
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of source.bricks nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY STEFAN LANGE-HEGERMANN ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL STEFAN LANGE-HEGERMANN BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#import "QPAppController.h"
#include "pronpas.h"
@interface NSStatusItem (Hack)
- (NSRect)hackFrame;
@end
@implementation NSStatusItem (Hack)
- (NSRect)hackFrame
{
return [_fWindow frame];
}
@end
@implementation QPAppController
+ (void)initialize {
NSDictionary *defaultValues=[NSDictionary dictionaryWithObjectsAndKeys:
@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890#*?-_",@"validchars",
@"6",@"minlength", // Minimum length for random passwords
@"8",@"maxlength", // Maximum length for random passwords
@"6",@"minlengthpron", // Minimum length for pronouncable passwords
@"8",@"maxlengthpron", // Maximum length for pronouncable passwords
[NSNumber numberWithBool:YES],@"firststart", // Is it the applications first start?
[NSNumber numberWithBool:YES],@"genpronpass", // Generate pronouncable passwords
[NSNumber numberWithBool:YES],@"genrandpass", // Generate random passwords
[NSNumber numberWithBool:NO],@"proncap", // Capitalize random characters in pronouncable passwords
[NSNumber numberWithBool:YES],@"pronrpunct", // Append number after password
[NSNumber numberWithBool:YES],@"pronleet", // Replace some characters in pronouncable passwords with 1337 representati0n
@"5",@"passcount", // Number of random passwords to generate
@"5",@"passcountpron", // Number of pronouncable passwords to generate
nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultValues];
[[NSUserDefaultsController sharedUserDefaultsController] setInitialValues:defaultValues];
srand ( time(NULL) );
seedPWD();
}
- (IBAction) toggleStartup:(id)sender
{
if ([sender state]==NSOnState) {
[self addStartupItem];
} else {
[self removeStartupItem];
}
}
- (void)addStartupItem
{
NSMutableDictionary *lwDomain=[[[NSUserDefaults standardUserDefaults] persistentDomainForName:@"loginwindow"] mutableCopy];
NSMutableArray *lwLaunchDict=[[lwDomain objectForKey:@"AutoLaunchedApplicationDictionary"] mutableCopy];
NSDictionary *appDict=[NSDictionary dictionaryWithObjectsAndKeys:@"YES",@"Hide",[[NSBundle mainBundle] bundlePath],@"Path",nil];
[lwLaunchDict addObject:appDict];
[lwDomain setValue:lwLaunchDict forKey:@"AutoLaunchedApplicationDictionary"];
[[NSUserDefaults standardUserDefaults] setPersistentDomain:lwDomain forName:@"loginwindow"];
[[NSUserDefaults standardUserDefaults] synchronize];
[lwDomain release];
[lwLaunchDict release];
}
- (void)removeStartupItem
{
int i;
NSMutableDictionary *lwDomain=[[[NSUserDefaults standardUserDefaults] persistentDomainForName:@"loginwindow"] mutableCopy];
NSMutableArray *lwLaunchDict=[[lwDomain objectForKey:@"AutoLaunchedApplicationDictionary"] mutableCopy];
for (i=0;i<[lwLaunchDict count];i++) {
if ([[[lwLaunchDict objectAtIndex:i] objectForKey:@"Path"] isEqualTo:[[NSBundle mainBundle] bundlePath]]) {
[lwLaunchDict removeObjectAtIndex:i];
}
}
[lwDomain setValue:lwLaunchDict forKey:@"AutoLaunchedApplicationDictionary"];
[[NSUserDefaults standardUserDefaults] setPersistentDomain:lwDomain forName:@"loginwindow"];
[[NSUserDefaults standardUserDefaults] synchronize];
[lwDomain release];
[lwLaunchDict release];
}
- (void)awakeFromNib
{
[self activateStatusMenu];
if ([[[[NSUserDefaultsController sharedUserDefaultsController] values] valueForKey:@"firststart"] isEqualTo:@"YES"] || YES) {
myTimer=[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(showInfo:) userInfo:nil repeats:NO];
[[[NSUserDefaultsController sharedUserDefaultsController] values] setValue:@"NO" forKey:@"firststart"];
}
}
- (IBAction)showInfo:(id)sender
{
NSRect screenRect = [[NSScreen mainScreen] frame];
if (myTimer)
[myTimer invalidate];
float left=[theItem hackFrame].origin.x;
NSPoint winframe=NSMakePoint(left-268 , screenRect.size.height-200);
[helpWindow setFrameOrigin:winframe];
[helpWindow fadeIn];
myTimer=[NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(hideInfo:) userInfo:nil repeats:NO];
}
- (IBAction)hideInfo:(id)sender
{
if (myTimer)
[myTimer invalidate];
myTimer=nil;
[helpWindow fadeOut];
}
- (void)activateStatusMenu
{
NSStatusBar *bar = [NSStatusBar systemStatusBar];
theItem = [bar statusItemWithLength:NSVariableStatusItemLength];
[theItem retain];
[theItem setImage:[[NSImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"key" ofType:@"png"]]];
[theItem setAlternateImage:[[NSImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"key-white" ofType:@"png"]]];
[theItem setHighlightMode:YES];
[theItem setAction:@selector(refreshMenu)];
}
-(NSString *)generatePassword
{
int i;
int charval;
NSString *validChars=[[[NSUserDefaultsController sharedUserDefaultsController] values] valueForKey:@"validchars"];
int minval=[[[[NSUserDefaultsController sharedUserDefaultsController] values] valueForKey:@"minlength"] intValue];
int maxval=[[[[NSUserDefaultsController sharedUserDefaultsController] values] valueForKey:@"maxlength"] intValue];
int subval=maxval-minval+1;
int loops=rand()%subval+minval;
NSMutableString *tmpstring=[NSMutableString string];
for (i=0;i<loops;i++) {
charval=rand()%[validChars length];
[tmpstring appendString:[validChars substringWithRange:NSMakeRange(charval,1)]];
}
return tmpstring;
}
-(NSString *)generatePronouncablePassword
{
//NSMutableString *tmpPass=@"";
int minval=[[[[NSUserDefaultsController sharedUserDefaultsController] values] valueForKey:@"minlengthpron"] intValue];
int maxval=[[[[NSUserDefaultsController sharedUserDefaultsController] values] valueForKey:@"maxlengthpron"] intValue];
int subval=maxval-minval+1;
int loops=rand()%subval+minval;
char *cstring;
cstring=getPWD(loops);
NSMutableString *tmpPass=[NSMutableString stringWithCString:cstring encoding:NSUTF8StringEncoding];
free(cstring);
if ([[[[NSUserDefaultsController sharedUserDefaultsController] values] valueForKey:@"proncap"] boolValue]) {
int i;
NSString *lcPass=tmpPass;
tmpPass=[NSMutableString string];
NSString *ucPass=[lcPass uppercaseString];
for (i=0;i<[lcPass length];i++) {
if (rand()%3!=1) {
[tmpPass appendString:[lcPass substringWithRange:NSMakeRange(i,1)]];
} else {
[tmpPass appendString:[ucPass substringWithRange:NSMakeRange(i,1)]];
}
}
}
if ([[[[NSUserDefaultsController sharedUserDefaultsController] values] valueForKey:@"pronrpunct"] boolValue]) {
int num=rand()%99;
return [tmpPass stringByAppendingFormat:@"%i",num];
}
return tmpPass;
}
-(void)refreshMenu
{
int a;
int passcount=[[[[NSUserDefaultsController sharedUserDefaultsController] values] valueForKey:@"passcount"] intValue];
int passcountpron=[[[[NSUserDefaultsController sharedUserDefaultsController] values] valueForKey:@"passcountpron"] intValue];
BOOL showRandom=[[[[NSUserDefaultsController sharedUserDefaultsController] values] valueForKey:@"genrandpass"] boolValue];
BOOL showPron=[[[[NSUserDefaultsController sharedUserDefaultsController] values] valueForKey:@"genpronpass"] boolValue];
while ([[theMenu itemArray] count]>4) {
[theMenu removeItemAtIndex:0];
}
if (showRandom) {
for (a=0;a<passcount;a++) {
[theMenu insertItemWithTitle:[self generatePassword] action:@selector(copyMenuEntry:) keyEquivalent:@"" atIndex:0];
}
}
if (showPron && showRandom) [theMenu insertItem:[NSMenuItem separatorItem] atIndex:0];
if (showPron) {
for (a=0;a<passcountpron;a++) {
[theMenu insertItemWithTitle:[self generatePronouncablePassword] action:@selector(copyMenuEntry:) keyEquivalent:@"" atIndex:0];
}
}
[theItem popUpStatusItemMenu:theMenu];
}
-(void)copyMenuEntry:(NSMenuItem *)test
{
NSPasteboard *pboard=[NSPasteboard generalPasteboard];
[pboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:self];
if (![pboard setString:[test title] forType:NSStringPboardType]) {
NSLog(@"Error");
}
}
-(IBAction) closeApp:(id)sender {
[NSApp terminate:self];
}
-(IBAction) about:(id)sender {
[helpWindow fadeOut];
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://www.sourcebricks.com/page/quickpass.html?qpabout=true"]];
}
-(IBAction) preferences:(id)sender {
[NSApp activateIgnoringOtherApps:YES];
[prefsWindow setAlphaValue:0];
[prefsWindow makeKeyAndOrderFront:self];
[[prefsWindow animator] setAlphaValue:1.0];
}
-(IBAction) resetChars:(id)sender {
[[[NSUserDefaultsController sharedUserDefaultsController] values] setValue:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890#*?-_" forKey:@"validchars"];
//[validField setStringValue:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890#*?-_"];
}
@end