fix message appcontrol argument handling
[platform/framework/native/app-controls.git] / src / message-app-control / MessageAppControlDllEntry.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file         MessageAppControlDllEntry.cpp
20  * @brief       This is the implementation for the MessageAppControlDllEntry.cpp class.
21  */
22
23 #include <appsvc/appsvc.h>
24
25 #include <FBaseSysLog.h>
26 #include <FBaseUtilStringTokenizer.h>
27 #include <FBaseColHashMap.h>
28 #include <FAppAppControl.h>
29
30 #include <FBase_StringConverter.h>
31 #include <FApp_AppControlManager.h>
32 #include <FApp_AppMessageImpl.h>
33 #include <FApp_Aul.h>
34 #include <FApp_AppArg.h>
35
36 using namespace Tizen::App;
37 using namespace Tizen::Base;
38 using namespace Tizen::Base::Collection;
39 using namespace Tizen::Base::Utility;
40
41
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45
46 static const wchar_t APP_CONTROL_ATTACH_PATH[] = L"http://tizen.org/appcontrol/data/path";
47 static const wchar_t APP_CONTROL_MESSAGE_TYPE[] = L"http://tizen.org/appcontrol/data/message/type";
48
49
50 result _OSP_EXPORT_ StartAppControl(int req, const String&, const String&, const String*, const String*, const IMap*);
51 result _OSP_EXPORT_ TerminateAppControl(int req);
52 static void OnAppControlResult(void*, int, service_result_e, void*);
53
54 static int __processId = -1;
55
56 static const char __allowedAppControlMessageTable[][2][96] =
57 {
58         {"osp.appcontrol.MESSAGE", "osp.appcontrol.operation.EDIT"},
59         {"osp.appcontrol.provider.message", "osp.appcontrol.operation.compose"},
60         {"tizen.messages", "http://tizen.org/appcontrol/operation/compose"},
61         {"tizen.smsmessages", "http://tizen.org/appcontrol/operation/compose"},
62         {"tizen.mmsmessages", "http://tizen.org/appcontrol/operation/compose"},
63 };
64
65 static const char __allowedAppControlEmailTable[][2][96] =
66 {
67         {"osp.appcontrol.EMAIL", "osp.appcontrol.operation.EDIT"},
68         {"osp.appcontrol.provider.email", "osp.appcontrol.operation.compose"},
69         {"tizen.email", "http://tizen.org/appcontrol/operation/compose"},
70 };
71
72
73 result
74 StartAppControl(int req, const String& aId, const String& oId, const String* pUri, const String* pMime, const IMap* pMap)
75 {
76         SysLog(NID_APP, "StartAppControl: Entry to Message AppControl");
77
78         result r = E_SUCCESS;
79
80         SysLog(NID_APP, "aId : %ls, oId : %ls", aId.GetPointer(), oId.GetPointer());
81
82         const bool isMessage = _AppControlManager::IsAllowedAppControl(__allowedAppControlMessageTable, 5, aId, oId);
83         const bool isEmail = _AppControlManager::IsAllowedAppControl(__allowedAppControlEmailTable, 3, aId, oId);
84
85         SysTryReturnResult(NID_APP, isMessage || isEmail, E_SYSTEM, "Invalid AppControl entry for (%ls, %ls).", aId.GetPointer(), oId.GetPointer());
86
87         if (isMessage)
88         {
89                 SysLog(NID_APP, "Message AppControl starts..");
90
91                 _AppMessageImpl msg;
92                 msg.AddData(pMap);
93
94                 // type
95                 const String& type = msg.GetValue(APP_CONTROL_MESSAGE_TYPE);
96                 if (type.IsEmpty())
97                 {
98                         String legacyType = msg.GetValue(L"type");
99                         if (!legacyType.IsEmpty())
100                         {
101                                 legacyType.ToLowerCase();
102
103                                 msg.AddData(APP_CONTROL_MESSAGE_TYPE, legacyType);
104                         }
105                 }
106
107                 // to
108                 String tmp = msg.GetValue(L"to");
109                 if (!tmp.IsEmpty())
110                 {
111                         // cc
112                         String tmpCc = msg.GetValue(L"cc");
113                         if (!tmpCc.IsEmpty())
114                         {
115                                 tmp.Append(L';' + tmpCc);
116                         }
117
118                         // bcc
119                         String tmpBcc = msg.GetValue(L"bcc");
120                         if (!tmpBcc.IsEmpty())
121                         {
122                                 tmp.Append(L';' + tmpBcc);
123                         }
124
125                         tmp.Replace(L';', L',');
126                         msg.AddData(SERVICE_DATA_TO, tmp);
127                 }
128
129                 // subject
130                 tmp = msg.GetValue(L"subject");
131                 if (!tmp.IsEmpty())
132                 {
133                         SysLog(NID_APP, "subject: [%ls]", tmp.GetPointer());
134                         msg.AddData(SERVICE_DATA_SUBJECT, tmp);
135                 }
136
137                 // text
138                 tmp = msg.GetValue(L"text");
139                 if (!tmp.IsEmpty())
140                 {
141                         SysLog(NID_APP, "text: [%ls]", tmp.GetPointer());
142                         msg.AddData(SERVICE_DATA_TEXT, tmp);
143                 }
144
145                 const String& path = msg.GetValue(APP_CONTROL_ATTACH_PATH);
146                 if (path.IsEmpty())
147                 {
148                         ArrayList list;
149                         list.Construct();
150
151                         // vcard
152                         const String& tmpVcard = msg.GetValue(L"attachVcard");
153                         if (!tmpVcard.IsEmpty())
154                         {
155                                 list.Add(tmpVcard);
156                         }
157
158                         // vcalendar
159                         const String& tmpVcal = msg.GetValue(L"attachVcalendar");
160                         if (!tmpVcal.IsEmpty())
161                         {
162                                 list.Add(tmpVcal);
163                         }
164
165                         // video
166                         const String& tmpVideo = msg.GetValue(L"attachVideo");
167                         if (!tmpVideo.IsEmpty())
168                         {
169                                 list.Add(tmpVideo);
170                         }
171
172                         // image
173                         const String& tmpImage = msg.GetValue(L"attachImage");
174                         if (!tmpImage.IsEmpty())
175                         {
176                                 list.Add(tmpImage);
177                         }
178
179                         // audio
180                         const String& tmpAudio = msg.GetValue(L"attachAudio");
181                         if (!tmpAudio.IsEmpty())
182                         {
183                                 list.Add(tmpAudio);
184                         }
185
186                         if (list.GetCount() != 0)
187                         {
188                                 SysLog(NID_APP, "Attaching %d items.", list.GetCount());
189
190                                 _AppMessageImpl::AddValueArray(msg.GetBundle(), APP_CONTROL_ATTACH_PATH, &list);
191                                 //msg.AddValueArray(APP_CONTROL_ATTACH_PATH, &list);
192                         }
193                 }
194
195                 const String& package = _AppControlManager::GetAliasAppId(aId);
196                 __processId = _AppControlManager::GetInstance()->Launch(msg, package, oId, pUri, pMime, NULL, reinterpret_cast<void*>(req));
197
198                 SysTryReturnResult(NID_APP, __processId >= 0, E_SYSTEM, "StartAppControl: Launching Email AppControl is failed.");
199                 SysLog(NID_APP, "StartAppControl: Launching Message AppControl succeeded.");
200
201         }
202         if (isEmail)
203         {
204                 SysLog(NID_APP, "Email AppControl starts..");
205
206                 _AppMessageImpl msg;
207                 msg.AddData(pMap);
208
209                 String tmp = msg.GetValue(L"to");
210                 if (!tmp.IsEmpty())
211                 {
212                         msg.AddData(SERVICE_DATA_TO, tmp);
213                 }
214
215                 tmp = msg.GetValue(L"cc");
216                 if (!tmp.IsEmpty())
217                 {
218                         msg.AddData(SERVICE_DATA_CC, tmp);
219                 }
220
221                 tmp = msg.GetValue(L"bcc");
222                 if (!tmp.IsEmpty())
223                 {
224                         msg.AddData(SERVICE_DATA_BCC, tmp);
225                 }
226
227                 tmp = msg.GetValue(L"subject");
228                 if (!tmp.IsEmpty())
229                 {
230                         msg.AddData(SERVICE_DATA_SUBJECT, tmp);
231                 }
232
233                 tmp = msg.GetValue(L"text");
234                 if (!tmp.IsEmpty())
235                 {
236                         msg.AddData(SERVICE_DATA_TEXT, tmp);
237                 }
238
239                 tmp = msg.GetValue(L"attachments");
240                 if (!tmp.IsEmpty())
241                 {
242                         const String& path = msg.GetValue(APP_CONTROL_ATTACH_PATH);
243                         // if path is delivered, then ignore "attachments" data
244                         if (path.IsEmpty())
245                         {
246                                 const String delim = L";";
247                                 StringTokenizer strTok(tmp, delim);
248
249                                 const int count = strTok.GetTokenCount();
250                                 if (count > 0)
251                                 {
252                                         ArrayList list(SingleObjectDeleter);
253                                         list.Construct();
254
255                                         String token;
256                                         while (strTok.HasMoreTokens())
257                                         {
258                                                 strTok.GetNextToken(token);
259                                                 list.Add(new (std::nothrow) String(token));
260                                         }
261
262                                         _AppMessageImpl::AddValueArray(msg.GetBundle(), APP_CONTROL_ATTACH_PATH, &list);
263                                         //msg.AddValueArray(APP_CONTROL_ATTACH_PATH, &list);
264                                 }
265                         }
266                 }
267
268                 const String& package = _AppControlManager::GetAliasAppId(aId);
269                 __processId = _AppControlManager::GetInstance()->Launch(msg, package, oId, pUri, pMime, OnAppControlResult, reinterpret_cast<void*>(req));
270
271                 SysTryReturnResult(NID_APP, __processId >= 0, E_SYSTEM, "StartAppControl: Launching Email AppControl is failed.");
272                 SysLog(NID_APP, "StartAppControl: Launching Email AppControl succeeded.");
273         }
274
275         return r;
276 }
277
278 result
279 TerminateAppControl(int req)
280 {
281         if (__processId >= 0)
282         {
283                 _Aul::TerminateApplicationByPid(__processId);           
284         }
285         return E_SUCCESS;
286 }
287
288 static void
289 OnAppControlResult(void* b, int requestCode, service_result_e res, void* userData)
290 {
291         SysLog(NID_APP, "OnAppControlResult with %d.", res);
292
293         result r = E_SYSTEM;
294         int req = -1;
295         bundle* pBundle = static_cast<bundle*>(b);
296
297         HashMap* pResult = new (std::nothrow) HashMap();
298         SysTryCatch(NID_APP, pResult != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Allocation failure.");
299
300         r = pResult->Construct();
301
302         _AppArg::SetArgMap(pBundle, pResult);
303
304         req = reinterpret_cast<int>(userData);
305         _AppControlManager::GetInstance()->FinishAppControl(req, res, pResult);
306
307 CATCH:
308         __processId = -1;
309 }
310
311
312 #ifdef __cplusplus
313 }
314 #endif