tizen beta release
[framework/web/wrt-plugins-common.git] / src / modules / tizen / Messaging / MailNotifier.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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  * @file   MailNotifier.cpp
18  * @author Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
19  */
20
21 #include <string>
22 #include <emf-types.h>
23 #include <Commons/ThreadPool.h>
24 #include "MailNotifier.h"
25 #include "StorageChangeMessage.h"
26 #include "NetworkStatus.h"
27
28 namespace {
29 const std::string DBUS_INTERFACE_STORAGE_CHANGE = "User.Email.StorageChange";
30 const std::string DBUS_FILTER_STORAGE_CHANGE =
31     "type='signal',interface='" + DBUS_INTERFACE_STORAGE_CHANGE + "'";
32 }
33
34 namespace WrtDeviceApis {
35 namespace Messaging {
36
37 MailNotifier::MailNotifier() : m_connection(new DBus::Connection())
38 {
39     m_connection->AddListener(this);
40     m_connection->addFilter(DBUS_FILTER_STORAGE_CHANGE);
41     m_connection->open(DBUS_BUS_SYSTEM);
42 }
43
44 MailNotifier::~MailNotifier()
45 {
46     m_connection->RemoveListener(this);
47     m_connection->close();
48 }
49
50 void MailNotifier::setWorkerThread(DPL::Thread* thread)
51 {
52     m_connection->SwitchListenerToThread(this, thread);
53 }
54
55 void MailNotifier::OnEventReceived(const DBus::MessageEvent& event)
56 {
57     LogDebug("ENTER");
58     DBus::MessagePtr message = event.GetArg0();
59     if (message->getInterface() == DBUS_INTERFACE_STORAGE_CHANGE)
60     {
61         onStorageChange(message);
62     }
63 }
64
65 void MailNotifier::onStorageChange(const DBus::MessagePtr& message)
66 {
67     LogDebug("ENTER");
68     StorageChangeMessage sc = StorageChangeMessage::parse(message);
69     int mailId = sc.getMailId();
70     switch (sc.getType())
71     {
72     case NOTI_MAIL_ADD:
73         LogDebug("NOTI_MAIL_ADD [mail id = " << mailId << "]");
74         m_incomming.push_back(mailId);
75         break;
76
77     case NOTI_MAIL_UPDATE:
78         LogDebug("NOTI_MAIL_UPDATE [mail id: " << mailId << "]");
79         if (sc.getExtra() == UPDATE_PARTIAL_BODY_DOWNLOAD)
80         {
81             auto it = std::find(m_incomming.begin(), m_incomming.end(), mailId);
82             if (m_incomming.end() != it)
83             {
84                 m_incomming.erase(it);
85                 DPL::Event::EventSupport<EmailReceivedEvent>::EmitEvent(
86                         EmailReceivedEvent(mailId));
87             }
88         }
89         break;
90     }
91 }
92
93 }
94 }