Revision: 7110
          http://skim-app.svn.sourceforge.net/skim-app/?rev=7110&view=rev
Author:   hofman
Date:     2011-02-16 13:24:58 +0000 (Wed, 16 Feb 2011)

Log Message:
-----------
make bookmark controller a true singleton so we cannot create duplicates, we're 
instantiating in MainMenu.nib

Modified Paths:
--------------
    trunk/SKBookmarkController.m

Modified: trunk/SKBookmarkController.m
===================================================================
--- trunk/SKBookmarkController.m        2011-02-15 10:27:50 UTC (rev 7109)
+++ trunk/SKBookmarkController.m        2011-02-16 13:24:58 UTC (rev 7110)
@@ -90,6 +90,8 @@
 
 @synthesize outlineView, statusBar, bookmarkRoot, recentDocuments, undoManager;
 
+static SKBookmarkController *sharedBookmarkController = nil;
+
 static NSUInteger maxRecentDocumentsCount = 0;
 
 + (void)initialize {
@@ -101,50 +103,60 @@
 }
 
 + (id)sharedBookmarkController {
-    static SKBookmarkController *sharedBookmarkController = nil;
     if (sharedBookmarkController == nil)
         sharedBookmarkController = [[self alloc] init];
     return sharedBookmarkController;
 }
 
++ (id)allocWithZone:(NSZone *)zone {
+    return [sharedBookmarkController retain] ?: [super allocWithZone:zone];
+}
+
 - (id)init {
-    if (self = [super initWithWindowNibName:@"BookmarksWindow"]) {
-        recentDocuments = [[NSMutableArray alloc] init];
-        
-        NSMutableArray *bookmarks = [NSMutableArray array];
-        NSData *data = [NSData dataWithContentsOfFile:[self 
bookmarksFilePath]];
-        if (data) {
-            NSString *error = nil;
-            NSPropertyListFormat format = NSPropertyListBinaryFormat_v1_0;
-            id plist = [NSPropertyListSerialization propertyListFromData:data
-                                                        
mutabilityOption:NSPropertyListMutableContainers
-                                                                  
format:&format 
-                                                        
errorDescription:&error];
+    if (sharedBookmarkController == nil) {
+        if (self = [super initWithWindowNibName:@"BookmarksWindow"]) {
+            recentDocuments = [[NSMutableArray alloc] init];
             
-            if (error) {
-                NSLog(@"Error deserializing: %@", error);
-                [error release];
-            } else if ([plist isKindOfClass:[NSDictionary class]]) {
-                [recentDocuments addObjectsFromArray:[plist 
objectForKey:RECENTDOCUMENTS_KEY]];
-                for (NSDictionary *dict in [plist objectForKey:BOOKMARKS_KEY]) 
{
-                    SKBookmark *bookmark = [[SKBookmark alloc] 
initWithProperties:dict];
-                    if (bookmark) {
-                        [bookmarks addObject:bookmark];
-                        [bookmark release];
-                    } else
-                        NSLog(@"Failed to read bookmark: %@", dict);
+            NSMutableArray *bookmarks = [NSMutableArray array];
+            NSData *data = [NSData dataWithContentsOfFile:[self 
bookmarksFilePath]];
+            if (data) {
+                NSString *error = nil;
+                NSPropertyListFormat format = NSPropertyListBinaryFormat_v1_0;
+                id plist = [NSPropertyListSerialization 
propertyListFromData:data
+                                                            
mutabilityOption:NSPropertyListMutableContainers
+                                                                      
format:&format 
+                                                            
errorDescription:&error];
+                
+                if (error) {
+                    NSLog(@"Error deserializing: %@", error);
+                    [error release];
+                } else if ([plist isKindOfClass:[NSDictionary class]]) {
+                    [recentDocuments addObjectsFromArray:[plist 
objectForKey:RECENTDOCUMENTS_KEY]];
+                    for (NSDictionary *dict in [plist 
objectForKey:BOOKMARKS_KEY]) {
+                        SKBookmark *bookmark = [[SKBookmark alloc] 
initWithProperties:dict];
+                        if (bookmark) {
+                            [bookmarks addObject:bookmark];
+                            [bookmark release];
+                        } else
+                            NSLog(@"Failed to read bookmark: %@", dict);
+                    }
                 }
+                
             }
             
+            bookmarkRoot = [[SKBookmark alloc] initRootWithChildren:bookmarks];
+            [self startObservingBookmarks:[NSArray 
arrayWithObject:bookmarkRoot]];
+            
+            [[NSNotificationCenter defaultCenter] addObserver:self
+                                                     
selector:@selector(handleApplicationWillTerminateNotification:)
+                                                         
name:NSApplicationWillTerminateNotification
+                                                       object:NSApp];
         }
-        
-        bookmarkRoot = [[SKBookmark alloc] initRootWithChildren:bookmarks];
-        [self startObservingBookmarks:[NSArray arrayWithObject:bookmarkRoot]];
-        
-               [[NSNotificationCenter defaultCenter] addObserver:self
-                                                 
selector:@selector(handleApplicationWillTerminateNotification:)
-                                                     
name:NSApplicationWillTerminateNotification
-                                                   object:NSApp];
+        sharedBookmarkController = [self retain];
+    } else if (self != sharedBookmarkController) {
+        NSLog(@"shouldn't be able to create multiple instances");
+        [self release];
+        self = [sharedBookmarkController retain];
     }
     return self;
 }


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
The ultimate all-in-one performance toolkit: Intel(R) Parallel Studio XE:
Pinpoint memory and threading errors before they happen.
Find and fix more than 250 security defects in the development cycle.
Locate bottlenecks in serial and parallel code that limit performance.
http://p.sf.net/sfu/intel-dev2devfeb
_______________________________________________
Skim-app-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/skim-app-commit

Reply via email to