1bc03f14fSopenharmony_ci/*
2bc03f14fSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3bc03f14fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4bc03f14fSopenharmony_ci * you may not use this file except in compliance with the License.
5bc03f14fSopenharmony_ci * You may obtain a copy of the License at
6bc03f14fSopenharmony_ci *
7bc03f14fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8bc03f14fSopenharmony_ci *
9bc03f14fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10bc03f14fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11bc03f14fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12bc03f14fSopenharmony_ci * See the License for the specific language governing permissions and
13bc03f14fSopenharmony_ci * limitations under the License.
14bc03f14fSopenharmony_ci */
15bc03f14fSopenharmony_ci
16bc03f14fSopenharmony_ci#include "pasteboard_client.h"
17bc03f14fSopenharmony_ci#include "pasteboard_error.h"
18bc03f14fSopenharmony_ci#include "system_pasteboard_impl.h"
19bc03f14fSopenharmony_ci
20bc03f14fSopenharmony_ciusing namespace OHOS::MiscServices;
21bc03f14fSopenharmony_ci
22bc03f14fSopenharmony_cinamespace OHOS {
23bc03f14fSopenharmony_cinamespace MiscServicesCj {
24bc03f14fSopenharmony_ci
25bc03f14fSopenharmony_cistatic sptr<SystemPasteboardImpl> g_systemPasteboard_instance = nullptr;
26bc03f14fSopenharmony_ci
27bc03f14fSopenharmony_ciOHOS::FFI::RuntimeType *SystemPasteboardImpl::GetClassType()
28bc03f14fSopenharmony_ci{
29bc03f14fSopenharmony_ci    static OHOS::FFI::RuntimeType runtimeType = OHOS::FFI::RuntimeType::Create<OHOS::FFI::FFIData>("SystemPasteboardIm"
30bc03f14fSopenharmony_ci                                                                                                   "pl");
31bc03f14fSopenharmony_ci    return &runtimeType;
32bc03f14fSopenharmony_ci}
33bc03f14fSopenharmony_ci
34bc03f14fSopenharmony_ciSystemPasteboardImpl::SystemPasteboardImpl()
35bc03f14fSopenharmony_ci{
36bc03f14fSopenharmony_ci    value_ = nullptr;
37bc03f14fSopenharmony_ci    pasteData_ = nullptr;
38bc03f14fSopenharmony_ci}
39bc03f14fSopenharmony_ci
40bc03f14fSopenharmony_ciint NewInstance(sptr<SystemPasteboardImpl> &instance)
41bc03f14fSopenharmony_ci{
42bc03f14fSopenharmony_ci    if (g_systemPasteboard_instance != nullptr) {
43bc03f14fSopenharmony_ci        instance = g_systemPasteboard_instance;
44bc03f14fSopenharmony_ci        return 0;
45bc03f14fSopenharmony_ci    }
46bc03f14fSopenharmony_ci    g_systemPasteboard_instance = FFI::FFIData::Create<SystemPasteboardImpl>();
47bc03f14fSopenharmony_ci    instance = g_systemPasteboard_instance;
48bc03f14fSopenharmony_ci    return 0;
49bc03f14fSopenharmony_ci}
50bc03f14fSopenharmony_ci
51bc03f14fSopenharmony_ciint32_t SystemPasteboardImpl::GetSystemPasteboardImpl(int64_t &id)
52bc03f14fSopenharmony_ci{
53bc03f14fSopenharmony_ci    sptr<SystemPasteboardImpl> instence = nullptr;
54bc03f14fSopenharmony_ci    int status = NewInstance(instence);
55bc03f14fSopenharmony_ci    if (status != 0) {
56bc03f14fSopenharmony_ci        LOGE("[SystemPasteboardImpl] CJgetSystemPasteboard create instance failed");
57bc03f14fSopenharmony_ci        return status;
58bc03f14fSopenharmony_ci    }
59bc03f14fSopenharmony_ci    id = instence->GetID();
60bc03f14fSopenharmony_ci    return 0;
61bc03f14fSopenharmony_ci}
62bc03f14fSopenharmony_ci
63bc03f14fSopenharmony_ciint32_t SystemPasteboardImpl::SetData(sptr<PasteDataImpl> dataImpl, std::shared_ptr<MiscServices::PasteData> data)
64bc03f14fSopenharmony_ci{
65bc03f14fSopenharmony_ci    if (data == nullptr) {
66bc03f14fSopenharmony_ci        LOGE("[SystemPasteboardImpl] SetData data nullptr");
67bc03f14fSopenharmony_ci        return PASTEBOARD_INVALID_PARAMETERS;
68bc03f14fSopenharmony_ci    }
69bc03f14fSopenharmony_ci    int32_t ret = PasteboardClient::GetInstance()->SetPasteData(*data);
70bc03f14fSopenharmony_ci    int32_t res = PASTEBOARD_SUCCESS;
71bc03f14fSopenharmony_ci    if (ret == static_cast<int>(PasteboardError::E_OK)) {
72bc03f14fSopenharmony_ci        value_ = dataImpl;
73bc03f14fSopenharmony_ci        pasteData_ = data;
74bc03f14fSopenharmony_ci        LOGI("[SystemPasteboardImpl] SetData OK");
75bc03f14fSopenharmony_ci    } else if (ret == static_cast<int>(PasteboardError::PROHIBIT_COPY)) {
76bc03f14fSopenharmony_ci        res = PASTEBOARD_COPY_FORBIDDEN;
77bc03f14fSopenharmony_ci        LOGE("[SystemPasteboardImpl] SetData ERR PROHIBIT_COPY");
78bc03f14fSopenharmony_ci    } else if (ret == static_cast<int>(PasteboardError::TASK_PROCESSING)) {
79bc03f14fSopenharmony_ci        LOGE("[SystemPasteboardImpl] SetData ERR TASK_PROCESSING");
80bc03f14fSopenharmony_ci        res = PASTEBOARD_IS_BEGING_PROCESSED;
81bc03f14fSopenharmony_ci    }
82bc03f14fSopenharmony_ci
83bc03f14fSopenharmony_ci    return res;
84bc03f14fSopenharmony_ci}
85bc03f14fSopenharmony_ci
86bc03f14fSopenharmony_cisptr<PasteDataImpl> SystemPasteboardImpl::GetSystemPasteboardPasteDataImpl()
87bc03f14fSopenharmony_ci{
88bc03f14fSopenharmony_ci    if (value_ == nullptr) {
89bc03f14fSopenharmony_ci        return nullptr;
90bc03f14fSopenharmony_ci    }
91bc03f14fSopenharmony_ci
92bc03f14fSopenharmony_ci    return value_;
93bc03f14fSopenharmony_ci}
94bc03f14fSopenharmony_ci
95bc03f14fSopenharmony_ciint32_t SystemPasteboardImpl::GetData(MiscServices::PasteData &pasteData)
96bc03f14fSopenharmony_ci{
97bc03f14fSopenharmony_ci    int32_t ret = PasteboardClient::GetInstance()->GetPasteData(pasteData);
98bc03f14fSopenharmony_ci    return ret;
99bc03f14fSopenharmony_ci}
100bc03f14fSopenharmony_ci
101bc03f14fSopenharmony_cibool SystemPasteboardImpl::HasData()
102bc03f14fSopenharmony_ci{
103bc03f14fSopenharmony_ci    bool res = PasteboardClient::GetInstance()->HasPasteData();
104bc03f14fSopenharmony_ci    return res;
105bc03f14fSopenharmony_ci}
106bc03f14fSopenharmony_ci
107bc03f14fSopenharmony_civoid SystemPasteboardImpl::ClearData()
108bc03f14fSopenharmony_ci{
109bc03f14fSopenharmony_ci    PasteboardClient::GetInstance()->Clear();
110bc03f14fSopenharmony_ci    value_ = nullptr;
111bc03f14fSopenharmony_ci    pasteData_ = nullptr;
112bc03f14fSopenharmony_ci}
113bc03f14fSopenharmony_ci
114bc03f14fSopenharmony_cibool SystemPasteboardImpl::IsRemoteData()
115bc03f14fSopenharmony_ci{
116bc03f14fSopenharmony_ci    bool res = PasteboardClient::GetInstance()->IsRemoteData();
117bc03f14fSopenharmony_ci    return res;
118bc03f14fSopenharmony_ci}
119bc03f14fSopenharmony_ci
120bc03f14fSopenharmony_cibool SystemPasteboardImpl::HasDataType(std::string mimeType)
121bc03f14fSopenharmony_ci{
122bc03f14fSopenharmony_ci    bool res = PasteboardClient::GetInstance()->HasDataType(mimeType);
123bc03f14fSopenharmony_ci    return res;
124bc03f14fSopenharmony_ci}
125bc03f14fSopenharmony_ci
126bc03f14fSopenharmony_cistd::string SystemPasteboardImpl::GetDataSource()
127bc03f14fSopenharmony_ci{
128bc03f14fSopenharmony_ci    std::string res;
129bc03f14fSopenharmony_ci    PasteboardClient::GetInstance()->GetDataSource(res);
130bc03f14fSopenharmony_ci    return res;
131bc03f14fSopenharmony_ci}
132bc03f14fSopenharmony_ci
133bc03f14fSopenharmony_ci} // namespace MiscServicesCj
134bc03f14fSopenharmony_ci}