Merge "Fix build warning based on GCC-9" into tizen
[platform/core/api/notification.git] / notification-ex / image_item.cc
1 /*
2  * Copyright (c) 2019 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 <unistd.h>
18 #include <sys/types.h>
19 #include <dlog.h>
20
21 #include <map>
22 #include <string>
23 #include <list>
24 #include <utility>
25
26 #include "notification-ex/image_item.h"
27 #include "notification-ex/image_item_implementation.h"
28 #include "notification-ex/factory_manager.h"
29 #include "notification-ex/exception.h"
30 #include "notification-ex/shared_file.h"
31 #include "notification-ex/item_info_internal.h"
32 #include "notification-ex/ex_util.h"
33
34 #ifdef LOG_TAG
35 #undef LOG_TAG
36 #endif
37
38 #define LOG_TAG "NOTIFICATION_EX"
39 #define IMAGE_PATH_KEY "__IMAGE_PATH_KEY__"
40 #define PRIV_IMAGE_PATH_KEY "__PRIV_IMAGE_PATH_KEY__"
41
42 using namespace tizen_base;
43
44 namespace notification {
45 namespace item {
46 ImageItem::ImageItem(std::string image_path,
47     std::shared_ptr<AbstractAction> action)
48     : AbstractItem(action), impl_(new Impl(this, image_path)) {
49   UpdatePrivatePath();
50 }
51
52 ImageItem::ImageItem(std::string id, std::string image_path,
53     std::shared_ptr<AbstractAction> action)
54     : AbstractItem(id, action), impl_(new Impl(this, image_path)) {
55   UpdatePrivatePath();
56 }
57
58 ImageItem::Impl::Impl(ImageItem* parent, std::string image_path)
59     : parent_(parent), image_path_(image_path) {
60   LOGI("ImageItem impl created");
61 }
62
63 int ImageItem::GetType() const {
64   return AbstractItem::Image;
65 }
66
67 Bundle ImageItem::Serialize() const {
68   Bundle b;
69   b = AbstractItem::Serialize();
70   b.Add(IMAGE_PATH_KEY, impl_->image_path_);
71
72   if (!impl_->priv_image_path_.empty())
73     b.Add(PRIV_IMAGE_PATH_KEY, impl_->priv_image_path_);
74
75   return b;
76 }
77
78 void ImageItem::Deserialize(Bundle b) {
79   AbstractItem::Deserialize(b);
80   impl_->image_path_ = b.GetString(IMAGE_PATH_KEY);
81   impl_->priv_image_path_ = b.GetString(PRIV_IMAGE_PATH_KEY);
82 }
83
84 bool ImageItem::IsItemTypeExist(int type) {
85   if (GetType() == type)
86     return true;
87   return false;
88 }
89
90 std::string ImageItem::GetImagePath() const {
91   return impl_->image_path_;
92 }
93
94 std::list<std::string> ImageItem::GetSharedPath() const {
95   std::list<std::string> ret;
96
97   if (!impl_->priv_image_path_.empty())
98     ret.push_back(impl_->priv_image_path_);
99
100   return ret;
101 }
102
103 void ImageItem::SetSharedPath() {
104   if (!impl_->priv_image_path_.empty())
105     impl_->image_path_ = impl_->priv_image_path_;
106 }
107
108 std::list<std::map<std::string, std::string>> ImageItem::GetPathMapList() const {
109   std::list<std::map<std::string, std::string>> path_map_list;
110   std::map<std::string, std::string> path_map;
111
112   if (!impl_->priv_image_path_.empty()) {
113     path_map.insert(std::pair<std::string, std::string>(impl_->image_path_,
114         impl_->priv_image_path_));
115     path_map_list.push_back(path_map);
116   }
117
118   return path_map_list;
119 }
120
121 void ImageItem::UpdatePrivatePath() {
122   std::string path;
123
124   SharedFile* shared_file = new SharedFile();
125   if (!shared_file->IsPrivatePath(impl_->image_path_)) {
126     delete(shared_file);
127     return;
128   }
129
130   path = shared_file->GetDataPath(AbstractItem::GetSenderAppId(),
131             impl_->image_path_);
132   if (!path.empty())
133     impl_->priv_image_path_ = path;
134
135   delete(shared_file);
136 }
137
138 ImageItem::~ImageItem() = default;
139 ImageItem::Impl::~Impl() = default;
140
141 }  // namespace item
142 }  // namespace notification