Modify AppCoreUiThreadBase class
[platform/core/appfw/app-core.git] / tizen-cpp / app-core-ui-cpp / window_position_private.cc
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
3  *
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 "app-core-ui-cpp/window_position_private.hh"
18
19 #include <aul.h>
20 #include <bundle_cpp.h>
21 #include <unistd.h>
22
23 #include <fstream>
24 #include <sstream>
25 #include <string>
26
27 #include "common/log_private.hh"
28
29 namespace tizen_cpp {
30 namespace {
31
32 constexpr const char PATH_AUL[] = "/run/aul/apps/";
33 constexpr uid_t REGULAR_UID_MIN = 5000;
34
35 std::string WindowPositionToRaw(
36     const std::unique_ptr<WindowPosition>& win_pos) {
37   tizen_base::Bundle b {
38     { AUL_K_HINT_SCREEN_POS_X, std::to_string(win_pos->GetPositionX()) },
39     { AUL_K_HINT_SCREEN_POS_Y, std::to_string(win_pos->GetPositionY()) },
40     { AUL_K_HINT_SCREEN_WIDTH, std::to_string(win_pos->GetScreenWidth()) },
41     { AUL_K_HINT_SCREEN_HEIGHT, std::to_string(win_pos->GetScreenHeight()) },
42   };
43
44   return std::string(reinterpret_cast<char*>(b.ToRaw().first.get()));
45 }
46
47 std::unique_ptr<WindowPosition> WindowPositionFromBundle(
48     const tizen_base::Bundle& b) {
49   auto x_str = b.GetString(AUL_K_HINT_SCREEN_POS_X);
50   if (x_str.empty())
51     return nullptr;
52
53   auto y_str = b.GetString(AUL_K_HINT_SCREEN_POS_Y);
54   if (y_str.empty())
55     return nullptr;
56
57   auto w_str = b.GetString(AUL_K_HINT_SCREEN_WIDTH);
58   if (w_str.empty())
59     return nullptr;
60
61   auto h_str = b.GetString(AUL_K_HINT_SCREEN_HEIGHT);
62   if (h_str.empty())
63     return nullptr;
64
65   return std::make_unique<WindowPosition>(std::stoi(x_str),
66       std::stoi(y_str), std::stoi(w_str), std::stoi(h_str));
67 }
68
69 std::unique_ptr<WindowPosition> WindowPositionFromRaw(
70     const std::string& raw) {
71   tizen_base::Bundle b(raw);
72   return WindowPositionFromBundle(b);
73 }
74
75 std::string GetAulAppPath() {
76   static std::string path;
77
78   if (!path.empty() || getuid() < REGULAR_UID_MIN)
79     return path;
80
81   char appid[256] = { 0, };
82   int ret = aul_app_get_appid_bypid(getpid(), appid, sizeof(appid));
83   if (ret != AUL_R_OK) {
84     _E("aul_app_get_appid_bypid() is failed. error(%d)", ret);
85     return {};
86   }
87
88   path = PATH_AUL + std::to_string(getuid()) + "/." + std::string(appid);
89   return path;
90 }
91
92 }  // namespace
93
94 WindowPosition::WindowPosition(int x, int y, int w, int h)
95     : x_(x), y_(y), w_(w), h_(h) {
96 }
97
98 int WindowPosition::GetPositionX() const {
99   return x_;
100 }
101
102 int WindowPosition::GetPositionY() const {
103   return y_;
104 }
105
106 int WindowPosition::GetScreenWidth() const {
107   return w_;
108 }
109
110 int WindowPosition::GetScreenHeight() const {
111   return h_;
112 }
113
114 void WindowPositionManager::WriteToFile(
115     const std::unique_ptr<WindowPosition>& win_pos) {
116   std::string raw = WindowPositionToRaw(win_pos);
117   std::ofstream stream(GetAulAppPath());
118   stream << raw;
119   stream.close();
120 }
121
122 std::unique_ptr<WindowPosition> WindowPositionManager::ReadFromFile() {
123   std::ifstream stream(GetAulAppPath());
124   if (!stream.good()) {
125     _W("There is no files");
126     stream.close();
127     return nullptr;
128   }
129
130   std::string raw;
131   stream >> raw;
132   stream.close();
133   if (raw.empty())
134     return nullptr;
135
136   return WindowPositionFromRaw(raw);
137 }
138
139 std::unique_ptr<WindowPosition> WindowPositionManager::ReadFromBundle(
140     const tizen_base::Bundle& b) {
141   return WindowPositionFromBundle(b);
142 }
143
144 }  // namespace tizen_cpp