Files
gitx/gitx_askpasswd_main.m
T
André Berg 50185248c7 Install gitx (the CLI) via Install.sh.
To do this we use a modified gitx_askpasswd to ask for sudo permissions during build.

Modifications to gitx_askpasswd include:

- Ability to specify the dialog info text (or title) via STDIN or a GITX_ASKPASSWD_DIALOG_TITLE env variable.
  Install.sh uses the env var in 'sudo -A -E' to kindly request the password.
  
  The point of this is to provide a sensible title which lets the user/dev know why he has to enter his password in a dialog that is obviously not from Mac OS X. 

Install.sh will now also create the folder hierarchy needed for the install paths set in Install.xcconfig using 'sudo -A -E' if neccessary (if the folders don't exist).
2010-03-26 11:33:32 +01:00

175 lines
4.8 KiB
Objective-C

/*
* gitx_askpasswd_main.m
* GitX
*
* Created by Uli Kusterer on 19.02.10.
* Copyright 2010 The Void Software. All rights reserved.
*
*/
#include <ApplicationServices/ApplicationServices.h>
#import <AppKit/AppKit.h>
#define OKBUTTONWIDTH 100.0
#define OKBUTTONHEIGHT 24.0
#define CANCELBUTTONWIDTH 100.0
#define CANCELBUTTONHEIGHT 24.0
#define PASSHEIGHT 22.0
#define PASSLABELHEIGHT 16.0
@interface GAPAppDelegate : NSObject
{
NSPanel* mPasswordPanel;
NSSecureTextField* mPasswordField;
}
-(NSPanel*) passwordPanel:(NSString *)title;
-(IBAction) doOKButton: (id)sender;
-(IBAction) doCancelButton: (id)sender;
@end
@implementation GAPAppDelegate
-(void) dealloc
{
[mPasswordPanel release];
mPasswordPanel = nil;
[mPasswordField release];
mPasswordField = nil;
[super dealloc];
}
-(NSPanel*) passwordPanel:(NSString *)title
{
if( !mPasswordPanel )
{
NSRect box = NSMakeRect( 100, 100, 250, 100 );
mPasswordPanel = [[NSPanel alloc] initWithContentRect: box
styleMask: NSTitledWindowMask
backing: NSBackingStoreBuffered
defer: NO];
[mPasswordPanel setHidesOnDeactivate: NO];
[mPasswordPanel setLevel: NSFloatingWindowLevel];
[mPasswordPanel center];
box.origin = NSZeroPoint; // Only need local coords from now on.
// OK:
NSRect okBox = box;
okBox.origin.x = NSMaxX( box ) -OKBUTTONWIDTH -10;
okBox.size.width = OKBUTTONWIDTH;
okBox.origin.y += 10;
okBox.size.height = OKBUTTONHEIGHT;
NSButton* okButton = [[[NSButton alloc] initWithFrame: okBox] autorelease];
[okButton setTarget: self];
[okButton setAction: @selector(doOKButton:)];
[okButton setTitle: @"OK"]; // +++ Localize.
[okButton setKeyEquivalent: @"\r"];
[okButton setBordered: YES];
[okButton setBezelStyle: NSRoundedBezelStyle];
[[mPasswordPanel contentView] addSubview: okButton];
// Cancel:
NSRect cancelBox = box;
cancelBox.origin.x = NSMinX( okBox ) -CANCELBUTTONWIDTH -6;
cancelBox.size.width = CANCELBUTTONWIDTH;
cancelBox.origin.y += 10;
cancelBox.size.height = CANCELBUTTONHEIGHT;
okButton = [[[NSButton alloc] initWithFrame: cancelBox] autorelease];
[okButton setTarget: self];
[okButton setAction: @selector(doCancelButton:)];
[okButton setTitle: @"Cancel"]; // +++ Localize.
[okButton setBordered: YES];
[okButton setBezelStyle: NSRoundedBezelStyle];
[[mPasswordPanel contentView] addSubview: okButton];
// Password field:
NSRect passBox = box;
passBox.origin.y = NSMaxY(okBox) + 12;
passBox.size.height = PASSHEIGHT;
passBox.origin.x += 12;
passBox.size.width -= 12 * 2;
mPasswordField = [[NSSecureTextField alloc] initWithFrame: passBox];
[mPasswordField setSelectable: YES];
[mPasswordField setEditable: YES];
[mPasswordField setBordered: YES];
[mPasswordField setBezeled: YES];
[mPasswordField setBezelStyle: NSTextFieldSquareBezel];
[mPasswordField selectText: self];
[[mPasswordPanel contentView] addSubview: mPasswordField];
// Password label:
NSRect passLabelBox = box;
passLabelBox.origin.y = NSMaxY(passBox) + 6;
passLabelBox.size.height = PASSLABELHEIGHT;
passLabelBox.origin.x += 12;
passLabelBox.size.width -= 12 * 2;
NSTextField* passwordLabel = [[[NSTextField alloc] initWithFrame: passLabelBox] autorelease];
[passwordLabel setSelectable: YES];
[passwordLabel setEditable: NO];
[passwordLabel setBordered: NO];
[passwordLabel setBezeled: NO];
[passwordLabel setDrawsBackground: NO];
[passwordLabel setStringValue: title]; // +++ Localize.
[[mPasswordPanel contentView] addSubview: passwordLabel];
}
return mPasswordPanel;
}
-(IBAction) doOKButton: (id)sender
{
printf( "%s\n", [[mPasswordField stringValue] UTF8String] );
[[NSApplication sharedApplication] terminate: self];
}
-(IBAction) doCancelButton: (id)sender
{
printf("\n");
[[NSApplication sharedApplication] terminate: self];
}
@end
int main( int argc, const char** argv )
{
ProcessSerialNumber myPSN = { 0, kCurrentProcess };
TransformProcessType( &myPSN, kProcessTransformToForegroundApplication );
NSApplication * app = [NSApplication sharedApplication];
GAPAppDelegate * appDel = [[GAPAppDelegate alloc] init];
[app setDelegate: appDel];
NSString * title;
const char * envtitle = getenv("GITX_ASKPASSWD_DIALOG_TITLE");
if (envtitle != NULL) {
title = [NSString stringWithUTF8String:envtitle];
} else {
if (argc > 1) {
title = [NSString stringWithUTF8String:argv[1]];
} else {
title = @"Please enter your password:";
}
}
NSWindow* passPanel = [appDel passwordPanel:title];
[app activateIgnoringOtherApps: YES];
[passPanel makeKeyAndOrderFront: nil];
[app runModalForWindow: passPanel];
return 0;
}