blob: a9be19e983742726e555f92f9bfc3ada9d73cffd [file] [log] [blame]
Alexandre Lision8521baa2015-03-13 11:08:00 -04001/*
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 Lision5855b6a2015-02-03 11:31:05 -050030#import "HistoryViewController.h"
Alexandre Lision5855b6a2015-02-03 11:31:05 -050031
Alexandre Lisione33b5ac2015-03-19 20:13:45 -040032#import <categorizedhistorymodel.h>
Alexandre Lision7837e4f2015-03-23 09:58:12 -040033#import <QSortFilterProxyModel>
Alexandre Lisionc5148052015-03-04 15:10:35 -050034#import <callmodel.h>
35#import <call.h>
36#import <contactmethod.h>
Alexandre Lision5855b6a2015-02-03 11:31:05 -050037
Alexandre Lisionc5148052015-03-04 15:10:35 -050038#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 Lision5855b6a2015-02-03 11:31:05 -050044
45
Alexandre Lisionc5148052015-03-04 15:10:35 -050046@interface HistoryViewController()
47
48@property NSTreeController *treeController;
49@property (assign) IBOutlet NSOutlineView *historyView;
Alexandre Lision7837e4f2015-03-23 09:58:12 -040050@property QSortFilterProxyModel *historyProxyModel;
Alexandre Lisionc5148052015-03-04 15:10:35 -050051@end
Alexandre Lision5855b6a2015-02-03 11:31:05 -050052
53@implementation HistoryViewController
Alexandre Lision5855b6a2015-02-03 11:31:05 -050054@synthesize treeController;
Alexandre Lisionc5148052015-03-04 15:10:35 -050055@synthesize historyView;
Alexandre Lision7837e4f2015-03-23 09:58:12 -040056@synthesize historyProxyModel;
Alexandre Lision5855b6a2015-02-03 11:31:05 -050057
58- (id)initWithCoder:(NSCoder *)aDecoder
59{
Alexandre Lision5855b6a2015-02-03 11:31:05 -050060 if (self = [super initWithCoder:aDecoder]) {
61 NSLog(@"INIT HVC");
62
63 }
64 return self;
65}
66
Alexandre Lision5855b6a2015-02-03 11:31:05 -050067- (void)awakeFromNib
68{
Alexandre Lision7837e4f2015-03-23 09:58:12 -040069 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 Lision5855b6a2015-02-03 11:31:05 -050074
75 [treeController setAvoidsEmptySelection:NO];
76 [treeController setChildrenKeyPath:@"children"];
77
Alexandre Lisionc5148052015-03-04 15:10:35 -050078 [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 Lision5855b6a2015-02-03 11:31:05 -050083
Alexandre Lisionc5148052015-03-04 15:10:35 -050084 NSInteger idx = [historyView columnWithIdentifier:COLUMNID_DAY];
Alexandre Lision5855b6a2015-02-03 11:31:05 -050085
Alexandre Lisione33b5ac2015-03-19 20:13:45 -040086 CategorizedHistoryModel::instance()->addCollection<MinimalHistoryBackend>(LoadOptions::FORCE_ENABLED);
Alexandre Lision5855b6a2015-02-03 11:31:05 -050087
88}
89
Alexandre Lisionc5148052015-03-04 15:10:35 -050090- (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 Lision7837e4f2015-03-23 09:58:12 -040095 QVariant var = historyProxyModel->data(qIdx, (int)Call::Role::ContactMethod);
Alexandre Lisionc5148052015-03-04 15:10:35 -050096 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 Lision5855b6a2015-02-03 11:31:05 -0500105#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 Lision5855b6a2015-02-03 11:31:05 -0500121 if(item == nil)
122 return returnCell;
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500123 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 Lisionc5148052015-03-04 15:10:35 -0500157 QModelIndex qIdx = [treeController toQIdx:((NSTreeNode*)item)];
158 if(!qIdx.isValid())
159 return;
160
161 if ([[tableColumn identifier] isEqualToString:COLUMNID_DAY])
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500162 {
Alexandre Lision7837e4f2015-03-23 09:58:12 -0400163 cell.title = historyProxyModel->data(qIdx, Qt::DisplayRole).toString().toNSString();
Alexandre Lisionc5148052015-03-04 15:10:35 -0500164 } else if ([[tableColumn identifier] isEqualToString:COLUMNID_CONTACTMETHOD])
165 {
Alexandre Lision7837e4f2015-03-23 09:58:12 -0400166 cell.title = historyProxyModel->data(qIdx, (int)Call::Role::Number).toString().toNSString();
Alexandre Lisionc5148052015-03-04 15:10:35 -0500167 } else if ([[tableColumn identifier] isEqualToString:COLUMNID_DATE])
168 {
Alexandre Lision7837e4f2015-03-23 09:58:12 -0400169 cell.title = historyProxyModel->data(qIdx, (int)Call::Role::FormattedDate).toString().toNSString();
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500170 }
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