Hide some members from popup class 51/107951/2
authorKyungwook Tak <k.tak@samsung.com>
Mon, 2 Jan 2017 04:45:43 +0000 (13:45 +0900)
committerKyungwook Tak <k.tak@samsung.com>
Mon, 2 Jan 2017 04:54:47 +0000 (13:54 +0900)
hypertext object, button object and some related setters can be hided as
private in popup class. It's provided as setButtons function with button
parameter which contains text and return type. Related resource(types,
hypertext string) are managed internally in popup class.

Change-Id: I3264db77b4c29ae409bb7d928473bf7a74930fac
Signed-off-by: Kyungwook Tak <k.tak@samsung.com>
src/framework/ui/popup/logic.cpp
src/framework/ui/popup/popup.cpp
src/framework/ui/popup/popup.h

index 7cb5999..5b1a135 100644 (file)
@@ -57,7 +57,7 @@ void split(const std::string &s, std::string &fileName, std::string &extraPath)
        extraPath = s.substr(0, idx);
 }
 
-std::string formatToString(const char *form, std::string arg)
+std::string formatToString(const char *form, const std::string &arg)
 {
        std::vector<char> buf(PATH_MAX, '\0');
        snprintf(buf.data(), buf.size(), form, arg.c_str());
@@ -70,26 +70,18 @@ void Logic::csPromptData(const std::string &message, const CsDetected &d)
        std::string risk(d.severity ==
                CSR_CS_SEVERITY_LOW ? LABEL_RISK_LEVEL_LOW : LABEL_RISK_LEVEL_MEDIUM);
 
-       m_popup.setButtons(2);
-
+       m_popup.reset();
        m_popup.setMessage(message);
        m_popup.setTitle(CS_TITLE);
        m_popup.setHeader(CS_PROMPT_DATA_HEADER);
        m_popup.setBody(FORMAT(
                "- " << LABEL_RISK << risk << " (" << d.malwareName << ")"));
        m_popup.setFooter(CS_PROMPT_DATA_FOOTER);
+       m_popup.setDetailedUrl(d.detailedUrl);
 
-       m_popup.setText(m_popup.m_buttons[0], BTN_CANCEL);
-       m_popup.setText(m_popup.m_buttons[1], BTN_OPEN);
-
-       m_popup.m_types.emplace_back(
-               static_cast<int>(CSR_CS_USER_RESPONSE_PROCESSING_DISALLOWED));
-       m_popup.m_types.emplace_back(
-               static_cast<int>(CSR_CS_USER_RESPONSE_PROCESSING_ALLOWED));
-
-       m_popup.callbackRegister(m_popup.m_buttons[0], &m_popup.m_types[0]);
-       m_popup.callbackRegister(m_popup.m_buttons[1], &m_popup.m_types[1]);
-       m_popup.callbackRegister(m_popup.m_hypertext, d.detailedUrl);
+       m_popup.setButtons({
+               Popup::Button(BTN_CANCEL, CSR_CS_USER_RESPONSE_PROCESSING_DISALLOWED),
+               Popup::Button(BTN_OPEN, CSR_CS_USER_RESPONSE_PROCESSING_ALLOWED)});
 }
 
 void Logic::csPromptFile(const std::string &message, const CsDetected &d)
@@ -99,8 +91,7 @@ void Logic::csPromptFile(const std::string &message, const CsDetected &d)
        std::string fileName, extraPath;
        split(d.targetName, fileName, extraPath);
 
-       m_popup.setButtons(3);
-
+       m_popup.reset();
        m_popup.setMessage(message);
        m_popup.setTitle(CS_TITLE);
        m_popup.setHeader(CS_PROMPT_FILE_HEADER);
@@ -110,22 +101,12 @@ void Logic::csPromptFile(const std::string &message, const CsDetected &d)
                "- " << LABEL_RISK << risk << " (" << d.malwareName << ")"));
        std::string prefix(CS_NOTIFY_FILE_FOOTER);
        m_popup.setFooter(FORMAT(prefix << "<br>" << CS_PROMPT_FILE_FOOTER));
+       m_popup.setDetailedUrl(d.detailedUrl);
 
-       m_popup.setText(m_popup.m_buttons[0], BTN_CANCEL);
-       m_popup.setText(m_popup.m_buttons[1], BTN_OPEN);
-       m_popup.setText(m_popup.m_buttons[2], BTN_DELETE);
-
-       m_popup.m_types.emplace_back(
-               static_cast<int>(CSR_CS_USER_RESPONSE_PROCESSING_DISALLOWED));
-       m_popup.m_types.emplace_back(
-               static_cast<int>(CSR_CS_USER_RESPONSE_PROCESSING_ALLOWED));
-       m_popup.m_types.emplace_back(
-               static_cast<int>(CSR_CS_USER_RESPONSE_REMOVE));
-
-       m_popup.callbackRegister(m_popup.m_buttons[0], &m_popup.m_types[0]);
-       m_popup.callbackRegister(m_popup.m_buttons[1], &m_popup.m_types[1]);
-       m_popup.callbackRegister(m_popup.m_buttons[2], &m_popup.m_types[2]);
-       m_popup.callbackRegister(m_popup.m_hypertext, d.detailedUrl);
+       m_popup.setButtons({
+               Popup::Button(BTN_CANCEL, CSR_CS_USER_RESPONSE_PROCESSING_DISALLOWED),
+               Popup::Button(BTN_OPEN, CSR_CS_USER_RESPONSE_PROCESSING_ALLOWED),
+               Popup::Button(BTN_DELETE, CSR_CS_USER_RESPONSE_REMOVE)});
 }
 
 void Logic::csPromptApp(const std::string &message, const CsDetected &d)
@@ -134,8 +115,7 @@ void Logic::csPromptApp(const std::string &message, const CsDetected &d)
                CSR_CS_SEVERITY_LOW ? LABEL_RISK_LEVEL_LOW : LABEL_RISK_LEVEL_MEDIUM);
        PackageInfo info(d.pkgId);
 
-       m_popup.setButtons(3);
-
+       m_popup.reset();
        m_popup.setMessage(message);
        m_popup.setTitle(CS_TITLE);
        m_popup.setHeader(CS_PROMPT_APP_HEADER);
@@ -146,28 +126,17 @@ void Logic::csPromptApp(const std::string &message, const CsDetected &d)
        m_popup.setIcon(info.getIconPath());
        std::string prefix = formatToString(CS_NOTIFY_APP_FOOTER, BTN_UNINSTALL);
        m_popup.setFooter(FORMAT(prefix << "<br>" << CS_PROMPT_APP_FOOTER));
+       m_popup.setDetailedUrl(d.detailedUrl);
 
-       m_popup.setText(m_popup.m_buttons[0], BTN_CANCEL);
-       m_popup.setText(m_popup.m_buttons[1], BTN_OPEN);
-       m_popup.setText(m_popup.m_buttons[2], BTN_UNINSTALL);
-
-       m_popup.m_types.emplace_back(
-               static_cast<int>(CSR_CS_USER_RESPONSE_PROCESSING_DISALLOWED));
-       m_popup.m_types.emplace_back(
-               static_cast<int>(CSR_CS_USER_RESPONSE_PROCESSING_ALLOWED));
-       m_popup.m_types.emplace_back(
-               static_cast<int>(CSR_CS_USER_RESPONSE_REMOVE));
-
-       m_popup.callbackRegister(m_popup.m_buttons[0], &m_popup.m_types[0]);
-       m_popup.callbackRegister(m_popup.m_buttons[1], &m_popup.m_types[1]);
-       m_popup.callbackRegister(m_popup.m_buttons[2], &m_popup.m_types[2]);
-       m_popup.callbackRegister(m_popup.m_hypertext, d.detailedUrl);
+       m_popup.setButtons({
+               Popup::Button(BTN_CANCEL, CSR_CS_USER_RESPONSE_PROCESSING_DISALLOWED),
+               Popup::Button(BTN_OPEN, CSR_CS_USER_RESPONSE_PROCESSING_ALLOWED),
+               Popup::Button(BTN_UNINSTALL, CSR_CS_USER_RESPONSE_REMOVE)});
 }
 
 void Logic::csNotifyData(const std::string &message, const CsDetected &d)
 {
-       m_popup.setButtons(1);
-
+       m_popup.reset();
        m_popup.setMessage(message);
        m_popup.setTitle(CS_TITLE);
        m_popup.setHeader(CS_NOTIFY_DATA_HEADER);
@@ -175,22 +144,18 @@ void Logic::csNotifyData(const std::string &message, const CsDetected &d)
                "- " << LABEL_RISK << LABEL_RISK_LEVEL_HIGH <<
                " (" << d.malwareName << ")"));
        m_popup.setFooter(CS_NOTIFY_DATA_FOOTER);
+       m_popup.setDetailedUrl(d.detailedUrl);
 
-       m_popup.setText(m_popup.m_buttons[0], BTN_OK);
-
-       m_popup.m_types.emplace_back(
-               static_cast<int>(CSR_CS_USER_RESPONSE_PROCESSING_DISALLOWED));
-
-       m_popup.callbackRegister(m_popup.m_buttons[0], &m_popup.m_types[0]);
-       m_popup.callbackRegister(m_popup.m_hypertext, d.detailedUrl);
+       m_popup.setButtons({
+               Popup::Button(BTN_OK, CSR_CS_USER_RESPONSE_PROCESSING_DISALLOWED)});
 }
 
 void Logic::csNotifyFile(const std::string &message, const CsDetected &d)
 {
-       m_popup.setButtons(2);
        std::string fileName, extraPath;
        split(d.targetName, fileName, extraPath);
 
+       m_popup.reset();
        m_popup.setMessage(message);
        m_popup.setTitle(CS_TITLE);
        m_popup.setHeader(CS_NOTIFY_FILE_HEADER);
@@ -200,26 +165,18 @@ void Logic::csNotifyFile(const std::string &message, const CsDetected &d)
                "- " << LABEL_RISK << LABEL_RISK_LEVEL_HIGH <<
                " (" << d.malwareName << ")"));
        m_popup.setFooter(CS_NOTIFY_FILE_FOOTER);
+       m_popup.setDetailedUrl(d.detailedUrl);
 
-       m_popup.setText(m_popup.m_buttons[0], BTN_CANCEL);
-       m_popup.setText(m_popup.m_buttons[1], BTN_DELETE);
-
-       m_popup.m_types.emplace_back(
-               static_cast<int>(CSR_CS_USER_RESPONSE_PROCESSING_DISALLOWED));
-       m_popup.m_types.emplace_back(
-               static_cast<int>(CSR_CS_USER_RESPONSE_REMOVE));
-
-       m_popup.callbackRegister(m_popup.m_buttons[0], &m_popup.m_types[0]);
-       m_popup.callbackRegister(m_popup.m_buttons[1], &m_popup.m_types[1]);
-       m_popup.callbackRegister(m_popup.m_hypertext, d.detailedUrl);
+       m_popup.setButtons({
+               Popup::Button(BTN_CANCEL, CSR_CS_USER_RESPONSE_PROCESSING_DISALLOWED),
+               Popup::Button(BTN_DELETE, CSR_CS_USER_RESPONSE_REMOVE)});
 }
 
 void Logic::csNotifyApp(const std::string &message, const CsDetected &d)
 {
        PackageInfo info(d.pkgId);
 
-       m_popup.setButtons(2);
-
+       m_popup.reset();
        m_popup.setMessage(message);
        m_popup.setTitle(CS_TITLE);
        m_popup.setHeader(CS_NOTIFY_APP_HEADER);
@@ -229,18 +186,11 @@ void Logic::csNotifyApp(const std::string &message, const CsDetected &d)
                LABEL_VERSION << info.getVersion() << "<br>" <<
                LABEL_RISK << LABEL_RISK_LEVEL_HIGH << " (" << d.malwareName << ")"));
        m_popup.setFooter(formatToString(CS_NOTIFY_APP_FOOTER, BTN_UNINSTALL));
+       m_popup.setDetailedUrl(d.detailedUrl);
 
-       m_popup.setText(m_popup.m_buttons[0], BTN_CANCEL);
-       m_popup.setText(m_popup.m_buttons[1], BTN_UNINSTALL);
-
-       m_popup.m_types.emplace_back(
-               static_cast<int>(CSR_CS_USER_RESPONSE_PROCESSING_DISALLOWED));
-       m_popup.m_types.emplace_back(
-               static_cast<int>(CSR_CS_USER_RESPONSE_PROCESSING_ALLOWED));
-
-       m_popup.callbackRegister(m_popup.m_buttons[0], &m_popup.m_types[0]);
-       m_popup.callbackRegister(m_popup.m_buttons[1], &m_popup.m_types[1]);
-       m_popup.callbackRegister(m_popup.m_hypertext, d.detailedUrl);
+       m_popup.setButtons({
+               Popup::Button(BTN_CANCEL, CSR_CS_USER_RESPONSE_PROCESSING_DISALLOWED),
+               Popup::Button(BTN_UNINSTALL, CSR_CS_USER_RESPONSE_REMOVE)});
 }
 
 void Logic::wpPrompt(const std::string &message, const UrlItem &item)
@@ -248,8 +198,7 @@ void Logic::wpPrompt(const std::string &message, const UrlItem &item)
        std::string risk(item.risk ==
                CSR_WP_RISK_LOW ? LABEL_RISK_LEVEL_LOW : LABEL_RISK_LEVEL_MEDIUM);
 
-       m_popup.setButtons(2);
-
+       m_popup.reset();
        m_popup.setMessage(message);
        m_popup.setTitle(WP_TITLE);
        m_popup.setHeader(WP_PROMPT_HEADER);
@@ -257,24 +206,16 @@ void Logic::wpPrompt(const std::string &message, const UrlItem &item)
                "- " << formatToString(LABEL_URL, item.url) << "<br>" <<
                "- " << LABEL_RISK << risk));
        m_popup.setFooter(WP_PROMPT_FOOTER);
+       m_popup.setDetailedUrl(item.url);
 
-       m_popup.setText(m_popup.m_buttons[0], BTN_BLOCK);
-       m_popup.setText(m_popup.m_buttons[1], BTN_VIEW);
-
-       m_popup.m_types.emplace_back(
-               static_cast<int>(CSR_WP_USER_RESPONSE_PROCESSING_DISALLOWED));
-       m_popup.m_types.emplace_back(
-               static_cast<int>(CSR_WP_USER_RESPONSE_PROCESSING_ALLOWED));
-
-       m_popup.callbackRegister(m_popup.m_buttons[0], &m_popup.m_types[0]);
-       m_popup.callbackRegister(m_popup.m_buttons[1], &m_popup.m_types[1]);
-       m_popup.callbackRegister(m_popup.m_hypertext, item.url);
+       m_popup.setButtons({
+               Popup::Button(BTN_BLOCK, CSR_WP_USER_RESPONSE_PROCESSING_DISALLOWED),
+               Popup::Button(BTN_VIEW, CSR_WP_USER_RESPONSE_PROCESSING_ALLOWED)});
 }
 
 void Logic::wpNotify(const std::string &message, const UrlItem &item)
 {
-       m_popup.setButtons(1);
-
+       m_popup.reset();
        m_popup.setMessage(message);
        m_popup.setTitle(WP_TITLE);
        m_popup.setHeader(WP_NOTIFY_HEADER);
@@ -282,13 +223,10 @@ void Logic::wpNotify(const std::string &message, const UrlItem &item)
                "- " << formatToString(LABEL_URL, item.url) << "<br>" <<
                "- " << LABEL_RISK << LABEL_RISK_LEVEL_HIGH));
        m_popup.setFooter(WP_NOTIFY_FOOTER);
+       m_popup.setDetailedUrl(item.url);
 
-       m_popup.setText(m_popup.m_buttons[0], BTN_OK);
-
-       m_popup.m_types.emplace_back(static_cast<int>(CSR_WP_USER_RESPONSE_PROCESSING_DISALLOWED));
-
-       m_popup.callbackRegister(m_popup.m_buttons[0], &m_popup.m_types[0]);
-       m_popup.callbackRegister(m_popup.m_hypertext, item.url);
+       m_popup.setButtons({
+               Popup::Button(BTN_OK, CSR_WP_USER_RESPONSE_PROCESSING_DISALLOWED)});
 }
 } // namespace Ui
 } // namespace Csr
index 844f1fb..7c8d3ec 100644 (file)
@@ -50,9 +50,10 @@ void Popup::setSender(Sender &&sender)
 
 Popup::Popup() : m_win(nullptr)
 {
+       ecore_event_handler_add(ECORE_EVENT_KEY_DOWN, keyDownCb, this);
 }
 
-void Popup::setButtons(int n)
+void Popup::reset()
 {
        clear();
 
@@ -67,7 +68,6 @@ void Popup::setButtons(int n)
        setRotationToWin(m_win);
 
        eext_win_keygrab_set(m_win, HOME_KEY.c_str());
-       ecore_event_handler_add(ECORE_EVENT_KEY_DOWN, keyDownCb, this);
 
        // Set popup properties.
        m_popup = elm_popup_add(m_win);
@@ -116,21 +116,11 @@ void Popup::setButtons(int n)
        elm_box_pack_end(m_box, m_footer);
        evas_object_show(m_footer);
 
-       // Add buttons dynamically.
        m_btnBox = elm_box_add(m_popup);
        setDefaultProperties(m_btnBox);
        elm_box_horizontal_set(m_btnBox, EINA_TRUE);
        elm_box_padding_set(m_btnBox, 0, 0);
 
-       for (int i = 0; i < n; ++i) {
-               Evas_Object *button = elm_button_add(m_popup);
-               elm_object_style_set(button, "bottom");
-               setDefaultProperties(button);
-               elm_box_pack_end(m_btnBox, button);
-               evas_object_show(button);
-               m_buttons.emplace_back(button);
-       }
-
        elm_box_pack_end(m_box, m_btnBox);
        evas_object_show(m_btnBox);
 
@@ -146,6 +136,23 @@ Popup::~Popup()
                evas_object_del(m_win);
 }
 
+void Popup::setButtons(const std::vector<Popup::Button> &buttons)
+{
+       for (auto &button : buttons) {
+               Evas_Object *obj = elm_button_add(m_btnBox);
+
+               elm_object_style_set(obj, "bottom");
+               setDefaultProperties(obj);
+
+               setText(obj, button.text);
+               m_buttonTypeMap[obj] = button.type;
+               evas_object_smart_callback_add(obj, "clicked", btnClickedCb, this);
+
+               elm_box_pack_end(m_btnBox, obj);
+               evas_object_show(obj);
+       }
+}
+
 void Popup::setHeader(const std::string &header) noexcept
 {
        setText(m_header, header);
@@ -215,20 +222,15 @@ void Popup::setText(Evas_Object *obj, const std::string &text) noexcept
        elm_object_text_set(obj, text.c_str());
 }
 
-void Popup::callbackRegister(Evas_Object *obj, int *type)
-{
-       m_buttonSelectorMap[obj] = [type]() { return *type; };
-       evas_object_smart_callback_add(obj, "clicked", btnClickedCb, this);
-}
-
-void Popup::callbackRegister(Evas_Object *obj, const std::string &url)
+void Popup::setDetailedUrl(const std::string &url)
 {
        if (url.empty())
-               evas_object_smart_callback_add(
-                       obj, "anchor,clicked", hypertextClickedCb, &DEFAULT_URL);
+               m_hypertextUrl = DEFAULT_URL;
        else
-               evas_object_smart_callback_add(
-                       obj, "anchor,clicked", hypertextClickedCb, &url);
+               m_hypertextUrl = url;
+
+       evas_object_smart_callback_add(
+               m_hypertext, "anchor,clicked", hypertextClickedCb, this);
 }
 
 void Popup::rotationChangedCb(void *data, Evas_Object *, void *)
@@ -258,8 +260,12 @@ void Popup::rotationChangedCb(void *data, Evas_Object *, void *)
 void Popup::hypertextClickedCb(void *data, Evas_Object *, void *)
 {
        DEBUG("Launch browser for detailed url.");
+       auto popup = reinterpret_cast<Popup *>(data);
+       if (popup == nullptr)
+               throw std::invalid_argument("Popup instance is null in static callback");
+
+       auto &url = popup->m_hypertextUrl;
 
-       std::string url = *(reinterpret_cast<std::string *>(data));
        std::unique_ptr<AppControl> ac(new AppControl);
 
        auto ret = app_control_set_operation(ac->handle, APP_CONTROL_OPERATION_VIEW);
@@ -287,7 +293,7 @@ void Popup::btnClickedCb(void *data, Evas_Object *obj, void *)
        if (popup == nullptr)
                throw std::invalid_argument("Popup instance is null in static btnClickedCb");
 
-       response = popup->m_buttonSelectorMap.at(obj)();
+       response = popup->m_buttonTypeMap.at(obj);
        popup->terminateWindow();
 }
 
@@ -322,8 +328,7 @@ void Popup::clear()
                m_win = nullptr;
        }
 
-       m_buttons.clear();
-       m_types.clear();
+       m_buttonTypeMap.clear();
 }
 
 } // namespace Ui
index 821c7ec..b60bfc7 100644 (file)
@@ -38,65 +38,64 @@ namespace Csr {
 namespace Ui {
 
 /*
- * --------------------
- * |      title       |
- * ========box=========
- * | content(header)  |
- * |------subBox------|
- * |icon|content(body)|
- * |------------------|
- * | -hypertext       |
- * |                  |
- * | content(footer)  |
- * ====================
- * |------btnBox------|
- * |     button(N)    |
- * --------------------
+ * ------------------------
+ * |        title         |
+ * |==========box=========|
+ * ||  content(header)   ||
+ * ||-------subBox-------||
+ * |||icon|content(body)|||
+ * ||--------------------||
+ * ||  -hypertext        ||
+ * ||   content(footer)  ||
+ * ||-------btnBox-------||
+ * |||     button(N)    |||
+ * ||--------------------||
+ * |======================|
  */
 
 class Popup {
 public:
-       Popup();
-       virtual ~Popup();
+       struct Button {
+               Button(const char *text, int type) : text(text), type(type) {}
 
-       void setButtons(int n);
+               const char *text;
+               int type;
+       };
 
-       RawBuffer getResult(void);
-       void setMessage(const std::string &msg) noexcept;
+       Popup();
+       virtual ~Popup();
 
        Popup(const Popup &) = delete;
        Popup &operator=(const Popup &) = delete;
        Popup(Popup &&) = delete;
        Popup &operator=(Popup &&) = delete;
 
+       void reset();
+
        using Sender = std::function<void(const RawBuffer &)>;
        void setSender(Sender &&sender);
 
+       void setMessage(const std::string &msg) noexcept;
        void setTitle(const std::string &title) noexcept;
        void setHeader(const std::string &header) noexcept;
        void setBody(const std::string &body) noexcept;
        void setFooter(const std::string &footer) noexcept;
        void setIcon(const std::string &path) noexcept;
-       void setText(Evas_Object *obj, const std::string &text) noexcept;
-
-       void callbackRegister(Evas_Object *obj, int *type);
-       void callbackRegister(Evas_Object *obj, const std::string &url);
-       static Eina_Bool keyDownCb(void *, int, void *);
-
-       std::vector<Evas_Object *> m_buttons;
-       Evas_Object *m_hypertext;
-       std::vector<int> m_types;
+       void setDetailedUrl(const std::string &url);
+       void setButtons(const std::vector<Button> &buttons);
 
 private:
        static void btnClickedCb(void *data, Evas_Object *, void *);
        static void hypertextClickedCb(void *data, Evas_Object *, void *);
        static void rotationChangedCb(void *data, Evas_Object *, void *);
+       static Eina_Bool keyDownCb(void *, int, void *);
 
-       using ButtonSelector = std::function<int()>;
-       std::map<Evas_Object *, ButtonSelector> m_buttonSelectorMap;
+       std::map<Evas_Object *, int> m_buttonTypeMap;
+       void setText(Evas_Object *obj, const std::string &text) noexcept;
        void terminateWindow();
        void clear();
 
+       RawBuffer getResult(void);
        void setDefaultProperties(Evas_Object *obj) noexcept;
        void setRotationToWin(Evas_Object *obj) noexcept;
 
@@ -109,6 +108,7 @@ private:
        Evas_Object *m_body;
        Evas_Object *m_footer;
        Evas_Object *m_icon;
+       Evas_Object *m_hypertext;
 
        std::string m_message;
        std::string m_iconPath;