Skip to content
This repository
tree: cca652018d
Fetching contributors…

Cannot retrieve contributors at this time

file 387 lines (331 sloc) 10.926 kb
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 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
/*
* Copyright © 2003, Bryan L Blackburn. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. 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.
*
* 3. Neither the names Bryan L Blackburn, Withay.com, nor the names of
* any contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY BRYAN L BLACKBURN ``AS IS'' AND ANY
* EXPRESSED 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 THE AUTHOR OR CONTRIBUTORS
* 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.
*
*/
// Interesting security issues are noted with XXX in comments

// XXX If it were possible, we'd clear out all the controls in windowWillClose:

/* CSWinCtrlEntry.m */

#import "CSWinCtrlEntry.h"
#import "CSDocument.h"
#import "CSAppController.h"
#import "NSData_crypto.h"
#import "NSData_clear.h"

// Defines for localized strings
#define CSWINCTRLENTRY_LOC_INVALIDURL NSLocalizedString( @"Invalid URL", @"" )
#define CSWINCTRLENTRY_LOC_URLNOTVALID \
NSLocalizedString( @"The URL entered is not a valid URL", @"" )
#define CSWINCTRLENTRY_LOC_NOTSAVED NSLocalizedString( @"Entry Not Saved", @"" )
#define CSWINCTRLENTRY_LOC_NOTSAVEDCLOSE \
NSLocalizedString( @"The entry has not been saved, close anyway?", @"" )
#define CSWINCTRLENTRY_LOC_CLOSEANYWAY NSLocalizedString( @"Close Anyway", @"" )
#define CSWINCTRLENTRY_LOC_DONTCLOSE NSLocalizedString( @"Don't Close", @"" )

@interface CSWinCtrlEntry (InternalMethods)
- (void) _closeSheetDidEnd:(NSWindow *)sheet
         returnCode:(int)returnCode
         contextInfo:(void *)contextInfo;
- (void) _undoManagerDidChange:(NSNotification *)notification;
@end

@implementation CSWinCtrlEntry

/*
* Character strings for password generation; the extra characters in genAll
* are doubled to help propagate them further
*/
static const char *genAlphanum = "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwW"
                                 "xXyYzZ0123456789";
static const char *genAll = "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwW"
                                 "xXyYzZ0123456789~!@#$%^&*()_+`-=[]\\{}|;':\",."
                                 "/<>?~!@#$%^&*()_+`-=[]\\{}|;':\",./<>?";

- (id) initWithWindowNibName:(NSString *)windowNibName
{
   self = [ super initWithWindowNibName:windowNibName ];
   if( self != nil )
   {
      /*
* Create an undo manager for the notes field, so that "Undo Typing" won't
* be present in the Edit menu when sitting on a text field which doesn't
* support undo
* Watching undo/redo change notifications lets us update the 'document
* is dirty' status
*/
      _notesUM = [ [ NSUndoManager alloc ] init ];
      [ [ NSNotificationCenter defaultCenter ]
        addObserver:self
        selector:@selector( _undoManagerDidChange: )
        name:NSUndoManagerDidUndoChangeNotification
        object:_notesUM ];
      [ [ NSNotificationCenter defaultCenter ]
        addObserver:self
        selector:@selector( _undoManagerDidChange: )
        name:NSUndoManagerDidRedoChangeNotification
        object:_notesUM ];
      // Undo manager for everything else in the window
      _otherUM = [ [ NSUndoManager alloc ] init ];
   }

   return self;
}


/*
* Generate a random password
*/
- (IBAction) doGenerate:(id)sender
{
   const char *genString;
   int genStringLength;
   int genSize;
   NSMutableString *randomString;
   NSMutableData *randomData;
   unsigned char *randomBytes;
   int index;

   if( [ [ NSUserDefaults standardUserDefaults ]
         boolForKey:CSPrefDictKey_AlphanumOnly ] )
      genString = genAlphanum;
   else
      genString = genAll;
   genStringLength = strlen( genString );

   genSize = [ [ NSUserDefaults standardUserDefaults ]
               integerForKey:CSPrefDictKey_GenSize ];
   randomString = [ NSMutableString stringWithCapacity:genSize ];
   randomData = [ NSData randomDataOfLength:genSize ];
   randomBytes = [ randomData mutableBytes ];
   for( index = 0; index < genSize; index++ )
      [ randomString appendFormat:@"%c",
                        genString[ randomBytes[ index ] % genStringLength ] ];
   [ _passwordText setStringValue:randomString ];
   [ randomData clearOutData ];
   /*
* XXX deleteCharactersInRange: probably just changes its length; strings are
* a pain in the ass in Cocoa from a security point of view
*/
   [ randomString deleteCharactersInRange:
                     NSMakeRange( 0, [ randomString length ] ) ];

   [ [ self window ] setDocumentEdited:YES ];
}


/*
* Open the URL in the URL field
*/
- (IBAction) doOpenURL:(id)sender
{
   BOOL urlIsInvalid;
   NSURL *theURL;

   urlIsInvalid = YES;
   theURL = [ NSURL URLWithString:[ _urlText stringValue ] ];
   if( theURL != nil && [ [ NSWorkspace sharedWorkspace ] openURL:theURL ] )
      urlIsInvalid = NO;

   if( urlIsInvalid )
      NSBeginInformationalAlertSheet( CSWINCTRLENTRY_LOC_INVALIDURL,
                                      nil, nil, nil, [ self window ], nil, nil,
                                      nil, nil, CSWINCTRLENTRY_LOC_URLNOTVALID );
}


/*
* When the window needs an undo manager, we give it the notes one for that
* view, or the rest-of-the-window manager
*/
- (NSUndoManager *) windowWillReturnUndoManager:(NSWindow *)window
{
   if( [ [ [ self window ] firstResponder ] isEqual:_notes ] )
      return _notesUM;
   else
      return _otherUM;
}


/*
* We ignore this as changes in the main document don't affect entry
* controllers; we handle edited state separately
*/
- (void) setDocumentEdited:(BOOL)dirtyFlag
{
   // Do nothing
}


/*
* Sent by the text fields
*/
- (void) controlTextDidChange:(NSNotification *)aNotification
{
   NSString *nameTextString;

   [ self updateDocumentEditedStatus ];
   if( [ [ aNotification object ] isEqual:_nameText ] )
   {
      nameTextString = [ _nameText stringValue ];
      if( nameTextString == nil || [ nameTextString length ] == 0 )
         [ _mainButton setEnabled:NO ];
      else
         [ _mainButton setEnabled:YES ];
   }
}


/*
* Sent by the category combo box
*/
- (void) comboBoxWillDismiss:(NSNotification *)notification
{
   [ self updateDocumentEditedStatus ];
}


/*
* Sent by the text view, but not for undo/redo
*/
- (void)textDidChange:(NSNotification *)notification
{
   [ self updateDocumentEditedStatus ];
}


/*
* If the window has been edited, ask about the close first
*/
- (BOOL) windowShouldClose:(id)sender
{
   BOOL retval;
   id alertPanel;

   retval = YES;
   if( [ [ self window ] isDocumentEdited ] )
   {
      alertPanel = NSGetCriticalAlertPanel( CSWINCTRLENTRY_LOC_NOTSAVED,
                                            CSWINCTRLENTRY_LOC_NOTSAVEDCLOSE,
                                            CSWINCTRLENTRY_LOC_CLOSEANYWAY,
                                            CSWINCTRLENTRY_LOC_DONTCLOSE, nil );
      [ NSApp beginSheet:alertPanel
              modalForWindow:[ self window ]
              modalDelegate:self
              didEndSelector:
                 @selector( _closeSheetDidEnd:returnCode:contextInfo: )
              contextInfo:NULL ];
      retval = NO;
   }

   return retval;
}


/*
* Make sure any attached sheet gets ended prior to closing, otherwise the
* app gets stuck
*/
- (void) windowWillClose:(NSNotification *)notification
{
   NSWindow *sheet;

   sheet = [ [ self window ] attachedSheet ];
   if( sheet != nil )
      [ NSApp endSheet:sheet ];
}


/*
* Update the window's edited status based on whether anything in the window
* has changed
*/
- (void) updateDocumentEditedStatus
{
   if( [ self nameChanged ] || [ self accountChanged ] ||
       [ self passwordChanged ] || [ self urlChanged ] ||
       [ self categoryChanged ] || [ self notesChanged ] )
      [ [ self window ] setDocumentEdited:YES ];
   else
      [ [ self window ] setDocumentEdited:NO ];
}


/*
* Provide information to combo boxes for categories
*/
- (int) numberOfItemsInComboBox:(NSComboBox *)aComboBox
{
   if( [ aComboBox isEqual:_category ] )
      return [ [ [ self document ] categories ] count ];
   return 0;
}

- (id) comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(int)index
{
   if( [ aComboBox isEqual:_category ] )
      return [ [ [ self document ] categories ] objectAtIndex:index ];
   return nil;
}

- (unsigned int) comboBox:(NSComboBox *)aComboBox
                 indexOfItemWithStringValue:(NSString *)aString
{
   if( [ aComboBox isEqual:_category ] )
      return [ [ [ self document ] categories ] indexOfObject:aString ];
   return -1;
}


/*
* The ...Changed are meant to be overridden in subclasses
*/
- (BOOL) nameChanged
{
   return YES;
}

- (BOOL) accountChanged
{
   return YES;
}

- (BOOL) passwordChanged
{
   return YES;
}

- (BOOL) urlChanged
{
   return YES;
}

- (BOOL) categoryChanged
{
   return YES;
}

- (BOOL) notesChanged
{
   return YES;
}


/*
* Cleanup
*/
- (void) dealloc
{
   [ _notesUM release ];
   [ _otherUM release ];
   [ super dealloc ];
}


/*
* Handle the "should close" sheet
*/
- (void) _closeSheetDidEnd:(NSWindow *)sheet
         returnCode:(int)returnCode
         contextInfo:(void *)contextInfo
{
   if( returnCode == NSAlertDefaultReturn )
   {
      // Close the window this way so the proper delegation is performed
      [ [ self window ] setDocumentEdited:NO ];
      [ [ NSRunLoop currentRunLoop ]
        performSelector:@selector( performClose: )
        target:[ self window ]
        argument:self
        order:9999
        modes:[ NSArray arrayWithObject:NSDefaultRunLoopMode ] ];
   }
   [ sheet orderOut:self ];
   [ NSApp endSheet:sheet ];
   NSReleaseAlertPanel( sheet );
}


/*
* When the undo manager for the notes text view performs undo or redo, we
* update the edited status
*/
- (void) _undoManagerDidChange:(NSNotification *)notification
{
   [ self updateDocumentEditedStatus ];
}

@end
Something went wrong with that request. Please try again.