From 26c7462eb0e1a86c9d644a27724f81d8ae496cb7 Mon Sep 17 00:00:00 2001 From: Hivert Quentin Date: Thu, 14 Dec 2023 18:11:10 +0100 Subject: [PATCH] fix(contacts): Custom contacts categories in sogo.conf wasn't working --- UI/PreferencesUI/UIxJSONPreferences.m | 82 +++++++++++++++++++++------ UI/PreferencesUI/UIxPreferences.m | 59 ++++++++++++++++--- 2 files changed, 118 insertions(+), 23 deletions(-) diff --git a/UI/PreferencesUI/UIxJSONPreferences.m b/UI/PreferencesUI/UIxJSONPreferences.m index ad19fa26b..7e52d8b82 100644 --- a/UI/PreferencesUI/UIxJSONPreferences.m +++ b/UI/PreferencesUI/UIxJSONPreferences.m @@ -78,6 +78,32 @@ static SoProduct *preferencesProduct = nil; return labelsDictionary; } +- (NSDictionary *) _localizedContactsLabels +{ + NSArray *categoryLabels, *localizedCategoryLabels; + NSDictionary *labelsDictionary; + + labelsDictionary = nil; + localizedCategoryLabels = [[self labelForKey: @"contacts_category_labels" + withResourceManager: [preferencesProduct resourceManager]] + componentsSeparatedByString: @","]; + categoryLabels = [[[preferencesProduct resourceManager] + stringForKey: @"contacts_category_labels" + inTableNamed: nil + withDefaultValue: @"" + languages: [NSArray arrayWithObject: @"English"]] + componentsSeparatedByString: @","]; + + if ([localizedCategoryLabels count] == [categoryLabels count]) + labelsDictionary = [NSDictionary dictionaryWithObjects: localizedCategoryLabels + forKeys: categoryLabels]; + else + [self logWithFormat: @"ERROR: localizable strings contacts_category_labels is incorrect for language %@", + [[[context activeUser] userDefaults] language]]; + + return labelsDictionary; +} + - (WOResponse *) activeExternalSieveScriptsAction { NGSieveClient *client; @@ -121,7 +147,7 @@ static SoProduct *preferencesProduct = nil; SOGoUserDefaults *defaults; SOGoDomainDefaults *domainDefaults; NSMutableArray *accounts; - NSDictionary *categoryLabels, *vacationOptions; + NSDictionary *calendarCategoryLabels, *contactsCategoriesLabels, *vacationOptions; if (!preferencesProduct) { @@ -131,7 +157,8 @@ static SoProduct *preferencesProduct = nil; defaults = [[context activeUser] userDefaults]; domainDefaults = [[context activeUser] domainDefaults]; - categoryLabels = nil; + calendarCategoryLabels = nil; + contactsCategoriesLabels = nil; // // Default General preferences @@ -223,7 +250,7 @@ static SoProduct *preferencesProduct = nil; { NSArray *defaultCalendarCategories; - categoryLabels = [self _localizedCategoryLabels]; + calendarCategoryLabels = [self _localizedCategoryLabels]; if ((defaultCalendarCategories = [defaults calendarCategories])) { @@ -239,7 +266,7 @@ static SoProduct *preferencesProduct = nil; for (count = 0; count < max; count++) { label = [defaultCalendarCategories objectAtIndex: count]; - if (!(localizedLabel = [categoryLabels objectForKey: label])) + if (!(localizedLabel = [calendarCategoryLabels objectForKey: label])) { localizedLabel = label; } @@ -252,15 +279,15 @@ static SoProduct *preferencesProduct = nil; { // Calendar categories are taken from localizable strings - [defaults setCalendarCategories: [categoryLabels allValues]]; + [defaults setCalendarCategories: [calendarCategoryLabels allValues]]; } } if (![[defaults source] objectForKey: @"SOGoCalendarCategoriesColors"]) { NSDictionary *defaultCalendarCategoriesColors; - if (!categoryLabels) - categoryLabels = [self _localizedCategoryLabels]; + if (!calendarCategoryLabels) + calendarCategoryLabels = [self _localizedCategoryLabels]; if ((defaultCalendarCategoriesColors = [defaults calendarCategoriesColors])) { @@ -278,7 +305,7 @@ static SoProduct *preferencesProduct = nil; for (count = 0; count < max; count++) { label = [defaultCalendarCategories objectAtIndex: count]; - if (!(localizedLabel = [categoryLabels objectForKey: label])) + if (!(localizedLabel = [calendarCategoryLabels objectForKey: label])) { localizedLabel = label; } @@ -296,7 +323,7 @@ static SoProduct *preferencesProduct = nil; NSMutableDictionary *colors; int i; - calendarCategories = [categoryLabels allValues]; + calendarCategories = [calendarCategoryLabels allValues]; colors = [NSMutableDictionary dictionaryWithCapacity: [calendarCategories count]]; for (i = 0; i < [calendarCategories count]; i++) @@ -311,18 +338,41 @@ static SoProduct *preferencesProduct = nil; // // Populate default contact categories, based on the user's preferred language // - if (![defaults contactsCategories]) + if (![[defaults source] objectForKey: @"SOGoContactsCategories"]) { NSArray *contactsCategories; - contactsCategories = [[[[self labelForKey: @"contacts_category_labels" withResourceManager: [preferencesProduct resourceManager]] - componentsSeparatedByString: @","] trimmedComponents] - sortedArrayUsingSelector: @selector (localizedCaseInsensitiveCompare:)]; + contactsCategoriesLabels = [self _localizedContactsLabels]; - if (!contactsCategories) - contactsCategories = [NSArray array]; + if((contactsCategories = [defaults contactsCategories])) + { + // Contact categories are taken from SOGo's configuration or SOGoDefaults.plist - [defaults setContactsCategories: contactsCategories]; + NSMutableArray *filteredContactsCategories; + NSString *label, *localizedLabel; + int count, max; + + max = [contactsCategories count]; + filteredContactsCategories = [NSMutableArray arrayWithCapacity: max]; + + for (count = 0; count < max; count++) + { + label = [contactsCategories objectAtIndex: count]; + if (!(localizedLabel = [contactsCategoriesLabels objectForKey: label])) + { + localizedLabel = label; + } + [filteredContactsCategories addObject: localizedLabel]; + } + + [defaults setContactsCategories: filteredContactsCategories]; + } + else + { + // Contact categories are taken from localizable strings + + [defaults setContactsCategories: [contactsCategoriesLabels allValues]]; + } } if (![[defaults source] objectForKey: @"SOGoMailAddOutgoingAddresses"]) diff --git a/UI/PreferencesUI/UIxPreferences.m b/UI/PreferencesUI/UIxPreferences.m index fcc85137a..43bbc7fb2 100644 --- a/UI/PreferencesUI/UIxPreferences.m +++ b/UI/PreferencesUI/UIxPreferences.m @@ -909,18 +909,63 @@ static NSArray *reminderValues = nil; // // Used by templates // +- (NSDictionary *) _localizedContactsLabels +{ + NSArray *categoryLabels, *localizedCategoryLabels; + NSDictionary *labelsDictionary; + + labelsDictionary = nil; + localizedCategoryLabels = [[self labelForKey: @"contacts_category_labels" + withResourceManager: [self resourceManager]] + componentsSeparatedByString: @","]; + categoryLabels = [[[self resourceManager] + stringForKey: @"contacts_category_labels" + inTableNamed: nil + withDefaultValue: @"" + languages: [NSArray arrayWithObject: @"English"]] + componentsSeparatedByString: @","]; + + if ([localizedCategoryLabels count] == [categoryLabels count]) + labelsDictionary = [NSDictionary dictionaryWithObjects: localizedCategoryLabels + forKeys: categoryLabels]; + else + [self logWithFormat: @"ERROR: localizable strings contacts_category_labels is incorrect for language %@", + [[[context activeUser] userDefaults] language]]; + + return labelsDictionary; +} + - (NSString *) defaultContactsCategories { - NSArray *contactsCategories; + NSArray *labels; + NSDictionary *localizedLabels; + NSMutableArray *defaultCategoriesLabels; + NSString *label, *localizedLabel; + unsigned int i; - contactsCategories = [[[[self labelForKey: @"contacts_category_labels" withResourceManager: [self resourceManager]] - componentsSeparatedByString: @","] trimmedComponents] - sortedArrayUsingSelector: @selector (localizedCaseInsensitiveCompare:)]; + localizedLabels = [self _localizedContactsLabels]; + labels = [[SOGoSystemDefaults sharedSystemDefaults] contactsCategories]; + defaultCategoriesLabels = [NSMutableArray array]; - if (!contactsCategories) - contactsCategories = [NSArray array]; + if(labels) + { + for (i = 0; i < [labels count]; i++) + { + label = [labels objectAtIndex: i]; + if (!(localizedLabel = [localizedLabels objectForKey: label])) + { + localizedLabel = label; + } + [defaultCategoriesLabels addObject: localizedLabel]; + } + } + else + { + defaultCategoriesLabels = [localizedLabels allValues]; + } - return [contactsCategories jsonRepresentation]; + + return [defaultCategoriesLabels jsonRepresentation]; } //