To not to GetSharedPath if there is no image item in chat-message item
[platform/core/api/notification.git] / notification-ex / icon_text_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 <dlog.h>
18
19 #include "notification-ex/icon_text_item.h"
20 #include "notification-ex/icon_text_item_implementation.h"
21 #include "notification-ex/factory_manager.h"
22 #include "notification-ex/exception.h"
23
24 #ifdef LOG_TAG
25 #undef LOG_TAG
26 #endif
27
28 #define LOG_TAG "NOTIFICATION_EX"
29 #define ICONTEXT_PATH_KEY "__ICONTEXT_PATH_KEY__"
30 #define ICONTEXT_TITLE_KEY "__ICONTEXT_TITLE_KEY__"
31
32 using namespace tizen_base;
33
34 namespace notification {
35 namespace item {
36
37 IconTextItem::IconTextItem(std::string id, std::shared_ptr<IconItem> icon,
38     std::shared_ptr<TextItem> text, std::shared_ptr<AbstractAction> action)
39     : AbstractItem(id, action), impl_(new Impl(this, icon, text)) {
40 }
41
42 IconTextItem::Impl::Impl(IconTextItem* parent, std::shared_ptr<IconItem> icon,
43     std::shared_ptr<TextItem> text)
44     : parent_(parent), icon_(icon), text_(text) {
45   LOGI("IconTextItem impl created");
46 }
47
48 int IconTextItem::GetType() const {
49   return AbstractItem::IconText;
50 }
51
52 Bundle IconTextItem::Serialize() const {
53   Bundle b;
54   b = AbstractItem::Serialize();
55
56   b.Add(ICONTEXT_PATH_KEY,
57     reinterpret_cast<char*>(impl_->icon_->Serialize().ToRaw().first.get()));
58   b.Add(ICONTEXT_TITLE_KEY,
59     reinterpret_cast<char*>(impl_->text_->Serialize().ToRaw().first.get()));
60   return b;
61 }
62
63 void IconTextItem::Deserialize(Bundle b) {
64   AbstractItem::Deserialize(b);
65
66   std::shared_ptr<AbstractItem> icon =
67       FactoryManager::GetInst().CreateItem(AbstractItem::Icon);
68   icon.get()->Deserialize(Bundle(b.GetString(ICONTEXT_PATH_KEY)));
69   impl_->icon_ = std::static_pointer_cast<IconItem>(icon);
70
71   std::shared_ptr<AbstractItem> text =
72     FactoryManager::GetInst().CreateItem(AbstractItem::Text);
73   text.get()->Deserialize(Bundle(b.GetString(ICONTEXT_TITLE_KEY)));
74   impl_->text_ = std::static_pointer_cast<TextItem>(text);
75 }
76
77 AbstractItem& IconTextItem::FindByID(std::string id) {
78   if (GetId() == id)
79     return *this;
80
81   if (impl_->icon_->GetId() == id)
82     return *(impl_->icon_);
83
84   if (impl_->text_->GetId() == id)
85     return *(impl_->text_);
86
87   return FactoryManager::GetInst().GetNullItem();
88 }
89
90 AbstractItem& IconTextItem::FindByMainType(MainType type) {
91   if (GetMainType() == type)
92     return *this;
93
94   if (impl_->icon_->GetMainType() == type)
95     return *(impl_->icon_);
96
97   if (impl_->text_->GetMainType() == type)
98     return *(impl_->text_);
99
100   return FactoryManager::GetInst().GetNullItem();
101 }
102
103 bool IconTextItem::IsItemTypeExist(int type) {
104   if (GetType() == type)
105     return true;
106   return false;
107 }
108
109 IconItem& IconTextItem::GetIconItem() const {
110   return *(impl_->icon_);
111 }
112
113 TextItem& IconTextItem::GetTextItem() const {
114   return *(impl_->text_);
115 }
116
117 IconTextItem::~IconTextItem() = default;
118 IconTextItem::Impl::~Impl() = default;
119
120 }  // namespace item
121 }  // namespace notification