• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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);
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 
39     std::vector<std::shared_ptr<Calendar>> ResultSetToCalendars(DataShareResultSetPtr &resultSet);
40     int ResultSetToEvents(std::vector<Event> &events,
41         DataShareResultSetPtr &resultSet, const std::vector<std::string>& columns);
42     int ResultSetToAttendees(std::vector<Attendee> &attendees, DataShareResultSetPtr &resultSet);
43     int ResultSetToReminders(std::vector<int> &reminders, DataShareResultSetPtr &resultSet);
44 
45     bool ColorParse(const std::string& colorStr, uint32_t& colorValue);
46 
47     template<typename T>
GetValue(DataShareResultSetPtr & resultSet,string_view fieldName,T & out)48     int GetValue(DataShareResultSetPtr &resultSet, string_view fieldName, T& out)
49     {
50         int index = 0;
51         auto fieldNameStr = string(fieldName);
52         auto ret = resultSet->GetColumnIndex(fieldNameStr, index);
53         if (ret != DataShare::E_OK) {
54             return ret;
55         }
56         return GetIndexValue(resultSet, index, out);
57     }
58 
59     template<typename T>
GetValueOptional(DataShareResultSetPtr & resultSet,string_view fieldName,std::optional<T> & out)60     int GetValueOptional(DataShareResultSetPtr &resultSet, string_view fieldName, std::optional<T>& out)
61     {
62         out = std::nullopt;
63         int index = 0;
64         auto fieldNameStr = string(fieldName);
65         auto ret = resultSet->GetColumnIndex(string(fieldName), index);
66         if (ret != DataShare::E_OK) {
67             return ret;
68         }
69         T value;
70         ret = GetIndexValue(resultSet, index, value);
71         if (ret != DataShare::E_OK) {
72             return ret;
73         }
74         out = value;
75         return ret;
76     }
77 }
78  // namespace OHOS::Calendar::NativeUtils
79 
80 #endif /* CALENDAR_NATIVE_UTILS_H */