Update wrt-installer_0.0.51
[framework/web/wrt-installer.git] / src / jobs / widget_install / task_prepare_files.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_generate_config.cpp
18  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
19  * @version    1.0
20  */
21
22 #include "task_prepare_files.h"
23 #include <memory>
24 #include <string>
25 #include <iostream>
26 #include <dpl/file_output.h>
27 #include <dpl/file_input.h>
28 #include <dpl/copy.h>
29 #include <dpl/log/log.h>
30 #include <dpl/wrt-dao-ro/feature_dao_read_only.h>
31 #include <dpl/foreach.h>
32 #include <widget_install/widget_install_context.h>
33 #include <widget_install_errors.h>
34 #include <task_commons.h>
35
36 namespace Jobs {
37 namespace WidgetInstall {
38
39 TaskPrepareFiles::TaskPrepareFiles(InstallerContext &installerContext) :
40     DPL::TaskDecl<TaskPrepareFiles>(this),
41     m_installerContext(installerContext)
42 {
43     // Install steps
44     AddStep(&TaskPrepareFiles::StepCreateTempPath);
45     AddStep(&TaskPrepareFiles::StepCopyFiles);
46
47     AddAbortStep(&TaskPrepareFiles::StepAbort);
48 }
49
50 void TaskPrepareFiles::StepCreateTempPath()
51 {
52     /*
53      * Config.xml and all icons will be copied to another location so the path
54      * has to be updated.
55      */
56     m_installerContext.tempWidgetPath = createTempPath();
57 }
58
59 void TaskPrepareFiles::CopyFile(const std::string& source)
60 {
61     if(source.empty()) {
62         LogWarning("No source file specified");
63         return;
64     }
65
66     std::string filename = source;
67     size_t last = source.find_last_of( "\\/" );
68     if(last != std::string::npos) {
69         filename = source.substr( last+1 );
70     }
71     std::string target = m_installerContext.tempWidgetPath + '/' + filename;
72     LogDebug("source " << source);
73     LogDebug("target " << target);
74
75     Try
76     {
77         DPL::FileInput input(source);
78         DPL::FileOutput output(target);
79         DPL::Copy(&input, &output);
80     }
81     Catch(DPL::FileInput::Exception::Base)
82     {
83         LogError("File input error");
84         // Error while opening or closing source file
85         ReThrowMsg(Exceptions::CopyIconFailed, source);
86     }
87     Catch(DPL::FileOutput::Exception::Base)
88     {
89         LogError("File output error");
90         // Error while opening or closing target file
91         ReThrowMsg(Exceptions::CopyIconFailed, target);
92     }
93     Catch(DPL::CopyFailed)
94     {
95         LogError("File copy error");
96         // Error while copying
97         ReThrowMsg(Exceptions::CopyIconFailed, target);
98     }
99 }
100
101 void TaskPrepareFiles::StepCopyFiles()
102 {
103     CopyFile(m_installerContext.widgetSource);
104
105     size_t last = m_installerContext.widgetSource.find_last_of("\\/");
106     std::string sourceDir = "";
107     if (last != std::string::npos) {
108         sourceDir = m_installerContext.widgetSource.substr(0,last+1);
109     }
110
111     FOREACH(it, m_installerContext.widgetConfig.configInfo.iconsList) {
112         std::ostringstream os;
113         os << sourceDir << DPL::ToUTF8String(it->src);
114         CopyFile(os.str());
115     }
116 }
117
118 void TaskPrepareFiles::StepAbort()
119 {
120     removeTemporaryDir(m_installerContext.tempWidgetPath);
121 }
122
123 } // namespace WidgetInstall
124 } // namespace Jobs