blob: 9bdbded11968f462897f17258a7257dc366cb7c5 [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
20//Qt
21#import <QItemSelectionModel>
22
23//LRC
24#import <account.h>
25#import <availableAccountModel.h>
26#import <contactRequest.h>
27#import <pendingContactRequestModel.h>
28
29#import "ContactRequestsListVC.h"
30#import "QNSTreeController.h"
31
32@interface ContactRequestsListVC ()
33
34@property QNSTreeController* requestsTreeController;
35@property (unsafe_unretained) IBOutlet NSOutlineView* contactRequestView;
36@property (unsafe_unretained) IBOutlet NSTextField* noRequestsLabel;
37@end
38
39@implementation ContactRequestsListVC
40
41@synthesize requestsTreeController;
42@synthesize contactRequestView;
43@synthesize noRequestsLabel;
44
45typedef NS_ENUM(NSInteger, ContactAction) {
46 ACCEPT = 0,
47 REFUSE,
48 BLOCK,
49};
50
51NSInteger const TAG_NAME = 100;
52NSInteger const TAG_RINGID = 200;
53
54- (void)awakeFromNib
55{
56 Account* chosenAccount = [self chosenAccount];
57 requestsTreeController = [[QNSTreeController alloc] initWithQModel:chosenAccount->pendingContactRequestModel()];
58 [requestsTreeController setAvoidsEmptySelection:NO];
59 [requestsTreeController setAlwaysUsesMultipleValuesMarker:YES];
60 [requestsTreeController setChildrenKeyPath:@"children"];
61
62 [contactRequestView bind:@"content" toObject:requestsTreeController withKeyPath:@"arrangedObjects" options:nil];
63 [contactRequestView bind:@"sortDescriptors" toObject:requestsTreeController withKeyPath:@"sortDescriptors" options:nil];
64 [contactRequestView bind:@"selectionIndexPaths" toObject:requestsTreeController withKeyPath:@"selectionIndexPaths" options:nil];
65 [noRequestsLabel setHidden:[contactRequestView numberOfRows]>0];
66
67}
68
69- (IBAction)acceptContactRequest:(NSView*)sender
70{
71 NSInteger row = [self.contactRequestView rowForView:sender];
72 [self performAction:ACCEPT forRequestAtRow:row];
73}
74
75- (IBAction)refuseContactRequest:(NSView*)sender
76{
77 NSInteger row = [self.contactRequestView rowForView:sender];
78 [self performAction:REFUSE forRequestAtRow:row];
79}
80
81- (IBAction)blockContactRequest:(NSView*)sender
82{
83 NSInteger row = [self.contactRequestView rowForView:sender];
84 [self performAction:BLOCK forRequestAtRow:row];
85}
86
87-(void) performAction:(ContactAction)action forRequestAtRow:(NSInteger)row {
88 id item = [self.contactRequestView itemAtRow:row];
89 QModelIndex qIdx = [self.requestsTreeController toQIdx:((NSTreeNode*)item)];
90 Account* chosenAccount = AccountModel::instance().userChosenAccount();
91 const auto& var = qIdx.data(static_cast<int>(Ring::Role::Object));
92 if (!var.isValid()) {
93 return;
94 }
95 auto contactRequest = qvariant_cast<ContactRequest*>(var);
96 switch (action) {
97 case ACCEPT:
98 contactRequest->accept();
99 break;
100 case REFUSE:
101 contactRequest->discard();
102 break;
103 case BLOCK:
104 contactRequest->block();
105 break;
106 default:
107 break;
108 }
109 [noRequestsLabel setHidden:[contactRequestView numberOfRows]>0];
110}
111
112#pragma mark - NSOutlineViewDelegate methods
113
114- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item
115{
116 return NO;
117}
118
119- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item
120{
121 NSTableView* result = [outlineView makeViewWithIdentifier:@"ContactRequestView" owner:self];
122
123 QModelIndex qIdx = [self.requestsTreeController toQIdx:((NSTreeNode*)item)];
124 if(!qIdx.isValid()) {
125 return result;
126 }
127 Account* chosenAccount = [self chosenAccount];
128 NSTextField* nameLabel = [result viewWithTag:TAG_NAME];
129 NSTextField* ringIDLabel = [result viewWithTag:TAG_RINGID];
130
131 NSString* ringID = chosenAccount->pendingContactRequestModel()->data(qIdx,Qt::DisplayRole).toString().toNSString();
132
133 [nameLabel setStringValue:ringID];
134 [ringIDLabel setStringValue:ringID];
135 return result;
136}
137
138-(Account* ) chosenAccount
139{
140 QModelIndex index = AvailableAccountModel::instance().selectionModel()->currentIndex();
141 return index.data(static_cast<int>(Account::Role::Object)).value<Account*>();
142}
143
144@end