author | benmeyer <benmeyer> | 2002-11-08 17:16:58 (UTC) |
---|---|---|
committer | benmeyer <benmeyer> | 2002-11-08 17:16:58 (UTC) |
commit | 56a6d067a51d741eb9be1912b69325bdd424dd84 (patch) (unidiff) | |
tree | e8db80af083fc44d31042e342726ecfcbbcce4d5 /noncore/net/networksetup/interfaces/interfaces.cpp | |
parent | e0db2259cc26cab12c6f1131b82dd867c454a3ff (diff) | |
download | opie-56a6d067a51d741eb9be1912b69325bdd424dd84.zip opie-56a6d067a51d741eb9be1912b69325bdd424dd84.tar.gz opie-56a6d067a51d741eb9be1912b69325bdd424dd84.tar.bz2 |
More optimizations, 1 memory leak fixed
Diffstat (limited to 'noncore/net/networksetup/interfaces/interfaces.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/net/networksetup/interfaces/interfaces.cpp | 77 |
1 files changed, 40 insertions, 37 deletions
diff --git a/noncore/net/networksetup/interfaces/interfaces.cpp b/noncore/net/networksetup/interfaces/interfaces.cpp index 708f399..e49998e 100644 --- a/noncore/net/networksetup/interfaces/interfaces.cpp +++ b/noncore/net/networksetup/interfaces/interfaces.cpp | |||
@@ -1,30 +1,31 @@ | |||
1 | #include "interfaces.h" | 1 | #include "interfaces.h" |
2 | 2 | ||
3 | #include <qfile.h> | 3 | #include <qfile.h> |
4 | #include <qtextstream.h> | 4 | #include <qtextstream.h> |
5 | #include <qregexp.h> | 5 | #include <qregexp.h> |
6 | 6 | ||
7 | // The three stanza's | ||
7 | #define AUTO "auto" | 8 | #define AUTO "auto" |
8 | #define IFACE "iface" | 9 | #define IFACE "iface" |
9 | #define MAPPING "mapping" | 10 | #define MAPPING "mapping" |
10 | 11 | ||
11 | /** | 12 | /** |
12 | * Constructor. Reads in the interfaces file and then split the file up by | 13 | * Constructor. Reads in the interfaces file and then split the file up by |
13 | * the \n for interfaces variable. | 14 | * the \n for interfaces variable. |
14 | * @param useInterfacesFile if an interface file other then the default is | 15 | * @param useInterfacesFile if an interface file other then the default is |
15 | * desired to be used it should be passed in. | 16 | * desired to be used it should be passed in. |
16 | */ | 17 | */ |
17 | Interfaces::Interfaces(QString useInterfacesFile){ | 18 | Interfaces::Interfaces(QString useInterfacesFile){ |
18 | acceptedFamily.append(INTERFACES_FAMILY_INET); | 19 | acceptedFamily.append(INTERFACES_FAMILY_INET); |
19 | acceptedFamily.append(INTERFACES_FAMILY_IPX); | 20 | acceptedFamily.append(INTERFACES_FAMILY_IPX); |
20 | acceptedFamily.append(INTERFACES_FAMILY_INET6); | 21 | acceptedFamily.append(INTERFACES_FAMILY_INET6); |
21 | 22 | ||
22 | interfacesFile = useInterfacesFile; | 23 | interfacesFile = useInterfacesFile; |
23 | QFile file(interfacesFile); | 24 | QFile file(interfacesFile); |
24 | if (!file.open(IO_ReadOnly)){ | 25 | if (!file.open(IO_ReadOnly)){ |
25 | qDebug(QString("Interfaces: Can't open file: %1 for reading.").arg(interfacesFile).latin1()); | 26 | qDebug(QString("Interfaces: Can't open file: %1 for reading.").arg(interfacesFile).latin1()); |
26 | currentIface = interfaces.end(); | 27 | currentIface = interfaces.end(); |
27 | currentMapping = interfaces.end(); | 28 | currentMapping = interfaces.end(); |
28 | return; | 29 | return; |
29 | } | 30 | } |
30 | QTextStream stream( &file ); | 31 | QTextStream stream( &file ); |
@@ -81,212 +82,204 @@ bool Interfaces::isAuto(const QString &interface){ | |||
81 | } | 82 | } |
82 | 83 | ||
83 | /** | 84 | /** |
84 | * Attempt to set the auto option for interface to setAuto. | 85 | * Attempt to set the auto option for interface to setAuto. |
85 | * @param interface the interface to set | 86 | * @param interface the interface to set |
86 | * @param setAuto the value to set interface to. | 87 | * @param setAuto the value to set interface to. |
87 | * @return false if already set to setAuto. | 88 | * @return false if already set to setAuto. |
88 | * */ | 89 | * */ |
89 | bool Interfaces::setAuto(const QString &interface, bool setAuto){ | 90 | bool Interfaces::setAuto(const QString &interface, bool setAuto){ |
90 | // Don't need to set it if it is already set. | 91 | // Don't need to set it if it is already set. |
91 | if(isAuto(interface) == setAuto) | 92 | if(isAuto(interface) == setAuto) |
92 | return false; | 93 | return false; |
93 | 94 | ||
94 | bool changed = false; | 95 | bool changed = false; |
95 | for ( QStringList::Iterator it = interfaces.begin(); it != interfaces.end(); ++it ) { | 96 | for ( QStringList::Iterator it = interfaces.begin(); it != interfaces.end(); ++it ) { |
96 | if((*it).contains(AUTO)){ | 97 | if((*it).contains(AUTO)){ |
97 | //We know that they are not in any group so let add to this auto. | 98 | //We know that they are not in any group so let add to this auto. |
98 | if(setAuto){ | 99 | if(setAuto){ |
99 | (*it) = (*it) += " " + interface; | 100 | (*it) = (*it) += " " + interface; |
100 | // Don't care to have such thins as: auto eth0 lo usb0 | 101 | // Don't care to have such thins as: auto eth0 lo usb0 |
101 | (*it) = (*it).simplifyWhiteSpace(); | 102 | (*it) = (*it).simplifyWhiteSpace(); |
102 | changed = true; | 103 | changed = true; |
103 | break; | 104 | break; |
104 | } | 105 | } |
106 | // else see if we need to remove from this one | ||
105 | else{ | 107 | else{ |
106 | if((*it).contains(interface)){ | 108 | if((*it).contains(interface)){ |
107 | (*it) = (*it).replace(QRegExp(interface), ""); | 109 | (*it) = (*it).replace(QRegExp(interface), ""); |
108 | // clean up | 110 | // if AUTO is the only thing left clear the line |
109 | QString line = (*it).simplifyWhiteSpace(); | 111 | if(((*it).simplifyWhiteSpace()).replace(QRegExp(" "),"") == AUTO) |
110 | line = line.replace(QRegExp(" "),""); | ||
111 | if(line == AUTO) | ||
112 | (*it) = ""; | 112 | (*it) = ""; |
113 | changed = true; | 113 | changed = true; |
114 | // Don't break because we want to make sure we remove all cases. | 114 | // Don't break because we want to make sure we remove all cases. |
115 | } | 115 | } |
116 | } | 116 | } |
117 | } | 117 | } |
118 | } | 118 | } |
119 | if(changed == false){ | 119 | // In the case where there is no AUTO field add one. |
120 | if(setAuto == true) | 120 | if(!changed && setAuto) |
121 | interfaces.append(QString(AUTO" %1").arg(interface)); | 121 | interfaces.append(QString(AUTO" %1").arg(interface)); |
122 | else{ | ||
123 | qDebug(QString("Interfaces: Can't set interface %1 auto to false sense it is already false.").arg(interface).latin1()); | ||
124 | } | ||
125 | } | ||
126 | return true; | 122 | return true; |
127 | } | 123 | } |
128 | 124 | ||
129 | /** | 125 | /** |
130 | * Set the current interface to interface. This needs to be done before you | 126 | * Set the current interface to interface. This needs to be done before you |
131 | * can call getFamily(), getMethod, and get/setOption(). | 127 | * can call getFamily(), getMethod, and get/setOption(). |
132 | * @param interface the name of the interface to set. All whitespace is | 128 | * @param interface the name of the interface to set. All whitespace is |
133 | * removed from the interface name. | 129 | * removed from the interface name. |
134 | * @return bool true if it is successfull. | 130 | * @return bool true if it is successfull. |
135 | */ | 131 | */ |
136 | bool Interfaces::setInterface(QString interface){ | 132 | bool Interfaces::setInterface(QString interface){ |
137 | interface = interface.simplifyWhiteSpace(); | 133 | interface = interface.simplifyWhiteSpace(); |
138 | interface = interface.replace(QRegExp(" "), ""); | 134 | interface = interface.replace(QRegExp(" "), ""); |
139 | return setStanza(IFACE, interface, currentIface); | 135 | return setStanza(IFACE, interface, currentIface); |
140 | } | 136 | } |
141 | 137 | ||
142 | /** | 138 | /** |
143 | * A quick helper funtion to see if the current interface is set. | 139 | * A quick helper funtion to see if the current interface is set. |
144 | * @return bool true if set, false otherwise. | 140 | * @return bool true if set, false otherwise. |
145 | */ | 141 | */ |
146 | bool Interfaces::isInterfaceSet(){ | 142 | bool Interfaces::isInterfaceSet(){ |
147 | return (currentIface != interfaces.end()); | 143 | return (currentIface != interfaces.end()); |
148 | } | 144 | } |
149 | 145 | ||
150 | /** | 146 | /** |
151 | * Add a new interface of with the settings - family and method | 147 | * Add a new interface of with the settings - family and method |
152 | * @param interface the name of the interface to set. All whitespace is | 148 | * @param interface the name of the interface to set. All whitespace is |
153 | * removed from the interface name. | 149 | * removed from the interface name. |
154 | * @param family the family of this interface inet or inet, ipx or inet6 | 150 | * @param family the family of this interface inet or inet, ipx or inet6 |
155 | * Must of one of the families defined in interfaces.h | 151 | * Must of one of the families defined in interfaces.h |
156 | * @param method for the family. see interfaces man page for family methods. | 152 | * @param method for the family. see interfaces man page for family methods. |
157 | * @return true if successfull. | 153 | * @return true if successfull. |
158 | */ | 154 | */ |
159 | bool Interfaces::addInterface(const QString &interface, const QString &family, const QString &method){ | 155 | bool Interfaces::addInterface(const QString &interface, const QString &family, const QString &method){ |
160 | if(acceptedFamily.contains(family)==0) | 156 | if(0 == acceptedFamily.contains(family)) |
161 | return false; | 157 | return false; |
162 | QString newInterface = interface.simplifyWhiteSpace(); | 158 | QString newInterface = interface.simplifyWhiteSpace(); |
163 | newInterface = newInterface.replace(QRegExp(" "), ""); | 159 | newInterface = newInterface.replace(QRegExp(" "), ""); |
164 | interfaces.append(""); | 160 | interfaces.append(""); |
165 | interfaces.append(QString(IFACE " %1 %2 %3").arg(newInterface).arg(family).arg(method)); | 161 | interfaces.append(QString(IFACE " %1 %2 %3").arg(newInterface).arg(family).arg(method)); |
166 | return true; | 162 | return true; |
167 | } | 163 | } |
168 | 164 | ||
169 | /** | 165 | /** |
170 | * Copies interface with name interface to name newInterface | 166 | * Copies interface with name interface to name newInterface |
171 | * @param newInterface name of the new interface. | 167 | * @param newInterface name of the new interface. |
172 | * @return bool true if successfull | 168 | * @return bool true if successfull |
173 | */ | 169 | */ |
174 | bool Interfaces::copyInterface(const QString &interface, const QString &newInterface){ | 170 | bool Interfaces::copyInterface(const QString &interface, const QString &newInterface){ |
175 | if(!setInterface(interface)) return false; | 171 | if(!setInterface(interface)) |
176 | 172 | return false; | |
173 | |||
174 | // Store the old interface and bump past the stanza line. | ||
177 | QStringList::Iterator it = currentIface; | 175 | QStringList::Iterator it = currentIface; |
178 | it++; | 176 | it++; |
179 | 177 | ||
178 | // Add the new interface | ||
180 | bool error; | 179 | bool error; |
181 | addInterface(newInterface, getInterfaceFamily(error), getInterfaceMethod(error)); | 180 | addInterface(newInterface, getInterfaceFamily(error), getInterfaceMethod(error)); |
182 | if(!setInterface(newInterface)) return false; | 181 | if(!setInterface(newInterface)) |
182 | return false; | ||
183 | |||
183 | QStringList::Iterator newIface = currentIface; | 184 | QStringList::Iterator newIface = currentIface; |
184 | newIface++; | 185 | newIface++; |
185 | 186 | ||
187 | // Copy all of the lines | ||
186 | for ( ; it != interfaces.end(); ++it ){ | 188 | for ( ; it != interfaces.end(); ++it ){ |
187 | if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO))) | 189 | if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO))) |
188 | break; | 190 | break; |
189 | newIface = interfaces.insert(newIface, *it); | 191 | newIface = interfaces.insert(newIface, *it); |
190 | } | 192 | } |
191 | 193 | ||
192 | return true; | 194 | return true; |
193 | } | 195 | } |
194 | 196 | ||
195 | /** | 197 | /** |
196 | * Remove the currently selected interface and all of its options. | 198 | * Remove the currently selected interface and all of its options. |
197 | * @return bool if successfull or not. | 199 | * @return bool if successfull or not. |
198 | */ | 200 | */ |
199 | bool Interfaces::removeInterface(){ | 201 | bool Interfaces::removeInterface(){ |
200 | if(currentIface == interfaces.end()) | 202 | return removeStanza(currentIface); |
201 | return false; | ||
202 | (*currentIface) = ""; | ||
203 | return removeAllInterfaceOptions(); | ||
204 | } | 203 | } |
205 | 204 | ||
206 | /** | 205 | /** |
207 | * Gets the hardware name of the interface that is currently selected. | 206 | * Gets the hardware name of the interface that is currently selected. |
208 | * @return QString name of the hardware interface (eth0, usb2, wlan1...). | 207 | * @return QString name of the hardware interface (eth0, usb2, wlan1...). |
209 | * @param error set to true if any error occurs, false otherwise. | 208 | * @param error set to true if any error occurs, false otherwise. |
210 | */ | 209 | */ |
211 | QString Interfaces::getInterfaceName(bool &error){ | 210 | QString Interfaces::getInterfaceName(bool &error){ |
212 | if(currentIface == interfaces.end()){ | 211 | if(currentIface == interfaces.end()){ |
213 | error = true; | 212 | error = true; |
214 | return QString(); | 213 | return QString(); |
215 | } | 214 | } |
216 | QString line = (*currentIface); | 215 | QString line = (*currentIface); |
217 | line = line.mid(QString(IFACE).length() +1, line.length()); | 216 | line = line.mid(QString(IFACE).length() +1, line.length()); |
218 | line = line.simplifyWhiteSpace(); | 217 | line = line.simplifyWhiteSpace(); |
219 | int findSpace = line.find(" "); | 218 | int findSpace = line.find(" "); |
220 | if( findSpace < 0){ | 219 | if( findSpace < 0){ |
221 | error = true; | 220 | error = true; |
222 | return QString(); | 221 | return QString(); |
223 | } | 222 | } |
224 | error = false; | 223 | error = false; |
225 | return line.mid(0, findSpace); | 224 | return line.mid(0, findSpace); |
226 | } | 225 | } |
227 | 226 | ||
228 | /** | 227 | /** |
229 | * Gets the family name of the interface that is currently selected. | 228 | * Gets the family name of the interface that is currently selected. |
230 | * @return QString name of the family (inet, inet6, ipx). | 229 | * @return QString name of the family (inet, inet6, ipx). |
231 | * @param error set to true if any error occurs, false otherwise. | 230 | * @param error set to true if any error occurs, false otherwise. |
232 | */ | 231 | */ |
233 | QString Interfaces::getInterfaceFamily(bool &error){ | 232 | QString Interfaces::getInterfaceFamily(bool &error){ |
234 | QString name = getInterfaceName(error); | 233 | QString name = getInterfaceName(error); |
235 | if(error){ | 234 | if(error) |
236 | error = true; | ||
237 | return QString(); | 235 | return QString(); |
238 | } | ||
239 | QString line = (*currentIface); | 236 | QString line = (*currentIface); |
240 | line = line.mid(QString(IFACE).length() +1, line.length()); | 237 | line = line.mid(QString(IFACE).length() +1, line.length()); |
241 | line = line.mid(name.length()+1, line.length()); | 238 | line = line.mid(name.length()+1, line.length()); |
242 | line = line.simplifyWhiteSpace(); | 239 | line = line.simplifyWhiteSpace(); |
243 | int findSpace = line.find(" "); | 240 | int findSpace = line.find(" "); |
244 | if( findSpace < 0){ | 241 | if( findSpace < 0){ |
245 | error = true; | 242 | error = true; |
246 | return QString(); | 243 | return QString(); |
247 | } | 244 | } |
248 | error = false; | 245 | error = false; |
249 | return line.mid(0, findSpace); | 246 | return line.mid(0, findSpace); |
250 | } | 247 | } |
251 | 248 | ||
252 | /** | 249 | /** |
253 | * Gets the method of the interface that is currently selected. | 250 | * Gets the method of the interface that is currently selected. |
254 | * @return QString name of the method such as staic or dhcp. | 251 | * @return QString name of the method such as staic or dhcp. |
255 | * See the man page of interfaces for possible methods depending on the family. | 252 | * See the man page of interfaces for possible methods depending on the family. |
256 | * @param error set to true if any error occurs, false otherwise. | 253 | * @param error set to true if any error occurs, false otherwise. |
257 | */ | 254 | */ |
258 | QString Interfaces::getInterfaceMethod(bool &error){ | 255 | QString Interfaces::getInterfaceMethod(bool &error){ |
259 | QString name = getInterfaceName(error); | 256 | QString name = getInterfaceName(error); |
260 | if(error){ | 257 | if(error) |
261 | error = true; | ||
262 | return QString(); | 258 | return QString(); |
263 | } | ||
264 | QString family = getInterfaceFamily(error); | 259 | QString family = getInterfaceFamily(error); |
265 | if(error){ | 260 | if(error) |
266 | error = true; | ||
267 | return QString(); | 261 | return QString(); |
268 | } | ||
269 | QString line = (*currentIface); | 262 | QString line = (*currentIface); |
270 | line = line.mid(QString(IFACE).length()+1, line.length()); | 263 | line = line.mid(QString(IFACE).length()+1, line.length()); |
271 | line = line.mid(name.length()+1, line.length()); | 264 | line = line.mid(name.length()+1, line.length()); |
272 | line = line.mid(family.length()+1, line.length()); | 265 | line = line.mid(family.length()+1, line.length()); |
273 | line = line.simplifyWhiteSpace(); | 266 | line = line.simplifyWhiteSpace(); |
274 | error = false; | 267 | error = false; |
275 | return line; | 268 | return line; |
276 | } | 269 | } |
277 | 270 | ||
278 | /** | 271 | /** |
279 | * Sets the interface name to newName. | 272 | * Sets the interface name to newName. |
280 | * @param newName the new name of the interface. All whitespace is removed. | 273 | * @param newName the new name of the interface. All whitespace is removed. |
281 | * @return bool true if successfull. | 274 | * @return bool true if successfull. |
282 | */ | 275 | */ |
283 | bool Interfaces::setInterfaceName(const QString &newName){ | 276 | bool Interfaces::setInterfaceName(const QString &newName){ |
284 | if(currentIface == interfaces.end()) | 277 | if(currentIface == interfaces.end()) |
285 | return false; | 278 | return false; |
286 | QString name = newName.simplifyWhiteSpace(); | 279 | QString name = newName.simplifyWhiteSpace(); |
287 | name = name.replace(QRegExp(" "), ""); | 280 | name = name.replace(QRegExp(" "), ""); |
288 | bool returnValue = false; | 281 | bool returnValue = false; |
289 | (*currentIface) = QString("iface %1 %2 %3").arg(name).arg(getInterfaceFamily(returnValue)).arg(getInterfaceMethod(returnValue)); | 282 | (*currentIface) = QString("iface %1 %2 %3").arg(name).arg(getInterfaceFamily(returnValue)).arg(getInterfaceMethod(returnValue)); |
290 | return !returnValue; | 283 | return !returnValue; |
291 | } | 284 | } |
292 | 285 | ||
@@ -370,52 +363,49 @@ bool Interfaces::removeAllInterfaceOptions(){ | |||
370 | * @param interface the name of the interface to set. All whitespace is | 363 | * @param interface the name of the interface to set. All whitespace is |
371 | * removed from the interface name. | 364 | * removed from the interface name. |
372 | * @return bool true if it is successfull. | 365 | * @return bool true if it is successfull. |
373 | */ | 366 | */ |
374 | bool Interfaces::setMapping(const QString &interface){ | 367 | bool Interfaces::setMapping(const QString &interface){ |
375 | QString interfaceName = interface.simplifyWhiteSpace(); | 368 | QString interfaceName = interface.simplifyWhiteSpace(); |
376 | interfaceName = interfaceName.replace(QRegExp(" "), ""); | 369 | interfaceName = interfaceName.replace(QRegExp(" "), ""); |
377 | return setStanza(MAPPING, interfaceName, currentMapping); | 370 | return setStanza(MAPPING, interfaceName, currentMapping); |
378 | } | 371 | } |
379 | 372 | ||
380 | /** | 373 | /** |
381 | * Adds a new Mapping to the interfaces file with interfaces. | 374 | * Adds a new Mapping to the interfaces file with interfaces. |
382 | * @param interface the name(s) of the interfaces to set to this mapping | 375 | * @param interface the name(s) of the interfaces to set to this mapping |
383 | */ | 376 | */ |
384 | void Interfaces::addMapping(const QString &option){ | 377 | void Interfaces::addMapping(const QString &option){ |
385 | interfaces.append(""); | 378 | interfaces.append(""); |
386 | interfaces.append(QString(MAPPING " %1").arg(option)); | 379 | interfaces.append(QString(MAPPING " %1").arg(option)); |
387 | } | 380 | } |
388 | 381 | ||
389 | /** | 382 | /** |
390 | * Remove the currently selected map and all of its options. | 383 | * Remove the currently selected map and all of its options. |
391 | * @return bool if successfull or not. | 384 | * @return bool if successfull or not. |
392 | */ | 385 | */ |
393 | bool Interfaces::removeMapping(){ | 386 | bool Interfaces::removeMapping(){ |
394 | if(currentMapping == interfaces.end()) | 387 | return removeStanza(currentMapping); |
395 | return false; | ||
396 | (*currentMapping) = ""; | ||
397 | return removeAllOptions(currentMapping); | ||
398 | } | 388 | } |
399 | 389 | ||
400 | /** | 390 | /** |
401 | * Set a map option within a mapping. | 391 | * Set a map option within a mapping. |
402 | * @param map map to use | 392 | * @param map map to use |
403 | * @param value value to go with map | 393 | * @param value value to go with map |
404 | * @return bool true if it is successfull. | 394 | * @return bool true if it is successfull. |
405 | */ | 395 | */ |
406 | bool Interfaces::setMap(const QString &map, const QString &value){ | 396 | bool Interfaces::setMap(const QString &map, const QString &value){ |
407 | return setOption(currentMapping, map, value); | 397 | return setOption(currentMapping, map, value); |
408 | } | 398 | } |
409 | 399 | ||
410 | /** | 400 | /** |
411 | * Removes a map option within a mapping. | 401 | * Removes a map option within a mapping. |
412 | * @param map map to use | 402 | * @param map map to use |
413 | * @param value value to go with map | 403 | * @param value value to go with map |
414 | * @return bool true if it is successfull. | 404 | * @return bool true if it is successfull. |
415 | */ | 405 | */ |
416 | bool Interfaces::removeMap(const QString &map, const QString &value){ | 406 | bool Interfaces::removeMap(const QString &map, const QString &value){ |
417 | return removeOption(currentMapping, map, value); | 407 | return removeOption(currentMapping, map, value); |
418 | } | 408 | } |
419 | 409 | ||
420 | /** | 410 | /** |
421 | * Get a map value within a mapping. | 411 | * Get a map value within a mapping. |
@@ -499,48 +489,61 @@ bool Interfaces::setOption(const QStringList::Iterator &start, const QString &op | |||
499 | for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) { | 489 | for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) { |
500 | if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){ | 490 | if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){ |
501 | if(!found && value != ""){ | 491 | if(!found && value != ""){ |
502 | // Got to the end of the stanza without finding it, so append it. | 492 | // Got to the end of the stanza without finding it, so append it. |
503 | interfaces.insert(--it, QString("\t%1 %2").arg(option).arg(value)); | 493 | interfaces.insert(--it, QString("\t%1 %2").arg(option).arg(value)); |
504 | } | 494 | } |
505 | found = true; | 495 | found = true; |
506 | break; | 496 | break; |
507 | } | 497 | } |
508 | if((*it).contains(option) && it != start && (*it).at(0) != '#'){ | 498 | if((*it).contains(option) && it != start && (*it).at(0) != '#'){ |
509 | // Found it in stanza so replace it. | 499 | // Found it in stanza so replace it. |
510 | if(found) | 500 | if(found) |
511 | qDebug(QString("Interfaces: Set Options found more then one value for option: %1 in stanza: %1").arg(option).arg((*start)).latin1()); | 501 | qDebug(QString("Interfaces: Set Options found more then one value for option: %1 in stanza: %1").arg(option).arg((*start)).latin1()); |
512 | found = true; | 502 | found = true; |
513 | (*it) = QString("\t%1 %2").arg(option).arg(value); | 503 | (*it) = QString("\t%1 %2").arg(option).arg(value); |
514 | } | 504 | } |
515 | } | 505 | } |
516 | if(!found){ | 506 | if(!found){ |
517 | QStringList::Iterator p = start; | 507 | QStringList::Iterator p = start; |
518 | interfaces.insert(++p, QString("\t%1 %2").arg(option).arg(value)); | 508 | interfaces.insert(++p, QString("\t%1 %2").arg(option).arg(value)); |
519 | found = true; | 509 | found = true; |
520 | } | 510 | } |
521 | return found; | 511 | return found; |
522 | } | 512 | } |
513 | |||
514 | /** | ||
515 | * Removes a stanza and all of its options | ||
516 | * @param stanza the stanza to remove | ||
517 | * @return bool true if successfull. | ||
518 | */ | ||
519 | bool Interfaces::removeStanza(QStringList::Iterator &stanza){ | ||
520 | if(stanza == interfaces.end()) | ||
521 | return false; | ||
522 | (*stanza) = ""; | ||
523 | return removeAllOptions(stanza); | ||
524 | } | ||
525 | |||
523 | /** | 526 | /** |
524 | * Removes a option in a stanza | 527 | * Removes a option in a stanza |
525 | * @param start the start of the stanza | 528 | * @param start the start of the stanza |
526 | * @param option the option to use when setting value. | 529 | * @param option the option to use when setting value. |
527 | * @return bool true if successfull, false otherwise. | 530 | * @return bool true if successfull, false otherwise. |
528 | */ | 531 | */ |
529 | bool Interfaces::removeOption(const QStringList::Iterator &start, const QString &option, const QString &value){ | 532 | bool Interfaces::removeOption(const QStringList::Iterator &start, const QString &option, const QString &value){ |
530 | if(start == interfaces.end()) | 533 | if(start == interfaces.end()) |
531 | return false; | 534 | return false; |
532 | 535 | ||
533 | bool found = false; | 536 | bool found = false; |
534 | for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) { | 537 | for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) { |
535 | if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){ | 538 | if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){ |
536 | // got to the end without finding it | 539 | // got to the end without finding it |
537 | break; | 540 | break; |
538 | } | 541 | } |
539 | if((*it).contains(option) && (*it).contains(value) && it != start && (*it).at(0) != '#'){ | 542 | if((*it).contains(option) && (*it).contains(value) && it != start && (*it).at(0) != '#'){ |
540 | // Found it in stanza so replace it. | 543 | // Found it in stanza so replace it. |
541 | if(found) | 544 | if(found) |
542 | qDebug(QString("Interfaces: Set Options found more then one value for option: %1 in stanza: %1").arg(option).arg((*start)).latin1()); | 545 | qDebug(QString("Interfaces: Set Options found more then one value for option: %1 in stanza: %1").arg(option).arg((*start)).latin1()); |
543 | found = true; | 546 | found = true; |
544 | (*it) = ""; | 547 | (*it) = ""; |
545 | } | 548 | } |
546 | } | 549 | } |
@@ -570,56 +573,56 @@ bool Interfaces::removeAllOptions(const QStringList::Iterator &start){ | |||
570 | return true; | 573 | return true; |
571 | } | 574 | } |
572 | 575 | ||
573 | /** | 576 | /** |
574 | * Gets a value of an option in a stanza | 577 | * Gets a value of an option in a stanza |
575 | * @param start the start of the stanza | 578 | * @param start the start of the stanza |
576 | * @param option the option to use when getting the value. | 579 | * @param option the option to use when getting the value. |
577 | * @param bool true if errors false otherwise. | 580 | * @param bool true if errors false otherwise. |
578 | * @return QString the value of option QString::null() if error == true. | 581 | * @return QString the value of option QString::null() if error == true. |
579 | */ | 582 | */ |
580 | QString Interfaces::getOption(const QStringList::Iterator &start, const QString &option, bool &error){ | 583 | QString Interfaces::getOption(const QStringList::Iterator &start, const QString &option, bool &error){ |
581 | if(start == interfaces.end()){ | 584 | if(start == interfaces.end()){ |
582 | error = false; | 585 | error = false; |
583 | return QString(); | 586 | return QString(); |
584 | } | 587 | } |
585 | 588 | ||
586 | QString value; | 589 | QString value; |
587 | bool found = false; | 590 | bool found = false; |
588 | for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) { | 591 | for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) { |
589 | if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){ | 592 | if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){ |
590 | break; | 593 | break; |
591 | } | 594 | } |
592 | if((*it).contains(option) && (*it).at(0) != '#'){ | 595 | if((*it).contains(option) && (*it).at(0) != '#'){ |
593 | if(found) | 596 | if(found) |
594 | qDebug(QString("Interfaces: Get Options found more then one value: %1 for option: %2 in stanza %3").arg((*it)).arg(option).arg((*start)).latin1()); | 597 | qDebug(QString("Interfaces: getOption found more then one value: %1 for option: %2 in stanza %3").arg((*it)).arg(option).arg((*start)).latin1()); |
595 | found = true; | 598 | found = true; |
596 | QString line = (*it).simplifyWhiteSpace(); | 599 | QString line = (*it).simplifyWhiteSpace(); |
597 | int space = line.find(" ", option.length()); | 600 | int space = line.find(" ", option.length()); |
598 | if(space != -1) | 601 | if(space != -1){ |
599 | value = line.mid(space+1, line.length()); | 602 | value = line.mid(space+1, line.length()); |
600 | else | 603 | break; |
601 | qDebug(QString("Interfaces: Option %1 with no value").arg(option).latin1()); | 604 | } |
602 | } | 605 | } |
603 | } | 606 | } |
604 | error = !found; | 607 | error = !found; |
605 | return value; | 608 | return value; |
606 | } | 609 | } |
607 | 610 | ||
608 | /** | 611 | /** |
609 | * Write out the interfaces file to the file passed into the constructor. | 612 | * Write out the interfaces file to the file passed into the constructor. |
610 | * Removes any excess blank lines over 1 line long. | 613 | * Removes any excess blank lines over 1 line long. |
611 | * @return bool true if successfull, false if not. | 614 | * @return bool true if successfull, false if not. |
612 | */ | 615 | */ |
613 | bool Interfaces::write(){ | 616 | bool Interfaces::write(){ |
614 | QFile::remove(interfacesFile); | 617 | QFile::remove(interfacesFile); |
615 | QFile file(interfacesFile); | 618 | QFile file(interfacesFile); |
616 | 619 | ||
617 | if (!file.open(IO_ReadWrite)){ | 620 | if (!file.open(IO_ReadWrite)){ |
618 | qDebug(QString("Interfaces: Can't open file: %1 for writing.").arg(interfacesFile).latin1()); | 621 | qDebug(QString("Interfaces: Can't open file: %1 for writing.").arg(interfacesFile).latin1()); |
619 | return false; | 622 | return false; |
620 | } | 623 | } |
621 | QTextStream stream( &file ); | 624 | QTextStream stream( &file ); |
622 | int whiteSpaceCount = 0; | 625 | int whiteSpaceCount = 0; |
623 | for ( QStringList::Iterator it = interfaces.begin(); it != interfaces.end(); ++it ) { | 626 | for ( QStringList::Iterator it = interfaces.begin(); it != interfaces.end(); ++it ) { |
624 | QString line = (*it).simplifyWhiteSpace(); | 627 | QString line = (*it).simplifyWhiteSpace(); |
625 | line = line.replace(QRegExp(" "),""); | 628 | line = line.replace(QRegExp(" "),""); |