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 | -(void) updateWithDelegate:(id <ChooseAccountDelegate> )mainWindow andModel:(lrc::api::NewAccountModel*) accMdl { |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 66 | accMdl_ = accMdl; |
Kateryna Kostiuk | ef66f97 | 2018-11-02 17:10:37 -0400 | [diff] [blame] | 67 | accountSelectionManager_ = [[AccountSelectionManager alloc] initWithAccountModel:accMdl_]; |
| 68 | self.delegate = mainWindow; |
| 69 | [self initView]; |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 70 | } |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 71 | |
Kateryna Kostiuk | ef66f97 | 2018-11-02 17:10:37 -0400 | [diff] [blame] | 72 | - (void)initView |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 73 | { |
| 74 | [profileImage setWantsLayer: YES]; |
| 75 | profileImage.layer.cornerRadius = profileImage.frame.size.width / 2; |
| 76 | profileImage.layer.masksToBounds = YES; |
Kateryna Kostiuk | ef66f97 | 2018-11-02 17:10:37 -0400 | [diff] [blame] | 77 | profileImage.layer.backgroundColor = [[NSColor disabledControlTextColor] CGColor]; |
Kateryna Kostiuk | 065aaf2 | 2017-07-07 13:04:23 -0400 | [diff] [blame] | 78 | |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 79 | accountsMenu = [[NSMenu alloc] initWithTitle:@""]; |
Kateryna Kostiuk | 04a5612 | 2018-08-27 10:51:27 -0400 | [diff] [blame] | 80 | if ([accountsMenu respondsToSelector: @selector(_setHasPadding:onEdge:)]) |
| 81 | { |
| 82 | [accountsMenu _setHasPadding: NO onEdge: 1]; |
| 83 | [accountsMenu _setHasPadding: NO onEdge: 3]; |
| 84 | } |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 85 | [accountsMenu setDelegate:self]; |
| 86 | accountSelectionButton.menu = accountsMenu; |
Kateryna Kostiuk | d73f960 | 2018-07-24 13:51:28 -0400 | [diff] [blame] | 87 | [accountSelectionButton setAutoenablesItems:NO]; |
Kateryna Kostiuk | 194f21e | 2018-10-22 16:21:03 -0400 | [diff] [blame] | 88 | menuItemsTags = [[NSMutableDictionary alloc] init]; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 89 | [self update]; |
| 90 | |
Kateryna Kostiuk | ef66f97 | 2018-11-02 17:10:37 -0400 | [diff] [blame] | 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 | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 94 | [self] (const QString& 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 | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 106 | [self] (const QString& accountID) { |
Kateryna Kostiuk | ab499f4 | 2018-04-16 12:27:33 -0400 | [diff] [blame] | 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 | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 121 | [self] (const QString& 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, |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 126 | [self] (const QString& 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 | |
Kateryna Kostiuk | ef66f97 | 2018-11-02 17:10:37 -0400 | [diff] [blame] | 136 | |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 137 | -(const lrc::api::account::Info&) selectedAccount |
| 138 | { |
Anthony Léonard | 72128c9 | 2017-12-26 16:48:39 -0500 | [diff] [blame] | 139 | try { |
| 140 | return [accountSelectionManager_ savedAccount]; |
Kateryna Kostiuk | ab499f4 | 2018-04-16 12:27:33 -0400 | [diff] [blame] | 141 | } catch (NSException *ex) { |
Anthony Léonard | 72128c9 | 2017-12-26 16:48:39 -0500 | [diff] [blame] | 142 | auto accountList = accMdl_->getAccountList(); |
| 143 | if (!accountList.empty()) { |
| 144 | const auto& fallbackAccount = accMdl_->getAccountInfo(accountList.at(0)); |
Kateryna Kostiuk | 194f21e | 2018-10-22 16:21:03 -0400 | [diff] [blame] | 145 | [accountSelectionManager_ setSavedAccount:fallbackAccount]; |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 146 | return fallbackAccount; |
Anthony Léonard | 72128c9 | 2017-12-26 16:48:39 -0500 | [diff] [blame] | 147 | } else { |
| 148 | NSException* noAccEx = [NSException |
| 149 | exceptionWithName:@"NoAccountException" |
| 150 | reason:@"No account in AccountModel" |
| 151 | userInfo:nil]; |
| 152 | @throw noAccEx; |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 153 | } |
| 154 | } |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 155 | } |
| 156 | |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 157 | -(void) updateMenuItemForAccount: (const QString&) accountID { |
| 158 | NSMenuItem *item =[accountsMenu itemWithTag:[menuItemsTags[accountID.toNSString()] intValue]]; |
Kateryna Kostiuk | 194f21e | 2018-10-22 16:21:03 -0400 | [diff] [blame] | 159 | if(!item) {return;} |
| 160 | AccountMenuItemView *itemView =item.view; |
| 161 | if(!itemView) {return;} |
| 162 | [self configureView:itemView forAccount:accountID forMenuItem: item]; |
Kateryna Kostiuk | 43a7923 | 2018-10-18 15:45:18 -0400 | [diff] [blame] | 163 | } |
| 164 | |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 165 | -(void) updateMenu { |
| 166 | [accountsMenu removeAllItems]; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 167 | |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 168 | auto accList = accMdl_->getAccountList(); |
| 169 | |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 170 | for (auto accId : accList) { |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 171 | auto& account = accMdl_->getAccountInfo(accId); |
| 172 | |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 173 | NSMenuItem* menuBarItem = [[NSMenuItem alloc] |
Kateryna Kostiuk | 312f913 | 2017-04-18 12:09:06 -0400 | [diff] [blame] | 174 | initWithTitle:[self itemTitleForAccount:account] |
| 175 | action:NULL |
| 176 | keyEquivalent:@""]; |
| 177 | |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 178 | AccountMenuItemView *itemView = [[AccountMenuItemView alloc] initWithFrame:CGRectZero]; |
Kateryna Kostiuk | 194f21e | 2018-10-22 16:21:03 -0400 | [diff] [blame] | 179 | [self configureView:itemView forAccount:accId forMenuItem: menuBarItem]; |
| 180 | int itemTag = arc4random_uniform(1000); |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 181 | menuItemsTags[accId.toNSString()] = [NSNumber numberWithInt: itemTag]; |
Kateryna Kostiuk | 194f21e | 2018-10-22 16:21:03 -0400 | [diff] [blame] | 182 | [menuBarItem setTag:itemTag]; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 183 | [menuBarItem setView:itemView]; |
| 184 | [accountsMenu addItem:menuBarItem]; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 185 | } |
Kateryna Kostiuk | d73f960 | 2018-07-24 13:51:28 -0400 | [diff] [blame] | 186 | |
| 187 | // create "add a new account" menu item |
| 188 | NSMenuItem* menuBarItem = [[NSMenuItem alloc] |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 189 | initWithTitle:@"Add Account" |
Kateryna Kostiuk | d73f960 | 2018-07-24 13:51:28 -0400 | [diff] [blame] | 190 | 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 Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 202 | [profileImage setHidden:accList.empty()]; |
| 203 | [accountStatus setHidden:accList.empty()]; |
Kateryna Kostiuk | d73f960 | 2018-07-24 13:51:28 -0400 | [diff] [blame] | 204 | } |
| 205 | |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 206 | -(void) configureView: (AccountMenuItemView *) itemView forAccount:(const QString&) accountId forMenuItem:(NSMenuItem *) item { |
Kateryna Kostiuk | d73f960 | 2018-07-24 13:51:28 -0400 | [diff] [blame] | 207 | auto& account = accMdl_->getAccountInfo(accountId); |
Kateryna Kostiuk | 194f21e | 2018-10-22 16:21:03 -0400 | [diff] [blame] | 208 | item.attributedTitle = [self attributedItemTitleForAccount:account]; |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 209 | [itemView.accountLabel setStringValue:account.profileInfo.alias.toNSString()]; |
Kateryna Kostiuk | d73f960 | 2018-07-24 13:51:28 -0400 | [diff] [blame] | 210 | NSString* userNameString = [self nameForAccount: account]; |
| 211 | [itemView.userNameLabel setStringValue:userNameString]; |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 212 | NSData *imageData = [[NSData alloc] initWithBase64EncodedString:account.profileInfo.avatar.toNSString() options:NSDataBase64DecodingIgnoreUnknownCharacters]; |
Kateryna Kostiuk | 43a7923 | 2018-10-18 15:45:18 -0400 | [diff] [blame] | 213 | NSImage *image = [[NSImage alloc] initWithData:imageData]; |
| 214 | if(image) { |
| 215 | [itemView.accountAvatar setImage: image]; |
Kateryna Kostiuk | d73f960 | 2018-07-24 13:51:28 -0400 | [diff] [blame] | 216 | } else { |
| 217 | [itemView.accountAvatar setImage: [NSImage imageNamed:@"default_avatar_overlay.png"]]; |
| 218 | } |
Kateryna Kostiuk | 9fca542 | 2018-11-19 16:27:46 -0500 | [diff] [blame] | 219 | itemView.accountStatus.bgColor = colorForAccountStatus(account.status); |
Kateryna Kostiuk | d73f960 | 2018-07-24 13:51:28 -0400 | [diff] [blame] | 220 | 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 Kostiuk | 85a334e | 2018-12-03 15:54:19 -0500 | [diff] [blame] | 225 | [itemView.accountTypeLabel setStringValue:@"Jami"]; |
Kateryna Kostiuk | d73f960 | 2018-07-24 13:51:28 -0400 | [diff] [blame] | 226 | 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 Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 237 | [self.delegate createNewAccount]; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 238 | } |
| 239 | |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 240 | -(void) updatePhoto |
| 241 | { |
Anthony Léonard | 72128c9 | 2017-12-26 16:48:39 -0500 | [diff] [blame] | 242 | @try { |
| 243 | auto& account = [self selectedAccount]; |
| 244 | if(account.profileInfo.type == lrc::api::profile::Type::INVALID) |
| 245 | return; |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 246 | NSData *imageData = [[NSData alloc] initWithBase64EncodedString:account.profileInfo.avatar.toNSString() options:NSDataBase64DecodingIgnoreUnknownCharacters]; |
Kateryna Kostiuk | 43a7923 | 2018-10-18 15:45:18 -0400 | [diff] [blame] | 247 | NSImage *image = [[NSImage alloc] initWithData:imageData]; |
| 248 | if(image) { |
| 249 | [profileImage setImage: image]; |
Kateryna Kostiuk | d73f960 | 2018-07-24 13:51:28 -0400 | [diff] [blame] | 250 | } else { |
| 251 | [profileImage setImage: [NSImage imageNamed:@"default_avatar_overlay.png"]]; |
| 252 | } |
Kateryna Kostiuk | 9fca542 | 2018-11-19 16:27:46 -0500 | [diff] [blame] | 253 | accountStatus.bgColor = colorForAccountStatus(account.status); |
| 254 | [accountStatus setNeedsDisplay: YES]; |
Anthony Léonard | 72128c9 | 2017-12-26 16:48:39 -0500 | [diff] [blame] | 255 | } |
| 256 | @catch (NSException *ex) { |
| 257 | NSLog(@"Caught exception %@: %@", [ex name], [ex reason]); |
| 258 | } |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 259 | } |
| 260 | |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 261 | -(NSString*) nameForAccount:(const lrc::api::account::Info&) account { |
Kateryna Kostiuk | d73f960 | 2018-07-24 13:51:28 -0400 | [diff] [blame] | 262 | return bestIDForAccount(account); |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | -(NSString*) itemTitleForAccount:(const lrc::api::account::Info&) account { |
Kateryna Kostiuk | d73f960 | 2018-07-24 13:51:28 -0400 | [diff] [blame] | 266 | NSString* alias = bestNameForAccount(account); |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 267 | NSString* userNameString = [self nameForAccount: account]; |
Kateryna Kostiuk | dff4c0e | 2018-08-31 10:58:32 -0400 | [diff] [blame] | 268 | if([alias isEqualToString:userNameString] || [userNameString length] == 0) { |
| 269 | return alias; |
Kateryna Kostiuk | a724846 | 2017-04-18 16:15:31 -0400 | [diff] [blame] | 270 | } |
Kateryna Kostiuk | dff4c0e | 2018-08-31 10:58:32 -0400 | [diff] [blame] | 271 | alias = [NSString stringWithFormat: @"%@\n", alias]; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 272 | return [alias stringByAppendingString:userNameString]; |
| 273 | } |
| 274 | |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 275 | - (NSAttributedString*) attributedItemTitleForAccount:(const lrc::api::account::Info&) account { |
Kateryna Kostiuk | d73f960 | 2018-07-24 13:51:28 -0400 | [diff] [blame] | 276 | NSString* alias = bestNameForAccount(account); |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 277 | NSString* userNameString = [self nameForAccount: account]; |
Kateryna Kostiuk | 9fca542 | 2018-11-19 16:27:46 -0500 | [diff] [blame] | 278 | NSFont *fontAlias = [NSFont fontWithName:@"Helvetica Neue Light" size:16.0]; |
Kateryna Kostiuk | ef66f97 | 2018-11-02 17:10:37 -0400 | [diff] [blame] | 279 | NSFont *fontUserName = [NSFont fontWithName:@"Helvetica Neue Light" size:13.0]; |
Kateryna Kostiuk | 9fca542 | 2018-11-19 16:27:46 -0500 | [diff] [blame] | 280 | NSColor *colorAlias = [NSColor textColor]; |
| 281 | NSColor *colorAUserName = [NSColor labelColor]; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 282 | NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; |
| 283 | paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail; |
Kateryna Kostiuk | ef66f97 | 2018-11-02 17:10:37 -0400 | [diff] [blame] | 284 | paragraphStyle.lineSpacing = 3; |
Kateryna Kostiuk | 9fca542 | 2018-11-19 16:27:46 -0500 | [diff] [blame] | 285 | NSDictionary *aliasAttrs = [NSDictionary dictionaryWithObjectsAndKeys: |
| 286 | fontAlias,NSFontAttributeName, |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 287 | colorAlias,NSForegroundColorAttributeName, |
| 288 | paragraphStyle,NSParagraphStyleAttributeName, nil]; |
Kateryna Kostiuk | 9fca542 | 2018-11-19 16:27:46 -0500 | [diff] [blame] | 289 | NSDictionary *userNameAttrs = [NSDictionary dictionaryWithObjectsAndKeys: |
| 290 | fontUserName,NSFontAttributeName, |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 291 | colorAUserName,NSForegroundColorAttributeName, |
| 292 | paragraphStyle,NSParagraphStyleAttributeName, nil]; |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 293 | |
| 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 Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 302 | 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 Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 310 | -(void) update { |
| 311 | if(menuIsOpen) { |
| 312 | return; |
| 313 | } |
| 314 | [self updateMenu]; |
Kateryna Kostiuk | 312f913 | 2017-04-18 12:09:06 -0400 | [diff] [blame] | 315 | [self setPopUpButtonSelection]; |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 316 | [self updatePhoto]; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 317 | } |
| 318 | |
Kateryna Kostiuk | 312f913 | 2017-04-18 12:09:06 -0400 | [diff] [blame] | 319 | -(void) setPopUpButtonSelection { |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 320 | if(accountsMenu.itemArray.count == 0) { |
| 321 | [self.view setHidden:YES]; |
| 322 | return; |
| 323 | } |
Kateryna Kostiuk | 312f913 | 2017-04-18 12:09:06 -0400 | [diff] [blame] | 324 | [self.view setHidden:NO]; |
Anthony Léonard | 72128c9 | 2017-12-26 16:48:39 -0500 | [diff] [blame] | 325 | @try { |
| 326 | auto& account = [self selectedAccount]; |
| 327 | if(account.profileInfo.type == lrc::api::profile::Type::INVALID){ |
| 328 | return; |
| 329 | } |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 330 | [accountSelectionButton selectItemWithTag:[menuItemsTags[account.id.toNSString()] intValue]]; |
Kateryna Kostiuk | 9fca542 | 2018-11-19 16:27:46 -0500 | [diff] [blame] | 331 | [selectedAccountTitle setAttributedStringValue: accountSelectionButton.attributedTitle]; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 332 | } |
Anthony Léonard | 72128c9 | 2017-12-26 16:48:39 -0500 | [diff] [blame] | 333 | @catch (NSException *ex) { |
| 334 | NSLog(@"Caught exception %@: %@", [ex name], [ex reason]); |
| 335 | } |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | #pragma mark - NSPopUpButton item selection |
| 339 | |
| 340 | - (IBAction)itemChanged:(id)sender { |
Kateryna Kostiuk | 04a5612 | 2018-08-27 10:51:27 -0400 | [diff] [blame] | 341 | NSInteger row = [(NSPopUpButton *)sender indexOfSelectedItem]; |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 342 | auto accList = accMdl_->getAccountList(); |
| 343 | if (row >= accList.size()) |
Kateryna Kostiuk | a16c986 | 2017-05-03 13:30:14 -0400 | [diff] [blame] | 344 | return; |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 345 | |
| 346 | auto& account = accMdl_->getAccountInfo(accList[row]); |
| 347 | [accountSelectionManager_ setSavedAccount:account]; |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 348 | [self.delegate selectAccount:account currentRemoved: NO]; |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 349 | [self updatePhoto]; |
Kateryna Kostiuk | 9fca542 | 2018-11-19 16:27:46 -0500 | [diff] [blame] | 350 | [selectedAccountTitle setAttributedStringValue: accountSelectionButton.attributedTitle]; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 351 | } |
| 352 | |
Kateryna Kostiuk | ef66f97 | 2018-11-02 17:10:37 -0400 | [diff] [blame] | 353 | - (IBAction)openMenu:(id)sender { |
| 354 | [accountSelectionButton performClick:nil]; |
| 355 | } |
| 356 | |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 357 | #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 Kostiuk | abf4e27 | 2017-04-18 14:18:00 -0400 | [diff] [blame] | 379 | -(void) enable { |
| 380 | [accountSelectionButton setEnabled:YES]; |
| 381 | } |
| 382 | -(void) disable { |
| 383 | [accountSelectionButton setEnabled:NO]; |
| 384 | } |
| 385 | |
Kateryna Kostiuk | e350384 | 2018-12-12 16:39:45 -0500 | [diff] [blame] | 386 | - (void)selectAccount:(NSString*)accountID { |
| 387 | auto accList = accMdl_->getAccountList(); |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 388 | if (!accList.contains(QString::fromNSString(accountID))) { |
| 389 | return; |
Kateryna Kostiuk | e350384 | 2018-12-12 16:39:45 -0500 | [diff] [blame] | 390 | } |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 391 | auto& account = accMdl_->getAccountInfo(QString::fromNSString(accountID)); |
| 392 | [accountSelectionManager_ setSavedAccount:account]; |
| 393 | [self updatePhoto]; |
| 394 | [self setPopUpButtonSelection]; |
Kateryna Kostiuk | e350384 | 2018-12-12 16:39:45 -0500 | [diff] [blame] | 395 | } |
| 396 | |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 397 | @end |