blob: 037137abb09de1e96a973c1ba1fb576a9efdad16 [file] [log] [blame]
Kateryna Kostiukdb1f3a12017-04-24 12:08:28 -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
Kateryna Kostiuk7ba242e2017-04-13 14:38:37 -040020//Qt
21#import <QItemSelectionModel>
22//LRC
23#import <account.h>
24#import <pendingContactRequestModel.h>
25#import <availableAccountModel.h>
Kateryna Kostiukdb1f3a12017-04-24 12:08:28 -040026
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 Kostiuk7ba242e2017-04-13 14:38:37 -040039QMetaObject::Connection requestAdded;
40QMetaObject::Connection requestRemoved;
41
Kateryna Kostiukdb1f3a12017-04-24 12:08:28 -040042- (void)awakeFromNib
43{
44 [self.view setHidden:AvailableAccountModel::instance().rowCount() == 0];
45 QObject::connect(&AvailableAccountModel::instance(),
46 &QAbstractItemModel::rowsRemoved,
47 [self]{
Kateryna Kostiuk78e1f122017-06-14 14:52:34 -040048 [self hideIfNeeded];
Kateryna Kostiukdb1f3a12017-04-24 12:08:28 -040049 });
50
51 QObject::connect(&AvailableAccountModel::instance(),
52 &QAbstractItemModel::dataChanged,
53 [self]{
Kateryna Kostiuk78e1f122017-06-14 14:52:34 -040054 [self hideIfNeeded];
55
Kateryna Kostiukdb1f3a12017-04-24 12:08:28 -040056 });
Kateryna Kostiuk7ba242e2017-04-13 14:38:37 -040057 QObject::connect(AvailableAccountModel::instance().selectionModel(),
58 &QItemSelectionModel::currentChanged,
59 [self](const QModelIndex& idx){
Kateryna Kostiuk78e1f122017-06-14 14:52:34 -040060 [self setupWithSelectedAccount];
Kateryna Kostiuk7ba242e2017-04-13 14:38:37 -040061 });
Kateryna Kostiuk7ba242e2017-04-13 14:38:37 -040062 self.hideRequestNumberLabel = YES;
Kateryna Kostiuk78e1f122017-06-14 14:52:34 -040063 [self setupWithSelectedAccount];
Kateryna Kostiukdb1f3a12017-04-24 12:08:28 -040064}
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 Kostiuk78e1f122017-06-14 14:52:34 -040080 // when popover is closed remove ContactRequestsListVC to let it be deallocated
Kateryna Kostiukdb1f3a12017-04-24 12:08:28 -040081 [pendingContactRequestPopover setContentViewController:nil];
82}
83
Kateryna Kostiuk7ba242e2017-04-13 14:38:37 -040084-(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 Kostiuk78e1f122017-06-14 14:52:34 -040093 if(!index.isValid()) {
94 return nullptr;
95 }
Kateryna Kostiuk7ba242e2017-04-13 14:38:37 -040096 Account* account = index.data(static_cast<int>(Account::Role::Object)).value<Account*>();
97 return account;
98}
99
Kateryna Kostiuk78e1f122017-06-14 14:52:34 -0400100-(void) setupWithSelectedAccount
Kateryna Kostiuk7ba242e2017-04-13 14:38:37 -0400101{
102 Account* chosenAccount = [self chosenAccount];
Kateryna Kostiuk78e1f122017-06-14 14:52:34 -0400103 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 Kostiuk7ba242e2017-04-13 14:38:37 -0400111 self.numberOfRequests = chosenAccount->pendingContactRequestModel()->rowCount();
112
113 QObject::disconnect(requestAdded);
114 requestAdded = QObject::connect(chosenAccount->pendingContactRequestModel(),
Kateryna Kostiuk78e1f122017-06-14 14:52:34 -0400115 &QAbstractItemModel::rowsInserted,
116 [=]() {
117 self.numberOfRequests = chosenAccount->pendingContactRequestModel()->rowCount();
118 }
119 );
Kateryna Kostiuk7ba242e2017-04-13 14:38:37 -0400120 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 Kostiuk78e1f122017-06-14 14:52:34 -0400130-(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 Kostiukdb1f3a12017-04-24 12:08:28 -0400143@end