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" |
| 40 | |
| 41 | @interface ChooseAccountVC () <NSMenuDelegate> |
| 42 | |
| 43 | @end |
| 44 | |
| 45 | @implementation ChooseAccountVC { |
Kateryna Kostiuk | 312f913 | 2017-04-18 12:09:06 -0400 | [diff] [blame] | 46 | |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 47 | __unsafe_unretained IBOutlet NSImageView* profileImage; |
| 48 | __unsafe_unretained IBOutlet NSPopUpButton* accountSelectionButton; |
Kateryna Kostiuk | 312f913 | 2017-04-18 12:09:06 -0400 | [diff] [blame] | 49 | |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 50 | } |
| 51 | Boolean menuIsOpen; |
| 52 | Boolean menuNeedsUpdate; |
| 53 | NSMenu* accountsMenu; |
| 54 | NSMenuItem* selectedMenuItem; |
| 55 | QMetaObject::Connection accountUpdate; |
| 56 | |
| 57 | - (void)awakeFromNib |
| 58 | { |
| 59 | [profileImage setWantsLayer: YES]; |
| 60 | profileImage.layer.cornerRadius = profileImage.frame.size.width / 2; |
| 61 | profileImage.layer.masksToBounds = YES; |
| 62 | |
| 63 | if (auto pro = ProfileModel::instance().selectedProfile()) { |
| 64 | auto photo = GlobalInstances::pixmapManipulator().contactPhoto(pro->person(), {140,140}); |
| 65 | [profileImage setImage:QtMac::toNSImage(qvariant_cast<QPixmap>(photo))]; |
| 66 | } |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 67 | accountsMenu = [[NSMenu alloc] initWithTitle:@""]; |
| 68 | [accountsMenu setDelegate:self]; |
| 69 | accountSelectionButton.menu = accountsMenu; |
| 70 | [self update]; |
| 71 | |
| 72 | QObject::disconnect(accountUpdate); |
| 73 | accountUpdate = QObject::connect(&AccountModel::instance(), |
Kateryna Kostiuk | 312f913 | 2017-04-18 12:09:06 -0400 | [diff] [blame] | 74 | &AccountModel::dataChanged, |
| 75 | [=] { |
| 76 | [self update]; |
| 77 | }); |
| 78 | QObject::connect(AvailableAccountModel::instance().selectionModel(), |
| 79 | &QItemSelectionModel::currentChanged, |
| 80 | [self](const QModelIndex& idx){ |
| 81 | [self update]; |
| 82 | }); |
| 83 | QObject::connect(&AvailableAccountModel::instance(), |
| 84 | &QAbstractItemModel::rowsRemoved, |
| 85 | [self]{ |
| 86 | [self update]; |
| 87 | }); |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | -(void) updateMenu { |
| 91 | [accountsMenu removeAllItems]; |
Kateryna Kostiuk | 312f913 | 2017-04-18 12:09:06 -0400 | [diff] [blame] | 92 | for (int i = 0; i < AvailableAccountModel::instance().rowCount(); i++) { |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 93 | |
Kateryna Kostiuk | 312f913 | 2017-04-18 12:09:06 -0400 | [diff] [blame] | 94 | QModelIndex index = AvailableAccountModel::instance().selectionModel()->model()->index(i, 0); |
| 95 | Account* account = index.data(static_cast<int>(Account::Role::Object)).value<Account*>(); |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 96 | NSMenuItem* menuBarItem = [[NSMenuItem alloc] |
Kateryna Kostiuk | 312f913 | 2017-04-18 12:09:06 -0400 | [diff] [blame] | 97 | initWithTitle:[self itemTitleForAccount:account] |
| 98 | action:NULL |
| 99 | keyEquivalent:@""]; |
| 100 | |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 101 | menuBarItem.attributedTitle = [self attributedItemTitleForAccount:account]; |
| 102 | AccountMenuItemView *itemView = [[AccountMenuItemView alloc] initWithFrame:CGRectZero]; |
| 103 | [itemView.accountLabel setStringValue:account->alias().toNSString()]; |
| 104 | NSString* userNameString = [self nameForAccount: account]; |
| 105 | [itemView.userNameLabel setStringValue:userNameString]; |
| 106 | [itemView.accountTypeLabel setStringValue:@"Ring"]; |
| 107 | auto humanState = account->toHumanStateName(); |
| 108 | [itemView.accountStatus setStringValue:humanState.toNSString()]; |
| 109 | [menuBarItem setView:itemView]; |
| 110 | [accountsMenu addItem:menuBarItem]; |
| 111 | [accountsMenu addItem:[NSMenuItem separatorItem]]; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
| 115 | -(NSString*) nameForAccount:(Account*) account { |
| 116 | auto name = account->registeredName(); |
| 117 | NSString* userNameString = nullptr; |
| 118 | if (!name.isNull() && !name.isEmpty()) { |
| 119 | userNameString = name.toNSString(); |
| 120 | } else { |
| 121 | userNameString = account->username().toNSString(); |
| 122 | } |
| 123 | return userNameString; |
| 124 | } |
| 125 | |
| 126 | -(NSString*) itemTitleForAccount:(Account*) account { |
| 127 | NSString* alias = account->alias().toNSString(); |
| 128 | alias = [NSString stringWithFormat: @"%@\n", alias]; |
| 129 | NSString* userNameString = [self nameForAccount: account]; |
| 130 | return [alias stringByAppendingString:userNameString]; |
| 131 | } |
| 132 | |
| 133 | - (NSAttributedString*) attributedItemTitleForAccount:(Account*) account { |
| 134 | NSString* alias = account->alias().toNSString(); |
| 135 | alias = [NSString stringWithFormat: @"%@\n", alias]; |
| 136 | NSString* userNameString = [self nameForAccount: account]; |
| 137 | NSFont *fontAlias = [NSFont userFontOfSize:14.0]; |
| 138 | NSFont *fontUserName = [NSFont userFontOfSize:11.0]; |
| 139 | NSColor *colorAlias = [NSColor labelColor]; |
| 140 | NSColor *colorAUserName = [NSColor secondaryLabelColor]; |
| 141 | NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; |
| 142 | paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail; |
| 143 | NSDictionary *aliasAttrs = [NSDictionary dictionaryWithObjectsAndKeys:fontAlias,NSFontAttributeName, |
| 144 | colorAlias,NSForegroundColorAttributeName, |
| 145 | paragraphStyle,NSParagraphStyleAttributeName, nil]; |
| 146 | NSDictionary *userNameAttrs = [NSDictionary dictionaryWithObjectsAndKeys:fontUserName,NSFontAttributeName, |
| 147 | colorAUserName,NSForegroundColorAttributeName, |
| 148 | paragraphStyle,NSParagraphStyleAttributeName, nil]; |
| 149 | NSAttributedString* attributedString = [[NSAttributedString alloc] initWithString:alias attributes:aliasAttrs]; |
| 150 | NSAttributedString* attributedStringSecond= [[NSAttributedString alloc] initWithString:userNameString attributes:userNameAttrs]; |
| 151 | NSMutableAttributedString *result = [[NSMutableAttributedString alloc] init]; |
| 152 | [result appendAttributedString:attributedString]; |
| 153 | [result appendAttributedString:attributedStringSecond]; |
| 154 | return result; |
| 155 | } |
| 156 | |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 157 | -(void) update { |
| 158 | if(menuIsOpen) { |
| 159 | return; |
| 160 | } |
| 161 | [self updateMenu]; |
Kateryna Kostiuk | 312f913 | 2017-04-18 12:09:06 -0400 | [diff] [blame] | 162 | [self setPopUpButtonSelection]; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 163 | } |
| 164 | |
Kateryna Kostiuk | 312f913 | 2017-04-18 12:09:06 -0400 | [diff] [blame] | 165 | -(void) setPopUpButtonSelection { |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 166 | if(accountsMenu.itemArray.count == 0) { |
| 167 | [self.view setHidden:YES]; |
| 168 | return; |
| 169 | } |
Kateryna Kostiuk | 312f913 | 2017-04-18 12:09:06 -0400 | [diff] [blame] | 170 | [self.view setHidden:NO]; |
| 171 | QModelIndex index = AvailableAccountModel::instance().selectionModel()->currentIndex(); |
| 172 | Account* account = index.data(static_cast<int>(Account::Role::Object)).value<Account*>(); |
| 173 | if(account == nil){ |
| 174 | return; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 175 | } |
Kateryna Kostiuk | 312f913 | 2017-04-18 12:09:06 -0400 | [diff] [blame] | 176 | [accountSelectionButton selectItemWithTitle:[self itemTitleForAccount:account]]; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | #pragma mark - NSPopUpButton item selection |
| 180 | |
| 181 | - (IBAction)itemChanged:(id)sender { |
Kateryna Kostiuk | 312f913 | 2017-04-18 12:09:06 -0400 | [diff] [blame] | 182 | NSInteger row = [(NSPopUpButton *)sender indexOfSelectedItem] / 2; |
| 183 | QModelIndex index = AvailableAccountModel::instance().selectionModel()->model()->index(row, 0); |
| 184 | AvailableAccountModel::instance().selectionModel()->setCurrentIndex(index, QItemSelectionModel::ClearAndSelect); |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | #pragma mark - NSMenuDelegate |
| 188 | - (void)menuWillOpen:(NSMenu *)menu { |
| 189 | menuIsOpen = true; |
| 190 | // remember selected item to remove highlighting when menu is open |
| 191 | selectedMenuItem = [accountSelectionButton selectedItem]; |
| 192 | } |
| 193 | - (void)menuDidClose:(NSMenu *)menu { |
| 194 | |
| 195 | menuIsOpen = false; |
| 196 | } |
| 197 | |
| 198 | - (void)menu:(NSMenu *)menu willHighlightItem:(nullable NSMenuItem *)item { |
| 199 | if (!selectedMenuItem || selectedMenuItem == item) { |
| 200 | return; |
| 201 | } |
| 202 | int index = [menu indexOfItem:selectedMenuItem]; |
| 203 | [menu removeItemAtIndex:index]; |
| 204 | [menu insertItem:selectedMenuItem atIndex:index]; |
| 205 | [accountSelectionButton selectItemAtIndex:index]; |
| 206 | selectedMenuItem = nil; |
| 207 | } |
| 208 | |
Kateryna Kostiuk | abf4e27 | 2017-04-18 14:18:00 -0400 | [diff] [blame^] | 209 | -(void) enable { |
| 210 | [accountSelectionButton setEnabled:YES]; |
| 211 | } |
| 212 | -(void) disable { |
| 213 | [accountSelectionButton setEnabled:NO]; |
| 214 | } |
| 215 | |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 216 | @end |