blob: f1d1c26fb7ed877f288223b0cb424c9ad038ba87 [file] [log] [blame]
Alexandre Lision3b0bd332015-03-15 18:43:07 -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 */
30
31#import "PersonsVC.h"
32
33#import <personmodel.h>
34#import <callmodel.h>
35#import <categorizedcontactmodel.h>
36#import <QSortFilterProxyModel>
37#import <person.h>
38#import <contactmethod.h>
39#import <QtMacExtras/qmacfunctions.h>
40#import <QPixmap>
41
42#import "backends/AddressBookBackend.h"
43#import "QNSTreeController.h"
44#import "delegates/ImageManipulationDelegate.h"
45#import "views/PersonCell.h"
46
47#define COLUMNID_NAME @"NameColumn"
48
49class ReachablePersonModel : public QSortFilterProxyModel
50{
51public:
52 ReachablePersonModel(QAbstractItemModel* parent) : QSortFilterProxyModel(parent)
53 {
54 setSourceModel(parent);
55 }
56 virtual bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const
57 {
58 return sourceModel()->index(source_row,0,source_parent).flags() & Qt::ItemIsEnabled;
59 }
60};
61
62
63@interface PersonsVC ()
64
Alexandre Lision81c97212015-06-17 15:51:53 -040065@property QNSTreeController *treeController;
Alexandre Lision3b0bd332015-03-15 18:43:07 -040066@property (assign) IBOutlet NSOutlineView *personsView;
67@property QSortFilterProxyModel *contactProxyModel;
68
69@end
70
71@implementation PersonsVC
72@synthesize treeController;
73@synthesize personsView;
74@synthesize contactProxyModel;
75
76-(void) awakeFromNib
77{
78 new ImageManipulationDelegate();
79 NSLog(@"INIT PersonsVC");
80 contactProxyModel = new ReachablePersonModel(CategorizedContactModel::instance());
81 contactProxyModel->setSortRole(static_cast<int>(Qt::DisplayRole));
82 contactProxyModel->sort(0,Qt::AscendingOrder);
83 treeController = [[QNSTreeController alloc] initWithQModel:contactProxyModel];
84
85 [treeController setAvoidsEmptySelection:NO];
86 [treeController setChildrenKeyPath:@"children"];
87
88 [personsView bind:@"content" toObject:treeController withKeyPath:@"arrangedObjects" options:nil];
89 [personsView bind:@"sortDescriptors" toObject:treeController withKeyPath:@"sortDescriptors" options:nil];
90 [personsView bind:@"selectionIndexPaths" toObject:treeController withKeyPath:@"selectionIndexPaths" options:nil];
91 [personsView setTarget:self];
92 [personsView setDoubleAction:@selector(callContact:)];
93
94 CategorizedContactModel::instance()->setUnreachableHidden(YES);
95 PersonModel::instance()->addCollection<AddressBookBackend>(LoadOptions::FORCE_ENABLED);
96
97}
98
99- (IBAction)callContact:(id)sender
100{
101 if([[treeController selectedNodes] count] > 0) {
102 QModelIndex qIdx = [treeController toQIdx:[treeController selectedNodes][0]];
103 ContactMethod* m = nil;
104 if(((NSTreeNode*)[treeController selectedNodes][0]).indexPath.length == 2) {
105 // Person
106 QVariant var = qIdx.data((int)Person::Role::Object);
107 if (var.isValid()) {
108 Person *c = var.value<Person*>();
109 if (c->phoneNumbers().size() == 1) {
110 m = c->phoneNumbers().first();
111 }
112 }
113 } else if (((NSTreeNode*)[treeController selectedNodes][0]).indexPath.length == 3) {
114 //ContactMethod
115 QVariant var = qIdx.data(static_cast<int>(ContactMethod::Role::Object));
116 if (var.isValid()) {
117 m = var.value<ContactMethod *>();
118 }
119 }
120
121 if(m){
122 Call* c = CallModel::instance()->dialingCall();
123 c->setDialNumber(m);
124 c << Call::Action::ACCEPT;
125 }
126 }
127}
128
129#pragma mark - NSOutlineViewDelegate methods
130
131// -------------------------------------------------------------------------------
132// shouldSelectItem:item
133// -------------------------------------------------------------------------------
134- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item;
135{
136 QModelIndex qIdx = [treeController toQIdx:((NSTreeNode*)item)];
137 if(!qIdx.isValid())
138 return NO;
139
140 if(!qIdx.parent().isValid()) {
141 return NO;
142 } else {
143 return YES;
144 }
145}
146
147// -------------------------------------------------------------------------------
148// dataCellForTableColumn:tableColumn:item
149// -------------------------------------------------------------------------------
150- (NSCell *)outlineView:(NSOutlineView *)outlineView dataCellForTableColumn:(NSTableColumn *)tableColumn item:(id)item
151{
152 QModelIndex qIdx = [treeController toQIdx:((NSTreeNode*)item)];
153 PersonCell *returnCell = [tableColumn dataCell];
154 if(!qIdx.isValid())
155 return returnCell;
156
157 if(!qIdx.parent().isValid()) {
158 [returnCell setDrawsBackground:YES];
159 [returnCell setBackgroundColor:[NSColor selectedControlColor]];
160 } else {
161 [returnCell setDrawsBackground:NO];
162 }
163
164 return returnCell;
165}
166
167// -------------------------------------------------------------------------------
168// textShouldEndEditing:fieldEditor
169// -------------------------------------------------------------------------------
170- (BOOL)control:(NSControl *)control textShouldEndEditing:(NSText *)fieldEditor
171{
172 if ([[fieldEditor string] length] == 0)
173 {
174 // don't allow empty node names
175 return NO;
176 }
177 else
178 {
179 return YES;
180 }
181}
182
183// -------------------------------------------------------------------------------
184// shouldEditTableColumn:tableColumn:item
185//
186// Decide to allow the edit of the given outline view "item".
187// -------------------------------------------------------------------------------
188- (BOOL)outlineView:(NSOutlineView *)outlineView shouldEditTableColumn:(NSTableColumn *)tableColumn item:(id)item
189{
190 return NO;
191}
192
193// -------------------------------------------------------------------------------
194// outlineView:willDisplayCell:forTableColumn:item
195// -------------------------------------------------------------------------------
196- (void)outlineView:(NSOutlineView *)olv willDisplayCell:(NSCell*)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item
197{
198 QModelIndex qIdx = [treeController toQIdx:((NSTreeNode*)item)];
199 if(!qIdx.isValid())
200 return;
201
202 if ([[tableColumn identifier] isEqualToString:COLUMNID_NAME])
203 {
204 PersonCell *pCell = (PersonCell *)cell;
205 [pCell setPersonImage:nil];
206 if(!qIdx.parent().isValid()) {
207 pCell.title = qIdx.data(Qt::DisplayRole).toString().toNSString();
208 } else {
209 pCell.title = qIdx.data(Qt::DisplayRole).toString().toNSString();
210 if(((NSTreeNode*)item).indexPath.length == 2) {
211 Person* p = qvariant_cast<Person*>(qIdx.data((int)Person::Role::Object));
212 QVariant photo = ImageManipulationDelegate::instance()->contactPhoto(p, QSize(35,35));
213 [pCell setPersonImage:QtMac::toNSImage(qvariant_cast<QPixmap>(photo))];
214 }
215 }
216 }
217}
218
219// -------------------------------------------------------------------------------
220// outlineViewSelectionDidChange:notification
221// -------------------------------------------------------------------------------
222- (void)outlineViewSelectionDidChange:(NSNotification *)notification
223{
224
225}
226
227- (CGFloat)outlineView:(NSOutlineView *)outlineView heightOfRowByItem:(id)item
228{
229 QModelIndex qIdx = [treeController toQIdx:((NSTreeNode*)item)];
230 if(!qIdx.isValid())
231 return 0.0f;
232
233 if(!qIdx.parent().isValid()) {
234 return 20.0;
235 } else {
236 return 45.0;
237 }
238}
239
240
241@end