blob: aeccad2f89c4b37299d3f5326f3241141cd4b621 [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];
Kateryna Kostiuk3164fb02018-08-28 17:51:43 -040078 [[self view] setAutoresizingMask: NSViewMinXMargin | NSViewMaxXMargin | NSViewHeightSizable];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040079 [photoView setBordered:YES];
80 [addProfilePhotoImage setWantsLayer: YES];
81 [self setEditingMode:NO];
82 [self updateView];
83}
84
85- (void)pictureTakerDidEnd:(IKPictureTaker *) picker
86 returnCode:(NSInteger) code
87 contextInfo:(void*) contextInfo
88{
Kateryna Kostiuk256814e2018-09-04 14:47:33 -040089 //do nothing when editing canceled
90 if (code == 0) {
91 return;
92 }
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040093 if (auto outputImage = [picker outputImage]) {
94 [photoView setBordered:NO];
95 auto image = [picker inputImage];
96 CGFloat newSize = MIN(image.size.height, image.size.width);
97 outputImage = [outputImage cropImageToSize:CGSizeMake(newSize, newSize)];
98 [photoView setImage: [outputImage roundCorners: outputImage.size.height * 0.5]];
99 [addProfilePhotoImage setHidden:YES];
100 auto imageToBytes = QByteArray::fromNSData([outputImage TIFFRepresentation]).toBase64();
101 std::string imageToString = std::string(imageToBytes.constData(), imageToBytes.length());
102 self.accountModel->setAvatar(self.selectedAccountID, imageToString);
103 } else if(!photoView.image) {
104 [photoView setBordered:YES];
105 [addProfilePhotoImage setHidden:NO];
106 }
107}
108
109#pragma mark - NSTextFieldDelegate methods
110
111- (BOOL)control:(NSControl *)control textShouldBeginEditing:(NSText *)fieldEditor
112{
113 return YES;
114}
115
116- (IBAction)triggerAdwancedSettings: (NSButton *)sender {
117 [self.delegate triggerAdvancedOptions];
118}
119
120- (void) setSelectedAccount:(std::string) account {
121 self.selectedAccountID = account;
122 [self updateView];
123}
124
125-(void)updateView {
126 const auto& account = accountModel->getAccountInfo(self.selectedAccountID);
127 QByteArray ba = QByteArray::fromStdString(account.profileInfo.avatar);
128
129 QVariant photo = GlobalInstances::pixmapManipulator().personPhoto(ba, nil);
130 if(QtMac::toNSImage(qvariant_cast<QPixmap>(photo))) {
131 [photoView setBordered:NO];
132 NSImage *image = QtMac::toNSImage(qvariant_cast<QPixmap>(photo));
133 CGFloat newSize = MIN(image.size.height, image.size.width);
134 image = [image cropImageToSize:CGSizeMake(newSize, newSize)];
135 [photoView setImage: [image roundCorners: image.size.height * 0.5]];
136 [addProfilePhotoImage setHidden:YES];
137 } else {
138 [photoView setImage:nil];
139 [photoView setBordered:YES];
140 [addProfilePhotoImage setHidden:NO];
141 }
142 NSString* displayName = @(account.profileInfo.alias.c_str());
143 [displayNameField setStringValue:displayName];
144
145 NSMutableAttributedString *colorTitle = [[NSMutableAttributedString alloc] initWithAttributedString:[removeAccountButton attributedTitle]];
146 NSRange titleRange = NSMakeRange(0, [colorTitle length]);
147 [colorTitle addAttribute:NSForegroundColorAttributeName value:[NSColor errorColor] range:titleRange];
148 [removeAccountButton setAttributedTitle:colorTitle];
149 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
150 [passwordField setStringValue: @(accountProperties.password.c_str())];
151 [proxyField setStringValue: @(accountProperties.routeset.c_str())];
152 [userNameField setStringValue: @(accountProperties.username.c_str())];
153 [serverField setStringValue: @(accountProperties.hostname.c_str())];
154 [voicemailField setStringValue: @(accountProperties.mailbox.c_str())];
155 self.accountEnabled = account.enabled;
156}
157
158#pragma mark - Actions
159
160- (IBAction)editPhoto:(id)sender
161{
162 auto pictureTaker = [IKPictureTaker pictureTaker];
163
164 [pictureTaker beginPictureTakerSheetForWindow:[self.view window]
165 withDelegate:self
166 didEndSelector:@selector(pictureTakerDidEnd:returnCode:contextInfo:)
167 contextInfo:nil];
168
169}
170
171- (IBAction)enableAccount: (NSButton *)sender {
172 const auto& account = accountModel->getAccountInfo(self.selectedAccountID);
173 self.accountModel->enableAccount(self.selectedAccountID, !account.enabled);
174 self.accountEnabled = account.enabled;
175 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
176 self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
177}
178
179- (IBAction)removeAccount:(id)sender
180{
181 NSAlert *alert = [[NSAlert alloc] init];
182 [alert addButtonWithTitle:@"OK"];
183 [alert addButtonWithTitle:@"Cancel"];
184 [alert setMessageText: NSLocalizedString(@"Remove account",
185 @"Remove account alert title")];
186 [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.",
187 @"Remove account alert message")];
188
189 if ([alert runModal] == NSAlertFirstButtonReturn) {
190 self.accountModel->removeAccount(self.selectedAccountID);
191 }
192}
193
194- (IBAction)changeEditingMode:(id)sender
195{
196 if([userNameField isEditable]) {
197 [self setEditingMode:NO];
198 return;
199 }
200 [self setEditingMode:YES];
201}
202
203-(void) setEditingMode:(BOOL) shouldEdit {
204 [userNameField setEditable:shouldEdit];
205 [passwordField setEditable:shouldEdit];
206 [proxyField setEditable:shouldEdit];
207 [voicemailField setEditable:shouldEdit];
208 [serverField setEditable:shouldEdit];
209 [userNameField setDrawsBackground:!shouldEdit];
210 [passwordField setDrawsBackground:!shouldEdit];
211 [proxyField setDrawsBackground:!shouldEdit];
212 [voicemailField setDrawsBackground:!shouldEdit];
213 [serverField setDrawsBackground:!shouldEdit];
214 [userNameField setBezeled:shouldEdit];
215 [passwordField setBezeled:shouldEdit];
216 [proxyField setBezeled:shouldEdit];
217 [voicemailField setBezeled:shouldEdit];
218 [serverField setBezeled:shouldEdit];
219 if(shouldEdit) {
220 [serverField setBezelStyle:NSTextFieldSquareBezel];
221 [userNameField setBezelStyle:NSTextFieldSquareBezel];
222 [passwordField setBezelStyle:NSTextFieldSquareBezel];
223 [proxyField setBezelStyle:NSTextFieldSquareBezel];
224 [voicemailField setBezelStyle:NSTextFieldSquareBezel];
225 [userNameField becomeFirstResponder];
226 [editAccountButton setTitle:@"Done"];
227 return;
228 }
229 [self saveAccount];
230 [editAccountButton setTitle:@"Edit Account"];
231 [self.view resignFirstResponder];
232}
233
234-(void) saveAccount {
235 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
236 accountProperties.hostname = [serverField.stringValue UTF8String];
237 accountProperties.password = [passwordField.stringValue UTF8String];
238 accountProperties.username = [userNameField.stringValue UTF8String];
239 accountProperties.routeset = [proxyField.stringValue UTF8String];
240 accountProperties.mailbox = [voicemailField.stringValue UTF8String];
241 self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
242}
243
244#pragma mark - NSTextFieldDelegate delegate methods
245
246- (void)controlTextDidChange:(NSNotification *)notif
247{
248 NSTextField* textField = [notif object];
249 if (textField.tag != DISPLAYNAME) {
250 return;
251 }
252 NSString* displayName = textField.stringValue;
253
254 [NSObject cancelPreviousPerformRequestsWithTarget:self];
255 self.accountModel->setAlias(self.selectedAccountID, [displayName UTF8String]);
256 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
257 self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
258}
259
260@end