Smart View: contextual menu for not selected row

Contextual menu item - "add contact" works only when row is selected.
This patch change it and now contact could be added regardless of row
selection.

Change-Id: I1f0ec195c478faf46de031c8b4ac38a0469116ba
Reviewed-by: Anthony LĂ©onard <anthony.leonard@savoirfairelinux.com>
diff --git a/src/HistoryVC.mm b/src/HistoryVC.mm
index 5fae480..514a389 100644
--- a/src/HistoryVC.mm
+++ b/src/HistoryVC.mm
@@ -230,10 +230,12 @@
             if (!contactmethod->contact() || contactmethod->contact()->isPlaceHolder()) {
                 NSMenu *theMenu = [[NSMenu alloc]
                                    initWithTitle:@""];
-                [theMenu insertItemWithTitle:NSLocalizedString(@"Add to contacts", @"Contextual menu action")
-                                      action:@selector(addToContact)
-                               keyEquivalent:@"a"
-                                     atIndex:0];
+                NSMenuItem* addContactItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Add to contacts", @"Contextual menu action")
+                                                                        action:@selector(addContactForRow:)
+                                                                 keyEquivalent:@""];
+
+                [addContactItem setRepresentedObject:item];
+                [theMenu addItem:addContactItem];
                 return theMenu;
             }
         }
@@ -272,6 +274,16 @@
     }
 }
 
+- (void) addContactForRow:(id) sender
+{
+    NSInteger row = [historyView rowForItem:[sender representedObject]];
+    if(row < 0) {
+        return;
+    }
+    [historyView selectRowIndexes:[NSIndexSet indexSetWithIndex:row] byExtendingSelection:NO];
+    [self addToContact];
+}
+
 #pragma mark - NSPopOverDelegate
 
 - (void)popoverDidClose:(NSNotification *)notification
diff --git a/src/SmartViewVC.mm b/src/SmartViewVC.mm
index 0b99107..948346a 100644
--- a/src/SmartViewVC.mm
+++ b/src/SmartViewVC.mm
@@ -477,6 +477,16 @@
     }
 }
 
+- (void) addContactForRow:(id) sender
+{
+    NSInteger row = [smartView rowForItem:[sender representedObject]];
+    if(row < 0) {
+        return;
+    }
+    [smartView selectRowIndexes:[NSIndexSet indexSetWithIndex:row] byExtendingSelection:NO];
+    [self addToContact];
+}
+
 /**
  Copy a NSString in the general Pasteboard
 
@@ -572,10 +582,11 @@
         && !contactmethods.first()->contact()
         || contactmethods.first()->contact()->isPlaceHolder()) {
 
-        [theMenu insertItemWithTitle:NSLocalizedString(@"Add to contacts", @"Contextual menu action")
-                              action:@selector(addToContact)
-                       keyEquivalent:@"a"
-                             atIndex:theMenu.itemArray.count];
+        NSMenuItem* addContactItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Add to contacts", @"Contextual menu action")
+                                                                action:@selector(addContactForRow:)
+                                                         keyEquivalent:@""];
+        [addContactItem setRepresentedObject:item];
+        [theMenu addItem:addContactItem];
     } else if (auto person = contactmethods.first()->contact()) {
         NSMenuItem* copyNameItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Copy name", @"Contextual menu action")
                                                              action:@selector(copyStringToPasteboard:)