Fix systemd and DBus configuaration, fix bus name
[platform/core/appfw/tizen-theme-manager.git] / src / theme_plugin / theme_parser.cc
1 // Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by an apache 2.0 license that can be
3 // found in the LICENSE file.
4
5 #include "theme_plugin/theme_parser.h"
6
7 #include <json/json.h>
8 #include <sys/types.h>
9
10 #include <fstream>
11 #include <streambuf>
12 #include <string>
13
14 #include "theme/dbus/request_broker.h"
15 #include "theme/loader/theme_info.h"
16 #include "theme/utils/logging.h"
17 #include "theme_plugin/theme_info_builder.h"
18
19 namespace ttm {
20 namespace plugin {
21
22 loader::ThemeInfo ThemeParser::Inflate(const std::string id,
23     const std::string pkgid, uid_t uid) {
24   Json::CharReaderBuilder rbuilder;
25   rbuilder["collectComments"] = false;
26
27   std::ifstream ifs(path_);
28   Json::Value root;
29   std::string error;
30   if (!Json::parseFromStream(rbuilder, ifs, &root, &error)) {
31     LOG(ERROR) << "Failed to read json file: " << error;
32     ifs.close();
33     return {};
34   }
35
36   // FIXME: this parser should parse more theme information.
37   ThemeInfoBuilder builder("testid");
38   builder.PutString("pkgid", pkgid).
39       PutString("uid", std::to_string(uid)).
40       PutString("version", root["version"].asString()).
41       PutString("tool_version", root["tool_version"].asString()).
42       PutString("title", root["header"]["title"].asString()).
43       PutString("description", root["header"]["description"].asString());
44
45   return builder.Build();
46 }
47
48 using ttm::dbus::Command;
49 using ttm::dbus::RequestBroker;
50 int ThemeParser::Commit(ThemeOperation operation,
51   const loader::ThemeInfo& theme) {
52   tizen_base::Bundle theme_info = theme.Serialize();
53   tizen_base::Bundle reply;
54   switch (operation) {
55     case ThemeOperation::ADD:
56       LOG(DEBUG) << "Add Theme: " << theme.GetId();
57       reply = RequestBroker::GetInst().SendData(Command::ADD, theme_info);
58       break;
59     case ThemeOperation::UPDATE:
60       LOG(DEBUG) << "Update Theme: " << theme.GetId();
61       reply = RequestBroker::GetInst().SendData(Command::UPDATE, theme_info);
62       break;
63     case ThemeOperation::REMOVE:
64       LOG(DEBUG) << "Remove Theme: " << theme.GetId();
65       reply = RequestBroker::GetInst().SendData(Command::REMOVE, theme_info);
66       break;
67     default:
68       return -1;
69   }
70   return 0;
71 }
72
73 }  // namespace plugin
74 }  // namespace ttm