fix unread msg counter update + dock icon counter

2 problems were preventing the unread msg counter to work properly:
- Messages were not marked as read when displayed.
- Signals announcing status update were not listened to.

Moreover, the one present on dock icon was still listening to old LRC
mechanisms and is thus removed (until further improvements).

Finally, as the interactionStatusUpdated signal is triggered multiple
times when many messages are unread, some logic is added to debounce
it so only one refresh is triggered for a group of update.

Change-Id: Ieaa02d685bbd85d965de5315e8694ccdbd18eff1
Reviewed-by: Olivier Soldano <olivier.soldano@savoirfairelinux.com>
diff --git a/src/AppDelegate.mm b/src/AppDelegate.mm
index c75002e..bb0d719 100644
--- a/src/AppDelegate.mm
+++ b/src/AppDelegate.mm
@@ -144,16 +144,6 @@
                          }
                      });
 
-
-    QObject::connect(&Media::RecordingModel::instance(),
-                     &Media::RecordingModel::unreadMessagesCountChanged,
-                     [=](int unreadCount) {
-                         NSDockTile *tile = [[NSApplication sharedApplication] dockTile];
-                         NSString* label = unreadCount ? [NSString stringWithFormat:@"%d", unreadCount]: @"";
-                         [tile setBadgeLabel:label];
-                         [NSApp requestUserAttention:NSCriticalRequest];
-                     });
-
     QObject::connect(&Media::RecordingModel::instance(),
                      &Media::RecordingModel::newTextMessage,
                      [=](Media::TextRecording* t, ContactMethod* cm) {
diff --git a/src/MessagesVC.mm b/src/MessagesVC.mm
index 73eb2c3..aa751ba 100644
--- a/src/MessagesVC.mm
+++ b/src/MessagesVC.mm
@@ -236,6 +236,8 @@
             } else {
                 result = [tableView makeViewWithIdentifier:@"LeftMessageView" owner:self];
             }
+            if (interaction.status == lrc::api::interaction::Status::UNREAD)
+                convModel_->setInteractionRead(convUid_, it->first);
             break;
         case lrc::api::interaction::Type::INCOMING_DATA_TRANSFER:
         case lrc::api::interaction::Type::OUTGOING_DATA_TRANSFER:
diff --git a/src/SmartViewVC.mm b/src/SmartViewVC.mm
index e529a39..35dd7da 100755
--- a/src/SmartViewVC.mm
+++ b/src/SmartViewVC.mm
@@ -56,7 +56,8 @@
     __strong IBOutlet NSSegmentedControl *listTypeSelector;
     bool selectorIsPresent;
 
-    QMetaObject::Connection modelSortedConnection_, filterChangedConnection_, newConversationConnection_, conversationRemovedConnection_;
+    QMetaObject::Connection modelSortedConnection_, filterChangedConnection_, newConversationConnection_, conversationRemovedConnection_, interactionStatusUpdatedConnection_;
+    NSTimer* statusUpdateDebounceTimer;
 
     lrc::api::ConversationModel* model_;
     std::string selectedUid_;
@@ -184,6 +185,7 @@
         QObject::disconnect(filterChangedConnection_);
         QObject::disconnect(newConversationConnection_);
         QObject::disconnect(conversationRemovedConnection_);
+        QObject::disconnect(interactionStatusUpdatedConnection_);
         [self reloadData];
         if (model_ != nil) {
             modelSortedConnection_ = QObject::connect(model_, &lrc::api::ConversationModel::modelSorted,
@@ -202,6 +204,23 @@
                                                               [self] (){
                                                                   [self reloadData];
                                                               });
+            interactionStatusUpdatedConnection_ = QObject::connect(model_, &lrc::api::ConversationModel::interactionStatusUpdated,
+                                                                   [self] (const std::string& convUid) {
+                                                                       if (convUid != selectedUid_)
+                                                                           return;
+                                                                       auto it = getConversationFromUid(selectedUid_, *model_);
+                                                                       if (it != model_->allFilteredConversations().end()) {
+                                                                           // The following mechanism is here to debounce the interactionStatusUpdated so
+                                                                           // we do not redraw the conversation list for each message status changing
+                                                                           if (statusUpdateDebounceTimer != nil) {
+                                                                               [statusUpdateDebounceTimer invalidate];
+                                                                           }
+                                                                           statusUpdateDebounceTimer = [NSTimer timerWithTimeInterval:1.0 repeats:NO block:^(NSTimer * _Nonnull timer) {
+                                                                               [self reloadData];
+                                                                           }];
+                                                                           [[NSRunLoop mainRunLoop] addTimer:statusUpdateDebounceTimer forMode:NSRunLoopCommonModes];
+                                                                       }
+                                                                   });
             model_->setFilter(""); // Reset the filter
         }
         [searchField setStringValue:@""];