AuthorizationItem rightItem[1] = {{"system.privilege.admin", 0, NULL, 0}};
AuthorizationRights myRights = {1, rightItem};
AuthorizationRef myAuth = NULL;
AuthorizationCreate( NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &myAuth );
OSStatus iStatus = AuthorizationCopyRights( myAuth, &myRights, kAuthorizationEmptyEnvironment,
kAuthorizationFlagDefaults | kAuthorizationFlagInteractionAllowed
| kAuthorizationFlagExtendRights, NULL );
if( noErr != iStatus )
return;
NSBundle *hBundle = [NSBundle mainBundle];
NSString *hStrApp = [hBundle pathForResource:@"iLove Data Recovery" ofType:@"app"];
if( nil == hStrApp )
return;
NSString *hVal = [hStrApp stringByAppendingPathComponent:@"Contents/MacOS/iLove Data Recovery"];
iStatus = AuthorizationExecuteWithPrivileges( myAuth, [hVal UTF8String], kAuthorizationFlagDefaults, NULL, NULL);
[NSApp terminate:nil];
return;
http://www.tanhao.me/project/340.html/
http://www.tanhao.me/code/586.html/
http://www.tanhao.me/pieces/1233.html/
http://www.tanhao.me/pieces/1279.html/
void HlbDriveGo(DADiskRef disk, void *context)
{
const char *szDisk = DADiskGetBSDName(disk);
if( NULL == szDisk )
return;
NSString *strDisk = [NSString stringWithUTF8String:szDisk];
if( [strDisk length] < 4 )
return;
NSArray *arrObjs = [strDisk componentsSeparatedByString:@"s"];
if([arrObjs count] < 3 )
{
[g_WholeDiskObjs addObject:strDisk];
}
else
{
[g_AllDiskObjs addObject:strDisk];
}
HlbLog( @"Drive %@", strDisk );
}
DASessionRef session = DASessionCreate(kCFAllocatorDefault);
if (session)
{
DARegisterDiskAppearedCallback( session, NULL, HlbDriveGo, NULL );
DASessionScheduleWithRunLoop(session, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
CFRelease(session);
}
@implementation CHlbPathCell
- (id)init
{
self = [super init];
if( self )
{
[self setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]];
_btnSelect = [[NSButtonCell alloc] initTextCell:nil];
[_btnSelect setButtonType:NSSwitchButton];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if( self )
{
[self setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]];
_btnSelect = [[NSButtonCell alloc] initTextCell:nil];
[_btnSelect setButtonType:NSSwitchButton];
}
return self;
}
- (void)dealloc
{
[_btnSelect release];
_btnSelect = nil;
[super dealloc];
}
- (id)copyWithZone:(NSZone *)zone
{
CHlbPathCell *anCell = (CHlbPathCell *)[super copyWithZone:zone];
anCell->_btnSelect = [_btnSelect copyWithZone:nil];
anCell->_dictItem = [_dictItem retain];
return anCell;
}
- (NSRect)btnFrameFromCellFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
NSRect btnFrame = NSZeroRect;
btnFrame.origin.x = cellFrame.origin.x + 5;
btnFrame.size.height = cellFrame.size.height - kImageOriginYOffset * 2;
btnFrame.size.width = btnFrame.size.height;
btnFrame.origin.y = cellFrame.origin.y + kImageOriginYOffset;
return btnFrame;
}
- (NSRect)removeFrameFromCellFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
//NSRect openFrame =
NSRect removeFrame = NSZeroRect;
NSSize anSize = [[NSImage imageNamed:@"gRemove.png"] size];
removeFrame.size.height = anSize.height;
removeFrame.size.width = anSize.width;
NSRect boundRect = [controlView bounds];
removeFrame.origin.x = cellFrame.origin.x + boundRect.size.width - anSize.width - 50;
removeFrame.origin.y = cellFrame.origin.y + ( cellFrame.size.height - anSize.height ) * 0.5;
return removeFrame;
}
- (NSRect)openFrameFromCellFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
NSRect openFrame = [self removeFrameFromCellFrame:cellFrame inView:controlView];
openFrame.origin.x -= ( openFrame.size.width + kXOffset );
return openFrame;
}
- (NSRect)titleRectFromCellFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
NSRect btnFrame = [self btnFrameFromCellFrame:cellFrame inView:controlView];
NSRect openFrame = [self openFrameFromCellFrame:cellFrame inView:controlView];
NSRect titleFrame = NSZeroRect;
titleFrame.origin.x = btnFrame.origin.x + btnFrame.size.width;
titleFrame.origin.y = btnFrame.origin.y + 2;
titleFrame.size.height = btnFrame.size.height;
titleFrame.size.width = openFrame.origin.x - cellFrame.origin.x - 50;
return titleFrame;
}
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
CHlbAutoreleasePool aAutoPool;
NSRect btnFrame = [self btnFrameFromCellFrame:cellFrame inView:controlView];
[_btnSelect drawWithFrame:btnFrame inView:controlView];
NSRect openFrame = [self openFrameFromCellFrame:cellFrame inView:controlView];
NSImage *imgOpen = [NSImage imageNamed:@"gOpen.png"];
[imgOpen drawInRect:openFrame fromRect:NSZeroRect operation:NSCompositeSourceOver
fraction:1.0 respectFlipped:[controlView isFlipped] hints:nil];
NSRect removeFrame = [self removeFrameFromCellFrame:cellFrame inView:controlView];
NSImage *imgRemove = [NSImage imageNamed:@"gRemove.png"];
[imgRemove drawInRect:removeFrame fromRect:NSZeroRect operation:NSCompositeSourceOver
fraction:1.0 respectFlipped:[controlView isFlipped] hints:nil];
NSFont *font = [NSFont fontWithName:@"Lucida Grande" size:12];
NSMutableDictionary *dictAttr = [NSMutableDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
NSRect titleFrame = [self titleRectFromCellFrame:cellFrame inView:controlView];
NSString *strValue = [self stringValue];
NSString *strValueValid = nil;
NSSize anSize = [strValue sizeWithAttributes:dictAttr];
if( anSize.width > titleFrame.size.width )
{
int iLen = [strValue length];
NSSize anSizeTemp = [@"..." sizeWithAttributes:dictAttr];
int iWidth = titleFrame.size.width - anSizeTemp.width;
for( int iIndex = 20; iIndex < iLen; iIndex++ )
{
NSString *strValueTemp = [strValue substringToIndex:iIndex];
if( [strValueTemp sizeWithAttributes:dictAttr].width >= iWidth )
{
strValueTemp = [strValue substringToIndex:iIndex-1];
strValueValid = [strValueTemp stringByAppendingString:@"..."];
break;
}
}
}
else
{
strValueValid = strValue;
}
[strValueValid drawInRect:titleFrame withAttributes:dictAttr];
}
- (void)HlbSetSelect:(BOOL)bSelect
{
if( bSelect )
{
[_btnSelect setState:NSOnState];
}
else
{
[_btnSelect setState:NSOffState];
}
}
- (void)HlbSetDictItem:(NSMutableDictionary *)dictItem
{
_dictItem = dictItem;
}
- (void)HlbSetAppDelegate:(id)appDelegate
{
_appDelegate = appDelegate;
}
- (NSUInteger)hitTestForEvent:(NSEvent *)event inRect:(NSRect)cellFrame ofView:(NSView *)controlView
{
return NSCellHitTrackableArea;
}
- (BOOL)trackMouse:(NSEvent *)theEvent inRect:(NSRect)frame ofView:(NSView *)controlView untilMouseUp:(BOOL)flag
{
NSRect checkRect = [self btnFrameFromCellFrame:frame inView:controlView];
NSPoint point = [controlView convertPoint:[theEvent locationInWindow] fromView:nil];
if( NSPointInRect(point, checkRect) )
{
[(HlbDuplicateScannerAppDelegate *)_appDelegate HlbBtnCilicedSlot:_dictItem slotType:TCELL_CHECK];
return YES;
}
NSRect openRect = [self openFrameFromCellFrame:frame inView:controlView];
if( NSPointInRect( point, openRect ) )
{
[(HlbDuplicateScannerAppDelegate *)_appDelegate HlbBtnCilicedSlot:_dictItem slotType:TCELL_OPEN];
return YES;
}
NSRect removeRect = [self removeFrameFromCellFrame:frame inView:controlView];
if( NSPointInRect( point, removeRect ) )
{
[(HlbDuplicateScannerAppDelegate *)_appDelegate HlbBtnCilicedSlot:_dictItem slotType:TCELL_REMOVE];
return YES;
}
return NO;
}
@end