tizen beta release
[framework/web/wrt-plugins-common.git] / src / modules / tizen / DEPRACATED / System / Environment.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 #include "Environment.h"
17 #include <stdlib.h>
18 #include <sys/utsname.h>
19 #include <stdio.h>
20 #include <limits.h>
21 extern "C" {
22   #include <iniparser.h>
23 }
24 #include <pcrecpp.h>
25 #include <sstream>
26 #include <commons/Exception.h>
27
28 namespace {
29 const char* VARIABLE_LANGUAGE_NAME = "LANG";
30 const char* VENDOR_NAME = "Samsung";
31 const char* FILE_INFO_INI = "/etc/info.ini";
32 const char* FILE_CPUINFO = "/proc/cpuinfo";
33 const char* PATTERN_CPUINFO_HARDWARE = "^Hardware\\s*:\\s+(\\w+)\\s*$";
34 } // anonymous
35
36 namespace WrtPlugins {
37 namespace Platform {
38 namespace System {
39 // TODO: Most of those values (if not any) doesn't have to be recalculated every
40 //       time someone request them. Optimalization could be introduced to do it
41 //       only once, e.g. on first request.
42
43 Environment& Environment::getInstance()
44 {
45     static Environment instance;
46     return instance;
47 }
48
49 std::string Environment::getLanguage() const
50 {
51     return getVariable(VARIABLE_LANGUAGE_NAME);
52 }
53
54 std::string Environment::getOsVersion() const
55 {
56     struct utsname buf;
57     if (uname(&buf) != 0) {
58         ThrowMsg(Commons::PlatformException, "Could not get uname data.");
59     }
60     return buf.sysname;
61 }
62
63 std::string Environment::getSoftwareVersion() const
64 {
65     dictionary* dict = iniparser_load(FILE_INFO_INI);
66     if (dict == NULL) {
67         ThrowMsg(Commons::PlatformException, "Could not get SW version.");
68     }
69     char* major =
70         iniparser_getstring(dict, ("Version:Major"), const_cast<char*>(""));
71
72     const char* minor =
73         iniparser_getstring(dict, "Version:Minor", const_cast<char*>(""));
74
75     std::stringstream ss;
76     ss << major;
77     if ((*major != '\0') && (*minor != '\0')) {
78         ss << "-";
79     }
80     ss << minor;
81     iniparser_freedict(dict);
82
83     return ss.str();
84 }
85
86 std::string Environment::getHardwareName() const
87 {
88     FILE* file = fopen(FILE_CPUINFO, "r");
89     if (file == NULL) {
90         ThrowMsg(Commons::UnsupportedException, "Could not open cpuinfo file.");
91     }
92
93     std::string result;
94     char line[LINE_MAX] = { 0 };
95     pcrecpp::RE re(PATTERN_CPUINFO_HARDWARE);
96     while (fgets(line, LINE_MAX, file) && !re.FullMatch(line, &result)) {
97     }
98     fclose(file);
99
100     if (result.empty()) {
101         ThrowMsg(Commons::UnsupportedException, "Hardware name not found.");
102     }
103
104     return result;
105 }
106
107 std::string Environment::getVendorName() const
108 {
109     return VENDOR_NAME;
110 }
111
112 std::string Environment::getVariable(const char* name)
113 {
114     char* value = getenv(name);
115     if (value == NULL) {
116         std::stringstream ss;
117         ss << "Could not read variable " << name << ".";
118         ThrowMsg(Commons::PlatformException, ss.str());
119     }
120     return value;
121 }
122
123 Environment::Environment()
124 {
125 }
126 } // System
127 } // Platform
128 } // WrtPlugins