author | kergoth <kergoth> | 2003-08-09 17:14:54 (UTC) |
---|---|---|
committer | kergoth <kergoth> | 2003-08-09 17:14:54 (UTC) |
commit | a7e015198a8c5ad3b6e144a9032b059086253e00 (patch) (unidiff) | |
tree | b712b6f11310d88744fe393a92b3160b741a7efe /noncore/settings/networksettings/interfaces/interfaces.cpp | |
parent | beba0e73306815337bf04dee39502233595e9739 (diff) | |
download | opie-a7e015198a8c5ad3b6e144a9032b059086253e00.zip opie-a7e015198a8c5ad3b6e144a9032b059086253e00.tar.gz opie-a7e015198a8c5ad3b6e144a9032b059086253e00.tar.bz2 |
Merge from BRANCH_1_0
Diffstat (limited to 'noncore/settings/networksettings/interfaces/interfaces.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/settings/networksettings/interfaces/interfaces.cpp | 60 |
1 files changed, 47 insertions, 13 deletions
diff --git a/noncore/settings/networksettings/interfaces/interfaces.cpp b/noncore/settings/networksettings/interfaces/interfaces.cpp index 71d0cf5..436e449 100644 --- a/noncore/settings/networksettings/interfaces/interfaces.cpp +++ b/noncore/settings/networksettings/interfaces/interfaces.cpp | |||
@@ -1,50 +1,51 @@ | |||
1 | #include "interfaces.h" | 1 | #include "interfaces.h" |
2 | 2 | ||
3 | #include <qcheckbox.h> | ||
3 | #include <qfile.h> | 4 | #include <qfile.h> |
4 | #include <qtextstream.h> | 5 | #include <qtextstream.h> |
5 | #include <qregexp.h> | 6 | #include <qregexp.h> |
6 | 7 | ||
7 | // The three stanza's | 8 | // The three stanza's |
8 | #define AUTO "auto" | 9 | #define AUTO "auto" |
9 | #define IFACE "iface" | 10 | #define IFACE "iface" |
10 | #define MAPPING "mapping" | 11 | #define MAPPING "mapping" |
11 | 12 | ||
12 | /** | 13 | /** |
13 | * Constructor. Reads in the interfaces file and then split the file up by | 14 | * Constructor. Reads in the interfaces file and then split the file up by |
14 | * the \n for interfaces variable. | 15 | * the \n for interfaces variable. |
15 | * @param useInterfacesFile if an interface file other then the default is | 16 | * @param useInterfacesFile if an interface file other then the default is |
16 | * desired to be used it should be passed in. | 17 | * desired to be used it should be passed in. |
17 | */ | 18 | */ |
18 | Interfaces::Interfaces(QString useInterfacesFile){ | 19 | Interfaces::Interfaces(QString useInterfacesFile){ |
19 | acceptedFamily.append(INTERFACES_FAMILY_INET); | 20 | acceptedFamily.append(INTERFACES_FAMILY_INET); |
20 | acceptedFamily.append(INTERFACES_FAMILY_IPX); | 21 | acceptedFamily.append(INTERFACES_FAMILY_IPX); |
21 | acceptedFamily.append(INTERFACES_FAMILY_INET6); | 22 | acceptedFamily.append(INTERFACES_FAMILY_INET6); |
22 | 23 | ||
23 | interfacesFile = useInterfacesFile; | 24 | interfacesFile = useInterfacesFile; |
24 | QFile file(interfacesFile); | 25 | QFile file(interfacesFile); |
25 | if (!file.open(IO_ReadOnly)){ | 26 | if (!file.open(IO_ReadOnly)){ |
26 | qDebug("Interfaces: Can't open file: %s for reading.", interfacesFile.latin1() ); | 27 | qDebug("Interfaces: Can't open file: %s for reading.", interfacesFile.latin1() ); |
27 | currentIface = interfaces.end(); | 28 | currentIface = interfaces.end(); |
28 | currentMapping = interfaces.end(); | 29 | currentMapping = interfaces.end(); |
29 | return; | 30 | return; |
30 | } | 31 | } |
31 | QTextStream stream( &file ); | 32 | QTextStream stream( &file ); |
32 | QString line; | 33 | QString line; |
33 | while ( !stream.eof() ) { | 34 | while ( !stream.eof() ) { |
34 | line += stream.readLine(); | 35 | line += stream.readLine(); |
35 | line += "\n"; | 36 | line += "\n"; |
36 | } | 37 | } |
37 | file.close(); | 38 | file.close(); |
38 | interfaces = QStringList::split("\n", line, true); | 39 | interfaces = QStringList::split("\n", line, true); |
39 | 40 | ||
40 | currentIface = interfaces.end(); | 41 | currentIface = interfaces.end(); |
41 | currentMapping = interfaces.end(); | 42 | currentMapping = interfaces.end(); |
42 | } | 43 | } |
43 | 44 | ||
44 | 45 | ||
45 | /** | 46 | /** |
46 | * Get a list of all interfaces in the interface file. Usefull for | 47 | * Get a list of all interfaces in the interface file. Usefull for |
47 | * hardware that is not currently connected such as an 802.11b card | 48 | * hardware that is not currently connected such as an 802.11b card |
48 | * not plugged in, but configured for when it is plugged in. | 49 | * not plugged in, but configured for when it is plugged in. |
49 | * @return Return string list of interfaces. | 50 | * @return Return string list of interfaces. |
50 | **/ | 51 | **/ |
@@ -106,111 +107,113 @@ bool Interfaces::setAuto(const QString &interface, bool setAuto){ | |||
106 | if((*it).contains(interface)){ | 107 | if((*it).contains(interface)){ |
107 | (*it) = (*it).replace(QRegExp(interface), ""); | 108 | (*it) = (*it).replace(QRegExp(interface), ""); |
108 | // if AUTO is the only thing left clear the line | 109 | // if AUTO is the only thing left clear the line |
109 | if(((*it).simplifyWhiteSpace()).replace(QRegExp(" "),"") == AUTO) | 110 | if(((*it).simplifyWhiteSpace()).replace(QRegExp(" "),"") == AUTO) |
110 | (*it) = ""; | 111 | (*it) = ""; |
111 | changed = true; | 112 | changed = true; |
112 | // Don't break because we want to make sure we remove all cases. | 113 | // Don't break because we want to make sure we remove all cases. |
113 | } | 114 | } |
114 | } | 115 | } |
115 | } | 116 | } |
116 | } | 117 | } |
117 | // In the case where there is no AUTO field add one. | 118 | // In the case where there is no AUTO field add one. |
118 | if(!changed && setAuto) | 119 | if(!changed && setAuto) |
119 | interfaces.append(QString(AUTO" %1").arg(interface)); | 120 | interfaces.append(QString(AUTO" %1").arg(interface)); |
120 | return true; | 121 | return true; |
121 | } | 122 | } |
122 | 123 | ||
123 | /** | 124 | /** |
124 | * Set the current interface to interface. This needs to be done before you | 125 | * Set the current interface to interface. This needs to be done before you |
125 | * can call getFamily(), getMethod, and get/setOption(). | 126 | * can call getFamily(), getMethod, and get/setOption(). |
126 | * @param interface the name of the interface to set. All whitespace is | 127 | * @param interface the name of the interface to set. All whitespace is |
127 | * removed from the interface name. | 128 | * removed from the interface name. |
128 | * @return bool true if it is successfull. | 129 | * @return bool true if it is successfull. |
129 | */ | 130 | */ |
130 | bool Interfaces::setInterface(QString interface){ | 131 | bool Interfaces::setInterface(QString interface){ |
131 | interface = interface.simplifyWhiteSpace(); | 132 | interface = interface.simplifyWhiteSpace(); |
132 | interface = interface.replace(QRegExp(" "), ""); | 133 | interface = interface.replace(QRegExp(" "), ""); |
133 | return setStanza(IFACE, interface, currentIface); | 134 | return setStanza(IFACE, interface, currentIface); |
134 | } | 135 | } |
135 | 136 | ||
136 | /** | 137 | /** |
137 | * A quick helper funtion to see if the current interface is set. | 138 | * A quick helper funtion to see if the current interface is set. |
138 | * @return bool true if set, false otherwise. | 139 | * @return bool true if set, false otherwise. |
139 | */ | 140 | */ |
140 | bool Interfaces::isInterfaceSet() const { | 141 | bool Interfaces::isInterfaceSet() const { |
141 | return (interfaces.end() != currentIface); | 142 | return (interfaces.end() != currentIface); |
142 | } | 143 | } |
143 | 144 | ||
144 | /** | 145 | /** |
145 | * Add a new interface of with the settings - family and method | 146 | * Add a new interface of with the settings - family and method |
146 | * @param interface the name of the interface to set. All whitespace is | 147 | * @param interface the name of the interface to set. All whitespace is |
147 | * removed from the interface name. | 148 | * removed from the interface name. |
148 | * @param family the family of this interface inet or inet, ipx or inet6 | 149 | * @param family the family of this interface inet or inet, ipx or inet6 |
149 | * Must of one of the families defined in interfaces.h | 150 | * Must of one of the families defined in interfaces.h |
150 | * @param method for the family. see interfaces man page for family methods. | 151 | * @param method for the family. see interfaces man page for family methods. |
151 | * @return true if successfull. | 152 | * @return true if successfull. |
152 | */ | 153 | */ |
153 | bool Interfaces::addInterface(const QString &interface, const QString &family, const QString &method){ | 154 | bool Interfaces::addInterface(const QString &interface, const QString &family, const QString &method){ |
155 | qDebug("Interfaces::addInterface(%s)",interface.latin1()); | ||
154 | if(0 == acceptedFamily.contains(family)) | 156 | if(0 == acceptedFamily.contains(family)) |
155 | return false; | 157 | return false; |
156 | QString newInterface = interface.simplifyWhiteSpace(); | 158 | QString newInterface = interface.simplifyWhiteSpace(); |
157 | newInterface = newInterface.replace(QRegExp(" "), ""); | 159 | newInterface = newInterface.replace(QRegExp(" "), ""); |
158 | interfaces.append(""); | 160 | interfaces.append(""); |
159 | 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)); |
160 | return true; | 162 | return true; |
161 | } | 163 | } |
162 | 164 | ||
163 | /** | 165 | /** |
164 | * Copies interface with name interface to name newInterface | 166 | * Copies interface with name interface to name newInterface |
165 | * @param newInterface name of the new interface. | 167 | * @param newInterface name of the new interface. |
166 | * @return bool true if successfull | 168 | * @return bool true if successfull |
167 | */ | 169 | */ |
168 | bool Interfaces::copyInterface(const QString &interface, const QString &newInterface){ | 170 | bool Interfaces::copyInterface(const QString &interface, const QString &newInterface){ |
171 | qDebug("copy interface %s to %s", interface.latin1(), newInterface.latin1()); | ||
169 | if(!setInterface(interface)) | 172 | if(!setInterface(interface)) |
170 | return false; | 173 | return false; |
171 | 174 | ||
172 | // Store the old interface and bump past the stanza line. | 175 | // Store the old interface and bump past the stanza line. |
173 | QStringList::Iterator it = currentIface; | 176 | QStringList::Iterator it = currentIface; |
174 | it++; | 177 | it++; |
175 | 178 | ||
176 | // Add the new interface | 179 | // Add the new interface |
177 | bool error; | 180 | bool error; |
178 | addInterface(newInterface, getInterfaceFamily(error), getInterfaceMethod(error)); | 181 | addInterface(newInterface, getInterfaceFamily(error), getInterfaceMethod(error)); |
179 | if(!setInterface(newInterface)) | 182 | if(!setInterface(newInterface)) |
180 | return false; | 183 | return false; |
181 | 184 | ||
182 | QStringList::Iterator newIface = currentIface; | 185 | QStringList::Iterator newIface = currentIface; |
183 | newIface++; | 186 | newIface++; |
184 | 187 | ||
185 | // Copy all of the lines | 188 | // Copy all of the lines |
186 | for ( ; it != interfaces.end(); ++it ){ | 189 | for ( ; it != interfaces.end(); ++it ){ |
187 | if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO))) | 190 | if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO))) |
188 | break; | 191 | break; |
189 | newIface = interfaces.insert(newIface, *it); | 192 | newIface = interfaces.insert(newIface, *it); |
190 | } | 193 | } |
191 | 194 | ||
192 | return true; | 195 | return true; |
193 | } | 196 | } |
194 | 197 | ||
195 | /** | 198 | /** |
196 | * Remove the currently selected interface and all of its options. | 199 | * Remove the currently selected interface and all of its options. |
197 | * @return bool if successfull or not. | 200 | * @return bool if successfull or not. |
198 | */ | 201 | */ |
199 | bool Interfaces::removeInterface(){ | 202 | bool Interfaces::removeInterface(){ |
200 | return removeStanza(currentIface); | 203 | return removeStanza(currentIface); |
201 | } | 204 | } |
202 | 205 | ||
203 | /** | 206 | /** |
204 | * Gets the hardware name of the interface that is currently selected. | 207 | * Gets the hardware name of the interface that is currently selected. |
205 | * @return QString name of the hardware interface (eth0, usb2, wlan1...). | 208 | * @return QString name of the hardware interface (eth0, usb2, wlan1...). |
206 | * @param error set to true if any error occurs, false otherwise. | 209 | * @param error set to true if any error occurs, false otherwise. |
207 | */ | 210 | */ |
208 | QString Interfaces::getInterfaceName(bool &error){ | 211 | QString Interfaces::getInterfaceName(bool &error){ |
209 | if(currentIface == interfaces.end()){ | 212 | if(currentIface == interfaces.end()){ |
210 | error = true; | 213 | error = true; |
211 | return QString(); | 214 | return QString(); |
212 | } | 215 | } |
213 | QString line = (*currentIface); | 216 | QString line = (*currentIface); |
214 | line = line.mid(QString(IFACE).length() +1, line.length()); | 217 | line = line.mid(QString(IFACE).length() +1, line.length()); |
215 | line = line.simplifyWhiteSpace(); | 218 | line = line.simplifyWhiteSpace(); |
216 | int findSpace = line.find(" "); | 219 | int findSpace = line.find(" "); |
@@ -227,158 +230,168 @@ QString Interfaces::getInterfaceName(bool &error){ | |||
227 | * @return QString name of the family (inet, inet6, ipx). | 230 | * @return QString name of the family (inet, inet6, ipx). |
228 | * @param error set to true if any error occurs, false otherwise. | 231 | * @param error set to true if any error occurs, false otherwise. |
229 | */ | 232 | */ |
230 | QString Interfaces::getInterfaceFamily(bool &error){ | 233 | QString Interfaces::getInterfaceFamily(bool &error){ |
231 | QString name = getInterfaceName(error); | 234 | QString name = getInterfaceName(error); |
232 | if(error) | 235 | if(error) |
233 | return QString(); | 236 | return QString(); |
234 | QString line = (*currentIface); | 237 | QString line = (*currentIface); |
235 | line = line.mid(QString(IFACE).length() +1, line.length()); | 238 | line = line.mid(QString(IFACE).length() +1, line.length()); |
236 | line = line.mid(name.length()+1, line.length()); | 239 | line = line.mid(name.length()+1, line.length()); |
237 | line = line.simplifyWhiteSpace(); | 240 | line = line.simplifyWhiteSpace(); |
238 | int findSpace = line.find(" "); | 241 | int findSpace = line.find(" "); |
239 | if( findSpace < 0){ | 242 | if( findSpace < 0){ |
240 | error = true; | 243 | error = true; |
241 | return QString(); | 244 | return QString(); |
242 | } | 245 | } |
243 | error = false; | 246 | error = false; |
244 | return line.mid(0, findSpace); | 247 | return line.mid(0, findSpace); |
245 | } | 248 | } |
246 | 249 | ||
247 | /** | 250 | /** |
248 | * Gets the method of the interface that is currently selected. | 251 | * Gets the method of the interface that is currently selected. |
249 | * @return QString name of the method such as staic or dhcp. | 252 | * @return QString name of the method such as staic or dhcp. |
250 | * See the man page of interfaces for possible methods depending on the family. | 253 | * See the man page of interfaces for possible methods depending on the family. |
251 | * @param error set to true if any error occurs, false otherwise. | 254 | * @param error set to true if any error occurs, false otherwise. |
252 | */ | 255 | */ |
253 | QString Interfaces::getInterfaceMethod(bool &error){ | 256 | QString Interfaces::getInterfaceMethod(bool &error){ |
254 | QString name = getInterfaceName(error); | 257 | QString name = getInterfaceName(error); |
255 | if(error) | 258 | if(error) |
256 | return QString(); | 259 | return QString(); |
257 | QString family = getInterfaceFamily(error); | 260 | QString family = getInterfaceFamily(error); |
258 | if(error) | 261 | if(error) |
259 | return QString(); | 262 | return QString(); |
260 | QString line = (*currentIface); | 263 | QString line = (*currentIface); |
261 | line = line.mid(QString(IFACE).length()+1, line.length()); | 264 | line = line.mid(QString(IFACE).length()+1, line.length()); |
262 | line = line.mid(name.length()+1, line.length()); | 265 | line = line.mid(name.length()+1, line.length()); |
263 | line = line.mid(family.length()+1, line.length()); | 266 | line = line.mid(family.length()+1, line.length()); |
264 | line = line.simplifyWhiteSpace(); | 267 | line = line.simplifyWhiteSpace(); |
265 | error = false; | 268 | error = false; |
266 | return line; | 269 | return line; |
267 | } | 270 | } |
268 | 271 | ||
269 | /** | 272 | /** |
270 | * Sets the interface name to newName. | 273 | * Sets the interface name to newName. |
271 | * @param newName the new name of the interface. All whitespace is removed. | 274 | * @param newName the new name of the interface. All whitespace is removed. |
272 | * @return bool true if successfull. | 275 | * @return bool true if successfull. |
273 | */ | 276 | */ |
274 | bool Interfaces::setInterfaceName(const QString &newName){ | 277 | bool Interfaces::setInterfaceName(const QString &newName){ |
278 | qDebug("setInterfaceName %s", newName.latin1()); | ||
275 | if(currentIface == interfaces.end()) | 279 | if(currentIface == interfaces.end()) |
276 | return false; | 280 | return false; |
277 | QString name = newName.simplifyWhiteSpace(); | 281 | QString name = newName.simplifyWhiteSpace(); |
278 | name = name.replace(QRegExp(" "), ""); | 282 | name = name.replace(QRegExp(" "), ""); |
279 | bool returnValue = false; | 283 | bool returnValue = false; |
280 | (*currentIface) = QString("iface %1 %2 %3").arg(name).arg(getInterfaceFamily(returnValue)).arg(getInterfaceMethod(returnValue)); | 284 | QString tmp = QString("iface %1 %2 %3").arg(name).arg(getInterfaceFamily(returnValue)).arg(getInterfaceMethod(returnValue)); |
285 | qDebug("setting %s",tmp.latin1()); | ||
286 | |||
287 | (*currentIface) = tmp; | ||
281 | return !returnValue; | 288 | return !returnValue; |
282 | } | 289 | } |
283 | 290 | ||
284 | /** | 291 | /** |
285 | * Sets the interface family to newName. | 292 | * Sets the interface family to newName. |
286 | * @param newName the new name of the interface. Must be one of the families | 293 | * @param newName the new name of the interface. Must be one of the families |
287 | * defined in the interfaces.h file. | 294 | * defined in the interfaces.h file. |
288 | * @return bool true if successfull. | 295 | * @return bool true if successfull. |
289 | */ | 296 | */ |
290 | bool Interfaces::setInterfaceFamily(const QString &newName){ | 297 | bool Interfaces::setInterfaceFamily(const QString &newName){ |
291 | if(currentIface == interfaces.end()) | 298 | if(currentIface == interfaces.end()) |
292 | return false; | 299 | return false; |
293 | if(acceptedFamily.contains(newName)==0) | 300 | if(acceptedFamily.contains(newName)==0) |
294 | return false; | 301 | return false; |
295 | bool returnValue = false; | 302 | bool returnValue = false; |
296 | (*currentIface) = QString("iface %1 %2 %3").arg(getInterfaceName(returnValue)).arg(newName).arg(getInterfaceMethod(returnValue)); | 303 | (*currentIface) = QString("iface %1 %2 %3").arg(getInterfaceName(returnValue)).arg(newName).arg(getInterfaceMethod(returnValue)); |
297 | return !returnValue; | 304 | return !returnValue; |
298 | } | 305 | } |
299 | 306 | ||
300 | /** | 307 | /** |
301 | * Sets the interface method to newName | 308 | * Sets the interface method to newName |
302 | * @param newName the new name of the interface | 309 | * @param newName the new name of the interface |
303 | * @return bool true if successfull. | 310 | * @return bool true if successfull. |
304 | */ | 311 | */ |
305 | bool Interfaces::setInterfaceMethod(const QString &newName){ | 312 | bool Interfaces::setInterfaceMethod(const QString &newName){ |
306 | if(currentIface == interfaces.end()) | 313 | if(currentIface == interfaces.end()) |
307 | return false; | 314 | return false; |
308 | bool returnValue = false; | 315 | bool returnValue = false; |
309 | (*currentIface) = QString("iface %1 %2 %3").arg(getInterfaceName(returnValue)).arg(getInterfaceFamily(returnValue)).arg(newName); | 316 | (*currentIface) = QString("iface %1 %2 %3").arg(getInterfaceName(returnValue)).arg(getInterfaceFamily(returnValue)).arg(newName); |
310 | return !returnValue; | 317 | return !returnValue; |
311 | } | 318 | } |
312 | 319 | ||
313 | /** | 320 | /** |
314 | * Get a value for an option in the currently selected interface. For example | 321 | * Get a value for an option in the currently selected interface. For example |
315 | * calling getInterfaceOption("address") on the following stanza would | 322 | * calling getInterfaceOption("address") on the following stanza would |
316 | * return 192.168.1.1. | 323 | * return 192.168.1.1. |
317 | * iface eth0 static | 324 | * iface eth0 static |
318 | * address 192.168.1.1 | 325 | * address 192.168.1.1 |
319 | * @param option the options to get the value. | 326 | * @param option the options to get the value. |
320 | * @param error set to true if any error occurs, false otherwise. | 327 | * @param error set to true if any error occurs, false otherwise. |
321 | * @return QString the options value. QString::null if error == true | 328 | * @return QString the options value. QString::null if error == true |
322 | */ | 329 | */ |
323 | QString Interfaces::getInterfaceOption(const QString &option, bool &error){ | 330 | QString Interfaces::getInterfaceOption(const QString &option, bool &error){ |
324 | return getOption(currentIface, option, error); | 331 | return getOption(currentIface, option, error); |
325 | } | 332 | } |
326 | 333 | ||
327 | /** | 334 | /** |
328 | * Set a value for an option in the currently selected interface. If option | 335 | * Set a value for an option in the currently selected interface. If option |
329 | * doesn't exist then it is added along with the value. | 336 | * doesn't exist then it is added along with the value. |
337 | * If value isEmpty() then we will remove the option | ||
338 | * | ||
330 | * @param option the options to set the value. | 339 | * @param option the options to set the value. |
331 | * @param value the value that option should be set to. | 340 | * @param value the value that option should be set to. |
332 | * @param error set to true if any error occurs, false otherwise. | 341 | * @param error set to true if any error occurs, false otherwise. |
333 | * @return QString the options value. QString::null if error == true | 342 | * @return QString the options value. QString::null if error == true |
334 | */ | 343 | */ |
335 | bool Interfaces::setInterfaceOption(const QString &option, const QString &value){ | 344 | bool Interfaces::setInterfaceOption(const QString &option, const QString &value){ |
336 | return setOption(currentIface, option, value); | 345 | if( value.stripWhiteSpace().isEmpty() ) |
346 | return removeInterfaceOption( option ); | ||
347 | |||
348 | qDebug("iface >%s< option >%s< value >%s<", (*currentIface).latin1(), option.latin1(),value.latin1()); | ||
349 | return setOption(currentIface, option, value); | ||
337 | } | 350 | } |
338 | 351 | ||
339 | /** | 352 | /** |
340 | * Removes a value for an option in the currently selected interface. | 353 | * Removes a value for an option in the currently selected interface. |
341 | * @param option the options to set the value. | 354 | * @param option the options to set the value. |
342 | * @param error set to true if any error occurs, false otherwise. | 355 | * @param error set to true if any error occurs, false otherwise. |
343 | * @return QString the options value. QString::null if error == true | 356 | * @return QString the options value. QString::null if error == true |
344 | */ | 357 | */ |
345 | bool Interfaces::removeInterfaceOption(const QString &option){ | 358 | bool Interfaces::removeInterfaceOption(const QString &option){ |
346 | return removeOption(currentIface, option); | 359 | return removeOption(currentIface, option); |
347 | } | 360 | } |
348 | 361 | ||
349 | /** | 362 | /** |
350 | * Removes a value for an option in the currently selected interface. | 363 | * Removes a value for an option in the currently selected interface. |
351 | * @param option the options to set the value. | 364 | * @param option the options to set the value. |
352 | * @param value the value that option should be set to. | 365 | * @param value the value that option should be set to. |
353 | * @param error set to true if any error occurs, false otherwise. | 366 | * @param error set to true if any error occurs, false otherwise. |
354 | * @return QString the options value. QString::null if error == true | 367 | * @return QString the options value. QString::null if error == true |
355 | */ | 368 | */ |
356 | bool Interfaces::removeInterfaceOption(const QString &option, const QString &value){ | 369 | bool Interfaces::removeInterfaceOption(const QString &option, const QString &value){ |
357 | return removeOption(currentIface, option, value); | 370 | return removeOption(currentIface, option, value); |
358 | } | 371 | } |
359 | 372 | ||
360 | /** | 373 | /** |
361 | * Removes all of the options from the currently selected interface. | 374 | * Removes all of the options from the currently selected interface. |
362 | * @return bool error if if successfull | 375 | * @return bool error if if successfull |
363 | */ | 376 | */ |
364 | bool Interfaces::removeAllInterfaceOptions(){ | 377 | bool Interfaces::removeAllInterfaceOptions(){ |
365 | return removeAllOptions(currentIface); | 378 | return removeAllOptions(currentIface); |
366 | } | 379 | } |
367 | 380 | ||
368 | /** | 381 | /** |
369 | * Set the current map to interface's map. This needs to be done before you | 382 | * Set the current map to interface's map. This needs to be done before you |
370 | * can call addMapping(), set/getMap(), and get/setScript(). | 383 | * can call addMapping(), set/getMap(), and get/setScript(). |
371 | * @param interface the name of the interface to set. All whitespace is | 384 | * @param interface the name of the interface to set. All whitespace is |
372 | * removed from the interface name. | 385 | * removed from the interface name. |
373 | * @return bool true if it is successfull. | 386 | * @return bool true if it is successfull. |
374 | */ | 387 | */ |
375 | bool Interfaces::setMapping(const QString &interface){ | 388 | bool Interfaces::setMapping(const QString &interface){ |
376 | QString interfaceName = interface.simplifyWhiteSpace(); | 389 | QString interfaceName = interface.simplifyWhiteSpace(); |
377 | interfaceName = interfaceName.replace(QRegExp(" "), ""); | 390 | interfaceName = interfaceName.replace(QRegExp(" "), ""); |
378 | return setStanza(MAPPING, interfaceName, currentMapping); | 391 | return setStanza(MAPPING, interfaceName, currentMapping); |
379 | } | 392 | } |
380 | 393 | ||
381 | /** | 394 | /** |
382 | * Adds a new Mapping to the interfaces file with interfaces. | 395 | * Adds a new Mapping to the interfaces file with interfaces. |
383 | * @param interface the name(s) of the interfaces to set to this mapping | 396 | * @param interface the name(s) of the interfaces to set to this mapping |
384 | */ | 397 | */ |
@@ -447,184 +460,205 @@ QString Interfaces::getScript(bool &error){ | |||
447 | /** | 460 | /** |
448 | * Helper function used to parse through the QStringList and put pointers in | 461 | * Helper function used to parse through the QStringList and put pointers in |
449 | * the correct place. | 462 | * the correct place. |
450 | * @param stanza The stanza (auto, iface, mapping) to look for. | 463 | * @param stanza The stanza (auto, iface, mapping) to look for. |
451 | * @param option string that must be in the stanza's main line. | 464 | * @param option string that must be in the stanza's main line. |
452 | * @param interator interator to place at location of stanza if successfull. | 465 | * @param interator interator to place at location of stanza if successfull. |
453 | * @return bool true if the stanza is found. | 466 | * @return bool true if the stanza is found. |
454 | */ | 467 | */ |
455 | bool Interfaces::setStanza(const QString &stanza, const QString &option, QStringList::Iterator &iterator){ | 468 | bool Interfaces::setStanza(const QString &stanza, const QString &option, QStringList::Iterator &iterator){ |
456 | bool found = false; | 469 | bool found = false; |
457 | iterator = interfaces.end(); | 470 | iterator = interfaces.end(); |
458 | for ( QStringList::Iterator it = interfaces.begin(); it != interfaces.end(); ++it ) { | 471 | for ( QStringList::Iterator it = interfaces.begin(); it != interfaces.end(); ++it ) { |
459 | QString line = (*it).simplifyWhiteSpace(); | 472 | QString line = (*it).simplifyWhiteSpace(); |
460 | if(line.contains(stanza) && line.contains(option) && line.at(0) != '#'){ | 473 | if(line.contains(stanza) && line.contains(option) && line.at(0) != '#'){ |
461 | uint point = line.find(option); | 474 | uint point = line.find(option); |
462 | bool valid = true; | 475 | bool valid = true; |
463 | if(point > 0){ | 476 | if(point > 0){ |
464 | // There are more chars in the line. check +1 | 477 | // There are more chars in the line. check +1 |
465 | if(line.at(point-1) != ' ') | 478 | if(line.at(point-1) != ' ') |
466 | valid = false; | 479 | valid = false; |
467 | } | 480 | } |
468 | point += option.length(); | 481 | point += option.length(); |
469 | if(point < line.length()-1){ | 482 | if(point < line.length()-1){ |
470 | // There are more chars in the line. check -1 | 483 | // There are more chars in the line. check -1 |
471 | if(line.at(point) != ' ') | 484 | if(line.at(point) != ' ') |
472 | valid = false; | 485 | valid = false; |
473 | } | 486 | } |
474 | if(valid){ | 487 | if(valid){ |
475 | if(found == true){ | 488 | if(found == true){ |
476 | qDebug(QString("Interfaces: Found multiple stanza's for search: %1 %2").arg(stanza).arg(option).latin1()); | 489 | qDebug(QString("Interfaces: Found multiple stanza's for search: %1 %2").arg(stanza).arg(option).latin1()); |
477 | } | 490 | } |
478 | found = true; | 491 | found = true; |
479 | iterator = it; | 492 | iterator = it; |
480 | } | 493 | } |
481 | } | 494 | } |
482 | } | 495 | } |
483 | return found; | 496 | return found; |
484 | } | 497 | } |
485 | 498 | ||
486 | /** | 499 | /** |
487 | * Sets a value of an option in a stanza | 500 | * Sets a value of an option in a stanza |
488 | * @param start the start of the stanza | 501 | * @param start the start of the stanza |
489 | * @param option the option to use when setting value. | 502 | * @param option the option to use when setting value. |
490 | * @return bool true if successfull, false otherwise. | 503 | * @return bool true if successfull, false otherwise. |
491 | */ | 504 | */ |
492 | bool Interfaces::setOption(const QStringList::Iterator &start, const QString &option, const QString &value){ | 505 | bool Interfaces::setOption(const QStringList::Iterator &start, const QString &option, const QString &value){ |
493 | if(start == interfaces.end()) | 506 | if(start == interfaces.end()) |
494 | return false; | 507 | return false; |
495 | 508 | qDebug("setting option"); | |
496 | bool found = false; | 509 | bool found = false; |
510 | bool replaced = false; | ||
511 | QStringList::Iterator insertAt = NULL; | ||
497 | for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) { | 512 | for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) { |
498 | if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){ | 513 | qDebug(" Interfaces::setOption got line >%s<",(*it).latin1()); |
499 | if(!found && value != ""){ | 514 | // FIXME: was not completly stupid just wrong sice all options got inserted bevore the iface line |
500 | // Got to the end of the stanza without finding it, so append it. | 515 | // but since it works with an empty interfaces file I (tille) will not do anything more |
501 | interfaces.insert(--it, QString("\t%1 %2").arg(option).arg(value)); | 516 | if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) ){ |
502 | } | 517 | if (found) break; |
503 | found = true; | 518 | // && it != start){ |
504 | break; | 519 | // if(!found && value != ""){ |
505 | } | 520 | // // Got to the end of the stanza without finding it, so append it. |
521 | // qDebug(" Got to the end of the stanza without finding it, so append it."); | ||
522 | // interfaces.insert(--it, QString("\t%1 %2").arg(option).arg(value)); | ||
523 | // } | ||
524 | qDebug("found 1"); | ||
525 | // interfaces.insert(++it, QString("\t%1 %2").arg(option).arg(value)); | ||
526 | found = true; | ||
527 | insertAt = it; | ||
528 | |||
529 | } | ||
506 | if((*it).contains(option) && it != start && (*it).at(0) != '#'){ | 530 | if((*it).contains(option) && it != start && (*it).at(0) != '#'){ |
507 | // Found it in stanza so replace it. | 531 | // Found it in stanza so replace it. |
532 | qDebug("found 2"); | ||
508 | if(found) | 533 | if(found) |
509 | qDebug(QString("Interfaces: Set Options found more then one value for option: %1 in stanza: %1").arg(option).arg((*start)).latin1()); | 534 | qDebug(QString("Interfaces: Set Options found more then one value for option: %1 in stanza: %1").arg(option).arg((*start)).latin1()); |
510 | found = true; | 535 | found = true; |
536 | replaced = true; | ||
511 | (*it) = QString("\t%1 %2").arg(option).arg(value); | 537 | (*it) = QString("\t%1 %2").arg(option).arg(value); |
512 | } | 538 | } |
513 | } | 539 | } |
514 | if(!found){ | 540 | if(!found){ |
541 | qDebug("! found insert anyway"); | ||
515 | QStringList::Iterator p = start; | 542 | QStringList::Iterator p = start; |
516 | interfaces.insert(++p, QString("\t%1 %2").arg(option).arg(value)); | 543 | interfaces.insert(++p, QString("\t%1 %2").arg(option).arg(value)); |
517 | found = true; | 544 | found = true; |
518 | } | 545 | } |
546 | |||
547 | if(found && !replaced){ | ||
548 | qDebug("found iface but not the option so insert it here..."); | ||
549 | interfaces.insert(++insertAt, QString("\t%1 %2").arg(option).arg(value)); | ||
550 | } | ||
519 | return found; | 551 | return found; |
520 | } | 552 | } |
521 | 553 | ||
522 | /** | 554 | /** |
523 | * Removes a stanza and all of its options | 555 | * Removes a stanza and all of its options |
524 | * @param stanza the stanza to remove | 556 | * @param stanza the stanza to remove |
525 | * @return bool true if successfull. | 557 | * @return bool true if successfull. |
526 | */ | 558 | */ |
527 | bool Interfaces::removeStanza(QStringList::Iterator &stanza){ | 559 | bool Interfaces::removeStanza(QStringList::Iterator &stanza){ |
528 | if(stanza == interfaces.end()) | 560 | if(stanza == interfaces.end()) |
529 | return false; | 561 | return false; |
530 | (*stanza) = ""; | 562 | (*stanza) = ""; |
531 | return removeAllOptions(stanza); | 563 | return removeAllOptions(stanza); |
532 | } | 564 | } |
533 | 565 | ||
534 | /** | 566 | /** |
535 | * Removes a option in a stanza | 567 | * Removes a option in a stanza |
536 | * @param start the start of the stanza | 568 | * @param start the start of the stanza |
537 | * @param option the option to remove | 569 | * @param option the option to remove |
538 | * @return bool true if successfull, false otherwise. | 570 | * @return bool true if successfull, false otherwise. |
539 | */ | 571 | */ |
540 | bool Interfaces::removeOption(const QStringList::Iterator &start, const QString &option){ | 572 | bool Interfaces::removeOption(const QStringList::Iterator &start, const QString &option){ |
541 | if(start == interfaces.end()) | 573 | if(start == interfaces.end()) |
542 | return false; | 574 | return false; |
543 | 575 | ||
544 | bool found = false; | 576 | bool found = false; |
545 | for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) { | 577 | for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) { |
546 | if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){ | 578 | if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){ |
547 | // got to the end without finding it | 579 | // got to the end without finding it |
548 | break; | 580 | break; |
549 | } | 581 | } |
550 | if((*it).contains(option) && it != start && (*it).at(0) != '#'){ | 582 | if((*it).contains(option) && it != start && (*it).at(0) != '#'){ |
551 | // Found it in stanza so replace it. | 583 | // Found it in stanza so replace it. |
552 | if(found) | 584 | if(found) |
553 | qDebug(QString("Interfaces: Set Options found more then one value for option: %1 in stanza: %1").arg(option).arg((*start)).latin1()); | 585 | qDebug(QString("Interfaces: Set Options found more then one value for option: %1 in stanza: %1").arg(option).arg((*start)).latin1()); |
554 | found = true; | 586 | found = true; |
555 | (*it) = ""; | 587 | it = interfaces.remove( it ); // we really want to remove the line |
588 | --it; // we do ++it later in the head of the for loop | ||
556 | } | 589 | } |
557 | } | 590 | } |
558 | return found; | 591 | return found; |
559 | } | 592 | } |
560 | 593 | ||
561 | /** | 594 | /** |
562 | * Removes a option in a stanza | 595 | * Removes a option in a stanza |
563 | * @param start the start of the stanza | 596 | * @param start the start of the stanza |
564 | * @param option the option to use when setting value. | 597 | * @param option the option to use when setting value. |
565 | * @return bool true if successfull, false otherwise. | 598 | * @return bool true if successfull, false otherwise. |
566 | */ | 599 | */ |
567 | bool Interfaces::removeOption(const QStringList::Iterator &start, const QString &option, const QString &value){ | 600 | bool Interfaces::removeOption(const QStringList::Iterator &start, const QString &option, const QString &value){ |
568 | if(start == interfaces.end()) | 601 | if(start == interfaces.end()) |
569 | return false; | 602 | return false; |
570 | 603 | ||
571 | bool found = false; | 604 | bool found = false; |
572 | for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) { | 605 | for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) { |
573 | if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){ | 606 | if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){ |
574 | // got to the end without finding it | 607 | // got to the end without finding it |
575 | break; | 608 | break; |
576 | } | 609 | } |
577 | if((*it).contains(option) && (*it).contains(value) && it != start && (*it).at(0) != '#'){ | 610 | if((*it).contains(option) && (*it).contains(value) && it != start && (*it).at(0) != '#'){ |
578 | // Found it in stanza so replace it. | 611 | // Found it in stanza so replace it. |
579 | if(found) | 612 | if(found) |
580 | qDebug(QString("Interfaces: Set Options found more then one value for option: %1 in stanza: %1").arg(option).arg((*start)).latin1()); | 613 | qDebug(QString("Interfaces: Set Options found more then one value for option: %1 in stanza: %1").arg(option).arg((*start)).latin1()); |
581 | found = true; | 614 | found = true; |
582 | (*it) = ""; | 615 | it = interfaces.remove( it ); // we really want to remove the line |
616 | --it; // we do ++it later in the head of the for loop | ||
583 | } | 617 | } |
584 | } | 618 | } |
585 | return found; | 619 | return found; |
586 | } | 620 | } |
587 | 621 | ||
588 | /** | 622 | /** |
589 | * Removes all options in a stanza | 623 | * Removes all options in a stanza |
590 | * @param start the start of the stanza | 624 | * @param start the start of the stanza |
591 | * @return bool true if successfull, false otherwise. | 625 | * @return bool true if successfull, false otherwise. |
592 | */ | 626 | */ |
593 | bool Interfaces::removeAllOptions(const QStringList::Iterator &start){ | 627 | bool Interfaces::removeAllOptions(const QStringList::Iterator &start){ |
594 | if(start == interfaces.end()) | 628 | if(start == interfaces.end()) |
595 | return false; | 629 | return false; |
596 | 630 | ||
597 | QStringList::Iterator it = start; | 631 | QStringList::Iterator it = start; |
598 | it = ++it; | 632 | it = ++it; |
599 | for (; it != interfaces.end(); ++it ) { | 633 | for (; it != interfaces.end(); ++it ) { |
600 | if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){ | 634 | if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){ |
601 | break; | 635 | break; |
602 | } | 636 | } |
603 | it = interfaces.remove(it); | 637 | it = interfaces.remove(it); |
604 | it = --it; | 638 | it = --it; |
605 | } | 639 | } |
606 | // Leave a space between this interface and the next. | 640 | // Leave a space between this interface and the next. |
607 | interfaces.insert(it, QString("")); | 641 | interfaces.insert(it, QString("")); |
608 | return true; | 642 | return true; |
609 | } | 643 | } |
610 | 644 | ||
611 | /** | 645 | /** |
612 | * Gets a value of an option in a stanza | 646 | * Gets a value of an option in a stanza |
613 | * @param start the start of the stanza | 647 | * @param start the start of the stanza |
614 | * @param option the option to use when getting the value. | 648 | * @param option the option to use when getting the value. |
615 | * @param bool true if errors false otherwise. | 649 | * @param bool true if errors false otherwise. |
616 | * @return QString the value of option QString::null() if error == true. | 650 | * @return QString the value of option QString::null() if error == true. |
617 | */ | 651 | */ |
618 | QString Interfaces::getOption(const QStringList::Iterator &start, const QString &option, bool &error){ | 652 | QString Interfaces::getOption(const QStringList::Iterator &start, const QString &option, bool &error){ |
619 | if(start == interfaces.end()){ | 653 | if(start == interfaces.end()){ |
620 | error = false; | 654 | error = false; |
621 | return QString(); | 655 | return QString(); |
622 | } | 656 | } |
623 | 657 | ||
624 | QString value; | 658 | QString value; |
625 | bool found = false; | 659 | bool found = false; |
626 | for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) { | 660 | for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) { |
627 | if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){ | 661 | if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){ |
628 | break; | 662 | break; |
629 | } | 663 | } |
630 | if((*it).contains(option) && (*it).at(0) != '#'){ | 664 | if((*it).contains(option) && (*it).at(0) != '#'){ |