blob: 5ebf969d913b2ebd86c2add04c0bbf5b57d17559 [file] [log] [blame]
Kateryna Kostiuk13b76882017-03-30 09:18:44 -04001/*
2 * Copyright (C) 2015-2017 Savoir-faire Linux Inc.
3 * Author: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com>
Anthony Léonard49cb2912017-11-13 16:15:39 -05004 * Author: Olivier Soldano <olivier.soldano@savoirfairelinux.com>
5 * Author: Anthony Léonard <anthony.leonard@savoirfairelinux.com>
Kateryna Kostiuk13b76882017-03-30 09:18:44 -04006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22#import "ChooseAccountVC.h"
23
24//Qt
25#import <QSize>
26#import <QtMacExtras/qmacfunctions.h>
27#import <QPixmap>
28
29//LRC
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040030#import <interfaces/pixmapmanipulatori.h>
Anthony Léonard49cb2912017-11-13 16:15:39 -050031#import <api/newaccountmodel.h>
32#import <api/account.h>
Kateryna Kostiuk312f9132017-04-18 12:09:06 -040033
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040034//RING
35#import "views/AccountMenuItemView.h"
Kateryna Kostiuka16c9862017-05-03 13:30:14 -040036#import "AccountSelectionManager.h"
Anthony Léonard9bebf1d2017-12-21 14:33:51 -050037#import "RingWindowController.h"
Kateryna Kostiukd73f9602018-07-24 13:51:28 -040038#import "utils.h"
39#import "views/NSColor+RingTheme.h"
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -050040#import "views/RoundedTextField.h"
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040041
Kateryna Kostiuk04a56122018-08-27 10:51:27 -040042@interface NSMenu ()
43- (void) _setHasPadding: (BOOL) pad onEdge: (int) whatEdge;
44@end
45
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040046@interface ChooseAccountVC () <NSMenuDelegate>
47
48@end
49
50@implementation ChooseAccountVC {
Kateryna Kostiuk312f9132017-04-18 12:09:06 -040051
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040052 __unsafe_unretained IBOutlet NSImageView* profileImage;
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -050053 __unsafe_unretained IBOutlet RoundedTextField* accountStatus;
54 __unsafe_unretained IBOutlet NSTextField* selectedAccountTitle;
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040055 __unsafe_unretained IBOutlet NSPopUpButton* accountSelectionButton;
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040056 lrc::api::NewAccountModel* accMdl_;
Anthony Léonard49cb2912017-11-13 16:15:39 -050057 AccountSelectionManager* accountSelectionManager_;
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040058}
59Boolean menuIsOpen;
60Boolean menuNeedsUpdate;
61NSMenu* accountsMenu;
62NSMenuItem* selectedMenuItem;
Kateryna Kostiuk194f21e2018-10-22 16:21:03 -040063NSMutableDictionary* menuItemsTags;
Anthony Léonard49cb2912017-11-13 16:15:39 -050064
Kateryna Kostiukef66f972018-11-02 17:10:37 -040065-(void) updateWithDelegate:(id <ChooseAccountDelegate> )mainWindow andModel:(lrc::api::NewAccountModel*) accMdl {
Anthony Léonard49cb2912017-11-13 16:15:39 -050066 accMdl_ = accMdl;
Kateryna Kostiukef66f972018-11-02 17:10:37 -040067 accountSelectionManager_ = [[AccountSelectionManager alloc] initWithAccountModel:accMdl_];
68 self.delegate = mainWindow;
69 [self initView];
Anthony Léonard49cb2912017-11-13 16:15:39 -050070}
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040071
Kateryna Kostiukef66f972018-11-02 17:10:37 -040072- (void)initView
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040073{
74 [profileImage setWantsLayer: YES];
75 profileImage.layer.cornerRadius = profileImage.frame.size.width / 2;
76 profileImage.layer.masksToBounds = YES;
Kateryna Kostiukef66f972018-11-02 17:10:37 -040077 profileImage.layer.backgroundColor = [[NSColor disabledControlTextColor] CGColor];
Kateryna Kostiuk065aaf22017-07-07 13:04:23 -040078
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040079 accountsMenu = [[NSMenu alloc] initWithTitle:@""];
Kateryna Kostiuk04a56122018-08-27 10:51:27 -040080 if ([accountsMenu respondsToSelector: @selector(_setHasPadding:onEdge:)])
81 {
82 [accountsMenu _setHasPadding: NO onEdge: 1];
83 [accountsMenu _setHasPadding: NO onEdge: 3];
84 }
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040085 [accountsMenu setDelegate:self];
86 accountSelectionButton.menu = accountsMenu;
Kateryna Kostiukd73f9602018-07-24 13:51:28 -040087 [accountSelectionButton setAutoenablesItems:NO];
Kateryna Kostiuk194f21e2018-10-22 16:21:03 -040088 menuItemsTags = [[NSMutableDictionary alloc] init];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040089 [self update];
90
Kateryna Kostiukef66f972018-11-02 17:10:37 -040091
Anthony Léonard49cb2912017-11-13 16:15:39 -050092 QObject::connect(accMdl_,
93 &lrc::api::NewAccountModel::accountAdded,
Kateryna Kostiukc867eb92020-03-08 13:15:17 -040094 [self] (const QString& accountID) {
Kateryna Kostiuk312f9132017-04-18 12:09:06 -040095 [self update];
Kateryna Kostiukab499f42018-04-16 12:27:33 -040096 @try {
97 auto& account = [self selectedAccount];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040098 [self.delegate selectAccount:account currentRemoved: NO];
Kateryna Kostiukab499f42018-04-16 12:27:33 -040099 }
100 @catch (NSException * e) {
101 NSLog(@"account selection failed");
102 }
Kateryna Kostiuk312f9132017-04-18 12:09:06 -0400103 });
Anthony Léonard49cb2912017-11-13 16:15:39 -0500104 QObject::connect(accMdl_,
105 &lrc::api::NewAccountModel::accountRemoved,
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400106 [self] (const QString& accountID) {
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400107 if ([self selectedAccount].id.compare(accountID) == 0) {
108 [accountSelectionManager_ clearSelectedAccount];
109 }
110 @try {
111 auto& account = [self selectedAccount];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400112 [self.delegate selectAccount:account currentRemoved: YES];
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400113 }
114 @catch (NSException * e) {
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400115 [self.delegate allAccountsDeleted];
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400116 }
Kateryna Kostiuk312f9132017-04-18 12:09:06 -0400117 [self update];
118 });
Anthony Léonard49cb2912017-11-13 16:15:39 -0500119 QObject::connect(accMdl_,
120 &lrc::api::NewAccountModel::profileUpdated,
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400121 [self] (const QString& accountID) {
Kateryna Kostiuk312f9132017-04-18 12:09:06 -0400122 [self update];
123 });
Kateryna Kostiukd73f9602018-07-24 13:51:28 -0400124 QObject::connect(accMdl_,
125 &lrc::api::NewAccountModel::accountStatusChanged,
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400126 [self] (const QString& accountID) {
Kateryna Kostiuk43a79232018-10-18 15:45:18 -0400127 [self updateMenuItemForAccount:accountID];
Kateryna Kostiuk194f21e2018-10-22 16:21:03 -0400128 if([self selectedAccount].id == accountID) {
129 // update account state
130 [self updatePhoto];
131 [self setPopUpButtonSelection];
132 }
Kateryna Kostiukd73f9602018-07-24 13:51:28 -0400133 });
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400134}
135
Kateryna Kostiukef66f972018-11-02 17:10:37 -0400136
Anthony Léonard49cb2912017-11-13 16:15:39 -0500137-(const lrc::api::account::Info&) selectedAccount
138{
Anthony Léonard72128c92017-12-26 16:48:39 -0500139 try {
140 return [accountSelectionManager_ savedAccount];
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400141 } catch (NSException *ex) {
Anthony Léonard72128c92017-12-26 16:48:39 -0500142 auto accountList = accMdl_->getAccountList();
143 if (!accountList.empty()) {
144 const auto& fallbackAccount = accMdl_->getAccountInfo(accountList.at(0));
Kateryna Kostiuk194f21e2018-10-22 16:21:03 -0400145 [accountSelectionManager_ setSavedAccount:fallbackAccount];
Anthony Léonard49cb2912017-11-13 16:15:39 -0500146 return fallbackAccount;
Anthony Léonard72128c92017-12-26 16:48:39 -0500147 } else {
148 NSException* noAccEx = [NSException
149 exceptionWithName:@"NoAccountException"
150 reason:@"No account in AccountModel"
151 userInfo:nil];
152 @throw noAccEx;
Anthony Léonard49cb2912017-11-13 16:15:39 -0500153 }
154 }
Anthony Léonard49cb2912017-11-13 16:15:39 -0500155}
156
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400157-(void) updateMenuItemForAccount: (const QString&) accountID {
158 NSMenuItem *item =[accountsMenu itemWithTag:[menuItemsTags[accountID.toNSString()] intValue]];
Kateryna Kostiuk194f21e2018-10-22 16:21:03 -0400159 if(!item) {return;}
160 AccountMenuItemView *itemView =item.view;
161 if(!itemView) {return;}
162 [self configureView:itemView forAccount:accountID forMenuItem: item];
Kateryna Kostiuk43a79232018-10-18 15:45:18 -0400163}
164
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400165-(void) updateMenu {
166 [accountsMenu removeAllItems];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400167
Anthony Léonard49cb2912017-11-13 16:15:39 -0500168 auto accList = accMdl_->getAccountList();
169
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400170 for (auto accId : accList) {
Anthony Léonard49cb2912017-11-13 16:15:39 -0500171 auto& account = accMdl_->getAccountInfo(accId);
172
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400173 NSMenuItem* menuBarItem = [[NSMenuItem alloc]
Kateryna Kostiuk312f9132017-04-18 12:09:06 -0400174 initWithTitle:[self itemTitleForAccount:account]
175 action:NULL
176 keyEquivalent:@""];
177
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400178 AccountMenuItemView *itemView = [[AccountMenuItemView alloc] initWithFrame:CGRectZero];
Kateryna Kostiuk194f21e2018-10-22 16:21:03 -0400179 [self configureView:itemView forAccount:accId forMenuItem: menuBarItem];
180 int itemTag = arc4random_uniform(1000);
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400181 menuItemsTags[accId.toNSString()] = [NSNumber numberWithInt: itemTag];
Kateryna Kostiuk194f21e2018-10-22 16:21:03 -0400182 [menuBarItem setTag:itemTag];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400183 [menuBarItem setView:itemView];
184 [accountsMenu addItem:menuBarItem];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400185 }
Kateryna Kostiukd73f9602018-07-24 13:51:28 -0400186
187 // create "add a new account" menu item
188 NSMenuItem* menuBarItem = [[NSMenuItem alloc]
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400189 initWithTitle:@"Add Account"
Kateryna Kostiukd73f9602018-07-24 13:51:28 -0400190 action:nil
191 keyEquivalent:@""];
192 AccountMenuItemView *itemView = [[AccountMenuItemView alloc] initWithFrame:CGRectZero];
193 [itemView.accountAvatar setHidden:YES];
194 [itemView.accountStatus setHidden:YES];
195 [itemView.accountTypeLabel setHidden:YES];
196 [itemView.userNameLabel setHidden:YES];
197 [itemView.accountLabel setHidden:YES];
198 [itemView.createNewAccount setAction:@selector(createNewAccount:)];
199 [itemView.createNewAccount setTarget:self];
200 [menuBarItem setView: itemView];
201 [accountsMenu addItem: menuBarItem];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400202 [profileImage setHidden:accList.empty()];
203 [accountStatus setHidden:accList.empty()];
Kateryna Kostiukd73f9602018-07-24 13:51:28 -0400204}
205
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400206-(void) configureView: (AccountMenuItemView *) itemView forAccount:(const QString&) accountId forMenuItem:(NSMenuItem *) item {
Kateryna Kostiukd73f9602018-07-24 13:51:28 -0400207 auto& account = accMdl_->getAccountInfo(accountId);
Kateryna Kostiuk194f21e2018-10-22 16:21:03 -0400208 item.attributedTitle = [self attributedItemTitleForAccount:account];
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400209 [itemView.accountLabel setStringValue:account.profileInfo.alias.toNSString()];
Kateryna Kostiukd73f9602018-07-24 13:51:28 -0400210 NSString* userNameString = [self nameForAccount: account];
211 [itemView.userNameLabel setStringValue:userNameString];
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400212 NSData *imageData = [[NSData alloc] initWithBase64EncodedString:account.profileInfo.avatar.toNSString() options:NSDataBase64DecodingIgnoreUnknownCharacters];
Kateryna Kostiuk43a79232018-10-18 15:45:18 -0400213 NSImage *image = [[NSImage alloc] initWithData:imageData];
214 if(image) {
215 [itemView.accountAvatar setImage: image];
Kateryna Kostiukd73f9602018-07-24 13:51:28 -0400216 } else {
217 [itemView.accountAvatar setImage: [NSImage imageNamed:@"default_avatar_overlay.png"]];
218 }
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -0500219 itemView.accountStatus.bgColor = colorForAccountStatus(account.status);
Kateryna Kostiukd73f9602018-07-24 13:51:28 -0400220 switch (account.profileInfo.type) {
221 case lrc::api::profile::Type::SIP:
222 [itemView.accountTypeLabel setStringValue:@"SIP"];
223 break;
224 case lrc::api::profile::Type::RING:
Kateryna Kostiuk85a334e2018-12-03 15:54:19 -0500225 [itemView.accountTypeLabel setStringValue:@"Jami"];
Kateryna Kostiukd73f9602018-07-24 13:51:28 -0400226 break;
227 default:
228 break;
229 }
230 [itemView.createNewAccount setHidden:YES];
231 [itemView.createNewAccountImage setHidden:YES];
232}
233
234
235- (void)createNewAccount:(id)sender {
236 [accountSelectionButton.menu cancelTrackingWithoutAnimation];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400237 [self.delegate createNewAccount];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400238}
239
Anthony Léonard49cb2912017-11-13 16:15:39 -0500240-(void) updatePhoto
241{
Anthony Léonard72128c92017-12-26 16:48:39 -0500242 @try {
243 auto& account = [self selectedAccount];
244 if(account.profileInfo.type == lrc::api::profile::Type::INVALID)
245 return;
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400246 NSData *imageData = [[NSData alloc] initWithBase64EncodedString:account.profileInfo.avatar.toNSString() options:NSDataBase64DecodingIgnoreUnknownCharacters];
Kateryna Kostiuk43a79232018-10-18 15:45:18 -0400247 NSImage *image = [[NSImage alloc] initWithData:imageData];
248 if(image) {
249 [profileImage setImage: image];
Kateryna Kostiukd73f9602018-07-24 13:51:28 -0400250 } else {
251 [profileImage setImage: [NSImage imageNamed:@"default_avatar_overlay.png"]];
252 }
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -0500253 accountStatus.bgColor = colorForAccountStatus(account.status);
254 [accountStatus setNeedsDisplay: YES];
Anthony Léonard72128c92017-12-26 16:48:39 -0500255 }
256 @catch (NSException *ex) {
257 NSLog(@"Caught exception %@: %@", [ex name], [ex reason]);
258 }
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400259}
260
Anthony Léonard49cb2912017-11-13 16:15:39 -0500261-(NSString*) nameForAccount:(const lrc::api::account::Info&) account {
Kateryna Kostiukd73f9602018-07-24 13:51:28 -0400262 return bestIDForAccount(account);
Anthony Léonard49cb2912017-11-13 16:15:39 -0500263}
264
265-(NSString*) itemTitleForAccount:(const lrc::api::account::Info&) account {
Kateryna Kostiukd73f9602018-07-24 13:51:28 -0400266 NSString* alias = bestNameForAccount(account);
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400267 NSString* userNameString = [self nameForAccount: account];
Kateryna Kostiukdff4c0e2018-08-31 10:58:32 -0400268 if([alias isEqualToString:userNameString] || [userNameString length] == 0) {
269 return alias;
Kateryna Kostiuka7248462017-04-18 16:15:31 -0400270 }
Kateryna Kostiukdff4c0e2018-08-31 10:58:32 -0400271 alias = [NSString stringWithFormat: @"%@\n", alias];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400272 return [alias stringByAppendingString:userNameString];
273}
274
Anthony Léonard49cb2912017-11-13 16:15:39 -0500275- (NSAttributedString*) attributedItemTitleForAccount:(const lrc::api::account::Info&) account {
Kateryna Kostiukd73f9602018-07-24 13:51:28 -0400276 NSString* alias = bestNameForAccount(account);
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400277 NSString* userNameString = [self nameForAccount: account];
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -0500278 NSFont *fontAlias = [NSFont fontWithName:@"Helvetica Neue Light" size:16.0];
Kateryna Kostiukef66f972018-11-02 17:10:37 -0400279 NSFont *fontUserName = [NSFont fontWithName:@"Helvetica Neue Light" size:13.0];
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -0500280 NSColor *colorAlias = [NSColor textColor];
281 NSColor *colorAUserName = [NSColor labelColor];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400282 NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
283 paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
Kateryna Kostiukef66f972018-11-02 17:10:37 -0400284 paragraphStyle.lineSpacing = 3;
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -0500285 NSDictionary *aliasAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
286 fontAlias,NSFontAttributeName,
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400287 colorAlias,NSForegroundColorAttributeName,
288 paragraphStyle,NSParagraphStyleAttributeName, nil];
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -0500289 NSDictionary *userNameAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
290 fontUserName,NSFontAttributeName,
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400291 colorAUserName,NSForegroundColorAttributeName,
292 paragraphStyle,NSParagraphStyleAttributeName, nil];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400293
294 if([alias isEqualToString:userNameString] || [userNameString length] == 0) {
295 paragraphStyle.paragraphSpacingBefore = 20;
296 aliasAttrs = [NSDictionary dictionaryWithObjectsAndKeys:fontAlias,NSFontAttributeName,
297 colorAlias,NSForegroundColorAttributeName,
298 paragraphStyle,NSParagraphStyleAttributeName, nil];
299 return [[NSAttributedString alloc] initWithString:alias attributes:aliasAttrs];
300 }
301 alias = [NSString stringWithFormat: @"%@\n", alias];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400302 NSAttributedString* attributedString = [[NSAttributedString alloc] initWithString:alias attributes:aliasAttrs];
303 NSAttributedString* attributedStringSecond= [[NSAttributedString alloc] initWithString:userNameString attributes:userNameAttrs];
304 NSMutableAttributedString *result = [[NSMutableAttributedString alloc] init];
305 [result appendAttributedString:attributedString];
306 [result appendAttributedString:attributedStringSecond];
307 return result;
308}
309
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400310-(void) update {
311 if(menuIsOpen) {
312 return;
313 }
314 [self updateMenu];
Kateryna Kostiuk312f9132017-04-18 12:09:06 -0400315 [self setPopUpButtonSelection];
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400316 [self updatePhoto];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400317}
318
Kateryna Kostiuk312f9132017-04-18 12:09:06 -0400319-(void) setPopUpButtonSelection {
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400320 if(accountsMenu.itemArray.count == 0) {
321 [self.view setHidden:YES];
322 return;
323 }
Kateryna Kostiuk312f9132017-04-18 12:09:06 -0400324 [self.view setHidden:NO];
Anthony Léonard72128c92017-12-26 16:48:39 -0500325 @try {
326 auto& account = [self selectedAccount];
327 if(account.profileInfo.type == lrc::api::profile::Type::INVALID){
328 return;
329 }
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400330 [accountSelectionButton selectItemWithTag:[menuItemsTags[account.id.toNSString()] intValue]];
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -0500331 [selectedAccountTitle setAttributedStringValue: accountSelectionButton.attributedTitle];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400332 }
Anthony Léonard72128c92017-12-26 16:48:39 -0500333 @catch (NSException *ex) {
334 NSLog(@"Caught exception %@: %@", [ex name], [ex reason]);
335 }
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400336}
337
338#pragma mark - NSPopUpButton item selection
339
340- (IBAction)itemChanged:(id)sender {
Kateryna Kostiuk04a56122018-08-27 10:51:27 -0400341 NSInteger row = [(NSPopUpButton *)sender indexOfSelectedItem];
Anthony Léonard49cb2912017-11-13 16:15:39 -0500342 auto accList = accMdl_->getAccountList();
343 if (row >= accList.size())
Kateryna Kostiuka16c9862017-05-03 13:30:14 -0400344 return;
Anthony Léonard49cb2912017-11-13 16:15:39 -0500345
346 auto& account = accMdl_->getAccountInfo(accList[row]);
347 [accountSelectionManager_ setSavedAccount:account];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400348 [self.delegate selectAccount:account currentRemoved: NO];
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400349 [self updatePhoto];
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -0500350 [selectedAccountTitle setAttributedStringValue: accountSelectionButton.attributedTitle];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400351}
352
Kateryna Kostiukef66f972018-11-02 17:10:37 -0400353- (IBAction)openMenu:(id)sender {
354 [accountSelectionButton performClick:nil];
355}
356
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400357#pragma mark - NSMenuDelegate
358- (void)menuWillOpen:(NSMenu *)menu {
359 menuIsOpen = true;
360 // remember selected item to remove highlighting when menu is open
361 selectedMenuItem = [accountSelectionButton selectedItem];
362}
363- (void)menuDidClose:(NSMenu *)menu {
364
365 menuIsOpen = false;
366}
367
368- (void)menu:(NSMenu *)menu willHighlightItem:(nullable NSMenuItem *)item {
369 if (!selectedMenuItem || selectedMenuItem == item) {
370 return;
371 }
372 int index = [menu indexOfItem:selectedMenuItem];
373 [menu removeItemAtIndex:index];
374 [menu insertItem:selectedMenuItem atIndex:index];
375 [accountSelectionButton selectItemAtIndex:index];
376 selectedMenuItem = nil;
377}
378
Kateryna Kostiukabf4e272017-04-18 14:18:00 -0400379-(void) enable {
380 [accountSelectionButton setEnabled:YES];
381}
382-(void) disable {
383 [accountSelectionButton setEnabled:NO];
384}
385
Kateryna Kostiuke3503842018-12-12 16:39:45 -0500386- (void)selectAccount:(NSString*)accountID {
387 auto accList = accMdl_->getAccountList();
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400388 if (!accList.contains(QString::fromNSString(accountID))) {
389 return;
Kateryna Kostiuke3503842018-12-12 16:39:45 -0500390 }
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400391 auto& account = accMdl_->getAccountInfo(QString::fromNSString(accountID));
392 [accountSelectionManager_ setSavedAccount:account];
393 [self updatePhoto];
394 [self setPopUpButtonSelection];
Kateryna Kostiuke3503842018-12-12 16:39:45 -0500395}
396
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400397@end