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 #ifndef CALENDAR_NATIVE_UTILS_H 17 #define CALENDAR_NATIVE_UTILS_H 18 19 #include <string> 20 #include <string_view> 21 #include <vector> 22 #include <map> 23 24 #include "calendar_define.h" 25 #include "datashare_result_set.h" 26 #include "datashare_values_bucket.h" 27 #include "native_calendar.h" 28 29 namespace OHOS::CalendarApi::Native { 30 using DataShareResultSetPtr = std::shared_ptr<DataShare::DataShareResultSet>; 31 void DumpCalendarAccount(const CalendarAccount &account); 32 void DumpEvent(const Event &event); 33 DataShare::DataShareValuesBucket BuildValueEvent(const Event &event, int calendarId, int channelId); 34 DataShare::DataShareValuesBucket BuildAttendeeValue(const Attendee &attendee, int eventId); 35 int GetIndexValue(const DataShareResultSetPtr &resultSet, int index, int& out); 36 int GetIndexValue(const DataShareResultSetPtr &resultSet, int index, std::string& out); 37 int GetIndexValue(const DataShareResultSetPtr &resultSet, int index, int64_t& out); 38 void SetRRuleValue(const std::map<std::string, std::string> &ruleMap, RecurrenceRule &out); 39 void SetByDayOfRRule(const std::vector<std::string> &weekDayList, RecurrenceRule &out); 40 std::string GetDaysOfWeekRule(int minValue, int maxValue, const std::vector<int64_t> &daysOfWeekList); 41 std::string GetDaysOfWeekMonthRule( 42 const std::vector<int64_t> &daysOfWeekList, const std::vector<int64_t> &weeksOfMonthList); 43 std::string GetRRuleSerial(int minValue, int maxValue, const std::vector<int64_t> &serialList); 44 std::string GetWeeklyRule(const Event &event, const std::tm &time); 45 std::string GetMonthlyRule(const Event &event, const std::tm &time); 46 std::string GetYearlyRule(const Event &event, const std::tm &time); 47 std::string GetEventRRule(const Event &event); 48 49 std::vector<std::shared_ptr<Calendar>> ResultSetToCalendars(DataShareResultSetPtr &resultSet); 50 int ResultSetToEvents(std::vector<Event> &events, 51 DataShareResultSetPtr &resultSet, const std::set<std::string>& columns); 52 int ResultSetToAttendees(std::vector<Attendee> &attendees, DataShareResultSetPtr &resultSet); 53 int ResultSetToReminders(std::vector<int> &reminders, DataShareResultSetPtr &resultSet); 54 void ResultSetToAttendeeStatus(Attendee &attendee, DataShareResultSetPtr &resultSet); 55 void ResultSetToAttendeeType(Attendee &attendee, DataShareResultSetPtr &resultSet); 56 void ResultSetToConfig(CalendarConfig &config, DataShareResultSetPtr &resultSet); 57 void SetField(const std::vector<string>& eventKey, 58 std::vector<string>& queryField, std::set<string>& resultSetField); 59 60 bool ColorParse(const std::string& colorStr, variant<string, int64_t>& colorValue); 61 62 std::string GetUTCTime(const int64_t &timeValue); 63 std::string GetUTCTimes(const std::vector<int64_t> &timeValues); 64 std::string GetRule(const Event &event); 65 std::time_t TimeToUTC(const std::string &strTime); 66 std::string EventIdsToString(const std::vector<int> &ids); 67 template<typename T> GetValue(DataShareResultSetPtr & resultSet,string_view fieldName,T & out)68 int GetValue(DataShareResultSetPtr &resultSet, string_view fieldName, T& out) 69 { 70 int index = 0; 71 auto fieldNameStr = string(fieldName); 72 auto ret = resultSet->GetColumnIndex(fieldNameStr, index); 73 if (ret != DataShare::E_OK) { 74 return ret; 75 } 76 return GetIndexValue(resultSet, index, out); 77 } 78 79 template<typename T> GetValueOptional(DataShareResultSetPtr & resultSet,string_view fieldName,std::optional<T> & out)80 int GetValueOptional(DataShareResultSetPtr &resultSet, string_view fieldName, std::optional<T>& out) 81 { 82 out = std::nullopt; 83 int index = 0; 84 auto fieldNameStr = string(fieldName); 85 auto ret = resultSet->GetColumnIndex(string(fieldName), index); 86 if (ret != DataShare::E_OK) { 87 return ret; 88 } 89 T value; 90 ret = GetIndexValue(resultSet, index, value); 91 if (ret != DataShare::E_OK) { 92 return ret; 93 } 94 out = value; 95 return ret; 96 } 97 } 98 // namespace OHOS::Calendar::NativeUtils 99 100 #endif /* CALENDAR_NATIVE_UTILS_H */