Merge "Do Not Use Repository in Build Script" into tizen
[platform/core/api/webapi-plugins.git] / src / alarm / alarm_utils.cc
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "alarm_utils.h"
18
19 #include "common/logger.h"
20
21 namespace extension {
22 namespace alarm {
23 namespace util {
24
25 const char* kSundayShort = "SU";
26 const char* kMondayShort = "MO";
27 const char* kTuesdayShort = "TU";
28 const char* kWednesdayShort = "WE";
29 const char* kThuesdayShort = "TH";
30 const char* kFridayShort = "FR";
31 const char* kSaturdayShort = "SA";
32
33 using namespace common;
34
35 PlatformResult AppControlToService(const picojson::object& obj, app_control_h *app_control) {
36   LoggerD("Entered");
37
38   const auto it_end = obj.end();
39
40   const auto it_operation = obj.find("operation");
41   if (it_operation == it_end || !it_operation->second.is<std::string>()) {
42     return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, "Invalid parameter passed.");
43   }
44
45   app_control_create(app_control);
46
47   int ret = app_control_set_operation(*app_control, it_operation->second.get<std::string>().c_str());
48   if (APP_CONTROL_ERROR_NONE != ret) {
49     return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Error while setting operation.",
50                               ("Failed app_control_set_operation(): %d (%s)", ret, get_error_message(ret)));
51   }
52
53   const auto it_uri = obj.find("uri");
54   if (it_end != it_uri && it_uri->second.is<std::string>()) {
55     ret = app_control_set_uri(*app_control, it_uri->second.get<std::string>().c_str());
56     if (APP_CONTROL_ERROR_NONE != ret) {
57       return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Error while setting uri.",
58                                 ("Failed app_control_set_uri(): %d (%s)", ret, get_error_message(ret)));
59     }
60   }
61
62   const auto it_mime = obj.find("mime");
63   if (it_end != it_mime && it_mime->second.is<std::string>()) {
64     ret = app_control_set_mime(*app_control, it_mime->second.get<std::string>().c_str());
65     if (APP_CONTROL_ERROR_NONE != ret) {
66       return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Error while setting mime.",
67                                 ("Failed app_control_set_mime(): %d (%s)", ret, get_error_message(ret)));
68     }
69   }
70
71   const auto it_category = obj.find("category");
72   if (it_end != it_category && it_category->second.is<std::string>()) {
73     ret = app_control_set_category(*app_control, it_category->second.get<std::string>().c_str());
74     if (APP_CONTROL_ERROR_NONE != ret) {
75       return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Error while setting category.",
76                                 ("Failed app_control_set_category(): %d (%s)", ret, get_error_message(ret)));
77     }
78   }
79
80   const auto it_data = obj.find("data");
81   if (it_end != it_data && it_data->second.is<picojson::array>()) {
82     const picojson::array& data = it_data->second.get<picojson::array>();
83     PlatformResult result = PlatformResult(ErrorCode::NO_ERROR);
84
85     for (auto iter = data.begin(); iter != data.end(); ++iter) {
86       result = AppControlToServiceExtraData(iter->get<picojson::object>(), app_control);
87       if (!result) {
88         LoggerE("Failed AppControlToServiceExtraData()");
89         return result;
90       }
91     }
92   }
93
94   return PlatformResult(ErrorCode::NO_ERROR);
95 }
96
97 PlatformResult AppControlToServiceExtraData(const picojson::object& app_obj,
98                                             app_control_h *app_control) {
99   LoggerD("Entered");
100
101   const auto it_key = app_obj.find("key");
102   const auto it_value = app_obj.find("value");
103   const auto it_end = app_obj.end();
104
105   if (it_key == it_end ||
106       it_value == it_end ||
107       !it_key->second.is<std::string>() ||
108       !it_value->second.is<picojson::array>()) {
109     return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, "Problem with key or value.");
110   }
111
112   const std::string& key = it_key->second.get<std::string>();
113   const picojson::array& values = it_value->second.get<picojson::array>();
114
115   const size_t size = values.size();
116   const char** arr = new const char*[size];
117   size_t i = 0;
118
119   for (auto iter = values.begin(); iter != values.end(); ++iter, ++i) {
120     arr[i] = iter->to_str().c_str();
121   }
122
123   int ret = APP_CONTROL_ERROR_NONE;
124   if (1 == size) {
125     ret = app_control_add_extra_data(*app_control, key.c_str(), arr[0]);
126   } else {
127     ret = app_control_add_extra_data_array(*app_control, key.c_str(), arr, size);
128   }
129   delete[] arr;
130
131   if (APP_CONTROL_ERROR_NONE != ret) {
132     return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Error while setting data.",
133                               ("Error while setting data: %d (%s)", ret, get_error_message(ret)));
134   }
135
136   return PlatformResult(ErrorCode::NO_ERROR);
137 }
138
139 PlatformResult ArrayDaysToMask(const picojson::array &days_of_the_week, int *repeat_value) {
140   LoggerD("Entered");
141   for (auto iter = days_of_the_week.begin(); iter != days_of_the_week.end(); ++iter) {
142     auto day = (*iter).get<std::string>();
143     if (kSundayShort == day) {
144       *repeat_value |= ALARM_WEEK_FLAG_SUNDAY;
145     } else if (kMondayShort == day) {
146       *repeat_value |= ALARM_WEEK_FLAG_MONDAY;
147     } else if (kTuesdayShort == day) {
148       *repeat_value |= ALARM_WEEK_FLAG_TUESDAY;
149     } else if (kWednesdayShort == day) {
150       *repeat_value |= ALARM_WEEK_FLAG_WEDNESDAY;
151     } else if (kThuesdayShort == day) {
152       *repeat_value |= ALARM_WEEK_FLAG_THURSDAY;
153     } else if (kFridayShort == day) {
154       *repeat_value |= ALARM_WEEK_FLAG_FRIDAY;
155     } else if (kSaturdayShort == day) {
156       *repeat_value |= ALARM_WEEK_FLAG_SATURDAY;
157     } else {
158       return LogAndCreateResult(ErrorCode::TYPE_MISMATCH_ERR, "Invalid days of the week value.");
159     }
160   }
161   return PlatformResult(ErrorCode::NO_ERROR);
162 }
163
164 } // util
165 } // alarm
166 } // extension