AccBannedContactsVC.mm 4.38 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 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
/*
 *  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>

//LRC
#import <account.h>
#import <availableAccountModel.h>
#import <contactmethod.h>
#import <bannedContactmodel.h>

#import "AccBannedContactsVC.h"
#import "QNSTreeController.h"

@interface AccBannedContactsVC ()

@property QNSTreeController* bannedContactsTreeController;
@property (unsafe_unretained) IBOutlet NSOutlineView* banedContactsView;

@end

@implementation AccBannedContactsVC

@synthesize bannedContactsTreeController;
@synthesize banedContactsView;

NSInteger const TAG_NAME        =   100;
NSInteger const TAG_RINGID      =   200;

- (void)awakeFromNib
{
    QObject::connect(AccountModel::instance().selectionModel(),
                     &QItemSelectionModel::currentChanged,
                     [=](const QModelIndex &current, const QModelIndex &previous) {
                         if(!current.isValid())
                             return;
                         [self loadAccount];
                     });
}

- (void)loadAccount
{
    auto account = AccountModel::instance().selectedAccount();
    self.bannedContactsTreeController = [[QNSTreeController alloc] initWithQModel:(QAbstractItemModel*)account->bannedContactModel()];
    [self.bannedContactsTreeController setAvoidsEmptySelection:NO];
    [self.bannedContactsTreeController setChildrenKeyPath:@"children"];

    [self.banedContactsView bind:@"content" toObject:self.bannedContactsTreeController withKeyPath:@"arrangedObjects" options:nil];
    [self.banedContactsView bind:@"sortDescriptors" toObject:self.bannedContactsTreeController withKeyPath:@"sortDescriptors" options:nil];
    [self.banedContactsView bind:@"selectionIndexPaths" toObject:self.bannedContactsTreeController withKeyPath:@"selectionIndexPaths" options:nil];
    NSLog(@"numberofRowsBanned, %d", account->bannedContactModel()->rowCount());
    self.bannedListIsEmpty = banedContactsView.numberOfRows == 0;
}

- (IBAction)unbanContact:(NSView*)sender
{
    NSInteger row = [self.banedContactsView rowForView:sender];
    if(row < 0) {
        return;
    }
    auto account = AccountModel::instance().selectedAccount();
    id item  = [self.banedContactsView itemAtRow:row];
    QModelIndex qIdx = [self.bannedContactsTreeController toQIdx:((NSTreeNode*)item)];
    if(!qIdx.isValid()) {
        return;
    }
    auto cm = qIdx.data(static_cast<int>(ContactMethod::Role::Object)).value<ContactMethod*>();
    if( account && cm) {
        account->bannedContactModel()->remove(cm);
    }
    self.bannedListIsEmpty = banedContactsView.numberOfRows == 0;
}

#pragma mark - NSOutlineViewDelegate methods

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

- (NSTableRowView *)outlineView:(NSOutlineView *)outlineView rowViewForItem:(id)item
{
    return [outlineView makeViewWithIdentifier:@"HoverRowView" owner:nil];
}

- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item
{
    NSTableView* result = [outlineView makeViewWithIdentifier:@"BannedContactsCellView" owner:self];

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

    NSTextField* nameLabel = [result viewWithTag:TAG_NAME];
    NSTextField* deviceIDLabel = [result viewWithTag:TAG_RINGID];

    auto account = AccountModel::instance().selectedAccount();

    NSString* stringID = account->bannedContactModel()->data(qIdx,Qt::DisplayRole).toString().toNSString();

    [nameLabel setStringValue:stringID];
    [deviceIDLabel setStringValue:stringID];

    return result;
}

@end