[Release] wrt-installer_0.1.4 for sdk branch
[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 TaskPrepareFiles::TaskPrepareFiles(InstallerContext &installerContext) :
39     DPL::TaskDecl<TaskPrepareFiles>(this),
40     m_installerContext(installerContext)
41 {
42     // Install steps
43     AddStep(&TaskPrepareFiles::StepCopyFiles);
44 }
45
46 void TaskPrepareFiles::CopyFile(const std::string& source)
47 {
48     if (source.empty()) {
49         LogWarning("No source file specified");
50         return;
51     }
52
53     std::string filename = source;
54     size_t last = source.find_last_of("\\/");
55     if (last != std::string::npos) {
56         filename = source.substr(last + 1);
57     }
58     std::string target =
59         m_installerContext.locations->getTemporaryPackageDir() + '/' +
60         filename;
61     LogDebug("source " << source);
62     LogDebug("target " << target);
63
64     Try
65     {
66         DPL::FileInput input(source);
67         DPL::FileOutput output(target);
68         DPL::Copy(&input, &output);
69     }
70     Catch(DPL::FileInput::Exception::Base)
71     {
72         LogError("File input error");
73         // Error while opening or closing source file
74         ReThrowMsg(Exceptions::CopyIconFailed, source);
75     }
76     Catch(DPL::FileOutput::Exception::Base)
77     {
78         LogError("File output error");
79         // Error while opening or closing target file
80         ReThrowMsg(Exceptions::CopyIconFailed, target);
81     }
82     Catch(DPL::CopyFailed)
83     {
84         LogError("File copy error");
85         // Error while copying
86         ReThrowMsg(Exceptions::CopyIconFailed, target);
87     }
88 }
89
90 void TaskPrepareFiles::StepCopyFiles()
91 {
92     CopyFile(m_installerContext.locations->getWidgetSource());
93
94     size_t last = m_installerContext.locations->getWidgetSource().find_last_of(
95             "\\/");
96     std::string sourceDir = "";
97     if (last != std::string::npos) {
98         sourceDir = m_installerContext.locations->getWidgetSource().substr(
99                 0,
100                 last
101                 + 1);
102     }
103
104     LogDebug("Icons copy...");
105     FOREACH(it, m_installerContext.widgetConfig.configInfo.iconsList) {
106         std::ostringstream os;
107         LogDebug("Coping: " << sourceDir << DPL::ToUTF8String(it->src));
108         os << sourceDir << DPL::ToUTF8String(it->src);
109         CopyFile(os.str());
110     }
111 }
112 } // namespace WidgetInstall
113 } // namespace Jobs