diff --git a/PCLib/PCProjectManager.h b/PCLib/PCProjectManager.h index fdb0af0..cf3d19b 100644 --- a/PCLib/PCProjectManager.h +++ b/PCLib/PCProjectManager.h @@ -43,16 +43,12 @@ NSString *rootBuildPath; + NSTimer *saveTimer; + @private BOOL _needsReleasing; } -// =========================================================================== -// ==== Class files -// =========================================================================== - -+ (void)initialize; - // =========================================================================== // ==== Intialization & deallocation // =========================================================================== @@ -67,24 +63,33 @@ - (id)delegate; - (void)setDelegate:(id)aDelegate; +// =========================================================================== +// ==== Timer handling +// =========================================================================== + +- (void)resetSaveTimer:(NSNotification *)notif; + // =========================================================================== // ==== Project management // =========================================================================== - (NSMutableDictionary *)loadedProjects; - // Returns all currently loaded projects. They are stored with their absolut paths as the keys. + // Returns all currently loaded projects. They are stored with their absolut paths as the keys. - (PCProject *)activeProject; - // Returns the currently active project + // Returns the currently active project - (void)setActiveProject:(PCProject *)aProject; -// Sets the new currently active project + // Sets the new currently active project + +- (void)saveAllProjectsIfNeeded; + // Calls saveAllProjects if the preferences are setup accordingly. - (void)saveAllProjects; - // Saves all projects if needed. + // Saves all projects if needed. - (NSString *)rootBuildPath; - // Gets set while initialising! + // Gets set while initialising! // =========================================================================== // ==== Project actions diff --git a/PCLib/PCProjectManager.m b/PCLib/PCProjectManager.m index 0c5a765..e347581 100644 --- a/PCLib/PCProjectManager.m +++ b/PCLib/PCProjectManager.m @@ -31,6 +31,8 @@ #import #endif +#define SavePeriodDCN @"SavePeriodDidChangeNotification" + NSString *ActiveProjectDidChangeNotification = @"ActiveProjectDidChange"; @interface PCProjectManager (CreateUI) @@ -89,14 +91,6 @@ NSString *ActiveProjectDidChangeNotification = @"ActiveProjectDidChange"; @implementation PCProjectManager -// =========================================================================== -// ==== Class methods -// =========================================================================== - -+ (void)initialize -{ -} - // =========================================================================== // ==== Intialization & deallocation // =========================================================================== @@ -104,12 +98,31 @@ NSString *ActiveProjectDidChangeNotification = @"ActiveProjectDidChange"; - (id)init { if ((self = [super init])) { + NSUserDefaults *defs = [NSUserDefaults standardUserDefaults]; + SEL sall = @selector(saveAllProjectsIfNeeded); + SEL spdc = @selector(resetSaveTimer:); + NSTimeInterval interval = [[defs objectForKey:AutoSavePeriod] intValue]; + loadedProjects = [[NSMutableDictionary alloc] init]; - rootBuildPath = [[[NSUserDefaults standardUserDefaults] stringForKey:RootBuildDirectory] copy]; - if (!rootBuildPath || rootBuildPath == @"") { + rootBuildPath = [[defs stringForKey:RootBuildDirectory] copy]; + if (!rootBuildPath || [rootBuildPath isEqualToString:@""]) { rootBuildPath = [NSTemporaryDirectory() copy]; } + + if( [[defs objectForKey:AutoSave] isEqualToString:@"YES"] ) { + saveTimer = [NSTimer scheduledTimerWithTimeInterval:interval + target:self + selector:sall + userInfo:nil + repeats:YES]; + } + + [[NSNotificationCenter defaultCenter] addObserver:self + selector:spdc + name:SavePeriodDCN + object:nil]; + _needsReleasing = NO; } return self; @@ -117,16 +130,24 @@ NSString *ActiveProjectDidChangeNotification = @"ActiveProjectDidChange"; - (void)dealloc { - [rootBuildPath release]; - [loadedProjects release]; + [rootBuildPath release]; + [loadedProjects release]; + + if( [saveTimer isValid] ) + { + [saveTimer invalidate]; + } + + [[NSNotificationCenter defaultCenter] removeObserver:self]; - if (_needsReleasing) { - [inspector release]; - [inspectorView release]; - [inspectorPopup release]; - } + if (_needsReleasing) + { + [inspector release]; + [inspectorView release]; + [inspectorPopup release]; + } - [super dealloc]; + [super dealloc]; } // =========================================================================== @@ -143,6 +164,27 @@ NSString *ActiveProjectDidChangeNotification = @"ActiveProjectDidChange"; delegate = aDelegate; } +// =========================================================================== +// ==== Timer handling +// =========================================================================== + +- (void)resetSaveTimer:(NSNotification *)notif +{ + NSTimeInterval interval = [[notif object] intValue]; + SEL sall = @selector(saveAllProjectsIfNeeded); + + if( [saveTimer isValid] ) + { + [saveTimer invalidate]; + } + + saveTimer = [NSTimer scheduledTimerWithTimeInterval:interval + target:self + selector:sall + userInfo:nil + repeats:YES]; +} + // =========================================================================== // ==== Project management // =========================================================================== @@ -159,20 +201,38 @@ NSString *ActiveProjectDidChangeNotification = @"ActiveProjectDidChange"; - (void)setActiveProject:(PCProject *)aProject { - if (aProject != activeProject) { - activeProject = aProject; + if (aProject != activeProject) { + activeProject = aProject; - [[NSNotificationCenter defaultCenter] postNotificationName:ActiveProjectDidChangeNotification object:activeProject]; + [[NSNotificationCenter defaultCenter] postNotificationName:ActiveProjectDidChangeNotification object:activeProject]; - //~ Is this needed? - if (activeProject) { - [[activeProject projectWindow] makeKeyAndOrderFront:self]; - } + //~ Is this needed? + if (activeProject) { + [[activeProject projectWindow] makeKeyAndOrderFront:self]; + } - if ([inspector isVisible]) { - [self inspectorPopupDidChange:inspectorPopup]; + if ([inspector isVisible]) { + [self inspectorPopupDidChange:inspectorPopup]; + } + } +} + +- (void)saveAllProjectsIfNeeded +{ + NSUserDefaults *defs = [NSUserDefaults standardUserDefaults]; + + if( [[defs objectForKey:AutoSave] isEqualToString:@"YES"] ) { + NSRunAlertPanel(@"Save All", + @"Going to save all projects!", + @"OK",nil,nil); + + [self saveAllProjects]; + } + else { + if( [saveTimer isValid] ) { + [saveTimer invalidate]; + } } - } } - (void)saveAllProjects diff --git a/ProjectCenter/PCPrefController.h b/ProjectCenter/PCPrefController.h index ae079e4..617db75 100644 --- a/ProjectCenter/PCPrefController.h +++ b/ProjectCenter/PCPrefController.h @@ -84,3 +84,6 @@ - (NSString *)selectFileWithTypes:(NSArray *)types; @end + +extern NSString *SavePeriodDidChangeNotification; + diff --git a/ProjectCenter/PCPrefController.m b/ProjectCenter/PCPrefController.m index 4027740..1f34bf5 100644 --- a/ProjectCenter/PCPrefController.m +++ b/ProjectCenter/PCPrefController.m @@ -31,6 +31,8 @@ #import #endif +NSString *SavePeriodDidChangeNotification = @"SavePeriodDidChangeNotification"; + @interface PCPrefController (CreateUI) - (void)_initUI; @@ -528,12 +530,14 @@ NSString *periodString = [autoSaveField stringValue]; if (periodString == nil || [periodString isEqualToString:@""]) { - periodString = [NSString stringWithString:@"120"]; + periodString = [NSString stringWithString:@"300"]; } [[NSUserDefaults standardUserDefaults] setObject:periodString forKey:AutoSavePeriod]; [preferencesDict setObject:periodString forKey:AutoSavePeriod]; + + [[NSNotificationCenter defaultCenter] postNotificationName:SavePeriodDidChangeNotification object:periodString]; } - (void)setSaveOnQuit:(id)sender