2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <tzplatform_config.h>
21 #include <pkgmgr-info.h>
22 #include <pkgmgr_installer_info.h>
25 #include "watchface-complication/include/watchface-complication-internal.h"
26 #include "watchface-complication/db-manager.h"
27 #include "watchface-common/watchface-util.h"
33 #define LOG_TAG "WATCHFACE_COMPLICATION"
35 #define DEFAULT_LOCALE "No Locale"
38 using namespace tizen_base;
43 using Cursor = std::unique_ptr<sqlite3_stmt, decltype(sqlite3_finalize)*>;
45 : db_(nullptr, sqlite3_close_v2), stmt_(nullptr, sqlite3_finalize) {}
48 const char* path = GetParserDataPath();
52 sqlite3* db = nullptr;
53 int ret = sqlite3_open_v2(path, &db, SQLITE_OPEN_READONLY, nullptr);
54 if (ret != SQLITE_OK) {
55 LOGE("open db(%s) error: %d", path, ret);
68 bool Prepare(const std::string& query) {
69 sqlite3_stmt* stmt = nullptr;
70 if (sqlite3_prepare_v2(db_.get(), query.c_str(), query.size(), &stmt,
71 nullptr) != SQLITE_OK) {
72 LOGE("prepare error: %s", sqlite3_errmsg(db_.get()));
81 const Cursor& GetCursor() const {
86 return sqlite3_step(stmt_.get());
89 bool Bind(int pos, std::string text) {
90 int ret = sqlite3_bind_text(stmt_.get(), pos, text.c_str(),
91 -1, SQLITE_TRANSIENT);
92 if (ret != SQLITE_OK) {
93 LOGE("bind error: %s", sqlite3_errmsg(db_.get()));
100 bool Bind(int pos, int val) {
101 if (sqlite3_bind_int(stmt_.get(), pos, val) != SQLITE_OK) {
102 LOGE("bind error: %s", sqlite3_errmsg(db_.get()));
109 const char* GetText(int pos) const {
110 return reinterpret_cast<const char*>(sqlite3_column_text(stmt_.get(), pos));
113 int GetInt(int pos) const {
114 return sqlite3_column_int(stmt_.get(), pos);
118 const char* GetParserDataPath() const {
123 pkgmgr_installer_info_get_target_uid(&target_uid);
125 if (target_uid == ROOT_USER
126 || target_uid == tzplatform_getuid(TZ_SYS_GLOBALAPP_USER))
132 tzplatform_set_user(target_uid);
134 path = tzplatform_mkpath(is_global ? TZ_SYS_DB : TZ_USER_DB,
135 ".complication_provider.db");
136 tzplatform_reset_user();
142 std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*> db_;
147 namespace watchface_complication {
149 DBManager::DBManager() = default;
150 DBManager::~DBManager() = default;
152 std::unique_ptr<Bundle> DBManager::GetDefaultData(const char* provider_id,
154 static const char query[] =
155 "SELECT trusted, appid, default_data FROM complication_provider "
156 "WHERE provider_id=? AND support_type=?";
163 if (!db.Prepare(query))
166 if (!db.Bind(1, provider_id))
169 if (!db.Bind(2, support_type))
172 if (db.Step() != SQLITE_ROW)
175 int trusted = db.GetInt(0);
176 const char* app_id = db.GetText(1);
177 if (app_id == nullptr) {
178 LOGE("app_id is nullptr");
182 std::string provider_app_id = app_id;
183 if (trusted && util::CheckCertificate(provider_app_id) == false)
186 const char* raw_data = db.GetText(2);
187 if (raw_data == nullptr) {
188 LOGE("raw_data is nullptr");
192 std::unique_ptr<Bundle> default_data;
194 default_data = std::unique_ptr<Bundle>(new Bundle(std::string(raw_data)));
195 } catch (const std::exception& e) {
196 LOGE("Exception occurred : %s", e.what());
200 util::ConvertPathToAppPath(provider_app_id.c_str(),
201 default_data.get()->GetHandle());
206 int DBManager::GetProviderPeriod(std::string& provider_id, int* period) {
207 static const char query[] =
208 "SELECT period FROM complication_provider WHERE provider_id=?";
213 return WATCHFACE_COMPLICATION_ERROR_DB;
215 if (!db.Prepare(query))
216 return WATCHFACE_COMPLICATION_ERROR_DB;
218 if (!db.Bind(1, provider_id))
219 return WATCHFACE_COMPLICATION_ERROR_DB;
221 /* complication_provider table's default value of period is -1.
222 * So, if nothing specified in period, it will return -1
224 if (db.Step() == SQLITE_ROW)
225 *period = db.GetInt(0);
227 return WATCHFACE_COMPLICATION_ERROR_NONE;
230 bool DBManager::IsProviderExist(std::string& provider_id, int support_type) {
231 static const char query[] =
232 "SELECT trusted, appid FROM complication_provider "
233 "WHERE provider_id=? AND support_type=?";
239 if (!db.Prepare(query))
242 if (!db.Bind(1, provider_id))
245 if (!db.Bind(2, support_type))
248 bool is_exist = false;
249 if (db.Step() == SQLITE_ROW) {
252 trusted = db.GetInt(0);
253 const char* app_id = db.GetText(1);
254 if (app_id == nullptr) {
255 LOGE("app_id is nullptr");
258 std::string provider_app_id = app_id;
259 if (trusted && util::CheckCertificate(provider_app_id) == false)
268 std::string DBManager::GetProviderAppId(const char* provider_id) {
269 static const char query[] =
270 "SELECT trusted, appid FROM complication_provider WHERE provider_id=?";
274 return std::string();
276 if (!db.Prepare(query))
277 return std::string();
279 if (!db.Bind(1, provider_id))
280 return std::string();
282 if (db.Step() != SQLITE_ROW)
283 return std::string();
285 int trusted = db.GetInt(0);
286 const char* app_id = db.GetText(1);
288 if (app_id == nullptr) {
289 LOGE("app_id is nullptr");
290 return std::string();
293 std::string provider_app_id = app_id;
294 if (trusted && util::CheckCertificate(provider_app_id) == false)
295 return std::string();
297 return provider_app_id;
300 std::list<std::unique_ptr<DBManager::ProviderInfo>>
301 DBManager::GetProviderListWithTypes(int supported_types) {
302 static const char query[] =
303 "SELECT trusted, appid, provider_id, support_type FROM complication_provider "
304 "WHERE (support_type & ?) > 0";
310 if (!db.Prepare(query))
313 if (!db.Bind(1, supported_types))
316 std::list<std::unique_ptr<DBManager::ProviderInfo>> provider_list;
317 while (db.Step() == SQLITE_ROW) {
318 int trusted = db.GetInt(0);
319 const char* app_id = db.GetText(1);
320 if (app_id == nullptr) {
321 LOGE("app_id is nullptr");
325 if (trusted && util::CheckCertificate(std::string(app_id)) == false)
328 const char* provider_id = db.GetText(2);
329 if (provider_id == nullptr) {
330 LOGE("prodiver_id is nullptr");
333 int type = db.GetInt(3);
334 provider_list.emplace_back(
335 std::unique_ptr<ProviderInfo>(new ProviderInfo(provider_id, type)));
338 return std::move(provider_list);
341 std::list<std::string> DBManager::GetProviderListWithAppId(
342 const char* provider_app_id) {
343 static const char query[] =
344 "SELECT DISTINCT provider_id FROM complication_provider WHERE appid=?";
350 if (!db.Prepare(query))
353 if (!db.Bind(1, std::string(provider_app_id)))
356 std::list<std::string> provider_list;
357 while (db.Step() == SQLITE_ROW) {
358 const char* provider = db.GetText(0);
359 if (provider == nullptr) {
360 LOGW("provider is nullptr");
363 provider_list.push_back(std::string(provider));
366 return provider_list;
369 int DBManager::GetSupportTypes(std::string& provider_id, int* types) {
370 static const char query[] =
371 "SELECT trusted, appid, SUM(support_type) FROM complication_provider "
372 "WHERE provider_id = ?";
376 return WATCHFACE_COMPLICATION_ERROR_DB;
378 if (!db.Prepare(query))
379 return WATCHFACE_COMPLICATION_ERROR_DB;
381 if (!db.Bind(1, provider_id))
382 return WATCHFACE_COMPLICATION_ERROR_DB;
384 int supported_types = 0;
385 if (db.Step() == SQLITE_ROW) {
386 int trusted = db.GetInt(0);
387 const char* app_id = db.GetText(1);
388 if (app_id == nullptr) {
389 LOGE("app_id is nullptr");
390 return WATCHFACE_COMPLICATION_ERROR_DB;
392 std::string provider_app_id = app_id;
393 if (trusted && util::CheckCertificate(provider_app_id) == false)
396 supported_types = db.GetInt(2);
399 *types = supported_types;
401 return WATCHFACE_COMPLICATION_ERROR_NONE;
404 std::list<std::string> DBManager::GetRequiredPrivilegeList(
405 std::string& provider_id) {
406 static const char query[] =
407 "SELECT DISTINCT privilege FROM provider_privilege WHERE provider_id=?";
413 if (!db.Prepare(query))
416 if (!db.Bind(1, provider_id))
419 std::list<std::string> privlege_list;
420 while (db.Step() == SQLITE_ROW) {
421 const char* privilege = db.GetText(0);
422 if (privilege == nullptr) {
423 LOGW("privilege is nullptr");
426 privlege_list.push_back(std::string(privilege));
429 return privlege_list;
433 Return WATCHFACE_COMPLICATION_EVENT_NONE if there is no required events.
434 Return bigger than 1 value if there is at least one required event.
435 Return -1 if error occurs.
437 int DBManager::GetRequiredSupportEvents(
438 std::string& provider_id) {
439 int support_events = WATCHFACE_COMPLICATION_EVENT_NONE;
440 static const char query[] =
441 "SELECT support_events FROM provider_support_events WHERE provider_id=?";
447 if (!db.Prepare(query))
450 if (!db.Bind(1, provider_id))
453 if (db.Step() == SQLITE_ROW)
454 support_events = db.GetInt(0);
456 return support_events;
459 char* DBManager::GetSystemLocale() {
465 lang = vconf_get_str(VCONFKEY_LANGSET);
466 if (lang == nullptr) {
467 locale = strdup(DEFAULT_LOCALE);
468 if (locale == nullptr) {
469 LOGE("out of memory");
475 tmp = strtok_r(lang, ".", &ptr);
476 if (tmp == nullptr) {
477 LOGE("failed to get language");
482 locale = strdup(tmp);
483 if (locale == nullptr) {
484 LOGE("out of memory");
489 for (int i = 0; i < static_cast<int>(strlen(locale)); i++) {
490 if (locale[i] == '_')
493 if (isupper(locale[i]))
494 locale[i] = tolower(locale[i]);
502 std::string DBManager::GetLabel(const char* provider_id) {
503 static const char query[] =
504 "SELECT provider_label FROM provider_localized_info "
505 "WHERE provider_id=? AND locale=?";
509 return std::string();
511 if (!db.Prepare(query))
512 return std::string();
514 char* locale = GetSystemLocale();
515 if (locale == nullptr)
516 return std::string();
518 std::unique_ptr<char, decltype(std::free)*> locale_ptr(locale, std::free);
520 if (!db.Bind(1, provider_id))
521 return std::string();
523 if (!db.Bind(2, locale_ptr.get()))
524 return std::string();
527 if (db.Step() == SQLITE_ROW) {
528 const char* lb = db.GetText(0);
530 LOGW("lb is nullptr");
537 sqlite3_reset(db.GetCursor().get());
538 sqlite3_clear_bindings(db.GetCursor().get());
540 db.Bind(1, provider_id);
541 db.Bind(2, DEFAULT_LOCALE);
543 if (db.Step() == SQLITE_ROW) {
544 const char* lb = db.GetText(0);
546 LOGW("lb is nullptr");
556 std::string DBManager::GetSetupAppId(const char* provider_id) {
557 static const char query[] =
558 "SELECT DISTINCT setup_appid FROM provider_setup_appid WHERE provider_id=?";
562 return std::string();
564 if (!db.Prepare(query))
565 return std::string();
567 if (!db.Bind(1, provider_id))
568 return std::string();
570 std::string setup_appid;
571 if (db.Step() == SQLITE_ROW) {
572 const char* appid = db.GetText(0);
573 if (appid == nullptr) {
574 LOGE("appid is nullptr");
575 return std::string();
583 int DBManager::GetTrustedInfo(std::string& provider_id, bool* trusted) {
584 static const char query[] =
585 "SELECT trusted FROM complication_provider WHERE provider_id = ?";
589 return WATCHFACE_COMPLICATION_ERROR_DB;
591 if (!db.Prepare(query))
592 return WATCHFACE_COMPLICATION_ERROR_DB;
594 if (!db.Bind(1, provider_id))
595 return WATCHFACE_COMPLICATION_ERROR_DB;
597 if (db.Step() != SQLITE_ROW)
598 return WATCHFACE_COMPLICATION_ERROR_INVALID_PARAMETER;
600 int trusted_info = db.GetInt(0);
601 if (trusted_info == 0)
603 else if (trusted_info == 1)
606 return WATCHFACE_COMPLICATION_ERROR_DB;
608 return WATCHFACE_COMPLICATION_ERROR_NONE;
611 } // namespace watchface_complication