WidgetHandle removal - part 2. Task order change
[framework/web/wrt-installer.git] / src / jobs / widget_install / task_database.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_new_db_insert.cpp
18  * @author  Lukasz Wrzosek(l.wrzosek@samsung.com)
19  * @author  Soyoung kim(sy037.kim@samsung.com)
20  * @version 1.0
21  * @brief   Implementation file for installer task database updating for widget update
22  */
23 #include <time.h>
24 #include <sys/stat.h>
25 #include <widget_install/task_database.h>
26 #include <widget_install/job_widget_install.h>
27 #include <widget_install/widget_install_errors.h>
28 #include <widget_install/widget_install_context.h>
29 //#include <dpl/wrt-dao-ro/config_parser_data.h>
30 #include <dpl/wrt-dao-rw/widget_dao.h>
31 #include <dpl/foreach.h>
32 #include <dpl/utils/wrt_utility.h>
33 #include <dpl/log/log.h>
34 #include <dpl/assert.h>
35 //#include <dpl/wrt-dao-ro/global_config.h>
36 #include <string>
37 #include <sstream>
38
39 using namespace WrtDB;
40
41 namespace Jobs {
42 namespace WidgetInstall {
43 TaskDatabase::TaskDatabase(InstallerContext& context) :
44     DPL::TaskDecl<TaskDatabase>(this),
45     m_context(context)
46 {
47     AddStep(&TaskDatabase::StepRegisterExternalFiles);
48     AddStep(&TaskDatabase::StepDBInsert);
49
50     AddAbortStep(&TaskDatabase::StepAbortDBInsert);
51 }
52
53 void TaskDatabase::StepDBInsert()
54 {
55     Try
56     {
57         /* Set install Time */
58         time(&m_context.widgetConfig.installedTime);
59
60         if (m_context.existingWidgetInfo.isExist) //update
61         {
62             LogInfo("Registering widget... (update)");
63             WidgetDAO::registerOrUpdateWidget(
64                     m_context.locations->getPkgname(),
65                     m_context.widgetConfig,
66                     m_context.wacSecurity);
67         }
68         else //new installation
69         {
70             LogInfo("Registering widget...");
71             WidgetDAO::registerWidget(
72                     m_context.locations->getPkgname(),
73                     m_context.widgetConfig,
74                     m_context.wacSecurity);
75         }
76
77         FOREACH (cap, m_context.staticPermittedDevCaps) {
78             LogInfo("staticPermittedDevCaps : " << cap->first
79                     << " smack status: " << cap->second);
80         }
81
82         LogInfo("Widget registered");
83     }
84     Catch(WidgetDAO::Exception::DatabaseError)
85     {
86         LogError("Database failure!");
87         ReThrowMsg(Exceptions::InsertNewWidgetFailed, "Database failure!");
88     }
89     Catch(DPL::DB::SqlConnection::Exception::Base)
90     {
91         LogError("Database failure!");
92         ReThrowMsg(Exceptions::InsertNewWidgetFailed, "Database failure!");
93     }
94
95     m_context.job->UpdateProgress(
96         InstallerContext::INSTALL_NEW_DB_INSERT,
97         "New Widget DB UPDATE Finished");
98 }
99
100 void TaskDatabase::StepRegisterExternalFiles()
101 {
102     WrtDB::ExternalLocationList externalLocationsUpdate = m_context.locations->listExternalLocations();
103     if (m_context.existingWidgetInfo.isExist) //update
104     {
105         WidgetDAO dao(m_context.locations->getPkgname());
106         WrtDB::ExternalLocationList externalLocationsDB = dao.getWidgetExternalLocations();
107         LogDebug("Removing external files:");
108         FOREACH(file, externalLocationsDB)
109         {
110             if(std::find(externalLocationsUpdate.begin(), externalLocationsUpdate.end(), *file) == externalLocationsUpdate.end())
111             {
112                 if(WrtUtilFileExists(*file))
113                 {
114                     LogDebug("  -> " << *file);
115                     remove(file->c_str());
116                 }
117                 else if(WrtUtilDirExists(*file))
118                 {
119                     LogDebug("  -> " << *file);
120                     if(!WrtUtilRemove(*file)){
121                         ThrowMsg(Exceptions::RemovingFolderFailure,
122                                 "Failed to remove external directory");
123                     }
124                 }
125                 else
126                 {
127                     LogWarning("  -> " << *file << "(no such a path)");
128                 }
129             }
130         }
131     }
132     LogDebug("Registering external files:");
133     FOREACH(file, externalLocationsUpdate)
134     {
135         LogDebug("  -> " << *file);
136     }
137
138     //set external lcoations to be registered
139     m_context.widgetConfig.externalLocations = externalLocationsUpdate;
140 }
141
142 void TaskDatabase::StepAbortDBInsert()
143 {
144     LogWarning("[DB Update Task] Aborting... (DB Clean)");
145     Try
146     {
147         WidgetDAO::unregisterWidget(m_context.locations->getPkgname());
148
149         LogDebug("Cleaning DB successful!");
150     }
151     Catch(DPL::DB::SqlConnection::Exception::Base)
152     {
153         LogError("Failed to handle StepAbortDBClean!");
154     }
155 }
156
157 } //namespace WidgetInstall
158 } //namespace Jobs