Fix group name for noti_ex
[platform/core/api/notification.git] / notification-ex / ex_util.cc
1 /*
2  * Copyright (c) 2018 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 <dlog.h>
18 #include <aul.h>
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include <package_info.h>
22 #include <package_manager.h>
23
24 #include <string>
25 #include <memory>
26
27 #include "notification-ex/ex_util.h"
28
29 #ifdef LOG_TAG
30 #undef LOG_TAG
31 #endif
32
33 #define LOG_TAG "NOTIFICATION_EX"
34 #define MAX_PACKAGE_STR_SIZE 512
35
36 using namespace std;
37 namespace notification {
38 namespace util {
39 GQuark GetQuarkFromString(string str) {
40   return g_quark_from_string(str.c_str());
41 }
42
43 std::string GetQuarkToString(GQuark quark) {
44   return g_quark_to_string(quark);
45 }
46
47 int GetRequestId() {
48   static int id = 0;
49   g_atomic_int_inc(&id);
50   return id;
51 }
52
53 string GetAppId() {
54   static string appid = "";
55   char appid_buf[MAX_PACKAGE_STR_SIZE] = {0, };
56
57   if (!appid.empty()) {
58     LOGI("appid(%s)", appid.c_str());
59     return appid;
60   }
61
62   int pid = getpid();
63   int ret = aul_app_get_appid_bypid(pid, appid_buf, sizeof(appid_buf));
64   if (ret == AUL_R_OK) {
65     appid = string(appid_buf);
66   } else {
67     int fd, i;
68     int last_slash_index = 0;
69     char proc_buf[MAX_PACKAGE_STR_SIZE] = { 0, };
70
71     snprintf(proc_buf, sizeof(proc_buf), "/proc/%d/cmdline", pid);
72
73     fd = open(proc_buf, O_RDONLY);
74     if (fd < 0) {
75       LOGE("Fail to get appid (%d)", errno);
76       return "";
77     }
78
79     ret = read(fd, appid_buf, sizeof(appid_buf) - 1);
80     if (ret <= 0) {
81       LOGE("Fail to get appid (%d)", errno);
82       close(fd);
83       return "";
84     }
85     close(fd);
86
87     for (i = 0 ; i < ret ; i++) {
88       if (appid_buf[i] == '/')
89         last_slash_index = i;
90     }
91
92     if (last_slash_index == (ret - 1)) {
93       LOGE("Fail to get appid (%s)", appid_buf);
94       return "";
95     }
96
97     if (last_slash_index == 0)
98       appid = string(appid_buf);
99     else
100       appid = string(&appid_buf[last_slash_index + 1]);
101   }
102
103   LOGI("appid(%s)", appid.c_str());
104   return appid;
105 }
106
107 string GetPkgId() {
108   static string pkgid = "";
109   if (!pkgid.empty()) {
110     LOGI("pkgid(%s)", pkgid.c_str());
111     return pkgid;
112   }
113
114   char pkgid_buf[MAX_PACKAGE_STR_SIZE] = {0, };
115   int ret = aul_app_get_pkgid_bypid(getpid(), pkgid_buf, sizeof(pkgid_buf));
116   if (ret == AUL_R_OK)
117     pkgid = string(pkgid_buf);
118
119   return pkgid;
120 }
121
122 string GetDomainName() {
123   static string domain_str = "";
124   if (!domain_str.empty()) {
125     LOGI("domain(%s)", domain_str.c_str());
126     return domain_str;
127   }
128
129   string pkgid = GetPkgId();
130   if (pkgid.empty())
131     return "";
132
133   string::size_type pos = pkgid.rfind('.');
134   if (pos != string::npos)
135     ++pos;
136   else
137     return "";
138
139   domain_str = pkgid.substr(pos);
140   return domain_str;
141 }
142
143 string GetLocaleDirectory() {
144   string pkgid = GetPkgId();
145   if (pkgid.empty())
146     return "";
147
148   static string locale_directory = "";
149   if (!locale_directory.empty()) {
150     LOGI("locale_directory(%s)", locale_directory.c_str());
151     return locale_directory;
152   }
153
154   package_info_h package_info;
155   int err = package_info_create(pkgid.c_str(), &package_info);
156   if (err != PACKAGE_MANAGER_ERROR_NONE) {
157     LOGE("fail to get package info");
158     return "";
159   }
160   unique_ptr<package_info_s, decltype(package_info_destroy)*> pkg_ptr(
161       package_info, package_info_destroy);
162
163   char* app_root_path;
164   err = package_info_get_root_path(pkg_ptr.get(), &app_root_path);
165   if (err != PACKAGE_MANAGER_ERROR_NONE) {
166     LOGE("Failed to get root path err[%d] path[%p]",
167         err, app_root_path);
168     return "";
169   }
170
171   string app_root_str(app_root_path);
172   free(app_root_path);
173
174   locale_directory = app_root_str + string("/res/locale");
175   return locale_directory;
176 }
177
178 }  // namespace util
179 }  // namespace notification