1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "calendar_enum_napi.h"
17 #include <map>
18 #include <vector>
19 #include <string_view>
20 #include "calendar_define.h"
21 #include "napi_queue.h"
22
23
24 namespace OHOS::CalendarApi {
25 struct JsEnumInt {
26 std::string_view enumName;
27 int32_t enumInt;
28 };
29
30 struct JsEnumString {
31 std::string_view enumName;
32 std::string_view enumString;
33 };
34
35
36 static const std::vector<struct JsEnumInt> g_eventType = {
37 { "IMPORTANT", EventType::Important },
38 { "NORMAL", EventType::Normal },
39 };
40
41 static const std::vector<struct JsEnumInt> g_recurrenceFrequency = {
42 { "YEARLY", RecurrenceType::YEARLY },
43 { "MONTHLY", RecurrenceType::MONTHLY },
44 { "WEEKLY", RecurrenceType::WEEKLY },
45 { "DAILY", RecurrenceType::DAILY },
46 };
47
48 static const std::vector<struct JsEnumString> g_calendarTypeKey = {
49 { "LOCAL", "local" },
50 { "EMAIL", "email" },
51 { "BIRTHDAY", "birthday" },
52 { "CALDAV", "caldav" },
53 { "SUBSCRIBED", "subscribed" },
54 };
55
56 static const std::vector<struct JsEnumString> g_serviceType = {
57 { "MEETING", "Meeting" },
58 { "WATCHING", "Watching" },
59 { "REPAYMENT", "Repayment" },
60 { "LIVE", "Live" },
61 { "SHOPPING", "Shopping" },
62 { "TRIP", "Trip" },
63 { "CLASS", "Class" },
64 { "SPORTS_EVENTS", "SportsEvents" },
65 { "SPORTS_EXERCISE", "SportsExercise" },
66 };
67
68 static const std::vector<struct JsEnumString> g_attendeeRole = {
69 { "ORGANIZER", "organizer" },
70 { "PARTICIPANT", "participant" },
71 };
72
73 static const std::vector<struct JsEnumInt> g_attendeeStatus = {
74 {"UNKNOWN", AttendeeStatus::UNKNOWN},
75 {"TENTATIVE", AttendeeStatus::TENTATIVE},
76 {"ACCEPTED", AttendeeStatus::ACCEPTED},
77 {"DECLINED", AttendeeStatus::DECLINED},
78 {"UNRESPONSIVE", AttendeeStatus::UNRESPONSIVE},
79 };
80
81 static const std::vector<struct JsEnumInt> g_attendeeType = {
82 {"REQUIRED", AttendeeType::REQUIRED},
83 {"OPTIONAL", AttendeeType::OPTIONAL},
84 {"RESOURCE", AttendeeType::RESOURCE},
85 };
86
87 static const std::map<std::string_view, const std::vector<struct JsEnumInt>&> g_intEnumClassMap = {
88 { "EventType", g_eventType},
89 { "RecurrenceFrequency", g_recurrenceFrequency},
90 {"AttendeeStatus", g_attendeeStatus},
91 {"AttendeeType", g_attendeeType}
92 };
93
94 static const std::map<std::string_view, const std::vector<struct JsEnumString>&> g_stringEnumClassMap = {
95 { "CalendarType", g_calendarTypeKey },
96 { "ServiceType", g_serviceType },
97 { "AttendeeRole", g_attendeeRole },
98 };
99
JsEnumIntInit(napi_env env,napi_value exports)100 napi_value CalendarEnumNapi::JsEnumIntInit(napi_env env, napi_value exports)
101 {
102 for (const auto &enumClass : g_intEnumClassMap) {
103 auto &enumClassName = enumClass.first;
104 auto &enumItemVec = enumClass.second;
105 int32_t vecSize = static_cast<int32_t>(enumItemVec.size());
106 std::vector<napi_value> value;
107 value.resize(vecSize);
108 for (int32_t index = 0; index < vecSize; ++index) {
109 napi_create_int32(env, enumItemVec[index].enumInt, &value[index]);
110 }
111
112 std::vector<napi_property_descriptor> property;
113 property.resize(vecSize);
114 for (int32_t index = 0; index < vecSize; ++index) {
115 property[index] = napi_property_descriptor DECLARE_NAPI_STATIC_PROPERTY(
116 enumItemVec[index].enumName.data(), value[index]);
117 }
118
119 auto constructor = [](napi_env env, napi_callback_info info) {
120 napi_value jsThis = nullptr;
121 napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr);
122 return jsThis;
123 };
124
125 napi_value result = nullptr;
126 napi_status status = napi_define_class(env, enumClassName.data(), NAPI_AUTO_LENGTH, constructor,
127 nullptr, property.size(), property.data(), &result);
128 CHECK_RETURN(status == napi_ok, "Failed to define enum", nullptr);
129
130 status = napi_set_named_property(env, exports, enumClassName.data(), result);
131 CHECK_RETURN(status == napi_ok, "Failed to set result", nullptr);
132 }
133 return exports;
134 }
135
JsEnumStringInit(napi_env env,napi_value exports)136 napi_value CalendarEnumNapi::JsEnumStringInit(napi_env env, napi_value exports)
137 {
138 for (auto it = g_stringEnumClassMap.begin(); it != g_stringEnumClassMap.end(); it++) {
139 auto &enumClassName = it->first;
140 auto &enumItemVec = it->second;
141 int32_t vecSize = static_cast<int32_t>(enumItemVec.size());
142 std::vector<napi_value> value;
143 value.resize(vecSize);
144 for (int32_t index = 0; index < vecSize; ++index) {
145 napi_create_string_utf8(env, enumItemVec[index].enumString.data(), NAPI_AUTO_LENGTH, &value[index]);
146 }
147
148 std::vector<napi_property_descriptor> property;
149 property.resize(vecSize);
150 for (int32_t index = 0; index < vecSize; ++index) {
151 property[index] = napi_property_descriptor DECLARE_NAPI_STATIC_PROPERTY(
152 enumItemVec[index].enumName.data(), value[index]);
153 }
154
155 auto constructor = [](napi_env env, napi_callback_info info) {
156 napi_value jsThis = nullptr;
157 napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr);
158 return jsThis;
159 };
160
161 napi_value result = nullptr;
162 napi_status status = napi_define_class(env, enumClassName.data(), NAPI_AUTO_LENGTH, constructor,
163 nullptr, property.size(), property.data(), &result);
164 CHECK_RETURN(status == napi_ok, "Failed to define enum", nullptr);
165
166 status = napi_set_named_property(env, exports, enumClassName.data(), result);
167 CHECK_RETURN(status == napi_ok, "Failed to set result", nullptr);
168 }
169 return exports;
170 }
171
Init(napi_env env,napi_value exports)172 napi_value CalendarEnumNapi::Init(napi_env env, napi_value exports)
173 {
174 JsEnumIntInit(env, exports);
175 JsEnumStringInit(env, exports);
176 return exports;
177 }
178 } // namespace CalendarApi