1
2/*
3 * Copyright (c) 2024 Huawei Device Co., Ltd.
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 "unified_data_ffi.h"
18#include "unified_data_impl.h"
19#include "unified_record_impl.h"
20#include "cj_common_ffi.h"
21#include <cstdlib>
22#include <string>
23#include <variant>
24#include <vector>
25#include <map>
26#include <iomanip>
27
28#include "ffi_remote_data.h"
29
30#include "application_defined_record_napi.h"
31#include "audio_napi.h"
32#include "file_napi.h"
33#include "folder_napi.h"
34#include "html_napi.h"
35#include "image_napi.h"
36#include "link_napi.h"
37#include "napi_data_utils.h"
38#include "napi_error_utils.h"
39#include "plain_text_napi.h"
40#include "system_defined_appitem_napi.h"
41#include "system_defined_form_napi.h"
42#include "system_defined_pixelmap_napi.h"
43#include "system_defined_record_napi.h"
44#include "text_napi.h"
45#include "unified_data.h"
46#include "unified_record_napi.h"
47#include "video_napi.h"
48
49#include "utils.h"
50
51using namespace OHOS::FFI;
52using namespace OHOS::UDMF;
53
54namespace OHOS {
55namespace UDMF {
56
57    CUnifiedData::CUnifiedData()
58    {
59        unifiedData_ = std::make_shared<UnifiedData>();
60    }
61
62    CUnifiedData::CUnifiedData(UDMF::CUnifiedRecord *record)
63    {
64        unifiedData_ = std::make_shared<UnifiedData>();
65
66        if (record == nullptr) {
67            return;
68        }
69        unifiedData_->AddRecord(record->GetUnifiedRecord());
70        this->records_.push_back(record);
71    }
72
73    void CUnifiedData::AddRecord(UDMF::CUnifiedRecord *record)
74    {
75        if (record == nullptr) {
76            return;
77        }
78        this->records_.push_back(record);
79        unifiedData_->AddRecord(record->GetUnifiedRecord());
80    }
81
82    static CArrUnifiedRecord VectorToArray(std::vector<int64_t> vector)
83    {
84        if (vector.size() == 0) {
85            return CArrUnifiedRecord{};
86        }
87        int64_t *head = static_cast<int64_t *>(malloc(vector.size() * sizeof(int64_t)));
88        if (head == nullptr) {
89            return CArrUnifiedRecord{};
90        }
91        for (unsigned long i = 0; i < vector.size(); i++) {
92            head[i] = vector[i];
93        }
94        CArrUnifiedRecord int64Array = {head, vector.size()};
95        return int64Array;
96    }
97
98    CArrUnifiedRecord CUnifiedData::GetRecords()
99    {
100        std::vector<int64_t> recordIds;
101        for (auto record : this->records_) {
102            if (record == nullptr) {
103                break;
104            }
105            recordIds.push_back(record->GetID());
106        }
107        return VectorToArray(recordIds);
108    }
109
110    bool CUnifiedData::HasType(const char *type)
111    {
112        return unifiedData_->HasType(type);
113    }
114
115    static CArrString StringVectorToArray(std::vector<std::string> vector)
116    {
117        if (vector.size() == 0) {
118            return CArrString{};
119        }
120        char **head = static_cast<char **>(malloc(vector.size() * sizeof(char *)));
121        if (head == nullptr) {
122            return CArrString{};
123        }
124        for (unsigned long i = 0; i < vector.size(); i++) {
125            head[i] = Utils::MallocCString(vector[i]);
126        }
127        CArrString stringArray = {head, vector.size()};
128        return stringArray;
129    }
130
131    CArrString CUnifiedData::GetTypes()
132    {
133        std::vector<std::string> types = unifiedData_->GetTypesLabels();
134        return StringVectorToArray(types);
135    }
136
137}
138}
139