blob: 8746f04ca0c0d877ada7e51485cff335d9c5891e [file] [log] [blame]
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -04001/*
2 * Copyright (C) 2015-2018 Savoir-faire Linux Inc.
3 * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
4 * Author: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21//cocoa
22#import <Quartz/Quartz.h>
23
24//Qt
25#import <QSize>
26#import <QtMacExtras/qmacfunctions.h>
27#import <QPixmap>
28
29//LRC
30#import <api/lrc.h>
31#import <api/newaccountmodel.h>
32#import <api/newdevicemodel.h>
33#import <interfaces/pixmapmanipulatori.h>
34#import <globalinstances.h>
35
36#import "AccSipGeneralVC.h"
37#import "views/NSColor+RingTheme.h"
38#import "views/NSImage+Extensions.h"
39
40@interface AccSipGeneralVC ()
41
42@property (unsafe_unretained) IBOutlet NSButton* photoView;
43@property (unsafe_unretained) IBOutlet NSImageView* addProfilePhotoImage;
44@property (unsafe_unretained) IBOutlet NSTextField* displayNameField;
45@property (unsafe_unretained) IBOutlet NSTextField* userNameField;
46@property (unsafe_unretained) IBOutlet NSSecureTextField* passwordField;
47@property (unsafe_unretained) IBOutlet NSTextField* proxyField;
48@property (unsafe_unretained) IBOutlet NSTextField* voicemailField;
49@property (unsafe_unretained) IBOutlet NSTextField* serverField;
50@property (unsafe_unretained) IBOutlet NSButton* removeAccountButton;
51@property (unsafe_unretained) IBOutlet NSButton* editAccountButton;
52@property std::string selectedAccountID;
53
54@end
55
56@implementation AccSipGeneralVC
57
58//Tags for views
59typedef NS_ENUM(NSInteger, TagViews) {
60 DISPLAYNAME = 100
61};
62
63@synthesize accountModel;
64@synthesize delegate;
65@synthesize photoView,addProfilePhotoImage,displayNameField, userNameField, passwordField,proxyField,voicemailField, serverField, removeAccountButton, editAccountButton;
66
67-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil accountmodel:(lrc::api::NewAccountModel*) accountModel
68{
69 if (self = [self initWithNibName: nibNameOrNil bundle:nibBundleOrNil])
70 {
71 self.accountModel= accountModel;
72 }
73 return self;
74}
75
76-(void)viewDidLoad {
77 [super viewDidLoad];
78 [photoView setBordered:YES];
79 [addProfilePhotoImage setWantsLayer: YES];
80 [self setEditingMode:NO];
81 [self updateView];
82}
83
84- (void)pictureTakerDidEnd:(IKPictureTaker *) picker
85 returnCode:(NSInteger) code
86 contextInfo:(void*) contextInfo
87{
88 if (auto outputImage = [picker outputImage]) {
89 [photoView setBordered:NO];
90 auto image = [picker inputImage];
91 CGFloat newSize = MIN(image.size.height, image.size.width);
92 outputImage = [outputImage cropImageToSize:CGSizeMake(newSize, newSize)];
93 [photoView setImage: [outputImage roundCorners: outputImage.size.height * 0.5]];
94 [addProfilePhotoImage setHidden:YES];
95 auto imageToBytes = QByteArray::fromNSData([outputImage TIFFRepresentation]).toBase64();
96 std::string imageToString = std::string(imageToBytes.constData(), imageToBytes.length());
97 self.accountModel->setAvatar(self.selectedAccountID, imageToString);
98 } else if(!photoView.image) {
99 [photoView setBordered:YES];
100 [addProfilePhotoImage setHidden:NO];
101 }
102}
103
104#pragma mark - NSTextFieldDelegate methods
105
106- (BOOL)control:(NSControl *)control textShouldBeginEditing:(NSText *)fieldEditor
107{
108 return YES;
109}
110
111- (IBAction)triggerAdwancedSettings: (NSButton *)sender {
112 [self.delegate triggerAdvancedOptions];
113}
114
115- (void) setSelectedAccount:(std::string) account {
116 self.selectedAccountID = account;
117 [self updateView];
118}
119
120-(void)updateView {
121 const auto& account = accountModel->getAccountInfo(self.selectedAccountID);
122 QByteArray ba = QByteArray::fromStdString(account.profileInfo.avatar);
123
124 QVariant photo = GlobalInstances::pixmapManipulator().personPhoto(ba, nil);
125 if(QtMac::toNSImage(qvariant_cast<QPixmap>(photo))) {
126 [photoView setBordered:NO];
127 NSImage *image = QtMac::toNSImage(qvariant_cast<QPixmap>(photo));
128 CGFloat newSize = MIN(image.size.height, image.size.width);
129 image = [image cropImageToSize:CGSizeMake(newSize, newSize)];
130 [photoView setImage: [image roundCorners: image.size.height * 0.5]];
131 [addProfilePhotoImage setHidden:YES];
132 } else {
133 [photoView setImage:nil];
134 [photoView setBordered:YES];
135 [addProfilePhotoImage setHidden:NO];
136 }
137 NSString* displayName = @(account.profileInfo.alias.c_str());
138 [displayNameField setStringValue:displayName];
139
140 NSMutableAttributedString *colorTitle = [[NSMutableAttributedString alloc] initWithAttributedString:[removeAccountButton attributedTitle]];
141 NSRange titleRange = NSMakeRange(0, [colorTitle length]);
142 [colorTitle addAttribute:NSForegroundColorAttributeName value:[NSColor errorColor] range:titleRange];
143 [removeAccountButton setAttributedTitle:colorTitle];
144 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
145 [passwordField setStringValue: @(accountProperties.password.c_str())];
146 [proxyField setStringValue: @(accountProperties.routeset.c_str())];
147 [userNameField setStringValue: @(accountProperties.username.c_str())];
148 [serverField setStringValue: @(accountProperties.hostname.c_str())];
149 [voicemailField setStringValue: @(accountProperties.mailbox.c_str())];
150 self.accountEnabled = account.enabled;
151}
152
153#pragma mark - Actions
154
155- (IBAction)editPhoto:(id)sender
156{
157 auto pictureTaker = [IKPictureTaker pictureTaker];
158
159 [pictureTaker beginPictureTakerSheetForWindow:[self.view window]
160 withDelegate:self
161 didEndSelector:@selector(pictureTakerDidEnd:returnCode:contextInfo:)
162 contextInfo:nil];
163
164}
165
166- (IBAction)enableAccount: (NSButton *)sender {
167 const auto& account = accountModel->getAccountInfo(self.selectedAccountID);
168 self.accountModel->enableAccount(self.selectedAccountID, !account.enabled);
169 self.accountEnabled = account.enabled;
170 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
171 self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
172}
173
174- (IBAction)removeAccount:(id)sender
175{
176 NSAlert *alert = [[NSAlert alloc] init];
177 [alert addButtonWithTitle:@"OK"];
178 [alert addButtonWithTitle:@"Cancel"];
179 [alert setMessageText: NSLocalizedString(@"Remove account",
180 @"Remove account alert title")];
181 [alert setInformativeText:NSLocalizedString(@"By clicking \"OK\" you will remove this account on this device! This action can not be undone. Also, your registered name can be lost.",
182 @"Remove account alert message")];
183
184 if ([alert runModal] == NSAlertFirstButtonReturn) {
185 self.accountModel->removeAccount(self.selectedAccountID);
186 }
187}
188
189- (IBAction)changeEditingMode:(id)sender
190{
191 if([userNameField isEditable]) {
192 [self setEditingMode:NO];
193 return;
194 }
195 [self setEditingMode:YES];
196}
197
198-(void) setEditingMode:(BOOL) shouldEdit {
199 [userNameField setEditable:shouldEdit];
200 [passwordField setEditable:shouldEdit];
201 [proxyField setEditable:shouldEdit];
202 [voicemailField setEditable:shouldEdit];
203 [serverField setEditable:shouldEdit];
204 [userNameField setDrawsBackground:!shouldEdit];
205 [passwordField setDrawsBackground:!shouldEdit];
206 [proxyField setDrawsBackground:!shouldEdit];
207 [voicemailField setDrawsBackground:!shouldEdit];
208 [serverField setDrawsBackground:!shouldEdit];
209 [userNameField setBezeled:shouldEdit];
210 [passwordField setBezeled:shouldEdit];
211 [proxyField setBezeled:shouldEdit];
212 [voicemailField setBezeled:shouldEdit];
213 [serverField setBezeled:shouldEdit];
214 if(shouldEdit) {
215 [serverField setBezelStyle:NSTextFieldSquareBezel];
216 [userNameField setBezelStyle:NSTextFieldSquareBezel];
217 [passwordField setBezelStyle:NSTextFieldSquareBezel];
218 [proxyField setBezelStyle:NSTextFieldSquareBezel];
219 [voicemailField setBezelStyle:NSTextFieldSquareBezel];
220 [userNameField becomeFirstResponder];
221 [editAccountButton setTitle:@"Done"];
222 return;
223 }
224 [self saveAccount];
225 [editAccountButton setTitle:@"Edit Account"];
226 [self.view resignFirstResponder];
227}
228
229-(void) saveAccount {
230 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
231 accountProperties.hostname = [serverField.stringValue UTF8String];
232 accountProperties.password = [passwordField.stringValue UTF8String];
233 accountProperties.username = [userNameField.stringValue UTF8String];
234 accountProperties.routeset = [proxyField.stringValue UTF8String];
235 accountProperties.mailbox = [voicemailField.stringValue UTF8String];
236 self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
237}
238
239#pragma mark - NSTextFieldDelegate delegate methods
240
241- (void)controlTextDidChange:(NSNotification *)notif
242{
243 NSTextField* textField = [notif object];
244 if (textField.tag != DISPLAYNAME) {
245 return;
246 }
247 NSString* displayName = textField.stringValue;
248
249 [NSObject cancelPreviousPerformRequestsWithTarget:self];
250 self.accountModel->setAlias(self.selectedAccountID, [displayName UTF8String]);
251 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
252 self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
253}
254
255@end