b0219dd1fb645d7d7af4e7008015bcadffde6f8b
[framework/web/wrt-installer.git] / src_mobile / 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 <installer_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     _D("WidgetInstallToExt::initialize()");
39     m_appId = appId;
40
41     if (NULL == m_handle) {
42         m_handle = app2ext_init(APP2EXT_SD_CARD);
43         if (NULL == m_handle) {
44             ThrowMsg(Exception::ErrorInstallToExt, "initialize failed");
45         }
46     }
47 }
48
49 void WidgetInstallToExt::deinitialize()
50 {
51     _D("WidgetInstallToExt::deinitialize()");
52     if (NULL != m_handle) {
53         if (0 < app2ext_deinit(m_handle)) {
54             ThrowMsg(Exception::ErrorInstallToExt,
55                      "app2ext deinitialize \
56                     failed");
57         }
58     }
59 }
60
61 void WidgetInstallToExt::preInstallation(GList *dirList, int dSize)
62 {
63     _D("WidgetInstallToExt::preInstallation()");
64     Assert(m_handle);
65
66     int ret = m_handle->interface.pre_install(m_appId.c_str(), dirList, dSize);
67
68     if (APP2EXT_SUCCESS == ret) {
69         _D("App2Ext pre install success");
70     } else {
71         postInstallation(false);
72         ThrowMsg(Exception::ErrorInstallToExt, "pre-install failed");
73     }
74 }
75
76 void WidgetInstallToExt::postInstallation(bool status)
77 {
78     _D("WidgetInstallToExt::postInstallation()");
79
80     if (NULL != m_handle) {
81         if (status) {
82             m_handle->interface.post_install(m_appId.c_str(),
83                                              APP2EXT_STATUS_SUCCESS);
84         } else {
85             m_handle->interface.post_install(m_appId.c_str(),
86                                              APP2EXT_STATUS_FAILED);
87         }
88     }
89 }
90
91 void WidgetInstallToExt::preUpgrade(GList *dirList, int dSize)
92 {
93     _D("WidgetInstallToExt::preUpgrade()");
94     Assert(m_handle);
95
96     int ret = m_handle->interface.pre_upgrade(m_appId.c_str(), dirList, dSize);
97     if (APP2EXT_SUCCESS == ret) {
98         _D("App2Ext pre-upgrade success");
99     } else {
100         postUpgrade(false);
101         ThrowMsg(Exception::ErrorInstallToExt, "pre-upgrade failed");
102     }
103 }
104
105 void WidgetInstallToExt::postUpgrade(bool status)
106 {
107     _D("WidgetInstallToExt::postUpgrade()");
108     if (NULL != m_handle) {
109         if (status) {
110             m_handle->interface.post_upgrade(m_appId.c_str(),
111                                              APP2EXT_STATUS_SUCCESS);
112         } else {
113             m_handle->interface.post_upgrade(m_appId.c_str(),
114                                              APP2EXT_STATUS_FAILED);
115         }
116     }
117 }
118
119 void WidgetInstallToExt::uninstallation()
120 {
121     _D("WidgetInstallToExt::uninstallation()");
122
123     Assert(m_handle);
124
125     int ret = m_handle->interface.pre_uninstall(m_appId.c_str());
126     if (APP2EXT_SUCCESS == ret) {
127         if (APP2EXT_SUCCESS ==
128             m_handle->interface.post_uninstall(m_appId.c_str()))
129         {
130             _D("App2Ext pre-uninstall success");
131         } else {
132             ThrowMsg(Exception::ErrorInstallToExt, "post-uninstall failed");
133         }
134     } else {
135         ThrowMsg(Exception::ErrorInstallToExt, "pre-uninstall failed");
136     }
137 }
138
139 void WidgetInstallToExt::disable()
140 {
141     _D("WidgetInstallToExt::disable()");
142     if (NULL != m_handle) {
143         int ret = m_handle->interface.disable(m_appId.c_str());
144         if (APP2EXT_SUCCESS != ret && APP2EXT_ERROR_UNMOUNT != ret) {
145             ThrowMsg(Exception::ErrorInstallToExt, "disable failed");
146         }
147     }
148 }