blob: 4073320b4cda1e2a0d11b2672566766bacce4f94 [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 Lision4ba18022015-04-23 12:17:40 -040030#import "HistoryVC.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 Lision7d2a48b2015-03-30 14:59:58 -040038#import "backends/MinimalHistoryBackend.h"
Alexandre Lisionc5148052015-03-04 15:10:35 -050039#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
Alexandre Lision4ba18022015-04-23 12:17:40 -040045@interface HistoryVC()
Alexandre Lisionc5148052015-03-04 15:10:35 -050046
47@property NSTreeController *treeController;
48@property (assign) IBOutlet NSOutlineView *historyView;
Alexandre Lision7837e4f2015-03-23 09:58:12 -040049@property QSortFilterProxyModel *historyProxyModel;
Alexandre Lisionc5148052015-03-04 15:10:35 -050050@end
Alexandre Lision5855b6a2015-02-03 11:31:05 -050051
Alexandre Lision4ba18022015-04-23 12:17:40 -040052@implementation HistoryVC
Alexandre Lision5855b6a2015-02-03 11:31:05 -050053@synthesize treeController;
Alexandre Lisionc5148052015-03-04 15:10:35 -050054@synthesize historyView;
Alexandre Lision7837e4f2015-03-23 09:58:12 -040055@synthesize historyProxyModel;
Alexandre Lision5855b6a2015-02-03 11:31:05 -050056
57- (id)initWithCoder:(NSCoder *)aDecoder
58{
Alexandre Lision5855b6a2015-02-03 11:31:05 -050059 if (self = [super initWithCoder:aDecoder]) {
60 NSLog(@"INIT HVC");
61
62 }
63 return self;
64}
65
Alexandre Lision5855b6a2015-02-03 11:31:05 -050066- (void)awakeFromNib
67{
Alexandre Lision7837e4f2015-03-23 09:58:12 -040068 historyProxyModel = new QSortFilterProxyModel(CategorizedHistoryModel::instance());
69 historyProxyModel->setSourceModel(CategorizedHistoryModel::instance());
70 historyProxyModel->setSortRole(static_cast<int>(Call::Role::Date));
71 historyProxyModel->sort(0,Qt::DescendingOrder);
72 treeController = [[QNSTreeController alloc] initWithQModel:historyProxyModel];
Alexandre Lision5855b6a2015-02-03 11:31:05 -050073
74 [treeController setAvoidsEmptySelection:NO];
75 [treeController setChildrenKeyPath:@"children"];
76
Alexandre Lisionc5148052015-03-04 15:10:35 -050077 [historyView bind:@"content" toObject:treeController withKeyPath:@"arrangedObjects" options:nil];
78 [historyView bind:@"sortDescriptors" toObject:treeController withKeyPath:@"sortDescriptors" options:nil];
79 [historyView bind:@"selectionIndexPaths" toObject:treeController withKeyPath:@"selectionIndexPaths" options:nil];
80 [historyView setTarget:self];
Alexandre Lision3b0bd332015-03-15 18:43:07 -040081 [historyView setDoubleAction:@selector(placeHistoryCall:)];
Alexandre Lision5855b6a2015-02-03 11:31:05 -050082
Alexandre Lisione33b5ac2015-03-19 20:13:45 -040083 CategorizedHistoryModel::instance()->addCollection<MinimalHistoryBackend>(LoadOptions::FORCE_ENABLED);
Alexandre Lision5855b6a2015-02-03 11:31:05 -050084}
85
Alexandre Lision3b0bd332015-03-15 18:43:07 -040086- (void)placeHistoryCall:(id)sender
Alexandre Lisionc5148052015-03-04 15:10:35 -050087{
88 if([[treeController selectedNodes] count] > 0) {
Alexandre Lisionc5148052015-03-04 15:10:35 -050089 QModelIndex qIdx = [treeController toQIdx:[treeController selectedNodes][0]];
Alexandre Lision7837e4f2015-03-23 09:58:12 -040090 QVariant var = historyProxyModel->data(qIdx, (int)Call::Role::ContactMethod);
Alexandre Lisionc5148052015-03-04 15:10:35 -050091 ContactMethod* m = qvariant_cast<ContactMethod*>(var);
92 if(m){
93 Call* c = CallModel::instance()->dialingCall();
94 c->setDialNumber(m);
95 c << Call::Action::ACCEPT;
96 }
97 }
98}
99
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500100#pragma mark - NSOutlineViewDelegate methods
101
102// -------------------------------------------------------------------------------
103// shouldSelectItem:item
104// -------------------------------------------------------------------------------
105- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item;
106{
107 return YES;
108}
109
110// -------------------------------------------------------------------------------
111// dataCellForTableColumn:tableColumn:item
112// -------------------------------------------------------------------------------
113- (NSCell *)outlineView:(NSOutlineView *)outlineView dataCellForTableColumn:(NSTableColumn *)tableColumn item:(id)item
114{
115 NSCell *returnCell = [tableColumn dataCell];
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500116 if(item == nil)
117 return returnCell;
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500118 return returnCell;
119}
120
121// -------------------------------------------------------------------------------
122// textShouldEndEditing:fieldEditor
123// -------------------------------------------------------------------------------
124- (BOOL)control:(NSControl *)control textShouldEndEditing:(NSText *)fieldEditor
125{
126 if ([[fieldEditor string] length] == 0)
127 {
128 // don't allow empty node names
129 return NO;
130 }
131 else
132 {
133 return YES;
134 }
135}
136
137// -------------------------------------------------------------------------------
138// shouldEditTableColumn:tableColumn:item
139//
140// Decide to allow the edit of the given outline view "item".
141// -------------------------------------------------------------------------------
142- (BOOL)outlineView:(NSOutlineView *)outlineView shouldEditTableColumn:(NSTableColumn *)tableColumn item:(id)item
143{
144 return NO;
145}
146
147// -------------------------------------------------------------------------------
148// outlineView:willDisplayCell:forTableColumn:item
149// -------------------------------------------------------------------------------
150- (void)outlineView:(NSOutlineView *)olv willDisplayCell:(NSCell*)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item
151{
Alexandre Lisionc5148052015-03-04 15:10:35 -0500152 QModelIndex qIdx = [treeController toQIdx:((NSTreeNode*)item)];
153 if(!qIdx.isValid())
154 return;
155
156 if ([[tableColumn identifier] isEqualToString:COLUMNID_DAY])
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500157 {
Alexandre Lision7837e4f2015-03-23 09:58:12 -0400158 cell.title = historyProxyModel->data(qIdx, Qt::DisplayRole).toString().toNSString();
Alexandre Lisionc5148052015-03-04 15:10:35 -0500159 } else if ([[tableColumn identifier] isEqualToString:COLUMNID_CONTACTMETHOD])
160 {
Alexandre Lision7837e4f2015-03-23 09:58:12 -0400161 cell.title = historyProxyModel->data(qIdx, (int)Call::Role::Number).toString().toNSString();
Alexandre Lisionc5148052015-03-04 15:10:35 -0500162 } else if ([[tableColumn identifier] isEqualToString:COLUMNID_DATE])
163 {
Alexandre Lision7837e4f2015-03-23 09:58:12 -0400164 cell.title = historyProxyModel->data(qIdx, (int)Call::Role::FormattedDate).toString().toNSString();
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500165 }
166}
167
168// -------------------------------------------------------------------------------
169// outlineViewSelectionDidChange:notification
170// -------------------------------------------------------------------------------
171- (void)outlineViewSelectionDidChange:(NSNotification *)notification
172{
173 // ask the tree controller for the current selection
174 //NSLog(@"outlineViewSelectionDidChange!!");
175}
176
177@end