4f794b6ea23df50711c17e29ef9b99dce68d611d
[framework/web/wrt-installer.git] / src / misc / widget_install_to_external.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        widget_install_to_external.cpp
18  * @author      Soyoung Kim (sy037.kim@smasung.com)
19  */
20 #include "widget_install_to_external.h"
21
22 #include <dpl/singleton_safe_impl.h>
23 #include <dpl/assert.h>
24 #include <dpl/log/log.h>
25
26 IMPLEMENT_SAFE_SINGLETON(WidgetInstallToExt)
27
28 WidgetInstallToExt::WidgetInstallToExt() :
29     m_handle(NULL),
30     m_appId("")
31 {}
32
33 WidgetInstallToExt::~WidgetInstallToExt()
34 {}
35
36 void WidgetInstallToExt::initialize(std::string appId)
37 {
38     LogDebug("WidgetInstallToExt::initialize()");
39     m_appId = appId;
40
41     m_handle = app2ext_init(APP2EXT_SD_CARD);
42     if (NULL == m_handle) {
43         ThrowMsg(Exception::ErrorInstallToExt, "initialize failed");
44     }
45 }
46
47 void WidgetInstallToExt::deinitialize()
48 {
49     LogDebug("WidgetInstallToExt::deinitialize()");
50     if (NULL != m_handle) {
51         if (0 < app2ext_deinit(m_handle)) {
52             ThrowMsg(Exception::ErrorInstallToExt,
53                      "app2ext deinitialize \
54                     failed");
55         }
56     }
57 }
58
59 void WidgetInstallToExt::preInstallation(GList *dirList, int dSize)
60 {
61     LogDebug("WidgetInstallToExt::preInstallation()");
62     Assert(m_handle);
63
64     int ret = m_handle->interface.pre_install(m_appId.c_str(), dirList, dSize);
65
66     if (APP2EXT_SUCCESS == ret) {
67         LogDebug("App2Ext pre install success");
68     } else {
69         postInstallation(false);
70         ThrowMsg(Exception::ErrorInstallToExt, "pre-install failed");
71     }
72 }
73
74 void WidgetInstallToExt::postInstallation(bool status)
75 {
76     LogDebug("WidgetInstallToExt::postInstallation()");
77     Assert(m_handle);
78
79     if (status) {
80         m_handle->interface.post_install(m_appId.c_str(),
81                                          APP2EXT_STATUS_SUCCESS);
82     } else {
83         m_handle->interface.post_install(m_appId.c_str(),
84                                          APP2EXT_STATUS_FAILED);
85     }
86 }
87
88 void WidgetInstallToExt::preUpgrade(GList *dirList, int dSize)
89 {
90     LogDebug("WidgetInstallToExt::preUpgrade()");
91     Assert(m_handle);
92
93     int ret = m_handle->interface.pre_upgrade(m_appId.c_str(), dirList, dSize);
94     if (APP2EXT_SUCCESS == ret) {
95         LogDebug("App2Ext pre-upgrade success");
96     } else {
97         postUpgrade(false);
98         ThrowMsg(Exception::ErrorInstallToExt, "pre-upgrade failed");
99     }
100 }
101
102 void WidgetInstallToExt::postUpgrade(bool status)
103 {
104     LogDebug("WidgetInstallToExt::postUpgrade()");
105     Assert(m_handle);
106
107     if (status) {
108         m_handle->interface.post_upgrade(m_appId.c_str(),
109                                          APP2EXT_STATUS_SUCCESS);
110     } else {
111         m_handle->interface.post_upgrade(m_appId.c_str(),
112                                          APP2EXT_STATUS_FAILED);
113     }
114 }
115
116 void WidgetInstallToExt::uninstallation()
117 {
118     LogDebug("WidgetInstallToExt::uninstallation()");
119
120     Assert(m_handle);
121
122     int ret = m_handle->interface.pre_uninstall(m_appId.c_str());
123     if (APP2EXT_SUCCESS == ret) {
124         if (APP2EXT_SUCCESS ==
125             m_handle->interface.post_uninstall(m_appId.c_str()))
126         {
127             LogDebug("App2Ext pre-uninstall success");
128         } else {
129             ThrowMsg(Exception::ErrorInstallToExt, "post-uninstall failed");
130         }
131     } else {
132         ThrowMsg(Exception::ErrorInstallToExt, "pre-uninstall failed");
133     }
134 }