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> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | */ |
| 19 | |
| 20 | #import "ChooseAccountVC.h" |
| 21 | |
| 22 | //Qt |
| 23 | #import <QSize> |
| 24 | #import <QtMacExtras/qmacfunctions.h> |
| 25 | #import <QPixmap> |
| 26 | |
| 27 | //LRC |
| 28 | #import <profilemodel.h> |
| 29 | #import <profile.h> |
| 30 | #import <person.h> |
| 31 | #import <globalinstances.h> |
| 32 | #import <accountmodel.h> |
| 33 | #import <account.h> |
| 34 | #import <QItemSelectionModel.h> |
| 35 | #import <interfaces/pixmapmanipulatori.h> |
Kateryna Kostiuk | 312f913 | 2017-04-18 12:09:06 -0400 | [diff] [blame] | 36 | #import <AvailableAccountModel.h> |
| 37 | |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 38 | //RING |
| 39 | #import "views/AccountMenuItemView.h" |
Kateryna Kostiuk | a16c986 | 2017-05-03 13:30:14 -0400 | [diff] [blame] | 40 | #import "AccountSelectionManager.h" |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 41 | |
| 42 | @interface ChooseAccountVC () <NSMenuDelegate> |
| 43 | |
| 44 | @end |
| 45 | |
| 46 | @implementation ChooseAccountVC { |
Kateryna Kostiuk | 312f913 | 2017-04-18 12:09:06 -0400 | [diff] [blame] | 47 | |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 48 | __unsafe_unretained IBOutlet NSImageView* profileImage; |
| 49 | __unsafe_unretained IBOutlet NSPopUpButton* accountSelectionButton; |
Kateryna Kostiuk | 312f913 | 2017-04-18 12:09:06 -0400 | [diff] [blame] | 50 | |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 51 | } |
| 52 | Boolean menuIsOpen; |
| 53 | Boolean menuNeedsUpdate; |
| 54 | NSMenu* accountsMenu; |
| 55 | NSMenuItem* selectedMenuItem; |
| 56 | QMetaObject::Connection accountUpdate; |
Kateryna Kostiuk | a16c986 | 2017-05-03 13:30:14 -0400 | [diff] [blame] | 57 | AccountSelectionManager* accountManager; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 58 | |
| 59 | - (void)awakeFromNib |
| 60 | { |
| 61 | [profileImage setWantsLayer: YES]; |
| 62 | profileImage.layer.cornerRadius = profileImage.frame.size.width / 2; |
| 63 | profileImage.layer.masksToBounds = YES; |
Kateryna Kostiuk | a16c986 | 2017-05-03 13:30:14 -0400 | [diff] [blame] | 64 | accountManager = [[AccountSelectionManager alloc] init]; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 65 | |
| 66 | if (auto pro = ProfileModel::instance().selectedProfile()) { |
| 67 | auto photo = GlobalInstances::pixmapManipulator().contactPhoto(pro->person(), {140,140}); |
| 68 | [profileImage setImage:QtMac::toNSImage(qvariant_cast<QPixmap>(photo))]; |
| 69 | } |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 70 | accountsMenu = [[NSMenu alloc] initWithTitle:@""]; |
| 71 | [accountsMenu setDelegate:self]; |
| 72 | accountSelectionButton.menu = accountsMenu; |
| 73 | [self update]; |
| 74 | |
| 75 | QObject::disconnect(accountUpdate); |
| 76 | accountUpdate = QObject::connect(&AccountModel::instance(), |
Kateryna Kostiuk | 312f913 | 2017-04-18 12:09:06 -0400 | [diff] [blame] | 77 | &AccountModel::dataChanged, |
| 78 | [=] { |
| 79 | [self update]; |
| 80 | }); |
| 81 | QObject::connect(AvailableAccountModel::instance().selectionModel(), |
| 82 | &QItemSelectionModel::currentChanged, |
| 83 | [self](const QModelIndex& idx){ |
Kateryna Kostiuk | a16c986 | 2017-05-03 13:30:14 -0400 | [diff] [blame] | 84 | if(!idx.isValid()) { |
| 85 | return; |
| 86 | } |
| 87 | [accountManager saveAccountWithIndex:idx]; |
Kateryna Kostiuk | 312f913 | 2017-04-18 12:09:06 -0400 | [diff] [blame] | 88 | [self update]; |
| 89 | }); |
| 90 | QObject::connect(&AvailableAccountModel::instance(), |
| 91 | &QAbstractItemModel::rowsRemoved, |
| 92 | [self]{ |
| 93 | [self update]; |
| 94 | }); |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | -(void) updateMenu { |
| 98 | [accountsMenu removeAllItems]; |
Kateryna Kostiuk | 312f913 | 2017-04-18 12:09:06 -0400 | [diff] [blame] | 99 | for (int i = 0; i < AvailableAccountModel::instance().rowCount(); i++) { |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 100 | |
Kateryna Kostiuk | 312f913 | 2017-04-18 12:09:06 -0400 | [diff] [blame] | 101 | QModelIndex index = AvailableAccountModel::instance().selectionModel()->model()->index(i, 0); |
| 102 | Account* account = index.data(static_cast<int>(Account::Role::Object)).value<Account*>(); |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 103 | NSMenuItem* menuBarItem = [[NSMenuItem alloc] |
Kateryna Kostiuk | 312f913 | 2017-04-18 12:09:06 -0400 | [diff] [blame] | 104 | initWithTitle:[self itemTitleForAccount:account] |
| 105 | action:NULL |
| 106 | keyEquivalent:@""]; |
| 107 | |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 108 | menuBarItem.attributedTitle = [self attributedItemTitleForAccount:account]; |
| 109 | AccountMenuItemView *itemView = [[AccountMenuItemView alloc] initWithFrame:CGRectZero]; |
| 110 | [itemView.accountLabel setStringValue:account->alias().toNSString()]; |
| 111 | NSString* userNameString = [self nameForAccount: account]; |
| 112 | [itemView.userNameLabel setStringValue:userNameString]; |
Kateryna Kostiuk | a724846 | 2017-04-18 16:15:31 -0400 | [diff] [blame] | 113 | switch (account->protocol()) { |
| 114 | case Account::Protocol::SIP: |
| 115 | [itemView.accountTypeLabel setStringValue:@"SIP"]; |
| 116 | break; |
| 117 | case Account::Protocol::RING: |
| 118 | [itemView.accountTypeLabel setStringValue:@"RING"]; |
| 119 | break; |
| 120 | default: |
| 121 | break; |
| 122 | } |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 123 | auto humanState = account->toHumanStateName(); |
| 124 | [itemView.accountStatus setStringValue:humanState.toNSString()]; |
| 125 | [menuBarItem setView:itemView]; |
| 126 | [accountsMenu addItem:menuBarItem]; |
| 127 | [accountsMenu addItem:[NSMenuItem separatorItem]]; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
| 131 | -(NSString*) nameForAccount:(Account*) account { |
| 132 | auto name = account->registeredName(); |
| 133 | NSString* userNameString = nullptr; |
| 134 | if (!name.isNull() && !name.isEmpty()) { |
| 135 | userNameString = name.toNSString(); |
| 136 | } else { |
| 137 | userNameString = account->username().toNSString(); |
| 138 | } |
| 139 | return userNameString; |
| 140 | } |
| 141 | |
| 142 | -(NSString*) itemTitleForAccount:(Account*) account { |
| 143 | NSString* alias = account->alias().toNSString(); |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 144 | NSString* userNameString = [self nameForAccount: account]; |
Kateryna Kostiuk | a724846 | 2017-04-18 16:15:31 -0400 | [diff] [blame] | 145 | if([userNameString length] > 0) { |
| 146 | alias = [NSString stringWithFormat: @"%@\n", alias]; |
| 147 | } |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 148 | return [alias stringByAppendingString:userNameString]; |
| 149 | } |
| 150 | |
| 151 | - (NSAttributedString*) attributedItemTitleForAccount:(Account*) account { |
| 152 | NSString* alias = account->alias().toNSString(); |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 153 | NSString* userNameString = [self nameForAccount: account]; |
Kateryna Kostiuk | a724846 | 2017-04-18 16:15:31 -0400 | [diff] [blame] | 154 | if([userNameString length] > 0){ |
| 155 | alias = [NSString stringWithFormat: @"%@\n", alias]; |
| 156 | } |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 157 | NSFont *fontAlias = [NSFont userFontOfSize:14.0]; |
| 158 | NSFont *fontUserName = [NSFont userFontOfSize:11.0]; |
| 159 | NSColor *colorAlias = [NSColor labelColor]; |
| 160 | NSColor *colorAUserName = [NSColor secondaryLabelColor]; |
| 161 | NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; |
| 162 | paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail; |
| 163 | NSDictionary *aliasAttrs = [NSDictionary dictionaryWithObjectsAndKeys:fontAlias,NSFontAttributeName, |
| 164 | colorAlias,NSForegroundColorAttributeName, |
| 165 | paragraphStyle,NSParagraphStyleAttributeName, nil]; |
| 166 | NSDictionary *userNameAttrs = [NSDictionary dictionaryWithObjectsAndKeys:fontUserName,NSFontAttributeName, |
| 167 | colorAUserName,NSForegroundColorAttributeName, |
| 168 | paragraphStyle,NSParagraphStyleAttributeName, nil]; |
| 169 | NSAttributedString* attributedString = [[NSAttributedString alloc] initWithString:alias attributes:aliasAttrs]; |
| 170 | NSAttributedString* attributedStringSecond= [[NSAttributedString alloc] initWithString:userNameString attributes:userNameAttrs]; |
| 171 | NSMutableAttributedString *result = [[NSMutableAttributedString alloc] init]; |
| 172 | [result appendAttributedString:attributedString]; |
| 173 | [result appendAttributedString:attributedStringSecond]; |
| 174 | return result; |
| 175 | } |
| 176 | |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 177 | -(void) update { |
| 178 | if(menuIsOpen) { |
| 179 | return; |
| 180 | } |
| 181 | [self updateMenu]; |
Kateryna Kostiuk | 312f913 | 2017-04-18 12:09:06 -0400 | [diff] [blame] | 182 | [self setPopUpButtonSelection]; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 183 | } |
| 184 | |
Kateryna Kostiuk | 312f913 | 2017-04-18 12:09:06 -0400 | [diff] [blame] | 185 | -(void) setPopUpButtonSelection { |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 186 | if(accountsMenu.itemArray.count == 0) { |
| 187 | [self.view setHidden:YES]; |
| 188 | return; |
| 189 | } |
Kateryna Kostiuk | 312f913 | 2017-04-18 12:09:06 -0400 | [diff] [blame] | 190 | [self.view setHidden:NO]; |
| 191 | QModelIndex index = AvailableAccountModel::instance().selectionModel()->currentIndex(); |
| 192 | Account* account = index.data(static_cast<int>(Account::Role::Object)).value<Account*>(); |
| 193 | if(account == nil){ |
| 194 | return; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 195 | } |
Kateryna Kostiuk | 312f913 | 2017-04-18 12:09:06 -0400 | [diff] [blame] | 196 | [accountSelectionButton selectItemWithTitle:[self itemTitleForAccount:account]]; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | #pragma mark - NSPopUpButton item selection |
| 200 | |
| 201 | - (IBAction)itemChanged:(id)sender { |
Kateryna Kostiuk | 312f913 | 2017-04-18 12:09:06 -0400 | [diff] [blame] | 202 | NSInteger row = [(NSPopUpButton *)sender indexOfSelectedItem] / 2; |
| 203 | QModelIndex index = AvailableAccountModel::instance().selectionModel()->model()->index(row, 0); |
Kateryna Kostiuk | a16c986 | 2017-05-03 13:30:14 -0400 | [diff] [blame] | 204 | if(!index.isValid()) { |
| 205 | return; |
| 206 | } |
Kateryna Kostiuk | 312f913 | 2017-04-18 12:09:06 -0400 | [diff] [blame] | 207 | AvailableAccountModel::instance().selectionModel()->setCurrentIndex(index, QItemSelectionModel::ClearAndSelect); |
Kateryna Kostiuk | a16c986 | 2017-05-03 13:30:14 -0400 | [diff] [blame] | 208 | [accountManager saveAccountWithIndex:index]; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | #pragma mark - NSMenuDelegate |
| 212 | - (void)menuWillOpen:(NSMenu *)menu { |
| 213 | menuIsOpen = true; |
| 214 | // remember selected item to remove highlighting when menu is open |
| 215 | selectedMenuItem = [accountSelectionButton selectedItem]; |
| 216 | } |
| 217 | - (void)menuDidClose:(NSMenu *)menu { |
| 218 | |
| 219 | menuIsOpen = false; |
| 220 | } |
| 221 | |
| 222 | - (void)menu:(NSMenu *)menu willHighlightItem:(nullable NSMenuItem *)item { |
| 223 | if (!selectedMenuItem || selectedMenuItem == item) { |
| 224 | return; |
| 225 | } |
| 226 | int index = [menu indexOfItem:selectedMenuItem]; |
| 227 | [menu removeItemAtIndex:index]; |
| 228 | [menu insertItem:selectedMenuItem atIndex:index]; |
| 229 | [accountSelectionButton selectItemAtIndex:index]; |
| 230 | selectedMenuItem = nil; |
| 231 | } |
| 232 | |
Kateryna Kostiuk | abf4e27 | 2017-04-18 14:18:00 -0400 | [diff] [blame] | 233 | -(void) enable { |
| 234 | [accountSelectionButton setEnabled:YES]; |
| 235 | } |
| 236 | -(void) disable { |
| 237 | [accountSelectionButton setEnabled:NO]; |
| 238 | } |
| 239 | |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 240 | @end |