summaryrefslogtreecommitdiff
path: root/notification-ex/group_item.cc
blob: 75a55c76f0630aabe8b2da55ff7bcf773f162e22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
 * Copyright (c) 2019 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <dlog.h>
#include <app_common.h>

#include <memory>
#include <vector>

#include "notification-ex/group_item.h"
#include "notification-ex/group_item_implementation.h"
#include "notification-ex/factory_manager.h"
#include "notification-ex/ex_util.h"
#include "notification-ex/exception.h"

#ifdef LOG_TAG
#undef LOG_TAG
#endif

#define LOG_TAG "NOTIFICATION_EX"
#define GROUP_CHILDREN_KEY "__GROUP_CHILDREN_KEY__"
#define GROUP_CHILDREN_TYPE_KEY "__GROUP_CHILDREN_TYPE_KEY__"
#define GROUP_DIRECTION_KEY "__GROUP_DIRECTION_KEY__"
#define GROUP_DIRECTION_VERTICAL "VERTICAL"
#define GROUP_DIRECTION_HORIZONTAL "HORIZONTAL"
#define GROUP_APP_LABEL_KEY "__GROUP_APP_LABEL_KEY__"

using namespace std;
using namespace tizen_base;
namespace notification {
namespace item {

GroupItem::GroupItem(string id, shared_ptr<AbstractAction> action)
    : AbstractItem(id), impl_(new Impl(this)) {
}

GroupItem::GroupItem(shared_ptr<AbstractAction> action)
    : AbstractItem(), impl_(new Impl(this)) {
}

GroupItem::Impl::Impl(GroupItem* parent)
    : parent_(parent) {
  LOGI("GroupItem created");
  char* name;
  int ret = app_get_name(&name);
  if (ret != APP_ERROR_NONE) {
    app_label_ = util::GetAppId();
  } else {
    app_label_ = string(name);
    free(name);
  }
}

GroupItem::~GroupItem() {
}

int GroupItem::GetType() const {
  return AbstractItem::Group;
}

Bundle GroupItem::Serialize() const {
  Bundle b;
  b = AbstractItem::Serialize();
  b.Add(GROUP_DIRECTION_KEY, impl_->is_vertical_ ?
      GROUP_DIRECTION_VERTICAL : GROUP_DIRECTION_HORIZONTAL);

  b.Add(GROUP_APP_LABEL_KEY, impl_->app_label_);
  if (impl_->children_list_.size() == 0)
    return b;

  vector<std::string> arr;
  for (auto& i : impl_->children_list_) {
    Bundle serialized = i.get()->Serialize();
    serialized.Add(
        GROUP_CHILDREN_TYPE_KEY, to_string(static_cast<int>(i.get()->GetType())));
    arr.push_back(reinterpret_cast<char*>(serialized.ToRaw().first.get()));
  }
  b.Add(GROUP_CHILDREN_KEY, arr);

  return b;
}

void GroupItem::Deserialize(Bundle b) {
  AbstractItem::Deserialize(b);
  impl_->is_vertical_ =
      (b.GetString(GROUP_DIRECTION_KEY) == GROUP_DIRECTION_VERTICAL);
  impl_->app_label_ = b.GetString(GROUP_APP_LABEL_KEY);

  vector<string> str_arr = b.GetStringArray(GROUP_CHILDREN_KEY);
  if (str_arr.size() == 0)
    return;

  for (string str : str_arr) {
    Bundle serialized(str);
    string type_str = serialized.GetString(GROUP_CHILDREN_TYPE_KEY);
    AbstractItem::Type child_type =
        static_cast<AbstractItem::Type>(strtol(type_str.c_str(), NULL, 10));
    shared_ptr<AbstractItem> child =
        FactoryManager::GetInst().CreateItem(child_type);
    child.get()->Deserialize(serialized);
    AddChild(child);
  }
}

AbstractItem& GroupItem::FindByID(std::string id) {
  if (GetId() == id)
    return *this;

  for (auto& i : impl_->children_list_) {
    auto& re = i.get()->FindByID(id);
    if (re.GetId() == id)
      return re;
  }
  return FactoryManager::GetInst().GetNullItem();
}

AbstractItem& GroupItem::FindByMainType(MainType type) {
  if (GetMainType() == type)
    return *this;

  for (auto& i : impl_->children_list_) {
    auto& re = i.get()->FindByMainType(type);
    if (re.GetType() != NullObject)
      return re;
  }
  return FactoryManager::GetInst().GetNullItem();
}

bool GroupItem::IsItemTypeExist(int type) {
  if (GetType() == type)
    return true;

  for (auto& i : impl_->children_list_) {
    if (i.get()->IsItemTypeExist(type))
      return true;
  }
  return false;
}

list<string> GroupItem::GetSharedPath() const {
  list<string> ret;

  auto r = AbstractItem::GetSharedPath();
  for (auto& shared_path_r : r)
    ret.push_back(move(shared_path_r));

  for (auto& i : impl_->children_list_) {
    auto c = i->GetSharedPath();
    for (auto& shared_path_c : c)
      ret.push_back(move(shared_path_c));
  }

  return ret;
}

void GroupItem::SetSharedPath() {
  for (auto& i : impl_->children_list_)
    i->SetSharedPath();
}

list<map<string, string>> GroupItem::GetPathMapList() const {
  list<map<string, string>> path_map_list;

  auto r = AbstractItem::GetPathMapList();
  for (auto& path_map_r : r)
    path_map_list.push_back(move(path_map_r));

  for (auto& i : impl_->children_list_) {
    auto c = i->GetPathMapList();
    for (auto& path_map_c : c)
      path_map_list.push_back(move(path_map_c));
  }

  return path_map_list;
}

void GroupItem::AddChild(shared_ptr<AbstractItem> child) {
  impl_->children_list_.emplace_back(child);
}

void GroupItem::RemoveChild(string itemId) {
  for (auto& i : impl_->children_list_) {
    if (i.get()->GetId() == itemId) {
      impl_->children_list_.remove(i);
      break;
    }
  }
}

void GroupItem::RemoveChildren() {
  impl_->children_list_.clear();
}

list<shared_ptr<AbstractItem>> GroupItem::GetChildren() {
  return impl_->children_list_;
}

void GroupItem::SetDirection(bool vertical) {
  impl_->is_vertical_ = vertical;
}

bool GroupItem::IsVertical() {
  return impl_->is_vertical_;
}

string GroupItem::GetAppLabel() {
  return impl_->app_label_;
}

void GroupItem::SetAppLabel(string label) {
  impl_->app_label_ = label;
}

}  // namespace item
}  // namespace notification