1 /*
2  * Copyright (c) 2024 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 #include "paste_data_impl.h"
16 
17 using namespace OHOS::MiscServices;
18 
19 namespace OHOS {
20 namespace MiscServicesCj {
21 
GetClassType()22 OHOS::FFI::RuntimeType *PasteDataImpl::GetClassType()
23 {
24     static OHOS::FFI::RuntimeType runtimeType =
25         OHOS::FFI::RuntimeType::Create<OHOS::FFI::FFIData>("PasteDataImpl");
26     return &runtimeType;
27 }
28 
CreateCjPasteDataObject(std::string mimeType,CJValueType value)29 int64_t CreateCjPasteDataObject(std::string mimeType, CJValueType value)
30 {
31     auto pasteDataImpl = FFI::FFIData::Create<PasteDataImpl>(mimeType, value);
32     return pasteDataImpl->GetID();
33 }
34 
PasteDataImpl()35 PasteDataImpl::PasteDataImpl()
36 {
37     value_ = std::make_shared<PasteData>();
38 }
39 
PasteDataImpl(std::shared_ptr<MiscServices::PasteData> pasteData)40 PasteDataImpl::PasteDataImpl(std::shared_ptr<MiscServices::PasteData> pasteData)
41 {
42     value_ = pasteData;
43 }
44 
PasteDataImpl(std::string mimeType,CJValueType value)45 PasteDataImpl::PasteDataImpl(std::string mimeType, CJValueType value)
46 {
47     if (mimeType == "text/html") {
48         CreateHtmlData(mimeType, value);
49     } else if (mimeType == "text/plain") {
50         CreatePlainTextData(mimeType, value);
51     } else if (mimeType == "text/uri") {
52         CreateUriData(mimeType, value);
53     } else if (mimeType == "pixelMap") {
54         CreatePixelMapData(mimeType, value);
55     } else if (mimeType == "text/want") {
56         CreateWantData(mimeType, value);
57     } else {
58         std::vector<uint8_t> arrayBuf(reinterpret_cast<uint8_t *>(value.arrayBufferData),
59             reinterpret_cast<uint8_t *>(value.arrayBufferData) + value.arrayBufferSize);
60         value_ = PasteboardClient::GetInstance()->CreateKvData(mimeType, arrayBuf);
61     }
62 }
63 
GetRealPasteData()64 std::shared_ptr<MiscServices::PasteData> PasteDataImpl::GetRealPasteData()
65 {
66     return value_;
67 }
68 
CreateHtmlData(std::string mimeType,CJValueType value)69 void PasteDataImpl::CreateHtmlData(std::string mimeType, CJValueType value)
70 {
71     value_ = PasteboardClient::GetInstance()->CreateHtmlData(value.stringValue);
72 }
73 
CreatePlainTextData(std::string mimeType,CJValueType value)74 void PasteDataImpl::CreatePlainTextData(std::string mimeType, CJValueType value)
75 {
76     value_ = PasteboardClient::GetInstance()->CreatePlainTextData(value.stringValue);
77 }
78 
CreateUriData(std::string mimeType,CJValueType value)79 void PasteDataImpl::CreateUriData(std::string mimeType, CJValueType value)
80 {
81     value_ = PasteboardClient::GetInstance()->CreateUriData(OHOS::Uri(value.stringValue));
82 }
83 
CreatePixelMapData(std::string mimeType,CJValueType value)84 void PasteDataImpl::CreatePixelMapData(std::string mimeType, CJValueType value)
85 {
86     value_ = PasteboardClient::GetInstance()->CreatePixelMapData(value.pixelMap);
87 }
88 
CreateWantData(std::string mimeType,CJValueType value)89 void PasteDataImpl::CreateWantData(std::string mimeType, CJValueType value)
90 {
91 }
92 
93 }
94 }