Skip to content

Commit

Permalink
Moved FolderImageCache out of Folder class
Browse files Browse the repository at this point in the history
Also re-added the db migration alert dialog.
  • Loading branch information
josh64x2 committed Mar 8, 2015
1 parent 4217183 commit e7de4b8
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 187 deletions.
10 changes: 8 additions & 2 deletions Vienna.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
/* End PBXAggregateTarget section */

/* Begin PBXBuildFile section */
030C38901AABF12D00AE1EB8 /* FolderImageCache.m in Sources */ = {isa = PBXBuildFile; fileRef = 030C388F1AABF12D00AE1EB8 /* FolderImageCache.m */; };
031E083019DFA3A900194F9F /* SubscriptionModel.m in Sources */ = {isa = PBXBuildFile; fileRef = 031E082F19DFA3A900194F9F /* SubscriptionModel.m */; };
032280311A33BCD8007DE933 /* Article.m in Sources */ = {isa = PBXBuildFile; fileRef = 032280301A33BCD8007DE933 /* Article.m */; };
03493D1A1A303BF700AA3F36 /* SyncingPreferencesView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 03493D1C1A303BF700AA3F36 /* SyncingPreferencesView.xib */; };
Expand Down Expand Up @@ -782,6 +783,8 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
030C388E1AABF12D00AE1EB8 /* FolderImageCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = FolderImageCache.h; path = src/models/FolderImageCache.h; sourceTree = "<group>"; };
030C388F1AABF12D00AE1EB8 /* FolderImageCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = FolderImageCache.m; path = src/models/FolderImageCache.m; sourceTree = "<group>"; };
031E082E19DFA3A900194F9F /* SubscriptionModel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SubscriptionModel.h; path = src/SubscriptionModel.h; sourceTree = "<group>"; };
031E082F19DFA3A900194F9F /* SubscriptionModel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = SubscriptionModel.m; path = src/SubscriptionModel.m; sourceTree = "<group>"; };
0322802F1A33BCD8007DE933 /* Article.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Article.h; path = src/models/Article.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1659,6 +1662,10 @@
children = (
0322802F1A33BCD8007DE933 /* Article.h */,
032280301A33BCD8007DE933 /* Article.m */,
AA26F4CA0604927300FE7994 /* Folder.h */,
AA26F4CB0604927300FE7994 /* Folder.m */,
030C388E1AABF12D00AE1EB8 /* FolderImageCache.h */,
030C388F1AABF12D00AE1EB8 /* FolderImageCache.m */,
);
name = Models;
sourceTree = "<group>";
Expand Down Expand Up @@ -2730,8 +2737,6 @@
03A131B21AA54EAC0037471F /* VNADatabaseMigration.m */,
AA36CD7906100692001E33A4 /* Field.h */,
AA36CD7A06100692001E33A4 /* Field.m */,
AA26F4CA0604927300FE7994 /* Folder.h */,
AA26F4CB0604927300FE7994 /* Folder.m */,
AA5AC2640C3ECAA000B2E14F /* SearchString.m */,
AA5AC2750C3ECB3F00B2E14F /* SearchString.h */,
);
Expand Down Expand Up @@ -3752,6 +3757,7 @@
AAFAF3FE088056D800DAFF04 /* KeyChain.m in Sources */,
AAD794E7088A25D200E2CE5E /* PopUpButtonExtensions.m in Sources */,
032280311A33BCD8007DE933 /* Article.m in Sources */,
030C38901AABF12D00AE1EB8 /* FolderImageCache.m in Sources */,
AAD799A0088B617900E2CE5E /* ImageAndTextCell.m in Sources */,
AA86B2AB0892DCAA0071FB33 /* BezierPathExtensions.m in Sources */,
AA9FE2EE08BC133600A9E977 /* Preferences.m in Sources */,
Expand Down
32 changes: 17 additions & 15 deletions src/Database.m
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,22 @@ - (BOOL)initialiseDatabase {
// Nothing to do here
return YES;
} else if (databaseVersion >= MA_Min_Supported_DB_Version) {
NSAlert * alert = [[NSAlert alloc] init];
[alert setMessageText:NSLocalizedString(@"Database Upgrade", nil)];
[alert setInformativeText:NSLocalizedString(@"Vienna must upgrade its database to the latest version. This may take a minute or so. We apologize for the inconveninece.", nil)];
[alert addButtonWithTitle:NSLocalizedString(@"Upgrade Database", nil)];
[alert addButtonWithTitle:NSLocalizedString(@"Quit Vienna", nil)];
NSInteger modalReturn = [alert runModal];
[alert release];
if (modalReturn == NSAlertSecondButtonReturn)
{
return NO;
}

// Backup the database before any upgrade
NSString * backupDatabaseFileName = [[Database databasePath] stringByAppendingPathExtension:@"bak"];
[[NSFileManager defaultManager] copyItemAtPath:[Database databasePath] toPath:backupDatabaseFileName error:nil];

[databaseQueue inDatabase:^(FMDatabase *db) {
// Migrate the database to the newest version
// TODO: move this into transaction so we can rollback on failure
Expand Down Expand Up @@ -175,21 +191,7 @@ - (BOOL)initialiseDatabase {
//
// else if (databaseVersion < MA_Current_DB_Version)
// {
// NSAlert * alert = [[NSAlert alloc] init];
// [alert setMessageText:NSLocalizedString(@"Database Upgrade", nil)];
// [alert setInformativeText:NSLocalizedString(@"Vienna must upgrade its database to the latest version. This may take a minute or so. We apologize for the inconveninece.", nil)];
// [alert addButtonWithTitle:NSLocalizedString(@"Upgrade Database", nil)];
// [alert addButtonWithTitle:NSLocalizedString(@"Quit Vienna", nil)];
// NSInteger modalReturn = [alert runModal];
// [alert release];
// if (modalReturn == NSAlertSecondButtonReturn)
// {
// return NO;
// }
//
// // Backup the database before any upgrade
// NSString * backupDatabaseFileName = [qualifiedDatabaseFileName stringByAppendingPathExtension:@"bak"];
// [[NSFileManager defaultManager] copyItemAtPath:qualifiedDatabaseFileName toPath:backupDatabaseFileName error:nil];
//
// }
//
//
Expand Down
176 changes: 6 additions & 170 deletions src/Folder.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,185 +21,21 @@
#import "Folder.h"
#import "AppController.h"
#import "Constants.h"
#import "Preferences.h"
#import "StringExtensions.h"
#import "KeyChain.h"

// Indexes into folder image array
enum {
MA_FolderIcon = 0,
MA_SmartFolderIcon,
MA_RSSFolderIcon,
MA_RSSFeedIcon,
MA_TrashFolderIcon,
MA_SearchFolderIcon,
MA_GoogleReaderFolderIcon,
MA_Max_Icons
};

// Folder image cache interface. This is exclusive to the
// folder code and only used privately.
@interface FolderImageCache : NSObject {
NSString * imagesCacheFolder;
NSMutableDictionary * folderImagesArray;
BOOL initializedFolderImagesArray;
}

// Accessor functions
+(FolderImageCache *)defaultCache;
-(void)addImage:(NSImage *)image forURL:(NSString *)baseURL;
-(NSImage *)retrieveImage:(NSString *)baseURL;

// Support functions
-(void)initFolderImagesArray;
@end

// Static pointers
static FolderImageCache * _folderImageCache = nil;
static NSArray * iconArray = nil;
#import "FolderImageCache.h"
#import "StringExtensions.h"
#import "Preferences.h"

// Private internal functions
@interface Folder (Private)
+(NSArray *)_iconArray;
@end

@implementation FolderImageCache

/* defaultCache
* Returns a pointer to the default cache. There is just one default cache
* and we instantiate it if it doesn't exist.
*/
+(FolderImageCache *)defaultCache
{
if (_folderImageCache == nil)
_folderImageCache = [[FolderImageCache alloc] init];
return _folderImageCache;
}

/* init
* Init an instance of the folder image cache.
*/
-(id)init
{
if ((self = [super init]) != nil)
{
imagesCacheFolder = nil;
initializedFolderImagesArray = NO;
folderImagesArray = [[NSMutableDictionary alloc] init];
}
return self;
}

/* addImage
* Add the specified image to the cache and save it to disk.
*/
-(void)addImage:(NSImage *)image forURL:(NSString *)baseURL
{
// Add in memory
[self initFolderImagesArray];
[folderImagesArray setObject:image forKey:baseURL];

// Save icon to disk here.
if (imagesCacheFolder != nil)
{
NSString * fullFilePath = [[imagesCacheFolder stringByAppendingPathComponent:baseURL] stringByAppendingPathExtension:@"tiff"];
NSData *imageData = nil;
@try {
imageData = [image TIFFRepresentation];
}
@catch (NSException *error) {
imageData = nil;
NSLog(@"tiff exception with %@", fullFilePath);
}
if (imageData != nil)
[[NSFileManager defaultManager] createFileAtPath:fullFilePath contents:imageData attributes:nil];
}
}

/* retrieveImage
* Retrieve the image for the specified URL from the cache.
*/
-(NSImage *)retrieveImage:(NSString *)baseURL
{
[self initFolderImagesArray];
return [folderImagesArray objectForKey:baseURL];
}

/* initFolderImagesArray
* Load the existing list of folder images from the designated folder image cache. We
* do this only once and we do it as quickly as possible. When we're done, the folderImagesArray
* will be filled with image representations for each valid image file we find in the cache.
*/
-(void)initFolderImagesArray
{
if (!initializedFolderImagesArray)
{
NSFileManager * fileManager = [NSFileManager defaultManager];
NSArray * listOfFiles;
BOOL isDir;

// Get and cache the path to the folder. This is the best time to make sure it
// exists. The penalty for it not existing AND us being unable to create it is that
// we don't cache folder icons in this session.
imagesCacheFolder = [[Preferences standardPreferences] imagesFolder];
if (![fileManager fileExistsAtPath:imagesCacheFolder isDirectory:&isDir])
{
if (![fileManager createDirectoryAtPath:imagesCacheFolder withIntermediateDirectories:YES attributes:nil error:nil])
{
NSLog(@"Cannot create image cache at %@. Will not cache folder images in this session.", imagesCacheFolder);
imagesCacheFolder = nil;
}
initializedFolderImagesArray = YES;
return;
}

if (!isDir)
{
NSLog(@"The file at %@ is not a directory. Will not cache folder images in this session.", imagesCacheFolder);
[imagesCacheFolder release];
imagesCacheFolder = nil;
initializedFolderImagesArray = YES;
return;
}

// Remember - not every file we find may be a valid image file. We use the filename as
// the key but check the extension too.
listOfFiles = [fileManager contentsOfDirectoryAtPath:imagesCacheFolder error:nil];
if (listOfFiles != nil)
{
NSString * fileName;

for (fileName in listOfFiles)
{
if ([[fileName pathExtension] isEqualToString:@"tiff"])
{
NSString * fullPath = [imagesCacheFolder stringByAppendingPathComponent:fileName];
NSData * imageData = [fileManager contentsAtPath:fullPath];
NSImage * iconImage = [[NSImage alloc] initWithData:imageData];
if ([iconImage isValid])
{
[iconImage setSize:NSMakeSize(16, 16)];
NSString * homePageSiteRoot = [[[fullPath lastPathComponent] stringByDeletingPathExtension] convertStringToValidPath];
[folderImagesArray setObject:iconImage forKey:homePageSiteRoot];
}
[iconImage release];
}
}
}
initializedFolderImagesArray = YES;
}
}
// Static pointers
static FolderImageCache * _folderImageCache = nil;
static NSArray * iconArray = nil;

/* dealloc
* Clean up.
*/
-(void)dealloc
{
[folderImagesArray release];
folderImagesArray=nil;
[super dealloc];
}
@end

@implementation Folder

Expand Down
33 changes: 33 additions & 0 deletions src/models/FolderImageCache.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// FolderImageCache.h
// Vienna
//
// Created by Joshua Pore on 8/03/2015.
// Copyright (c) 2015 The Vienna Project. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface FolderImageCache : NSObject {
NSString * imagesCacheFolder;
NSMutableDictionary * folderImagesArray;
BOOL initializedFolderImagesArray;
}

// Indexes into folder image array
enum {
MA_FolderIcon = 0,
MA_SmartFolderIcon,
MA_RSSFolderIcon,
MA_RSSFeedIcon,
MA_TrashFolderIcon,
MA_SearchFolderIcon,
MA_GoogleReaderFolderIcon,
MA_Max_Icons
};

+(FolderImageCache *)defaultCache;
-(void)addImage:(NSImage *)image forURL:(NSString *)baseURL;
-(NSImage *)retrieveImage:(NSString *)baseURL;

@end
Loading

0 comments on commit e7de4b8

Please sign in to comment.