Alexandre Lision | 8521baa | 2015-03-13 11:08:00 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2015 Savoir-Faire Linux Inc. |
| 3 | * Author: Alexandre Lision <alexandre.lision@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 | * Additional permission under GNU GPL version 3 section 7: |
| 20 | * |
| 21 | * If you modify this program, or any covered work, by linking or |
| 22 | * combining it with the OpenSSL project's OpenSSL library (or a |
| 23 | * modified version of that library), containing parts covered by the |
| 24 | * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc. |
| 25 | * grants you additional permission to convey the resulting work. |
| 26 | * Corresponding Source for a non-source form of such a combination |
| 27 | * shall include the source code for the parts of OpenSSL used as well |
| 28 | * as that of the covered work. |
| 29 | */ |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 30 | #import "HistoryViewController.h" |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 31 | |
Alexandre Lision | e33b5ac | 2015-03-19 20:13:45 -0400 | [diff] [blame] | 32 | #import <categorizedhistorymodel.h> |
Alexandre Lision | 7837e4f | 2015-03-23 09:58:12 -0400 | [diff] [blame^] | 33 | #import <QSortFilterProxyModel> |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 34 | #import <callmodel.h> |
| 35 | #import <call.h> |
| 36 | #import <contactmethod.h> |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 37 | |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 38 | #import "MinimalHistoryBackend.h" |
| 39 | #import "QNSTreeController.h" |
| 40 | |
| 41 | #define COLUMNID_DAY @"DayColumn" // the single column name in our outline view |
| 42 | #define COLUMNID_CONTACTMETHOD @"ContactMethodColumn" // the single column name in our outline view |
| 43 | #define COLUMNID_DATE @"DateColumn" // the single column name in our outline view |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 44 | |
| 45 | |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 46 | @interface HistoryViewController() |
| 47 | |
| 48 | @property NSTreeController *treeController; |
| 49 | @property (assign) IBOutlet NSOutlineView *historyView; |
Alexandre Lision | 7837e4f | 2015-03-23 09:58:12 -0400 | [diff] [blame^] | 50 | @property QSortFilterProxyModel *historyProxyModel; |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 51 | @end |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 52 | |
| 53 | @implementation HistoryViewController |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 54 | @synthesize treeController; |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 55 | @synthesize historyView; |
Alexandre Lision | 7837e4f | 2015-03-23 09:58:12 -0400 | [diff] [blame^] | 56 | @synthesize historyProxyModel; |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 57 | |
| 58 | - (id)initWithCoder:(NSCoder *)aDecoder |
| 59 | { |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 60 | if (self = [super initWithCoder:aDecoder]) { |
| 61 | NSLog(@"INIT HVC"); |
| 62 | |
| 63 | } |
| 64 | return self; |
| 65 | } |
| 66 | |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 67 | - (void)awakeFromNib |
| 68 | { |
Alexandre Lision | 7837e4f | 2015-03-23 09:58:12 -0400 | [diff] [blame^] | 69 | historyProxyModel = new QSortFilterProxyModel(CategorizedHistoryModel::instance()); |
| 70 | historyProxyModel->setSourceModel(CategorizedHistoryModel::instance()); |
| 71 | historyProxyModel->setSortRole(static_cast<int>(Call::Role::Date)); |
| 72 | historyProxyModel->sort(0,Qt::DescendingOrder); |
| 73 | treeController = [[QNSTreeController alloc] initWithQModel:historyProxyModel]; |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 74 | |
| 75 | [treeController setAvoidsEmptySelection:NO]; |
| 76 | [treeController setChildrenKeyPath:@"children"]; |
| 77 | |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 78 | [historyView bind:@"content" toObject:treeController withKeyPath:@"arrangedObjects" options:nil]; |
| 79 | [historyView bind:@"sortDescriptors" toObject:treeController withKeyPath:@"sortDescriptors" options:nil]; |
| 80 | [historyView bind:@"selectionIndexPaths" toObject:treeController withKeyPath:@"selectionIndexPaths" options:nil]; |
| 81 | [historyView setTarget:self]; |
| 82 | [historyView setDoubleAction:@selector(placeCall:)]; |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 83 | |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 84 | NSInteger idx = [historyView columnWithIdentifier:COLUMNID_DAY]; |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 85 | |
Alexandre Lision | e33b5ac | 2015-03-19 20:13:45 -0400 | [diff] [blame] | 86 | CategorizedHistoryModel::instance()->addCollection<MinimalHistoryBackend>(LoadOptions::FORCE_ENABLED); |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 87 | |
| 88 | } |
| 89 | |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 90 | - (void)placeCall:(id)sender |
| 91 | { |
| 92 | if([[treeController selectedNodes] count] > 0) { |
| 93 | Call* c = CallModel::instance()->dialingCall(); |
| 94 | QModelIndex qIdx = [treeController toQIdx:[treeController selectedNodes][0]]; |
Alexandre Lision | 7837e4f | 2015-03-23 09:58:12 -0400 | [diff] [blame^] | 95 | QVariant var = historyProxyModel->data(qIdx, (int)Call::Role::ContactMethod); |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 96 | ContactMethod* m = qvariant_cast<ContactMethod*>(var); |
| 97 | if(m){ |
| 98 | Call* c = CallModel::instance()->dialingCall(); |
| 99 | c->setDialNumber(m); |
| 100 | c << Call::Action::ACCEPT; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 105 | #pragma mark - NSOutlineViewDelegate methods |
| 106 | |
| 107 | // ------------------------------------------------------------------------------- |
| 108 | // shouldSelectItem:item |
| 109 | // ------------------------------------------------------------------------------- |
| 110 | - (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item; |
| 111 | { |
| 112 | return YES; |
| 113 | } |
| 114 | |
| 115 | // ------------------------------------------------------------------------------- |
| 116 | // dataCellForTableColumn:tableColumn:item |
| 117 | // ------------------------------------------------------------------------------- |
| 118 | - (NSCell *)outlineView:(NSOutlineView *)outlineView dataCellForTableColumn:(NSTableColumn *)tableColumn item:(id)item |
| 119 | { |
| 120 | NSCell *returnCell = [tableColumn dataCell]; |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 121 | if(item == nil) |
| 122 | return returnCell; |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 123 | return returnCell; |
| 124 | } |
| 125 | |
| 126 | // ------------------------------------------------------------------------------- |
| 127 | // textShouldEndEditing:fieldEditor |
| 128 | // ------------------------------------------------------------------------------- |
| 129 | - (BOOL)control:(NSControl *)control textShouldEndEditing:(NSText *)fieldEditor |
| 130 | { |
| 131 | if ([[fieldEditor string] length] == 0) |
| 132 | { |
| 133 | // don't allow empty node names |
| 134 | return NO; |
| 135 | } |
| 136 | else |
| 137 | { |
| 138 | return YES; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | // ------------------------------------------------------------------------------- |
| 143 | // shouldEditTableColumn:tableColumn:item |
| 144 | // |
| 145 | // Decide to allow the edit of the given outline view "item". |
| 146 | // ------------------------------------------------------------------------------- |
| 147 | - (BOOL)outlineView:(NSOutlineView *)outlineView shouldEditTableColumn:(NSTableColumn *)tableColumn item:(id)item |
| 148 | { |
| 149 | return NO; |
| 150 | } |
| 151 | |
| 152 | // ------------------------------------------------------------------------------- |
| 153 | // outlineView:willDisplayCell:forTableColumn:item |
| 154 | // ------------------------------------------------------------------------------- |
| 155 | - (void)outlineView:(NSOutlineView *)olv willDisplayCell:(NSCell*)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item |
| 156 | { |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 157 | QModelIndex qIdx = [treeController toQIdx:((NSTreeNode*)item)]; |
| 158 | if(!qIdx.isValid()) |
| 159 | return; |
| 160 | |
| 161 | if ([[tableColumn identifier] isEqualToString:COLUMNID_DAY]) |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 162 | { |
Alexandre Lision | 7837e4f | 2015-03-23 09:58:12 -0400 | [diff] [blame^] | 163 | cell.title = historyProxyModel->data(qIdx, Qt::DisplayRole).toString().toNSString(); |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 164 | } else if ([[tableColumn identifier] isEqualToString:COLUMNID_CONTACTMETHOD]) |
| 165 | { |
Alexandre Lision | 7837e4f | 2015-03-23 09:58:12 -0400 | [diff] [blame^] | 166 | cell.title = historyProxyModel->data(qIdx, (int)Call::Role::Number).toString().toNSString(); |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 167 | } else if ([[tableColumn identifier] isEqualToString:COLUMNID_DATE]) |
| 168 | { |
Alexandre Lision | 7837e4f | 2015-03-23 09:58:12 -0400 | [diff] [blame^] | 169 | cell.title = historyProxyModel->data(qIdx, (int)Call::Role::FormattedDate).toString().toNSString(); |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
| 173 | // ------------------------------------------------------------------------------- |
| 174 | // outlineViewSelectionDidChange:notification |
| 175 | // ------------------------------------------------------------------------------- |
| 176 | - (void)outlineViewSelectionDidChange:(NSNotification *)notification |
| 177 | { |
| 178 | // ask the tree controller for the current selection |
| 179 | //NSLog(@"outlineViewSelectionDidChange!!"); |
| 180 | } |
| 181 | |
| 182 | @end |