project_files/HedgewarsMobile/Classes/MainMenuViewController.m
changeset 3923 694e6f6e0e30
parent 3903 db01c37494af
child 3971 5c82ee165ed5
--- a/project_files/HedgewarsMobile/Classes/MainMenuViewController.m	Sat Oct 02 04:23:32 2010 +0200
+++ b/project_files/HedgewarsMobile/Classes/MainMenuViewController.m	Sun Oct 03 00:23:05 2010 +0200
@@ -22,7 +22,6 @@
 #import "MainMenuViewController.h"
 #import "CommodityFunctions.h"
 #import "SDL_uikitappdelegate.h"
-#import "SDL_mixer.h"
 #import "PascalImports.h"
 #import "GameConfigViewController.h"
 #import "SplitViewRootController.h"
@@ -30,7 +29,7 @@
 #import "SavedGamesViewController.h"
 
 @implementation MainMenuViewController
-@synthesize versionLabel, gameConfigViewController, settingsViewController, aboutViewController, savedGamesViewController;
+@synthesize gameConfigViewController, settingsViewController, aboutViewController, savedGamesViewController;
 
 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
     return rotationManager(interfaceOrientation);
@@ -55,175 +54,106 @@
 
 // check if some configuration files are already set; if they are present it means that the current copy must be updated
 -(void) createNecessaryFiles {
-    BOOL doCreateFiles = NO;
+    NSError *err = nil;
+    NSString *directoryToCheck, *fileToCheck, *fileToUpdate;
     NSString *resDir = [[NSBundle mainBundle] resourcePath];
+    DLog(@"Creating necessary files");
     
-    NSString *versionFileToCheck = [NSString stringWithFormat:@"%@/version.txt",DOCUMENTS_FOLDER()];
-    if ([[NSFileManager defaultManager] fileExistsAtPath:versionFileToCheck]) {
-        NSString *currentVersion = [NSString stringWithContentsOfFile:versionFileToCheck encoding:NSUTF8StringEncoding error:nil];
-        NSString *newVersion = [NSString stringWithContentsOfFile:[NSString stringWithFormat:@"%@/Settings/version.txt",resDir] encoding:NSUTF8StringEncoding error:nil];
-        if ([currentVersion intValue] < [newVersion intValue]) {
-            doCreateFiles = YES;
-            [newVersion writeToFile:versionFileToCheck atomically:YES encoding:NSUTF8StringEncoding error:nil];
-        }
-    } else {
-        doCreateFiles = YES;
-        [[NSFileManager defaultManager] copyItemAtPath:[NSString stringWithFormat:@"%@/Settings/version.txt",resDir] toPath:versionFileToCheck error:nil];
-    } 
-
+    // create an empty saves directory by deleting the previous one (saves are incompatible between releases)
+    if ([[NSFileManager defaultManager] fileExistsAtPath:SAVES_DIRECTORY()])
+        [[NSFileManager defaultManager] removeItemAtPath:SAVES_DIRECTORY() error:NULL];
+    [[NSFileManager defaultManager] createDirectoryAtPath:SAVES_DIRECTORY() withIntermediateDirectories:NO attributes:nil error:NULL];
     
-    if (doCreateFiles == YES) {
-        NSError *err = nil;
-        NSString *directoryToCheck, *fileToCheck, *fileToUpdate;
-        DLog(@"Creating necessary files");
-        
-        // create an empty saves directory by deleting the previous one (saves are incompatible between releases)
-        if ([[NSFileManager defaultManager] fileExistsAtPath:SAVES_DIRECTORY()])
-            [[NSFileManager defaultManager] removeItemAtPath:SAVES_DIRECTORY() error:NULL];
-        [[NSFileManager defaultManager] createDirectoryAtPath:SAVES_DIRECTORY() withIntermediateDirectories:NO attributes:nil error:NULL];
-        
-        // if the settings file is already present, we merge current preferences with the update
-        directoryToCheck = [NSString stringWithFormat:@"%@/Settings/settings.plist",resDir];
-        if ([[NSFileManager defaultManager] fileExistsAtPath:SETTINGS_FILE()]) {
-            NSDictionary *settings = [[NSDictionary alloc] initWithContentsOfFile:SETTINGS_FILE()];
-            NSMutableDictionary *update = [[NSMutableDictionary alloc] initWithContentsOfFile:directoryToCheck];
-            [update addEntriesFromDictionary:settings];
-            [settings release];
-            [update writeToFile:SETTINGS_FILE() atomically:YES];
-            [update release];
-        } else 
-            [[NSFileManager defaultManager] copyItemAtPath:directoryToCheck toPath:SETTINGS_FILE() error:&err];
-        
-        // if the teams are already present we merge the old teams if they still exist
-        directoryToCheck = [NSString stringWithFormat:@"%@/Settings/Teams",resDir];
-        if ([[NSFileManager defaultManager] fileExistsAtPath:TEAMS_DIRECTORY()]) {
-            for (NSString *str in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryToCheck error:nil]) {
-                fileToCheck = [NSString stringWithFormat:@"%@/%@",TEAMS_DIRECTORY(),str];
-                fileToUpdate = [NSString stringWithFormat:@"%@/Settings/Teams/%@",resDir,str];
-                if ([[NSFileManager defaultManager] fileExistsAtPath:fileToCheck]) {
-                    NSDictionary *team = [[NSDictionary alloc] initWithContentsOfFile:fileToCheck];
-                    NSMutableDictionary *update = [[NSMutableDictionary alloc] initWithContentsOfFile:fileToUpdate];
-                    [update addEntriesFromDictionary:team];
-                    [team release];
-                    [update writeToFile:fileToCheck atomically:YES];
-                    [update release];
-                }
+    // if the settings file is already present, we merge current preferences with the update
+    directoryToCheck = [NSString stringWithFormat:@"%@/Settings/settings.plist",resDir];
+    if ([[NSFileManager defaultManager] fileExistsAtPath:SETTINGS_FILE()]) {
+        NSDictionary *settings = [[NSDictionary alloc] initWithContentsOfFile:SETTINGS_FILE()];
+        NSMutableDictionary *update = [[NSMutableDictionary alloc] initWithContentsOfFile:directoryToCheck];
+        [update addEntriesFromDictionary:settings];
+        [settings release];
+        [update writeToFile:SETTINGS_FILE() atomically:YES];
+        [update release];
+    } else 
+        [[NSFileManager defaultManager] copyItemAtPath:directoryToCheck toPath:SETTINGS_FILE() error:&err];
+    
+    // if the teams are already present we merge the old teams if they still exist
+    directoryToCheck = [NSString stringWithFormat:@"%@/Settings/Teams",resDir];
+    if ([[NSFileManager defaultManager] fileExistsAtPath:TEAMS_DIRECTORY()]) {
+        for (NSString *str in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryToCheck error:&err]) {
+            fileToCheck = [NSString stringWithFormat:@"%@/%@",TEAMS_DIRECTORY(),str];
+            fileToUpdate = [NSString stringWithFormat:@"%@/Settings/Teams/%@",resDir,str];
+            if ([[NSFileManager defaultManager] fileExistsAtPath:fileToCheck]) {
+                NSDictionary *team = [[NSDictionary alloc] initWithContentsOfFile:fileToCheck];
+                NSMutableDictionary *update = [[NSMutableDictionary alloc] initWithContentsOfFile:fileToUpdate];
+                [update addEntriesFromDictionary:team];
+                [team release];
+                [update writeToFile:fileToCheck atomically:YES];
+                [update release];
             }
-        } else
-            [[NSFileManager defaultManager] copyItemAtPath:directoryToCheck toPath:TEAMS_DIRECTORY() error:&err];
-
-        // the same holds for schemes (but they're arrays)
-        directoryToCheck = [NSString stringWithFormat:@"%@/Settings/Schemes",resDir];
-        if ([[NSFileManager defaultManager] fileExistsAtPath:SCHEMES_DIRECTORY()]) {
-            for (NSString *str in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryToCheck error:nil]) {
-                fileToCheck = [NSString stringWithFormat:@"%@/%@",SCHEMES_DIRECTORY(),str];
-                fileToUpdate = [NSString stringWithFormat:@"%@/Settings/Schemes/%@",resDir,str];
-                if ([[NSFileManager defaultManager] fileExistsAtPath:fileToCheck]) {
-                    NSArray *scheme = [[NSArray alloc] initWithContentsOfFile:fileToCheck];
-                    NSArray *update = [[NSArray alloc] initWithContentsOfFile:fileToUpdate];
-                    if ([update count] > [scheme count])
-                        [update writeToFile:fileToCheck atomically:YES];
-                    [update release];
-                    [scheme release];
-                }
+        }
+    } else
+        [[NSFileManager defaultManager] copyItemAtPath:directoryToCheck toPath:TEAMS_DIRECTORY() error:&err];
+    
+    // the same holds for schemes (but they're arrays)
+    directoryToCheck = [NSString stringWithFormat:@"%@/Settings/Schemes",resDir];
+    if ([[NSFileManager defaultManager] fileExistsAtPath:SCHEMES_DIRECTORY()]) {
+        for (NSString *str in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryToCheck error:nil]) {
+            fileToCheck = [NSString stringWithFormat:@"%@/%@",SCHEMES_DIRECTORY(),str];
+            fileToUpdate = [NSString stringWithFormat:@"%@/Settings/Schemes/%@",resDir,str];
+            if ([[NSFileManager defaultManager] fileExistsAtPath:fileToCheck]) {
+                NSArray *scheme = [[NSArray alloc] initWithContentsOfFile:fileToCheck];
+                NSArray *update = [[NSArray alloc] initWithContentsOfFile:fileToUpdate];
+                if ([update count] > [scheme count])
+                    [update writeToFile:fileToCheck atomically:YES];
+                [update release];
+                [scheme release];
             }
-        } else
-            [[NSFileManager defaultManager] copyItemAtPath:directoryToCheck toPath:SCHEMES_DIRECTORY() error:&err];
-        
-        // we create weapons the first time only, they are autoupdated each time
-        if ([[NSFileManager defaultManager] fileExistsAtPath:WEAPONS_DIRECTORY()] == NO) {
-            [[NSFileManager defaultManager] createDirectoryAtPath:WEAPONS_DIRECTORY()
-                                      withIntermediateDirectories:YES
-                                                       attributes:nil
-                                                            error:&err];
-            createWeaponNamed(@"Default", 0);
-            createWeaponNamed(@"Crazy", 1);
-            createWeaponNamed(@"Pro mode", 2);
-            createWeaponNamed(@"Shoppa", 3);
-            createWeaponNamed(@"Basketball", 4);
-            createWeaponNamed(@"Minefield", 5);
         }
-        
-        DLog(@"Success");
-        
-        if (err != nil) 
-            DLog(@"%@", err);
-    }
-}
-
-/* // ask the user to leave a review for this app
--(void) reviewCounter {
-    CGFloat reviewInt = [[NSUserDefaults standardUserDefaults] integerForKey: @"intValueKey"];
+    } else
+        [[NSFileManager defaultManager] copyItemAtPath:directoryToCheck toPath:SCHEMES_DIRECTORY() error:&err];
     
-    if (reviewInt) {
-        reviewInt++;
-        [[NSUserDefaults standardUserDefaults] setInteger:reviewInt forKey:@"intValueKey"];
-    } else {
-        CGFloat start = 1;
-        NSUserDefaults *reviewPrefs = [NSUserDefaults standardUserDefaults];
-        [reviewPrefs setInteger:start forKey: @"intValueKey"];
-        [reviewPrefs synchronize]; // writes modifications to disk
+    // we create weapons the first time only, they are autoupdated each time
+    if ([[NSFileManager defaultManager] fileExistsAtPath:WEAPONS_DIRECTORY()] == NO) {
+        [[NSFileManager defaultManager] createDirectoryAtPath:WEAPONS_DIRECTORY()
+                                  withIntermediateDirectories:YES
+                                                   attributes:nil
+                                                        error:&err];
+        createWeaponNamed(@"Default", 0);
+        createWeaponNamed(@"Crazy", 1);
+        createWeaponNamed(@"Pro mode", 2);
+        createWeaponNamed(@"Shoppa", 3);
+        createWeaponNamed(@"Basketball", 4);
+        createWeaponNamed(@"Minefield", 5);
     }
     
-    if (1) {
-        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Mabuhay!"
-                                                        message:@"Looks like you Enjoy using this app. Could you spare a moment of your time to review it in the AppStore?"
-                                                       delegate:self
-                                              cancelButtonTitle:nil
-                                              otherButtonTitles: @"OK, I'll Review It Now", @"Remind Me Later", @"Don't Remind Me", nil];
-        [alert show]; 
-        [alert release];
-        
-        reviewInt++;
-        
-        [[NSUserDefaults standardUserDefaults] setInteger:reviewInt forKey:@"intValueKey"];
-    }
+    if (err != nil) 
+        DLog(@"%@", err);
+    else
+        DLog(@"Success");
 }
 
 #pragma mark -
-#pragma mark alert view delegate
--(void) alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger) buttonIndex {
-    // the user clicked one of the OK/Cancel buttons
-    if (buttonIndex == 0) {
-        NSString *str = @"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa";
-        str = [NSString stringWithFormat:@"%@/wa/viewContentsUserReviews?", str]; 
-        str = [NSString stringWithFormat:@"%@type=Vittorio+Giovara&id=", str];
-        
-        // Here is the app id from itunesconnect
-        str = [NSString stringWithFormat:@"%@391234866", str]; 
-        
-        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=391234866&onlyLatestVersion=true&pageNumber=0&sortOrdering=1&type=Purple+Software"]]; 
-    } else if (buttonIndex == 1) {
-        int startAgain = 0;
-        [[NSUserDefaults standardUserDefaults] setInteger:startAgain forKey:@"intValueKey"];
-        
-    } else if (buttonIndex == 2) { 
-        int neverRemind = 4;
-        [[NSUserDefaults standardUserDefaults] setInteger:neverRemind forKey:@"intValueKey"];
-    }
-} */
+-(void) viewDidLoad {
+    [super viewDidLoad];
 
-#pragma mark -
--(void) viewDidLoad {
-    [NSThread detachNewThreadSelector:@selector(initAudioThread)
-                             toTarget:self
-                           withObject:nil];
-
-    char *ver;
-    HW_versionInfo(NULL, &ver);
-    NSString *versionNumber = [[NSString alloc] initWithCString:ver];
-    self.versionLabel.text = @"";//versionNumber;
-    [versionNumber release];
-
-    // listen to request to remove the modalviewcontroller
+    // listen to request to remove the modalviewcontroller (needed due to the splitcontroller)
     [[NSNotificationCenter defaultCenter] addObserver:self
                                              selector:@selector(dismissModalViewController)
                                                  name: @"dismissModalView"
                                                object:nil];
 
-    [self createNecessaryFiles];
-    
-    [super viewDidLoad];
+    // get the app's version
+    NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleVersionKey];
+
+    // get the version number that we've been tracking
+    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
+    NSString *trackingVersion = [userDefaults stringForKey:@"HedgeVersion"];
+
+    if (trackingVersion == nil || [trackingVersion isEqualToString:version] == NO) {
+        [userDefaults setObject:version forKey:@"HedgeVersion"];
+        [userDefaults synchronize];
+        [self createNecessaryFiles];
+    }
 }
 
 
@@ -318,7 +248,6 @@
 
 -(void) viewDidUnload {
     [[NSNotificationCenter defaultCenter] removeObserver:self];
-    self.versionLabel = nil;
     self.gameConfigViewController = nil;
     self.settingsViewController = nil;
     self.aboutViewController = nil;
@@ -328,7 +257,6 @@
 }
 
 -(void) dealloc {
-    [versionLabel release];
     [settingsViewController release];
     [gameConfigViewController release];
     [aboutViewController release];