Revision: 27796
          http://sourceforge.net/p/bibdesk/svn/27796
Author:   hofman
Date:     2022-08-12 18:32:20 +0000 (Fri, 12 Aug 2022)
Log Message:
-----------
save last visited date for favicon pages and remove pages and icons visited 
more than a year ago

Modified Paths:
--------------
    trunk/bibdesk/BDSKWebIconDatabase.h
    trunk/bibdesk/BDSKWebIconDatabase.m

Modified: trunk/bibdesk/BDSKWebIconDatabase.h
===================================================================
--- trunk/bibdesk/BDSKWebIconDatabase.h 2022-08-12 11:31:08 UTC (rev 27795)
+++ trunk/bibdesk/BDSKWebIconDatabase.h 2022-08-12 18:32:20 UTC (rev 27796)
@@ -40,8 +40,8 @@
 
 
 @interface BDSKWebIconDatabase : NSObject  {
-    NSMutableDictionary *iconURLs;
     NSMutableDictionary *icons;
+    NSMutableDictionary *pages;
     NSMutableDictionary *recentIcons;
     NSMutableDictionary *cachedIcons;
 }

Modified: trunk/bibdesk/BDSKWebIconDatabase.m
===================================================================
--- trunk/bibdesk/BDSKWebIconDatabase.m 2022-08-12 11:31:08 UTC (rev 27795)
+++ trunk/bibdesk/BDSKWebIconDatabase.m 2022-08-12 18:32:20 UTC (rev 27796)
@@ -39,11 +39,15 @@
 #import "BDSKWebIconDatabase.h"
 #import "NSFileManager_BDSKExtensions.h"
 #import "NSString_BDSKExtensions.h"
+#import "BDSKBookmark.h"
+#import "BDSKBookmarkController.h"
 
-#define ICONS_KEY @"icons"
-#define ICONURLS_KEY @"iconURLs"
-#define VERSION_KEY @"version"
-#define VERSION @"1"
+#define ICONS_KEY       @"icons"
+#define PAGES_KEY       @"pages"
+#define VERSION_KEY     @"version"
+#define ICONURL_KEY     @"iconURL"
+#define LASTVISITED_KEY @"lastVisited"
+#define VERSION         @"1"
 #define WEBICONS_FILENAME @"WebIcons.plist"
 
 @interface BDSKWebIconDatabase ()
@@ -74,7 +78,7 @@
                 dict == nil;
         }
         icons = [[NSMutableDictionary alloc] initWithDictionary:[dict 
objectForKey:ICONS_KEY]];
-        iconURLs = [[NSMutableDictionary alloc] initWithDictionary:[dict 
objectForKey:ICONURLS_KEY]];
+        pages = [[NSMutableDictionary alloc] initWithDictionary:[dict 
objectForKey:PAGES_KEY]];
         [[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(handleApplicationWillTerminate:) 
name:NSApplicationWillTerminateNotification object:nil];
     }
     return self;
@@ -81,8 +85,8 @@
 }
 
 - (void)dealloc {
-    BDSKDESTROY(iconURLs);
     BDSKDESTROY(icons);
+    BDSKDESTROY(pages);
     BDSKDESTROY(recentIcons);
     BDSKDESTROY(cachedIcons);
     [super dealloc];
@@ -94,7 +98,7 @@
     NSImage *icon = [recentIcons objectForKey:pageURLString];
     if (icon)
         return icon;
-    NSString *iconURLString = [iconURLs objectForKey:pageURLString];
+    NSString *iconURLString = [[pages objectForKey:pageURLString] 
objectForKey:ICONURL_KEY];
     if (iconURLString == nil)
         return nil;
     icon = [cachedIcons objectForKey:iconURLString];
@@ -114,7 +118,11 @@
 - (NSImage *)recentIconForURLString:(NSString *)aURLString {
     if ([NSString isEmptyString:aURLString])
         return nil;
-    return [recentIcons objectForKey:aURLString];
+    NSImage *icon = [recentIcons objectForKey:aURLString];
+    if (icon == nil)
+        return nil;
+    [[pages objectForKey:aURLString] setObject:[NSDate date] 
forKey:LASTVISITED_KEY];
+    return icon;
 }
 
 - (void)setIcon:(NSImage *)icon withData:(NSData *)data 
fromURLString:(NSString *)iconURLString forURLString:(NSString *)pageURLString {
@@ -123,7 +131,8 @@
     if (recentIcons == nil)
         recentIcons = [[NSMutableDictionary alloc] init];
     [recentIcons setObject:icon forKey:pageURLString];
-    [iconURLs setObject:iconURLString forKey:pageURLString];
+    NSMutableDictionary *dict = [NSMutableDictionary 
dictionaryWithObjectsAndKeys:iconURLString, ICONURL_KEY, [NSDate date], 
LASTVISITED_KEY, nil];
+    [pages setObject:dict forKey:pageURLString];
     if (data) {
         [recentIcons setObject:icon forKey:iconURLString];
         [cachedIcons removeObjectForKey:iconURLString];
@@ -135,11 +144,42 @@
     return [[[NSFileManager defaultManager] applicationSupportDirectoryURL] 
URLByAppendingPathComponent:WEBICONS_FILENAME];
 }
 
+static void addAllURLStrings(NSMutableSet *set, BDSKBookmark *bookmark) {
+    if ([bookmark bookmarkType] == BDSKBookmarkTypeFolder) {
+        for (BDSKBookmark *bm in [bookmark children])
+            addAllURLStrings(set, bm);
+    } else if ([bookmark bookmarkType] == BDSKBookmarkTypeBookmark) {
+        NSString *url = [bookmark URLDescription];
+        if (url)
+            [set addObject:url];
+    }
+}
+
+- (void)removePagesWithLimit:(NSTimeInterval)limit {
+    NSMutableSet *bookmarkURLs = [NSMutableSet set];
+    addAllURLStrings(bookmarkURLs, [[BDSKBookmarkController 
sharedBookmarkController] bookmarkRoot]);
+    NSMutableArray *pagesToRemove = [NSMutableArray array];
+    NSMutableSet *iconsToRemove = [NSMutableSet set];
+    NSMutableSet *iconsToKeep = [NSMutableSet set];
+    [pages enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL 
*stop){
+        if ([bookmarkURLs containsObject:key] == NO && -[[obj 
objectForKey:LASTVISITED_KEY] timeIntervalSinceNow] > limit) {
+            [pagesToRemove addObject:key];
+            [iconsToRemove addObject:[obj objectForKey:ICONURL_KEY]];
+        } else {
+            [iconsToKeep addObject:[obj objectForKey:ICONURL_KEY]];
+        }
+    }];
+    [iconsToRemove minusSet:iconsToKeep];
+    [pages removeObjectsForKeys:pagesToRemove];
+    [icons removeObjectsForKeys:[iconsToRemove allObjects]];
+}
+
 - (void)handleApplicationWillTerminate:(NSNotification *)notification {
-    if (icons == nil || iconURLs == nil || [recentIcons count] == 0)
+    if (icons == nil || pages == nil || [recentIcons count] == 0)
         return;
+    [self removePagesWithLimit:365.0 * 24.0 * 3600.0];
     NSURL *dbURL = [self webIconDatabaseURL];
-    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:iconURLs, 
ICONURLS_KEY, icons, ICONS_KEY, VERSION, VERSION_KEY, nil];
+    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:pages, 
PAGES_KEY, icons, ICONS_KEY, VERSION, VERSION_KEY, nil];
     NSData *data = [NSPropertyListSerialization dataWithPropertyList:dict 
format:NSPropertyListBinaryFormat_v1_0 options:0 error:NULL];
     [data writeToURL:dbURL atomically:YES];
 }

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



_______________________________________________
Bibdesk-commit mailing list
Bibdesk-commit@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit

Reply via email to