From b3c908499fa5a6577036bcd31369058deb0f4dda Mon Sep 17 00:00:00 2001
From: agsantos <aline.gondimsantos@savoirfairelinux.com>
Date: Thu, 10 Dec 2020 14:19:41 -0500
Subject: [PATCH] AutoAnswer: add EditText preference

- code cleanup

Change-Id: Ic15b71f1037424a463b0b52c260c24745cb14b00
---
 AudioFilter/FilterAudioSubscriber.cpp    |  4 +--
 AudioFilter/FilterAudioSubscriber.h      |  2 +-
 AudioFilter/FilterMediaHandler.cpp       |  4 +--
 AudioFilter/data/preferences.json        |  2 +-
 AutoAnswer/BotChatHandler.cpp            | 36 +++++++++++++++---------
 AutoAnswer/BotChatHandler.h              |  4 +--
 AutoAnswer/BotPeerChatSubscriber.cpp     | 33 ++++++++++++++++------
 AutoAnswer/BotPeerChatSubscriber.h       |  9 ++++--
 AutoAnswer/data/preferences.json         | 21 +++++++++++++-
 GreenScreen/videoSubscriber.h            |  2 +-
 HelloWorld/CenterCircleVideoSubscriber.h |  2 +-
 HelloWorld/CoinCircleVideoSubscriber.h   |  2 +-
 SDK/Templates/preferences.json           |  9 ++++++
 lib/frameFilter.cpp                      |  4 +--
 lib/frameFilter.h                        |  4 +--
 15 files changed, 97 insertions(+), 41 deletions(-)

diff --git a/AudioFilter/FilterAudioSubscriber.cpp b/AudioFilter/FilterAudioSubscriber.cpp
index 0ff8a27..f4542fe 100644
--- a/AudioFilter/FilterAudioSubscriber.cpp
+++ b/AudioFilter/FilterAudioSubscriber.cpp
@@ -29,7 +29,7 @@ extern "C" {
 
 #include <pluglog.h>
 
-const std::string TAG = "filter";
+const std::string TAG = "Filter";
 const char sep = separator();
 
 namespace jami {
@@ -43,7 +43,7 @@ FilterAudioSubscriber::FilterAudioSubscriber(const std::string& dataPath, const
 FilterAudioSubscriber::~FilterAudioSubscriber()
 {
     std::ostringstream oss;
-    oss << "~filterMediaProcessor" << std::endl;
+    oss << "~FilterMediaProcessor" << std::endl;
     Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
 }
 
diff --git a/AudioFilter/FilterAudioSubscriber.h b/AudioFilter/FilterAudioSubscriber.h
index 98ab5d4..6fa9a4e 100644
--- a/AudioFilter/FilterAudioSubscriber.h
+++ b/AudioFilter/FilterAudioSubscriber.h
@@ -45,7 +45,7 @@ public:
 
 private:
     // Observer pattern
-    Observable<AVFrame*>* observable_ = nullptr;
+    Observable<AVFrame*>* observable_{};
 
     // Data
     std::string path_;
diff --git a/AudioFilter/FilterMediaHandler.cpp b/AudioFilter/FilterMediaHandler.cpp
index 0c85734..3d978c2 100644
--- a/AudioFilter/FilterMediaHandler.cpp
+++ b/AudioFilter/FilterMediaHandler.cpp
@@ -24,9 +24,9 @@
 #include <string_view>
 
 const char sep = separator();
-const std::string TAG = "filter";
+const std::string TAG = "Filter";
 
-#define NAME "filter"
+#define NAME "Filter"
 
 namespace jami {
 
diff --git a/AudioFilter/data/preferences.json b/AudioFilter/data/preferences.json
index c878d46..8da6414 100644
--- a/AudioFilter/data/preferences.json
+++ b/AudioFilter/data/preferences.json
@@ -6,7 +6,7 @@
         "title": "Impulse Response",
         "summary": "Choose a impulse response",
         "defaultValue": "average_space_ir_0.mp3",
-        "scope": "plugin, filter",
+        "scope": "plugin,Filter",
         "entryValues": [
             "average_space_ir_0.mp3",
             "rir_jack_lyons_lp2_96k.mp3",
diff --git a/AutoAnswer/BotChatHandler.cpp b/AutoAnswer/BotChatHandler.cpp
index 371e51c..fa70e47 100644
--- a/AutoAnswer/BotChatHandler.cpp
+++ b/AutoAnswer/BotChatHandler.cpp
@@ -23,9 +23,9 @@
 #include "pluglog.h"
 
 const char sep = separator();
-const std::string TAG = "bot";
+const std::string TAG = "Bot";
 
-#define NAME "bot"
+#define NAME "Bot"
 
 namespace jami {
 
@@ -37,14 +37,19 @@ BotChatHandler::BotChatHandler(const JAMI_PluginAPI* api,
 {
     preferences_ = preferences;
     setId(datapath_);
-    peerChatSubscriber_ = std::make_shared<BotPeerChatSubscriber>(api_, dataPath);
+    auto answerIt = preferences_.find("answer");
+    auto inTextIt = preferences_.find("inText");
+    if (answerIt != preferences_.end() && inTextIt != preferences_.end())
+        peerChatSubscriber_ = std::make_shared<BotPeerChatSubscriber>(api_,
+                                                                      answerIt->second,
+                                                                      inTextIt->second);
 };
 
 void
 BotChatHandler::notifyChatSubject(std::pair<std::string, std::string>& subjectConnection,
                                   chatSubjectPtr subject)
 {
-    if (subjects.find(subject) == subjects.end()) {
+    if (peerChatSubscriber_ && subjects.find(subject) == subjects.end()) {
         std::ostringstream oss;
         oss << "NEW SUBJECT: account = " << subjectConnection.first
             << " peer = " << subjectConnection.second << std::endl;
@@ -63,10 +68,14 @@ BotChatHandler::getChatHandlerDetails()
 void
 BotChatHandler::setPreferenceAttribute(const std::string& key, const std::string& value)
 {
-    auto it = preferences_.find(key);
-    if (it != preferences_.end()) {
-        if (preferences_[key] != value) {
-            preferences_[key] = value;
+    if (peerChatSubscriber_) {
+        auto it = preferences_.find(key);
+        if (it != preferences_.end() && it->second != value) {
+            it->second = value;
+            if (key == "answer")
+                peerChatSubscriber_->setAnswer(value);
+            if (key == "inText")
+                peerChatSubscriber_->setInText(value);
         }
     }
 }
@@ -74,24 +83,23 @@ BotChatHandler::setPreferenceAttribute(const std::string& key, const std::string
 bool
 BotChatHandler::preferenceMapHasKey(const std::string& key)
 {
-    return false;
+    return key == "answer" || key == "inText";
 }
 
 void
 BotChatHandler::detach(chatSubjectPtr subject)
 {
-    if (subjects.find(subject) != subjects.end()) {
+    auto it = subjects.find(subject);
+    if (it != subjects.end()) {
         subject->detach(peerChatSubscriber_.get());
-        subjects.erase(subject);
+        subjects.erase(it);
     }
 }
 
 BotChatHandler::~BotChatHandler()
 {
-    auto& copy(subjects);
-    for (const auto& subject : copy) {
+    for (const auto& subject : subjects) {
         detach(subject);
     }
-    copy.clear();
 }
 } // namespace jami
diff --git a/AutoAnswer/BotChatHandler.h b/AutoAnswer/BotChatHandler.h
index ee0f72f..7b3eb78 100644
--- a/AutoAnswer/BotChatHandler.h
+++ b/AutoAnswer/BotChatHandler.h
@@ -40,7 +40,7 @@ public:
     BotChatHandler(const JAMI_PluginAPI* api,
                    std::map<std::string, std::string>&& preferences,
                    std::string&& dataPath);
-    ~BotChatHandler() override;
+    ~BotChatHandler();
 
     virtual void notifyChatSubject(std::pair<std::string, std::string>& subjectConnection,
                                    chatSubjectPtr subject) override;
@@ -49,7 +49,7 @@ public:
     virtual void setPreferenceAttribute(const std::string& key, const std::string& value) override;
     virtual bool preferenceMapHasKey(const std::string& key) override;
 
-    std::shared_ptr<BotPeerChatSubscriber> peerChatSubscriber_;
+    std::shared_ptr<BotPeerChatSubscriber> peerChatSubscriber_{};
 
 private:
     const JAMI_PluginAPI* api_;
diff --git a/AutoAnswer/BotPeerChatSubscriber.cpp b/AutoAnswer/BotPeerChatSubscriber.cpp
index 0d30eb6..f02b121 100644
--- a/AutoAnswer/BotPeerChatSubscriber.cpp
+++ b/AutoAnswer/BotPeerChatSubscriber.cpp
@@ -25,10 +25,14 @@ const std::string TAG = "bot";
 
 namespace jami {
 
-BotPeerChatSubscriber::BotPeerChatSubscriber(const JAMI_PluginAPI* api, std::string& textAnswer)
+BotPeerChatSubscriber::BotPeerChatSubscriber(const JAMI_PluginAPI* api,
+                                             const std::string& textAnswer,
+                                             const std::string& inText)
     : api_ {api}
-    , textAnswer_ {textAnswer}
-{}
+{
+    setAnswer(textAnswer);
+    setInText(inText);
+}
 
 BotPeerChatSubscriber::~BotPeerChatSubscriber()
 {
@@ -37,6 +41,18 @@ BotPeerChatSubscriber::~BotPeerChatSubscriber()
     Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
 }
 
+void
+BotPeerChatSubscriber::setAnswer(const std::string& textAnswer)
+{
+    textAnswer_ = textAnswer;
+}
+
+void
+BotPeerChatSubscriber::setInText(const std::string& inText)
+{
+    inText_ = inText;
+}
+
 void
 BotPeerChatSubscriber::update(Observable<pluginMessagePtr>*, const pluginMessagePtr& message)
 {
@@ -44,10 +60,8 @@ BotPeerChatSubscriber::update(Observable<pluginMessagePtr>*, const pluginMessage
         if (message->direction) {
             std::map<std::string, std::string> sendMsg;
             for (auto& pair : message->data) {
-                if (pair.first == "text/plain" && pair.second == "hi") {
-                    std::ostringstream sendMsgStream;
-                    sendMsgStream << "HelloWorld from bot plugin";
-                    sendMsg[pair.first] = sendMsgStream.str();
+                if (pair.first == "text/plain" && pair.second == inText_) {
+                    sendMsg[pair.first] = textAnswer_;
                 }
             }
             if (!sendMsg.empty()) {
@@ -72,8 +86,9 @@ BotPeerChatSubscriber::attached(Observable<pluginMessagePtr>* observable)
 void
 BotPeerChatSubscriber::detached(Observable<pluginMessagePtr>* observable)
 {
-    if (observables_.find(observable) != observables_.end()) {
-        observables_.erase(observable);
+    auto it = observables_.find(observable);
+    if (it != observables_.end()) {
+        observables_.erase(it);
         std::ostringstream oss;
         oss << "::Detached()" << std::endl;
         Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
diff --git a/AutoAnswer/BotPeerChatSubscriber.h b/AutoAnswer/BotPeerChatSubscriber.h
index faaffd3..ca6416a 100644
--- a/AutoAnswer/BotPeerChatSubscriber.h
+++ b/AutoAnswer/BotPeerChatSubscriber.h
@@ -34,7 +34,9 @@ namespace jami {
 class BotPeerChatSubscriber : public Observer<pluginMessagePtr>
 {
 public:
-    BotPeerChatSubscriber(const JAMI_PluginAPI* api, std::string& textAnswer);
+    BotPeerChatSubscriber(const JAMI_PluginAPI* api,
+                          const std::string& textAnswer,
+                          const std::string& inText);
     ~BotPeerChatSubscriber();
     virtual void update(Observable<pluginMessagePtr>*, pluginMessagePtr const&) override;
     virtual void attached(Observable<pluginMessagePtr>*) override;
@@ -43,12 +45,15 @@ public:
     void sendText(std::string& accountId,
                   std::string& peerId,
                   std::map<std::string, std::string>& sendMsg);
+    void setAnswer(const std::string& textAnswer);
+    void setInText(const std::string& inText);
 
 protected:
     // Observer pattern
     std::set<Observable<pluginMessagePtr>*> observables_;
     bool isAttached {false};
     const JAMI_PluginAPI* api_;
-    std::string textAnswer_{};
+    std::string textAnswer_ {};
+    std::string inText_ {};
 };
 } // namespace jami
diff --git a/AutoAnswer/data/preferences.json b/AutoAnswer/data/preferences.json
index 1e3ec72..5552907 100644
--- a/AutoAnswer/data/preferences.json
+++ b/AutoAnswer/data/preferences.json
@@ -1 +1,20 @@
-[ ]
+[
+    {
+        "category": "text",
+        "type": "EditText",
+        "key": "answer",
+        "title": "Bot Answer",
+        "summary": "Set bot text",
+        "defaultValue": "Hello World from bot preference",
+        "scope": "plugin,Bot"
+    },
+    {
+        "category": "text",
+        "type": "EditText",
+        "key": "inText",
+        "title": "Bot Trigger",
+        "summary": "Text that bot will answer to",
+        "defaultValue": "Hi",
+        "scope": "plugin,Bot"
+    }
+]
\ No newline at end of file
diff --git a/GreenScreen/videoSubscriber.h b/GreenScreen/videoSubscriber.h
index 5ca6618..8598feb 100644
--- a/GreenScreen/videoSubscriber.h
+++ b/GreenScreen/videoSubscriber.h
@@ -67,7 +67,7 @@ public:
 
 private:
     // Observer pattern
-    Observable<AVFrame*>* observable_ = nullptr;
+    Observable<AVFrame*>* observable_{};
 
     // Data
     std::string path_;
diff --git a/HelloWorld/CenterCircleVideoSubscriber.h b/HelloWorld/CenterCircleVideoSubscriber.h
index 512d7f2..e12d84c 100644
--- a/HelloWorld/CenterCircleVideoSubscriber.h
+++ b/HelloWorld/CenterCircleVideoSubscriber.h
@@ -45,7 +45,7 @@ public:
 
 private:
     // Observer pattern
-    Observable<AVFrame*>* observable_ = nullptr;
+    Observable<AVFrame*>* observable_{};
 
     // Data
     std::string path_;
diff --git a/HelloWorld/CoinCircleVideoSubscriber.h b/HelloWorld/CoinCircleVideoSubscriber.h
index fb82a7f..9d38c6e 100644
--- a/HelloWorld/CoinCircleVideoSubscriber.h
+++ b/HelloWorld/CoinCircleVideoSubscriber.h
@@ -45,7 +45,7 @@ public:
 
 private:
     // Observer pattern
-    Observable<AVFrame*>* observable_ = nullptr;
+    Observable<AVFrame*>* observable_{};
 
     // Data
     std::string path_;
diff --git a/SDK/Templates/preferences.json b/SDK/Templates/preferences.json
index 4caacdc..c8b86d5 100644
--- a/SDK/Templates/preferences.json
+++ b/SDK/Templates/preferences.json
@@ -19,5 +19,14 @@
         "defaultValue": "default values",
         "scope": "plugin, Name of API implementation",
         "mimeType": "*/*"
+    },
+    {
+        "category" : "genericEditText",
+        "type" : "EditText",
+        "key": "keyName",
+        "title" : "preference title",
+        "summary" : "preference summary",
+        "defaultValue" : "default value",
+        "scope" : "plugin, Name of API implementation"
     }
 ]
diff --git a/lib/frameFilter.cpp b/lib/frameFilter.cpp
index 9766b33..0d5b07f 100644
--- a/lib/frameFilter.cpp
+++ b/lib/frameFilter.cpp
@@ -236,7 +236,7 @@ FrameFilter::initOutputFilter(AVFilterInOut* out)
 {
     int ret = 0;
     const AVFilter* buffersink;
-    AVFilterContext* buffersinkCtx = nullptr;
+    AVFilterContext* buffersinkCtx{};
     AVMediaType mediaType = avfilter_pad_get_type(out->filter_ctx->input_pads, out->pad_idx);
 
     if (mediaType == AVMEDIA_TYPE_VIDEO)
@@ -283,7 +283,7 @@ FrameFilter::initInputFilter(AVFilterInOut* in, MediaStream msp)
         buffersrc = avfilter_get_by_name("abuffer");
     }
 
-    AVFilterContext* buffersrcCtx = nullptr;
+    AVFilterContext* buffersrcCtx{};
     if (buffersrc) {
         char name[128];
         snprintf(name, sizeof(name), "buffersrc_%s_%d", in->name, in->pad_idx);
diff --git a/lib/frameFilter.h b/lib/frameFilter.h
index 4376fb6..f06197c 100644
--- a/lib/frameFilter.h
+++ b/lib/frameFilter.h
@@ -138,14 +138,14 @@ private:
     /**
      * @brief Filter graph pointer.
      */
-    AVFilterGraph* graph_ = nullptr;
+    AVFilterGraph* graph_{};
 
     /**
      * @brief Filter graph output.
      *
      * Corresponds to a buffersink/abuffersink filter.
      */
-    AVFilterContext* output_ = nullptr;
+    AVFilterContext* output_{};
 
     /**
      * @brief List of filter graph inputs.
-- 
GitLab