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]{ |
Kateryna Kostiuk | 78e1f12 | 2017-06-14 14:52:34 -0400 | [diff] [blame] | 48 | [self hideIfNeeded]; |
Kateryna Kostiuk | db1f3a1 | 2017-04-24 12:08:28 -0400 | [diff] [blame] | 49 | }); |
| 50 | |
| 51 | QObject::connect(&AvailableAccountModel::instance(), |
| 52 | &QAbstractItemModel::dataChanged, |
| 53 | [self]{ |
Kateryna Kostiuk | 78e1f12 | 2017-06-14 14:52:34 -0400 | [diff] [blame] | 54 | [self hideIfNeeded]; |
| 55 | |
Kateryna Kostiuk | db1f3a1 | 2017-04-24 12:08:28 -0400 | [diff] [blame] | 56 | }); |
Kateryna Kostiuk | 7ba242e | 2017-04-13 14:38:37 -0400 | [diff] [blame] | 57 | QObject::connect(AvailableAccountModel::instance().selectionModel(), |
| 58 | &QItemSelectionModel::currentChanged, |
| 59 | [self](const QModelIndex& idx){ |
Kateryna Kostiuk | 78e1f12 | 2017-06-14 14:52:34 -0400 | [diff] [blame] | 60 | [self setupWithSelectedAccount]; |
Kateryna Kostiuk | 7ba242e | 2017-04-13 14:38:37 -0400 | [diff] [blame] | 61 | }); |
Kateryna Kostiuk | 7ba242e | 2017-04-13 14:38:37 -0400 | [diff] [blame] | 62 | self.hideRequestNumberLabel = YES; |
Kateryna Kostiuk | 78e1f12 | 2017-06-14 14:52:34 -0400 | [diff] [blame] | 63 | [self setupWithSelectedAccount]; |
Kateryna Kostiuk | db1f3a1 | 2017-04-24 12:08:28 -0400 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | - (IBAction)displayTrustRequests:(NSView*)sender |
| 67 | { |
| 68 | ContactRequestsListVC* contactRequestVC = [[ContactRequestsListVC alloc] initWithNibName:@"ContactRequestList" bundle:nil]; |
| 69 | pendingContactRequestPopover = [[NSPopover alloc] init]; |
| 70 | pendingContactRequestPopover.delegate = self; |
| 71 | [pendingContactRequestPopover setContentSize:contactRequestVC.view.frame.size]; |
| 72 | [pendingContactRequestPopover setContentViewController:contactRequestVC]; |
| 73 | [pendingContactRequestPopover setAnimates:YES]; |
| 74 | [pendingContactRequestPopover setBehavior:NSPopoverBehaviorTransient]; |
| 75 | [pendingContactRequestPopover setDelegate:self]; |
| 76 | [pendingContactRequestPopover showRelativeToRect: sender.frame ofView:sender preferredEdge:NSMaxYEdge]; |
| 77 | } |
| 78 | |
| 79 | - (void)popoverDidClose:(NSNotification *)notification { |
Kateryna Kostiuk | 78e1f12 | 2017-06-14 14:52:34 -0400 | [diff] [blame] | 80 | // when popover is closed remove ContactRequestsListVC to let it be deallocated |
Kateryna Kostiuk | db1f3a1 | 2017-04-24 12:08:28 -0400 | [diff] [blame] | 81 | [pendingContactRequestPopover setContentViewController:nil]; |
| 82 | } |
| 83 | |
Kateryna Kostiuk | 7ba242e | 2017-04-13 14:38:37 -0400 | [diff] [blame] | 84 | -(void)setNumberOfRequests:(NSInteger)numberOfRequests |
| 85 | { |
| 86 | _numberOfRequests = numberOfRequests; |
| 87 | self.hideRequestNumberLabel = (_numberOfRequests == 0); |
| 88 | } |
| 89 | |
| 90 | -(Account* ) chosenAccount |
| 91 | { |
| 92 | QModelIndex index = AvailableAccountModel::instance().selectionModel()->currentIndex(); |
Kateryna Kostiuk | 78e1f12 | 2017-06-14 14:52:34 -0400 | [diff] [blame] | 93 | if(!index.isValid()) { |
| 94 | return nullptr; |
| 95 | } |
Kateryna Kostiuk | 7ba242e | 2017-04-13 14:38:37 -0400 | [diff] [blame] | 96 | Account* account = index.data(static_cast<int>(Account::Role::Object)).value<Account*>(); |
| 97 | return account; |
| 98 | } |
| 99 | |
Kateryna Kostiuk | 78e1f12 | 2017-06-14 14:52:34 -0400 | [diff] [blame] | 100 | -(void) setupWithSelectedAccount |
Kateryna Kostiuk | 7ba242e | 2017-04-13 14:38:37 -0400 | [diff] [blame] | 101 | { |
| 102 | Account* chosenAccount = [self chosenAccount]; |
Kateryna Kostiuk | 78e1f12 | 2017-06-14 14:52:34 -0400 | [diff] [blame] | 103 | if(!chosenAccount) { |
| 104 | return; |
| 105 | } |
| 106 | [self.view setHidden:chosenAccount->protocol() != Account::Protocol::RING]; |
| 107 | if(chosenAccount->protocol() != Account::Protocol::RING) { |
| 108 | return; |
| 109 | } |
| 110 | |
Kateryna Kostiuk | 7ba242e | 2017-04-13 14:38:37 -0400 | [diff] [blame] | 111 | self.numberOfRequests = chosenAccount->pendingContactRequestModel()->rowCount(); |
| 112 | |
| 113 | QObject::disconnect(requestAdded); |
| 114 | requestAdded = QObject::connect(chosenAccount->pendingContactRequestModel(), |
Kateryna Kostiuk | 78e1f12 | 2017-06-14 14:52:34 -0400 | [diff] [blame] | 115 | &QAbstractItemModel::rowsInserted, |
| 116 | [=]() { |
| 117 | self.numberOfRequests = chosenAccount->pendingContactRequestModel()->rowCount(); |
| 118 | } |
| 119 | ); |
Kateryna Kostiuk | 7ba242e | 2017-04-13 14:38:37 -0400 | [diff] [blame] | 120 | QObject::disconnect(requestRemoved); |
| 121 | requestRemoved = QObject::connect(chosenAccount->pendingContactRequestModel(), |
| 122 | &QAbstractItemModel::rowsRemoved, |
| 123 | [=]() { |
| 124 | self.numberOfRequests = chosenAccount->pendingContactRequestModel()->rowCount(); |
| 125 | } |
| 126 | |
| 127 | ); |
| 128 | } |
| 129 | |
Kateryna Kostiuk | 78e1f12 | 2017-06-14 14:52:34 -0400 | [diff] [blame] | 130 | -(void)hideIfNeeded |
| 131 | { |
| 132 | if(AvailableAccountModel::instance().rowCount() == 0) { |
| 133 | [self.view setHidden:YES]; |
| 134 | return; |
| 135 | } |
| 136 | Account* chosenAccount = [self chosenAccount]; |
| 137 | if(!chosenAccount) { |
| 138 | return; |
| 139 | } |
| 140 | [self.view setHidden:chosenAccount->protocol() != Account::Protocol::RING]; |
| 141 | } |
| 142 | |
Kateryna Kostiuk | db1f3a1 | 2017-04-24 12:08:28 -0400 | [diff] [blame] | 143 | @end |