Update wrt-plugins-common_0.3.60
[platform/framework/web/wrt-plugins-common.git] / src / plugins-installer / wrt-installer / wrt_installer.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 /* @file    wrt_installer.cpp
17  * @version 1.0
18  * @brief   Implementation file for installer
19  */
20
21 #include "wrt_installer.h"
22 #include "plugin_utils.h"
23
24 #include <cstdlib>
25 #include <string>
26 #include <fstream>
27 #include <unistd.h>
28 #include <sys/resource.h>
29 #include <dpl/optional.h>
30 #include <dpl/scoped_free.h>
31 #include <dpl/optional_typedefs.h>
32 #include <dpl/exception.h>
33 #include <dpl/sstream.h>
34 #include <vconf.h>
35 #include <dpl/wrt-dao-ro/global_config.h>
36 #include <dpl/wrt-dao-ro/config_parser_data.h>
37 #include <dpl/localization/localization_utils.h>
38 #include <dpl/popup/popup_controller.h>
39 #include <dpl/optional_typedefs.h>
40 #include <dpl/string.h>
41 #include <dpl/abstract_waitable_input_adapter.h>
42 #include <dpl/abstract_waitable_output_adapter.h>
43 #include <dpl/zip_input.h>
44 #include <dpl/scoped_ptr.h>
45 #include <dpl/binary_queue.h>
46 #include <dpl/copy.h>
47 #include <dpl/utils/wrt_global_settings.h>
48 #include "option_parser.h"
49 #include <parser_runner.h>
50 #include <widget_parser.h>
51 #include <root_parser.h>
52 //#include <pkg-manager/pkgmgr_signal.h>
53
54 #define NOFILE_CNT_FOR_INSTALLER 9999
55
56 using namespace WrtDB;
57
58 namespace { // anonymous
59 const char AUL_ARG_KEY[] = "widget_arg";
60 const char * const PKGMGR_INSTALL_MSG = "Install widget";
61 const char * const PKGMGR_UNINSTALL_MSG = "Uninstall widget";
62
63 const double BASE_LAYOUT_W = 720.0f;
64 const double BASE_LAYOUT_H = 1280.0f;
65
66 const char * const CONFIG_XML = "config.xml";
67
68 struct PluginInstallerData
69 {
70     void* wrtInstaller;
71     std::string pluginPath;
72 };
73 } // namespace anonymous
74
75 WrtInstaller::WrtInstaller(int argc, char **argv) :
76     Application(argc, argv, "backend", false),
77     DPL::TaskDecl<WrtInstaller>(this),
78     m_packagePath(),
79     m_handle(-1),
80     m_initialized(false),
81     m_numPluginsToInstall(0),
82     m_totalPlugins(0),
83     m_returnStatus(-1),
84 //    m_installByPkgmgr(false),
85     m_quiet(true),
86 //    m_popup(NULL),
87     m_startupPluginInstallation(false)
88 {
89     Touch();
90     LogDebug("App Created");
91 }
92
93 WrtInstaller::~WrtInstaller()
94 {
95     LogDebug("App Finished");
96 }
97
98 void WrtInstaller::OnStop()
99 {
100     LogInfo("Stopping Dummy Client");
101 }
102
103 void WrtInstaller::OnCreate()
104 {
105     LogInfo("Creating DummyClient");
106
107     AddStep(&WrtInstaller::initStep);
108
109     std::string arg = m_argv[0];
110
111     if (arg.empty()) {
112         return showHelpAndQuit();
113     }
114
115     installNewPlugins();
116
117     if (arg.find("wrt-installer") != std::string::npos)
118     {
119         if (m_argc <= 1) {
120             return showHelpAndQuit();
121         }
122
123         arg = m_argv[1];
124         if (arg == "-h" || arg == "--help") {
125             if (m_argc != 2) {
126                 return showHelpAndQuit();
127             }
128
129             // Just show help
130             return showHelpAndQuit();
131         } else if (arg == "-p" || arg == "--install-plugins") {
132             if (m_argc != 2) {
133                 return showHelpAndQuit();
134             }
135             if (!m_startupPluginInstallation) {
136                 AddStep(&WrtInstaller::installPluginsStep);
137             } else {
138                 LogInfo("Plugin installation alredy started");
139             }
140         }
141         else
142         {
143             LogError("Unknown option");
144             return showHelpAndQuit();
145         }
146     }
147
148     AddStep(&WrtInstaller::shutdownStep);
149     DPL::Event::ControllerEventHandler<
150         WRTInstallerNS::NextStepEvent>::PostEvent(
151             WRTInstallerNS::NextStepEvent());
152 }
153
154 //void WrtInstaller::OnReset(bundle *b)
155 //{
156 //    const char * bundledVal = bundle_get_val(b, AUL_ARG_KEY);
157 //    if (bundledVal != NULL) {
158 //        m_bundleValue = bundledVal;
159 //        LogInfo("Bundled value for (" << AUL_ARG_KEY << ") key received: " <<
160 //                m_bundleValue);
161 //    }
162 //}
163
164 int WrtInstaller::getReturnStatus() const
165 {
166     if (!m_returnStatus) {
167         return RE_SUCCESS;
168     } else {
169         return RE_FAIL;
170     }
171 }
172
173 void WrtInstaller::OnTerminate()
174 {
175     LogDebug("Wrt Shutdown now");
176     PluginUtils::unlockPluginInstallation();
177     if (m_initialized) {
178         wrt_installer_shutdown();
179     }
180 //    delete m_popup;
181 }
182
183 void WrtInstaller::showHelpAndQuit()
184 {
185     printf("Usage: wrt-installer [OPTION]... [WIDGET: ID/NAME/GUID/PATH]...\n"
186            "Operate with WebRuntime daemon: install, uninstall"
187            " and launch widgets.\n"
188            "Query list of installed widgets and setup up debugging support.\n"
189            "\n"
190            "Exactly one option must be selected.\n"
191            "Mandatory arguments to long options are mandatory for short "
192            "options too.\n"
193            "  -h,    --help                                 show this help\n"
194            "  -p,    --install-plugins                      install plugins\n"
195            "\n");
196
197     Quit();
198 }
199
200 void WrtInstaller::OnEventReceived(const WRTInstallerNS::QuitEvent& /*event*/)
201 {
202     LogDebug("Quiting");
203
204     if (m_initialized) {
205         LogDebug("Wrt Shutdown now");
206         SwitchToStep(&WrtInstaller::shutdownStep);
207         DPL::Event::ControllerEventHandler<WRTInstallerNS::NextStepEvent>::PostEvent(
208             WRTInstallerNS::NextStepEvent());
209     } else {
210         LogDebug("Quiting application");
211         return Quit();
212     }
213 }
214
215 void WrtInstaller::OnEventReceived(
216     const WRTInstallerNS::NextStepEvent& /*event*/)
217 {
218     LogDebug("Executing next step");
219     NextStep();
220 }
221
222 void WrtInstaller::OnEventReceived(
223     const WRTInstallerNS::InstallPluginEvent& /*event*/)
224 {
225     PluginInstallerData* privateData = new PluginInstallerData;
226     privateData->wrtInstaller = this;
227
228     if (!(*m_pluginsPaths).empty()) {
229         privateData->pluginPath = (*m_pluginsPaths).front();
230         (*m_pluginsPaths).pop_front();
231
232         wrt_install_plugin(privateData->pluginPath.c_str(),
233                 static_cast<void*>(privateData),
234                 &staticWrtPluginInstallationCallback,
235                 &staticWrtPluginInstallProgressCb);
236     } else {
237         delete privateData;
238     }
239 }
240
241 void WrtInstaller::initStep()
242 {
243     wrt_installer_init(this, staticWrtInitCallback);
244 }
245
246 //void WrtInstaller::installStep()
247 //{
248 //    LogDebug("Installing widget ...");
249 //    DPL::ScopedFree<char> packagePath(canonicalize_file_name(
250 //            m_packagePath.c_str()));
251 //
252 //    wrt_install_widget(packagePath ? packagePath.Get() : m_packagePath.c_str(),
253 //                       this, &staticWrtStatusCallback,
254 //                       (!m_quiet || m_installByPkgmgr)
255 //                       ? &staticWrtInstallProgressCallback : NULL,
256 //                       m_installPolicy,
257 //                       m_quiet);
258 //}
259
260 void WrtInstaller::installPluginsStep()
261 {
262     LogDebug("Installing plugins ...");
263
264     if (m_startupPluginInstallation) {
265         LogInfo("Plugin installation started because new plugin package found");
266     } else if (!PluginUtils::lockPluginInstallation()) {
267         LogError("Failed to open plugin installation lock file"
268                 " Plugins are currently installed by other process");
269         staticWrtPluginInstallationCallback(WRT_PLUGIN_INSTALLER_ERROR_LOCK,
270                 this);
271         return;
272     }
273
274     std::string PLUGIN_PATH = std::string(GlobalConfig::GetDevicePluginPath());
275
276     DIR *dir;
277     dir = opendir(PLUGIN_PATH.c_str());
278
279     if (!dir) {
280         return;
281     }
282
283     LogInfo("Plugin DIRECTORY IS" << PLUGIN_PATH);
284     struct dirent* libdir;
285
286     errno = 0;
287
288     std::list<std::string> pluginsPaths;
289
290     while ((libdir = readdir(dir)) != 0) {
291         if (strcmp(libdir->d_name, ".") == 0 ||
292             strcmp(libdir->d_name, "..") == 0)
293         {
294             continue;
295         }
296
297         std::string path = PLUGIN_PATH;
298         path += "/";
299         path += libdir->d_name;
300
301         struct stat tmp;
302
303         if (stat(path.c_str(), &tmp) == -1) {
304             LogError("Failed to open file" << path);
305             continue;
306         }
307
308         if (!S_ISDIR(tmp.st_mode)) {
309             LogError("Not a directory" << path);
310             continue;
311         }
312
313         pluginsPaths.push_back(path);
314     }
315
316     //set nb of plugins to install
317     //this value indicate how many callbacks are expected
318     m_numPluginsToInstall = pluginsPaths.size();
319     LogInfo("Plugins to install: " << m_numPluginsToInstall);
320     m_pluginsPaths = pluginsPaths;
321
322     m_totalPlugins = m_numPluginsToInstall;
323     DPL::Event::ControllerEventHandler<WRTInstallerNS::InstallPluginEvent>
324         ::PostEvent(WRTInstallerNS::InstallPluginEvent());
325
326     if (-1 == TEMP_FAILURE_RETRY(closedir(dir))) {
327         LogError("Failed to close dir: " << dir);
328     }
329 }
330
331 //void WrtInstaller::uninstallStep()
332 //{
333 //    LogDebug("Uninstalling widget ...");
334 //    wrt_uninstall_widget(m_handle, this, &staticWrtStatusCallback,
335 //            (!m_quiet || m_installByPkgmgr)
336 //            ? &staticWrtUninstallProgressCallback : NULL);
337 //}
338
339 //void WrtInstaller::uninstallPkgNameStep()
340 //{
341 //    LogDebug("Uninstalling widget ...");
342 //    WrtErrStatus status = wrt_get_widget_by_pkgname(m_name, &m_handle);
343 //    if (status == WRT_SUCCESS) {
344 //        LogDebug("Get Widget Handle by package name : " << m_handle);
345 //        wrt_uninstall_widget(m_handle, this, &staticWrtStatusCallback,
346 //                (!m_quiet || m_installByPkgmgr)
347 //                ? &staticWrtUninstallProgressCallback : NULL);
348 //    } else {
349 //        printf("failed: can not uninstall widget\n");
350 //        LogError("Fail to uninstalling widget... ");
351 //        m_returnStatus = -1;
352 //        DPL::Event::ControllerEventHandler<WRTInstallerNS::QuitEvent>::PostEvent(
353 //            WRTInstallerNS::QuitEvent());
354 //    }
355 //}
356
357 //void WrtInstaller::uninstallGuidStep()
358 //{
359 //    LogDebug("Uninstalling widget ...");
360 //    WrtErrStatus status = wrt_get_widget_by_guid(m_name, &m_handle);
361 //    if (status == WRT_SUCCESS) {
362 //        LogDebug("Get Widget Handle by guid : " << m_handle);
363 //        wrt_uninstall_widget(m_handle, this, &staticWrtStatusCallback,
364 //                !m_quiet ? &staticWrtUninstallProgressCallback : NULL);
365 //    } else {
366 //        printf("failed: can not uninstall widget\n");
367 //        LogError("Fail to uninstalling widget... ");
368 //        m_returnStatus = -1;
369 //        DPL::Event::ControllerEventHandler<WRTInstallerNS::QuitEvent>::PostEvent(
370 //            WRTInstallerNS::QuitEvent());
371 //    }
372 //}
373 //
374 //void WrtInstaller::unistallWgtFileStep()
375 //{
376 //    LogDebug("Uninstalling widget ...");
377 //
378 //    Try {
379 //        // Parse config
380 //        ParserRunner parser;
381 //        ConfigParserData configInfo;
382 //
383 //        // Open zip file
384 //        DPL::ScopedPtr<DPL::ZipInput> zipFile(
385 //                new DPL::ZipInput(m_packagePath));
386 //
387 //        // Open config.xml file
388 //        DPL::ScopedPtr<DPL::ZipInput::File> configFile(
389 //                zipFile->OpenFile(CONFIG_XML));
390 //
391 //        // Extract config
392 //        DPL::BinaryQueue buffer;
393 //        DPL::AbstractWaitableInputAdapter inputAdapter(configFile.Get());
394 //        DPL::AbstractWaitableOutputAdapter outputAdapter(&buffer);
395 //        DPL::Copy(&inputAdapter, &outputAdapter);
396 //        parser.Parse(&buffer,
397 //                ElementParserPtr(
398 //                    new RootParser<WidgetParser>(configInfo,
399 //                        DPL::FromUTF32String(
400 //                            L"widget"))));
401 //
402 //        DPL::OptionalString widgetGUID = configInfo.widget_id;
403 //
404 //        std::string guid = DPL::ToUTF8String(*widgetGUID);
405 //
406 //        WrtErrStatus status = wrt_get_widget_by_guid(guid, &m_handle);
407 //        if (status == WRT_SUCCESS) {
408 //            LogDebug("Get Widget Handle by guid : " << m_handle);
409 //            wrt_uninstall_widget(m_handle, this, &staticWrtStatusCallback,
410 //                    !m_quiet ? &staticWrtUninstallProgressCallback : NULL);
411 //        } else {
412 //            LogError("Fail to uninstalling widget... ");
413 //            m_returnStatus = -1;
414 //            DPL::Event::ControllerEventHandler<WRTInstallerNS::QuitEvent>::PostEvent(
415 //                    WRTInstallerNS::QuitEvent());
416 //        }
417 //    }
418 //    Catch(DPL::ZipInput::Exception::OpenFailed)
419 //    {
420 //        LogError("Failed to open widget package");
421 //        printf("failed: widget package does not exist\n");
422 //        m_returnStatus = -1;
423 //        DPL::Event::ControllerEventHandler<WRTInstallerNS::QuitEvent>::PostEvent(
424 //                WRTInstallerNS::QuitEvent());
425 //    }
426 //    Catch(DPL::ZipInput::Exception::OpenFileFailed)
427 //    {
428 //        printf("failed: widget config file does not exist\n");
429 //        LogError("Failed to open config.xml file");
430 //        m_returnStatus = -1;
431 //        DPL::Event::ControllerEventHandler<WRTInstallerNS::QuitEvent>::PostEvent(
432 //                WRTInstallerNS::QuitEvent());
433 //    }
434 //    Catch(ElementParser::Exception::ParseError)
435 //    {
436 //        printf("failed: can not parse config file\n");
437 //        LogError("Failed to parse config.xml file");
438 //        m_returnStatus = -1;
439 //        DPL::Event::ControllerEventHandler<WRTInstallerNS::QuitEvent>::PostEvent(
440 //                WRTInstallerNS::QuitEvent());
441 //    }
442 //}
443
444 void WrtInstaller::shutdownStep()
445 {
446     LogDebug("Closing Wrt connection ...");
447     if (m_initialized) {
448         wrt_installer_shutdown();
449         m_initialized = false;
450         DPL::Event::ControllerEventHandler<WRTInstallerNS::QuitEvent>::PostEvent(
451             WRTInstallerNS::QuitEvent());
452     }
453 }
454
455 void WrtInstaller::staticWrtInitCallback(WrtErrStatus status,
456                                       void* userdata)
457 {
458     WrtInstaller *This = static_cast<WrtInstaller*>(userdata);
459     Assert(This);
460
461     if (status == WRT_SUCCESS) {
462         LogDebug("Init succesfull");
463         This->m_initialized = true;
464         This->m_returnStatus = 0;
465
466 //        if (!This->m_quiet) {
467 //            This->m_popup = new InstallerPopup;
468 //            This->m_popup->init();
469 //        }
470
471         This->DPL::Event::ControllerEventHandler<WRTInstallerNS::NextStepEvent>
472             ::PostEvent(WRTInstallerNS::NextStepEvent());
473     } else {
474         LogError("Init unsuccesfull");
475         This->m_returnStatus = -1;
476         This->DPL::Event::ControllerEventHandler<WRTInstallerNS::QuitEvent>::PostEvent(
477             WRTInstallerNS::QuitEvent());
478     }
479 }
480 //
481 //void WrtInstaller::staticWrtStatusCallback(int handle,
482 //                                           WrtErrStatus status,
483 //                                           void* userdata)
484 //{
485 //    WrtInstaller *This = static_cast<WrtInstaller*>(userdata);
486 //    Assert(This);
487 //
488 //    Step current = This->GetCurrentStep();
489 //    DPL::String resultMsg;
490 //    std::string printMsg;
491 //
492 //    if (current == &WrtInstaller::installStep)
493 //    {
494 //        resultMsg = DPL::FromUTF8String(PKGMGR_INSTALL_MSG);
495 //        printMsg = "installed";
496 //    } else if (current == &WrtInstaller::uninstallStep ||
497 //            current == &WrtInstaller::uninstallPkgNameStep ||
498 //            current == &WrtInstaller::uninstallGuidStep ||
499 //            current == &WrtInstaller::unistallWgtFileStep)
500 //    {
501 //        resultMsg = DPL::FromUTF8String(PKGMGR_UNINSTALL_MSG);
502 //        printMsg = "uninstalled";
503 //    }
504 //
505 //    if (WRT_SUCCESS != status) {
506 //        // Failure
507 //        LogDebug("Step failed");
508 //        This->m_returnStatus = -1;
509 //
510 //        if (!This->m_quiet) {
511 //            resultMsg +=  L" : " + DPL::FromUTF8String(PKGMGR_END_FAILURE);
512 //            This->m_popup->showPopup(This, resultMsg, failResultCallback);
513 //        } else {
514 //            This->DPL::Event::ControllerEventHandler<WRTInstallerNS::QuitEvent>
515 //                ::PostEvent(WRTInstallerNS::QuitEvent());
516 //        }
517 //
518 //        switch (status) {
519 //            case WRT_INSTALLER_ERROR_INVALID_WIDGET_PACKAGE:
520 //                This->m_returnStatus = 1; //this status is specific
521 //                printf("failed: invalid widget package\n");
522 //                break;
523 //
524 //            case WRT_INSTALLER_ERROR_WIDGET_DOES_NOT_EXIST:
525 //                printf("failed: widget package does not exist\n");
526 //                break;
527 //
528 //            case WRT_INSTALLER_ERROR_FACTORY_WIDGET:
529 //                printf("failed: factory widget\n");
530 //                break;
531 //
532 //            case WRT_INSTALLER_ERROR_ALREADY_UNINSTALLING:
533 //                printf("failed: already uninstalling\n");
534 //                break;
535 //
536 //            case WRT_INSTALLER_ERROR_OUT_OUT_DISK_SPACE:
537 //                printf("failed: out of disk space\n");
538 //                break;
539 //
540 //            case WRT_INSTALLER_ERROR_INVALID_CERTIFICATE:
541 //                printf("failed: invalid certificate\n");
542 //                break;
543 //
544 //            case WRT_INSTALLER_ERROR_ALREADY_INSTALLED:
545 //                printf("failed: already installed\n");
546 //                break;
547 //
548 //            case WRT_INSTALLER_ERROR_INTERNAL:
549 //                printf("failed: internal error\n");
550 //                break;
551 //
552 //            case WRT_INSTALLER_ERROR_NOT_ALLOWED:
553 //                printf("failed: installation or update not allowed; invalid"
554 //                       " mode\n");
555 //                break;
556 //
557 //            case WRT_INSTALLER_ERROR_DEFERRED:
558 //                printf("deferred: widget update will continue after the widget"
559 //                       " has been stopped\n");
560 //                break;
561 //
562 //            case WRT_INSTALLER_ERROR_DATABASE_FAILURE:
563 //                printf("failed: database failure\n");
564 //                break;
565 //
566 //            case WRT_INSTALLER_ERROR_OSPSVC:
567 //                printf("failed: during installation or uninstallation osp service\n");
568 //                break;
569 //
570 //            case WRT_INSTALLER_ERROR_UNKNOWN:
571 //                printf("failed: unknown error\n");
572 //                break;
573 //
574 //            default:
575 //                break;
576 //        }
577 //    } else {
578 //
579 //        printf("%s : %d\n", printMsg.c_str(), handle);
580 //        LogDebug("Status succesfull");
581 //        This->m_handle = handle;
582 //        This->m_returnStatus = 0;
583 //        resultMsg +=  L" : " + DPL::FromUTF8String(PKGMGR_END_SUCCESS);
584 //
585 //        if (!This->m_quiet) {
586 //            This->m_popup->showPopup(This, resultMsg, showResultCallback);
587 //        } else {
588 //            This->DPL::Event::ControllerEventHandler<WRTInstallerNS::NextStepEvent>
589 //                ::PostEvent(WRTInstallerNS::NextStepEvent());
590 //        }
591 //    }
592 //}
593
594 void WrtInstaller::staticWrtPluginInstallationCallback(WrtErrStatus status,
595                                                     void* userdata)
596 {
597     Assert(userdata);
598
599     PluginInstallerData* data = static_cast<PluginInstallerData*>(userdata);
600
601     WrtInstaller *This = static_cast<WrtInstaller*>(data->wrtInstaller);
602
603     std::string path = std::string(data->pluginPath);
604     delete data;
605
606     This->m_numPluginsToInstall--;
607     LogDebug("Plugins to install: " << This->m_numPluginsToInstall);
608
609     if (This->m_numPluginsToInstall < 1) {
610         LogDebug("All plugins installation completed");
611
612         //remove installation request
613         if (!PluginUtils::removeInstallationRequiredFlag()) {
614             LogInfo("Failed to remove file initializing plugin installation");
615         }
616
617         //remove lock file
618         if (!PluginUtils::unlockPluginInstallation()) {
619             LogInfo("Failed to remove installation lock");
620         }
621
622 //        if (!This->m_quiet) {
623 //            This->m_popup->init();
624 //            elm_progressbar_value_set(This->m_popup->m_progressbar, 100.0);
625 //            evas_object_show(This->m_popup->m_popup);
626 //        }
627
628         This->DPL::Event::ControllerEventHandler<WRTInstallerNS::NextStepEvent>
629             ::PostEvent(WRTInstallerNS::NextStepEvent());
630     } else {
631 //        if (!This->m_quiet) {
632 //            This->m_popup->init();
633 //            float percent = (This->m_totalPlugins - This->m_numPluginsToInstall)/(float)This->m_totalPlugins;
634 //            elm_progressbar_value_set(This->m_popup->m_progressbar, percent);
635 //            evas_object_show(This->m_popup->m_popup);
636 //        }
637
638         This->DPL::Event::ControllerEventHandler<WRTInstallerNS::InstallPluginEvent>::PostEvent(
639                 WRTInstallerNS::InstallPluginEvent());
640     }
641
642     if (WRT_SUCCESS == status) {
643         This->m_returnStatus = 0;
644         LogDebug("One plugin Installation succesfull: " << path);
645         return;
646     }
647
648     // Failure
649     LogWarning("One of the plugins installation failed!: " << path);
650
651     if (WRT_PLUGIN_INSTALLER_ERROR_WAITING == status) {
652         LogInfo("Plugin installation is waiting for dependencies");
653     }
654
655     switch (status) {
656     case WRT_PLUGIN_INSTALLER_ERROR_WRONG_PATH:
657         LogError("failed: wrong path to plugin directory\n");
658         break;
659
660     case WRT_PLUGIN_INSTALLER_ERROR_METAFILE:
661         LogError("failed: plugin metafile error\n");
662         break;
663
664     case WRT_PLUGIN_INSTALLER_ERROR_ALREADY_INSTALLED:
665         LogError("failed: plugin already installed\n");
666         break;
667
668     case WRT_PLUGIN_INSTALLER_ERROR_LIBRARY_ERROR:
669         LogError("failed: plugin library: missing symbols or structures\n");
670         break;
671
672     case WRT_PLUGIN_INSTALLER_ERROR_UNKNOWN:
673         LogError("failed: unknown error\n");
674         break;
675
676     default:
677         break;
678     }
679 }
680
681 void WrtInstaller::staticWrtPluginInstallProgressCb(float percent,
682                                                     const char* description,
683                                                     void* userdata)
684 {
685     PluginInstallerData* data = static_cast<PluginInstallerData*>(userdata);
686
687     std::string path = std::string(data->pluginPath);
688
689     LogInfo("Plugin Installation: " << path <<
690             " progress: " << percent <<
691             "description " << description);
692 }
693
694 //void WrtInstaller::staticWrtInstallProgressCallback(float percent,
695 //        const char* description, void* userdata)
696 //{
697 //    WrtInstaller *This = static_cast<WrtInstaller*>(userdata);
698 //    std::stringstream percentStr;
699 //    LogInfo(" progress: " << percent <<
700 //            " description: " << description);
701 //
702 //    if (!This->m_quiet) {
703 //        This->m_popup->init();
704 //        elm_progressbar_value_set(This->m_popup->m_progressbar, percent/100.0);
705 //        evas_object_show(This->m_popup->m_popup);
706 //    }
707 //}
708 //void WrtInstaller::staticWrtUninstallProgressCallback(float percent,
709 //        const char* description, void* userdata)
710 //{
711 //    WrtInstaller *This = static_cast<WrtInstaller*>(userdata);
712 //    std::stringstream percentStr;
713 //    LogInfo(" progress: " << percent <<
714 //            " description: " << description);
715 //
716 //    if (!This->m_quiet) {
717 //        This->m_popup->init();
718 //        elm_progressbar_value_set(This->m_popup->m_progressbar, percent/100.0);
719 //        evas_object_show(This->m_popup->m_popup);
720 //    }
721 //}
722
723 #if 0
724 WrtInstaller::InstallerPopup::InstallerPopup() :
725     m_win(NULL),
726     m_popup(NULL),
727     m_progressbar(NULL)
728 {
729 }
730
731 WrtInstaller::InstallerPopup::~InstallerPopup()
732 {
733     LogDebug("App Finished");
734 }
735
736 void WrtInstaller::InstallerPopup::init()
737 {
738     LogDebug("Window Init");
739
740     if (m_win == NULL) {
741         // create window
742         m_win = createWin("wrt-installer");
743
744         // create popup
745         m_popup = elm_popup_add(m_win);
746
747         // create progressbar
748         m_progressbar = elm_progressbar_add(m_popup);
749         elm_object_style_set(m_progressbar, "list_progress");
750         elm_progressbar_horizontal_set(m_progressbar, EINA_TRUE);
751         evas_object_size_hint_align_set(m_progressbar, EVAS_HINT_FILL,
752             EVAS_HINT_FILL);
753         evas_object_size_hint_weight_set(m_progressbar, EVAS_HINT_EXPAND,
754             EVAS_HINT_EXPAND);
755         elm_object_content_set(m_popup, m_progressbar);
756         elm_progressbar_value_set(m_progressbar, 0.0);
757         evas_object_show(m_progressbar);
758
759         evas_object_show(m_popup);
760         evas_object_show(m_win);
761     }
762 }
763
764 Evas_Object* WrtInstaller::InstallerPopup::createWin(const char *name)
765 {
766     Evas_Object *win;
767     win = elm_win_add(NULL, name, ELM_WIN_DIALOG_BASIC);
768
769     int w, h;
770     if(!win)
771         return NULL;
772
773     elm_win_alpha_set(win, EINA_TRUE);
774     elm_win_title_set(win, name);
775     elm_win_borderless_set(win, EINA_TRUE);
776     elm_win_raise(win);
777
778     ecore_x_window_size_get(ecore_x_window_root_first_get(), &w, &h);
779     evas_object_resize(win, w, h);
780     return win;
781 }
782
783 void WrtInstaller::InstallerPopup::showPopup(void* userdata,
784                                              const DPL::String& pkgMsg,
785                                              ShowResultCallback callback)
786 {
787     Evas_Object *btn;
788
789
790     LogDebug("Result Popup Created");
791     evas_object_del(m_popup);
792     m_popup = NULL;
793
794     m_popup = elm_popup_add(m_win);
795     if (!m_popup)
796         return;
797
798     btn = elm_button_add(m_popup);
799     if (!btn) {
800         evas_object_del(m_popup);
801         return;
802     }
803     elm_object_text_set(btn, "OK");
804     evas_object_smart_callback_add(btn, "clicked", callback, userdata);
805     elm_object_part_content_set(m_popup, "button1", btn);
806     elm_object_part_text_set(m_popup, "title,text", "RESULT");
807     elm_object_text_set(m_popup, DPL::ToUTF8String(pkgMsg).c_str());
808
809     evas_object_show(m_popup);
810     evas_object_show(m_win);
811
812 }
813 #endif
814
815 //void WrtInstaller::showResultCallback(void *data, Evas_Object* /*obj*/,
816 //                                      void* /*event_info*/)
817 //{
818 //    WrtInstaller *This = static_cast<WrtInstaller*>(data);
819 //    Assert(This);
820 //
821 //    This->DPL::Event::ControllerEventHandler<WRTInstallerNS::NextStepEvent>
822 //        ::PostEvent(WRTInstallerNS::NextStepEvent());
823 //}
824 //
825 //void WrtInstaller::failResultCallback(void *data, Evas_Object* /*obj*/,
826 //                                      void* /*event_info*/)
827 //{
828 //    WrtInstaller *This = static_cast<WrtInstaller*>(data);
829 //    Assert(This);
830 //
831 //    This->DPL::Event::ControllerEventHandler<WRTInstallerNS::QuitEvent>
832 //        ::PostEvent(WRTInstallerNS::QuitEvent());
833 //}
834
835 void WrtInstaller::installNewPlugins()
836 {
837     LogDebug("Install new plugins");
838
839     if (!PluginUtils::lockPluginInstallation()) {
840         LogInfo("Lock NOT created");
841         return;
842     }
843
844     if (!PluginUtils::checkPluginInstallationRequired()) {
845         LogDebug("Plugin installation not required");
846         PluginUtils::unlockPluginInstallation();
847         return;
848     }
849
850     m_startupPluginInstallation = true;
851     AddStep(&WrtInstaller::installPluginsStep);
852 }
853
854 int main(int argc, char *argv[])
855 {
856     // Output on stdout will be flushed after every newline character,
857     // even if it is redirected to a pipe. This is useful for running
858     // from a script and parsing output.
859     // (Standard behavior of stdlib is to use full buffering when
860     // redirected to a pipe, which means even after an end of line
861     // the output may not be flushed).
862     setlinebuf(stdout);
863
864     // Check and re-set the file open limitation
865     struct rlimit rlim;
866     if (getrlimit(RLIMIT_NOFILE, &rlim) != -1) {
867         LogDebug("RLIMIT_NOFILE sft(" << rlim.rlim_cur << ")" );
868         LogDebug("RLIMIT_NOFILE hrd(" << rlim.rlim_max << ")" );
869
870         if (rlim.rlim_cur < NOFILE_CNT_FOR_INSTALLER) {
871             rlim.rlim_cur = NOFILE_CNT_FOR_INSTALLER;
872             rlim.rlim_max = NOFILE_CNT_FOR_INSTALLER;
873             if (setrlimit(RLIMIT_NOFILE, &rlim) == -1) {
874                 LogError("setrlimit is fail!!");
875             }
876         }
877     } else {
878         LogError("getrlimit is fail!!");
879     }
880
881     // set evas backend type for emulator
882     // popup isn't showed in the emulator,
883     // if backend isn't set to SW backend
884 //    if (GlobalSettings::IsEmulator()) {
885 //        if (setenv("ELM_ENGINE", "x11", 1)) {
886 //            LogDebug("Enable backend");
887 //        }
888 //    }
889
890     WrtInstaller app(argc, argv);
891     int ret = app.Exec();
892     LogDebug("App returned: " << ret);
893     ret = app.getReturnStatus();
894     LogDebug("WrtInstaller returned: " << ret);
895     return ret;
896 }