// // MPT_Demo.m // AUCoderDemo // // Created by Andrew Salamon on 10/5/07. // Copyright 2007 Machine Perception Laboratory, University of California San Diego. All rights reserved. // #import "MPT_Demo.h" #import #import "DemoAndLicensing.h" #import "CERT_License.h" static MPT_Demo *sharedDemo = nil; @interface MPT_Demo(PrivateMethods) - (NSCalendarDate *)dateFromBoost:(const bg::date &)dt; @end @implementation MPT_Demo + (MPT_Demo *)sharedDemo { if( !sharedDemo ) { sharedDemo = [[MPT_Demo alloc] init]; } return sharedDemo; } - (id)init { if( self = [super init] ) { imp = new CERT::DemoAndLicensing(); NSString *licensePath = [[NSBundle mainBundle] pathForResource:@"License" ofType:@"mptlic"]; if( [licensePath length] > 0 ) license = new CERT::License( [licensePath UTF8String] ); else license = new CERT::License(); if( lic_evaluation == [self type] ) { unsigned int dur = ((CERT::License *)license)->getDuration(); if( dur > 0 ) [self setDemoLength:dur]; } // [self setFilename:@".mplab2"]; // This is a hack to fix the fact that CERT::DemoAndLicensing doesn't properly handle ~'s in paths. // So we get the default set of paths, expand them (including ~ expansion) then set the expaneded versions back again. [self setAllowedPaths:[self allowedPaths]]; } return self; } - (void)dealloc { if( imp ) delete ((CERT::DemoAndLicensing *)imp); [super dealloc]; } - (LicenseType)type { int _type = ((CERT::License *)license)->getType(); if( CERT::License::internal == _type ) return lic_internal; else if( CERT::License::evaluation == _type) return lic_evaluation; else if( CERT::License::licensed == _type) return lic_licensed; else return lic_invalid; } - (BOOL)isEvaluation { return (lic_evaluation == [self type]); } - (BOOL)isLicensed { return (lic_licensed == [self type]); } - (BOOL)isInternal { return (lic_internal == [self type]); } - (BOOL)startDemo { return ((CERT::DemoAndLicensing *)imp)->startDemo(); } - (BOOL)isDemo { return ((CERT::DemoAndLicensing *)imp)->isDemo(); } - (BOOL)hasDemoEnded { return ((CERT::DemoAndLicensing *)imp)->hasDemoEnded(); } - (NSString *)filename { std::string tmp( ((CERT::DemoAndLicensing *)imp)->getFilename() ); return [NSString stringWithCString:tmp.c_str() length:tmp.length()]; } - (void)setFilename:(NSString *)value { std::string dir( ([value length]?[value UTF8String]:"") ); ((CERT::DemoAndLicensing *)imp)->setFilename( dir ); } - (NSArray *)allowedPaths { NSMutableArray *pathsArray = [NSMutableArray array]; std::vector paths = ((CERT::DemoAndLicensing *)imp)->getAllowedPaths(); for( std::vector::iterator iter = paths.begin(); iter != paths.end(); ++iter ) { [pathsArray addObject:[NSString stringWithCString:iter->c_str() length:iter->length()]]; } return [[pathsArray copy] autorelease]; } - (void)setAllowedPaths:(NSArray *)value { std::vector newPaths; NSEnumerator *pathEnum = [value objectEnumerator]; NSString *path; while( path = [pathEnum nextObject] ) { path = [[[path stringByExpandingTildeInPath] stringByResolvingSymlinksInPath] stringByStandardizingPath]; std::string pathStr( ([path length]?[path UTF8String]:"") ); newPaths.push_back( pathStr ); } ((CERT::DemoAndLicensing *)imp)->setAllowedPaths( newPaths ); } - (unsigned int)demoLength { return ((CERT::DemoAndLicensing *)imp)->getDemoLength(); } - (void)setDemoLength:(unsigned int)days { ((CERT::DemoAndLicensing *)imp)->setDemoLength( days ); } - (NSCalendarDate *)demoStartDate { return [self dateFromBoost:((CERT::DemoAndLicensing *)imp)->demoStartDate()]; } - (void)notifyUserDemoEnded { NSString *msg = @"The CERT evaluation period has ended."; NSAlert *alert = [NSAlert alertWithMessageText:msg defaultButton:@"Quit" alternateButton:nil otherButton:nil informativeTextWithFormat:@"Please contact MPLab if you have any questions:\nhttp://mplab.ucsd.edu"]; [alert runModal]; } - (void)notifyUserDemoNotAllowed { NSString *msg = @"Unable to start the CERT evaluation."; NSAlert *alert = [NSAlert alertWithMessageText:msg defaultButton:@"Quit" alternateButton:nil otherButton:nil informativeTextWithFormat:@"Please contact MPLab if you have any questions:\nhttp://mplab.ucsd.edu"]; [alert runModal]; } - (NSCalendarDate *)dateFromBoost:(const bg::date &)dt { tm timeStruct = bg::to_tm( dt ); time_t timeInt = mktime( &timeStruct ); return [NSCalendarDate dateWithTimeIntervalSince1970:timeInt]; } - (void)notifyUserDemoLeft { bg::date endDate = ((CERT::DemoAndLicensing *)imp)->demoEndDate(); tm timeStruct = bg::to_tm( endDate ); time_t timeInt = mktime( &timeStruct ); NSCalendarDate *calDate = [NSCalendarDate dateWithTimeIntervalSince1970:timeInt]; NSString *msg = @"This is an evaluation verision of CERT."; NSString *dateMsg = @"The evaluation period will end %@."; NSAlert *alert = [NSAlert alertWithMessageText:msg defaultButton:@"Continue" alternateButton:nil otherButton:nil informativeTextWithFormat:dateMsg, [calDate descriptionWithCalendarFormat:@"%A %B %e, %Y"]]; [alert runModal]; } - (void)notifyUserInvalidLicense { NSString *msg = @"No valid CERT license was found."; NSAlert *alert = [NSAlert alertWithMessageText:msg defaultButton:@"Quit" alternateButton:nil otherButton:nil informativeTextWithFormat:@"Please contact UCSD Tech Transfer for a new license or to address any questions.\nhttp://invent.ucsd.edu/index_flash.htm"]; [alert runModal]; } - (BOOL)licenseHasEnded { int daysLeft = ((CERT::License *)license)->daysLeft(); return (daysLeft <= 0); } - (void)notifyUserLicenseEnded { bg::date startDate = ((CERT::License *)license)->getStartDate(); bg::date today = bg::day_clock::local_day(); if( startDate > today ) { NSCalendarDate *dt = [self dateFromBoost:((CERT::License *)license)->getEndDate()]; NSString *msg = @"The CERT license period has not yet started."; NSAlert *alert = [NSAlert alertWithMessageText:msg defaultButton:@"Quit" alternateButton:nil otherButton:nil informativeTextWithFormat:@"The license will begin on %@.\nPlease contact UCSD Tech Transfer to address any questions.\nhttp://invent.ucsd.edu/index_flash.htm", [dt descriptionWithCalendarFormat:@"%A %B %e, %Y"]]; [alert runModal]; } else { NSString *msg = @"The CERT license period has ended."; NSAlert *alert = [NSAlert alertWithMessageText:msg defaultButton:@"Quit" alternateButton:nil otherButton:nil informativeTextWithFormat:@"Please contact UCSD Tech Transfer for a new license or to address any questions.\nhttp://invent.ucsd.edu/index_flash.htm"]; [alert runModal]; } } - (void)notifyUserLicenseInfo { NSCalendarDate *dt = [self dateFromBoost:((CERT::License *)license)->getEndDate()]; NSString *msg = [NSString stringWithFormat:@"This version of CERT is licensed to:\n\t%s.", ((CERT::License *)license)->getName().c_str()]; NSString *dateMsg = @"The license is valid through %@."; // This version of CERT is licensed to [insert]. The license is valid through [insert]. NSAlert *alert = [NSAlert alertWithMessageText:msg defaultButton:@"Continue" alternateButton:nil otherButton:nil informativeTextWithFormat:dateMsg, [dt descriptionWithCalendarFormat:@"%A %B %e, %Y"]]; [alert runModal]; } @end