1 | /* |
---|
2 | * SNumber.cpp |
---|
3 | * |
---|
4 | * Created on: May 19, 2009 |
---|
5 | * Author: saemy |
---|
6 | */ |
---|
7 | |
---|
8 | #include "snumber.h" |
---|
9 | |
---|
10 | #include <QDebug> |
---|
11 | #include <QStringList> |
---|
12 | |
---|
13 | #include <StdExceptions.h> |
---|
14 | |
---|
15 | #include <typeconvert.h> |
---|
16 | #include <strutils.h> |
---|
17 | |
---|
18 | /** |
---|
19 | * Format: |
---|
20 | * c country code (1-digit) |
---|
21 | * cc country code (2-digits) |
---|
22 | * CC country code trim zeros |
---|
23 | * |
---|
24 | * a area code (1-digit) |
---|
25 | * aa area code (2-digits) |
---|
26 | * aaa area code (3-digits) |
---|
27 | * AAA area code trim zeros |
---|
28 | * |
---|
29 | * u The user number |
---|
30 | * |
---|
31 | * 'foo' Ignored part |
---|
32 | */ |
---|
33 | const QString SNumber::IsoFormat = "'+'cc' 'aaa' 'u"; // +41 079 1234567 |
---|
34 | const QString SNumber::IsoFormatShort = "'+'CC' 'AAA' 'u"; // +41 79 1234567 |
---|
35 | const QString SNumber::GuessFormat = "%%guess%%"; |
---|
36 | |
---|
37 | SNumber::SNumber() { |
---|
38 | clear(); |
---|
39 | } |
---|
40 | SNumber::SNumber(const QString& numberStr, const QString& format) { |
---|
41 | setNumber(numberStr, format); |
---|
42 | } |
---|
43 | SNumber::SNumber(const SNumber& other) { |
---|
44 | fromString(other.toString("ccaaau"), "ccaaau"); |
---|
45 | } |
---|
46 | |
---|
47 | void SNumber::clear() { |
---|
48 | countryCode_ = 0; |
---|
49 | areaCode_ = 0; |
---|
50 | userNumber_ = ""; |
---|
51 | } |
---|
52 | |
---|
53 | bool SNumber::isEmpty() const { |
---|
54 | return countryCode() == -1; |
---|
55 | } |
---|
56 | bool SNumber::isValid() const { |
---|
57 | return (countryCode_ > 0) && |
---|
58 | (areaCode_ > 0) && |
---|
59 | (userNumber_ != ""); |
---|
60 | } |
---|
61 | |
---|
62 | |
---|
63 | short SNumber::countryCode() const{ |
---|
64 | return countryCode_; |
---|
65 | } |
---|
66 | short SNumber::areaCode() const{ |
---|
67 | return areaCode_; |
---|
68 | } |
---|
69 | QString SNumber::userNumber() const{ |
---|
70 | return userNumber_; |
---|
71 | } |
---|
72 | |
---|
73 | /* CC & AAA are not applicable here! */ |
---|
74 | bool SNumber::setNumber(const QString& numberStr, const QString& format){ |
---|
75 | if (format == SNumber::GuessFormat) { |
---|
76 | QStringList formats; |
---|
77 | formats.append("'+'cc' 'aaa' 'u");// +41 079 1234567 |
---|
78 | formats.append("'+'cc' 'aa' 'u"); // +41 79 1234567 |
---|
79 | formats.append("'+'cc' 'a' 'u"); // +41 7 1234567 |
---|
80 | formats.append("'+'c' 'aaa' 'u"); // +1 079 1234567 |
---|
81 | formats.append("'+'c' 'aa' 'u"); // +1 79 1234567 |
---|
82 | formats.append("'+'c' 'a' 'u"); // +1 7 1234567 |
---|
83 | formats.append("'+'cc' 'aaau"); // +41 0791234567 (Attention! A part of the usernumber could be interpreted as the areacode) |
---|
84 | formats.append("'+'cc' 'aau"); // +41 791234567 (Attention! A part of the usernumber could be interpreted as the areacode) |
---|
85 | formats.append("'+'cc' 'au"); // +41 71234567 (Attention! A part of the usernumber could be interpreted as the areacode) |
---|
86 | formats.append("'+'c' 'aaau"); // +1 0791234567 (Attention! A part of the usernumber could be interpreted as the areacode) |
---|
87 | formats.append("'+'c' 'aau"); // +1 791234567 (Attention! A part of the usernumber could be interpreted as the areacode) |
---|
88 | formats.append("'+'c' 'au"); // +1 71234567 (Attention! A part of the usernumber could be interpreted as the areacode) |
---|
89 | |
---|
90 | foreach(QString format_, formats) { |
---|
91 | if (setNumber(numberStr, format_)) { |
---|
92 | return true; |
---|
93 | } |
---|
94 | } |
---|
95 | return false; |
---|
96 | } |
---|
97 | |
---|
98 | clear(); |
---|
99 | |
---|
100 | qDebug() << "Checking " + format + " on " + numberStr; |
---|
101 | |
---|
102 | const QLatin1Char quote('\''); |
---|
103 | if (format.isEmpty()) |
---|
104 | return false; |
---|
105 | |
---|
106 | bool inQuote = false; |
---|
107 | int j = 0; |
---|
108 | for (int i = 0; i < format.count(); i++) { |
---|
109 | if (numberStr.count() <= j) { |
---|
110 | clear(); |
---|
111 | break; |
---|
112 | } |
---|
113 | |
---|
114 | if (format.at(i) == quote) { |
---|
115 | inQuote = !inQuote; |
---|
116 | continue; |
---|
117 | } |
---|
118 | |
---|
119 | if (inQuote) { |
---|
120 | if (format.at(i) == numberStr.at(j)) { |
---|
121 | j++; |
---|
122 | continue; |
---|
123 | } else { |
---|
124 | clear(); |
---|
125 | break; |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | if (format.mid(i, 2) == "cc") { |
---|
130 | setCountryCode(numberStr.mid(j, 2).toShort()); |
---|
131 | j += 2; |
---|
132 | i++; |
---|
133 | } else if (format.mid(i, 1) == "c") { |
---|
134 | setCountryCode(numberStr.mid(i, 1).toShort()); |
---|
135 | j++; |
---|
136 | } else if (format.mid(i, 3) == "aaa") { |
---|
137 | setAreaCode(numberStr.mid(j, 3).toShort()); |
---|
138 | j += 3; |
---|
139 | i += 2; |
---|
140 | } else if (format.mid(i, 2) == "aa") { |
---|
141 | setAreaCode(numberStr.mid(j, 2).toShort()); |
---|
142 | j += 2; |
---|
143 | i++; |
---|
144 | } else if (format.mid(i, 1) == "a") { |
---|
145 | setAreaCode(numberStr.mid(j, 1).toShort()); |
---|
146 | j++; |
---|
147 | } else if (format.at(i) == 'u') { |
---|
148 | QString userNumber = ""; |
---|
149 | while (j < numberStr.count()) { |
---|
150 | userNumber += numberStr.at(j); |
---|
151 | j++; |
---|
152 | } |
---|
153 | setUserNumber(userNumber); |
---|
154 | j++; |
---|
155 | } else if (format.at(i) == numberStr.at(j)) { |
---|
156 | j++; |
---|
157 | } else { |
---|
158 | if (format.mid(i, 2) == "CC") { |
---|
159 | qWarning() << "CC not supported here!"; |
---|
160 | } else if (format.mid(i, 3) == "AAA") { |
---|
161 | qWarning() << "AAA not supported here!"; |
---|
162 | } |
---|
163 | clear(); |
---|
164 | break; |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | return isValid(); |
---|
169 | } |
---|
170 | |
---|
171 | bool SNumber::isStringPositiveNumber(const QString& str) { |
---|
172 | if (str == "") |
---|
173 | return false; |
---|
174 | |
---|
175 | for (int i = 0; i < str.count(); i++) { |
---|
176 | if (!str.at(i).isNumber()) |
---|
177 | return false; |
---|
178 | } |
---|
179 | |
---|
180 | return true; |
---|
181 | } |
---|
182 | |
---|
183 | void SNumber::setCountryCode(short countryCode){ |
---|
184 | if ((countryCode <= 0) || (countryCode >= 100)) |
---|
185 | return; |
---|
186 | |
---|
187 | countryCode_ = countryCode; |
---|
188 | } |
---|
189 | void SNumber::setAreaCode(short areaCode){ |
---|
190 | if ((areaCode <= 0) || (areaCode >= 1000)) |
---|
191 | return; |
---|
192 | |
---|
193 | areaCode_ = areaCode; |
---|
194 | } |
---|
195 | void SNumber::setUserNumber(const QString& userNumberStr){ |
---|
196 | if (userNumberStr.toInt() <= 0) |
---|
197 | return; |
---|
198 | |
---|
199 | userNumber_ = userNumberStr; |
---|
200 | } |
---|
201 | |
---|
202 | QString SNumber::toString(const QString& format) const { |
---|
203 | if (!isValid()) { |
---|
204 | return ""; |
---|
205 | } |
---|
206 | |
---|
207 | const QLatin1Char quote('\''); |
---|
208 | if (format.isEmpty()) |
---|
209 | return QString(); |
---|
210 | |
---|
211 | QString res = ""; |
---|
212 | |
---|
213 | bool inQuote = false; |
---|
214 | for (int i = 0; i < format.length(); i++) { |
---|
215 | if (format.at(i) == quote) { |
---|
216 | inQuote = !inQuote; |
---|
217 | continue; |
---|
218 | } |
---|
219 | |
---|
220 | if (inQuote) { |
---|
221 | res += format.at(i); |
---|
222 | continue; |
---|
223 | } |
---|
224 | |
---|
225 | if (format.mid(i, 2) == "CC") { |
---|
226 | res += QString::number(countryCode()); |
---|
227 | i++; |
---|
228 | } else if (format.mid(i, 2) == "cc") { |
---|
229 | res += QString::number(countryCode()).rightJustified(2, '0'); |
---|
230 | i++; |
---|
231 | } else if (format.mid(i, 1) == "c") { |
---|
232 | res += QString::number(countryCode()).rightJustified(1, '0'); |
---|
233 | } else if (format.mid(i, 3) == "AAA") { |
---|
234 | res += QString::number(areaCode()); |
---|
235 | i += 2; |
---|
236 | } else if (format.mid(i, 3) == "aaa") { |
---|
237 | res += QString::number(areaCode()).rightJustified(3, '0'); |
---|
238 | i += 2; |
---|
239 | } else if (format.mid(i, 2) == "aa") { |
---|
240 | res += QString::number(areaCode()).rightJustified(2, '0'); |
---|
241 | i += 1; |
---|
242 | } else if (format.mid(i, 1) == "a") { |
---|
243 | res += QString::number(areaCode()).rightJustified(1, '0'); |
---|
244 | } else if (format.at(i) == 'u') { |
---|
245 | res += userNumber(); |
---|
246 | } else { |
---|
247 | res += format.at(i); |
---|
248 | } |
---|
249 | } |
---|
250 | return res; |
---|
251 | } |
---|
252 | |
---|
253 | bool SNumber::readFromString(const QString& string, const QString& format) { |
---|
254 | return setNumber(string, format); |
---|
255 | } |
---|
256 | SNumber SNumber::fromString(const QString& string, const QString& format) { |
---|
257 | return SNumber(string, format); |
---|
258 | } |
---|
259 | |
---|
260 | bool SNumber::operator==(const SNumber& n) const { |
---|
261 | return this->toString() == n.toString(); |
---|
262 | } |
---|