5b02a952d319061bcbed3a78cc998fcaff895394
[framework/web/wrt-installer.git] / src / jobs / widget_install / 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 installer task database updating
21  */
22 //#include <time.h>
23 #include <widget_install/task_db_update.h>
24 #include <widget_install/job_widget_install.h>
25 #include <widget_install/widget_install_errors.h>
26 #include <widget_install/widget_install_context.h>
27 #include <dpl/wrt-dao-ro/config_parser_data.h>
28 #include <dpl/utils/wrt_utility.h>
29 #include <dpl/wrt-dao-rw/widget_dao.h>
30 #include <dpl/foreach.h>
31 #include <dpl/log/log.h>
32 #include <dpl/assert.h>
33 #include <dpl/wrt-dao-ro/global_config.h>
34 #include <string>
35 #include <Ecore_File.h>
36 #include <sstream>
37
38 using namespace WrtDB;
39
40 namespace Jobs {
41 namespace WidgetInstall {
42 TaskDbUpdate::TaskDbUpdate(InstallerContext& context) :
43     DPL::TaskDecl<TaskDbUpdate>(this),
44     m_context(context)
45 {
46     AddStep(&TaskDbUpdate::StepDbUpdate);
47
48     AddAbortStep(&TaskDbUpdate::StepAbortDBUpdate);
49 }
50
51 void TaskDbUpdate::StepDbUpdate()
52 {
53     Try
54     {
55         // If there is existing model, remove its database data
56         if (true == m_context.existingWidgetInfo.isExist) {
57             WidgetHandle old = m_context.existingWidgetInfo.existingHandle;
58             LogInfo("Unregistering widget...: " << old);
59             WidgetDAO::unregisterWidget(old);
60             LogInfo("Widget unregistered");
61         }
62
63         /* Set install Time */
64         time(&m_context.widgetConfig.installedTime);
65
66         LogInfo("Registering widget...");
67
68         WidgetDAO::registerWidget(
69                 *(m_context.widgetHandle),
70                 m_context.widgetConfig,
71                 m_context.wacSecurity);
72
73         FOREACH (cap, m_context.staticPermittedDevCaps) {
74             LogInfo("staticPermittedDevCaps : " << cap->first
75                     << " smack status: " << cap->second);
76         }
77
78         Assert(!!m_context.widgetConfig.pkgname
79                && "pkgName should be initialized");
80
81         WrtDB::WidgetDAO widgetDao(*m_context.widgetHandle);
82         widgetDao.setPkgName(m_context.widgetConfig.pkgname);
83
84         LogInfo("Widget registered");
85     }
86     Catch(WidgetDAO::Exception::DatabaseError)
87     {
88         LogWarning("Database failure!");
89         ReThrowMsg(Exceptions::DatabaseFailure, "Database failure!");
90     }
91     Catch(DPL::DB::SqlConnection::Exception::Base)
92     {
93         LogDebug("Database failure!");
94         ReThrowMsg(Exceptions::DatabaseFailure, "Database failure!");
95     }
96
97     m_context.job->UpdateProgress(
98         InstallerContext::INSTALL_DB_UPDATE,
99         "Widget DB UPDATE Finished");
100 }
101
102 void TaskDbUpdate::StepAbortDBUpdate()
103 {
104     LogWarning("[DB Update Task] Aborting... (DB Clean)");
105     Assert(!!m_context.widgetHandle);
106     Try
107     {
108         WidgetDAO::unregisterWidget(*m_context.widgetHandle);
109
110         LogDebug("Cleaning DB successful!");
111     }
112     Catch(DPL::DB::SqlConnection::Exception::Base)
113     {
114         //TODO What should happen here?
115         LogError("Failed to handle StepAbortDBClean!");
116         //        ReThrowMsg(Exceptions::DbStepFailed, "Failed to handle StepAbortDBClean!");
117     }
118 }
119
120 } //namespace WidgetInstall
121 } //namespace Jobs