-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
OTRSettingsManager.m
229 lines (191 loc) · 9.78 KB
/
OTRSettingsManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//
// OTRSettingsManager.m
// Off the Record
//
// Created by Chris Ballinger on 4/10/12.
// Copyright (c) 2012 Chris Ballinger. All rights reserved.
//
// This file is part of ChatSecure.
//
// ChatSecure is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ChatSecure is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ChatSecure. If not, see <http://www.gnu.org/licenses/>.
#import "OTRSettingsManager.h"
#import "OTRViewSetting.h"
@import OTRAssets;
#import "OTRSetting.h"
#import "OTRBoolSetting.h"
#import "OTRViewSetting.h"
#import "OTRDoubleSetting.h"
#import "OTRFeedbackSetting.h"
#import "OTRConstants.h"
#import "OTRShareSetting.h"
#import "OTRSettingsGroup.h"
#import "OTRLanguageSetting.h"
#import "OTRDonateSetting.h"
#import "OTRIntSetting.h"
#import "OTRCertificateSetting.h"
#import "OTRUtilities.h"
#import "ChatSecureCoreCompat-Swift.h"
#import "OTRUtilities.h"
@interface OTRSettingsManager ()
@end
@implementation OTRSettingsManager
- (instancetype) init
{
if (self = [super init])
{
[self populateSettings];
}
return self;
}
- (void) populateSettings
{
NSMutableArray<OTRSettingsGroup*> *settingsGroups = [NSMutableArray array];
NSMutableDictionary *newSettingsDictionary = [NSMutableDictionary dictionary];
// Leave this in for now
OTRViewSetting *accountsViewSetting = [[OTRViewSetting alloc] initWithTitle:ACCOUNTS_STRING() description:nil viewControllerClass:nil];
OTRSettingsGroup *accountsGroup = [[OTRSettingsGroup alloc] initWithTitle:ACCOUNTS_STRING() settings:@[accountsViewSetting]];
[settingsGroups addObject:accountsGroup];
if (OTRBranding.allowsDonation) {
NSString *donateTitle = nil;
if (TransactionObserver.hasValidReceipt) {
donateTitle = [NSString stringWithFormat:@"%@ ✅", DONATE_STRING()];
} else {
donateTitle = [NSString stringWithFormat:@"%@ 🎁", DONATE_STRING()];
}
OTRDonateSetting *donateSetting = [[OTRDonateSetting alloc] initWithTitle:donateTitle description:nil];
//donateSetting.imageName = @"29-heart.png";
OTRSetting *moreSetting = [[OTRSetting alloc] initWithTitle:MORE_WAYS_TO_HELP_STRING() description:nil];
moreSetting.actionBlock = ^void(id sender) {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Purchase" bundle:[OTRAssets resourcesBundle]];
UIViewController *moreVC = [storyboard instantiateViewControllerWithIdentifier:@"moreWaysToHelp"];
UIViewController *sourceVC = sender;
if (![sender isKindOfClass:[UIViewController class]]) {
return;
}
[sourceVC presentViewController:moreVC animated:YES completion:nil];
};
OTRSettingsGroup *donateGroup = [[OTRSettingsGroup alloc] initWithTitle:DONATE_STRING() settings:@[donateSetting, moreSetting]];
[settingsGroups addObject:donateGroup];
}
OTRBoolSetting *deletedDisconnectedConversations = [[OTRBoolSetting alloc] initWithTitle:DELETE_CONVERSATIONS_ON_DISCONNECT_TITLE_STRING()
description:DELETE_CONVERSATIONS_ON_DISCONNECT_DESCRIPTION_STRING()
settingsKey:kOTRSettingKeyDeleteOnDisconnect];
[newSettingsDictionary setObject:deletedDisconnectedConversations forKey:kOTRSettingKeyDeleteOnDisconnect];
OTRCertificateSetting * certSetting = [[OTRCertificateSetting alloc] initWithTitle:PINNED_CERTIFICATES_STRING()
description:PINNED_CERTIFICATES_DESCRIPTION_STRING()];
certSetting.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
OTRBoolSetting *backupKeySetting = [[OTRBoolSetting alloc] initWithTitle:ALLOW_DB_PASSPHRASE_BACKUP_TITLE_STRING()
description:ALLOW_DB_PASSPHRASE_BACKUP_DESCRIPTION_STRING()
settingsKey:kOTRSettingKeyAllowDBPassphraseBackup];
if (![PushController canReceivePushNotifications] ||
[PushController getPushPreference] != PushPreferenceEnabled) {
OTRViewSetting *pushViewSetting = [[OTRViewSetting alloc] initWithTitle:CHATSECURE_PUSH_STRING() description:nil viewControllerClass:[EnablePushViewController class]];
pushViewSetting.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
OTRSettingsGroup *pushGroup = [[OTRSettingsGroup alloc] initWithTitle:PUSH_TITLE_STRING() settings:@[pushViewSetting]];
[settingsGroups addObject:pushGroup];
}
NSArray *chatSettings = @[deletedDisconnectedConversations];
OTRSettingsGroup *chatSettingsGroup = [[OTRSettingsGroup alloc] initWithTitle:CHAT_STRING() settings:chatSettings];
[settingsGroups addObject:chatSettingsGroup];
NSArray * securitySettings = @[certSetting, backupKeySetting];
OTRSettingsGroup *securitySettingsGroup = [[OTRSettingsGroup alloc] initWithTitle:SECURITY_STRING() settings:securitySettings];
[settingsGroups addObject:securitySettingsGroup];
OTRShareSetting * shareViewSetting = [[OTRShareSetting alloc] initWithTitle:SHARE_STRING() description:nil];
shareViewSetting.imageName = @"275-broadcast.png";
OTRLanguageSetting * languageSetting = [[OTRLanguageSetting alloc]initWithTitle:LANGUAGE_STRING() description:nil settingsKey:kOTRSettingKeyLanguage];
languageSetting.imageName = @"globe.png";
[newSettingsDictionary setObject:languageSetting forKey:kOTRSettingKeyLanguage];
NSMutableArray *otherSettings = [NSMutableArray arrayWithCapacity:5];
[otherSettings addObjectsFromArray:@[languageSetting, shareViewSetting]];
if ([OTRBranding githubURL]) {
OTRFeedbackSetting * feedbackViewSetting = [[OTRFeedbackSetting alloc] initWithTitle:SEND_FEEDBACK_STRING() description:nil];
feedbackViewSetting.imageName = @"18-envelope.png";
[otherSettings addObject:feedbackViewSetting];
}
OTRSettingsGroup *otherGroup = [[OTRSettingsGroup alloc] initWithTitle:OTHER_STRING() settings:otherSettings];
OTRSettingsGroup *advancedGroup = [[OTRSettingsGroup alloc] initWithTitle:ADVANCED_STRING()];
if (OTRBranding.allowGroupOMEMO) {
OTRBoolSetting *omemoGroupKeySetting = [[OTRBoolSetting alloc] initWithTitle:OMEMO_GROUP_ENCRYPTION_STRING()
description:OMEMO_GROUP_ENCRYPTION_DETAIL_STRING()
settingsKey:kOTRShowOMEMOGroupEncryptionKey];
[advancedGroup addSetting:omemoGroupKeySetting];
}
if (OTRBranding.allowDebugFileLogging) {
OTRViewSetting *logsSetting = [[OTRViewSetting alloc] initWithTitle:MANAGE_DEBUG_LOGS_STRING()
description:nil
viewControllerClass:[OTRLogListViewController class]];
logsSetting.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
[advancedGroup addSetting:logsSetting];
}
[settingsGroups addObject:otherGroup];
if (advancedGroup.settings.count > 0) {
[settingsGroups addObject:advancedGroup];
}
_settingsDictionary = newSettingsDictionary;
_settingsGroups = settingsGroups;
}
- (OTRSetting*) settingAtIndexPath:(NSIndexPath*)indexPath
{
OTRSettingsGroup *settingsGroup = [self.settingsGroups objectAtIndex:indexPath.section];
return [settingsGroup.settings objectAtIndex:indexPath.row];
}
- (NSString*) stringForGroupInSection:(NSUInteger)section
{
OTRSettingsGroup *settingsGroup = [self.settingsGroups objectAtIndex:section];
return settingsGroup.title;
}
- (NSUInteger) numberOfSettingsInSection:(NSUInteger)section
{
OTRSettingsGroup *settingsGroup = [self.settingsGroups objectAtIndex:section];
return [settingsGroup.settings count];
}
- (nullable NSIndexPath *)indexPathForSetting:(OTRSetting *)setting
{
__block NSIndexPath *indexPath = nil;
[self.settingsGroups enumerateObjectsUsingBlock:^(OTRSettingsGroup *group, NSUInteger idx, BOOL *stop) {
NSUInteger row = [group.settings indexOfObject:setting];
if (row != NSNotFound) {
indexPath = [NSIndexPath indexPathForItem:row inSection:idx];
*stop = YES;
}
}];
return indexPath;
}
+ (BOOL) boolForOTRSettingKey:(NSString*)key
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
return [defaults boolForKey:key];
}
+ (double) doubleForOTRSettingKey:(NSString*)key
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
return [defaults doubleForKey:key];
}
+ (NSInteger) intForOTRSettingKey:(NSString *)key
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
return [defaults integerForKey:key];
}
+ (float) floatForOTRSettingKey:(NSString *)key
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
return [defaults floatForKey:key];
}
- (nullable OTRSetting*) settingForOTRSettingKey:(NSString*)key {
return [self.settingsDictionary objectForKey:key];
}
+ (BOOL) allowGroupOMEMO {
return OTRBranding.allowGroupOMEMO && [self boolForOTRSettingKey:kOTRShowOMEMOGroupEncryptionKey];
}
@end