Kateryna Kostiuk | 77e9390 | 2017-05-30 16:38:11 -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> |
| 22 | |
| 23 | //LRC |
| 24 | #import <account.h> |
| 25 | #import <availableAccountModel.h> |
| 26 | #import <contactmethod.h> |
| 27 | #import <bannedContactmodel.h> |
| 28 | |
| 29 | #import "AccBannedContactsVC.h" |
| 30 | #import "QNSTreeController.h" |
| 31 | |
| 32 | @interface AccBannedContactsVC () |
| 33 | |
| 34 | @property QNSTreeController* bannedContactsTreeController; |
| 35 | @property (unsafe_unretained) IBOutlet NSOutlineView* banedContactsView; |
| 36 | |
| 37 | @end |
| 38 | |
| 39 | @implementation AccBannedContactsVC |
| 40 | |
| 41 | @synthesize bannedContactsTreeController; |
| 42 | @synthesize banedContactsView; |
| 43 | |
| 44 | NSInteger const TAG_NAME = 100; |
| 45 | NSInteger const TAG_RINGID = 200; |
| 46 | |
| 47 | - (void)awakeFromNib |
| 48 | { |
| 49 | QObject::connect(AccountModel::instance().selectionModel(), |
| 50 | &QItemSelectionModel::currentChanged, |
| 51 | [=](const QModelIndex ¤t, const QModelIndex &previous) { |
| 52 | if(!current.isValid()) |
| 53 | return; |
| 54 | [self loadAccount]; |
| 55 | }); |
| 56 | } |
| 57 | |
| 58 | - (void)loadAccount |
| 59 | { |
| 60 | auto account = AccountModel::instance().selectedAccount(); |
| 61 | self.bannedContactsTreeController = [[QNSTreeController alloc] initWithQModel:(QAbstractItemModel*)account->bannedContactModel()]; |
| 62 | [self.bannedContactsTreeController setAvoidsEmptySelection:NO]; |
| 63 | [self.bannedContactsTreeController setChildrenKeyPath:@"children"]; |
| 64 | |
| 65 | [self.banedContactsView bind:@"content" toObject:self.bannedContactsTreeController withKeyPath:@"arrangedObjects" options:nil]; |
| 66 | [self.banedContactsView bind:@"sortDescriptors" toObject:self.bannedContactsTreeController withKeyPath:@"sortDescriptors" options:nil]; |
| 67 | [self.banedContactsView bind:@"selectionIndexPaths" toObject:self.bannedContactsTreeController withKeyPath:@"selectionIndexPaths" options:nil]; |
| 68 | NSLog(@"numberofRowsBanned, %d", account->bannedContactModel()->rowCount()); |
| 69 | self.bannedListIsEmpty = banedContactsView.numberOfRows == 0; |
| 70 | } |
| 71 | |
| 72 | - (IBAction)unbanContact:(NSView*)sender |
| 73 | { |
| 74 | NSInteger row = [self.banedContactsView rowForView:sender]; |
| 75 | if(row < 0) { |
| 76 | return; |
| 77 | } |
| 78 | auto account = AccountModel::instance().selectedAccount(); |
| 79 | id item = [self.banedContactsView itemAtRow:row]; |
| 80 | QModelIndex qIdx = [self.bannedContactsTreeController toQIdx:((NSTreeNode*)item)]; |
| 81 | if(!qIdx.isValid()) { |
| 82 | return; |
| 83 | } |
| 84 | auto cm = qIdx.data(static_cast<int>(ContactMethod::Role::Object)).value<ContactMethod*>(); |
| 85 | if( account && cm) { |
| 86 | account->bannedContactModel()->remove(cm); |
| 87 | } |
| 88 | self.bannedListIsEmpty = banedContactsView.numberOfRows == 0; |
| 89 | } |
| 90 | |
| 91 | #pragma mark - NSOutlineViewDelegate methods |
| 92 | |
| 93 | - (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item |
| 94 | { |
| 95 | return YES; |
| 96 | } |
| 97 | |
| 98 | - (NSTableRowView *)outlineView:(NSOutlineView *)outlineView rowViewForItem:(id)item |
| 99 | { |
| 100 | return [outlineView makeViewWithIdentifier:@"HoverRowView" owner:nil]; |
| 101 | } |
| 102 | |
| 103 | - (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item |
| 104 | { |
| 105 | NSTableView* result = [outlineView makeViewWithIdentifier:@"BannedContactsCellView" owner:self]; |
| 106 | |
| 107 | QModelIndex qIdx = [self.bannedContactsTreeController toQIdx:((NSTreeNode*)item)]; |
| 108 | if(!qIdx.isValid()) |
| 109 | return result; |
| 110 | |
| 111 | NSTextField* nameLabel = [result viewWithTag:TAG_NAME]; |
| 112 | NSTextField* deviceIDLabel = [result viewWithTag:TAG_RINGID]; |
| 113 | |
| 114 | auto account = AccountModel::instance().selectedAccount(); |
| 115 | |
| 116 | NSString* stringID = account->bannedContactModel()->data(qIdx,Qt::DisplayRole).toString().toNSString(); |
| 117 | |
| 118 | [nameLabel setStringValue:stringID]; |
| 119 | [deviceIDLabel setStringValue:stringID]; |
| 120 | |
| 121 | return result; |
| 122 | } |
| 123 | |
| 124 | @end |