Implement _pkginfo_get_packages
[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/filter_parcelable.hh"
27 #include "common/parcel/pkginfo_parcelable.hh"
28 #include "common/parcel/result_parcelable.hh"
29
30 #include "client/pkginfo_client.hh"
31
32 #include <parcel.hh>
33
34 #ifdef LOG_TAG
35 #undef LOG_TAG
36 #endif
37 #define LOG_TAG "PKGMGR_INFO"
38
39 #ifdef EXPORT_API
40 #undef EXPORT_API
41 #endif
42 #define EXPORT_API __attribute__((visibility("default")))
43
44 extern "C" EXPORT_API int _pkginfo_get_packages(uid_t uid,
45     pkgmgrinfo_filter_x *filter, int flag, GHashTable *packages) {
46
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   if (result_list.size() == 0)
64     return PMINFO_R_ENOENT;
65         for (auto& pkginfo : result_list) {
66     g_hash_table_insert(packages, (gpointer)pkginfo->package, (gpointer)pkginfo);
67     // TODO: remove element from list. is this work?
68     pkginfo = nullptr;
69         }
70
71         return PMINFO_R_OK;
72 }