blob: 09ac4a56bb16e01519ca13bb4538d0af4d601fbb [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>
Kateryna Kostiuka9e5c712017-05-15 16:46:16 -040022#import <QSize>
23#import <QtMacExtras/qmacfunctions.h>
24#import <QPixmap>
Kateryna Kostiukdb1f3a12017-04-24 12:08:28 -040025
26//LRC
27#import <account.h>
28#import <availableAccountModel.h>
29#import <contactRequest.h>
30#import <pendingContactRequestModel.h>
Kateryna Kostiuka9e5c712017-05-15 16:46:16 -040031#import <globalinstances.h>
32#import <contactmethod.h>
Kateryna Kostiukdb1f3a12017-04-24 12:08:28 -040033
34#import "ContactRequestsListVC.h"
35#import "QNSTreeController.h"
Kateryna Kostiuka9e5c712017-05-15 16:46:16 -040036#import <interfaces/pixmapmanipulatori.h>
37#import "views/ContactRequestCellView.h"
Kateryna Kostiukdb1f3a12017-04-24 12:08:28 -040038
39@interface ContactRequestsListVC ()
40
41@property QNSTreeController* requestsTreeController;
42@property (unsafe_unretained) IBOutlet NSOutlineView* contactRequestView;
43@property (unsafe_unretained) IBOutlet NSTextField* noRequestsLabel;
44@end
45
46@implementation ContactRequestsListVC
47
48@synthesize requestsTreeController;
49@synthesize contactRequestView;
50@synthesize noRequestsLabel;
51
52typedef NS_ENUM(NSInteger, ContactAction) {
53 ACCEPT = 0,
54 REFUSE,
55 BLOCK,
56};
57
58NSInteger const TAG_NAME = 100;
59NSInteger const TAG_RINGID = 200;
Kateryna Kostiuka9e5c712017-05-15 16:46:16 -040060NSInteger const TAG_PHOTO = 300;
61
62NSString* defaultMsg = @"Hello, I would like invite you";
63
Kateryna Kostiukdb1f3a12017-04-24 12:08:28 -040064
65- (void)awakeFromNib
66{
67 Account* chosenAccount = [self chosenAccount];
68 requestsTreeController = [[QNSTreeController alloc] initWithQModel:chosenAccount->pendingContactRequestModel()];
69 [requestsTreeController setAvoidsEmptySelection:NO];
70 [requestsTreeController setAlwaysUsesMultipleValuesMarker:YES];
71 [requestsTreeController setChildrenKeyPath:@"children"];
72
73 [contactRequestView bind:@"content" toObject:requestsTreeController withKeyPath:@"arrangedObjects" options:nil];
74 [contactRequestView bind:@"sortDescriptors" toObject:requestsTreeController withKeyPath:@"sortDescriptors" options:nil];
75 [contactRequestView bind:@"selectionIndexPaths" toObject:requestsTreeController withKeyPath:@"selectionIndexPaths" options:nil];
Kateryna Kostiuka9e5c712017-05-15 16:46:16 -040076 contactRequestView.selectionHighlightStyle = NSTableViewSelectionHighlightStyleNone;
Kateryna Kostiukdb1f3a12017-04-24 12:08:28 -040077 [noRequestsLabel setHidden:[contactRequestView numberOfRows]>0];
78
79}
80
81- (IBAction)acceptContactRequest:(NSView*)sender
82{
83 NSInteger row = [self.contactRequestView rowForView:sender];
84 [self performAction:ACCEPT forRequestAtRow:row];
85}
86
87- (IBAction)refuseContactRequest:(NSView*)sender
88{
89 NSInteger row = [self.contactRequestView rowForView:sender];
90 [self performAction:REFUSE forRequestAtRow:row];
91}
92
93- (IBAction)blockContactRequest:(NSView*)sender
94{
95 NSInteger row = [self.contactRequestView rowForView:sender];
96 [self performAction:BLOCK forRequestAtRow:row];
97}
98
99-(void) performAction:(ContactAction)action forRequestAtRow:(NSInteger)row {
100 id item = [self.contactRequestView itemAtRow:row];
101 QModelIndex qIdx = [self.requestsTreeController toQIdx:((NSTreeNode*)item)];
102 Account* chosenAccount = AccountModel::instance().userChosenAccount();
103 const auto& var = qIdx.data(static_cast<int>(Ring::Role::Object));
104 if (!var.isValid()) {
105 return;
106 }
107 auto contactRequest = qvariant_cast<ContactRequest*>(var);
108 switch (action) {
109 case ACCEPT:
110 contactRequest->accept();
111 break;
112 case REFUSE:
113 contactRequest->discard();
114 break;
115 case BLOCK:
116 contactRequest->block();
117 break;
118 default:
119 break;
120 }
121 [noRequestsLabel setHidden:[contactRequestView numberOfRows]>0];
122}
123
124#pragma mark - NSOutlineViewDelegate methods
125
126- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item
127{
128 return NO;
129}
130
131- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item
132{
Kateryna Kostiuka9e5c712017-05-15 16:46:16 -0400133 ContactRequestCellView* result;
134 result = [outlineView makeViewWithIdentifier:@"ContactRequestView" owner:self];
Kateryna Kostiukdb1f3a12017-04-24 12:08:28 -0400135
136 QModelIndex qIdx = [self.requestsTreeController toQIdx:((NSTreeNode*)item)];
137 if(!qIdx.isValid()) {
138 return result;
139 }
Kateryna Kostiuka9e5c712017-05-15 16:46:16 -0400140
141 [result setup];
Kateryna Kostiukdb1f3a12017-04-24 12:08:28 -0400142 NSTextField* nameLabel = [result viewWithTag:TAG_NAME];
143 NSTextField* ringIDLabel = [result viewWithTag:TAG_RINGID];
Kateryna Kostiuka9e5c712017-05-15 16:46:16 -0400144 NSImageView* photoView = [result viewWithTag:TAG_PHOTO];
Kateryna Kostiukdb1f3a12017-04-24 12:08:28 -0400145
Kateryna Kostiuka9e5c712017-05-15 16:46:16 -0400146 NSString* localizedTitle = [NSString stringWithFormat:
147 NSLocalizedString(@"Hi %@. Please add me to your contact list.", @"Default contact request msg"), [self nameForAccount:[self chosenAccount]]];
148 [result.msgView setString:localizedTitle];
Kateryna Kostiukdb1f3a12017-04-24 12:08:28 -0400149
Kateryna Kostiuka9e5c712017-05-15 16:46:16 -0400150 ContactRequest* contactRequest = qvariant_cast<ContactRequest*>(qIdx.data((int)Ring::Role::Object));
151 Person* person = contactRequest->peer();
152 if(!person) {
153 Account* chosenAccount = [self chosenAccount];
154 NSString* ringID = chosenAccount->pendingContactRequestModel()->data(qIdx,Qt::DisplayRole).toString().toNSString();
155 [nameLabel setStringValue:ringID];
156 return result;
157 }
158
159 QVariant photo = GlobalInstances::pixmapManipulator().contactPhoto(person, {100,100});
160 [photoView setImage:QtMac::toNSImage(qvariant_cast<QPixmap>(photo))];
161
162 NSString* idString = person->phoneNumbers()[0]->getBestId().toNSString();
163 if(person->formattedName() != nil && person->formattedName().length()>0) {
164 NSString* name = person->formattedName().toNSString();
165 [nameLabel setStringValue:name];
166 if(![person->formattedName().toNSString() isEqualToString:idString]){
167 NSString* formattedID = [NSString stringWithFormat:@"%@%@%@",@"(",idString, @")"];
168 [ringIDLabel setStringValue:formattedID];
169 }
170 return result;
171 }
172 [nameLabel setStringValue:idString];
173
Kateryna Kostiukdb1f3a12017-04-24 12:08:28 -0400174 return result;
175}
176
177-(Account* ) chosenAccount
178{
179 QModelIndex index = AvailableAccountModel::instance().selectionModel()->currentIndex();
180 return index.data(static_cast<int>(Account::Role::Object)).value<Account*>();
181}
182
Kateryna Kostiuka9e5c712017-05-15 16:46:16 -0400183-(NSString*) nameForAccount:(Account*) account {
184 auto name = account->registeredName();
185 NSString* userNameString = nullptr;
186 if (!name.isNull() && !name.isEmpty()) {
187 userNameString = name.toNSString();
188 } else {
189 userNameString = account->username().toNSString();
190 }
191 return userNameString;
192}
193
Kateryna Kostiukdb1f3a12017-04-24 12:08:28 -0400194@end