Skip to content

Commit

Permalink
Auto Shutdown MAC
Browse files Browse the repository at this point in the history
  • Loading branch information
vineetchoudhary committed Sep 7, 2016
1 parent 121ad58 commit 34659bc
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
1 change: 1 addition & 0 deletions AppBox/Common/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

@interface Common : NSObject

+ (void)shutdownSystem;
+ (NSString*)generateUUID;
+ (NSModalResponse)showAlertWithTitle:(NSString *)title andMessage:(NSString *)message;
+ (void)showLocalNotificationWithTitle:(NSString *)title andMessage:(NSString *)message;
Expand Down
23 changes: 18 additions & 5 deletions AppBox/Common/Common.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ @implementation Common

+ (NSString*)generateUUID {
NSMutableData *data = [NSMutableData dataWithLength:32];
int result = SecRandomCopyBytes(NULL, 32, data.mutableBytes);
NSAssert(result == 0, @"Error generating random bytes: %d", errno);
NSString *base64EncodedData = [data base64EncodedStringWithOptions:0];
base64EncodedData = [base64EncodedData stringByReplacingOccurrencesOfString:@"/" withString:@""];
return base64EncodedData;
if (((NSInteger *)SecRandomCopyBytes(NULL, 32, data.mutableBytes)) == 0){
return @"";
}else{
NSString *base64EncodedData = [data base64EncodedStringWithOptions:0];
base64EncodedData = [base64EncodedData stringByReplacingOccurrencesOfString:@"/" withString:@""];
return base64EncodedData;
}
}

#pragma mark - Notifications
Expand Down Expand Up @@ -60,4 +62,15 @@ + (void)sendEmailToAddress:(NSString *)address withSubject:(NSString *)subject a
[emailScript executeAndReturnError:nil];
NSLog(@"Message passed to Mail");
}

#pragma mark - Handle System
+ (void)shutdownSystem{
NSString *scriptAction = @"shut down"; // @"restart"/@"shut down"/@"sleep"/@"log out"
NSString *scriptSource = [NSString stringWithFormat:@"tell application \"Finder\" to %@", scriptAction];
NSAppleScript *appleScript = [[NSAppleScript alloc] initWithSource:scriptSource];
NSDictionary *errDict = nil;
if (![appleScript executeAndReturnError:&errDict]) {
NSLog(@"%@", errDict);
}
}
@end
2 changes: 1 addition & 1 deletion AppBox/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>0.2</string>
<string>0.5</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleURLTypes</key>
Expand Down
12 changes: 9 additions & 3 deletions AppBox/ViewController/HomeViewController/HomeViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,15 @@ -(void)restClient:(DBRestClient *)restClient loadedSharableLink:(NSString *)link
if (textFieldEmail.stringValue.length > 0) {
[Common sendEmailToAddress:textFieldEmail.stringValue withSubject:textFieldEmailSubject.stringValue andBody:[NSString stringWithFormat:@"%@\n\n%@\n\n---\n%@",textViewEmailContent.string,shortURL.absoluteString,@"Build generated and distributed by AppBox - http://bit.ly/GetAppBox"]];
}
dispatch_async(dispatch_get_main_queue(), ^{
[self performSegueWithIdentifier:@"ShowLink" sender:self];
});
if (buttonShutdownMac.state == NSOffState){
dispatch_async(dispatch_get_main_queue(), ^{
[self performSegueWithIdentifier:@"ShowLink" sender:self];
});
}else{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(600 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[Common shutdownSystem];
});
}
[self progressCompletedViewState];
}];
}
Expand Down

0 comments on commit 34659bc

Please sign in to comment.