|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (developer.feedback@nokia.com) |
|
6 ** |
|
7 ** This file is part of the HbCore module of the UI Extensions for Mobile. |
|
8 ** |
|
9 ** GNU Lesser General Public License Usage |
|
10 ** This file may be used under the terms of the GNU Lesser General Public |
|
11 ** License version 2.1 as published by the Free Software Foundation and |
|
12 ** appearing in the file LICENSE.LGPL included in the packaging of this file. |
|
13 ** Please review the following information to ensure the GNU Lesser General |
|
14 ** Public License version 2.1 requirements will be met: |
|
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
16 ** |
|
17 ** In addition, as a special exception, Nokia gives you certain additional |
|
18 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
20 ** |
|
21 ** If you have questions regarding the use of this file, please contact |
|
22 ** Nokia at developer.feedback@nokia.com. |
|
23 ** |
|
24 ****************************************************************************/ |
|
25 |
|
26 #include <QFile> |
|
27 #include <QTextStream> |
|
28 #include <QStringList> |
|
29 #include <QMap> |
|
30 |
|
31 #include "hbwsiniparser_p.h" |
|
32 |
|
33 // ======== MEMBER FUNCTIONS ======== |
|
34 |
|
35 #define WSINI_PARSE_ENTRY(keyword, func) { keyword, &HbWsiniParser::call_##func } |
|
36 const HbWsiniParser::ParserEntry HbWsiniParser::mParseTable[] = |
|
37 { |
|
38 WSINI_PARSE_ENTRY("S60_HWSTATE_KEYCODE", hardwareStateKeycode), //S60_HWSTATE_KEYCODEn <KeyCode> |
|
39 WSINI_PARSE_ENTRY("S60_HWSTATE_SCREENMODE", hardwareStateScreenMode), //S60_HWSTATE_SCREENMODEn <ScreenModeNumber> |
|
40 WSINI_PARSE_ENTRY("S60_HWSTATE_ALT_SCREENMODE", hardwareStateAltScreenMode), //S60_HWSTATE_ALT_SCREENMODEn <ScreenModeNumber> |
|
41 WSINI_PARSE_ENTRY("S60_SCR_SOFTKEY_LOCATION", screenModeSoftkeyLocation), //S60_SCR_SOFTKEY_LOCATIONn <Location> |
|
42 WSINI_PARSE_ENTRY("S60_SCR_STYLE_NAME", screenModeStyleName), //S60_SCR_STYLE_NAMEn <ScreenStyleName> |
|
43 WSINI_PARSE_ENTRY("SCR_WIDTH", screenWidth), //SCR_WIDTHn <ScreenStyleName> |
|
44 WSINI_PARSE_ENTRY("SCR_HEIGHT", screenHeight) //SCR_HEIGHTn <ScreenStyleName> |
|
45 }; |
|
46 |
|
47 /*! |
|
48 Parses the wsini file and puts the found screen modes into the referenced map |
|
49 |
|
50 On non-Symbian OSes will normally insert no screen modes. If, on Windows, |
|
51 a wsini file is put in the directory c:\hb\data, then that wsini will |
|
52 simulate the behavior on a Symbian device. |
|
53 */ |
|
54 void HbWsiniParser::parseModes(QMap<int, HbScreenMode> &modes) |
|
55 { |
|
56 HbWsiniParser parser(modes); |
|
57 #if defined(Q_WS_S60) |
|
58 parser.parseFile("z:\\system\\data\\wsini.ini"); |
|
59 #elif defined(Q_OS_WIN32) |
|
60 parser.parseFile("c:/hb/data/wsini.ini"); |
|
61 #endif |
|
62 } |
|
63 |
|
64 HbWsiniParser::HbWsiniParser(QMap<int, HbScreenMode> &modes) |
|
65 : mDeviceModes(modes) |
|
66 { |
|
67 } |
|
68 |
|
69 void HbWsiniParser::parseFile(const QString& filename) |
|
70 { |
|
71 QFile file(filename); |
|
72 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) |
|
73 return; |
|
74 |
|
75 // Wsini.ini file is always UTF-16. Use QTextStream and set the codec |
|
76 QTextStream input(&file); |
|
77 input.setCodec("UTF-16"); |
|
78 |
|
79 while (!input.atEnd()) { |
|
80 QString line = input.readLine(); |
|
81 // Is it all whitespace? |
|
82 QString trimmedLine = line.trimmed(); |
|
83 if (!trimmedLine.isEmpty()){ |
|
84 parseLine(trimmedLine); |
|
85 } |
|
86 } |
|
87 |
|
88 return; |
|
89 } |
|
90 |
|
91 void HbWsiniParser::parseLine(const QString &line) |
|
92 { |
|
93 // Split at whitespace |
|
94 QStringList tokens = line.split(QRegExp("\\s+")); |
|
95 |
|
96 // strip off square brackets from first token |
|
97 QString first = tokens[0].remove(QRegExp("[^A-Z0-9_]")); |
|
98 |
|
99 if (!first.isEmpty()) { |
|
100 QString number; |
|
101 // Keywords have screen mode number appended to them. Need to separate |
|
102 // the number part from the end |
|
103 int pos = first.lastIndexOf( QRegExp("[A-Z_]") ); |
|
104 int lastCharacter = first.length()-1; |
|
105 if (pos >= 0 && pos < lastCharacter) { |
|
106 int numDigits = lastCharacter - pos; |
|
107 number = first.right(numDigits); |
|
108 first.chop(numDigits); |
|
109 } |
|
110 |
|
111 if (!number.isEmpty()){ |
|
112 tokens.replace(0, number); // Number becomes first token |
|
113 } |
|
114 else{ |
|
115 tokens.removeAt(0); // remove the keyword from the tokens |
|
116 } |
|
117 parseTokens(first, tokens); |
|
118 } |
|
119 } |
|
120 |
|
121 void HbWsiniParser::parseTokens(const QString &keyword, const QStringList &line) |
|
122 { |
|
123 if (keyword.isEmpty()) |
|
124 return; |
|
125 |
|
126 int targetCount = sizeof(mParseTable)/sizeof(ParserEntry); |
|
127 for (int ii=0; ii<targetCount; ii++) |
|
128 { |
|
129 const ParserEntry& parseEntry = mParseTable[ii]; |
|
130 QString header(parseEntry.iKeyword); |
|
131 if (keyword == header) |
|
132 { |
|
133 (*parseEntry.iFunc)(this, line); |
|
134 break; |
|
135 } |
|
136 } |
|
137 } |
|
138 |
|
139 /*! |
|
140 Parser method for screen width. |
|
141 Implementation is not refactored to help show correspondence between parsed content |
|
142 and the parsing steps. |
|
143 */ |
|
144 void HbWsiniParser::screenWidth(const QStringList &line) |
|
145 { |
|
146 bool convertedOk; |
|
147 int stateNumber = line[0].toInt(&convertedOk); |
|
148 ensureModeExists(stateNumber); |
|
149 int width = line[1].toInt(&convertedOk); |
|
150 mDeviceModes[stateNumber].setPixelWidth(width); |
|
151 } |
|
152 |
|
153 /*! |
|
154 Parser method for screen height. |
|
155 Implementation is not refactored to help show correspondence between parsed content |
|
156 and the parsing steps. |
|
157 */ |
|
158 void HbWsiniParser::screenHeight(const QStringList &line) |
|
159 { |
|
160 bool convertedOk; |
|
161 int stateNumber = line[0].toInt(&convertedOk); |
|
162 ensureModeExists(stateNumber); |
|
163 int height = line[1].toInt(&convertedOk); |
|
164 mDeviceModes[stateNumber].setPixelHeight(height); |
|
165 } |
|
166 |
|
167 /*! |
|
168 Parses a line containing the screen mode name |
|
169 */ |
|
170 void HbWsiniParser::screenModeStyleName(const QStringList &line) |
|
171 { |
|
172 bool convertedOk; |
|
173 int stateNumber = line[0].toInt(&convertedOk); |
|
174 ensureModeExists(stateNumber); |
|
175 mDeviceModes[stateNumber].setName(line[1]); |
|
176 } |
|
177 |
|
178 /*! |
|
179 Stub parser for currently unparsed lines |
|
180 */ |
|
181 void HbWsiniParser::hardwareStateKeycode(const QStringList &line) |
|
182 { |
|
183 Q_UNUSED(line); |
|
184 } |
|
185 |
|
186 /*! |
|
187 Stub parser for currently unparsed lines |
|
188 */ |
|
189 void HbWsiniParser::hardwareStateScreenMode(const QStringList &line) |
|
190 { |
|
191 Q_UNUSED(line); |
|
192 } |
|
193 |
|
194 /*! |
|
195 Stub parser for currently unparsed lines |
|
196 */ |
|
197 void HbWsiniParser::hardwareStateAltScreenMode(const QStringList &line) |
|
198 { |
|
199 Q_UNUSED(line); |
|
200 } |
|
201 |
|
202 /*! |
|
203 Stub parser for currently unparsed lines |
|
204 */ |
|
205 void HbWsiniParser::screenModeSoftkeyLocation(const QStringList &line) |
|
206 { |
|
207 Q_UNUSED(line); |
|
208 } |
|
209 |
|
210 |
|
211 /*! |
|
212 Creates a new mode if necessary |
|
213 */ |
|
214 void HbWsiniParser::ensureModeExists(int stateNumber) |
|
215 { |
|
216 if (!mDeviceModes.contains(stateNumber)) { |
|
217 HbScreenMode mode; |
|
218 HbScreenMode &m(mode); |
|
219 int s(stateNumber); |
|
220 mDeviceModes.insert( s, m ); |
|
221 } |
|
222 } |
|
223 |