Implement basic pkgmgrinfo_appinfo_ APIs using newly implemented pkginfo_manager
[platform/core/appfw/pkgmgr-info.git] / src / manager / pkginfo_manager.cc
1 /*
2  * Copyright (c) 2021 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 "manager/pkginfo_manager.h"
18
19 #include <sys/types.h>
20
21 #include "glib.h"
22
23 #include "pkgmgrinfo_private.h"
24
25 #include "logging.hh"
26 #include "common/parcel/appinfo_parcelable.hh"
27 #include "common/parcel/filter_parcelable.hh"
28 #include "common/parcel/pkginfo_parcelable.hh"
29 #include "common/parcel/result_parcelable.hh"
30
31 #include "client/pkginfo_client.hh"
32
33 #include <parcel.hh>
34
35 #ifdef LOG_TAG
36 #undef LOG_TAG
37 #endif
38 #define LOG_TAG "PKGMGR_INFO"
39
40 #ifdef EXPORT_API
41 #undef EXPORT_API
42 #endif
43 #define EXPORT_API __attribute__((visibility("default")))
44
45 extern "C" EXPORT_API int _pkginfo_get_packages(uid_t uid,
46             pkgmgrinfo_filter_x *filter, int flag, GHashTable *packages) { 
47         std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
48                         new pkgmgr_common::parcel::FilterParcelable(uid,
49                                         static_cast<pkgmgrinfo_filter_x*>(filter), flag));
50
51         pkgmgr_client::PkgInfoClient client(parcelable, uid);
52         if (!client.SendRequest())
53       return PMINFO_R_ERROR;
54
55         std::shared_ptr<pkgmgr_common::parcel::PkgInfoParcelable> return_parcel(
56                         std::static_pointer_cast<pkgmgr_common::parcel::PkgInfoParcelable>(
57                                         client.GetResultParcel()));
58
59         tizen_base::Parcel parcel;
60         parcel.ReadParcelable(return_parcel.get());
61
62         auto result_list = return_parcel->GetPkgInfo();
63         // TODO: check noentry error has returned if size of result_list is 0
64         for (auto& pkginfo : result_list) {
65                 g_hash_table_insert(packages, (gpointer)pkginfo->package, (gpointer)pkginfo);
66                 // TODO: remove element from list. is this work?
67                 pkginfo = nullptr;      
68         }
69
70         return PMINFO_R_OK;
71 }
72
73 // TODO: Need to add target db uid to identify which database to be searched
74 extern "C" EXPORT_API int _appinfo_get_applications(uid_t db_uid, uid_t uid,
75                 pkgmgrinfo_filter_x *filter, int flag, GHashTable *packages) {
76         std::shared_ptr<pkgmgr_common::parcel::AbstractParcelable> parcelable(
77                         new pkgmgr_common::parcel::FilterParcelable(uid,
78                                         static_cast<pkgmgrinfo_filter_x*>(filter), flag));
79
80         pkgmgr_client::PkgInfoClient client(parcelable, uid);
81         if (!client.SendRequest())
82       return PMINFO_R_ERROR;
83
84         std::shared_ptr<pkgmgr_common::parcel::AppInfoParcelable> return_parcel(
85                         std::static_pointer_cast<pkgmgr_common::parcel::AppInfoParcelable>(
86                                         client.GetResultParcel()));
87
88         tizen_base::Parcel parcel;
89         parcel.ReadParcelable(return_parcel.get());
90         auto result_list = return_parcel->GetAppInfo();
91         for (auto& appinfo : result_list) {
92                 g_hash_table_insert(packages, (gpointer)appinfo->appid, (gpointer)appinfo);
93                 // TODO: remove element from list. is this work?
94                 appinfo = nullptr;      
95         }
96
97         return PMINFO_R_OK;
98 }