blob: f33e57f9b524c179c740872bc2c861d0304190b6 [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 Lisionc5148052015-03-04 15:10:35 -050033#import <callmodel.h>
34#import <call.h>
35#import <contactmethod.h>
Alexandre Lision5855b6a2015-02-03 11:31:05 -050036
Alexandre Lisionc5148052015-03-04 15:10:35 -050037#import "MinimalHistoryBackend.h"
38#import "QNSTreeController.h"
39
40#define COLUMNID_DAY @"DayColumn" // the single column name in our outline view
41#define COLUMNID_CONTACTMETHOD @"ContactMethodColumn" // the single column name in our outline view
42#define COLUMNID_DATE @"DateColumn" // the single column name in our outline view
Alexandre Lision5855b6a2015-02-03 11:31:05 -050043
44
Alexandre Lisionc5148052015-03-04 15:10:35 -050045@interface HistoryViewController()
46
47@property NSTreeController *treeController;
48@property (assign) IBOutlet NSOutlineView *historyView;
49
50@end
Alexandre Lision5855b6a2015-02-03 11:31:05 -050051
52@implementation HistoryViewController
Alexandre Lision5855b6a2015-02-03 11:31:05 -050053@synthesize treeController;
Alexandre Lisionc5148052015-03-04 15:10:35 -050054@synthesize historyView;
Alexandre Lision5855b6a2015-02-03 11:31:05 -050055
56- (id)initWithCoder:(NSCoder *)aDecoder
57{
Alexandre Lision5855b6a2015-02-03 11:31:05 -050058 if (self = [super initWithCoder:aDecoder]) {
59 NSLog(@"INIT HVC");
60
61 }
62 return self;
63}
64
Alexandre Lision5855b6a2015-02-03 11:31:05 -050065- (void)awakeFromNib
66{
Alexandre Lisione33b5ac2015-03-19 20:13:45 -040067 treeController = [[QNSTreeController alloc] initWithQModel:CategorizedHistoryModel::instance()];
Alexandre Lision5855b6a2015-02-03 11:31:05 -050068
69 [treeController setAvoidsEmptySelection:NO];
70 [treeController setChildrenKeyPath:@"children"];
71
Alexandre Lisionc5148052015-03-04 15:10:35 -050072 [historyView bind:@"content" toObject:treeController withKeyPath:@"arrangedObjects" options:nil];
73 [historyView bind:@"sortDescriptors" toObject:treeController withKeyPath:@"sortDescriptors" options:nil];
74 [historyView bind:@"selectionIndexPaths" toObject:treeController withKeyPath:@"selectionIndexPaths" options:nil];
75 [historyView setTarget:self];
76 [historyView setDoubleAction:@selector(placeCall:)];
Alexandre Lision5855b6a2015-02-03 11:31:05 -050077
Alexandre Lisionc5148052015-03-04 15:10:35 -050078 NSInteger idx = [historyView columnWithIdentifier:COLUMNID_DAY];
Alexandre Lision5855b6a2015-02-03 11:31:05 -050079 [[[[self.historyView tableColumns] objectAtIndex:idx] headerCell] setStringValue:@"Name"];
80
Alexandre Lisione33b5ac2015-03-19 20:13:45 -040081 CategorizedHistoryModel::instance()->addCollection<MinimalHistoryBackend>(LoadOptions::FORCE_ENABLED);
Alexandre Lision5855b6a2015-02-03 11:31:05 -050082
83}
84
Alexandre Lisionc5148052015-03-04 15:10:35 -050085- (void)placeCall:(id)sender
86{
87 if([[treeController selectedNodes] count] > 0) {
88 Call* c = CallModel::instance()->dialingCall();
89 QModelIndex qIdx = [treeController toQIdx:[treeController selectedNodes][0]];
Alexandre Lisione33b5ac2015-03-19 20:13:45 -040090 QVariant var = CategorizedHistoryModel::instance()->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 Lisione33b5ac2015-03-19 20:13:45 -0400158 cell.title = CategorizedHistoryModel::instance()->data(qIdx, Qt::DisplayRole).toString().toNSString();
Alexandre Lisionc5148052015-03-04 15:10:35 -0500159 } else if ([[tableColumn identifier] isEqualToString:COLUMNID_CONTACTMETHOD])
160 {
Alexandre Lisione33b5ac2015-03-19 20:13:45 -0400161 cell.title = CategorizedHistoryModel::instance()->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 Lisione33b5ac2015-03-19 20:13:45 -0400164 cell.title = CategorizedHistoryModel::instance()->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