347b391f6619c5b4a7e32d319e971415e7612fa0
[framework/web/wrt-installer.git] / src / jobs / widget_uninstall / task_db_update.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    task_db_update.cpp
18  * @author  Lukasz Wrzosek(l.wrzosek@samsung.com)
19  * @version 1.0
20  * @brief   Implementation file for uninstaller task database updating
21  */
22
23 #include <dpl/platform.h>
24 #include <widget_uninstall/task_db_update.h>
25 #include <widget_uninstall/job_widget_uninstall.h>
26 #include <widget_uninstall/widget_uninstall_errors.h>
27 #if USE(WEB_PROVIDER)
28 #include <web_provider_widget_info.h>
29 #endif
30 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
31 #include <dpl/assert.h>
32 #include <dpl/wrt-dao-rw/widget_dao.h>
33 #include <boost/filesystem.hpp>
34 #include <dpl/log/secure_log.h>
35
36 using namespace WrtDB;
37 namespace bf = boost::filesystem;
38
39 namespace Jobs {
40 namespace WidgetUninstall {
41 TaskDbUpdate::TaskDbUpdate(UninstallerContext& context) :
42     DPL::TaskDecl<TaskDbUpdate>(this),
43     m_context(context)
44 {
45     AddStep(&TaskDbUpdate::StartStep);
46     AddStep(&TaskDbUpdate::StepRemoveExternalLocations);
47     AddStep(&TaskDbUpdate::StepDbUpdate);
48 #if USE(WEB_PROVIDER)
49     AddStep(&TaskDbUpdate::StepLiveboxDBDelete);
50 #endif
51     AddStep(&TaskDbUpdate::EndStep);
52 }
53
54 TaskDbUpdate::~TaskDbUpdate()
55 {}
56
57 void TaskDbUpdate::StepDbUpdate()
58 {
59     Try
60     {
61         //TODO: widget handle should not be used any more
62         FOREACH(it , m_context.tzAppIdList){
63             WidgetDAO::unregisterWidget(*it);
64         }
65         _D("Unregistered widget successfully!");
66     }
67     Catch(DPL::DB::SqlConnection::Exception::Base)
68     {
69         _E("Failed to handle StepDbUpdate!");
70         ReThrowMsg(Exceptions::DatabaseFailure,
71                    "Failed to handle StepDbUpdate!");
72     }
73 }
74
75 #if USE(WEB_PROVIDER)
76 void TaskDbUpdate::StepLiveboxDBDelete()
77 {
78     FOREACH(it, m_context.tzAppIdList){
79     int ret =
80             web_provider_widget_delete_by_app_id(DPL::ToUTF8String(*it).c_str());
81
82         if (ret < 0) {
83             _D("failed to delete box info");
84         } else {
85             _D("delete box info: %s", it);
86         }
87     }
88 }
89 #endif
90
91 void TaskDbUpdate::StepRemoveExternalLocations()
92 {
93     if (!m_context.removeAbnormal) {
94         FOREACH(it, m_context.tzAppIdList){
95             WidgetDAO dao(*it);
96             _D("Removing external locations:");
97             WrtDB::ExternalLocationList externalPaths = dao.getWidgetExternalLocations();
98             FOREACH(file, externalPaths)
99             {
100                 bf::path path(*file);
101                 try {
102                     if(bf::exists(path)){
103                         JobWidgetUninstall::SecureRemove(path);
104                     } else {
105                         _W("  -> %s(no such a path)", path.c_str());
106                     }
107                 } catch (const bf::filesystem_error& ex) {
108                     _E("Failed to remove the file: %s", path.c_str());
109                     Throw(Jobs::WidgetUninstall::TaskDbUpdate::Exception::RemoveFilesFailed);
110                 }
111             }
112             dao.unregisterAllExternalLocations();
113         }
114     }
115 }
116
117 void TaskDbUpdate::StartStep()
118 {
119     LOGI("--------- <TaskDbUpdate> : START ----------");
120 }
121
122 void TaskDbUpdate::EndStep()
123 {
124     m_context.job->UpdateProgress(
125         UninstallerContext::UNINSTALL_DB_UPDATE,
126         "Widget DB Update Finished");
127
128     LOGI("--------- <TaskDbUpdate> : END ----------");
129 }
130 } //namespace WidgetUninstall
131 } //namespace Jobs