Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / platform / Tizen / Messaging / MessagingServiceManager.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
18 /*
19  * @author      Wojciech Bielawski (w.bielawski@samsung.com)
20  */
21
22 #include <cassert>
23 #include <Commons/Exception.h>
24 #include <API/Messaging/EventGetMessagingService.h>
25 #include "MessagingServiceManager.h"
26 #include "MessagingService.h"
27
28 //for email service
29 #include <emf-types.h>
30 #include <Emf_Mapi.h>
31
32 using namespace std;
33 using namespace DPL;
34 using namespace TizenApis::Api::Messaging;
35
36 namespace TizenApis {
37 namespace Platform {
38 namespace Messaging {   
39
40 MessagingServiceManager::MessagingServiceManager() :
41         m_initialized(false), 
42         messaging_service_handler(-1) 
43 {
44     LogDebug("Enter");
45
46 }
47
48 MessagingServiceManager::~MessagingServiceManager() {
49         LogDebug("Leave");
50 }
51
52 std::vector<Api::Messaging::EmailAccountInfo> MessagingServiceManager::getEmailAccounts()
53 {
54         emf_account_t* accounts = NULL;
55         int count = 0;
56         std::vector<Api::Messaging::EmailAccountInfo> result;
57         
58         Try {
59                 if (email_get_account_list(&accounts, &count)) {
60                         if (0 < count)
61                         {
62                                 
63                                 for (int i = 0; i < count; ++i) {
64                                         LogDebug("account ID :" << accounts[i].account_id << "Name :" << accounts[i].user_name << "Addr :" << accounts[i].email_addr );
65                                         Api::Messaging::EmailAccountInfo account(accounts[i].account_id,
66                                                                   accounts[i].user_name,
67                                                                   accounts[i].email_addr );
68                                         result.push_back(account);
69                                 }
70
71                                 if (accounts != NULL) {
72                                     email_free_account(&accounts, count);
73                                 }
74
75                         }
76                 }               
77         }
78         Catch(WrtDeviceApis::Commons::PlatformException) {
79
80                 if (accounts != NULL) {
81                         email_free_account(&accounts, count);
82                 }
83       }
84                 
85         return result;
86 }
87
88 //public function.
89 void MessagingServiceManager::getMessagingServiceManager(const EventGetMessagingServicePtr& event) {
90         LogDebug("<<<");
91
92         LogDebug("event->getAccountID():" << event->getAccountID());
93
94         if (m_initialized == false) {
95                 initialize();
96         }
97
98         EventRequestReceiver<EventGetMessagingService>::PostRequest(event);
99
100         LogDebug(">>>");
101 }
102
103 //get the request which posted from the standard layer
104 void MessagingServiceManager::OnRequestReceived(const EventGetMessagingServicePtr& event)
105 {
106         LogDebug("<<<");
107         
108         try 
109         {
110                 m_messagingServices.clear();    //clear services
111                 if (m_messagingServices.empty())
112                 {
113                         //ToDo: The Message Service Code will be inserted here.
114                         LogDebug("Message Service Type : " << event->getMessagingServiceType());
115                         if ( event->getMessagingServiceType() == Api::Messaging::SMS)
116                         {
117                                 MessagingService* messagingService = new MessagingService();
118                                 messagingService->setMessagingServiceType(Api::Messaging::SMS);
119                                 messagingService->setMessagingServiceName("messaging.sms");
120                                 
121                                 //ToDol: set messaging Service instance.
122                                 m_messagingServices.push_back(Api::Messaging::IMessagingServicePtr(messagingService));
123                         }
124                         else if ( event->getMessagingServiceType() == Api::Messaging::MMS)
125                         {
126                                 MessagingService* messagingService = new MessagingService();
127                                 messagingService->setMessagingServiceType(Api::Messaging::MMS);
128                                 messagingService->setMessagingServiceName("messaging.mms");
129                                 
130                                 m_messagingServices.push_back(Api::Messaging::IMessagingServicePtr(messagingService));
131                         }
132                         else if (event->getMessagingServiceType() == Api::Messaging::EMAIL)
133                         {       
134                                 std::vector<Api::Messaging::EmailAccountInfo> EmailAccounts = getEmailAccounts();
135                                 LogDebug("get Account list, size:" << EmailAccounts.size());
136                                 
137                                 int idx = 0;
138                                 for ( idx = 0 ; idx < (int)EmailAccounts.size(); idx++)
139                                 {
140                                         LogDebug("Account ID:" << EmailAccounts[idx].getIntId() );
141
142                                         MessagingService* messagingService = NULL;
143                                         messagingService = new MessagingService();
144                                         messagingService->setMessagingServiceType(Api::Messaging::EMAIL);
145                                         messagingService->setCurrentEmailAccount( EmailAccounts[idx] );
146
147                                         LogDebug("Account ID:" << event->getAccountID());
148                                         if ( event->getAccountID() >= 0 )
149                                         {
150                                                 if (event->getAccountID() == EmailAccounts[idx].getIntId())
151                                                         m_messagingServices.push_back(Api::Messaging::IMessagingServicePtr(messagingService));
152                                                 else
153                                                         delete messagingService;        //remove service.
154                                         }
155                                         else 
156                                         {
157                                                 m_messagingServices.push_back(Api::Messaging::IMessagingServicePtr(messagingService));
158                                         }
159                                         
160                                         idx++;                                  
161                                 }
162                                                                                         
163                         }
164
165                 }
166                 
167                 event->setMessagingServicesRef(m_messagingServices);    //set message services
168         }
169         catch (WrtDeviceApis::Commons::Exception &exc)
170         {
171                 LogError("Exception has occurred: " << std::hex << exc.getCode());
172         event->setExceptionCode(exc.getCode());
173         }
174
175         LogDebug(">>>");
176 }
177
178 // create platform's resources. For examples, it could be camera handle, location handle and so on.
179
180 void MessagingServiceManager::initialize() {
181         LogDebug("<<<");
182
183         if (!m_initialized) {
184         //              retVal = create_messaging_service();
185         //              if(retVal != MessagingServiceManager_ERROR_NONE){
186         //                      ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Couldn't init MEssagingServiceManager module.");
187         //              }
188                         m_initialized = true;
189         }
190         
191         LogDebug(">>>");
192 }
193
194 #if 0
195 void MessagingServiceManager::getEmailAccountList( vector<Api::Messaging::EmailAccountInfo>& accounts )
196 {
197         LogDebug("<<<");
198
199         emf_account_t* emf_accounts = NULL;
200         int count = 0;
201         Try {
202                 if (!email_get_account_list(&emf_accounts, &count)) {
203                         ThrowMsg(WrtDeviceApis::Commons::PlatformException,
204                         "Couldn't get e-mail accounts.");
205                 }
206
207                 if (0 == count) {
208                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "No e-mail account found.");
209                 }
210         
211                 for (int i = 0; i < count; ++i) {
212                         //make email account info.
213
214                         LogDebug("account id :" << emf_accounts[i].account_id );
215                         LogDebug("account name :" << emf_accounts[i].user_name );
216                         LogDebug("account addr :" << emf_accounts[i].email_addr );
217
218                         Api::Messaging::EmailAccountInfo account(emf_accounts[i].account_id,
219                                                    emf_accounts[i].user_name,
220                                              emf_accounts[i].email_addr);
221                         if (std::find(accounts.begin(), accounts.end(), account) == accounts.end()) {
222                                 LogDebug( "push " << emf_accounts[i].account_id <<  "account" );
223                                 accounts.push_back(account);
224                         }
225                 }
226
227                 if (emf_accounts != NULL) {
228                         email_free_account(&emf_accounts, count);       //free
229                 }
230         }
231         Catch(WrtDeviceApis::Commons::PlatformException) {
232                 if (emf_accounts != NULL) {
233                         email_free_account(&emf_accounts, count);
234                 }
235                 throw;
236         }
237         
238         LogDebug(">>>");
239 }
240 #endif
241
242 }
243 }       //namespace Platform
244 }       //namespace WrtPlugins