From 2bc44dec9b6fc150eefa4a40f303af4121e4a0a1 Mon Sep 17 00:00:00 2001 From: Ludovic Marcotte Date: Thu, 26 Mar 2015 13:37:44 -0400 Subject: [PATCH] Improved preferences module (save/load, checks on enabled modules, etc.) --- UI/PreferencesUI/UIxJSONPreferences.m | 30 ++- UI/PreferencesUI/UIxPreferences.m | 100 +++++++-- UI/Templates/PreferencesUI/UIxPreferences.wox | 209 +++++++++--------- 3 files changed, 212 insertions(+), 127 deletions(-) diff --git a/UI/PreferencesUI/UIxJSONPreferences.m b/UI/PreferencesUI/UIxJSONPreferences.m index 099e44e04..75f69414d 100644 --- a/UI/PreferencesUI/UIxJSONPreferences.m +++ b/UI/PreferencesUI/UIxJSONPreferences.m @@ -35,25 +35,28 @@ @implementation UIxJSONPreferences -- (WOResponse *) _makeResponse: (SOGoUserProfile *) profile +- (WOResponse *) _makeResponse: (NSDictionary *) values { WOResponse *response; response = [context response]; [response setHeader: @"text/plain; charset=utf-8" forKey: @"content-type"]; - [response appendContentString: [profile jsonRepresentation]]; + [response appendContentString: [values jsonRepresentation]]; return response; } - (WOResponse *) jsonDefaultsAction { + NSMutableDictionary *values; SOGoUserDefaults *defaults; NSArray *categoryLabels; - + BOOL dirty; + defaults = [[context activeUser] userDefaults]; - + dirty = NO; + if (![[defaults source] objectForKey: @"SOGoLongDateFormat"]) [[defaults source] setObject: @"default" forKey: @"SOGoLongDateFormat"]; @@ -65,6 +68,7 @@ sortedArrayUsingSelector: @selector (localizedCaseInsensitiveCompare:)]; [defaults setCalendarCategories: categoryLabels]; + dirty = YES; } // Populate default contact categories, based on the user's preferred language @@ -78,6 +82,7 @@ categoryLabels = [NSArray array]; [defaults setContactsCategories: categoryLabels]; + dirty = YES; } // Populate default mail lablels, based on the user's preferred language @@ -90,12 +95,19 @@ // TODO - translate + refactor to not pass self since it's not a component //[defaults setMailLabelsColors: [SOGoMailLabel labelsFromDefaults: v component: self]]; [defaults setMailLabelsColors: v]; + dirty = YES; } - - [defaults synchronize]; - - return [self _makeResponse: [defaults source]]; + if (dirty) + [defaults synchronize]; + + // We inject our default mail account, something we don't want to do before we + // call -synchronize on our defaults. + values = [[[[defaults source] values] mutableCopy] autorelease]; + [[values objectForKey: @"AuxiliaryMailAccounts"] insertObject: [[[context activeUser] mailAccounts] objectAtIndex: 0] + atIndex: 0]; + + return [self _makeResponse: values]; } - (WOResponse *) jsonSettingsAction @@ -104,7 +116,7 @@ settings = [[context activeUser] userSettings]; - return [self _makeResponse: [settings source]]; + return [self _makeResponse: [[settings source] values]]; } @end diff --git a/UI/PreferencesUI/UIxPreferences.m b/UI/PreferencesUI/UIxPreferences.m index 492830b97..dd40a1c94 100644 --- a/UI/PreferencesUI/UIxPreferences.m +++ b/UI/PreferencesUI/UIxPreferences.m @@ -1746,7 +1746,12 @@ static NSArray *reminderValues = nil; return [[user domainDefaults] mailAuxiliaryUserAccountsEnabled]; } +// +// Used internally +// - (void) _extractMainIdentity: (NSDictionary *) identity + inDictionary: (NSMutableDictionary *) target + { /* We perform some validation here as we have no guaranty on the input validity. */ @@ -1755,9 +1760,11 @@ static NSArray *reminderValues = nil; if ([identity isKindOfClass: [NSDictionary class]]) { value = [identity objectForKey: @"signature"]; - if (!value) - value = @""; - [userDefaults setMailSignature: value]; + + if (value) + [target setObject: value forKey: @"SOGoMailSignature"]; + else + [target removeObjectForKey: @"SOGoMailSignature"]; if (mailCustomFromEnabled) { @@ -1769,22 +1776,37 @@ static NSArray *reminderValues = nil; if ([value length] == 0 || [[user allEmails] containsObject: value]) value = nil; - [userDefaults setMailCustomEmail: value]; + if (value) + [target setObject: value forKey: @"SOGoMailCustomEmail"]; + else + [target removeObjectForKey: @"SOGoMailCustomEmail"]; + value = [[identity objectForKey: @"fullName"] stringByTrimmingSpaces]; if ([value length] == 0 || [[user cn] isEqualToString: value]) value = nil; - [userDefaults setMailCustomFullName: value]; + + if (value) + [target setObject: value forKey: @"SOGoMailCustomFullName"]; + else + [target removeObjectForKey: @"SOGoMailCustomFullName"]; } value = [[identity objectForKey: @"replyTo"] stringByTrimmingSpaces]; - [userDefaults setMailReplyTo: value]; + + if (value && [value length] > 0) + [target setObject: value forKey: @"SOGoMailReplyTo"]; + else + [target removeObjectForKey: @"SOGoMailReplyTo"]; } } +// +// Used internally +// - (BOOL) _validateReceiptAction: (NSString *) action { return ([action isKindOfClass: [NSString class]] @@ -1793,7 +1815,12 @@ static NSArray *reminderValues = nil; || [action isEqualToString: @"ask"])); } +// +// Used internally +// - (void) _extractMainReceiptsPreferences: (NSDictionary *) receipts + inDictionary: (NSMutableDictionary *) target + { /* We perform some validation here as we have no guaranty on the input validity. */ @@ -1802,31 +1829,39 @@ static NSArray *reminderValues = nil; if ([receipts isKindOfClass: [NSDictionary class]]) { action = [receipts objectForKey: @"receiptAction"]; - [userDefaults - setAllowUserReceipt: [action isEqualToString: @"allow"]]; + [target setObject: @"1" forKey: @"SOGoMailReceiptAllow"]; action = [receipts objectForKey: @"receiptNonRecipientAction"]; if ([self _validateReceiptAction: action]) - [userDefaults setUserReceiptNonRecipientAction: action]; - + [target setObject: action forKey: @"SOGoMailReceiptNonRecipientAction"]; + action = [receipts objectForKey: @"receiptOutsideDomainAction"]; if ([self _validateReceiptAction: action]) - [userDefaults setUserReceiptOutsideDomainAction: action]; - + [target setObject: action forKey: @"SOGoMailReceiptOutsideDomainAction"]; + action = [receipts objectForKey: @"receiptAnyAction"]; if ([self _validateReceiptAction: action]) - [userDefaults setUserReceiptAnyAction: action]; + [target setObject: action forKey: @"SOGoMailReceiptAnyAction"]; } } +// +// Used internally +// - (void) _extractMainCustomFrom: (NSDictionary *) account { } +// +// Used internally +// - (void) _extractMainReplyTo: (NSDictionary *) account { } +// +// Used internally +// - (BOOL) _validateAccountIdentities: (NSArray *) identities { static NSString *identityKeys[] = { @"fullName", @"email", nil }; @@ -1880,6 +1915,9 @@ static NSArray *reminderValues = nil; return valid; } +// +// Used internally +// - (BOOL) _validateAccount: (NSDictionary *) account { static NSString *accountKeys[] = { @"name", @"serverName", @"userName", @@ -1934,7 +1972,11 @@ static NSArray *reminderValues = nil; return valid; } +// +// Used internally +// - (void) _extractMainAccountSettings: (NSDictionary *) account + inDictionary: (NSMutableDictionary *) target { NSArray *identities; @@ -1943,12 +1985,15 @@ static NSArray *reminderValues = nil; identities = [account objectForKey: @"identities"]; if ([identities isKindOfClass: [NSArray class]] && [identities count] > 0) - [self _extractMainIdentity: [identities objectAtIndex: 0]]; - [self _extractMainReceiptsPreferences: [account objectForKey: @"receipts"]]; + [self _extractMainIdentity: [identities objectAtIndex: 0] inDictionary: target]; + [self _extractMainReceiptsPreferences: [account objectForKey: @"receipts"] inDictionary: target]; } } -- (void) _extractAuxiliaryAccounts: (NSArray *) accounts +// +// Used internally +// +- (NSArray *) _extractAuxiliaryAccounts: (NSArray *) accounts { int count, max, oldMax; NSArray *oldAccounts; @@ -1984,7 +2029,7 @@ static NSArray *reminderValues = nil; } } - [userDefaults setAuxiliaryMailAccounts: auxAccounts]; + return auxAccounts; } // - (void) setMailAccounts: (NSString *) newMailAccounts @@ -2027,6 +2072,9 @@ static NSArray *reminderValues = nil; return (mailCustomFromEnabled ? @"true" : @"false"); } +// +// +// - (id ) saveAction { id o, v; @@ -2037,8 +2085,8 @@ static NSArray *reminderValues = nil; if ((v = [o objectForKey: @"defaults"])) { NSMutableDictionary *sanitizedLabels; + NSArray *allKeys, *accounts; NSDictionary *newLabels; - NSArray *allKeys; NSString *name; int i; @@ -2068,6 +2116,22 @@ static NSArray *reminderValues = nil; [v setObject: sanitizedLabels forKey: @"SOGoMailLabelsColors"]; } + // We sanitize our auxilary mail accounts + accounts = [v objectForKey: @"AuxiliaryMailAccounts"]; + if (accounts && [accounts isKindOfClass: [NSArray class]]) + { + if ([accounts count] > 0) + { + [self _extractMainAccountSettings: [accounts objectAtIndex: 0] inDictionary: v]; + if ([self mailAuxiliaryUserAccountsEnabled]) + accounts = [self _extractAuxiliaryAccounts: accounts]; + else + accounts = [NSArray array]; + + [v setObject: accounts forKey: @"AuxiliaryMailAccounts"]; + } + } + [[[user userDefaults] source] setValues: v]; [[user userDefaults] synchronize]; } diff --git a/UI/Templates/PreferencesUI/UIxPreferences.wox b/UI/Templates/PreferencesUI/UIxPreferences.wox index 6076eb8fe..0c721fcf5 100644 --- a/UI/Templates/PreferencesUI/UIxPreferences.wox +++ b/UI/Templates/PreferencesUI/UIxPreferences.wox @@ -669,21 +669,22 @@ - -
- - + + +
+ + + ng-true-value="1" + ng-false-value="0"> @@ -705,8 +706,9 @@
-
-
+ + + @@ -748,112 +750,119 @@ track by $index"> - +
- +
- -
- -
-
-
+ + +
+ +
+
+
+
- -
- - - - - - -
-