upload tizen1.0 source
[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 <dpl/file_output.h>
26 #include <dpl/file_input.h>
27 #include <dpl/copy.h>
28 #include <dpl/log/log.h>
29 #include <dpl/wrt-dao-ro/feature_dao_read_only.h>
30 #include <dpl/binary_queue.h> // config.xml logging only
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 #include <config_generator/config_generator.h>
36
37 namespace Jobs {
38 namespace WidgetInstall {
39
40 namespace {
41
42 const std::string TIZEN_KEYWORD = "tizen";
43 const char* BACKKEY = "backbutton-presence";
44 const char* ENABLE = "enable";
45 const char* DISABLE = "disable";
46 const char* ROTATION = "rotation-lock";
47 const char* LANDSCAPE = "landscape";
48 const char* PORTRAIT = "portrait";
49 const char* INDICATOR = "indicator-presence";
50
51 const char* VIEWMODES[] = {
52         "windowed ",
53         "floating ",
54         "fullscreen ",
55         "maximized ",
56         "minimized "};
57
58 } // namespace
59
60 TaskPrepareFiles::TaskPrepareFiles(InstallerContext &installerContext) :
61     DPL::TaskDecl<TaskPrepareFiles>(this),
62     m_installerContext(installerContext)
63 {
64     // Install steps
65     AddStep(&TaskPrepareFiles::StepShowPopup);
66     AddStep(&TaskPrepareFiles::StepCreateTempPath);
67     AddStep(&TaskPrepareFiles::StepCopyIcon);
68     AddStep(&TaskPrepareFiles::StepCreateConfig);
69
70     AddAbortStep(&TaskPrepareFiles::StepAbort);
71 }
72
73 void TaskPrepareFiles::StepCreateTempPath()
74 {
75     m_installerContext.tempWidgetPath = createTempPath();
76 }
77
78 void TaskPrepareFiles::StepCopyIcon()
79 {
80     if(m_installerContext.iconPath.empty()) {
81         LogWarning("No icon specified");
82         return;
83     }
84
85     DPL::String path = DPL::FromUTF8String(m_installerContext.iconPath);
86     std::string filename = m_installerContext.iconPath;
87     size_t last = m_installerContext.iconPath.find_last_of( "\\/" );
88     if(last != std::string::npos) {
89         filename = m_installerContext.iconPath.substr( last+1 );
90     }
91     std::string target = m_installerContext.tempWidgetPath + '/' + filename;
92     LogDebug("source " << m_installerContext.iconPath << "#");
93     LogDebug("target " << target);
94
95     Try
96     {
97         DPL::FileInput input(m_installerContext.iconPath);
98         DPL::FileOutput output(target);
99         DPL::Copy(&input, &output);
100         m_installerContext.iconPath = target;
101     }
102     Catch(DPL::FileInput::Exception::Base)
103     {
104         LogError("File input error");
105         // Error while opening or closing source file
106         ReThrowMsg(Exceptions::CopyIconFailed, m_installerContext.iconPath);
107     }
108     Catch(DPL::FileOutput::Exception::Base)
109     {
110         LogError("File output error");
111         // Error while opening or closing target file
112         ReThrowMsg(Exceptions::CopyIconFailed, target);
113     }
114     Catch(DPL::CopyFailed)
115     {
116         LogError("File copy error");
117         // Error while copying
118         ReThrowMsg(Exceptions::CopyIconFailed, target);
119     }
120 }
121
122 void TaskPrepareFiles::StepShowPopup()
123 {
124     LogError("Not implemented");
125     /*
126      * TODO show configuration popup, store results in installer context or
127      * abort. The icon path is stored in m_installerContext.iconPath. It should
128      * be updated if necessary.
129      */
130
131     // TODO get user options from popup
132     m_enableIndicator = true;
133     m_backKey = false;
134     (void)m_portrait;
135     m_viewModes.push_back(FULLSCREEN);
136     m_viewModes.push_back(WINDOWED);
137     m_viewModes.push_back(FLOATING);
138     m_viewModes.unique();
139 }
140
141
142 void TaskPrepareFiles::StepCreateConfig()
143 {
144     LogInfo("creating config.xml...");
145
146     Try
147     {
148         // create document
149         ConfigXml::DocumentPtr doc = ConfigXml::Document::Create();
150
151         // setup view modes
152         std::string modes;
153         FOREACH(it, m_viewModes) {
154             modes += VIEWMODES[*it];
155         }
156
157         ConfigXml::ElementPtr root = doc->Add<ConfigXml::WIDGET>(
158                 m_installerContext.widgetSource.c_str(),
159                 modes.c_str());
160
161         // access to all sites
162         root->Add<ConfigXml::ACCESS>("*");
163
164         // add features
165         {
166             using namespace WrtDB;
167             FeatureHandleList features = FeatureDAOReadOnly::GetHandleList();
168             FOREACH(it, features) {
169                 FeatureDAOReadOnly feature(*it);
170                 std::string name = feature.GetName();
171                 // tizen only
172                 if (std::string::npos != name.find(TIZEN_KEYWORD)) {
173                     root->Add<ConfigXml::FEATURE>(name.c_str(), false);
174                 }
175             }
176         }
177
178         // add settings
179         if (!!m_enableIndicator) {
180             root->Add<ConfigXml::TIZEN_SETTING>(
181                     INDICATOR,
182                     (*m_enableIndicator ? ENABLE : DISABLE));
183         }
184         if (!!m_backKey) {
185             root->Add<ConfigXml::TIZEN_SETTING>(
186                     BACKKEY,
187                     (*m_backKey ? ENABLE : DISABLE));
188         }
189         if (!!m_portrait) {
190             root->Add<ConfigXml::TIZEN_SETTING>(
191                     ROTATION,
192                     (*m_portrait ? PORTRAIT : LANDSCAPE));
193         }
194
195         DPL::BinaryQueue bq;
196         doc->Write(bq);
197
198         std::unique_ptr<char[]> buffer(new char[bq.Size()]);
199
200         bq.FlattenConsume(buffer.get(),bq.Size());
201         LogInfo("Generated XML:\n\n" << buffer.get());
202
203         std::string configPath =
204             m_installerContext.tempWidgetPath + "/config.xml";
205
206         DPL::FileOutput fo(configPath);
207         doc->Write(fo);
208         fo.Close();
209
210         // Done
211         LogInfo("Config xml created");
212     }
213     Catch(ConfigXml::Base)
214     {
215         LogError("Config xml creation failed");
216         ReThrowMsg(ConfigXml::Base, m_installerContext.widgetSource);
217     }
218 }
219
220 void TaskPrepareFiles::StepAbort()
221 {
222     removeTemporaryDir(m_installerContext.tempWidgetPath);
223 }
224
225 } // namespace WidgetInstall
226 } // namespace Jobs