2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "callhistory/callhistory_instance.h"
19 #include "common/logger.h"
20 #include "common/picojson.h"
21 #include "common/platform_exception.h"
22 #include "common/tools.h"
25 namespace callhistory {
28 // The privileges that required in CallHistory API
29 const std::string kPrivilegeCallHistoryRead = "http://tizen.org/privilege/callhistory.read";
30 const std::string kPrivilegeCallHistoryWrite = "http://tizen.org/privilege/callhistory.write";
33 using namespace common;
35 CallHistoryInstance::CallHistoryInstance() : history_(*this) {
37 using std::placeholders::_1;
38 using std::placeholders::_2;
40 #define REGISTER_METHOD(M) \
41 RegisterSyncHandler(#M, std::bind(&CallHistoryInstance::M, this, _1, _2))
43 REGISTER_METHOD(CallHistoryRemove);
44 REGISTER_METHOD(CallHistoryAddChangeListener);
45 REGISTER_METHOD(CallHistoryRemoveChangeListener);
46 REGISTER_METHOD(CallHistorySetMissedDirection);
48 REGISTER_METHOD(CallHistoryFind);
49 REGISTER_METHOD(CallHistoryRemoveBatch);
50 REGISTER_METHOD(CallHistoryRemoveAll);
52 #undef REGISTER_METHOD
55 CallHistoryInstance::~CallHistoryInstance() {
59 void CallHistoryInstance::CallHistoryFind(const picojson::value& args, picojson::object& out) {
61 CHECK_PRIVILEGE_ACCESS(kPrivilegeCallHistoryRead, &out);
62 history_.find(args.get<picojson::object>());
66 void CallHistoryInstance::CallHistoryRemove(const picojson::value& args, picojson::object& out) {
68 CHECK_PRIVILEGE_ACCESS(kPrivilegeCallHistoryWrite, &out);
69 PlatformResult result = history_.remove(args.get<picojson::object>());
70 if (result.IsSuccess()) {
73 LogAndReportError(result, &out);
77 void CallHistoryInstance::CallHistoryRemoveBatch(const picojson::value& args, picojson::object& out) {
79 CHECK_PRIVILEGE_ACCESS(kPrivilegeCallHistoryWrite, &out);
80 PlatformResult result = history_.removeBatch(args.get<picojson::object>());
81 if (result.IsSuccess()) {
84 LogAndReportError(result, &out);
88 void CallHistoryInstance::CallHistoryRemoveAll(const picojson::value& args, picojson::object& out) {
90 CHECK_PRIVILEGE_ACCESS(kPrivilegeCallHistoryWrite, &out);
91 history_.removeAll(args.get<picojson::object>());
95 void CallHistoryInstance::CallHistoryAddChangeListener(const picojson::value& args, picojson::object& out) {
97 CHECK_PRIVILEGE_ACCESS(kPrivilegeCallHistoryRead, &out);
98 PlatformResult result = history_.startCallHistoryChangeListener();
99 if (result.IsSuccess()) {
102 LogAndReportError(result, &out);
106 void CallHistoryInstance::CallHistoryRemoveChangeListener(const picojson::value& args, picojson::object& out) {
108 CHECK_PRIVILEGE_ACCESS(kPrivilegeCallHistoryRead, &out);
109 PlatformResult result = history_.stopCallHistoryChangeListener();
110 if (result.IsSuccess()) {
113 LogAndReportError(result, &out);
117 void CallHistoryInstance::CallHistorySetMissedDirection(const picojson::value& args, picojson::object& out) {
120 if (!args.contains("uid")) {
121 LoggerD("args doesn't contain attribute 'uid'");
126 int uid = std::atoi(args.get("uid").get<std::string>().c_str());
128 PlatformResult result = history_.setMissedDirection(uid);
129 if (result.IsSuccess()) {
136 void CallHistoryInstance::CallHistoryChange(picojson::object& data) {
138 picojson::value event = picojson::value(data);
139 picojson::object& obj = event.get<picojson::object>();
140 obj["listenerId"] = picojson::value("CallHistoryChangeCallback");
142 Instance::PostMessage(this, event.serialize().c_str());
145 } // namespace callhistory
146 } // namespace extension