#include "query_request_handler.hh"
#include "set_cert_request_handler.hh"
#include "set_pkginfo_request_handler.hh"
+#include "system_locale.hh"
#include "pkgmgrinfo_debug.h"
-
namespace pkgmgr_client {
constexpr const char SOCK_PATH[] = "/run/pkgmgr-info-server";
tizen_base::Parcel p;
p.WriteParcelable(*parcel_.get());
std::vector<uint8_t> raw = p.GetRaw();
- std::unique_ptr<char, decltype(std::free)*> locale(
- _get_system_locale(), std::free);
- if (locale.get() == nullptr)
- return false;
- handler->HandleRequest(&raw[0], raw.size(), locale.get());
+ handler->HandleRequest(&raw[0], raw.size(),
+ pkgmgr_common::SystemLocale::GetInst().Get());
auto result = handler->ExtractResult();
if (result.size() == 0)
return true;
--- /dev/null
+// Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
+// Use of this source code is governed by an apache-2.0 license that can be
+// found in the LICENSE file.
+
+#include "system_locale.hh"
+
+namespace {
+
+constexpr const char DEFAULT_LOCALE[] = "No Locale";
+
+} // namespace
+
+namespace pkgmgr_common {
+
+SystemLocale& SystemLocale::GetInst() {
+ static SystemLocale inst;
+
+ return inst;
+}
+
+SystemLocale::SystemLocale() {
+ vconf_notify_key_changed(VCONFKEY_LANGSET, LanChangedCb, this);
+ char* lang = vconf_get_str(VCONFKEY_LANGSET);
+ lang_ = lang ? std::string(lang) : "";
+ free(lang);
+ SetLocale();
+}
+
+SystemLocale::~SystemLocale() {
+ vconf_ignore_key_changed(VCONFKEY_LANGSET, LanChangedCb);
+}
+
+void SystemLocale::RegisterEvent(IEvent* listener) {
+ listener_ = listener;
+}
+
+void SystemLocale::UnRegisterEvent() {
+ listener_ = nullptr;
+}
+
+const std::string& SystemLocale::Get() const {
+ return locale_;
+}
+
+void SystemLocale::SetLocale() {
+ if (lang_.empty() || lang_.length() < 5) {
+ locale_ = DEFAULT_LOCALE;
+ return;
+ }
+
+ char local_buf[6] = {0, };
+ snprintf(local_buf, sizeof(local_buf), "%c%c-%c%c",
+ lang_[0], lang_[1], tolower(lang_[3]), tolower(lang_[4]));
+ locale_ = local_buf;
+ if (listener_)
+ listener_->OnChanged(locale_);
+}
+
+void SystemLocale::LanChangedCb(keynode_t* node, void* user_data) {
+ char* val = vconf_keynode_get_str(node);
+ if (val == nullptr)
+ return;
+ SystemLocale* sl = reinterpret_cast<SystemLocale*>(user_data);
+ sl->lang_ = val;
+ sl->SetLocale();
+}
+
+} // namespace pkgmgr_common
\ No newline at end of file
--- /dev/null
+// Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
+// Use of this source code is governed by an apache-2.0 license that can be
+// found in the LICENSE file.
+
+#ifndef COMMON_SYSTEM_LOCALE_HH_
+#define COMMON_SYSTEM_LOCALE_HH_
+
+#include <vconf.h>
+
+#include <string>
+
+namespace pkgmgr_common {
+
+#ifndef EXPORT_API
+#define EXPORT_API __attribute__((visibility("default")))
+#endif
+
+class EXPORT_API SystemLocale {
+ public:
+ class IEvent {
+ public:
+ virtual ~IEvent() = default;
+ virtual void OnChanged(const std::string& locale) = 0;
+ };
+
+ static SystemLocale& GetInst();
+ const std::string& Get() const;
+ void RegisterEvent(IEvent* listener);
+ void UnRegisterEvent();
+
+ private:
+ SystemLocale();
+ ~SystemLocale();
+ void SetLocale();
+ static void LanChangedCb(keynode_t* node, void* user_data);
+
+ private:
+ std::string lang_;
+ std::string locale_;
+ IEvent* listener_ = nullptr;
+};
+
+} // namespace pkgmgr_common
+
+#endif // COMMON_SYSTEM_LOCALE_HH_
\ No newline at end of file
return PMINFO_R_OK;
}
-API char *_get_system_locale(void)
-{
- char *lang;
- char *locale;
-
- lang = vconf_get_str(VCONFKEY_LANGSET);
- if (lang == NULL) {
- locale = strdup(DEFAULT_LOCALE);
- if (locale == NULL) {
- LOGE("out of memory");
- return NULL;
- }
- return locale;
- }
-
- locale = malloc(sizeof(char) * 6);
- if (locale == NULL) {
- LOGE("out of memory");
- free(lang);
- return NULL;
- }
-
- strncpy(locale, lang, 2);
- locale[2] = '-';
- locale[3] = tolower(lang[3]);
- locale[4] = tolower(lang[4]);
- locale[5] = '\0';
-
- free(lang);
-
- return locale;
-}
-
API int __pkginfo_check_installed_storage(package_x *pkginfo)
{
char buf[MAX_QUERY_LEN] = {'\0'};
int _check_create_cert_db(void);
void _save_column_int(sqlite3_stmt *stmt, int idx, int *i);
void _save_column_str(sqlite3_stmt *stmt, int idx, char **str);
-char *_get_system_locale(void);
int __get_filter_condition(gpointer data, uid_t uid, char **condition, GList **param);
int __get_metadata_filter_condition(gpointer data, char **condition, GList **param);
int _add_icon_info_into_list(const char *locale, char *value, GList **icon);
auto condition = static_cast<GIOCondition>(
G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL);
sid_ = g_unix_fd_add(server_->GetFd(), condition, OnReceiveRequest, this);
- vconf_notify_key_changed(VCONFKEY_LANGSET, OnLanguageChange, this);
- std::unique_ptr<char, decltype(std::free)*> locale(
- _get_system_locale(), std::free);
- if (locale.get() == nullptr)
- return;
-
- thread_pool_->SetLocale(locale.get());
+ pkgmgr_common::SystemLocale::GetInst().RegisterEvent(this);
+ thread_pool_->SetLocale(pkgmgr_common::SystemLocale::GetInst().Get());
LOGI("Start Runner");
}
Runner::~Runner() {
g_source_remove(sid_);
- vconf_ignore_key_changed(VCONFKEY_LANGSET, OnLanguageChange);
+ pkgmgr_common::SystemLocale::GetInst().UnRegisterEvent();
}
int Runner::OnReceiveRequest(int fd, GIOCondition cond, void* user_data) {
return G_SOURCE_CONTINUE;
}
-void Runner::OnLanguageChange(keynode_t* key, void* user_data) {
- auto runner = static_cast<Runner*>(user_data);
- std::unique_ptr<char, decltype(std::free)*> locale(
- _get_system_locale(), std::free);
- if (locale.get() == nullptr)
- return;
-
- runner->thread_pool_->SetLocale(locale.get());
+void Runner::OnChanged(const std::string& locale) {
+ thread_pool_->SetLocale(locale);
}
bool Runner::QueueRequest(int client_fd) {
#include "server_socket.hh"
#include "worker_thread.hh"
+#include "system_locale.hh"
namespace pkgmgr_server {
#define EXPORT_API __attribute__((visibility("default")))
#endif
-class EXPORT_API Runner {
+class EXPORT_API Runner
+ : public pkgmgr_common::SystemLocale::IEvent {
public:
Runner(unsigned int thread_num);
~Runner();
+ void OnChanged(const std::string& locale) override;
+
private:
static int OnReceiveRequest(int fd, GIOCondition cond, void* user_data);
- static void OnLanguageChange(keynode_t* key, void* user_data);
bool QueueRequest(int client_fd);
private: