From aa53653fa2a8066d58f2a20a1b677a6317bc98e5 Mon Sep 17 00:00:00 2001 From: "jh9216.park" Date: Wed, 9 Jun 2021 20:24:42 -0400 Subject: [PATCH] Add internal api for c++ Change-Id: I38de1aa87f4b12d597062f2a40bd481dc730961e Signed-off-by: jh9216.park --- CMakeLists.txt | 2 +- include/app_control.hpp | 296 ++++++++++++++++++++++++++ packaging/capi-appfw-app-control.spec | 1 + 3 files changed, 298 insertions(+), 1 deletion(-) create mode 100644 include/app_control.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 2751b1b..d192907 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,7 +48,7 @@ INSTALL(FILES ${CMAKE_SOURCE_DIR}/${TARGET_APP_CONTROL}.pc DESTINATION INSTALL(DIRECTORY ${CMAKE_SOURCE_DIR}/include/ DESTINATION include/appfw FILES_MATCHING PATTERN "*_private.h" EXCLUDE - PATTERN "include/*.h" + PATTERN "include/*.h*" ) ADD_SUBDIRECTORY(src) diff --git a/include/app_control.hpp b/include/app_control.hpp new file mode 100644 index 0000000..8212331 --- /dev/null +++ b/include/app_control.hpp @@ -0,0 +1,296 @@ +/* + * Copyright (c) 2021 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef APP_CONTROL_HPP_ +#define APP_CONTROL_HPP_ + +#include + +#include +#include +#include +#include + +namespace tizen_appfw { + +class AppControl { + public: + enum class Result { + APP_STARTED = 1, + SUCCEEDED = 0, + FAILED = -1, + CANCELED = -2, + }; + + class IEventHandler { + public: + virtual ~IEventHandler() = default; + virtual void OnReceive(AppControl request, AppControl reply, + Result result) = 0; + }; + + enum class LaunchMode { + SINGLE = 0, + GROUP + }; + + AppControl(bool app_started_result_event = false) { + app_control_create(&handle_); + if (app_started_result_event) + app_control_enable_app_started_result_event(handle_); + } + + AppControl(app_control_h h, bool own = false) { + own_ = own; + handle_ = h; + } + + virtual ~AppControl() { + if (handle_ && own_) + app_control_destroy(handle_); + } + + AppControl(const AppControl& c) { + app_control_clone(&handle_, c.handle_); + own_ = true; + } + + AppControl& operator = (const AppControl& c) { + if (this != &c) { + if (handle_ && own_) + app_control_destroy(handle_); + app_control_clone(&handle_, c.handle_); + own_ = true; + } + return *this; + } + + AppControl(AppControl&& c) noexcept { + handle_ = c.handle_; + c.handle_ = nullptr; + own_ = c.own_; + } + + AppControl& operator = (AppControl&& c) noexcept { + if (this != &c) { + if (handle_ && own_) + app_control_destroy(handle_); + handle_ = c.handle_; + c.handle_ = nullptr; + own_ = c.own_; + } + return *this; + } + + app_control_h GetHandle() const { + return handle_; + } + + app_control_h Detach() { + auto h = handle_; + handle_ = nullptr; + return h; + } + + int SetOperation(const std::string& op) { + return app_control_set_operation(handle_, op.c_str()); + } + + std::string GetOperation() const { + char* op = nullptr; + int ret = app_control_get_operation(handle_, &op); + if (ret != 0) + return ""; + auto p = std::unique_ptr(op, std::free); + + return p.get(); + } + + int SetUri(const std::string& uri) { + return app_control_set_uri(handle_, uri.c_str()); + } + + std::string GetUri() const { + char* uri = nullptr; + int ret = app_control_get_uri(handle_, &uri); + if (ret != 0) + return ""; + auto p = std::unique_ptr(uri, std::free); + + return p.get(); + } + + int SetMime(const std::string& mime) { + return app_control_set_mime(handle_, mime.c_str()); + } + + std::string GetMime() const { + char* mime = nullptr; + int ret = app_control_get_mime(handle_, &mime); + if (ret != 0) + return ""; + auto p = std::unique_ptr(mime, std::free); + + return p.get(); + } + + int SetAppId(const std::string& appid) { + return app_control_set_app_id(handle_, appid.c_str()); + } + + std::string GetAppId() const { + char* appid = nullptr; + int ret = app_control_get_app_id(handle_, &appid); + if (ret != 0) + return ""; + auto p = std::unique_ptr(appid, std::free); + + return p.get(); + } + + int AddExtraData(const std::string& key, const std::string& val) { + return app_control_add_extra_data(handle_, key.c_str(), val.c_str()); + } + + int AddExtraData(const std::string& key, std::vector vals) { + std::vector v; + for (auto& i : vals) { + v.push_back(i.c_str()); + } + + return app_control_add_extra_data_array(handle_, key.c_str(), + v.data(), v.size()); + } + + int RemoveExtraData(const std::string& key) { + return app_control_remove_extra_data(handle_, key.c_str()); + } + + std::string GetExtraData(const std::string& key) const { + char* val = nullptr; + int ret = app_control_get_extra_data(handle_, key.c_str(), &val); + if (ret != 0) + return ""; + auto p = std::unique_ptr(val, std::free); + + return p.get(); + } + + std::vector GetExtraDataArray(const std::string& key) const { + char** val = nullptr; + int len; + int ret = app_control_get_extra_data_array(handle_, key.c_str(), &val, &len); + if (ret != 0) + return {}; + + std::vector v; + for (int i = 0; i < len; i++) { + v.push_back(val[i]); + std::free(val[i]); + } + std::free(val); + + return v; + } + + bool IsExtraDataArray(const std::string& key) const { + bool array = false; + app_control_is_extra_data_array(handle_, key.c_str(), &array); + + return array; + } + + int SendLaunchRequest(IEventHandler* ev = nullptr) { + if (!ev) + return app_control_send_launch_request(handle_, nullptr, nullptr); + + return app_control_send_launch_request(handle_, [](app_control_h request, + app_control_h reply, app_control_result_e result, void* user_data) { + IEventHandler* ev = static_cast(user_data); + ev->OnReceive(AppControl(request), AppControl(reply), + static_cast(result)); + }, ev); + } + + int SetLaunchMode(LaunchMode m) { + return app_control_set_launch_mode(handle_, + static_cast(m)); + } + + LaunchMode GetLaunchMode() const { + app_control_launch_mode_e m; + int r = app_control_get_launch_mode(handle_, &m); + if (r != 0) + return LaunchMode::SINGLE; + + return static_cast(m); + } + + int SendTerminateRequest() { + return app_control_send_terminate_request(handle_); + } + + std::string GetCaller() const { + char* id = nullptr; + + app_control_get_caller(handle_, &id); + if (!id) + return ""; + std::string ret_id = id; + std::free(id); + return ret_id; + } + + std::vector GetExtraDataKeys() const { + std::vector v; + app_control_foreach_extra_data(handle_, [](app_control_h app_control, + const char* key, void* user_data) -> bool { + std::vector* v = + static_cast*>(user_data); + v->push_back(key); + return true; + }, &v); + + return v; + } + + private: + app_control_h handle_; + bool own_ = true; +}; + +class ReceivedAppControl : public AppControl { + public: + ReceivedAppControl(app_control_h h, bool own = false) + : AppControl(h, own) {} + ReceivedAppControl(bool app_started_result_event = false) + : AppControl(app_started_result_event) {} + + int ReplyToLaunchRequest(const AppControl& reply, Result result) { + return app_control_reply_to_launch_request(reply.GetHandle(), + GetHandle(), static_cast(result)); + } + + bool IsReplyRequested() const { + bool req = false; + app_control_is_reply_requested(GetHandle(), &req); + return req; + } +}; + +} // namespace tizen_appfw + +#endif // APP_CONTROL_HPP_ \ No newline at end of file diff --git a/packaging/capi-appfw-app-control.spec b/packaging/capi-appfw-app-control.spec index e57c758..977d21c 100644 --- a/packaging/capi-appfw-app-control.spec +++ b/packaging/capi-appfw-app-control.spec @@ -111,6 +111,7 @@ install -m 0644 gcov-obj/* %{buildroot}%{_datadir}/gcov/obj %files devel %manifest %{name}.manifest %{_includedir}/appfw/app_control.h +%{_includedir}/appfw/app_control.hpp %{_includedir}/appfw/app_control_internal.h %{_includedir}/appfw/app_control_uri.h %{_libdir}/pkgconfig/capi-appfw-app-control.pc -- 2.34.1