tizen beta release
[framework/web/wrt-plugins-common.git] / src / modules / tizen / Messaging / Messaging.h
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  *
18  *
19  * @file       Messaging.h
20  * @author     Pawel Misiak (p.misiak@samsung.com)
21  * @version    0.1
22  * @brief
23  */
24 #ifndef MESSAGING_H
25 #define MESSAGING_H
26
27 #include <memory>
28 #include <vector>
29 #include <string>
30 #include <dpl/atomic.h>
31 #include <dpl/thread.h>
32 extern "C" {
33 #include <MsgTypes.h>
34 }
35 #include <dpl/event/event_listener.h>
36 #include <API/Messaging/IMessaging.h>
37 #include <API/Messaging/IMessage.h>
38 #include <API/Messaging/EmailAccountInfo.h>
39 #include <Commons/Emitters.h>
40 #include <DBus/Connection.h>
41 #include <DBus/MessageEvent.h>
42 #include "MsgServiceHandleMgr.h"
43 #include "MailNotifier.h"
44
45 namespace WrtDeviceApis {
46 namespace Messaging {
47 class Messaging : public Api::IMessaging,
48     private DPL::Event::EventListener<EmailReceivedEvent>
49 {
50   public:
51     static Messaging& getInstance();
52
53   public:
54     virtual ~Messaging();
55
56     virtual void getNumberOfMessages(Api::MessageType msgType,
57             Api::FolderType folder,
58             int* readed,
59             int* unReaded);
60
61     virtual std::vector<Api::IMessagePtr> findMessages(
62             const std::vector<Api::MessageType>& msgTypes,
63             Api::FolderType folder,
64             const Api::MessageFilterPtr& filter);
65
66     virtual std::vector<Api::IMessagePtr> findMessages(
67             const std::vector<Api::MessageType>& msgTypes,
68             const std::string &folder,
69             const Api::MessageFilterPtr& filter);
70
71     virtual std::vector<std::string> getMessageIds(Api::MessageType msgType,
72             Api::FolderType folder);
73
74     virtual void createFolder(Api::MessageType msgType,
75             const std::string& userFolder);
76
77     virtual void deleteFolder(Api::MessageType msgType,
78             const std::string& userFolder);
79
80     virtual std::vector<std::string> getFolderNames(Api::MessageType msgType);
81
82     virtual std::vector<Api::EmailAccountInfo> getEmailAccounts() const;
83
84     int getEmailAccountId(const std::string& account);
85
86     void fetchEmailHeaders();
87
88     static int convertFolderToPlatform(const Api::FolderType folder);
89
90     /**
91      * Gets folder id by given folder name.
92      * @return Folder id or 0 if not found
93      * @throw PlatformException on platform error.
94      */
95     static int convertFolderToPlatform(const std::string &folder);
96
97     void addOnMessageReceived(const Api::EmitterMessageReceivedPtr& emitter);
98
99     void removeOnMessageReceived(Api::EmitterMessageReceived::IdType id);
100
101   public:
102     static int m_currentEmailAccountId;
103
104     static DPL::Atomic m_objCounter;
105
106   protected:
107     void OnEventReceived(const EmailReceivedEvent& event);
108
109   private:
110     typedef Commons::Emitters<Api::EmitterMessageReceived> EmittersMessageReceived;
111
112     template<class MessageType>
113     struct MessageFilterMatcher
114     {
115       public:
116         MessageFilterMatcher(const MessageType& message) : m_message(message)
117         {
118         }
119
120         bool operator()(const EmittersMessageReceived::EmitterPtrType& emitter)
121         const
122         {
123             if (!m_message) {
124                 return false;
125             }
126             if (!emitter->getFilter()) {
127                 return true;
128             }
129             return emitter->getFilter()->compare(m_message);
130         }
131
132       private:
133         MessageType m_message;
134     };
135
136   private:
137     Messaging();
138
139     static void onSmsReceived(MSG_HANDLE_T handle,
140             msg_message_t msg,
141             void* data);
142     static void onMmsReceived(MSG_HANDLE_T handle,
143             msg_message_t msg,
144             void* data);
145
146   private:
147
148     void getNumberOfEmails(Api::FolderType folder,
149             int* read,
150             int* unread);
151
152     void getNumberOfSms(Api::FolderType folder,
153             int* read,
154             int* unread);
155
156     void getNumberOfMms(Api::FolderType folder,
157             int* read,
158             int* unread);
159
160     void getNumberOfSmsMms(Api::FolderType folder,
161             int* read,
162             int* unread,
163             Api::MessageType msgType);
164
165     virtual std::vector<Api::IMessagePtr> findSms(Api::FolderType folder,
166             const Api::MessageFilterPtr& filter);
167
168     virtual std::vector<Api::IMessagePtr> findMms(Api::FolderType folder,
169             const Api::MessageFilterPtr& filter);
170
171     virtual std::vector<Api::IMessagePtr> findEmail(Api::FolderType folder,
172             const Api::MessageFilterPtr& filter);
173
174     virtual std::vector<Api::IMessagePtr> findEmail(const std::string &folder,
175             const Api::MessageFilterPtr& filter);
176
177     std::vector<std::string> getSmsIds(Api::FolderType folder);
178
179     std::vector<std::string> getMmsIds(Api::FolderType folder);
180
181     std::vector<std::string> getEmailIds(Api::FolderType folder);
182
183     void createMsgServiceFolder(const std::string& userFolder);
184
185     void createEmailFolder(const std::string& userFolder);
186
187     void deleteMsgServiceFolder(const std::string& userFolder);
188
189     void deleteEmailFolder(const std::string& userFolder);
190
191     std::vector<std::string> getFolderNamesMsgService();
192
193     std::vector<std::string> getFolderNamesEmail();
194
195   private:
196     EmittersMessageReceived m_onMessageReceived;
197     MsgServiceHandleMgrPtr m_onMessageReceivedHandleMgr;
198     std::unique_ptr<MailNotifier> m_mailNotifier;
199 };
200 }
201 }
202
203 #endif