minor fix
[platform/core/security/suspicious-activity-monitor.git] / device-agent / daemon / settingshandler.cpp
1 /**
2  * Samsung Ukraine R&D Center (SRK under a contract between)
3  * LLC "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
4  * Copyright (C) 2017 Samsung Electronics Co., Ltd. All rights reserved.
5  */
6 /**
7  * @file   settingshandler.h
8  * @brief  Class for handling change settings events
9  * @date   Created Mar 6, 2018
10  * @author Mail to: <A HREF="mailto:d.lomtev@samsung.com">Dmytro Lomtiev, d.lomtev@samsung.com</A>
11  */
12
13 #include <jsoncpp/json/reader.h>
14 #include "settingshandler.h"
15 #include "settings.h"
16 #include "logging.h"
17 #include "samonitor_tag.h"
18
19
20 namespace
21 {
22 const std::string TIMEOUT_KEY{"timeout"};
23 const std::string LOCK_KEY{"lock"};
24 }
25
26 const std::string SettingsHandler::SETTINGS_EVENT_TYPE{"settings"};
27
28 SettingsHandler::SettingsHandler(NetworkManager::Connection& conn): NetworkManager::EventListener(conn, SETTINGS_EVENT_TYPE)
29 {
30 }
31
32 void SettingsHandler::accept(NetworkManager::Event& event)
33 {
34     try {
35         Json::Reader reader;
36         Json::Value root;
37
38         if (reader.parse(event.getContent(), root)) {
39
40             bool modified = false;
41
42             if (root.isMember(TIMEOUT_KEY)) {
43                 int timeout = root[TIMEOUT_KEY].asInt();
44
45                 if (timeout > 0) {
46                     LOG_D(TAG, "SettingsHandler: keepalive timeout set to %d", timeout);
47                     NetworkManager::Settings::instance().setKeepAliveTimeout(std::chrono::milliseconds(timeout));
48                     modified = true;
49                 } else {
50                     LOG_E(TAG, "SettingsHandler: wrong timeout value %d", timeout);
51                 }
52             }
53
54             if (root.isMember(LOCK_KEY)) {
55                 bool lock = root[LOCK_KEY].asBool();
56                 LOG_D(TAG, "SettingsHandler: lock set to %d", int(lock));
57                 NetworkManager::Settings::instance().setLock(lock);
58                 modified = true;
59             }
60
61             if (modified) {
62                 NetworkManager::Settings::instance().save();
63             }
64
65             event.confirm();
66         }
67     } catch (std::exception& e) {
68         LOG_E(TAG, "SettingsHandler: %s", e.what());
69     }
70 }