blob: d77471c230fb4492e994289c917d56823a286855 [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
Alexandre Lision54b0fae2015-08-04 15:19:01 -0400101- (void) dealloc
102{
103 delete contactProxyModel;
104}
105
Alexandre Lision3b0bd332015-03-15 18:43:07 -0400106- (IBAction)callContact:(id)sender
107{
108 if([[treeController selectedNodes] count] > 0) {
109 QModelIndex qIdx = [treeController toQIdx:[treeController selectedNodes][0]];
110 ContactMethod* m = nil;
111 if(((NSTreeNode*)[treeController selectedNodes][0]).indexPath.length == 2) {
112 // Person
113 QVariant var = qIdx.data((int)Person::Role::Object);
114 if (var.isValid()) {
115 Person *c = var.value<Person*>();
116 if (c->phoneNumbers().size() == 1) {
117 m = c->phoneNumbers().first();
118 }
119 }
120 } else if (((NSTreeNode*)[treeController selectedNodes][0]).indexPath.length == 3) {
121 //ContactMethod
122 QVariant var = qIdx.data(static_cast<int>(ContactMethod::Role::Object));
123 if (var.isValid()) {
124 m = var.value<ContactMethod *>();
125 }
126 }
127
128 if(m){
129 Call* c = CallModel::instance()->dialingCall();
130 c->setDialNumber(m);
131 c << Call::Action::ACCEPT;
132 }
133 }
134}
135
136#pragma mark - NSOutlineViewDelegate methods
137
138// -------------------------------------------------------------------------------
139// shouldSelectItem:item
140// -------------------------------------------------------------------------------
141- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item;
142{
143 QModelIndex qIdx = [treeController toQIdx:((NSTreeNode*)item)];
144 if(!qIdx.isValid())
145 return NO;
146
147 if(!qIdx.parent().isValid()) {
148 return NO;
149 } else {
150 return YES;
151 }
152}
153
154// -------------------------------------------------------------------------------
155// dataCellForTableColumn:tableColumn:item
156// -------------------------------------------------------------------------------
157- (NSCell *)outlineView:(NSOutlineView *)outlineView dataCellForTableColumn:(NSTableColumn *)tableColumn item:(id)item
158{
159 QModelIndex qIdx = [treeController toQIdx:((NSTreeNode*)item)];
160 PersonCell *returnCell = [tableColumn dataCell];
161 if(!qIdx.isValid())
162 return returnCell;
163
164 if(!qIdx.parent().isValid()) {
165 [returnCell setDrawsBackground:YES];
166 [returnCell setBackgroundColor:[NSColor selectedControlColor]];
167 } else {
168 [returnCell setDrawsBackground:NO];
169 }
170
171 return returnCell;
172}
173
174// -------------------------------------------------------------------------------
175// textShouldEndEditing:fieldEditor
176// -------------------------------------------------------------------------------
177- (BOOL)control:(NSControl *)control textShouldEndEditing:(NSText *)fieldEditor
178{
179 if ([[fieldEditor string] length] == 0)
180 {
181 // don't allow empty node names
182 return NO;
183 }
184 else
185 {
186 return YES;
187 }
188}
189
190// -------------------------------------------------------------------------------
191// shouldEditTableColumn:tableColumn:item
192//
193// Decide to allow the edit of the given outline view "item".
194// -------------------------------------------------------------------------------
195- (BOOL)outlineView:(NSOutlineView *)outlineView shouldEditTableColumn:(NSTableColumn *)tableColumn item:(id)item
196{
197 return NO;
198}
199
200// -------------------------------------------------------------------------------
201// outlineView:willDisplayCell:forTableColumn:item
202// -------------------------------------------------------------------------------
203- (void)outlineView:(NSOutlineView *)olv willDisplayCell:(NSCell*)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item
204{
205 QModelIndex qIdx = [treeController toQIdx:((NSTreeNode*)item)];
206 if(!qIdx.isValid())
207 return;
208
209 if ([[tableColumn identifier] isEqualToString:COLUMNID_NAME])
210 {
211 PersonCell *pCell = (PersonCell *)cell;
212 [pCell setPersonImage:nil];
213 if(!qIdx.parent().isValid()) {
214 pCell.title = qIdx.data(Qt::DisplayRole).toString().toNSString();
215 } else {
216 pCell.title = qIdx.data(Qt::DisplayRole).toString().toNSString();
217 if(((NSTreeNode*)item).indexPath.length == 2) {
218 Person* p = qvariant_cast<Person*>(qIdx.data((int)Person::Role::Object));
219 QVariant photo = ImageManipulationDelegate::instance()->contactPhoto(p, QSize(35,35));
220 [pCell setPersonImage:QtMac::toNSImage(qvariant_cast<QPixmap>(photo))];
221 }
222 }
223 }
224}
225
226// -------------------------------------------------------------------------------
227// outlineViewSelectionDidChange:notification
228// -------------------------------------------------------------------------------
229- (void)outlineViewSelectionDidChange:(NSNotification *)notification
230{
231
232}
233
234- (CGFloat)outlineView:(NSOutlineView *)outlineView heightOfRowByItem:(id)item
235{
236 QModelIndex qIdx = [treeController toQIdx:((NSTreeNode*)item)];
Alexandre Lision3b0bd332015-03-15 18:43:07 -0400237 if(!qIdx.parent().isValid()) {
238 return 20.0;
239 } else {
240 return 45.0;
241 }
242}
243
Alexandre Lision3b0bd332015-03-15 18:43:07 -0400244@end