Commits

Anonymous committed 2ce646f

Saving newly created account and adding it to the AccountList

  • Participants
  • Parent commits ec185b3

Comments (0)

Files changed (4)

File Source/Account.m

     
     NSURL *saveURL = accountFileURL;
     if (nil == saveURL) {
-        NSString *fileName = [accountName stringByAppendingPathExtension: @"sieveAccount"];
-        saveURL = [[[AccountList sharedAccountList] accountsFolder] URLByAppendingPathComponent: fileName];
+        NSString *fileName = accountName;
+        NSURL *accountsFolder = [[AccountList sharedAccountList] accountsFolder];
+        saveURL = [accountsFolder URLByAppendingPathComponent: [fileName stringByAppendingPathExtension: @"sieveAccount"]];
+     
+        int try = 1;
+        NSFileManager *fm = [NSFileManager defaultManager];
+        while ([fm fileExistsAtPath: [saveURL path]]) {
+            fileName = [NSString stringWithFormat: @"%@ (%d).sieveAccount", accountName, try];
+            saveURL = [accountsFolder URLByAppendingPathComponent: [fileName stringByAppendingPathExtension: @"sieveAccount"]];
+            ++try;
+        }
+        
+        [self setAccountName: fileName];
     }
     
     if (![saveData writeToURL: saveURL options: 0 error: &error]) {

File Source/AccountList.h

 
 #import <Cocoa/Cocoa.h>
 
+
+@class Account;
+
 extern NSString * const kAccountUTI;
 extern NSString * const kAccountsFolderDefaultsKey;
 
 - (NSArray *) accounts;
 - (void) setAccounts: (NSArray *) newAccounts;
 
+- (void) addAccount: (Account *) newAccount;
+
 - (IBAction) openAccount: (id) sender;
 - (IBAction) createAccount: (id) sender;
 

File Source/AccountList.m

 
 - (void) scanAccountsDirectory;
 
+- (NSArray *)accounts;
+- (unsigned)countOfAccounts;
+- (id)objectInAccountsAtIndex:(unsigned)theIndex;
+- (void)getAccounts:(id *)objsPtr range:(NSRange)range;
+- (void)insertObject:(id)obj inAccountsAtIndex:(unsigned)theIndex;
+- (void)removeObjectFromAccountsAtIndex:(unsigned)theIndex;
+- (void)replaceObjectInAccountsAtIndex:(unsigned)theIndex withObject:(id)obj;
+
 @end
 
 
     [wc run];
 }
 
+- (void) addAccount: (Account *) newAccount;
+{
+    [self insertObject: newAccount inAccountsAtIndex: [accounts count]];
+}
+
 #pragma mark -
 #pragma mark NSMenuDelegate
 
     }
 }
 
+- (unsigned)countOfAccounts 
+{
+    return [accounts count];
+}
+
+- (id)objectInAccountsAtIndex:(unsigned)theIndex {
+    return [accounts objectAtIndex:theIndex];
+}
+
+- (void)getAccounts:(id *)objsPtr range:(NSRange)range {
+    if (!accounts) {
+        accounts = [[NSMutableArray alloc] init];
+    }
+    [accounts getObjects:objsPtr range:range];
+}
+
+- (void)insertObject:(id)obj inAccountsAtIndex:(unsigned)theIndex {
+    if (!accounts) {
+        accounts = [[NSMutableArray alloc] init];
+    }
+    [accounts insertObject:obj atIndex:theIndex];
+    updateMenu = YES;
+}
+
+- (void)removeObjectFromAccountsAtIndex:(unsigned)theIndex {
+    [accounts removeObjectAtIndex:theIndex];
+    updateMenu = YES;
+}
+
+- (void)replaceObjectInAccountsAtIndex:(unsigned)theIndex withObject:(id)obj {
+    if (!accounts) {
+        accounts = [[NSMutableArray alloc] init];
+    }
+    [accounts replaceObjectAtIndex:theIndex withObject:obj];
+    updateMenu = YES;
+}
+
 @end

File Source/CreateAccountWindowController.m

 #import "CreateAccountWindowController.h"
 #import "ServiceLookup.h"
 #import "Account.h"
+#import "AccountList.h"
 
 NSString * const kAppErrorDomain = @"AppErrorDomain";
 
     [client connectToHost: [account host] port: [account port]];
 }
 
+- (void) didPresentErrorWithRecovery: (BOOL)didRecover contextInfo: (void *)contextInfo
+{
+    
+}
 
 - (void) savePreset;
 {
-    // TODO: save preset/bookmark
-    [self closeSelf];
+    NSError *error = nil;
+    if (![account saveError: &error]) {
+        [self presentError: error modalForWindow: [self window] delegate: self didPresentSelector: @selector(didPresentErrorWithRecovery:contextInfo:) contextInfo: NULL];
+    } else {
+        [[AccountList sharedAccountList] addAccount: account];
+        [self closeSelf];
+    }
 }
 
 #pragma mark -