Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 1 | /* |
Alexandre Lision | 9fe374b | 2016-01-06 10:17:31 -0500 | [diff] [blame] | 2 | * Copyright (C) 2015-2016 Savoir-faire Linux Inc. |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 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 | |
| 20 | #import "SmartViewVC.h" |
| 21 | |
| 22 | //Qt |
| 23 | #import <QtMacExtras/qmacfunctions.h> |
| 24 | #import <QPixmap> |
| 25 | #import <QIdentityProxyModel> |
| 26 | #import <QItemSelectionModel> |
| 27 | |
| 28 | //LRC |
| 29 | #import <recentmodel.h> |
| 30 | #import <callmodel.h> |
| 31 | #import <call.h> |
Alexandre Lision | 45c1867 | 2016-11-02 15:24:45 -0400 | [diff] [blame] | 32 | #import <uri.h> |
Alexandre Lision | d14bda3 | 2015-10-13 11:34:29 -0400 | [diff] [blame] | 33 | #import <itemdataroles.h> |
Alexandre Lision | 45c1867 | 2016-11-02 15:24:45 -0400 | [diff] [blame] | 34 | #import <namedirectory.h> |
| 35 | #import <accountmodel.h> |
| 36 | #import <account.h> |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 37 | #import <person.h> |
| 38 | #import <contactmethod.h> |
| 39 | #import <globalinstances.h> |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 40 | #import <phonedirectorymodel.h> |
Kateryna Kostiuk | 1f705ab | 2017-04-18 12:45:59 -0400 | [diff] [blame^] | 41 | #import <AvailableAccountModel.h> |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 42 | |
| 43 | #import "QNSTreeController.h" |
| 44 | #import "delegates/ImageManipulationDelegate.h" |
Alexandre Lision | 4e280d6 | 2015-09-09 15:56:30 -0400 | [diff] [blame] | 45 | #import "views/HoverTableRowView.h" |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 46 | #import "PersonLinkerVC.h" |
Alexandre Lision | 4baba4c | 2016-02-11 13:00:57 -0500 | [diff] [blame] | 47 | #import "views/IconButton.h" |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 48 | #import "views/RingOutlineView.h" |
Alexandre Lision | 4e280d6 | 2015-09-09 15:56:30 -0400 | [diff] [blame] | 49 | #import "views/ContextualTableCellView.h" |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 50 | |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 51 | @interface SmartViewVC () <NSOutlineViewDelegate, NSPopoverDelegate, ContextMenuDelegate, ContactLinkedDelegate, KeyboardShortcutDelegate> { |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 52 | |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 53 | QNSTreeController *treeController; |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 54 | NSPopover* addToContactPopover; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 55 | |
| 56 | //UI elements |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 57 | __unsafe_unretained IBOutlet RingOutlineView* smartView; |
| 58 | __unsafe_unretained IBOutlet NSSearchField* searchField; |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 59 | __unsafe_unretained IBOutlet NSTabView* tabbar; |
Alexandre Lision | 45c1867 | 2016-11-02 15:24:45 -0400 | [diff] [blame] | 60 | |
| 61 | /* Pending ring usernames lookup for the search entry */ |
| 62 | QMetaObject::Connection usernameLookupConnection; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | @end |
| 66 | |
| 67 | @implementation SmartViewVC |
| 68 | |
| 69 | // Tags for views |
Alexandre Lision | 57914fa | 2016-02-10 14:52:24 -0500 | [diff] [blame] | 70 | NSInteger const IMAGE_TAG = 100; |
| 71 | NSInteger const DISPLAYNAME_TAG = 200; |
| 72 | NSInteger const DETAILS_TAG = 300; |
| 73 | NSInteger const CALL_BUTTON_TAG = 400; |
| 74 | NSInteger const TXT_BUTTON_TAG = 500; |
| 75 | NSInteger const CANCEL_BUTTON_TAG = 600; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 76 | |
| 77 | - (void)awakeFromNib |
| 78 | { |
| 79 | NSLog(@"INIT SmartView VC"); |
| 80 | |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 81 | treeController = [[QNSTreeController alloc] initWithQModel:RecentModel::instance().peopleProxy()]; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 82 | [treeController setAvoidsEmptySelection:NO]; |
| 83 | [treeController setChildrenKeyPath:@"children"]; |
| 84 | |
| 85 | [smartView bind:@"content" toObject:treeController withKeyPath:@"arrangedObjects" options:nil]; |
| 86 | [smartView bind:@"sortDescriptors" toObject:treeController withKeyPath:@"sortDescriptors" options:nil]; |
| 87 | [smartView bind:@"selectionIndexPaths" toObject:treeController withKeyPath:@"selectionIndexPaths" options:nil]; |
| 88 | [smartView setTarget:self]; |
Alexandre Lision | 89edc6a | 2015-11-09 11:30:47 -0500 | [diff] [blame] | 89 | [smartView setAction:@selector(selectRow:)]; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 90 | [smartView setDoubleAction:@selector(placeCall:)]; |
| 91 | |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 92 | [smartView setContextMenuDelegate:self]; |
| 93 | [smartView setShortcutsDelegate:self]; |
| 94 | |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 95 | QObject::connect(RecentModel::instance().peopleProxy(), |
Alexandre Lision | ee09846 | 2015-10-22 17:22:50 -0400 | [diff] [blame] | 96 | &QAbstractItemModel::dataChanged, |
| 97 | [self](const QModelIndex &topLeft, const QModelIndex &bottomRight) { |
| 98 | for(int row = topLeft.row() ; row <= bottomRight.row() ; ++row) |
| 99 | { |
| 100 | [smartView reloadDataForRowIndexes:[NSIndexSet indexSetWithIndex:row] |
| 101 | columnIndexes:[NSIndexSet indexSetWithIndex:0]]; |
| 102 | } |
| 103 | }); |
| 104 | |
Alexandre Lision | 89edc6a | 2015-11-09 11:30:47 -0500 | [diff] [blame] | 105 | QObject::connect(RecentModel::instance().selectionModel(), |
| 106 | &QItemSelectionModel::currentChanged, |
| 107 | [=](const QModelIndex ¤t, const QModelIndex &previous) { |
Alexandre Lision | 01cf5e3 | 2016-01-21 15:54:30 -0500 | [diff] [blame] | 108 | if(!current.isValid()) { |
| 109 | [smartView deselectAll:nil]; |
Alexandre Lision | 89edc6a | 2015-11-09 11:30:47 -0500 | [diff] [blame] | 110 | return; |
Alexandre Lision | 01cf5e3 | 2016-01-21 15:54:30 -0500 | [diff] [blame] | 111 | } |
Alexandre Lision | 89edc6a | 2015-11-09 11:30:47 -0500 | [diff] [blame] | 112 | |
| 113 | auto proxyIdx = RecentModel::instance().peopleProxy()->mapFromSource(current); |
| 114 | if (proxyIdx.isValid()) { |
| 115 | [treeController setSelectionQModelIndex:proxyIdx]; |
Alexandre Lision | 89edc6a | 2015-11-09 11:30:47 -0500 | [diff] [blame] | 116 | [tabbar selectTabViewItemAtIndex:0]; |
| 117 | [smartView scrollRowToVisible:proxyIdx.row()]; |
| 118 | } |
| 119 | }); |
| 120 | |
Alexandre Lision | 25fc82a | 2016-05-08 17:30:40 -0400 | [diff] [blame] | 121 | QObject::connect(RecentModel::instance().peopleProxy(), |
| 122 | &QAbstractItemModel::rowsInserted, |
| 123 | [=](const QModelIndex &parent, int first, int last) { |
| 124 | Q_UNUSED(parent) |
| 125 | Q_UNUSED(first) |
| 126 | Q_UNUSED(last) |
| 127 | [smartView scrollRowToVisible:0]; |
| 128 | }); |
| 129 | |
Kateryna Kostiuk | 1f705ab | 2017-04-18 12:45:59 -0400 | [diff] [blame^] | 130 | QObject::connect(AvailableAccountModel::instance().selectionModel(), |
| 131 | &QItemSelectionModel::currentChanged, |
| 132 | [self](const QModelIndex& idx){ |
| 133 | [self clearSearchField]; |
| 134 | }); |
| 135 | |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 136 | [self.view setWantsLayer:YES]; |
| 137 | [self.view setLayer:[CALayer layer]]; |
| 138 | [self.view.layer setBackgroundColor:[NSColor whiteColor].CGColor]; |
| 139 | |
| 140 | [searchField setWantsLayer:YES]; |
| 141 | [searchField setLayer:[CALayer layer]]; |
| 142 | [searchField.layer setBackgroundColor:[NSColor colorWithCalibratedRed:0.949 green:0.949 blue:0.949 alpha:0.9].CGColor]; |
| 143 | } |
| 144 | |
Alexandre Lision | 89edc6a | 2015-11-09 11:30:47 -0500 | [diff] [blame] | 145 | -(void) selectRow:(id)sender |
| 146 | { |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 147 | if ([treeController selectedNodes].count == 0) { |
| 148 | RecentModel::instance().selectionModel()->clearCurrentIndex(); |
| 149 | return; |
| 150 | } |
Alexandre Lision | 89edc6a | 2015-11-09 11:30:47 -0500 | [diff] [blame] | 151 | auto qIdx = [treeController toQIdx:[treeController selectedNodes][0]]; |
| 152 | auto proxyIdx = RecentModel::instance().peopleProxy()->mapToSource(qIdx); |
| 153 | RecentModel::instance().selectionModel()->setCurrentIndex(proxyIdx, QItemSelectionModel::ClearAndSelect); |
| 154 | } |
| 155 | |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 156 | - (void)placeCall:(id)sender |
| 157 | { |
| 158 | QModelIndex qIdx = [treeController toQIdx:[treeController selectedNodes][0]]; |
| 159 | ContactMethod* m = nil; |
| 160 | |
| 161 | // Double click on an ongoing call |
| 162 | if (qIdx.parent().isValid()) { |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | if([[treeController selectedNodes] count] > 0) { |
| 167 | QVariant var = qIdx.data((int)Call::Role::ContactMethod); |
| 168 | m = qvariant_cast<ContactMethod*>(var); |
| 169 | if (!m) { |
| 170 | // test if it is a person |
| 171 | QVariant var = qIdx.data((int)Person::Role::Object); |
| 172 | if (var.isValid()) { |
| 173 | Person *c = var.value<Person*>(); |
| 174 | if (c->phoneNumbers().size() > 0) { |
| 175 | m = c->phoneNumbers().first(); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // Before calling check if we properly extracted a contact method and that |
| 182 | // there is NOT already an ongoing call for this index (e.g: no children for this node) |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 183 | if(m && !RecentModel::instance().peopleProxy()->index(0, 0, qIdx).isValid()){ |
Alexandre Lision | 89edc6a | 2015-11-09 11:30:47 -0500 | [diff] [blame] | 184 | auto c = CallModel::instance().dialingCall(); |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 185 | c->setPeerContactMethod(m); |
| 186 | c << Call::Action::ACCEPT; |
Alexandre Lision | 89edc6a | 2015-11-09 11:30:47 -0500 | [diff] [blame] | 187 | CallModel::instance().selectCall(c); |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
Alexandre Lision | a3a43dc | 2017-03-30 16:21:30 -0400 | [diff] [blame] | 191 | - (void)showHistory |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 192 | { |
Alexandre Lision | ac4f5b6 | 2016-02-17 13:53:12 -0500 | [diff] [blame] | 193 | [tabbar selectTabViewItemAtIndex:1]; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 194 | } |
| 195 | |
Alexandre Lision | a3a43dc | 2017-03-30 16:21:30 -0400 | [diff] [blame] | 196 | - (void)showContacts |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 197 | { |
Alexandre Lision | ac4f5b6 | 2016-02-17 13:53:12 -0500 | [diff] [blame] | 198 | [tabbar selectTabViewItemAtIndex:2]; |
| 199 | } |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 200 | |
Alexandre Lision | a3a43dc | 2017-03-30 16:21:30 -0400 | [diff] [blame] | 201 | - (void)showSmartlist |
Alexandre Lision | ac4f5b6 | 2016-02-17 13:53:12 -0500 | [diff] [blame] | 202 | { |
| 203 | [tabbar selectTabViewItemAtIndex:0]; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | #pragma mark - NSOutlineViewDelegate methods |
| 207 | |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 208 | - (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item; |
| 209 | { |
| 210 | return YES; |
| 211 | } |
| 212 | |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 213 | - (BOOL)outlineView:(NSOutlineView *)outlineView shouldEditTableColumn:(NSTableColumn *)tableColumn item:(id)item |
| 214 | { |
| 215 | return NO; |
| 216 | } |
| 217 | |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 218 | - (void)outlineViewSelectionDidChange:(NSNotification *)notification |
| 219 | { |
| 220 | if ([treeController selectedNodes].count <= 0) { |
Alexandre Lision | 89edc6a | 2015-11-09 11:30:47 -0500 | [diff] [blame] | 221 | RecentModel::instance().selectionModel()->clearCurrentIndex(); |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 222 | return; |
| 223 | } |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 224 | } |
| 225 | |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 226 | - (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item |
| 227 | { |
Alexandre Lision | a4bb051 | 2016-04-19 18:04:31 -0400 | [diff] [blame] | 228 | QModelIndex proxyIdx = [treeController toQIdx:((NSTreeNode*)item)]; |
| 229 | QModelIndex qIdx = RecentModel::instance().peopleProxy()->mapToSource(proxyIdx); |
| 230 | |
| 231 | NSTableCellView* result; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 232 | if (!qIdx.parent().isValid()) { |
| 233 | result = [outlineView makeViewWithIdentifier:@"MainCell" owner:outlineView]; |
| 234 | NSTextField* details = [result viewWithTag:DETAILS_TAG]; |
| 235 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 236 | NSMutableArray* controls = [NSMutableArray arrayWithObject:[result viewWithTag:CALL_BUTTON_TAG]]; |
| 237 | [((ContextualTableCellView*) result) setContextualsControls:controls]; |
Alexandre Lision | 4e280d6 | 2015-09-09 15:56:30 -0400 | [diff] [blame] | 238 | |
Alexandre Lision | a4bb051 | 2016-04-19 18:04:31 -0400 | [diff] [blame] | 239 | if (auto call = RecentModel::instance().getActiveCall(qIdx)) { |
Alexandre Lision | d14bda3 | 2015-10-13 11:34:29 -0400 | [diff] [blame] | 240 | [details setStringValue:call->roleData((int)Ring::Role::FormattedState).toString().toNSString()]; |
Alexandre Lision | 21666f3 | 2015-09-22 17:04:36 -0400 | [diff] [blame] | 241 | [((ContextualTableCellView*) result) setActiveState:YES]; |
| 242 | } else { |
Alexandre Lision | d14bda3 | 2015-10-13 11:34:29 -0400 | [diff] [blame] | 243 | [details setStringValue:qIdx.data((int)Ring::Role::FormattedLastUsed).toString().toNSString()]; |
Alexandre Lision | 21666f3 | 2015-09-22 17:04:36 -0400 | [diff] [blame] | 244 | [((ContextualTableCellView*) result) setActiveState:NO]; |
| 245 | } |
| 246 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 247 | NSTextField* unreadCount = [result viewWithTag:TXT_BUTTON_TAG]; |
| 248 | int unread = qIdx.data((int)Ring::Role::UnreadTextMessageCount).toInt(); |
| 249 | [unreadCount setHidden:(unread == 0)]; |
| 250 | [unreadCount.layer setCornerRadius:5.0f]; |
| 251 | [unreadCount setStringValue:qIdx.data((int)Ring::Role::UnreadTextMessageCount).toString().toNSString()]; |
| 252 | |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 253 | } else { |
| 254 | result = [outlineView makeViewWithIdentifier:@"CallCell" owner:outlineView]; |
Alexandre Lision | 57914fa | 2016-02-10 14:52:24 -0500 | [diff] [blame] | 255 | NSMutableArray* controls = [NSMutableArray arrayWithObject:[result viewWithTag:CANCEL_BUTTON_TAG]]; |
| 256 | [((ContextualTableCellView*) result) setContextualsControls:controls]; |
| 257 | [((ContextualTableCellView*) result) setActiveState:NO]; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 258 | NSTextField* details = [result viewWithTag:DETAILS_TAG]; |
| 259 | |
| 260 | [details setStringValue:qIdx.data((int)Call::Role::HumanStateName).toString().toNSString()]; |
| 261 | } |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 262 | |
| 263 | NSTextField* displayName = [result viewWithTag:DISPLAYNAME_TAG]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 264 | [displayName setStringValue:qIdx.data((int)Ring::Role::Name).toString().toNSString()]; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 265 | NSImageView* photoView = [result viewWithTag:IMAGE_TAG]; |
Alexandre Lision | 43e91bc | 2016-04-19 18:04:52 -0400 | [diff] [blame] | 266 | |
| 267 | [photoView setImage:QtMac::toNSImage(qvariant_cast<QPixmap>(qIdx.data(Qt::DecorationRole)))]; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 268 | return result; |
| 269 | } |
| 270 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 271 | - (NSTableRowView *)outlineView:(NSOutlineView *)outlineView rowViewForItem:(id)item |
| 272 | { |
| 273 | return [outlineView makeViewWithIdentifier:@"HoverRowView" owner:nil]; |
| 274 | } |
| 275 | |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 276 | - (IBAction)callClickedAtRow:(id)sender { |
| 277 | NSInteger row = [smartView rowForView:sender]; |
| 278 | [smartView selectRowIndexes:[NSIndexSet indexSetWithIndex:row] byExtendingSelection:NO]; |
| 279 | [self placeCall:nil]; |
| 280 | } |
| 281 | |
| 282 | - (IBAction)hangUpClickedAtRow:(id)sender { |
| 283 | NSInteger row = [smartView rowForView:sender]; |
Alexandre Lision | 57914fa | 2016-02-10 14:52:24 -0500 | [diff] [blame] | 284 | id callNode = [smartView itemAtRow:row]; |
| 285 | auto callIdx = [treeController toQIdx:((NSTreeNode*)callNode)]; |
| 286 | |
| 287 | if (callIdx.isValid()) { |
| 288 | auto call = RecentModel::instance().getActiveCall(RecentModel::instance().peopleProxy()->mapToSource(callIdx)); |
| 289 | call << Call::Action::REFUSE; |
| 290 | } |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 291 | } |
| 292 | |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 293 | - (CGFloat)outlineView:(NSOutlineView *)outlineView heightOfRowByItem:(id)item |
| 294 | { |
| 295 | QModelIndex qIdx = [treeController toQIdx:((NSTreeNode*)item)]; |
Alexandre Lision | 57914fa | 2016-02-10 14:52:24 -0500 | [diff] [blame] | 296 | return (((NSTreeNode*)item).indexPath.length == 1) ? 60.0 : 50.0; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 297 | } |
| 298 | |
Alexandre Lision | ac4f5b6 | 2016-02-17 13:53:12 -0500 | [diff] [blame] | 299 | - (IBAction)placeCallFromSearchField:(id)sender |
| 300 | { |
| 301 | if ([searchField stringValue].length == 0) { |
| 302 | return; |
| 303 | } |
Alexandre Lision | 45c1867 | 2016-11-02 15:24:45 -0400 | [diff] [blame] | 304 | [self processSearchFieldInputAndStartCall:YES]; |
| 305 | } |
| 306 | |
| 307 | - (void) startCallFromURI:(const URI&) uri |
| 308 | { |
| 309 | auto cm = PhoneDirectoryModel::instance().getNumber(uri); |
Alexandre Lision | ac4f5b6 | 2016-02-17 13:53:12 -0500 | [diff] [blame] | 310 | auto c = CallModel::instance().dialingCall(); |
Alexandre Lision | ac4f5b6 | 2016-02-17 13:53:12 -0500 | [diff] [blame] | 311 | c->setPeerContactMethod(cm); |
| 312 | c << Call::Action::ACCEPT; |
| 313 | CallModel::instance().selectCall(c); |
| 314 | } |
| 315 | |
Alexandre Lision | 45c1867 | 2016-11-02 15:24:45 -0400 | [diff] [blame] | 316 | - (void) startConversationFromURI:(const URI&) uri |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 317 | { |
Alexandre Lision | 45c1867 | 2016-11-02 15:24:45 -0400 | [diff] [blame] | 318 | auto cm = PhoneDirectoryModel::instance().getNumber(uri); |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 319 | time_t currentTime; |
| 320 | ::time(¤tTime); |
| 321 | cm->setLastUsed(currentTime); |
Alexandre Lision | ac4f5b6 | 2016-02-17 13:53:12 -0500 | [diff] [blame] | 322 | auto proxyIdx = RecentModel::instance().peopleProxy()->mapToSource(RecentModel::instance().peopleProxy()->index(0, 0)); |
| 323 | RecentModel::instance().selectionModel()->setCurrentIndex(proxyIdx, QItemSelectionModel::ClearAndSelect); |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 324 | } |
| 325 | |
Alexandre Lision | 45c1867 | 2016-11-02 15:24:45 -0400 | [diff] [blame] | 326 | - (void) displayErrorModalWithTitle:(NSString*) title WithMessage:(NSString*) message |
| 327 | { |
| 328 | NSAlert* alert = [NSAlert alertWithMessageText:title |
| 329 | defaultButton:@"Ok" |
| 330 | alternateButton:nil |
| 331 | otherButton:nil |
| 332 | informativeTextWithFormat:message]; |
| 333 | |
| 334 | [alert beginSheetModalForWindow:self.view.window modalDelegate:nil didEndSelector:NULL contextInfo:NULL]; |
| 335 | } |
| 336 | |
| 337 | - (void) processSearchFieldInputAndStartCall:(BOOL) shouldCall |
| 338 | { |
| 339 | auto ringAccountList = AccountModel::instance().getAccountsByProtocol(Account::Protocol::RING); |
| 340 | auto sipAccountList = AccountModel::instance().getAccountsByProtocol(Account::Protocol::SIP); |
| 341 | BOOL hasValidRingAccount = NO; |
| 342 | BOOL hasValidSIPAccount = NO; |
| 343 | |
| 344 | NSString* noValidAccountTitle = NSLocalizedString(@"No valid account available", |
| 345 | @"Alert dialog title"); |
| 346 | NSString* noValidAccountMessage = NSLocalizedString(@"Make sure you have at least one valid account", |
| 347 | @"Alert dialo message"); |
| 348 | |
| 349 | Q_FOREACH(auto account, ringAccountList) { |
| 350 | if (account->isEnabled() && account->registrationState() == Account::RegistrationState::READY) { |
| 351 | hasValidRingAccount = YES; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | Q_FOREACH(auto account, sipAccountList) { |
| 356 | if (account->isEnabled() && account->registrationState() == Account::RegistrationState::READY) { |
| 357 | hasValidSIPAccount = YES; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | const auto* numberEntered = [searchField stringValue]; |
| 362 | URI uri = URI(numberEntered.UTF8String); |
Alexandre Lision | f909fb1 | 2016-11-03 12:35:36 -0400 | [diff] [blame] | 363 | [self clearSearchField]; |
Alexandre Lision | 45c1867 | 2016-11-02 15:24:45 -0400 | [diff] [blame] | 364 | |
| 365 | if (hasValidRingAccount) { |
| 366 | if (uri.protocolHint() == URI::ProtocolHint::RING) { |
| 367 | // If it is a RingID start the conversation or the call |
| 368 | if (shouldCall) { |
| 369 | [self startCallFromURI:uri]; |
| 370 | } else { |
| 371 | [self startConversationFromURI:uri]; |
| 372 | } |
| 373 | } else { |
| 374 | // If it's not a ringID and the user has a valid Ring account do a search on the blockchain |
| 375 | // If the user wants to make SIP calls, he can unregister his ring account |
| 376 | QString usernameToLookup = uri.userinfo(); |
| 377 | QObject::disconnect(usernameLookupConnection); |
| 378 | usernameLookupConnection = QObject::connect(&NameDirectory::instance(), |
| 379 | &NameDirectory::registeredNameFound, |
| 380 | [self, usernameToLookup, hasValidSIPAccount, shouldCall] (const Account* account, NameDirectory::LookupStatus status, const QString& address, const QString& name) { |
| 381 | if (usernameToLookup.compare(name) != 0) { |
| 382 | //That is not our lookup. |
| 383 | return; |
| 384 | } |
| 385 | |
| 386 | switch(status) { |
| 387 | case NameDirectory::LookupStatus::SUCCESS: { |
| 388 | URI uri = URI("ring:" + address); |
| 389 | if (shouldCall) { |
| 390 | [self startCallFromURI:uri]; |
| 391 | } else { |
| 392 | [self startConversationFromURI:uri]; |
| 393 | } |
| 394 | break; |
| 395 | } |
| 396 | case NameDirectory::LookupStatus::INVALID_NAME: |
| 397 | case NameDirectory::LookupStatus::ERROR: |
| 398 | case NameDirectory::LookupStatus::NOT_FOUND: { |
| 399 | // The lookup on the blockchain has failed for this user |
| 400 | // Check if the user has a valid sip account to use instead |
| 401 | if (hasValidSIPAccount) { |
| 402 | if (shouldCall) { |
| 403 | [self startCallFromURI:usernameToLookup]; |
| 404 | } else { |
| 405 | [self startConversationFromURI:usernameToLookup]; |
| 406 | } |
| 407 | } else { |
| 408 | [self displayErrorModalWithTitle:NSLocalizedString(@"Entered name not found", |
| 409 | @"Alert dialog title") |
| 410 | WithMessage:NSLocalizedString(@"The username you entered do not match a RingID on the network", |
| 411 | @"Alert dialog title")]; |
| 412 | } |
| 413 | break; |
| 414 | } |
| 415 | } |
Alexandre Lision | f909fb1 | 2016-11-03 12:35:36 -0400 | [diff] [blame] | 416 | }); |
| 417 | |
Alexandre Lision | 45c1867 | 2016-11-02 15:24:45 -0400 | [diff] [blame] | 418 | NameDirectory::instance().lookupName(nullptr, QString(), usernameToLookup); |
| 419 | } |
| 420 | } else if (hasValidSIPAccount) { |
| 421 | if (uri.protocolHint() == URI::ProtocolHint::RING) { |
| 422 | // If it is a RingID and no valid account is available, present error |
| 423 | [self displayErrorModalWithTitle:noValidAccountTitle |
| 424 | WithMessage:noValidAccountMessage]; |
Alexandre Lision | f909fb1 | 2016-11-03 12:35:36 -0400 | [diff] [blame] | 425 | return; |
Alexandre Lision | 45c1867 | 2016-11-02 15:24:45 -0400 | [diff] [blame] | 426 | } |
| 427 | if (shouldCall) { |
| 428 | [self startCallFromURI:uri]; |
| 429 | } else { |
| 430 | [self startConversationFromURI:uri]; |
| 431 | } |
| 432 | } else { |
| 433 | [self displayErrorModalWithTitle:noValidAccountTitle |
| 434 | WithMessage:noValidAccountMessage]; |
| 435 | } |
| 436 | } |
| 437 | |
Alexandre Lision | f909fb1 | 2016-11-03 12:35:36 -0400 | [diff] [blame] | 438 | - (void) clearSearchField |
| 439 | { |
| 440 | [searchField setStringValue:@""]; |
| 441 | RecentModel::instance().peopleProxy()->setFilterWildcard(QString::fromNSString([searchField stringValue])); |
| 442 | } |
| 443 | |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 444 | - (void) addToContact |
| 445 | { |
| 446 | if ([treeController selectedNodes].count == 0) |
| 447 | return; |
| 448 | |
| 449 | auto qIdx = [treeController toQIdx:[treeController selectedNodes][0]]; |
| 450 | auto originIdx = RecentModel::instance().peopleProxy()->mapToSource(qIdx); |
| 451 | auto contactmethod = RecentModel::instance().getContactMethods(originIdx); |
| 452 | if (contactmethod.isEmpty()) |
| 453 | return; |
| 454 | |
| 455 | if (addToContactPopover != nullptr) { |
| 456 | [addToContactPopover performClose:self]; |
| 457 | addToContactPopover = NULL; |
| 458 | } else if (contactmethod.first()) { |
| 459 | auto* editorVC = [[PersonLinkerVC alloc] initWithNibName:@"PersonLinker" bundle:nil]; |
| 460 | [editorVC setMethodToLink:contactmethod.first()]; |
| 461 | [editorVC setContactLinkedDelegate:self]; |
| 462 | addToContactPopover = [[NSPopover alloc] init]; |
| 463 | [addToContactPopover setContentSize:editorVC.view.frame.size]; |
| 464 | [addToContactPopover setContentViewController:editorVC]; |
| 465 | [addToContactPopover setAnimates:YES]; |
| 466 | [addToContactPopover setBehavior:NSPopoverBehaviorTransient]; |
| 467 | [addToContactPopover setDelegate:self]; |
| 468 | |
| 469 | [addToContactPopover showRelativeToRect:[smartView frameOfCellAtColumn:0 row:[smartView selectedRow]] |
| 470 | ofView:smartView preferredEdge:NSMaxXEdge]; |
| 471 | } |
| 472 | } |
| 473 | |
Alexandre Lision | 72a669e | 2016-09-14 17:52:16 -0400 | [diff] [blame] | 474 | /** |
| 475 | Copy a NSString in the general Pasteboard |
| 476 | |
| 477 | @param sender the NSObject containing the represented object to copy |
| 478 | */ |
| 479 | - (void) copyStringToPasteboard:(id) sender |
Alexandre Lision | d7bf288 | 2015-10-22 22:54:32 -0400 | [diff] [blame] | 480 | { |
| 481 | NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard]; |
| 482 | [pasteBoard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil]; |
| 483 | [pasteBoard setString:[sender representedObject] forType:NSStringPboardType]; |
| 484 | } |
| 485 | |
| 486 | - (void) callNumber:(id) sender |
| 487 | { |
| 488 | Call* c = CallModel::instance().dialingCall(); |
| 489 | c->setDialNumber(QString::fromNSString([sender representedObject])); |
| 490 | c << Call::Action::ACCEPT; |
| 491 | } |
| 492 | |
Alexandre Lision | bf0385e | 2015-10-22 17:36:28 -0400 | [diff] [blame] | 493 | #pragma NSTextFieldDelegate |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 494 | |
| 495 | - (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector |
| 496 | { |
| 497 | if (commandSelector == @selector(insertNewline:)) { |
| 498 | if([[searchField stringValue] isNotEqualTo:@""]) { |
Alexandre Lision | 45c1867 | 2016-11-02 15:24:45 -0400 | [diff] [blame] | 499 | [self processSearchFieldInputAndStartCall:NO]; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 500 | return YES; |
| 501 | } |
| 502 | } |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 503 | return NO; |
| 504 | } |
| 505 | |
Alexandre Lision | ac4f5b6 | 2016-02-17 13:53:12 -0500 | [diff] [blame] | 506 | - (void)controlTextDidChange:(NSNotification *) notification |
| 507 | { |
Alexandre Lision | ac4f5b6 | 2016-02-17 13:53:12 -0500 | [diff] [blame] | 508 | RecentModel::instance().peopleProxy()->setFilterWildcard(QString::fromNSString([searchField stringValue])); |
| 509 | } |
| 510 | |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 511 | #pragma mark - NSPopOverDelegate |
| 512 | |
| 513 | - (void)popoverDidClose:(NSNotification *)notification |
| 514 | { |
| 515 | if (addToContactPopover != nullptr) { |
| 516 | [addToContactPopover performClose:self]; |
| 517 | addToContactPopover = NULL; |
| 518 | } |
| 519 | } |
| 520 | |
Alexandre Lision | bf0385e | 2015-10-22 17:36:28 -0400 | [diff] [blame] | 521 | |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 522 | #pragma mark - ContactLinkedDelegate |
| 523 | |
| 524 | - (void)contactLinked |
| 525 | { |
| 526 | if (addToContactPopover != nullptr) { |
| 527 | [addToContactPopover performClose:self]; |
| 528 | addToContactPopover = NULL; |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | #pragma mark - KeyboardShortcutDelegate |
| 533 | |
| 534 | - (void) onAddShortcut |
| 535 | { |
| 536 | auto qIdx = [treeController toQIdx:[treeController selectedNodes][0]]; |
| 537 | auto originIdx = RecentModel::instance().peopleProxy()->mapToSource(qIdx); |
| 538 | auto contactmethods = RecentModel::instance().getContactMethods(originIdx); |
| 539 | if (contactmethods.isEmpty()) |
| 540 | return; |
| 541 | |
| 542 | auto contactmethod = contactmethods.first(); |
| 543 | if (contactmethod && (!contactmethod->contact() || contactmethod->contact()->isPlaceHolder())) { |
| 544 | [self addToContact]; |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | #pragma mark - ContextMenuDelegate |
| 549 | |
Alexandre Lision | 12946a7 | 2016-03-08 13:39:34 -0500 | [diff] [blame] | 550 | - (NSMenu*) contextualMenuForIndex:(NSTreeNode*) item |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 551 | { |
Alexandre Lision | 12946a7 | 2016-03-08 13:39:34 -0500 | [diff] [blame] | 552 | auto qIdx = [treeController toQIdx:item]; |
| 553 | |
| 554 | if (!qIdx.isValid()) { |
| 555 | return nil; |
| 556 | } |
| 557 | |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 558 | auto originIdx = RecentModel::instance().peopleProxy()->mapToSource(qIdx); |
| 559 | auto contactmethods = RecentModel::instance().getContactMethods(originIdx); |
| 560 | if (contactmethods.isEmpty()) |
| 561 | return nil; |
| 562 | |
Alexandre Lision | d7bf288 | 2015-10-22 22:54:32 -0400 | [diff] [blame] | 563 | NSMenu *theMenu = [[NSMenu alloc] initWithTitle:@""]; |
| 564 | |
| 565 | if (contactmethods.size() == 1 |
| 566 | && !contactmethods.first()->contact() |
| 567 | || contactmethods.first()->contact()->isPlaceHolder()) { |
| 568 | |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 569 | [theMenu insertItemWithTitle:NSLocalizedString(@"Add to contacts", @"Contextual menu action") |
| 570 | action:@selector(addToContact) |
| 571 | keyEquivalent:@"a" |
Alexandre Lision | d7bf288 | 2015-10-22 22:54:32 -0400 | [diff] [blame] | 572 | atIndex:theMenu.itemArray.count]; |
Alexandre Lision | 72a669e | 2016-09-14 17:52:16 -0400 | [diff] [blame] | 573 | } else if (auto person = contactmethods.first()->contact()) { |
| 574 | NSMenuItem* copyNameItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Copy name", @"Contextual menu action") |
| 575 | action:@selector(copyStringToPasteboard:) |
| 576 | keyEquivalent:@""]; |
| 577 | |
| 578 | [copyNameItem setRepresentedObject:person->formattedName().toNSString()]; |
| 579 | [theMenu addItem:copyNameItem]; |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 580 | } |
Alexandre Lision | d7bf288 | 2015-10-22 22:54:32 -0400 | [diff] [blame] | 581 | |
| 582 | NSMenu* copySubmenu = [[NSMenu alloc] init]; |
| 583 | NSMenu* callSubmenu = [[NSMenu alloc] init]; |
| 584 | |
Alexandre Lision | 72a669e | 2016-09-14 17:52:16 -0400 | [diff] [blame] | 585 | for (auto cm : contactmethods) { |
Alexandre Lision | d7bf288 | 2015-10-22 22:54:32 -0400 | [diff] [blame] | 586 | NSMenuItem* tmpCopyItem = [[NSMenuItem alloc] initWithTitle:cm->uri().toNSString() |
Alexandre Lision | 72a669e | 2016-09-14 17:52:16 -0400 | [diff] [blame] | 587 | action:@selector(copyStringToPasteboard:) |
Alexandre Lision | d7bf288 | 2015-10-22 22:54:32 -0400 | [diff] [blame] | 588 | keyEquivalent:@""]; |
| 589 | |
| 590 | [tmpCopyItem setRepresentedObject:cm->uri().toNSString()]; |
| 591 | [copySubmenu addItem:tmpCopyItem]; |
| 592 | |
| 593 | NSMenuItem* tmpCallItem = [[NSMenuItem alloc] initWithTitle:cm->uri().toNSString() |
| 594 | action:@selector(callNumber:) |
| 595 | keyEquivalent:@""]; |
| 596 | [tmpCallItem setRepresentedObject:cm->uri().toNSString()]; |
| 597 | [callSubmenu addItem:tmpCallItem]; |
| 598 | } |
| 599 | |
Alexandre Lision | 72a669e | 2016-09-14 17:52:16 -0400 | [diff] [blame] | 600 | NSMenuItem* copyNumberItem = [[NSMenuItem alloc] init]; |
| 601 | [copyNumberItem setTitle:NSLocalizedString(@"Copy number", @"Contextual menu action")]; |
| 602 | [copyNumberItem setSubmenu:copySubmenu]; |
Alexandre Lision | d7bf288 | 2015-10-22 22:54:32 -0400 | [diff] [blame] | 603 | |
| 604 | NSMenuItem* callItems = [[NSMenuItem alloc] init]; |
| 605 | [callItems setTitle:NSLocalizedString(@"Call number", @"Contextual menu action")]; |
| 606 | [callItems setSubmenu:callSubmenu]; |
| 607 | |
Alexandre Lision | 72a669e | 2016-09-14 17:52:16 -0400 | [diff] [blame] | 608 | [theMenu insertItem:copyNumberItem atIndex:theMenu.itemArray.count]; |
Alexandre Lision | d7bf288 | 2015-10-22 22:54:32 -0400 | [diff] [blame] | 609 | [theMenu insertItem:[NSMenuItem separatorItem] atIndex:theMenu.itemArray.count]; |
| 610 | [theMenu insertItem:callItems atIndex:theMenu.itemArray.count]; |
| 611 | |
| 612 | return theMenu; |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 613 | } |
| 614 | |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 615 | @end |