merge with master
[platform/framework/web/wrt-plugins-common.git] / src / modules / tizen / DEPRACATED / AppLauncher / AppLauncherManager.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        AppLauncherManager.cpp
18  * @author      Lukasz Marek (l.marek@samsung.com)
19  * @author      Yujie Zeng (yj.zeng@samsung.com)
20  * @version     0.1
21  */
22
23 #include <algorithm>
24 #include <vector>
25 #include <pcrecpp.h>
26 #include <menu_db.h>
27 #include <aul.h>
28 #include <bundle.h>
29 #include <dpl/scoped_free.h>
30 #include <dpl/log/log.h>
31 #include <commons/StringUtils.h>
32 #include <Filesystem/Manager.h>
33 #include "AppLauncherManager.h"
34
35 using namespace WrtPlugins::Api;
36 using namespace WrtPlugins::Api::AppLauncher;
37
38 namespace {
39 const char* EMPTY_STRING_PATTERN = "^\\s*$";
40 const char* EMAIL_PATTERN = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+$";
41 //According to RFC1738,any character is possible in http address
42 const char* HTTP_ADDRESS_PATTERN = "^(http(s)?://)?\\w+.*$";
43 const char* FILE_ADDRESS_PATTERN = "^.*\\..*$";
44
45 const char* APPLICATION_ALARM = "org.tizen.clock";      // alarm has been
46                                                         // included in clock app
47 const char* APPLICATION_BROWSER = "org.tizen.browser";
48 const char* APPLICATION_CALCULATOR = "org.tizen.calculator";
49 const char* APPLICATION_CALENDAR = "org.tizen.efl-calendar";
50 const char* APPLICATION_CAMERA = "org.tizen.camera-app";
51 const char* APPLICATION_CONTACTS = "org.tizen.contacts";
52 const char* APPLICATION_FILES = "org.tizen.myfile";
53 const char* APPLICATION_GAMES = "";
54 const char* APPLICATION_MAIL = "org.tizen.email";
55 const char* APPLICATION_MEDIAPLAYER = "org.tizen.music-player";
56 const char* APPLICATION_MESSAGING = "org.tizen.message";
57 const char* APPLICATION_PHONECALL = "org.tizen.dialer";
58 const char* APPLICATION_PHONECALL_CALLING = "org.tizen.call";
59 const char* APPLICATION_PICTURES = "org.tizen.gallery";
60 const char* APPLICATION_PICTURES_IMG_PATH = "org.tizen.image-viewer";
61 const char* APPLICATION_PROG_MANAGER = "org.tizen.taskmgr";
62 const char* APPLICATION_SETTINGS = "org.tizen.setting";
63 const char* APPLICATION_TASKS = "org.tizen.dailybriefing";
64 const char* APPLICATION_WIDGET_MANAGER = "org.tizen.wrt-test-ui";
65 const char* APPLICATION_WIDGET = "org.tizen.";
66 const char* APPLICATION_EMPTY = "";
67
68 const char* WIDGET_URI_PREFIX = "widget://";
69
70 const int SUCCESS_LAUNCHER = 0;
71 const int ERROR_LAUNCHER_INVALID_URI = 1;
72 const int ERROR_LAUNCHER_INVALID_PARAMETER = 2;
73 const int ERROR_LAUNCHER_EXECUTION_ERROR = 3;
74 }
75
76 namespace WrtPlugins {
77 namespace Platform {
78 std::map<Api::AppLauncher::ApplicationType,
79          std::string> AppLauncherManager::m_applicationTypesByPath;
80 std::map<Api::AppLauncher::ApplicationType,
81          std::string> AppLauncherManager::m_applicationTypesByName;
82 std::vector<std::string> AppLauncherManager::m_installedApplications;
83 std::vector<std::string> AppLauncherManager::m_installedApplicationsNames;
84
85 AppLauncherManager::AppLauncherManager()
86 {
87     LogDebug("entered");
88     static bool initialized = initializeApplicationList();
89     (void) initialized;
90 }
91
92 AppLauncherManager::~AppLauncherManager()
93 {
94     LogDebug("entered");
95 }
96
97 bool AppLauncherManager::initializeApplicationList()
98 {
99     //application paths
100     m_applicationTypesByPath.insert(std::pair<ApplicationType,
101                                               std::string>(
102                                         APPLICATION_TYPE_ALARM,
103                                         APPLICATION_EMPTY));
104     m_applicationTypesByPath.insert(std::pair<ApplicationType,
105                                               std::string>(
106                                         APPLICATION_TYPE_BROWSER,
107                                         APPLICATION_EMPTY));
108     m_applicationTypesByPath.insert(std::pair<ApplicationType,
109                                               std::string>(
110                                         APPLICATION_TYPE_CALCULATOR,
111                                         APPLICATION_EMPTY));
112     m_applicationTypesByPath.insert(std::pair<ApplicationType,
113                                               std::string>(
114                                         APPLICATION_TYPE_CALENDAR,
115                                         APPLICATION_EMPTY));
116     m_applicationTypesByPath.insert(std::pair<ApplicationType,
117                                               std::string>(
118                                         APPLICATION_TYPE_CAMERA,
119                                         APPLICATION_EMPTY));
120     m_applicationTypesByPath.insert(std::pair<ApplicationType,
121                                               std::string>(
122                                         APPLICATION_TYPE_CONTACTS,
123                                         APPLICATION_EMPTY));
124     m_applicationTypesByPath.insert(std::pair<ApplicationType,
125                                               std::string>(
126                                         APPLICATION_TYPE_FILES,
127                                         APPLICATION_EMPTY));
128     m_applicationTypesByPath.insert(std::pair<ApplicationType,
129                                               std::string>(
130                                         APPLICATION_TYPE_GAMES,
131                                         APPLICATION_EMPTY));
132     m_applicationTypesByPath.insert(std::pair<ApplicationType,
133                                               std::string>(
134                                         APPLICATION_TYPE_MAIL,
135                                         APPLICATION_EMPTY));
136     m_applicationTypesByPath.insert(std::pair<ApplicationType,
137                                               std::string>(
138                                         APPLICATION_TYPE_MEDIAPLAYER,
139                                         APPLICATION_EMPTY));
140     m_applicationTypesByPath.insert(std::pair<ApplicationType,
141                                               std::string>(
142                                         APPLICATION_TYPE_MESSAGING,
143                                         APPLICATION_EMPTY));
144     m_applicationTypesByPath.insert(std::pair<ApplicationType,
145                                               std::string>(
146                                         APPLICATION_TYPE_PHONECALL,
147                                         APPLICATION_EMPTY));
148     m_applicationTypesByPath.insert(std::pair<ApplicationType,
149                                               std::string>(
150                                         APPLICATION_TYPE_PICTURES,
151                                         APPLICATION_EMPTY));
152     m_applicationTypesByPath.insert(std::pair<ApplicationType,
153                                               std::string>(
154                                         APPLICATION_TYPE_PROG_MANAGER,
155                                         APPLICATION_EMPTY));
156     m_applicationTypesByPath.insert(std::pair<ApplicationType,
157                                               std::string>(
158                                         APPLICATION_TYPE_SETTINGS,
159                                         APPLICATION_EMPTY));
160     m_applicationTypesByPath.insert(std::pair<ApplicationType,
161                                               std::string>(
162                                         APPLICATION_TYPE_TASKS,
163                                         APPLICATION_EMPTY));
164     m_applicationTypesByPath.insert(std::pair<ApplicationType,
165                                               std::string>(
166                                         APPLICATION_TYPE_WIDGET_MANAGER,
167                                         APPLICATION_EMPTY));
168     //application names
169     m_applicationTypesByName.insert(std::pair<ApplicationType,
170                                               std::string>(
171                                         APPLICATION_TYPE_ALARM,
172                                         APPLICATION_EMPTY));
173     m_applicationTypesByName.insert(std::pair<ApplicationType,
174                                               std::string>(
175                                         APPLICATION_TYPE_BROWSER,
176                                         APPLICATION_EMPTY));
177     m_applicationTypesByName.insert(std::pair<ApplicationType,
178                                               std::string>(
179                                         APPLICATION_TYPE_CALCULATOR,
180                                         APPLICATION_EMPTY));
181     m_applicationTypesByName.insert(std::pair<ApplicationType,
182                                               std::string>(
183                                         APPLICATION_TYPE_CALENDAR,
184                                         APPLICATION_EMPTY));
185     m_applicationTypesByName.insert(std::pair<ApplicationType,
186                                               std::string>(
187                                         APPLICATION_TYPE_CAMERA,
188                                         APPLICATION_EMPTY));
189     m_applicationTypesByName.insert(std::pair<ApplicationType,
190                                               std::string>(
191                                         APPLICATION_TYPE_CONTACTS,
192                                         APPLICATION_EMPTY));
193     m_applicationTypesByName.insert(std::pair<ApplicationType,
194                                               std::string>(
195                                         APPLICATION_TYPE_FILES,
196                                         APPLICATION_EMPTY));
197     m_applicationTypesByName.insert(std::pair<ApplicationType,
198                                               std::string>(
199                                         APPLICATION_TYPE_GAMES,
200                                         APPLICATION_EMPTY));
201     m_applicationTypesByName.insert(std::pair<ApplicationType,
202                                               std::string>(
203                                         APPLICATION_TYPE_MAIL,
204                                         APPLICATION_EMPTY));
205     m_applicationTypesByName.insert(std::pair<ApplicationType,
206                                               std::string>(
207                                         APPLICATION_TYPE_MEDIAPLAYER,
208                                         APPLICATION_EMPTY));
209     m_applicationTypesByName.insert(std::pair<ApplicationType,
210                                               std::string>(
211                                         APPLICATION_TYPE_MESSAGING,
212                                         APPLICATION_EMPTY));
213     m_applicationTypesByName.insert(std::pair<ApplicationType,
214                                               std::string>(
215                                         APPLICATION_TYPE_PHONECALL,
216                                         APPLICATION_EMPTY));
217     m_applicationTypesByName.insert(std::pair<ApplicationType,
218                                               std::string>(
219                                         APPLICATION_TYPE_PICTURES,
220                                         APPLICATION_EMPTY));
221     m_applicationTypesByName.insert(std::pair<ApplicationType,
222                                               std::string>(
223                                         APPLICATION_TYPE_PROG_MANAGER,
224                                         APPLICATION_EMPTY));
225     m_applicationTypesByName.insert(std::pair<ApplicationType,
226                                               std::string>(
227                                         APPLICATION_TYPE_SETTINGS,
228                                         APPLICATION_EMPTY));
229     m_applicationTypesByName.insert(std::pair<ApplicationType,
230                                               std::string>(
231                                         APPLICATION_TYPE_TASKS,
232                                         APPLICATION_EMPTY));
233     m_applicationTypesByName.insert(std::pair<ApplicationType,
234                                               std::string>(
235                                         APPLICATION_TYPE_WIDGET_MANAGER,
236                                         APPLICATION_EMPTY));
237
238     bool registered_application = false; // A variable that gets set if the
239                                          // current pkg_name is needed by WAC
240                                          // spec, needless of weather the
241                                          // package is visible or not.
242     app_info info = {};
243     unsigned int count = record_count(COUNT_MENU, &info);
244     LogDebug("number of registered applications: " << count);
245
246     DPL::ScopedFree<app_info> infoAll(static_cast<app_info*>(calloc(
247                                                                  count,
248                                                                  sizeof(
249                                                                      app_info))));
250     if (infoAll.Get()) {
251         if (!record_get(GET_MENU, infoAll.Get())) {
252             LogError("error during executing record_get() function");
253             return false;
254         }
255         for (unsigned int i = 0; i < count; i++) {
256             LogDebug("pkg_name " << infoAll.Get()[i].pkg_name);
257             LogDebug("app_path " << infoAll.Get()[i].app_path);
258             LogDebug("app_name " << infoAll.Get()[i].app_name);
259             registered_application = false;
260             if (infoAll.Get()[i].app_path == NULL ||
261                 infoAll.Get()[i].app_name == NULL)
262             {
263                 LogDebug("Not Registered.");
264                 LogDebug("-----");
265                 continue;
266             }
267             if (0 ==
268                 strncmp(APPLICATION_ALARM, infoAll.Get()[i].pkg_name,
269                         NAME_LEN))
270             {
271                 m_applicationTypesByPath[APPLICATION_TYPE_ALARM] =
272                     infoAll.Get()[i].app_path;
273                 m_applicationTypesByName[APPLICATION_TYPE_ALARM] =
274                     infoAll.Get()[i].app_name;
275                 registered_application = true;
276             }
277             if (0 ==
278                 strncmp(APPLICATION_BROWSER, infoAll.Get()[i].pkg_name,
279                         NAME_LEN))
280             {
281                 m_applicationTypesByPath[APPLICATION_TYPE_BROWSER] =
282                     infoAll.Get()[i].app_path;
283                 m_applicationTypesByName[APPLICATION_TYPE_BROWSER] =
284                     infoAll.Get()[i].app_name;
285                 registered_application = true;
286             }
287             if (0 ==
288                 strncmp(APPLICATION_CALCULATOR, infoAll.Get()[i].pkg_name,
289                         NAME_LEN))
290             {
291                 m_applicationTypesByPath[APPLICATION_TYPE_CALCULATOR] =
292                     infoAll.Get()[i].app_path;
293                 m_applicationTypesByName[APPLICATION_TYPE_CALCULATOR] =
294                     infoAll.Get()[i].app_name;
295                 registered_application = true;
296             }
297             if (0 ==
298                 strncmp(APPLICATION_CALENDAR, infoAll.Get()[i].pkg_name,
299                         NAME_LEN))
300             {
301                 m_applicationTypesByPath[APPLICATION_TYPE_CALENDAR] =
302                     infoAll.Get()[i].app_path;
303                 m_applicationTypesByName[APPLICATION_TYPE_CALENDAR] =
304                     infoAll.Get()[i].app_name;
305                 registered_application = true;
306             }
307             if (0 ==
308                 strncmp(APPLICATION_CAMERA, infoAll.Get()[i].pkg_name,
309                         NAME_LEN))
310             {
311                 m_applicationTypesByPath[APPLICATION_TYPE_CAMERA] =
312                     infoAll.Get()[i].app_path;
313                 m_applicationTypesByName[APPLICATION_TYPE_CAMERA] =
314                     infoAll.Get()[i].app_name;
315                 registered_application = true;
316             }
317             if (0 ==
318                 strncmp(APPLICATION_CONTACTS, infoAll.Get()[i].pkg_name,
319                         NAME_LEN))
320             {
321                 m_applicationTypesByPath[APPLICATION_TYPE_CONTACTS] =
322                     infoAll.Get()[i].app_path;
323                 m_applicationTypesByName[APPLICATION_TYPE_CONTACTS] =
324                     infoAll.Get()[i].app_name;
325                 registered_application = true;
326             }
327             if (0 ==
328                 strncmp(APPLICATION_FILES, infoAll.Get()[i].pkg_name,
329                         NAME_LEN))
330             {
331                 m_applicationTypesByPath[APPLICATION_TYPE_FILES] =
332                     infoAll.Get()[i].app_path;
333                 m_applicationTypesByName[APPLICATION_TYPE_FILES] =
334                     infoAll.Get()[i].app_name;
335                 registered_application = true;
336             }
337             if (0 ==
338                 strncmp(APPLICATION_GAMES, infoAll.Get()[i].pkg_name,
339                         NAME_LEN))
340             {
341                 m_applicationTypesByPath[APPLICATION_TYPE_GAMES] =
342                     infoAll.Get()[i].app_path;
343                 m_applicationTypesByName[APPLICATION_TYPE_GAMES] =
344                     infoAll.Get()[i].app_name;
345                 registered_application = true;
346             }
347             if (0 ==
348                 strncmp(APPLICATION_MAIL, infoAll.Get()[i].pkg_name,
349                         NAME_LEN))
350             {
351                 m_applicationTypesByPath[APPLICATION_TYPE_MAIL] =
352                     infoAll.Get()[i].app_path;
353                 m_applicationTypesByName[APPLICATION_TYPE_MAIL] =
354                     infoAll.Get()[i].app_name;
355                 registered_application = true;
356             }
357             if (0 ==
358                 strncmp(APPLICATION_MEDIAPLAYER, infoAll.Get()[i].pkg_name,
359                         NAME_LEN))
360             {
361                 m_applicationTypesByPath[APPLICATION_TYPE_MEDIAPLAYER] =
362                     infoAll.Get()[i].app_path;
363                 m_applicationTypesByName[APPLICATION_TYPE_MEDIAPLAYER] =
364                     infoAll.Get()[i].app_name;
365                 registered_application = true;
366             }
367             if (0 ==
368                 strncmp(APPLICATION_MESSAGING, infoAll.Get()[i].pkg_name,
369                         NAME_LEN))
370             {
371                 m_applicationTypesByPath[APPLICATION_TYPE_MESSAGING] =
372                     infoAll.Get()[i].app_path;
373                 m_applicationTypesByName[APPLICATION_TYPE_MESSAGING] =
374                     infoAll.Get()[i].app_name;
375                 registered_application = true;
376             }
377             if (0 ==
378                 strncmp(APPLICATION_PHONECALL, infoAll.Get()[i].pkg_name,
379                         NAME_LEN))
380             {
381                 m_applicationTypesByPath[APPLICATION_TYPE_PHONECALL] =
382                     infoAll.Get()[i].app_path;
383                 m_applicationTypesByName[APPLICATION_TYPE_PHONECALL] =
384                     infoAll.Get()[i].app_name;
385                 registered_application = true;
386             }
387             if (0 ==
388                 strncmp(APPLICATION_PICTURES, infoAll.Get()[i].pkg_name,
389                         NAME_LEN))
390             {
391                 m_applicationTypesByPath[APPLICATION_TYPE_PICTURES] =
392                     infoAll.Get()[i].app_path;
393                 m_applicationTypesByName[APPLICATION_TYPE_PICTURES] =
394                     infoAll.Get()[i].app_name;
395                 registered_application = true;
396             }
397             if (0 ==
398                 strncmp(APPLICATION_PROG_MANAGER, infoAll.Get()[i].pkg_name,
399                         NAME_LEN))
400             {
401                 m_applicationTypesByPath[APPLICATION_TYPE_PROG_MANAGER] =
402                     infoAll.Get()[i].app_path;
403                 m_applicationTypesByName[APPLICATION_TYPE_PROG_MANAGER] =
404                     infoAll.Get()[i].app_name;
405                 registered_application = true;
406             }
407             if (0 ==
408                 strncmp(APPLICATION_SETTINGS, infoAll.Get()[i].pkg_name,
409                         NAME_LEN))
410             {
411                 m_applicationTypesByPath[APPLICATION_TYPE_SETTINGS] =
412                     infoAll.Get()[i].app_path;
413                 m_applicationTypesByName[APPLICATION_TYPE_SETTINGS] =
414                     infoAll.Get()[i].app_name;
415                 registered_application = true;
416             }
417             if (0 ==
418                 strncmp(APPLICATION_TASKS, infoAll.Get()[i].pkg_name,
419                         NAME_LEN))
420             {
421                 m_applicationTypesByPath[APPLICATION_TYPE_TASKS] =
422                     infoAll.Get()[i].app_path;
423                 m_applicationTypesByName[APPLICATION_TYPE_TASKS] =
424                     infoAll.Get()[i].app_name;
425                 registered_application = true;
426             }
427             if (0 ==
428                 strncmp(APPLICATION_WIDGET_MANAGER, infoAll.Get()[i].pkg_name,
429                         NAME_LEN))
430             {
431                 m_applicationTypesByPath[APPLICATION_TYPE_WIDGET_MANAGER] =
432                     infoAll.Get()[i].app_path;
433                 m_applicationTypesByName[APPLICATION_TYPE_WIDGET_MANAGER] =
434                     infoAll.Get()[i].app_name;
435                 registered_application = true;
436             }
437             if (infoAll.Get()[i].visible == 0 && registered_application ==
438                 false)
439             {
440                 LogDebug("Not Registered.");
441                 LogDebug("-----");
442                 continue;
443             }
444             m_installedApplications.push_back(infoAll.Get()[i].app_path);
445             m_installedApplicationsNames.push_back(infoAll.Get()[i].app_name);
446             LogDebug("Registered.");
447             LogDebug("-----");
448         }
449     }
450     return true;
451 }
452
453 ApplicationType AppLauncherManager::getRegisteredApplicationType(
454     const std::string &name) const
455 {
456     if (!name.empty()) {
457         std::map<ApplicationType, std::string>::const_iterator it;
458         for (it = m_applicationTypesByPath.begin();
459              it != m_applicationTypesByPath.end();
460              ++it)
461         {
462             if (it->second == name) {
463                 return it->first;
464             }
465         }
466     }
467     return APPLICATION_TYPE_UNDEFINED;
468 }
469
470 bool AppLauncherManager::isRegisteredApplication(const std::string &name) const
471 {
472     if (name.empty()) {
473         return false;
474     }
475     std::vector<std::string>::const_iterator pos =
476         find(m_installedApplications.begin(),
477              m_installedApplications.end(), name);
478     return pos != m_installedApplications.end();
479 }
480
481 bool AppLauncherManager::validateHttpAddress(const std::string &value) const
482 {
483     if (!pcrecpp::RE(HTTP_ADDRESS_PATTERN).FullMatch(value)) {
484         LogDebug("not valid paterrn of http address");
485         return false;
486     }
487     return true;
488 }
489
490 bool AppLauncherManager::isFilePath(const std::string &value) const
491 {
492     if (!pcrecpp::RE(FILE_ADDRESS_PATTERN).FullMatch(value)) {
493         return false;
494     }
495     return true;
496 }
497
498 bool AppLauncherManager::validateEmailAddress(const std::string &value) const
499 {
500     if (!pcrecpp::RE(EMAIL_PATTERN).FullMatch(value)) {
501         LogDebug("not valid paterrn of email address");
502         return false;
503     }
504     return true;
505 }
506
507 std::string AppLauncherManager::getRealPath(const std::string &path) const
508 {
509     Try
510     {
511         Api::Filesystem::IPathPtr currentPath = Api::Filesystem::IPath::create(
512                 path);
513         return currentPath->getFullPath();
514     }
515     Catch(Commons::Exception)
516     {
517         LogError("invalid path");
518     }
519     return std::string();
520 }
521
522 std::string AppLauncherManager::getPathFromApplicationName(
523     const std::string &applicationName) const
524 {
525     if (0 == applicationName.find(WIDGET_URI_PREFIX) && std::string::npos !=
526         applicationName.find("?"))
527     {
528         //uri format is: widget://{widgetId}?wname={WidgetName}
529         LogDebug("found widget:// URI");
530         std::string uri = applicationName;
531         uri.erase(0, strlen(WIDGET_URI_PREFIX)); //remove widget://part
532         std::string widgetId = uri.substr(0, uri.find("?"));
533         uri.erase(0, widgetId.size() + 7); //remove widget id and "?wname="
534                                            // string, so uri contains only
535                                            // widget name now
536         std::string packageName = APPLICATION_WIDGET + widgetId;
537         LogDebug(
538             "Widget id: " << widgetId << ", widget name: " << uri <<
539             ", package: " << packageName);
540         app_info info = { 0, };
541         strncpy(info.pkg_name, packageName.c_str(), NAME_LEN);
542         if (record_get(GET_ONE_RECORD_BY_PKG_NAME, &info)) {
543             if (info.app_path && info.app_name && uri == info.app_name) {
544                 return std::string(info.app_path);
545             }
546         }
547     }
548     for (size_t i = 0; i < m_installedApplicationsNames.size(); ++i) {
549         if (m_installedApplicationsNames[i] == applicationName) {
550             return m_installedApplications[i];
551         }
552     }
553     return std::string();
554 }
555
556 std::string AppLauncherManager::getPathFromPackageName(
557     const std::string &packageName) const
558 {
559     app_info info = { 0, };
560     strncpy(info.pkg_name, packageName.c_str(), NAME_LEN);
561     if (record_get(GET_ONE_RECORD_BY_PKG_NAME, &info)) {
562         LogDebug("pkg_name " << info.pkg_name);
563         LogDebug("app_path " << info.app_path);
564         LogDebug("app_name " << info.app_name);
565         if (info.app_path) {
566             return std::string(info.app_path);
567         }
568     }
569     return std::string();
570 }
571
572 std::string AppLauncherManager::getPackageNameFromPath(const std::string &path)
573 const
574 {
575     app_info info = { 0, };
576     strncpy(info.app_path, path.c_str(), PATH_LEN);
577     if (record_get(GET_ONE_RECORD_BY_APP_PATH, &info)) {
578         LogDebug("pkg_name " << info.pkg_name);
579         LogDebug("app_path " << info.app_path);
580         LogDebug("app_name " << info.app_name);
581         if (info.app_path) {
582             return std::string(info.pkg_name);
583         }
584     }
585     return std::string();
586 }
587
588 int AppLauncherManager::launchApplicationFileInternal(
589     const std::string &uriParameter,
590     const std::vector<std::string> &applicationParams)
591 {
592     LogDebug("entered with uriParameter: '" << uriParameter << "'");
593     ApplicationType appType = getRegisteredApplicationType(uriParameter);
594     if (!isRegisteredApplication(uriParameter)) {
595         LogDebug("Its not registered application");
596         return ERROR_LAUNCHER_INVALID_URI;
597     }
598     //try to launch as package using aul
599     std::string packageName = getPackageNameFromPath(uriParameter);
600     if (!packageName.empty()) {
601         LogDebug("executing as package, package name " << packageName);
602         //check parameters for empty or extra spaces
603         std::vector<std::string> params;
604         std::vector<std::string>::const_iterator it;
605         for (it = applicationParams.begin();
606              it != applicationParams.end();
607              ++it)
608         {
609             std::string param(Commons::String::trim(*it));
610             if (param.empty()) {
611                 continue;
612             }
613             params.push_back(param);
614         }
615         bundle *kb = NULL;
616         if (params.size() > 0) {
617             if (appType == APPLICATION_TYPE_PHONECALL) {
618                 //phone number is provided.
619                 //replace package to make a call, not run application with this
620                 // functionality
621
622                 packageName = APPLICATION_PHONECALL_CALLING;
623                 kb = bundle_create();
624                 if (!kb) {
625                     return ERROR_LAUNCHER_EXECUTION_ERROR;
626                 }
627                 if (bundle_add(kb, "launch-type", "MO") < 0) { // "MO" : normal
628                                                                // call,
629                                                                // "EMERGENCY" :
630                                                                // emergency call
631                     bundle_free(kb);
632                     return ERROR_LAUNCHER_EXECUTION_ERROR;
633                 }
634                 if (bundle_add(kb, "number", params.front().c_str()) < 0) {
635                     bundle_free(kb);
636                     return ERROR_LAUNCHER_EXECUTION_ERROR;
637                 }
638                 LogDebug("running with number: " << params.front());
639             } else if (appType == APPLICATION_TYPE_BROWSER) {
640                 if (!validateHttpAddress(params.front())) {
641                     return ERROR_LAUNCHER_INVALID_PARAMETER;
642                 }
643                 kb = bundle_create();
644                 if (!kb) {
645                     return ERROR_LAUNCHER_EXECUTION_ERROR;
646                 }
647                 if (bundle_add(kb, "url", params.front().c_str()) < 0) {
648                     bundle_free(kb);
649                     return ERROR_LAUNCHER_EXECUTION_ERROR;
650                 }
651                 LogDebug("running with url: " << params.front().c_str());
652             } else if (appType == APPLICATION_TYPE_MEDIAPLAYER) {
653                 LogDebug("opening file: " << getRealPath(params.front()).c_str());
654                 int result = aul_open_file(getRealPath(params.front()).c_str());
655                 if (result < 0) {
656                     LogError("Cannot open file, launch application only");
657                 } else {
658                     return SUCCESS_LAUNCHER;
659                 }
660             } else if (appType == APPLICATION_TYPE_FILES) {
661                 struct stat sb;
662                 if (stat(getRealPath(params.front()).c_str(), &sb) == -1) {
663                     LogError(
664                         "The selected path does not exist: " <<
665                         getRealPath(params.front()).c_str());
666                 } else {
667                     if (!S_ISDIR(sb.st_mode)) {
668                         LogDebug("opening file: " << getRealPath(
669                                      params.front()).c_str());
670                         int result = aul_open_file(getRealPath(
671                                                        params.front()).c_str());
672                         if (result < 0) {
673                             LogError(
674                                 "Cannot open file, launch application only");
675                         } else {
676                             return SUCCESS_LAUNCHER;
677                         }
678                     } else {
679                         kb = bundle_create();
680                         if (!kb) {
681                             return ERROR_LAUNCHER_EXECUTION_ERROR;
682                         }
683                         if (bundle_add(kb, "path",
684                                        getRealPath(params.front()).c_str()) <
685                             0)
686                         {
687                             bundle_free(kb);
688                             return ERROR_LAUNCHER_EXECUTION_ERROR;
689                         }
690                         LogDebug("running with folder path: " <<
691                                  getRealPath(params.front()).c_str());
692                     }
693                 }
694             } else if (appType == APPLICATION_TYPE_PICTURES) {
695                 //open photo with image path
696                 //replace package to show only one picture with given image path
697                 packageName = APPLICATION_PICTURES_IMG_PATH;
698                 kb = bundle_create();
699                 if (!kb) {
700                     return ERROR_LAUNCHER_EXECUTION_ERROR;
701                 }
702                 if (bundle_add(kb, "View Mode", "SINGLE") < 0) {
703                     bundle_free(kb);
704                     return ERROR_LAUNCHER_EXECUTION_ERROR;
705                 }
706                 if (bundle_add(kb, "Path",
707                                getRealPath(params.front()).c_str()) < 0)
708                 {
709                     bundle_free(kb);
710                     return ERROR_LAUNCHER_EXECUTION_ERROR;
711                 }
712                 LogDebug("running with img file path: " <<
713                          getRealPath(params.front()).c_str());
714             }
715             //TODO: add more parameters when needed
716         }
717         int result = aul_launch_app(packageName.c_str(), kb);
718         if (kb) {
719             bundle_free(kb);
720         }
721         if (result > 0) {
722             return SUCCESS_LAUNCHER;
723         }
724         LogError("aul_launch_app result " << result);
725         return ERROR_LAUNCHER_EXECUTION_ERROR;
726     }
727     return ERROR_LAUNCHER_EXECUTION_ERROR;
728 }
729
730 int AppLauncherManager::launchApplicationInternal(
731     const ApplicationUriType uriType,
732     const std::string &uriParameter,
733     const std::vector<std::string> &applicationParams)
734 {
735     LogDebug(
736         "entered with uriType: " << uriType << ", uriParameter: " <<
737         uriParameter);
738     if (APPLICATION_URI_TYPE_HTTP == uriType) {
739         std::string application =
740             m_applicationTypesByPath[APPLICATION_TYPE_BROWSER];
741         std::vector<std::string> params;
742         if (Commons::String::startsWith(uriParameter, "http://")) {
743             params.push_back(uriParameter);
744         } else if (!pcrecpp::RE(EMPTY_STRING_PATTERN).FullMatch(uriParameter))
745         {
746             params.push_back("http://" + uriParameter);
747         }
748         return launchApplicationFileInternal(application, params);
749     } else if (APPLICATION_URI_TYPE_HTTPS == uriType) {
750         std::string application =
751             m_applicationTypesByPath[APPLICATION_TYPE_BROWSER];
752         std::vector<std::string> params;
753         if (Commons::String::startsWith(uriParameter, "https://")) {
754             params.push_back(uriParameter);
755         } else if (!pcrecpp::RE(EMPTY_STRING_PATTERN).FullMatch(uriParameter))
756         {
757             params.push_back("https://" + uriParameter);
758         }
759         return launchApplicationFileInternal(application, params);
760     } else if (APPLICATION_URI_TYPE_TEL == uriType) {
761         std::string application =
762             m_applicationTypesByPath[APPLICATION_TYPE_PHONECALL];
763         std::vector<std::string> params;
764         params.push_back(uriParameter);
765         return launchApplicationFileInternal(application, params);
766     } else if (APPLICATION_URI_TYPE_MAILTO == uriType) {
767         std::string application =
768             m_applicationTypesByPath[APPLICATION_TYPE_MAIL];
769         std::vector<std::string> params;
770         params.push_back(uriParameter);
771         return launchApplicationFileInternal(application, params);
772     } else if (APPLICATION_URI_TYPE_SMS == uriType) {
773         std::string application =
774             m_applicationTypesByPath[APPLICATION_TYPE_MESSAGING];
775         std::vector<std::string> params;
776         params.push_back(uriParameter);
777         return launchApplicationFileInternal(application, params);
778     } else if (APPLICATION_URI_TYPE_FILE == uriType) {
779         return launchApplicationFileInternal(uriParameter, applicationParams);
780     } else if (APPLICATION_URI_TYPE_APPLICATION_BY_NAME == uriType) {
781         std::string application = getPathFromApplicationName(uriParameter);
782         return launchApplicationFileInternal(application, applicationParams);
783     } else {
784         return ERROR_LAUNCHER_INVALID_URI;
785     }
786     return SUCCESS_LAUNCHER;
787 }
788
789 std::string AppLauncherManager::getDefaultApplicationByMimeInternal(
790     const std::string &mime) const
791 {
792     LogDebug("entered with mime: " << mime);
793     const int bufferSize = 1024;
794     char buffer[bufferSize] = { 0 };
795     int result = aul_get_defapp_from_mime(mime.c_str(), buffer, bufferSize - 1);
796     if (AUL_R_OK == result) {
797         LogDebug("returning default application");
798         return std::string(buffer);
799     } else {
800         LogError("aul_get_defapp_from_mime failed, error code " << result);
801         Throw(Commons::PlatformException);
802     }
803     return std::string();
804 }
805
806 void AppLauncherManager::OnRequestReceived(
807     const EventGetDefaultApplicationPtr &event)
808 {
809     LogDebug("entered");
810     Try
811     {
812         std::string result = getDefaultApplicationByMimeInternal(event->getMime());
813         event->setApplicationFullPath(result);
814         event->setExceptionCode(Commons::ExceptionCodes::None);
815     }
816     Catch(Commons::PlatformException)
817     {
818         event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
819     }
820 }
821
822 void AppLauncherManager::OnRequestReceived(
823     const EventGetInstalledApplicationsPtr &event)
824 {
825     LogDebug("entered");
826     std::vector<std::string>::const_iterator it1;
827     for (it1 = m_installedApplications.begin();
828          it1 != m_installedApplications.end();
829          ++it1)
830     {
831         event->addApplicationFullPath(*it1);
832     }
833     for (it1 = m_installedApplicationsNames.begin();
834          it1 != m_installedApplicationsNames.end();
835          ++it1)
836     {
837         event->addApplicationName(*it1);
838     }
839     std::map<ApplicationType, std::string>::const_iterator it2;
840     for (it2 = m_applicationTypesByPath.begin();
841          it2 != m_applicationTypesByPath.end();
842          ++it2)
843     {
844         event->addApplicationTypeByPath(*it2);
845     }
846     for (it2 = m_applicationTypesByName.begin();
847          it2 != m_applicationTypesByName.end();
848          ++it2)
849     {
850         event->addApplicationTypeByName(*it2);
851     }
852     event->setExceptionCode(Commons::ExceptionCodes::None);
853 }
854
855 void AppLauncherManager::OnRequestReceived(
856     const EventLaunchApplicationPtr &event)
857 {
858     LogDebug("entered");
859     ApplicationUriType uriType = event->getApplicationUriType();
860     std::string uriParam = event->getApplicationUriParam();
861     std::vector<std::string> parameters = event->getApplicationParameters();
862     int result = launchApplicationInternal(uriType, uriParam, parameters);
863     if (result == SUCCESS_LAUNCHER) {
864         event->setExceptionCode(Commons::ExceptionCodes::None);
865     } else if (result == ERROR_LAUNCHER_INVALID_PARAMETER ||
866                result == ERROR_LAUNCHER_INVALID_URI)
867     {
868         event->setExceptionCode(
869             Commons::ExceptionCodes::InvalidArgumentException);
870     } else {
871         event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
872     }
873 }
874 }
875 }