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, bool isUpdate); 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<std::string> &eventIds, std::vector<Event> &events, 51 DataShareResultSetPtr &resultSet, const std::set<std::string>& columns); 52 int ResultSetToMultiReminders(std::vector<Event> &events, DataShareResultSetPtr &resultSet); 53 int ResultSetToMultiAttendees(std::vector<Event> &events, 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 void GetEventAttendeesValue(std::vector<Event> &events, const std::map<int, std::vector<Attendee>> &attendeesMap); 60 61 bool ColorParse(const std::string& colorStr, variant<string, int64_t>& colorValue); 62 63 std::string GetUTCTime(const int64_t &timeValue); 64 std::string GetUTCTimes(const std::vector<int64_t> &timeValues); 65 std::string GetRule(const Event &event); 66 std::time_t TimeToUTC(const std::string &strTime); 67 std::string EventIdsToString(const std::vector<int> &ids); 68 template<typename T> GetValue(DataShareResultSetPtr & resultSet,string_view fieldName,T & out)69 int GetValue(DataShareResultSetPtr &resultSet, string_view fieldName, T& out) 70 { 71 int index = 0; 72 auto fieldNameStr = string(fieldName); 73 auto ret = resultSet->GetColumnIndex(fieldNameStr, index); 74 if (ret != DataShare::E_OK) { 75 return ret; 76 } 77 return GetIndexValue(resultSet, index, out); 78 } 79 80 template<typename T> GetValueOptional(DataShareResultSetPtr & resultSet,string_view fieldName,std::optional<T> & out)81 int GetValueOptional(DataShareResultSetPtr &resultSet, string_view fieldName, std::optional<T>& out) 82 { 83 out = std::nullopt; 84 int index = 0; 85 auto fieldNameStr = string(fieldName); 86 auto ret = resultSet->GetColumnIndex(string(fieldName), index); 87 if (ret != DataShare::E_OK) { 88 return ret; 89 } 90 T value; 91 ret = GetIndexValue(resultSet, index, value); 92 if (ret != DataShare::E_OK) { 93 return ret; 94 } 95 out = value; 96 return ret; 97 } 98 } 99 // namespace OHOS::Calendar::NativeUtils 100 101 #endif /* CALENDAR_NATIVE_UTILS_H */