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