blob: 691dc50bf249709a91af41e3474d2562645f9806 [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"
Kateryna Kostiuka16c9862017-05-03 13:30:14 -040040#import "AccountSelectionManager.h"
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040041
42@interface ChooseAccountVC () <NSMenuDelegate>
43
44@end
45
46@implementation ChooseAccountVC {
Kateryna Kostiuk312f9132017-04-18 12:09:06 -040047
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040048 __unsafe_unretained IBOutlet NSImageView* profileImage;
49 __unsafe_unretained IBOutlet NSPopUpButton* accountSelectionButton;
Kateryna Kostiuk312f9132017-04-18 12:09:06 -040050
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040051}
52Boolean menuIsOpen;
53Boolean menuNeedsUpdate;
54NSMenu* accountsMenu;
55NSMenuItem* selectedMenuItem;
56QMetaObject::Connection accountUpdate;
Kateryna Kostiuka16c9862017-05-03 13:30:14 -040057AccountSelectionManager* accountManager;
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040058
59- (void)awakeFromNib
60{
61 [profileImage setWantsLayer: YES];
62 profileImage.layer.cornerRadius = profileImage.frame.size.width / 2;
63 profileImage.layer.masksToBounds = YES;
Kateryna Kostiuka16c9862017-05-03 13:30:14 -040064 accountManager = [[AccountSelectionManager alloc] init];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040065
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 Kostiuk13b76882017-03-30 09:18:44 -040070 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 Kostiuk312f9132017-04-18 12:09:06 -040077 &AccountModel::dataChanged,
78 [=] {
79 [self update];
80 });
81 QObject::connect(AvailableAccountModel::instance().selectionModel(),
82 &QItemSelectionModel::currentChanged,
83 [self](const QModelIndex& idx){
Kateryna Kostiuka16c9862017-05-03 13:30:14 -040084 if(!idx.isValid()) {
85 return;
86 }
87 [accountManager saveAccountWithIndex:idx];
Kateryna Kostiuk312f9132017-04-18 12:09:06 -040088 [self update];
89 });
90 QObject::connect(&AvailableAccountModel::instance(),
91 &QAbstractItemModel::rowsRemoved,
92 [self]{
93 [self update];
94 });
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040095}
96
97-(void) updateMenu {
98 [accountsMenu removeAllItems];
Kateryna Kostiuk312f9132017-04-18 12:09:06 -040099 for (int i = 0; i < AvailableAccountModel::instance().rowCount(); i++) {
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400100
Kateryna Kostiuk312f9132017-04-18 12:09:06 -0400101 QModelIndex index = AvailableAccountModel::instance().selectionModel()->model()->index(i, 0);
102 Account* account = index.data(static_cast<int>(Account::Role::Object)).value<Account*>();
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400103 NSMenuItem* menuBarItem = [[NSMenuItem alloc]
Kateryna Kostiuk312f9132017-04-18 12:09:06 -0400104 initWithTitle:[self itemTitleForAccount:account]
105 action:NULL
106 keyEquivalent:@""];
107
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400108 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 Kostiuka7248462017-04-18 16:15:31 -0400113 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 Kostiuk13b76882017-03-30 09:18:44 -0400123 auto humanState = account->toHumanStateName();
124 [itemView.accountStatus setStringValue:humanState.toNSString()];
125 [menuBarItem setView:itemView];
126 [accountsMenu addItem:menuBarItem];
127 [accountsMenu addItem:[NSMenuItem separatorItem]];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400128 }
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 Kostiuk13b76882017-03-30 09:18:44 -0400144 NSString* userNameString = [self nameForAccount: account];
Kateryna Kostiuka7248462017-04-18 16:15:31 -0400145 if([userNameString length] > 0) {
146 alias = [NSString stringWithFormat: @"%@\n", alias];
147 }
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400148 return [alias stringByAppendingString:userNameString];
149}
150
151- (NSAttributedString*) attributedItemTitleForAccount:(Account*) account {
152 NSString* alias = account->alias().toNSString();
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400153 NSString* userNameString = [self nameForAccount: account];
Kateryna Kostiuka7248462017-04-18 16:15:31 -0400154 if([userNameString length] > 0){
155 alias = [NSString stringWithFormat: @"%@\n", alias];
156 }
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400157 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 Kostiuk13b76882017-03-30 09:18:44 -0400177-(void) update {
178 if(menuIsOpen) {
179 return;
180 }
181 [self updateMenu];
Kateryna Kostiuk312f9132017-04-18 12:09:06 -0400182 [self setPopUpButtonSelection];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400183}
184
Kateryna Kostiuk312f9132017-04-18 12:09:06 -0400185-(void) setPopUpButtonSelection {
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400186 if(accountsMenu.itemArray.count == 0) {
187 [self.view setHidden:YES];
188 return;
189 }
Kateryna Kostiuk312f9132017-04-18 12:09:06 -0400190 [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 Kostiuk13b76882017-03-30 09:18:44 -0400195 }
Kateryna Kostiuk312f9132017-04-18 12:09:06 -0400196 [accountSelectionButton selectItemWithTitle:[self itemTitleForAccount:account]];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400197}
198
199#pragma mark - NSPopUpButton item selection
200
201- (IBAction)itemChanged:(id)sender {
Kateryna Kostiuk312f9132017-04-18 12:09:06 -0400202 NSInteger row = [(NSPopUpButton *)sender indexOfSelectedItem] / 2;
203 QModelIndex index = AvailableAccountModel::instance().selectionModel()->model()->index(row, 0);
Kateryna Kostiuka16c9862017-05-03 13:30:14 -0400204 if(!index.isValid()) {
205 return;
206 }
Kateryna Kostiuk312f9132017-04-18 12:09:06 -0400207 AvailableAccountModel::instance().selectionModel()->setCurrentIndex(index, QItemSelectionModel::ClearAndSelect);
Kateryna Kostiuka16c9862017-05-03 13:30:14 -0400208 [accountManager saveAccountWithIndex:index];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400209}
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 Kostiukabf4e272017-04-18 14:18:00 -0400233-(void) enable {
234 [accountSelectionButton setEnabled:YES];
235}
236-(void) disable {
237 [accountSelectionButton setEnabled:NO];
238}
239
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400240@end