/* * 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 #include #include #include #include "notification-ex/reporter.h" #include "notification-ex/reporter_implementation.h" #include "notification-ex/event_info_internal.h" #include "notification-ex/item_inflator.h" #include "notification-ex/dbus_connection_manager.h" #include "notification-ex/ex_util.h" #include "notification-ex/item_info_internal.h" #include "notification-ex/shared_file.h" #include "notification-ex/exception.h" #ifdef LOG_TAG #undef LOG_TAG #endif #define LOG_TAG "NOTIFICATION_EX" #define MAX_PACKAGE_STR_SIZE 512 #define NOTIFICATION_EX_REPORTER_OBJECT_PATH "/org/tizen/notification_ex_reporter" using namespace std; using namespace tizen_base; using namespace notification::item; namespace notification { Reporter::Reporter( unique_ptr sender, unique_ptr listener) : impl_(new Impl(this, move(sender), move(listener))) { } Reporter::~Reporter() = default; Reporter::Impl::~Impl() { listener_->UnRegisterObserver(parent_); } Reporter::Impl::Impl(Reporter* parent, unique_ptr sender, unique_ptr listener) : sender_(move(sender)), listener_(move(listener)), parent_(parent) { LOGI("impl created"); listener_->RegisterObserver(parent_); } void Reporter::SendError(const IEventInfo& info, NotificationError error) { list serialized_list {}; IEventInfo& i = const_cast(info); static_cast(i).SetError(error); static_cast(i).SetEventType(EventInfo::Error); impl_->sender_->Notify(info, serialized_list, info.GetOwner()); } int Reporter::Post(std::shared_ptr noti) { LOGI("Post noti"); static_pointer_cast(noti->GetInfo())->SetTime(time(NULL)); Bundle serialized = noti->Serialize(); EventInfo info( EventInfo::Post, util::GetAppId(), noti->GetChannel(), noti->GetId()); list serialized_list {serialized}; SharedFile::CopyPrivateFile(noti); impl_->sender_->SendMessage(info, serialized_list); return info.GetRequestId(); } int Reporter::Post(std::list> notiList) { LOGI("Post noti list"); EventInfo info(EventInfo::Post, util::GetAppId(), ""); list serialized_list; for (auto& i : notiList) { static_pointer_cast(i->GetInfo())->SetTime(time(NULL)); Bundle b = i->Serialize(); serialized_list.push_back(b); SharedFile::CopyPrivateFile(i); } impl_->sender_->SendMessage(info, serialized_list); return info.GetRequestId(); } int Reporter::Update(std::shared_ptr noti) { static_pointer_cast(noti->GetInfo())->SetTime(time(NULL)); Bundle serialized = noti->Serialize(); EventInfo info( EventInfo::Update, util::GetAppId(), noti->GetChannel(), noti->GetId()); list serialized_list {serialized}; SharedFile::CopyPrivateFile(noti); impl_->sender_->SendMessage(info, serialized_list); return info.GetRequestId(); } int Reporter::Update( std::list> notiList) { EventInfo info(EventInfo::Update, util::GetAppId(), ""); list serialized_list; for (auto& i : notiList) { static_pointer_cast(i->GetInfo())->SetTime(time(NULL)); Bundle b = i->Serialize(); serialized_list.push_back(b); SharedFile::CopyPrivateFile(i); } impl_->sender_->SendMessage(info, serialized_list); return info.GetRequestId(); } int Reporter::Delete(std::shared_ptr noti) { Bundle serialized = noti->Serialize(); EventInfo info( EventInfo::Delete, util::GetAppId(), noti->GetChannel(), noti->GetId()); list serialized_list {serialized}; impl_->sender_->SendMessage( info, serialized_list, noti->GetSenderAppId()); return info.GetRequestId(); } int Reporter::Delete( std::list> notiList) { EventInfo info(EventInfo::Delete, util::GetAppId(), ""); list serialized_list; for (auto& i : notiList) { static_pointer_cast(i->GetInfo())->SetTime(time(NULL)); Bundle b = i->Serialize(); serialized_list.push_back(b); SharedFile::CopyPrivateFile(i); } impl_->sender_->SendMessage(info, serialized_list); return info.GetRequestId(); } int Reporter::DeleteAll() { EventInfo info(EventInfo::DeleteAll, util::GetAppId(), ""); impl_->sender_->RequestReturnValue(info); return info.GetRequestId(); } int Reporter::DeleteByChannel(string channel) { EventInfo info(EventInfo::DeleteAll, util::GetAppId(), channel); impl_->sender_->RequestReturnValue(info); return info.GetRequestId(); } std::unique_ptr Reporter::FindByRootID(std::string id) { EventInfo info(EventInfo::Get, util::GetAppId(), "", id); list result = impl_->sender_->Request(info); Bundle b = result.front(); unique_ptr gen_item = ItemInflator::Create(b); return gen_item; } list> Reporter::FindByChannel(string channel) { EventInfo info(EventInfo::Get, util::GetAppId(), channel, ""); list result = impl_->sender_->Request(info); list> gen_item_list; for (auto& i : result) gen_item_list.push_back(ItemInflator::Create(i)); return gen_item_list; } list> Reporter::FindAll() { EventInfo info(EventInfo::Get, util::GetAppId(), "", ""); list result = impl_->sender_->Request(info); list> gen_item_list; for (auto& i : result) gen_item_list.push_back(ItemInflator::Create(i)); return gen_item_list; } int Reporter::GetCount(string channel) const { EventInfo info(EventInfo::Count, util::GetAppId(), channel); return impl_->sender_->RequestReturnValue(info); } int Reporter::SendEvent(const IEventInfo& info, std::list> notiList) { list serialized_list; for (auto& i : notiList) { Bundle b = i->Serialize(); serialized_list.push_back(b); } impl_->sender_->Notify(info, serialized_list); return info.GetRequestId(); } void Reporter::OnEvent(const IEventInfo& info, list serialized) { NotificationError error = (static_cast(info)).GetError(); list> item_list; if (info.GetEventType() == EventInfo::Error) { OnError(error, info.GetRequestId()); return; } else if (info.GetEventType() == EventInfo::DeleteAll) { OnEvent(info, item_list); return; } for (auto& i : serialized) { shared_ptr gen_item = ItemInflator::Create(i); item_list.emplace_back(gen_item); } OnEvent(info, item_list); } list> Reporter::OnRequestEvent( const IEventInfo& info) { return list>({}); } list Reporter::OnRequest(const IEventInfo& info) { list> item_list = OnRequestEvent(info); list serialized_list; for (auto& i : item_list) { serialized_list.push_back(i->Serialize()); } return serialized_list; } int Reporter::OnRequestNumber(const IEventInfo& info) { return 0; } void Reporter::OnEvent( const IEventInfo& info, list> notiList) { } void Reporter::OnError(NotificationError error, int requestId) { } string Reporter::GetPath() { return NOTIFICATION_EX_REPORTER_OBJECT_PATH; } void Reporter::OnRegister(const IEventInfo& info) { } } // namespace notification