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