Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015-2017 Savoir-faire Linux Inc. |
| 3 | * Author: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com> |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 4 | * Author: Olivier Soldano <olivier.soldano@savoirfairelinux.com> |
| 5 | * Author: Anthony Léonard <anthony.leonard@savoirfairelinux.com> |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 6 | * |
| 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 Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 30 | #import <globalinstances.h> |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 31 | #import <QItemSelectionModel.h> |
| 32 | #import <interfaces/pixmapmanipulatori.h> |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 33 | #import <api/newaccountmodel.h> |
| 34 | #import <api/account.h> |
Kateryna Kostiuk | 312f913 | 2017-04-18 12:09:06 -0400 | [diff] [blame] | 35 | |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 36 | //RING |
| 37 | #import "views/AccountMenuItemView.h" |
Kateryna Kostiuk | a16c986 | 2017-05-03 13:30:14 -0400 | [diff] [blame] | 38 | #import "AccountSelectionManager.h" |
Anthony Léonard | 9bebf1d | 2017-12-21 14:33:51 -0500 | [diff] [blame] | 39 | #import "RingWindowController.h" |
Kateryna Kostiuk | d73f960 | 2018-07-24 13:51:28 -0400 | [diff] [blame] | 40 | #import "utils.h" |
| 41 | #import "views/NSColor+RingTheme.h" |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 42 | |
Kateryna Kostiuk | 04a5612 | 2018-08-27 10:51:27 -0400 | [diff] [blame] | 43 | @interface NSMenu () |
| 44 | - (void) _setHasPadding: (BOOL) pad onEdge: (int) whatEdge; |
| 45 | @end |
| 46 | |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 47 | @interface ChooseAccountVC () <NSMenuDelegate> |
| 48 | |
| 49 | @end |
| 50 | |
| 51 | @implementation ChooseAccountVC { |
Kateryna Kostiuk | 312f913 | 2017-04-18 12:09:06 -0400 | [diff] [blame] | 52 | |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 53 | __unsafe_unretained IBOutlet NSImageView* profileImage; |
Kateryna Kostiuk | d73f960 | 2018-07-24 13:51:28 -0400 | [diff] [blame] | 54 | __unsafe_unretained IBOutlet NSTextField* accountStatus; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 55 | __unsafe_unretained IBOutlet NSPopUpButton* accountSelectionButton; |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 56 | lrc::api::NewAccountModel* accMdl_; |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 57 | AccountSelectionManager* accountSelectionManager_; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 58 | } |
| 59 | Boolean menuIsOpen; |
| 60 | Boolean menuNeedsUpdate; |
| 61 | NSMenu* accountsMenu; |
| 62 | NSMenuItem* selectedMenuItem; |
Kateryna Kostiuk | 194f21e | 2018-10-22 16:21:03 -0400 | [diff] [blame^] | 63 | NSMutableDictionary* menuItemsTags; |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 64 | |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 65 | -(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil model:(lrc::api::NewAccountModel*) accMdl delegate:(id <ChooseAccountDelegate> )mainWindow |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 66 | { |
| 67 | accMdl_ = accMdl; |
| 68 | accountSelectionManager_ = [[AccountSelectionManager alloc] initWithAccountModel:accMdl_]; |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 69 | self.delegate = mainWindow; |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 70 | return [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; |
| 71 | } |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 72 | |
| 73 | - (void)awakeFromNib |
| 74 | { |
| 75 | [profileImage setWantsLayer: YES]; |
| 76 | profileImage.layer.cornerRadius = profileImage.frame.size.width / 2; |
| 77 | profileImage.layer.masksToBounds = YES; |
Kateryna Kostiuk | d73f960 | 2018-07-24 13:51:28 -0400 | [diff] [blame] | 78 | profileImage.layer.backgroundColor = [[NSColor ringGreyLight] CGColor]; |
Kateryna Kostiuk | 065aaf2 | 2017-07-07 13:04:23 -0400 | [diff] [blame] | 79 | |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 80 | accountsMenu = [[NSMenu alloc] initWithTitle:@""]; |
Kateryna Kostiuk | 04a5612 | 2018-08-27 10:51:27 -0400 | [diff] [blame] | 81 | if ([accountsMenu respondsToSelector: @selector(_setHasPadding:onEdge:)]) |
| 82 | { |
| 83 | [accountsMenu _setHasPadding: NO onEdge: 1]; |
| 84 | [accountsMenu _setHasPadding: NO onEdge: 3]; |
| 85 | } |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 86 | [accountsMenu setDelegate:self]; |
| 87 | accountSelectionButton.menu = accountsMenu; |
Kateryna Kostiuk | d73f960 | 2018-07-24 13:51:28 -0400 | [diff] [blame] | 88 | [accountSelectionButton setAutoenablesItems:NO]; |
Kateryna Kostiuk | 194f21e | 2018-10-22 16:21:03 -0400 | [diff] [blame^] | 89 | menuItemsTags = [[NSMutableDictionary alloc] init]; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 90 | [self update]; |
| 91 | |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 92 | QObject::connect(accMdl_, |
| 93 | &lrc::api::NewAccountModel::accountAdded, |
Kateryna Kostiuk | ab499f4 | 2018-04-16 12:27:33 -0400 | [diff] [blame] | 94 | [self] (const std::string& accountID) { |
Kateryna Kostiuk | 312f913 | 2017-04-18 12:09:06 -0400 | [diff] [blame] | 95 | [self update]; |
Kateryna Kostiuk | ab499f4 | 2018-04-16 12:27:33 -0400 | [diff] [blame] | 96 | @try { |
| 97 | auto& account = [self selectedAccount]; |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 98 | [self.delegate selectAccount:account currentRemoved: NO]; |
Kateryna Kostiuk | ab499f4 | 2018-04-16 12:27:33 -0400 | [diff] [blame] | 99 | } |
| 100 | @catch (NSException * e) { |
| 101 | NSLog(@"account selection failed"); |
| 102 | } |
Kateryna Kostiuk | 312f913 | 2017-04-18 12:09:06 -0400 | [diff] [blame] | 103 | }); |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 104 | QObject::connect(accMdl_, |
| 105 | &lrc::api::NewAccountModel::accountRemoved, |
Kateryna Kostiuk | ab499f4 | 2018-04-16 12:27:33 -0400 | [diff] [blame] | 106 | [self] (const std::string& accountID) { |
| 107 | if ([self selectedAccount].id.compare(accountID) == 0) { |
| 108 | [accountSelectionManager_ clearSelectedAccount]; |
| 109 | } |
| 110 | @try { |
| 111 | auto& account = [self selectedAccount]; |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 112 | [self.delegate selectAccount:account currentRemoved: YES]; |
Kateryna Kostiuk | ab499f4 | 2018-04-16 12:27:33 -0400 | [diff] [blame] | 113 | } |
| 114 | @catch (NSException * e) { |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 115 | [self.delegate allAccountsDeleted]; |
Kateryna Kostiuk | ab499f4 | 2018-04-16 12:27:33 -0400 | [diff] [blame] | 116 | } |
Kateryna Kostiuk | 312f913 | 2017-04-18 12:09:06 -0400 | [diff] [blame] | 117 | [self update]; |
| 118 | }); |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 119 | QObject::connect(accMdl_, |
| 120 | &lrc::api::NewAccountModel::profileUpdated, |
Kateryna Kostiuk | ab499f4 | 2018-04-16 12:27:33 -0400 | [diff] [blame] | 121 | [self] (const std::string& accountID) { |
Kateryna Kostiuk | 312f913 | 2017-04-18 12:09:06 -0400 | [diff] [blame] | 122 | [self update]; |
| 123 | }); |
Kateryna Kostiuk | d73f960 | 2018-07-24 13:51:28 -0400 | [diff] [blame] | 124 | QObject::connect(accMdl_, |
| 125 | &lrc::api::NewAccountModel::accountStatusChanged, |
| 126 | [self] (const std::string& accountID) { |
Kateryna Kostiuk | 43a7923 | 2018-10-18 15:45:18 -0400 | [diff] [blame] | 127 | [self updateMenuItemForAccount:accountID]; |
Kateryna Kostiuk | 194f21e | 2018-10-22 16:21:03 -0400 | [diff] [blame^] | 128 | if([self selectedAccount].id == accountID) { |
| 129 | // update account state |
| 130 | [self updatePhoto]; |
| 131 | [self setPopUpButtonSelection]; |
| 132 | } |
Kateryna Kostiuk | d73f960 | 2018-07-24 13:51:28 -0400 | [diff] [blame] | 133 | }); |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 134 | } |
| 135 | |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 136 | -(const lrc::api::account::Info&) selectedAccount |
| 137 | { |
Anthony Léonard | 72128c9 | 2017-12-26 16:48:39 -0500 | [diff] [blame] | 138 | try { |
| 139 | return [accountSelectionManager_ savedAccount]; |
Kateryna Kostiuk | ab499f4 | 2018-04-16 12:27:33 -0400 | [diff] [blame] | 140 | } catch (NSException *ex) { |
Anthony Léonard | 72128c9 | 2017-12-26 16:48:39 -0500 | [diff] [blame] | 141 | auto accountList = accMdl_->getAccountList(); |
| 142 | if (!accountList.empty()) { |
| 143 | const auto& fallbackAccount = accMdl_->getAccountInfo(accountList.at(0)); |
Kateryna Kostiuk | 194f21e | 2018-10-22 16:21:03 -0400 | [diff] [blame^] | 144 | [accountSelectionManager_ setSavedAccount:fallbackAccount]; |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 145 | return fallbackAccount; |
Anthony Léonard | 72128c9 | 2017-12-26 16:48:39 -0500 | [diff] [blame] | 146 | } else { |
| 147 | NSException* noAccEx = [NSException |
| 148 | exceptionWithName:@"NoAccountException" |
| 149 | reason:@"No account in AccountModel" |
| 150 | userInfo:nil]; |
| 151 | @throw noAccEx; |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 152 | } |
| 153 | } |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 154 | } |
| 155 | |
Kateryna Kostiuk | 43a7923 | 2018-10-18 15:45:18 -0400 | [diff] [blame] | 156 | -(void) updateMenuItemForAccount: (const std::string&) accountID { |
Kateryna Kostiuk | 194f21e | 2018-10-22 16:21:03 -0400 | [diff] [blame^] | 157 | NSMenuItem *item =[accountsMenu itemWithTag:[menuItemsTags[@(accountID.c_str())] intValue]]; |
| 158 | if(!item) {return;} |
| 159 | AccountMenuItemView *itemView =item.view; |
| 160 | if(!itemView) {return;} |
| 161 | [self configureView:itemView forAccount:accountID forMenuItem: item]; |
Kateryna Kostiuk | 43a7923 | 2018-10-18 15:45:18 -0400 | [diff] [blame] | 162 | } |
| 163 | |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 164 | -(void) updateMenu { |
| 165 | [accountsMenu removeAllItems]; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 166 | |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 167 | auto accList = accMdl_->getAccountList(); |
| 168 | |
| 169 | for (std::string accId : accList) { |
| 170 | auto& account = accMdl_->getAccountInfo(accId); |
| 171 | |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 172 | NSMenuItem* menuBarItem = [[NSMenuItem alloc] |
Kateryna Kostiuk | 312f913 | 2017-04-18 12:09:06 -0400 | [diff] [blame] | 173 | initWithTitle:[self itemTitleForAccount:account] |
| 174 | action:NULL |
| 175 | keyEquivalent:@""]; |
| 176 | |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 177 | AccountMenuItemView *itemView = [[AccountMenuItemView alloc] initWithFrame:CGRectZero]; |
Kateryna Kostiuk | 194f21e | 2018-10-22 16:21:03 -0400 | [diff] [blame^] | 178 | [self configureView:itemView forAccount:accId forMenuItem: menuBarItem]; |
| 179 | int itemTag = arc4random_uniform(1000); |
| 180 | menuItemsTags[@(accId.c_str())] = [NSNumber numberWithInt: itemTag]; |
| 181 | [menuBarItem setTag:itemTag]; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 182 | [menuBarItem setView:itemView]; |
| 183 | [accountsMenu addItem:menuBarItem]; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 184 | } |
Kateryna Kostiuk | d73f960 | 2018-07-24 13:51:28 -0400 | [diff] [blame] | 185 | |
| 186 | // create "add a new account" menu item |
| 187 | NSMenuItem* menuBarItem = [[NSMenuItem alloc] |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 188 | initWithTitle:@"Add Account" |
Kateryna Kostiuk | d73f960 | 2018-07-24 13:51:28 -0400 | [diff] [blame] | 189 | action:nil |
| 190 | keyEquivalent:@""]; |
| 191 | AccountMenuItemView *itemView = [[AccountMenuItemView alloc] initWithFrame:CGRectZero]; |
| 192 | [itemView.accountAvatar setHidden:YES]; |
| 193 | [itemView.accountStatus setHidden:YES]; |
| 194 | [itemView.accountTypeLabel setHidden:YES]; |
| 195 | [itemView.userNameLabel setHidden:YES]; |
| 196 | [itemView.accountLabel setHidden:YES]; |
| 197 | [itemView.createNewAccount setAction:@selector(createNewAccount:)]; |
| 198 | [itemView.createNewAccount setTarget:self]; |
| 199 | [menuBarItem setView: itemView]; |
| 200 | [accountsMenu addItem: menuBarItem]; |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 201 | [profileImage setHidden:accList.empty()]; |
| 202 | [accountStatus setHidden:accList.empty()]; |
Kateryna Kostiuk | d73f960 | 2018-07-24 13:51:28 -0400 | [diff] [blame] | 203 | } |
| 204 | |
Kateryna Kostiuk | 194f21e | 2018-10-22 16:21:03 -0400 | [diff] [blame^] | 205 | -(void) configureView: (AccountMenuItemView *) itemView forAccount:(const std::string&) accountId forMenuItem:(NSMenuItem *) item { |
Kateryna Kostiuk | d73f960 | 2018-07-24 13:51:28 -0400 | [diff] [blame] | 206 | auto& account = accMdl_->getAccountInfo(accountId); |
Kateryna Kostiuk | 194f21e | 2018-10-22 16:21:03 -0400 | [diff] [blame^] | 207 | item.attributedTitle = [self attributedItemTitleForAccount:account]; |
Kateryna Kostiuk | d73f960 | 2018-07-24 13:51:28 -0400 | [diff] [blame] | 208 | [itemView.accountLabel setStringValue:@(account.profileInfo.alias.c_str())]; |
| 209 | NSString* userNameString = [self nameForAccount: account]; |
| 210 | [itemView.userNameLabel setStringValue:userNameString]; |
Kateryna Kostiuk | 43a7923 | 2018-10-18 15:45:18 -0400 | [diff] [blame] | 211 | NSData *imageData = [[NSData alloc] initWithBase64EncodedString:@(account.profileInfo.avatar.c_str()) options:NSDataBase64DecodingIgnoreUnknownCharacters]; |
| 212 | NSImage *image = [[NSImage alloc] initWithData:imageData]; |
| 213 | if(image) { |
| 214 | [itemView.accountAvatar setImage: image]; |
Kateryna Kostiuk | d73f960 | 2018-07-24 13:51:28 -0400 | [diff] [blame] | 215 | } else { |
| 216 | [itemView.accountAvatar setImage: [NSImage imageNamed:@"default_avatar_overlay.png"]]; |
| 217 | } |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 218 | [itemView.accountStatus setHidden:!account.enabled]; |
Kateryna Kostiuk | d73f960 | 2018-07-24 13:51:28 -0400 | [diff] [blame] | 219 | switch (account.profileInfo.type) { |
| 220 | case lrc::api::profile::Type::SIP: |
| 221 | [itemView.accountTypeLabel setStringValue:@"SIP"]; |
| 222 | break; |
| 223 | case lrc::api::profile::Type::RING: |
| 224 | [itemView.accountTypeLabel setStringValue:@"RING"]; |
| 225 | break; |
| 226 | default: |
| 227 | break; |
| 228 | } |
| 229 | [itemView.createNewAccount setHidden:YES]; |
| 230 | [itemView.createNewAccountImage setHidden:YES]; |
| 231 | } |
| 232 | |
| 233 | |
| 234 | - (void)createNewAccount:(id)sender { |
| 235 | [accountSelectionButton.menu cancelTrackingWithoutAnimation]; |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 236 | [self.delegate createNewAccount]; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 237 | } |
| 238 | |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 239 | -(void) updatePhoto |
| 240 | { |
Anthony Léonard | 72128c9 | 2017-12-26 16:48:39 -0500 | [diff] [blame] | 241 | @try { |
| 242 | auto& account = [self selectedAccount]; |
| 243 | if(account.profileInfo.type == lrc::api::profile::Type::INVALID) |
| 244 | return; |
Kateryna Kostiuk | 43a7923 | 2018-10-18 15:45:18 -0400 | [diff] [blame] | 245 | NSData *imageData = [[NSData alloc] initWithBase64EncodedString:@(account.profileInfo.avatar.c_str()) options:NSDataBase64DecodingIgnoreUnknownCharacters]; |
| 246 | NSImage *image = [[NSImage alloc] initWithData:imageData]; |
| 247 | if(image) { |
| 248 | [profileImage setImage: image]; |
Kateryna Kostiuk | d73f960 | 2018-07-24 13:51:28 -0400 | [diff] [blame] | 249 | } else { |
| 250 | [profileImage setImage: [NSImage imageNamed:@"default_avatar_overlay.png"]]; |
| 251 | } |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 252 | [accountStatus setHidden:!account.enabled]; |
Anthony Léonard | 72128c9 | 2017-12-26 16:48:39 -0500 | [diff] [blame] | 253 | } |
| 254 | @catch (NSException *ex) { |
| 255 | NSLog(@"Caught exception %@: %@", [ex name], [ex reason]); |
| 256 | } |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 257 | } |
| 258 | |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 259 | -(NSString*) nameForAccount:(const lrc::api::account::Info&) account { |
Kateryna Kostiuk | d73f960 | 2018-07-24 13:51:28 -0400 | [diff] [blame] | 260 | return bestIDForAccount(account); |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | -(NSString*) itemTitleForAccount:(const lrc::api::account::Info&) account { |
Kateryna Kostiuk | d73f960 | 2018-07-24 13:51:28 -0400 | [diff] [blame] | 264 | NSString* alias = bestNameForAccount(account); |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 265 | NSString* userNameString = [self nameForAccount: account]; |
Kateryna Kostiuk | dff4c0e | 2018-08-31 10:58:32 -0400 | [diff] [blame] | 266 | if([alias isEqualToString:userNameString] || [userNameString length] == 0) { |
| 267 | return alias; |
Kateryna Kostiuk | a724846 | 2017-04-18 16:15:31 -0400 | [diff] [blame] | 268 | } |
Kateryna Kostiuk | dff4c0e | 2018-08-31 10:58:32 -0400 | [diff] [blame] | 269 | alias = [NSString stringWithFormat: @"%@\n", alias]; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 270 | return [alias stringByAppendingString:userNameString]; |
| 271 | } |
| 272 | |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 273 | - (NSAttributedString*) attributedItemTitleForAccount:(const lrc::api::account::Info&) account { |
Kateryna Kostiuk | d73f960 | 2018-07-24 13:51:28 -0400 | [diff] [blame] | 274 | NSString* alias = bestNameForAccount(account); |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 275 | NSString* userNameString = [self nameForAccount: account]; |
| 276 | NSFont *fontAlias = [NSFont userFontOfSize:14.0]; |
| 277 | NSFont *fontUserName = [NSFont userFontOfSize:11.0]; |
| 278 | NSColor *colorAlias = [NSColor labelColor]; |
| 279 | NSColor *colorAUserName = [NSColor secondaryLabelColor]; |
| 280 | NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; |
| 281 | paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail; |
| 282 | NSDictionary *aliasAttrs = [NSDictionary dictionaryWithObjectsAndKeys:fontAlias,NSFontAttributeName, |
| 283 | colorAlias,NSForegroundColorAttributeName, |
| 284 | paragraphStyle,NSParagraphStyleAttributeName, nil]; |
| 285 | NSDictionary *userNameAttrs = [NSDictionary dictionaryWithObjectsAndKeys:fontUserName,NSFontAttributeName, |
| 286 | colorAUserName,NSForegroundColorAttributeName, |
| 287 | paragraphStyle,NSParagraphStyleAttributeName, nil]; |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 288 | |
| 289 | if([alias isEqualToString:userNameString] || [userNameString length] == 0) { |
| 290 | paragraphStyle.paragraphSpacingBefore = 20; |
| 291 | aliasAttrs = [NSDictionary dictionaryWithObjectsAndKeys:fontAlias,NSFontAttributeName, |
| 292 | colorAlias,NSForegroundColorAttributeName, |
| 293 | paragraphStyle,NSParagraphStyleAttributeName, nil]; |
| 294 | return [[NSAttributedString alloc] initWithString:alias attributes:aliasAttrs]; |
| 295 | } |
| 296 | alias = [NSString stringWithFormat: @"%@\n", alias]; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 297 | NSAttributedString* attributedString = [[NSAttributedString alloc] initWithString:alias attributes:aliasAttrs]; |
| 298 | NSAttributedString* attributedStringSecond= [[NSAttributedString alloc] initWithString:userNameString attributes:userNameAttrs]; |
| 299 | NSMutableAttributedString *result = [[NSMutableAttributedString alloc] init]; |
| 300 | [result appendAttributedString:attributedString]; |
| 301 | [result appendAttributedString:attributedStringSecond]; |
| 302 | return result; |
| 303 | } |
| 304 | |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 305 | -(void) update { |
| 306 | if(menuIsOpen) { |
| 307 | return; |
| 308 | } |
| 309 | [self updateMenu]; |
Kateryna Kostiuk | 312f913 | 2017-04-18 12:09:06 -0400 | [diff] [blame] | 310 | [self setPopUpButtonSelection]; |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 311 | [self updatePhoto]; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 312 | } |
| 313 | |
Kateryna Kostiuk | 312f913 | 2017-04-18 12:09:06 -0400 | [diff] [blame] | 314 | -(void) setPopUpButtonSelection { |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 315 | if(accountsMenu.itemArray.count == 0) { |
| 316 | [self.view setHidden:YES]; |
| 317 | return; |
| 318 | } |
Kateryna Kostiuk | 312f913 | 2017-04-18 12:09:06 -0400 | [diff] [blame] | 319 | [self.view setHidden:NO]; |
Anthony Léonard | 72128c9 | 2017-12-26 16:48:39 -0500 | [diff] [blame] | 320 | @try { |
| 321 | auto& account = [self selectedAccount]; |
| 322 | if(account.profileInfo.type == lrc::api::profile::Type::INVALID){ |
| 323 | return; |
| 324 | } |
Kateryna Kostiuk | 194f21e | 2018-10-22 16:21:03 -0400 | [diff] [blame^] | 325 | [accountSelectionButton selectItemWithTag:[menuItemsTags[@(account.id.c_str())] intValue]]; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 326 | } |
Anthony Léonard | 72128c9 | 2017-12-26 16:48:39 -0500 | [diff] [blame] | 327 | @catch (NSException *ex) { |
| 328 | NSLog(@"Caught exception %@: %@", [ex name], [ex reason]); |
| 329 | } |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | #pragma mark - NSPopUpButton item selection |
| 333 | |
| 334 | - (IBAction)itemChanged:(id)sender { |
Kateryna Kostiuk | 04a5612 | 2018-08-27 10:51:27 -0400 | [diff] [blame] | 335 | NSInteger row = [(NSPopUpButton *)sender indexOfSelectedItem]; |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 336 | auto accList = accMdl_->getAccountList(); |
| 337 | if (row >= accList.size()) |
Kateryna Kostiuk | a16c986 | 2017-05-03 13:30:14 -0400 | [diff] [blame] | 338 | return; |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 339 | |
| 340 | auto& account = accMdl_->getAccountInfo(accList[row]); |
| 341 | [accountSelectionManager_ setSavedAccount:account]; |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 342 | [self.delegate selectAccount:account currentRemoved: NO]; |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 343 | [self updatePhoto]; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | #pragma mark - NSMenuDelegate |
| 347 | - (void)menuWillOpen:(NSMenu *)menu { |
| 348 | menuIsOpen = true; |
| 349 | // remember selected item to remove highlighting when menu is open |
| 350 | selectedMenuItem = [accountSelectionButton selectedItem]; |
| 351 | } |
| 352 | - (void)menuDidClose:(NSMenu *)menu { |
| 353 | |
| 354 | menuIsOpen = false; |
| 355 | } |
| 356 | |
| 357 | - (void)menu:(NSMenu *)menu willHighlightItem:(nullable NSMenuItem *)item { |
| 358 | if (!selectedMenuItem || selectedMenuItem == item) { |
| 359 | return; |
| 360 | } |
| 361 | int index = [menu indexOfItem:selectedMenuItem]; |
| 362 | [menu removeItemAtIndex:index]; |
| 363 | [menu insertItem:selectedMenuItem atIndex:index]; |
| 364 | [accountSelectionButton selectItemAtIndex:index]; |
| 365 | selectedMenuItem = nil; |
| 366 | } |
| 367 | |
Kateryna Kostiuk | abf4e27 | 2017-04-18 14:18:00 -0400 | [diff] [blame] | 368 | -(void) enable { |
| 369 | [accountSelectionButton setEnabled:YES]; |
| 370 | } |
| 371 | -(void) disable { |
| 372 | [accountSelectionButton setEnabled:NO]; |
| 373 | } |
| 374 | |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 375 | @end |