ContactRequestsListVC.mm 6.04 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 *  Copyright (C) 2015-2017 Savoir-faire Linux Inc.
 *  Author: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */

//Qt
#import <QItemSelectionModel>
22 23 24
#import <QSize>
#import <QtMacExtras/qmacfunctions.h>
#import <QPixmap>
25 26 27 28 29 30

//LRC
#import <account.h>
#import <availableAccountModel.h>
#import <contactRequest.h>
#import <pendingContactRequestModel.h>
31 32
#import <globalinstances.h>
#import <contactmethod.h>
33 34 35

#import "ContactRequestsListVC.h"
#import "QNSTreeController.h"
36
#import <interfaces/pixmapmanipulatori.h>
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

@interface ContactRequestsListVC ()

@property QNSTreeController* requestsTreeController;
@property (unsafe_unretained) IBOutlet NSOutlineView* contactRequestView;
@property (unsafe_unretained) IBOutlet NSTextField* noRequestsLabel;
@end

@implementation ContactRequestsListVC

@synthesize requestsTreeController;
@synthesize contactRequestView;
@synthesize noRequestsLabel;

typedef NS_ENUM(NSInteger, ContactAction) {
    ACCEPT = 0,
    REFUSE,
    BLOCK,
};

NSInteger const TAG_NAME        =   100;
NSInteger const TAG_RINGID      =   200;
59 60 61
NSInteger const TAG_PHOTO       =   300;


62 63 64 65 66 67 68 69 70 71 72 73

- (void)awakeFromNib
{
    Account* chosenAccount = [self chosenAccount];
    requestsTreeController = [[QNSTreeController alloc] initWithQModel:chosenAccount->pendingContactRequestModel()];
    [requestsTreeController setAvoidsEmptySelection:NO];
    [requestsTreeController setAlwaysUsesMultipleValuesMarker:YES];
    [requestsTreeController setChildrenKeyPath:@"children"];

    [contactRequestView bind:@"content" toObject:requestsTreeController withKeyPath:@"arrangedObjects" options:nil];
    [contactRequestView bind:@"sortDescriptors" toObject:requestsTreeController withKeyPath:@"sortDescriptors" options:nil];
    [contactRequestView bind:@"selectionIndexPaths" toObject:requestsTreeController withKeyPath:@"selectionIndexPaths" options:nil];
74
    contactRequestView.selectionHighlightStyle = NSTableViewSelectionHighlightStyleNone;
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    [noRequestsLabel setHidden:[contactRequestView numberOfRows]>0];

}

- (IBAction)acceptContactRequest:(NSView*)sender
{
    NSInteger row = [self.contactRequestView rowForView:sender];
    [self performAction:ACCEPT forRequestAtRow:row];
}

- (IBAction)refuseContactRequest:(NSView*)sender
{
    NSInteger row = [self.contactRequestView rowForView:sender];
    [self performAction:REFUSE forRequestAtRow:row];
}

- (IBAction)blockContactRequest:(NSView*)sender
{
    NSInteger row = [self.contactRequestView rowForView:sender];
    [self performAction:BLOCK forRequestAtRow:row];
}

-(void) performAction:(ContactAction)action forRequestAtRow:(NSInteger)row {
    id item  = [self.contactRequestView itemAtRow:row];
    QModelIndex qIdx = [self.requestsTreeController toQIdx:((NSTreeNode*)item)];
    Account* chosenAccount = AccountModel::instance().userChosenAccount();
    const auto& var = qIdx.data(static_cast<int>(Ring::Role::Object));
    if (!var.isValid()) {
        return;
    }
    auto contactRequest = qvariant_cast<ContactRequest*>(var);
    switch (action) {
        case ACCEPT:
            contactRequest->accept();
            break;
        case REFUSE:
            contactRequest->discard();
            break;
        case BLOCK:
            contactRequest->block();
            break;
        default:
            break;
    }
    [noRequestsLabel setHidden:[contactRequestView numberOfRows]>0];
}

#pragma mark - NSOutlineViewDelegate methods

- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item
{
    return NO;
}

- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item
{
131
    NSTableCellView* result = [outlineView makeViewWithIdentifier:@"ContactRequestView" owner:self];
132 133 134 135 136

    QModelIndex qIdx = [self.requestsTreeController toQIdx:((NSTreeNode*)item)];
    if(!qIdx.isValid()) {
        return result;
    }
137

138 139
    NSTextField* nameLabel = [result viewWithTag:TAG_NAME];
    NSTextField* ringIDLabel = [result viewWithTag:TAG_RINGID];
140 141 142 143 144 145 146 147 148 149
    NSImageView* photoView = [result viewWithTag:TAG_PHOTO];

    ContactRequest* contactRequest = qvariant_cast<ContactRequest*>(qIdx.data((int)Ring::Role::Object));
    Person* person = contactRequest->peer();
    if(!person) {
        Account* chosenAccount = [self chosenAccount];
        NSString* ringID = chosenAccount->pendingContactRequestModel()->data(qIdx,Qt::DisplayRole).toString().toNSString();
        [nameLabel setStringValue:ringID];
        return result;
    }
150

151 152 153 154 155 156 157 158 159 160 161 162 163 164
    QVariant photo = GlobalInstances::pixmapManipulator().contactPhoto(person, {100,100});
    [photoView setImage:QtMac::toNSImage(qvariant_cast<QPixmap>(photo))];

    NSString* idString  = person->phoneNumbers()[0]->getBestId().toNSString();
    if(person->formattedName() != nil && person->formattedName().length()>0) {
        NSString* name = person->formattedName().toNSString();
        [nameLabel setStringValue:name];
        if(![person->formattedName().toNSString() isEqualToString:idString]){
            NSString* formattedID = [NSString stringWithFormat:@"%@%@%@",@"(",idString, @")"];
            [ringIDLabel setStringValue:formattedID];
        }
        return result;
    }
    [nameLabel setStringValue:idString];
165 166 167 168 169 170 171 172 173 174 175

    return result;
}

-(Account* ) chosenAccount
{
    QModelIndex index = AvailableAccountModel::instance().selectionModel()->currentIndex();
    return index.data(static_cast<int>(Account::Role::Object)).value<Account*>();
}

@end