#include "theme_plugin/theme_info_builder.h"
+#include <algorithm>
+#include <string>
+
+#include "theme/utils/logging.h"
+
+namespace {
+
+const char kThemeFilesPrefix[] = "theme/files";
+
+bool StartsWith(const std::string& str, const std::string& substr) {
+ return str.length() >= substr.length() &&
+ std::equal(substr.begin(), substr.end(), str.begin());
+}
+
+} // namespace
+
namespace ttm {
namespace plugin {
return loader::ThemeInfo(bundle_);
}
+void ThemeInfoBuilder::GenerateFileKey() {
+ std::vector<tizen_base::Bundle::KeyInfo> keyinfos = bundle_.GetKeys();
+ for (const auto& keyinfo : keyinfos) {
+ std::string key = keyinfo.GetName();
+ if (!StartsWith(key, kThemeFilesPrefix))
+ continue;
+
+ size_t dot_pos = key.find_last_of(".");
+ if (dot_pos == std::string::npos)
+ continue;
+
+ std::string newkey = key.substr(0, dot_pos);
+ LOG(DEBUG) << "Adding generated key: " << newkey;
+ bundle_.Add(newkey, bundle_.GetString(key));
+ }
+}
+
} // namespace plugin
} // namespace ttm
ThemeInfoBuilder& PutStringArray(const std::string& key,
const std::vector<std::string>& value);
loader::ThemeInfo Build();
+ void GenerateFileKey();
private:
tizen_base::Bundle bundle_;
namespace {
const char kAutosetKey[] = "http://tizen.org/metadata/theme/auto_set";
+const char kAutoFileKeyKey[] = "http://tizen.org/metadata/theme/auto_file_key";
const char kDefaultThemeKey[] = "http://tizen.org/metadata/theme/default";
const char kThemeKey[] = "http://tizen.org/metadata/theme";
}
bool ParseAndCommitTheme(ThemeParser::ThemeOperation op, const std::string& id,
- const std::string& pkgid, const std::string& path, bool is_default) {
+ const std::string& pkgid, const std::string& path, bool is_default,
+ bool auto_file_key_gen) {
uid_t uid;
int r = pkgmgr_installer_info_get_target_uid(&uid);
if (r < 0) {
return false;
}
- ThemeParser parser(path);
+ ThemeParser parser(path, auto_file_key_gen);
ThemeInfo theme = parser.Inflate(id, pkgid, uid, is_default);
if (!theme.IsValid()) {
LOG(ERROR) << "Failed to parse theme";
return -1;
bool is_default = false;
+ bool auto_file_key_gen = false;
std::string json_file;
for (__metadata_t* md : GListRange<__metadata_t*>(metadata)) {
// Assume that there is a only json file for theme
json_file = root_path + md->value;
} else if (!strcmp(md->key, kDefaultThemeKey)) {
is_default = true;
+ } else if (!strcmp(md->key, kAutoFileKeyKey)) {
+ auto_file_key_gen = true;
} else if (!strcmp(md->key, kAutosetKey)) {
if (IsPlatformPrivilege()) {
LOG(INFO) << "Found auto_set attribute. "
}
if (!ParseAndCommitTheme(ThemeParser::ThemeOperation::ADD, appid, pkgid,
- json_file, is_default))
+ json_file, is_default, auto_file_key_gen))
return -1;
return 0;
}
if (root_path.empty())
return -1;
+ bool auto_file_key_gen = false;
std::string json_file;
for (__metadata_t* md : GListRange<__metadata_t*>(metadata)) {
// Assume that there is a only json file for theme
if (!strcmp(md->key, kThemeKey))
json_file = root_path + md->value;
+ else if (!strcmp(md->key, kAutoFileKeyKey))
+ auto_file_key_gen = true;
}
// Upgrading default theme is not allowed
if (!ParseAndCommitTheme(ThemeParser::ThemeOperation::UPDATE, appid, pkgid,
- json_file, false))
+ json_file, false, auto_file_key_gen))
return -1;
return 0;
}
// Removing default theme is not allowed
if (!ParseAndCommitTheme(ThemeParser::ThemeOperation::REMOVE, appid, pkgid,
- json_file, false))
+ json_file, false, false))
return -1;
return 0;
}
return {};
}
+ if (auto_file_key_gen_)
+ builder.GenerateFileKey();
+
return builder.Build();
}
REMOVE
};
- explicit ThemeParser(const std::string& path) : path_(path) {}
+ explicit ThemeParser(const std::string& path, bool auto_file_key_gen)
+ : path_(path), auto_file_key_gen_(auto_file_key_gen) {}
loader::ThemeInfo Inflate(const std::string id, const std::string pkgid,
uid_t uid, bool is_default);
bool Commit(ThemeOperation operation, const loader::ThemeInfo& theme);
std::string parent_key, ThemeInfoBuilder* builder);
std::string path_;
+ bool auto_file_key_gen_;
};
} // namespace plugin
},
"keyboard": {
"keypad_bg_color": "#050a28"
+ },
+ "files": {
+ "shared/res/image1.png": "shared/res/image1.png",
+ "shared/res/image2.svg": "shared/res/image2.svg"
}
}
}
};
TEST_F(ThemeParserTest, Inflate) {
- ttm::plugin::ThemeParser parser("test_samples/test_theme.json");
- auto info = parser.Inflate("testid", "testpkgid", 5001, false);
+ ttm::plugin::ThemeParser parser("test_samples/test_theme.json", true);
+ auto info = parser.Inflate("testid", "testpkgid", 5001, true);
EXPECT_EQ(info.GetId(), "testid");
EXPECT_EQ(info.GetPkgid(), "testpkgid");
EXPECT_EQ(info.GetUid(), 5001);
"https://www.download.gogowatch/get");
EXPECT_EQ(info.GetStringArray("theme/preview"),
std::vector<std::string>({"GOGO_Preview.png"}));
+ EXPECT_EQ(info.GetString("theme/files/shared/res/image1.png"),
+ "shared/res/image1.png");
+ EXPECT_EQ(info.GetString("theme/files/shared/res/image1"),
+ "shared/res/image1.png");
+ EXPECT_EQ(info.GetString("theme/files/shared/res/image2.svg"),
+ "shared/res/image2.svg");
+ EXPECT_EQ(info.GetString("theme/files/shared/res/image2"),
+ "shared/res/image2.svg");
}
TEST_F(ThemeParserTest, MultidimensionalArray) {
ttm::plugin::ThemeParser parser(
- "test_samples/test_multidimensional_array.json");
+ "test_samples/test_multidimensional_array.json", false);
auto info = parser.Inflate("testid", "testpkgid", 5001, false);
std::vector<std::string> array_value({"1", "2", "3"});
EXPECT_EQ(info.GetStringArray("1D_array"), array_value);
TEST_F(ThemeParserTest, InvalidJson) {
ttm::plugin::ThemeParser parser(
- "test_samples/test_invalid_json.json");
+ "test_samples/test_invalid_json.json", false);
auto info = parser.Inflate("testid", "testpkgid", 5001, false);
EXPECT_FALSE(info.IsValid());
}
TEST_F(ThemeParserTest, DifferentArrayElementType) {
ttm::plugin::ThemeParser parser(
- "test_samples/test_different_array_element_type.json");
+ "test_samples/test_different_array_element_type.json", false);
auto info = parser.Inflate("testid", "testpkgid", 5001, false);
EXPECT_FALSE(info.IsValid());
}
TEST_F(ThemeParserTest, EmptyArray) {
ttm::plugin::ThemeParser parser(
- "test_samples/test_empty_array.json");
+ "test_samples/test_empty_array.json", false);
auto info = parser.Inflate("testid", "testpkgid", 5001, false);
EXPECT_FALSE(info.IsValid());
}