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 | |
| 20 | //Qt |
| 21 | #import <QItemSelectionModel> |
Kateryna Kostiuk | a9e5c71 | 2017-05-15 16:46:16 -0400 | [diff] [blame] | 22 | #import <QSize> |
| 23 | #import <QtMacExtras/qmacfunctions.h> |
| 24 | #import <QPixmap> |
Kateryna Kostiuk | db1f3a1 | 2017-04-24 12:08:28 -0400 | [diff] [blame] | 25 | |
| 26 | //LRC |
| 27 | #import <account.h> |
| 28 | #import <availableAccountModel.h> |
| 29 | #import <contactRequest.h> |
| 30 | #import <pendingContactRequestModel.h> |
Kateryna Kostiuk | a9e5c71 | 2017-05-15 16:46:16 -0400 | [diff] [blame] | 31 | #import <globalinstances.h> |
| 32 | #import <contactmethod.h> |
Kateryna Kostiuk | db1f3a1 | 2017-04-24 12:08:28 -0400 | [diff] [blame] | 33 | |
| 34 | #import "ContactRequestsListVC.h" |
| 35 | #import "QNSTreeController.h" |
Kateryna Kostiuk | a9e5c71 | 2017-05-15 16:46:16 -0400 | [diff] [blame] | 36 | #import <interfaces/pixmapmanipulatori.h> |
Kateryna Kostiuk | db1f3a1 | 2017-04-24 12:08:28 -0400 | [diff] [blame] | 37 | |
| 38 | @interface ContactRequestsListVC () |
| 39 | |
| 40 | @property QNSTreeController* requestsTreeController; |
| 41 | @property (unsafe_unretained) IBOutlet NSOutlineView* contactRequestView; |
| 42 | @property (unsafe_unretained) IBOutlet NSTextField* noRequestsLabel; |
| 43 | @end |
| 44 | |
| 45 | @implementation ContactRequestsListVC |
| 46 | |
| 47 | @synthesize requestsTreeController; |
| 48 | @synthesize contactRequestView; |
| 49 | @synthesize noRequestsLabel; |
| 50 | |
| 51 | typedef NS_ENUM(NSInteger, ContactAction) { |
| 52 | ACCEPT = 0, |
| 53 | REFUSE, |
| 54 | BLOCK, |
| 55 | }; |
| 56 | |
| 57 | NSInteger const TAG_NAME = 100; |
| 58 | NSInteger const TAG_RINGID = 200; |
Kateryna Kostiuk | a9e5c71 | 2017-05-15 16:46:16 -0400 | [diff] [blame] | 59 | NSInteger const TAG_PHOTO = 300; |
| 60 | |
Kateryna Kostiuk | a9e5c71 | 2017-05-15 16:46:16 -0400 | [diff] [blame] | 61 | |
Kateryna Kostiuk | db1f3a1 | 2017-04-24 12:08:28 -0400 | [diff] [blame] | 62 | |
| 63 | - (void)awakeFromNib |
| 64 | { |
| 65 | Account* chosenAccount = [self chosenAccount]; |
| 66 | requestsTreeController = [[QNSTreeController alloc] initWithQModel:chosenAccount->pendingContactRequestModel()]; |
| 67 | [requestsTreeController setAvoidsEmptySelection:NO]; |
| 68 | [requestsTreeController setAlwaysUsesMultipleValuesMarker:YES]; |
| 69 | [requestsTreeController setChildrenKeyPath:@"children"]; |
| 70 | |
| 71 | [contactRequestView bind:@"content" toObject:requestsTreeController withKeyPath:@"arrangedObjects" options:nil]; |
| 72 | [contactRequestView bind:@"sortDescriptors" toObject:requestsTreeController withKeyPath:@"sortDescriptors" options:nil]; |
| 73 | [contactRequestView bind:@"selectionIndexPaths" toObject:requestsTreeController withKeyPath:@"selectionIndexPaths" options:nil]; |
Kateryna Kostiuk | a9e5c71 | 2017-05-15 16:46:16 -0400 | [diff] [blame] | 74 | contactRequestView.selectionHighlightStyle = NSTableViewSelectionHighlightStyleNone; |
Kateryna Kostiuk | db1f3a1 | 2017-04-24 12:08:28 -0400 | [diff] [blame] | 75 | [noRequestsLabel setHidden:[contactRequestView numberOfRows]>0]; |
| 76 | |
| 77 | } |
| 78 | |
| 79 | - (IBAction)acceptContactRequest:(NSView*)sender |
| 80 | { |
| 81 | NSInteger row = [self.contactRequestView rowForView:sender]; |
| 82 | [self performAction:ACCEPT forRequestAtRow:row]; |
| 83 | } |
| 84 | |
| 85 | - (IBAction)refuseContactRequest:(NSView*)sender |
| 86 | { |
| 87 | NSInteger row = [self.contactRequestView rowForView:sender]; |
| 88 | [self performAction:REFUSE forRequestAtRow:row]; |
| 89 | } |
| 90 | |
| 91 | - (IBAction)blockContactRequest:(NSView*)sender |
| 92 | { |
| 93 | NSInteger row = [self.contactRequestView rowForView:sender]; |
| 94 | [self performAction:BLOCK forRequestAtRow:row]; |
| 95 | } |
| 96 | |
| 97 | -(void) performAction:(ContactAction)action forRequestAtRow:(NSInteger)row { |
| 98 | id item = [self.contactRequestView itemAtRow:row]; |
| 99 | QModelIndex qIdx = [self.requestsTreeController toQIdx:((NSTreeNode*)item)]; |
| 100 | Account* chosenAccount = AccountModel::instance().userChosenAccount(); |
| 101 | const auto& var = qIdx.data(static_cast<int>(Ring::Role::Object)); |
| 102 | if (!var.isValid()) { |
| 103 | return; |
| 104 | } |
| 105 | auto contactRequest = qvariant_cast<ContactRequest*>(var); |
| 106 | switch (action) { |
| 107 | case ACCEPT: |
| 108 | contactRequest->accept(); |
| 109 | break; |
| 110 | case REFUSE: |
| 111 | contactRequest->discard(); |
| 112 | break; |
| 113 | case BLOCK: |
| 114 | contactRequest->block(); |
| 115 | break; |
| 116 | default: |
| 117 | break; |
| 118 | } |
| 119 | [noRequestsLabel setHidden:[contactRequestView numberOfRows]>0]; |
| 120 | } |
| 121 | |
| 122 | #pragma mark - NSOutlineViewDelegate methods |
| 123 | |
| 124 | - (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item |
| 125 | { |
| 126 | return NO; |
| 127 | } |
| 128 | |
| 129 | - (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item |
| 130 | { |
Kateryna Kostiuk | 8ca0c7e | 2017-06-14 15:18:58 -0400 | [diff] [blame] | 131 | NSTableCellView* result = [outlineView makeViewWithIdentifier:@"ContactRequestView" owner:self]; |
Kateryna Kostiuk | db1f3a1 | 2017-04-24 12:08:28 -0400 | [diff] [blame] | 132 | |
| 133 | QModelIndex qIdx = [self.requestsTreeController toQIdx:((NSTreeNode*)item)]; |
| 134 | if(!qIdx.isValid()) { |
| 135 | return result; |
| 136 | } |
Kateryna Kostiuk | a9e5c71 | 2017-05-15 16:46:16 -0400 | [diff] [blame] | 137 | |
Kateryna Kostiuk | db1f3a1 | 2017-04-24 12:08:28 -0400 | [diff] [blame] | 138 | NSTextField* nameLabel = [result viewWithTag:TAG_NAME]; |
| 139 | NSTextField* ringIDLabel = [result viewWithTag:TAG_RINGID]; |
Kateryna Kostiuk | a9e5c71 | 2017-05-15 16:46:16 -0400 | [diff] [blame] | 140 | NSImageView* photoView = [result viewWithTag:TAG_PHOTO]; |
Kateryna Kostiuk | db1f3a1 | 2017-04-24 12:08:28 -0400 | [diff] [blame] | 141 | |
Kateryna Kostiuk | a9e5c71 | 2017-05-15 16:46:16 -0400 | [diff] [blame] | 142 | ContactRequest* contactRequest = qvariant_cast<ContactRequest*>(qIdx.data((int)Ring::Role::Object)); |
| 143 | Person* person = contactRequest->peer(); |
| 144 | if(!person) { |
| 145 | Account* chosenAccount = [self chosenAccount]; |
| 146 | NSString* ringID = chosenAccount->pendingContactRequestModel()->data(qIdx,Qt::DisplayRole).toString().toNSString(); |
| 147 | [nameLabel setStringValue:ringID]; |
| 148 | return result; |
| 149 | } |
| 150 | |
| 151 | QVariant photo = GlobalInstances::pixmapManipulator().contactPhoto(person, {100,100}); |
| 152 | [photoView setImage:QtMac::toNSImage(qvariant_cast<QPixmap>(photo))]; |
| 153 | |
| 154 | NSString* idString = person->phoneNumbers()[0]->getBestId().toNSString(); |
| 155 | if(person->formattedName() != nil && person->formattedName().length()>0) { |
| 156 | NSString* name = person->formattedName().toNSString(); |
| 157 | [nameLabel setStringValue:name]; |
| 158 | if(![person->formattedName().toNSString() isEqualToString:idString]){ |
| 159 | NSString* formattedID = [NSString stringWithFormat:@"%@%@%@",@"(",idString, @")"]; |
| 160 | [ringIDLabel setStringValue:formattedID]; |
| 161 | } |
| 162 | return result; |
| 163 | } |
| 164 | [nameLabel setStringValue:idString]; |
| 165 | |
Kateryna Kostiuk | db1f3a1 | 2017-04-24 12:08:28 -0400 | [diff] [blame] | 166 | return result; |
| 167 | } |
| 168 | |
| 169 | -(Account* ) chosenAccount |
| 170 | { |
| 171 | QModelIndex index = AvailableAccountModel::instance().selectionModel()->currentIndex(); |
| 172 | return index.data(static_cast<int>(Account::Role::Object)).value<Account*>(); |
| 173 | } |
| 174 | |
Kateryna Kostiuk | db1f3a1 | 2017-04-24 12:08:28 -0400 | [diff] [blame] | 175 | @end |