diff options
author | Teddy Wing | 2018-10-25 19:55:21 +0200 |
---|---|---|
committer | Teddy Wing | 2018-10-25 19:55:21 +0200 |
commit | b854b50348c566cb1c3fb3f8f76610e90492bae6 (patch) | |
tree | aeee2a39a4d4d3c019f86a2d897a358bbc73f281 | |
parent | 0c25fdc79f6aeec643c9c91caeaacbc0813d206c (diff) | |
download | DomeKey-b854b50348c566cb1c3fb3f8f76610e90492bae6.tar.bz2 |
LicenseHandler: Implement `addLicense:` method
Copies the given license into XDG_DATA_HOME and validates it. This
allows us to keep the user's license in a consistent location where it
can be validated on every launch.
Errors are printed to stderr. If the license file fails validation, it
gets moved to the trash. The original license file passed in by the user
will remain intact, only the one copied to XDG_DATA_HOME gets removed.
Call the function from `main` with a temporary hard-coded value to test
out the function. The real path value will come from a command-line
argument.
We may end up removing the `validateLicense` call and subsequent related
code here if it always gets called anyway. Still not sure if it should
look like:
if (--license passed in) {
[LicenseHandler addLicense:l];
}
else {
[LicenseHandler check];
}
or
if (--license passed in) {
[LicenseHandler addLicense:l];
}
[LicenseHandler check];
If the second, we should keep some kind of trashing functionality as
otherwise a subsequent `addLicense:` call will result in an error like
this:
dome-key: error: “dome-key-license.plist” couldn’t be copied to
“dome-key” because an item with the same name already exists.
TODO: `trashItemAtURL:resultingItemURL:error:` was introduced in OS X
10.8. We need to change this to use an `NSWorkspace` method.
-rw-r--r-- | DomeKey/LicenseHandler.h | 2 | ||||
-rw-r--r-- | DomeKey/LicenseHandler.m | 32 | ||||
-rw-r--r-- | DomeKey/main.m | 3 |
3 files changed, 34 insertions, 3 deletions
diff --git a/DomeKey/LicenseHandler.h b/DomeKey/LicenseHandler.h index fb3ab51..4d86923 100644 --- a/DomeKey/LicenseHandler.h +++ b/DomeKey/LicenseHandler.h @@ -15,6 +15,6 @@ @interface LicenseHandler : NSObject + (void)check; -+ (void)addLicense; ++ (void)addLicense:(NSString *)filePath; @end diff --git a/DomeKey/LicenseHandler.m b/DomeKey/LicenseHandler.m index 1bad0b8..021c7d4 100644 --- a/DomeKey/LicenseHandler.m +++ b/DomeKey/LicenseHandler.m @@ -31,8 +31,38 @@ static NSString * const LICENSE_FILE_NAME = @"license.plist"; } } -+ (void)addLicense ++ (void)addLicense:(NSString *)filePath { + // printf("current directory: %s\n", [[[NSFileManager defaultManager] currentDirectoryPath] UTF8String]); + + NSError *error = nil; + + // Copy license file to XDG_DATA_HOME + BOOL copied = [[NSFileManager defaultManager] + copyItemAtPath:[filePath stringByExpandingTildeInPath] + toPath:[[self licensePath] path] + error:&error]; + + if (!copied) { + eprintf("%s\n", [[error localizedDescription] UTF8String]); + } + + BOOL validated = [self validateLicense]; + + if (validated) { + printf("Thank you for registering DomeKey!\n"); + } + else { + BOOL trashed = [[NSFileManager defaultManager] + trashItemAtURL:[self licensePath] + resultingItemURL:nil + error:&error]; + + if (!trashed) { + eprintf("%s\n", [[error localizedDescription] UTF8String]); + } + } + // Copy license file into path // Validate license // If license doesn't validate, remove copied file diff --git a/DomeKey/main.m b/DomeKey/main.m index 2263bac..5daf4b8 100644 --- a/DomeKey/main.m +++ b/DomeKey/main.m @@ -23,7 +23,8 @@ int main(int argc, const char * argv[]) { config = c_parse_args(argv, argc, config); - [LicenseHandler check]; + [LicenseHandler addLicense:@"dome-key-license.plist"]; + // [LicenseHandler check]; if (config->args.reload) { return [Mappings dispatchReload]; |