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