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 
16 #include "clipboard_utils.h"
17 
18 #include "edm_log.h"
19 #include "pasteboard_client.h"
20 namespace OHOS {
21 namespace EDM {
HandlePasteboardPolicy(std::map<int32_t,ClipboardPolicy> & data)22 ErrCode ClipboardUtils::HandlePasteboardPolicy(std::map<int32_t, ClipboardPolicy> &data)
23 {
24     EDMLOGI("ClipboardUtils handlePasteboardPolicy.");
25     auto plugin_ = MiscServices::PasteboardClient::GetInstance();
26     std::map<uint32_t, MiscServices::ShareOption> setMap;
27     std::vector<uint32_t> removeVector;
28     for (auto it = data.begin(); it != data.end(); it++) {
29         switch (it->second) {
30             case ClipboardPolicy::DEFAULT:
31                 removeVector.emplace_back(it->first);
32                 break;
33             case ClipboardPolicy::IN_APP:
34                 setMap.insert(std::make_pair(it->first, MiscServices::ShareOption::InApp));
35                 break;
36             case ClipboardPolicy::LOCAL_DEVICE:
37                 setMap.insert(std::make_pair(it->first, MiscServices::ShareOption::LocalDevice));
38                 break;
39             case ClipboardPolicy::CROSS_DEVICE:
40                 setMap.insert(std::make_pair(it->first, MiscServices::ShareOption::CrossDevice));
41                 break;
42             default:
43                 break;
44         }
45     }
46     ErrCode setRet = ERR_OK;
47     ErrCode rmRet = ERR_OK;
48     if (!setMap.empty()) {
49         setRet = plugin_->SetGlobalShareOption(setMap);
50     }
51     if (!removeVector.empty()) {
52         rmRet = plugin_->RemoveGlobalShareOption(removeVector);
53     }
54     if (setRet != ERR_OK || rmRet != ERR_OK) {
55         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
56     }
57     return ERR_OK;
58 }
59 
RemoveAllPasteboardPolicy(std::map<int32_t,ClipboardPolicy> & data)60 ErrCode ClipboardUtils::RemoveAllPasteboardPolicy(std::map<int32_t, ClipboardPolicy> &data)
61 {
62     EDMLOGI("ClipboardUtils removeAllPasteboardPolicy.");
63     auto plugin_ = MiscServices::PasteboardClient::GetInstance();
64     std::vector<uint32_t> removeVector;
65     for (auto it = data.begin(); it != data.end(); it++) {
66         removeVector.emplace_back(it->first);
67     }
68     return plugin_->RemoveGlobalShareOption(removeVector);
69 }
70 } // namespace EDM
71 } // namespace OHOS