sensor: update app uninstall event dbus signal
[platform/core/context/context-provider.git] / src / app-stats / DbInit.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
17 #include <sstream>
18 #include <Json.h>
19 #include <Types.h>
20 #include "AppStatisticsTypes.h"
21 #include "DbInit.h"
22
23 #define EMPTY_CHECKER_QID 999
24
25 ctx::AppDbInitializer::AppDbInitializer()
26 {
27         __createTable();
28         __checkAppList();
29 }
30
31 ctx::AppDbInitializer::~AppDbInitializer()
32 {
33 }
34
35 void ctx::AppDbInitializer::__createTable()
36 {
37         static bool done = false;
38         IF_FAIL_VOID(!done);
39
40         __dbManager.createTable(0, APP_TABLE_USAGE_LOG, APP_TABLE_USAGE_LOG_COLUMNS, NULL, NULL);
41         __dbManager.createTable(0, APP_TABLE_REMOVABLE_APP, APP_TABLE_REMOVABLE_APP_COLUMNS, NULL, NULL);
42         __dbManager.execute(0, APP_TEMP_USAGE_FREQ_SQL, NULL);
43
44         done = true;
45 }
46
47 void ctx::AppDbInitializer::__checkAppList()
48 {
49         __dbManager.execute(EMPTY_CHECKER_QID, "SELECT * FROM " APP_TABLE_REMOVABLE_APP " LIMIT 1", this);
50 }
51
52 void ctx::AppDbInitializer::__duplicateAppList()
53 {
54         int err;
55         package_manager_filter_h filter;
56
57         err = package_manager_filter_create(&filter);
58         IF_FAIL_VOID_TAG(err == PACKAGE_MANAGER_ERROR_NONE, _E, "package_manager_filter_create() failed");
59
60         err = package_manager_filter_add_bool(filter, PACKAGE_MANAGER_PKGINFO_PROP_REMOVABLE, true);
61         IF_FAIL_CATCH_TAG(err == PACKAGE_MANAGER_ERROR_NONE, _E, "package_manager_filter_add_bool() failed");
62
63         err = package_manager_filter_foreach_package_info(filter, __packageInfoCb, this);
64         IF_FAIL_CATCH_TAG(err == PACKAGE_MANAGER_ERROR_NONE, _E, "package_manager_filter_foreach_package_info() failed");
65
66 CATCH:
67         if (filter)
68                 package_manager_filter_destroy(filter);
69 }
70
71 bool ctx::AppDbInitializer::__packageInfoCb(package_info_h packageInfo, void *userData)
72 {
73         int err = package_info_foreach_app_from_package(packageInfo, PACKAGE_INFO_UIAPP, __appInfoCb, userData);
74         IF_FAIL_RETURN_TAG(err == PACKAGE_MANAGER_ERROR_NONE, false, _E, "package_info_foreach_app_from_package() failed");
75         return true;
76 }
77
78 bool ctx::AppDbInitializer::__appInfoCb(package_info_app_component_type_e compType, const char *appId, void *userData)
79 {
80         Json data;
81         DatabaseManager dbManager;
82
83         data.set(NULL, KEY_APP_ID, appId);
84         return dbManager.insert(0, APP_TABLE_REMOVABLE_APP, data, NULL);
85 }
86
87 void ctx::AppDbInitializer::onTableCreated(unsigned int queryId, int error)
88 {
89 }
90
91 void ctx::AppDbInitializer::onInserted(unsigned int queryId, int error, int64_t rowId)
92 {
93 }
94
95 void ctx::AppDbInitializer::onExecuted(unsigned int queryId, int error, std::vector<Json>& records)
96 {
97         if (queryId != EMPTY_CHECKER_QID) {
98                 _E("Unknown Query ID: %d", queryId);
99                 delete this;
100                 return;
101         }
102
103         if (records.empty())
104                 __duplicateAppList();
105
106         delete this;
107 }