Update wrt_0.8.85
[platform/framework/web/wrt.git] / src / domain / widget_localize_model.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    widget_localize_model.cpp
18  * @author  Piotr Marcinkiewicz (p.marcinkiew@samsung.com)
19  * @version 1.0
20  * @brief   Widget localize localizes WidgetModel from system configuration
21  */
22
23 #include <algorithm>
24
25 #include <dpl/foreach.h>
26 #include <dpl/log/log.h>
27
28 #include <widget_model.h>
29 #include <dpl/localization/w3c_file_localization.h>
30 #include <dpl/localization/localization_utils.h>
31
32 namespace Domain {
33
34 void localizeWidgetModel(const std::shared_ptr<WidgetModel>& model)
35 {
36     using namespace WrtDB;
37
38     LogDebug("Updating model with localized info ...");
39     LanguageTagsList tags = LocalizationUtils::GetUserAgentLanguageTags();
40     DPL::OptionalString defaultlocale = model->defaultlocale.Get();
41     LogDebug("Default locale for widget: " << defaultlocale);
42     if (!!defaultlocale &&
43         std::find(tags.begin(), tags.end(), *defaultlocale) == tags.end())
44     {
45         if (tags.size() < 2) {
46             tags.push_front(*defaultlocale);
47         } else {
48             LanguageTagsList::iterator placeToInsert = tags.end();
49             --placeToInsert;
50             if (*placeToInsert != L"") { ++placeToInsert; }
51             tags.insert(placeToInsert, *defaultlocale);
52         }
53     }
54     LogDebug("Setting runtime widget locale:");
55     FOREACH(it, tags) {
56         LogDebug("Locale: " << *it);
57     }
58     model->LanguageTags.Set(tags);
59
60     WidgetLocalizedInfo localizedInfo =
61         W3CFileLocalization::getLocalizedInfo(model->Handle.Get());
62
63     model->Name.Set(localizedInfo.name);
64     model->ShortName.Set(localizedInfo.shortName);
65     model->Description.Set(localizedInfo.description);
66     model->License.Set(localizedInfo.license);
67     model->LicenseHref.Set(localizedInfo.licenseHref);
68     model->StartURL.Set(W3CFileLocalization::getStartFile(model->Handle.Get()));
69     model->Icon.Set(W3CFileLocalization::getIcon(model->Handle.Get()));
70     LogDebug("... finished");
71     OptionalWidgetStartFileInfo info =
72         W3CFileLocalization::getStartFileInfo(
73             model->Handle.Get(),
74             model->LanguageTags.Get());
75     model->StartFileInfo.Set(info);
76 }
77
78 } //Namespace Domain