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