summaryrefslogtreecommitdiff
path: root/notification-ex/manager.cc
blob: d46b1e138e8af3486e786cf6986d829c08999a6a (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
/*
 * 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 <glib.h>
#include <unistd.h>

#include <list>

#include "notification-ex/item_inflator.h"
#include "notification-ex/manager.h"
#include "notification-ex/manager_implementation.h"
#include "notification-ex/event_info.h"
#include "notification-ex/dbus_connection_manager.h"
#include "notification-ex/ex_util.h"
#include "notification-ex/item_info_internal.h"

#ifdef LOG_TAG
#undef LOG_TAG
#endif

#define LOG_TAG "NOTIFICATION_EX"

#define MAX_PACKAGE_STR_SIZE 512
#define NOTIFICATION_EX_MANAGER_OBJECT_PATH "/org/tizen/notification_ex_manager"

using namespace std;
using namespace notification::item;
namespace notification {

Manager::Manager(IEventSender* sender, IEventListener* listener, string receiver_group)
  : impl_(new Impl(this, sender, listener, receiver_group)) {
}
Manager::~Manager() = default;

Manager::Impl::~Impl() {
  listener_->UnRegisterObserver(parent_);
}
Manager::Impl::Impl(Manager* parent,
    IEventSender* sender, IEventListener* listener, string receiver_group)
  : sender_(sender), listener_(listener), receiver_group_(receiver_group),
    parent_(parent) {
  LOGI("impl created");
  listener_->RegisterObserver(parent_);
}

void Manager::Impl::SendNotify(shared_ptr<item::AbstractItem> noti,
    EventInfo::EventType type) {
  Bundle serialized = noti->Serialize();
  EventInfo info(type, util::GetAppId(), noti->GetChannel());
  list<Bundle> serialized_list {serialized};

  /* Reply to Sender */
  sender_->Notify(info, serialized_list, noti->GetSenderAppId());
}

void Manager::Update(shared_ptr<item::AbstractItem> noti) {
  impl_->SendNotify(noti, EventInfo::Update);
}

void Manager::Remove(shared_ptr<item::AbstractItem> noti) {
  impl_->SendNotify(noti, EventInfo::Delete);
}

void Manager::Hide(shared_ptr<item::AbstractItem> noti) {
  ((IItemInfoInternal*)noti->GetInfo().get())->AddHideViewer(util::GetAppId());
  impl_->SendNotify(noti, EventInfo::Update);
}

shared_ptr<item::AbstractItem> Manager::FindByRootID(string id) {
  EventInfo info(EventInfo::Get,
     util::GetAppId(), "", id);
  list<Bundle> result = impl_->sender_->Request(info);
  if (result.size() == 0) {
    LOGE("Fail to get noti");
    return shared_ptr<item::AbstractItem>({});
  }
  Bundle b = result.front();
  shared_ptr<AbstractItem> gen_item = ItemInflator::Create(b);
  return gen_item;
}

list<shared_ptr<item::AbstractItem>> Manager::Get() {
  EventInfo info(EventInfo::Get, util::GetAppId(), "");
  list<Bundle> result = impl_->sender_->Request(info);
  list<shared_ptr<item::AbstractItem>> gen_list;
  for (auto& i : result) {
    shared_ptr<AbstractItem> gen_item = ItemInflator::Create(i);
    gen_list.emplace_back(gen_item);
  }
  return gen_list;
}

void Manager::SendEvent(const EventInfo& info, shared_ptr<item::AbstractItem> noti) {
  Bundle serialized = noti->Serialize();
  Bundle serialized_info = info.Serialize();
  list<Bundle> serialized_list {serialized};
  impl_->sender_->Notify(info, serialized_list, noti->GetSenderAppId());
}

list<Bundle> Manager::OnRequest(const EventInfo& info) {
  list<shared_ptr<item::AbstractItem>> item_list = OnRequestEvent(info);
  list<Bundle> serialized_list;
  for (auto& i : item_list) {
    serialized_list.push_back(i->Serialize());
  }
  return serialized_list;
}

void Manager::OnEvent(const EventInfo& info, list<Bundle> serialized) {
  shared_ptr<AbstractItem> gen_item;
  const EventInfo::EventType type = info.GetEventType();
  switch(type) {
  case EventInfo::Post:
  {
    list<shared_ptr<item::AbstractItem>> added;
    for (auto& i : serialized) {
      gen_item = ItemInflator::Create(i);
      if (gen_item->CanReceive(impl_->receiver_group_))
        added.emplace_back(gen_item);
    }
    if (added.size() > 0)
      OnAdd(info, added);
    break;
  }
  case EventInfo::Update:
  {
    for (auto& i : serialized) {
      gen_item = ItemInflator::Create(i);
      if (gen_item->CanReceive(impl_->receiver_group_))
        OnUpdate(info, gen_item);
    }
    break;
  }
  case EventInfo::Delete:
    for (auto& i : serialized) {
      gen_item = ItemInflator::Create(i);
      if (gen_item->CanReceive(impl_->receiver_group_))
        OnDelete(info, gen_item);
    }
    break;
  case EventInfo::Get:
    break;
  }
}

void Manager::OnAdd(const EventInfo& info, list<shared_ptr<item::AbstractItem>> addedItem) {
}

void Manager::OnUpdate(const EventInfo& info, shared_ptr<item::AbstractItem> updatedItem) {
}

void Manager::OnDelete(const EventInfo& info, shared_ptr<item::AbstractItem> deletedItem) {
}

list<shared_ptr<item::AbstractItem>> Manager::OnRequestEvent(const EventInfo& info) {
  return list<shared_ptr<item::AbstractItem>>({});
}

string Manager::GetPath() {
  return NOTIFICATION_EX_MANAGER_OBJECT_PATH;
}

}  // nampace notification