Contextual menu: remove contact

Add contextual menu item in contacts list for removing contact.

Change-Id: I370347cc7470d84c50384bf1d72e2ac3f526fef1
Reviewed-by: Anthony LĂ©onard <anthony.leonard@savoirfairelinux.com>
diff --git a/src/PersonsVC.h b/src/PersonsVC.h
index f784d21..a56bf48 100644
--- a/src/PersonsVC.h
+++ b/src/PersonsVC.h
@@ -18,7 +18,9 @@
  */
 #import <Cocoa/Cocoa.h>
 
-@interface PersonsVC : NSViewController <NSOutlineViewDelegate>
+#import "views/RingOutlineView.h"
+
+@interface PersonsVC : NSViewController <NSOutlineViewDelegate, ContextMenuDelegate>
 
 - (void)startCallForRow:(id)sender;
 
diff --git a/src/PersonsVC.mm b/src/PersonsVC.mm
index 5398cb4..0e01929 100644
--- a/src/PersonsVC.mm
+++ b/src/PersonsVC.mm
@@ -39,6 +39,8 @@
 #import "views/HoverTableRowView.h"
 #import "views/ContextualTableCellView.h"
 
+#import <AddressBook/AddressBook.h>
+
 class ReachablePersonModel : public QSortFilterProxyModel
 {
 public:
@@ -56,7 +58,7 @@
 @interface PersonsVC () {
 
     QNSTreeController *treeController;
-    __unsafe_unretained IBOutlet NSOutlineView *personsView;
+    __unsafe_unretained IBOutlet RingOutlineView *personsView;
     QSortFilterProxyModel *contactProxyModel;
 
 }
@@ -87,6 +89,7 @@
     [personsView bind:@"selectionIndexPaths" toObject:treeController withKeyPath:@"selectionIndexPaths" options:nil];
     [personsView setTarget:self];
     [personsView setDoubleAction:@selector(callContact:)];
+    [personsView setContextMenuDelegate:self];
 
     CategorizedContactModel::instance().setUnreachableHidden(YES);
 }
@@ -232,4 +235,74 @@
     return (((NSTreeNode*)item).indexPath.length == 2) ? 60.0 : 20.0;
 }
 
+#pragma mark - ContextMenuDelegate
+
+- (NSMenu*) contextualMenuForIndex:(NSTreeNode*) item
+{
+    QModelIndex qIdx = [treeController toQIdx:item];
+    if (!qIdx.isValid()) {
+        return nil;
+    }
+
+    if (qIdx.parent().isValid()) {
+        Person* p = qvariant_cast<Person*>(qIdx.data((int)Person::Role::Object));
+        if (p) {
+            NSMenu *theMenu = [[NSMenu alloc] initWithTitle:@""];
+            NSMenuItem* removeContactItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Delete contact", @"Contextual menu action")
+                                                                       action:@selector(removeContactForRow:)
+                                                                keyEquivalent:@""];
+            [removeContactItem setRepresentedObject:item];
+            [theMenu addItem:removeContactItem];
+            return theMenu;
+        }
+    }
+    return nil;
+}
+
+- (void) removeContactForRow:(id) sender
+{
+    QModelIndex qIdx = [treeController toQIdx:[sender representedObject]];
+    if (!qIdx.isValid()) {
+        return;
+    }
+
+    if (!qIdx.parent().isValid()) {
+        return;
+    }
+    Person* p = qvariant_cast<Person*>(qIdx.data((int)Person::Role::Object));
+    if(!p) {
+        return;
+    }
+
+    //check if contact is from MAC address book
+    ABPerson* adPerson = [[ABAddressBook sharedAddressBook] recordForUniqueId:[[NSString alloc] initWithUTF8String:p->uid().data()]];
+
+    if(adPerson) {
+        NSAlert *alert = [[NSAlert alloc] init];
+        [alert addButtonWithTitle:@"OK"];
+        [alert setMessageText:NSLocalizedString(@"Could not delete MAC contact", @"Contextual menu alert title")];
+        [alert setInformativeText:NSLocalizedString(@"To delete go to MAC Contacts App", @"Contextual menu alert remove contact")];
+        [alert setAlertStyle:NSAlertStyleWarning];
+        [alert runModal];
+        return;
+    }
+    NSString* name =  qIdx.data(Qt::DisplayRole).toString().toNSString();
+    if(name.length == 0) {
+        name = qIdx.data((int)Person::Role::IdOfLastCMUsed).toString().toNSString();
+    }
+    NSAlert *alert = [[NSAlert alloc] init];
+    [alert addButtonWithTitle:@"OK"];
+    [alert addButtonWithTitle:@"Cancel"];
+    [alert setMessageText:NSLocalizedString(@"Contact will be deleted", @"Contextual menu alert title")];
+    NSString* allertMsg = [NSString stringWithFormat:
+                           NSLocalizedString(@"Are you sure you want to delete contact \"%@\"", @"Contextual menu alert remove contact {Name}"), name];
+    [alert setInformativeText:allertMsg];
+    [alert setAlertStyle:NSAlertStyleWarning];
+
+    NSInteger answer = [alert runModal];
+    if (answer == NSAlertFirstButtonReturn) {
+        p->remove();
+    }
+}
+
 @end