Add manager implementation
[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 <aul.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22
23 #include <string>
24
25 #include "notification-ex/ex_util.h"
26
27 #ifdef LOG_TAG
28 #undef LOG_TAG
29 #endif
30
31 #define LOG_TAG "NOTIFICATION_EX"
32 #define MAX_PACKAGE_STR_SIZE 512
33
34 using namespace std;
35 namespace notification {
36 namespace util {
37   GQuark GetQuarkFromString(string str) {
38     return g_quark_from_string(str.c_str());
39   }
40
41   std::string GetQuarkToString(GQuark quark) {
42     return g_quark_to_string(quark);
43   }
44
45   string GetAppId() {
46     static string appid = "";
47     char appid_buf[MAX_PACKAGE_STR_SIZE] = {0, };
48
49     if (!appid.empty()) {
50       LOGI("appid(%s)", appid.c_str());
51       return appid;
52     }
53
54     int pid = getpid();
55     int ret = aul_app_get_appid_bypid(pid, appid_buf, sizeof(appid_buf));
56     if (ret == AUL_R_OK) {
57       appid = string(appid_buf);
58     } else {
59       int fd, i;
60       int last_slash_index = 0;
61       char proc_buf[MAX_PACKAGE_STR_SIZE] = { 0, };
62
63       snprintf(proc_buf, sizeof(proc_buf), "/proc/%d/cmdline", pid);
64
65       fd = open(proc_buf, O_RDONLY);
66       if (fd < 0) {
67         LOGE("Fail to get appid (%d)", errno);
68         return "";
69       }
70
71       ret = read(fd, appid_buf, sizeof(appid_buf) - 1);
72       if (ret <= 0) {
73         LOGE("Fail to get appid (%d)", errno);
74         close(fd);
75         return "";
76       }
77       close(fd);
78
79       for (i = 0 ; i < ret ; i++) {
80         if (appid_buf[i] == '/')
81           last_slash_index = i;
82       }
83
84       if (last_slash_index == (ret - 1)) {
85         LOGE("Fail to get appid (%s)", appid_buf);
86         return "";
87       }
88
89       if (last_slash_index == 0)
90         appid = string(appid_buf);
91       else
92         appid = string(&appid_buf[last_slash_index + 1]);
93     }
94
95     LOGI("appid(%s)", appid.c_str());
96     return appid;
97   }
98 }  // namespace util
99 }  // namespace watchface_complication