blob: 422a33d2b2cc36d97a216c1c83aff6141e9c8011 [file] [log] [blame]
Kateryna Kostiuk13b76882017-03-30 09:18:44 -04001/*
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 Kostiuk312f9132017-04-18 12:09:06 -040036#import <AvailableAccountModel.h>
37
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040038//RING
39#import "views/AccountMenuItemView.h"
40
41@interface ChooseAccountVC () <NSMenuDelegate>
42
43@end
44
45@implementation ChooseAccountVC {
Kateryna Kostiuk312f9132017-04-18 12:09:06 -040046
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040047 __unsafe_unretained IBOutlet NSImageView* profileImage;
48 __unsafe_unretained IBOutlet NSPopUpButton* accountSelectionButton;
Kateryna Kostiuk312f9132017-04-18 12:09:06 -040049
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040050}
51Boolean menuIsOpen;
52Boolean menuNeedsUpdate;
53NSMenu* accountsMenu;
54NSMenuItem* selectedMenuItem;
55QMetaObject::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 Kostiuk13b76882017-03-30 09:18:44 -040067 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 Kostiuk312f9132017-04-18 12:09:06 -040074 &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 Kostiuk13b76882017-03-30 09:18:44 -040088}
89
90-(void) updateMenu {
91 [accountsMenu removeAllItems];
Kateryna Kostiuk312f9132017-04-18 12:09:06 -040092 for (int i = 0; i < AvailableAccountModel::instance().rowCount(); i++) {
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040093
Kateryna Kostiuk312f9132017-04-18 12:09:06 -040094 QModelIndex index = AvailableAccountModel::instance().selectionModel()->model()->index(i, 0);
95 Account* account = index.data(static_cast<int>(Account::Role::Object)).value<Account*>();
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040096 NSMenuItem* menuBarItem = [[NSMenuItem alloc]
Kateryna Kostiuk312f9132017-04-18 12:09:06 -040097 initWithTitle:[self itemTitleForAccount:account]
98 action:NULL
99 keyEquivalent:@""];
100
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400101 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 Kostiuk13b76882017-03-30 09:18:44 -0400112 }
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 Kostiuk13b76882017-03-30 09:18:44 -0400157-(void) update {
158 if(menuIsOpen) {
159 return;
160 }
161 [self updateMenu];
Kateryna Kostiuk312f9132017-04-18 12:09:06 -0400162 [self setPopUpButtonSelection];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400163}
164
Kateryna Kostiuk312f9132017-04-18 12:09:06 -0400165-(void) setPopUpButtonSelection {
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400166 if(accountsMenu.itemArray.count == 0) {
167 [self.view setHidden:YES];
168 return;
169 }
Kateryna Kostiuk312f9132017-04-18 12:09:06 -0400170 [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 Kostiuk13b76882017-03-30 09:18:44 -0400175 }
Kateryna Kostiuk312f9132017-04-18 12:09:06 -0400176 [accountSelectionButton selectItemWithTitle:[self itemTitleForAccount:account]];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400177}
178
179#pragma mark - NSPopUpButton item selection
180
181- (IBAction)itemChanged:(id)sender {
Kateryna Kostiuk312f9132017-04-18 12:09:06 -0400182 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 Kostiuk13b76882017-03-30 09:18:44 -0400185}
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 Kostiukabf4e272017-04-18 14:18:00 -0400209-(void) enable {
210 [accountSelectionButton setEnabled:YES];
211}
212-(void) disable {
213 [accountSelectionButton setEnabled:NO];
214}
215
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400216@end