Fix to apply whitespace coding rules
[platform/core/security/krate.git] / server / package-proxy.cpp
1 /*
2  *  Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
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 #include <sys/stat.h>
17 #include <sys/types.h>
18
19 #include <system_settings.h>
20 #include <klay/auth/user.h>
21 #include <klay/audit/logger.h>
22
23 #include "packman.h"
24
25 #include "rmi/package-proxy.h"
26
27 namespace Krate {
28
29 PackageProxy::PackageProxy(KrateControlContext& ctx) :
30         context(ctx)
31 {
32         context.registerParametricMethod(this, "", (PackageProxy::PackageInfo)(PackageProxy::getPackageInfo)(std::string, std::string));
33         context.registerParametricMethod(this, "", (std::vector<std::string>)(PackageProxy::getPackageList)(std::string));
34
35         context.registerParametricMethod(this, "", (int)(PackageProxy::install)(std::string, std::string));
36         context.registerParametricMethod(this, "", (int)(PackageProxy::uninstall)(std::string, std::string));
37 }
38
39 PackageProxy::~PackageProxy()
40 {
41 }
42
43 PackageProxy::PackageInfo PackageProxy::getPackageInfo(const std::string& name, const std::string& pkgid)
44 {
45         PackageProxy::PackageInfo package;
46         char* locale = NULL;
47
48         system_settings_get_value_string(SYSTEM_SETTINGS_KEY_LOCALE_LANGUAGE, &locale);
49         if (locale == NULL) {
50                 locale = strdup("No locale");
51         }
52
53         package.krate = name;
54         package.id = pkgid;
55         package.locale = locale;
56
57         free(locale);
58
59         try {
60                 runtime::User user(name);
61                 ::PackageInfo pkginfo(pkgid, user.getUid());
62
63                 package.type = pkginfo.getType();
64                 package.icon = pkginfo.getIcon();
65                 package.label = pkginfo.getLabel();
66                 package.description = pkginfo.getDescription();
67
68                 package.author.name = pkginfo.getAuthorName();
69                 package.author.email = pkginfo.getAuthorEmail();
70                 package.author.href = pkginfo.getAuthorHref();
71
72                 package.version = pkginfo.getVersion();
73                 package.apiVersion = pkginfo.getApiVersion();
74                 package.mainAppId = pkginfo.getMainAppId();
75
76                 package.isSystem = pkginfo.isSystem();
77                 package.isRemovable = pkginfo.isRemovable();
78                 package.isPreload = pkginfo.isPreload();
79         } catch (runtime::Exception& e) {
80                 ERROR("Failed to retrieve package info installed in the krate");
81         }
82
83         return package;
84 }
85
86 std::vector<std::string> PackageProxy::getPackageList(const std::string& name)
87 {
88         try {
89                 runtime::User user(name);
90                 PackageManager& packman = PackageManager::instance();
91                 return packman.getPackageList(user.getUid());
92         } catch (runtime::Exception& e) {
93                 ERROR("Failed to retrieve package info installed in the krate");
94         }
95         return std::vector<std::string>();
96 }
97
98
99 int PackageProxy::install(const std::string& name, const std::string& pkgpath)
100 {
101         try {
102                 runtime::User user(name);
103                 PackageManager& packman = PackageManager::instance();
104                 packman.installPackage(pkgpath, user.getUid());
105         } catch (runtime::Exception& e) {
106                 ERROR("Failed to install package in the krate");
107                 return -1;
108         }
109
110         return 0;
111 }
112
113 int PackageProxy::uninstall(const std::string& name, const std::string& pkgid)
114 {
115         try {
116                 runtime::User user(name);
117                 PackageManager& packman = PackageManager::instance();
118                 packman.uninstallPackage(pkgid, user.getUid());
119         } catch (runtime::Exception& e) {
120                 ERROR("Failed to uninstall package of pkgid in the krate");
121                 return -1;
122         }
123         return 0;
124 }
125
126 } // namespace Krate