Kateryna Kostiuk | db1f3a1 | 2017-04-24 12:08:28 -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 | |
Kateryna Kostiuk | 7ba242e | 2017-04-13 14:38:37 -0400 | [diff] [blame] | 20 | //Qt |
| 21 | #import <QItemSelectionModel> |
| 22 | //LRC |
| 23 | #import <account.h> |
| 24 | #import <pendingContactRequestModel.h> |
| 25 | #import <availableAccountModel.h> |
Kateryna Kostiuk | db1f3a1 | 2017-04-24 12:08:28 -0400 | [diff] [blame] | 26 | |
| 27 | #import "ContactRequestVC.h" |
| 28 | #import "ContactRequestsListVC.h" |
| 29 | |
| 30 | @interface ContactRequestVC ()<NSPopoverDelegate> { |
| 31 | |
| 32 | NSPopover* pendingContactRequestPopover; |
| 33 | } |
| 34 | |
| 35 | @end |
| 36 | |
| 37 | @implementation ContactRequestVC |
| 38 | |
Kateryna Kostiuk | 7ba242e | 2017-04-13 14:38:37 -0400 | [diff] [blame] | 39 | QMetaObject::Connection requestAdded; |
| 40 | QMetaObject::Connection requestRemoved; |
| 41 | |
Kateryna Kostiuk | db1f3a1 | 2017-04-24 12:08:28 -0400 | [diff] [blame] | 42 | - (void)awakeFromNib |
| 43 | { |
| 44 | [self.view setHidden:AvailableAccountModel::instance().rowCount() == 0]; |
| 45 | QObject::connect(&AvailableAccountModel::instance(), |
| 46 | &QAbstractItemModel::rowsRemoved, |
| 47 | [self]{ |
| 48 | [self.view setHidden:AvailableAccountModel::instance().rowCount() == 0]; |
| 49 | }); |
| 50 | |
| 51 | QObject::connect(&AvailableAccountModel::instance(), |
| 52 | &QAbstractItemModel::dataChanged, |
| 53 | [self]{ |
| 54 | [self.view setHidden:AvailableAccountModel::instance().rowCount() == 0]; |
| 55 | }); |
Kateryna Kostiuk | 7ba242e | 2017-04-13 14:38:37 -0400 | [diff] [blame] | 56 | QObject::connect(AvailableAccountModel::instance().selectionModel(), |
| 57 | &QItemSelectionModel::currentChanged, |
| 58 | [self](const QModelIndex& idx){ |
| 59 | Account* chosenAccount = [self chosenAccount]; |
| 60 | if(chosenAccount) { |
| 61 | [self connectAccountContactRequests]; |
| 62 | } |
| 63 | }); |
| 64 | Account* chosenAccount = [self chosenAccount]; |
| 65 | self.hideRequestNumberLabel = YES; |
| 66 | if(chosenAccount) { |
| 67 | [self connectAccountContactRequests]; |
| 68 | } |
Kateryna Kostiuk | db1f3a1 | 2017-04-24 12:08:28 -0400 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | - (IBAction)displayTrustRequests:(NSView*)sender |
| 72 | { |
| 73 | ContactRequestsListVC* contactRequestVC = [[ContactRequestsListVC alloc] initWithNibName:@"ContactRequestList" bundle:nil]; |
| 74 | pendingContactRequestPopover = [[NSPopover alloc] init]; |
| 75 | pendingContactRequestPopover.delegate = self; |
| 76 | [pendingContactRequestPopover setContentSize:contactRequestVC.view.frame.size]; |
| 77 | [pendingContactRequestPopover setContentViewController:contactRequestVC]; |
| 78 | [pendingContactRequestPopover setAnimates:YES]; |
| 79 | [pendingContactRequestPopover setBehavior:NSPopoverBehaviorTransient]; |
| 80 | [pendingContactRequestPopover setDelegate:self]; |
| 81 | [pendingContactRequestPopover showRelativeToRect: sender.frame ofView:sender preferredEdge:NSMaxYEdge]; |
| 82 | } |
| 83 | |
| 84 | - (void)popoverDidClose:(NSNotification *)notification { |
| 85 | // when popover is closed remove ContactRequestsListVC to let it be allocated |
| 86 | [pendingContactRequestPopover setContentViewController:nil]; |
| 87 | } |
| 88 | |
Kateryna Kostiuk | 7ba242e | 2017-04-13 14:38:37 -0400 | [diff] [blame] | 89 | -(void)setNumberOfRequests:(NSInteger)numberOfRequests |
| 90 | { |
| 91 | _numberOfRequests = numberOfRequests; |
| 92 | self.hideRequestNumberLabel = (_numberOfRequests == 0); |
| 93 | } |
| 94 | |
| 95 | -(Account* ) chosenAccount |
| 96 | { |
| 97 | QModelIndex index = AvailableAccountModel::instance().selectionModel()->currentIndex(); |
| 98 | Account* account = index.data(static_cast<int>(Account::Role::Object)).value<Account*>(); |
| 99 | return account; |
| 100 | } |
| 101 | |
| 102 | -(void) connectAccountContactRequests |
| 103 | { |
| 104 | Account* chosenAccount = [self chosenAccount]; |
| 105 | self.numberOfRequests = chosenAccount->pendingContactRequestModel()->rowCount(); |
| 106 | |
| 107 | QObject::disconnect(requestAdded); |
| 108 | requestAdded = QObject::connect(chosenAccount->pendingContactRequestModel(), |
| 109 | &QAbstractItemModel::rowsInserted, |
| 110 | [=]() { |
| 111 | self.numberOfRequests = chosenAccount->pendingContactRequestModel()->rowCount(); |
| 112 | } |
| 113 | ); |
| 114 | QObject::disconnect(requestRemoved); |
| 115 | requestRemoved = QObject::connect(chosenAccount->pendingContactRequestModel(), |
| 116 | &QAbstractItemModel::rowsRemoved, |
| 117 | [=]() { |
| 118 | self.numberOfRequests = chosenAccount->pendingContactRequestModel()->rowCount(); |
| 119 | } |
| 120 | |
| 121 | ); |
| 122 | } |
| 123 | |
Kateryna Kostiuk | db1f3a1 | 2017-04-24 12:08:28 -0400 | [diff] [blame] | 124 | @end |