author | schurig <schurig> | 2003-06-13 10:49:17 (UTC) |
---|---|---|
committer | schurig <schurig> | 2003-06-13 10:49:17 (UTC) |
commit | c48af71638614aaff280178693448916e0e98102 (patch) (unidiff) | |
tree | 127db502aedb7411dd68e0a49861018e1b09d262 | |
parent | 60ba51b33a060dde352caa678f2e337f5f5590f5 (diff) | |
download | opie-c48af71638614aaff280178693448916e0e98102.zip opie-c48af71638614aaff280178693448916e0e98102.tar.gz opie-c48af71638614aaff280178693448916e0e98102.tar.bz2 |
little simplificatin -> less code size
-rw-r--r-- | noncore/settings/networksettings/interfaces/interfaces.cpp | 8 |
1 files changed, 3 insertions, 5 deletions
diff --git a/noncore/settings/networksettings/interfaces/interfaces.cpp b/noncore/settings/networksettings/interfaces/interfaces.cpp index 8d3e151..a62a90c 100644 --- a/noncore/settings/networksettings/interfaces/interfaces.cpp +++ b/noncore/settings/networksettings/interfaces/interfaces.cpp | |||
@@ -1,680 +1,678 @@ | |||
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 | // The three stanza's |
8 | #define AUTO "auto" | 8 | #define AUTO "auto" |
9 | #define IFACE "iface" | 9 | #define IFACE "iface" |
10 | #define MAPPING "mapping" | 10 | #define MAPPING "mapping" |
11 | 11 | ||
12 | /** | 12 | /** |
13 | * 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 |
14 | * the \n for interfaces variable. | 14 | * the \n for interfaces variable. |
15 | * @param useInterfacesFile if an interface file other then the default is | 15 | * @param useInterfacesFile if an interface file other then the default is |
16 | * desired to be used it should be passed in. | 16 | * desired to be used it should be passed in. |
17 | */ | 17 | */ |
18 | Interfaces::Interfaces(QString useInterfacesFile){ | 18 | Interfaces::Interfaces(QString useInterfacesFile){ |
19 | acceptedFamily.append(INTERFACES_FAMILY_INET); | 19 | acceptedFamily.append(INTERFACES_FAMILY_INET); |
20 | acceptedFamily.append(INTERFACES_FAMILY_IPX); | 20 | acceptedFamily.append(INTERFACES_FAMILY_IPX); |
21 | acceptedFamily.append(INTERFACES_FAMILY_INET6); | 21 | acceptedFamily.append(INTERFACES_FAMILY_INET6); |
22 | 22 | ||
23 | interfacesFile = useInterfacesFile; | 23 | interfacesFile = useInterfacesFile; |
24 | QFile file(interfacesFile); | 24 | QFile file(interfacesFile); |
25 | if (!file.open(IO_ReadOnly)){ | 25 | if (!file.open(IO_ReadOnly)){ |
26 | qDebug(QString("Interfaces: Can't open file: %1 for reading.").arg(interfacesFile).latin1()); | 26 | qDebug("Interfaces: Can't open file: %s for reading.", interfacesFile.latin1() ); |
27 | currentIface = interfaces.end(); | 27 | currentIface = interfaces.end(); |
28 | currentMapping = interfaces.end(); | 28 | currentMapping = interfaces.end(); |
29 | return; | 29 | return; |
30 | } | 30 | } |
31 | QTextStream stream( &file ); | 31 | QTextStream stream( &file ); |
32 | QString line; | 32 | QString line; |
33 | while ( !stream.eof() ) { | 33 | while ( !stream.eof() ) { |
34 | line += stream.readLine(); | 34 | line += stream.readLine(); |
35 | line += "\n"; | 35 | line += "\n"; |
36 | } | 36 | } |
37 | file.close(); | 37 | file.close(); |
38 | interfaces = QStringList::split("\n", line, true); | 38 | interfaces = QStringList::split("\n", line, true); |
39 | 39 | ||
40 | currentIface = interfaces.end(); | 40 | currentIface = interfaces.end(); |
41 | currentMapping = interfaces.end(); | 41 | currentMapping = interfaces.end(); |
42 | } | 42 | } |
43 | 43 | ||
44 | 44 | ||
45 | /** | 45 | /** |
46 | * Get a list of all interfaces in the interface file. Usefull for | 46 | * 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 | 47 | * hardware that is not currently connected such as an 802.11b card |
48 | * not plugged in, but configured for when it is plugged in. | 48 | * not plugged in, but configured for when it is plugged in. |
49 | * @return Return string list of interfaces. | 49 | * @return Return string list of interfaces. |
50 | **/ | 50 | **/ |
51 | QStringList Interfaces::getInterfaceList(){ | 51 | QStringList Interfaces::getInterfaceList(){ |
52 | QStringList list; | 52 | QStringList list; |
53 | for ( QStringList::Iterator it = interfaces.begin(); it != interfaces.end(); ++it ) { | 53 | for ( QStringList::Iterator it = interfaces.begin(); it != interfaces.end(); ++it ) { |
54 | QString line = (*it).simplifyWhiteSpace(); | 54 | QString line = (*it).simplifyWhiteSpace(); |
55 | if(line.contains(IFACE) && line.at(0) != '#'){ | 55 | if(line.contains(IFACE) && line.at(0) != '#'){ |
56 | line = line.mid(QString(IFACE).length() +1, line.length()); | 56 | line = line.mid(QString(IFACE).length() +1, line.length()); |
57 | line = line.simplifyWhiteSpace(); | 57 | line = line.simplifyWhiteSpace(); |
58 | int findSpace = line.find(" "); | 58 | int findSpace = line.find(" "); |
59 | if( findSpace >= 0){ | 59 | if( findSpace >= 0){ |
60 | line = line.mid(0, findSpace); | 60 | line = line.mid(0, findSpace); |
61 | list.append(line); | 61 | list.append(line); |
62 | } | 62 | } |
63 | } | 63 | } |
64 | } | 64 | } |
65 | return list; | 65 | return list; |
66 | } | 66 | } |
67 | 67 | ||
68 | /** | 68 | /** |
69 | * Find out if interface is in an "auto" group or not. | 69 | * Find out if interface is in an "auto" group or not. |
70 | * Report any duplicates such as eth0 being in two differnt auto's | 70 | * Report any duplicates such as eth0 being in two differnt auto's |
71 | * @param interface interface to check to see if it is on or not. | 71 | * @param interface interface to check to see if it is on or not. |
72 | * @return true is interface is in auto | 72 | * @return true is interface is in auto |
73 | */ | 73 | */ |
74 | bool Interfaces::isAuto(const QString &interface) const { | 74 | bool Interfaces::isAuto(const QString &interface) const { |
75 | QStringList autoLines = interfaces.grep(QRegExp(AUTO)); | 75 | QStringList autoLines = interfaces.grep(QRegExp(AUTO)); |
76 | QStringList awi = autoLines.grep(QRegExp(interface)); | 76 | QStringList awi = autoLines.grep(QRegExp(interface)); |
77 | if(awi.count() > 1) | 77 | if(awi.count() > 1) |
78 | qDebug(QString("Interfaces: Found more then auto group with interface: %1.").arg(interface).latin1()); | 78 | qDebug(QString("Interfaces: Found more then auto group with interface: %1.").arg(interface).latin1()); |
79 | if(awi.count() < 1) | 79 | return awi.count() > 0; |
80 | return false; | ||
81 | return true; | ||
82 | } | 80 | } |
83 | 81 | ||
84 | /** | 82 | /** |
85 | * Attempt to set the auto option for interface to setAuto. | 83 | * Attempt to set the auto option for interface to setAuto. |
86 | * @param interface the interface to set | 84 | * @param interface the interface to set |
87 | * @param setAuto the value to set interface to. | 85 | * @param setAuto the value to set interface to. |
88 | * @return false if already set to setAuto. | 86 | * @return false if already set to setAuto. |
89 | * */ | 87 | * */ |
90 | bool Interfaces::setAuto(const QString &interface, bool setAuto){ | 88 | bool Interfaces::setAuto(const QString &interface, bool setAuto){ |
91 | // Don't need to set it if it is already set. | 89 | // Don't need to set it if it is already set. |
92 | if(isAuto(interface) == setAuto) | 90 | if(isAuto(interface) == setAuto) |
93 | return false; | 91 | return false; |
94 | 92 | ||
95 | bool changed = false; | 93 | bool changed = false; |
96 | for ( QStringList::Iterator it = interfaces.begin(); it != interfaces.end(); ++it ) { | 94 | for ( QStringList::Iterator it = interfaces.begin(); it != interfaces.end(); ++it ) { |
97 | if((*it).contains(AUTO)){ | 95 | if((*it).contains(AUTO)){ |
98 | //We know that they are not in any group so let add to this auto. | 96 | //We know that they are not in any group so let add to this auto. |
99 | if(setAuto){ | 97 | if(setAuto){ |
100 | (*it) = (*it) += " " + interface; | 98 | (*it) = (*it) += " " + interface; |
101 | // Don't care to have such thins as: auto eth0 lo usb0 | 99 | // Don't care to have such thins as: auto eth0 lo usb0 |
102 | (*it) = (*it).simplifyWhiteSpace(); | 100 | (*it) = (*it).simplifyWhiteSpace(); |
103 | changed = true; | 101 | changed = true; |
104 | break; | 102 | break; |
105 | } | 103 | } |
106 | // else see if we need to remove from this one | 104 | // else see if we need to remove from this one |
107 | else{ | 105 | else{ |
108 | if((*it).contains(interface)){ | 106 | if((*it).contains(interface)){ |
109 | (*it) = (*it).replace(QRegExp(interface), ""); | 107 | (*it) = (*it).replace(QRegExp(interface), ""); |
110 | // if AUTO is the only thing left clear the line | 108 | // if AUTO is the only thing left clear the line |
111 | if(((*it).simplifyWhiteSpace()).replace(QRegExp(" "),"") == AUTO) | 109 | if(((*it).simplifyWhiteSpace()).replace(QRegExp(" "),"") == AUTO) |
112 | (*it) = ""; | 110 | (*it) = ""; |
113 | changed = true; | 111 | changed = true; |
114 | // Don't break because we want to make sure we remove all cases. | 112 | // Don't break because we want to make sure we remove all cases. |
115 | } | 113 | } |
116 | } | 114 | } |
117 | } | 115 | } |
118 | } | 116 | } |
119 | // In the case where there is no AUTO field add one. | 117 | // In the case where there is no AUTO field add one. |
120 | if(!changed && setAuto) | 118 | if(!changed && setAuto) |
121 | interfaces.append(QString(AUTO" %1").arg(interface)); | 119 | interfaces.append(QString(AUTO" %1").arg(interface)); |
122 | return true; | 120 | return true; |
123 | } | 121 | } |
124 | 122 | ||
125 | /** | 123 | /** |
126 | * Set the current interface to interface. This needs to be done before you | 124 | * Set the current interface to interface. This needs to be done before you |
127 | * can call getFamily(), getMethod, and get/setOption(). | 125 | * can call getFamily(), getMethod, and get/setOption(). |
128 | * @param interface the name of the interface to set. All whitespace is | 126 | * @param interface the name of the interface to set. All whitespace is |
129 | * removed from the interface name. | 127 | * removed from the interface name. |
130 | * @return bool true if it is successfull. | 128 | * @return bool true if it is successfull. |
131 | */ | 129 | */ |
132 | bool Interfaces::setInterface(QString interface){ | 130 | bool Interfaces::setInterface(QString interface){ |
133 | interface = interface.simplifyWhiteSpace(); | 131 | interface = interface.simplifyWhiteSpace(); |
134 | interface = interface.replace(QRegExp(" "), ""); | 132 | interface = interface.replace(QRegExp(" "), ""); |
135 | return setStanza(IFACE, interface, currentIface); | 133 | return setStanza(IFACE, interface, currentIface); |
136 | } | 134 | } |
137 | 135 | ||
138 | /** | 136 | /** |
139 | * A quick helper funtion to see if the current interface is set. | 137 | * A quick helper funtion to see if the current interface is set. |
140 | * @return bool true if set, false otherwise. | 138 | * @return bool true if set, false otherwise. |
141 | */ | 139 | */ |
142 | bool Interfaces::isInterfaceSet() const { | 140 | bool Interfaces::isInterfaceSet() const { |
143 | return (interfaces.end() != currentIface); | 141 | return (interfaces.end() != currentIface); |
144 | } | 142 | } |
145 | 143 | ||
146 | /** | 144 | /** |
147 | * Add a new interface of with the settings - family and method | 145 | * Add a new interface of with the settings - family and method |
148 | * @param interface the name of the interface to set. All whitespace is | 146 | * @param interface the name of the interface to set. All whitespace is |
149 | * removed from the interface name. | 147 | * removed from the interface name. |
150 | * @param family the family of this interface inet or inet, ipx or inet6 | 148 | * @param family the family of this interface inet or inet, ipx or inet6 |
151 | * Must of one of the families defined in interfaces.h | 149 | * Must of one of the families defined in interfaces.h |
152 | * @param method for the family. see interfaces man page for family methods. | 150 | * @param method for the family. see interfaces man page for family methods. |
153 | * @return true if successfull. | 151 | * @return true if successfull. |
154 | */ | 152 | */ |
155 | bool Interfaces::addInterface(const QString &interface, const QString &family, const QString &method){ | 153 | bool Interfaces::addInterface(const QString &interface, const QString &family, const QString &method){ |
156 | if(0 == acceptedFamily.contains(family)) | 154 | if(0 == acceptedFamily.contains(family)) |
157 | return false; | 155 | return false; |
158 | QString newInterface = interface.simplifyWhiteSpace(); | 156 | QString newInterface = interface.simplifyWhiteSpace(); |
159 | newInterface = newInterface.replace(QRegExp(" "), ""); | 157 | newInterface = newInterface.replace(QRegExp(" "), ""); |
160 | interfaces.append(""); | 158 | interfaces.append(""); |
161 | interfaces.append(QString(IFACE " %1 %2 %3").arg(newInterface).arg(family).arg(method)); | 159 | interfaces.append(QString(IFACE " %1 %2 %3").arg(newInterface).arg(family).arg(method)); |
162 | return true; | 160 | return true; |
163 | } | 161 | } |
164 | 162 | ||
165 | /** | 163 | /** |
166 | * Copies interface with name interface to name newInterface | 164 | * Copies interface with name interface to name newInterface |
167 | * @param newInterface name of the new interface. | 165 | * @param newInterface name of the new interface. |
168 | * @return bool true if successfull | 166 | * @return bool true if successfull |
169 | */ | 167 | */ |
170 | bool Interfaces::copyInterface(const QString &interface, const QString &newInterface){ | 168 | bool Interfaces::copyInterface(const QString &interface, const QString &newInterface){ |
171 | if(!setInterface(interface)) | 169 | if(!setInterface(interface)) |
172 | return false; | 170 | return false; |
173 | 171 | ||
174 | // Store the old interface and bump past the stanza line. | 172 | // Store the old interface and bump past the stanza line. |
175 | QStringList::Iterator it = currentIface; | 173 | QStringList::Iterator it = currentIface; |
176 | it++; | 174 | it++; |
177 | 175 | ||
178 | // Add the new interface | 176 | // Add the new interface |
179 | bool error; | 177 | bool error; |
180 | addInterface(newInterface, getInterfaceFamily(error), getInterfaceMethod(error)); | 178 | addInterface(newInterface, getInterfaceFamily(error), getInterfaceMethod(error)); |
181 | if(!setInterface(newInterface)) | 179 | if(!setInterface(newInterface)) |
182 | return false; | 180 | return false; |
183 | 181 | ||
184 | QStringList::Iterator newIface = currentIface; | 182 | QStringList::Iterator newIface = currentIface; |
185 | newIface++; | 183 | newIface++; |
186 | 184 | ||
187 | // Copy all of the lines | 185 | // Copy all of the lines |
188 | for ( ; it != interfaces.end(); ++it ){ | 186 | for ( ; it != interfaces.end(); ++it ){ |
189 | if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO))) | 187 | if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO))) |
190 | break; | 188 | break; |
191 | newIface = interfaces.insert(newIface, *it); | 189 | newIface = interfaces.insert(newIface, *it); |
192 | } | 190 | } |
193 | 191 | ||
194 | return true; | 192 | return true; |
195 | } | 193 | } |
196 | 194 | ||
197 | /** | 195 | /** |
198 | * Remove the currently selected interface and all of its options. | 196 | * Remove the currently selected interface and all of its options. |
199 | * @return bool if successfull or not. | 197 | * @return bool if successfull or not. |
200 | */ | 198 | */ |
201 | bool Interfaces::removeInterface(){ | 199 | bool Interfaces::removeInterface(){ |
202 | return removeStanza(currentIface); | 200 | return removeStanza(currentIface); |
203 | } | 201 | } |
204 | 202 | ||
205 | /** | 203 | /** |
206 | * Gets the hardware name of the interface that is currently selected. | 204 | * Gets the hardware name of the interface that is currently selected. |
207 | * @return QString name of the hardware interface (eth0, usb2, wlan1...). | 205 | * @return QString name of the hardware interface (eth0, usb2, wlan1...). |
208 | * @param error set to true if any error occurs, false otherwise. | 206 | * @param error set to true if any error occurs, false otherwise. |
209 | */ | 207 | */ |
210 | QString Interfaces::getInterfaceName(bool &error){ | 208 | QString Interfaces::getInterfaceName(bool &error){ |
211 | if(currentIface == interfaces.end()){ | 209 | if(currentIface == interfaces.end()){ |
212 | error = true; | 210 | error = true; |
213 | return QString(); | 211 | return QString(); |
214 | } | 212 | } |
215 | QString line = (*currentIface); | 213 | QString line = (*currentIface); |
216 | line = line.mid(QString(IFACE).length() +1, line.length()); | 214 | line = line.mid(QString(IFACE).length() +1, line.length()); |
217 | line = line.simplifyWhiteSpace(); | 215 | line = line.simplifyWhiteSpace(); |
218 | int findSpace = line.find(" "); | 216 | int findSpace = line.find(" "); |
219 | if( findSpace < 0){ | 217 | if( findSpace < 0){ |
220 | error = true; | 218 | error = true; |
221 | return QString(); | 219 | return QString(); |
222 | } | 220 | } |
223 | error = false; | 221 | error = false; |
224 | return line.mid(0, findSpace); | 222 | return line.mid(0, findSpace); |
225 | } | 223 | } |
226 | 224 | ||
227 | /** | 225 | /** |
228 | * Gets the family name of the interface that is currently selected. | 226 | * Gets the family name of the interface that is currently selected. |
229 | * @return QString name of the family (inet, inet6, ipx). | 227 | * @return QString name of the family (inet, inet6, ipx). |
230 | * @param error set to true if any error occurs, false otherwise. | 228 | * @param error set to true if any error occurs, false otherwise. |
231 | */ | 229 | */ |
232 | QString Interfaces::getInterfaceFamily(bool &error){ | 230 | QString Interfaces::getInterfaceFamily(bool &error){ |
233 | QString name = getInterfaceName(error); | 231 | QString name = getInterfaceName(error); |
234 | if(error) | 232 | if(error) |
235 | return QString(); | 233 | return QString(); |
236 | QString line = (*currentIface); | 234 | QString line = (*currentIface); |
237 | line = line.mid(QString(IFACE).length() +1, line.length()); | 235 | line = line.mid(QString(IFACE).length() +1, line.length()); |
238 | line = line.mid(name.length()+1, line.length()); | 236 | line = line.mid(name.length()+1, line.length()); |
239 | line = line.simplifyWhiteSpace(); | 237 | line = line.simplifyWhiteSpace(); |
240 | int findSpace = line.find(" "); | 238 | int findSpace = line.find(" "); |
241 | if( findSpace < 0){ | 239 | if( findSpace < 0){ |
242 | error = true; | 240 | error = true; |
243 | return QString(); | 241 | return QString(); |
244 | } | 242 | } |
245 | error = false; | 243 | error = false; |
246 | return line.mid(0, findSpace); | 244 | return line.mid(0, findSpace); |
247 | } | 245 | } |
248 | 246 | ||
249 | /** | 247 | /** |
250 | * Gets the method of the interface that is currently selected. | 248 | * Gets the method of the interface that is currently selected. |
251 | * @return QString name of the method such as staic or dhcp. | 249 | * @return QString name of the method such as staic or dhcp. |
252 | * See the man page of interfaces for possible methods depending on the family. | 250 | * See the man page of interfaces for possible methods depending on the family. |
253 | * @param error set to true if any error occurs, false otherwise. | 251 | * @param error set to true if any error occurs, false otherwise. |
254 | */ | 252 | */ |
255 | QString Interfaces::getInterfaceMethod(bool &error){ | 253 | QString Interfaces::getInterfaceMethod(bool &error){ |
256 | QString name = getInterfaceName(error); | 254 | QString name = getInterfaceName(error); |
257 | if(error) | 255 | if(error) |
258 | return QString(); | 256 | return QString(); |
259 | QString family = getInterfaceFamily(error); | 257 | QString family = getInterfaceFamily(error); |
260 | if(error) | 258 | if(error) |
261 | return QString(); | 259 | return QString(); |
262 | QString line = (*currentIface); | 260 | QString line = (*currentIface); |
263 | line = line.mid(QString(IFACE).length()+1, line.length()); | 261 | line = line.mid(QString(IFACE).length()+1, line.length()); |
264 | line = line.mid(name.length()+1, line.length()); | 262 | line = line.mid(name.length()+1, line.length()); |
265 | line = line.mid(family.length()+1, line.length()); | 263 | line = line.mid(family.length()+1, line.length()); |
266 | line = line.simplifyWhiteSpace(); | 264 | line = line.simplifyWhiteSpace(); |
267 | error = false; | 265 | error = false; |
268 | return line; | 266 | return line; |
269 | } | 267 | } |
270 | 268 | ||
271 | /** | 269 | /** |
272 | * Sets the interface name to newName. | 270 | * Sets the interface name to newName. |
273 | * @param newName the new name of the interface. All whitespace is removed. | 271 | * @param newName the new name of the interface. All whitespace is removed. |
274 | * @return bool true if successfull. | 272 | * @return bool true if successfull. |
275 | */ | 273 | */ |
276 | bool Interfaces::setInterfaceName(const QString &newName){ | 274 | bool Interfaces::setInterfaceName(const QString &newName){ |
277 | if(currentIface == interfaces.end()) | 275 | if(currentIface == interfaces.end()) |
278 | return false; | 276 | return false; |
279 | QString name = newName.simplifyWhiteSpace(); | 277 | QString name = newName.simplifyWhiteSpace(); |
280 | name = name.replace(QRegExp(" "), ""); | 278 | name = name.replace(QRegExp(" "), ""); |
281 | bool returnValue = false; | 279 | bool returnValue = false; |
282 | (*currentIface) = QString("iface %1 %2 %3").arg(name).arg(getInterfaceFamily(returnValue)).arg(getInterfaceMethod(returnValue)); | 280 | (*currentIface) = QString("iface %1 %2 %3").arg(name).arg(getInterfaceFamily(returnValue)).arg(getInterfaceMethod(returnValue)); |
283 | return !returnValue; | 281 | return !returnValue; |
284 | } | 282 | } |
285 | 283 | ||
286 | /** | 284 | /** |
287 | * Sets the interface family to newName. | 285 | * Sets the interface family to newName. |
288 | * @param newName the new name of the interface. Must be one of the families | 286 | * @param newName the new name of the interface. Must be one of the families |
289 | * defined in the interfaces.h file. | 287 | * defined in the interfaces.h file. |
290 | * @return bool true if successfull. | 288 | * @return bool true if successfull. |
291 | */ | 289 | */ |
292 | bool Interfaces::setInterfaceFamily(const QString &newName){ | 290 | bool Interfaces::setInterfaceFamily(const QString &newName){ |
293 | if(currentIface == interfaces.end()) | 291 | if(currentIface == interfaces.end()) |
294 | return false; | 292 | return false; |
295 | if(acceptedFamily.contains(newName)==0) | 293 | if(acceptedFamily.contains(newName)==0) |
296 | return false; | 294 | return false; |
297 | bool returnValue = false; | 295 | bool returnValue = false; |
298 | (*currentIface) = QString("iface %1 %2 %3").arg(getInterfaceName(returnValue)).arg(newName).arg(getInterfaceMethod(returnValue)); | 296 | (*currentIface) = QString("iface %1 %2 %3").arg(getInterfaceName(returnValue)).arg(newName).arg(getInterfaceMethod(returnValue)); |
299 | return !returnValue; | 297 | return !returnValue; |
300 | } | 298 | } |
301 | 299 | ||
302 | /** | 300 | /** |
303 | * Sets the interface method to newName | 301 | * Sets the interface method to newName |
304 | * @param newName the new name of the interface | 302 | * @param newName the new name of the interface |
305 | * @return bool true if successfull. | 303 | * @return bool true if successfull. |
306 | */ | 304 | */ |
307 | bool Interfaces::setInterfaceMethod(const QString &newName){ | 305 | bool Interfaces::setInterfaceMethod(const QString &newName){ |
308 | if(currentIface == interfaces.end()) | 306 | if(currentIface == interfaces.end()) |
309 | return false; | 307 | return false; |
310 | bool returnValue = false; | 308 | bool returnValue = false; |
311 | (*currentIface) = QString("iface %1 %2 %3").arg(getInterfaceName(returnValue)).arg(getInterfaceFamily(returnValue)).arg(newName); | 309 | (*currentIface) = QString("iface %1 %2 %3").arg(getInterfaceName(returnValue)).arg(getInterfaceFamily(returnValue)).arg(newName); |
312 | return !returnValue; | 310 | return !returnValue; |
313 | } | 311 | } |
314 | 312 | ||
315 | /** | 313 | /** |
316 | * Get a value for an option in the currently selected interface. For example | 314 | * Get a value for an option in the currently selected interface. For example |
317 | * calling getInterfaceOption("address") on the following stanza would | 315 | * calling getInterfaceOption("address") on the following stanza would |
318 | * return 192.168.1.1. | 316 | * return 192.168.1.1. |
319 | * iface eth0 static | 317 | * iface eth0 static |
320 | * address 192.168.1.1 | 318 | * address 192.168.1.1 |
321 | * @param option the options to get the value. | 319 | * @param option the options to get the value. |
322 | * @param error set to true if any error occurs, false otherwise. | 320 | * @param error set to true if any error occurs, false otherwise. |
323 | * @return QString the options value. QString::null if error == true | 321 | * @return QString the options value. QString::null if error == true |
324 | */ | 322 | */ |
325 | QString Interfaces::getInterfaceOption(const QString &option, bool &error){ | 323 | QString Interfaces::getInterfaceOption(const QString &option, bool &error){ |
326 | return getOption(currentIface, option, error); | 324 | return getOption(currentIface, option, error); |
327 | } | 325 | } |
328 | 326 | ||
329 | /** | 327 | /** |
330 | * Set a value for an option in the currently selected interface. If option | 328 | * Set a value for an option in the currently selected interface. If option |
331 | * doesn't exist then it is added along with the value. | 329 | * doesn't exist then it is added along with the value. |
332 | * @param option the options to set the value. | 330 | * @param option the options to set the value. |
333 | * @param value the value that option should be set to. | 331 | * @param value the value that option should be set to. |
334 | * @param error set to true if any error occurs, false otherwise. | 332 | * @param error set to true if any error occurs, false otherwise. |
335 | * @return QString the options value. QString::null if error == true | 333 | * @return QString the options value. QString::null if error == true |
336 | */ | 334 | */ |
337 | bool Interfaces::setInterfaceOption(const QString &option, const QString &value){ | 335 | bool Interfaces::setInterfaceOption(const QString &option, const QString &value){ |
338 | return setOption(currentIface, option, value); | 336 | return setOption(currentIface, option, value); |
339 | } | 337 | } |
340 | 338 | ||
341 | /** | 339 | /** |
342 | * Removes a value for an option in the currently selected interface. | 340 | * Removes a value for an option in the currently selected interface. |
343 | * @param option the options to set the value. | 341 | * @param option the options to set the value. |
344 | * @param error set to true if any error occurs, false otherwise. | 342 | * @param error set to true if any error occurs, false otherwise. |
345 | * @return QString the options value. QString::null if error == true | 343 | * @return QString the options value. QString::null if error == true |
346 | */ | 344 | */ |
347 | bool Interfaces::removeInterfaceOption(const QString &option){ | 345 | bool Interfaces::removeInterfaceOption(const QString &option){ |
348 | return removeOption(currentIface, option); | 346 | return removeOption(currentIface, option); |
349 | } | 347 | } |
350 | 348 | ||
351 | /** | 349 | /** |
352 | * Removes a value for an option in the currently selected interface. | 350 | * Removes a value for an option in the currently selected interface. |
353 | * @param option the options to set the value. | 351 | * @param option the options to set the value. |
354 | * @param value the value that option should be set to. | 352 | * @param value the value that option should be set to. |
355 | * @param error set to true if any error occurs, false otherwise. | 353 | * @param error set to true if any error occurs, false otherwise. |
356 | * @return QString the options value. QString::null if error == true | 354 | * @return QString the options value. QString::null if error == true |
357 | */ | 355 | */ |
358 | bool Interfaces::removeInterfaceOption(const QString &option, const QString &value){ | 356 | bool Interfaces::removeInterfaceOption(const QString &option, const QString &value){ |
359 | return removeOption(currentIface, option, value); | 357 | return removeOption(currentIface, option, value); |
360 | } | 358 | } |
361 | 359 | ||
362 | /** | 360 | /** |
363 | * Removes all of the options from the currently selected interface. | 361 | * Removes all of the options from the currently selected interface. |
364 | * @return bool error if if successfull | 362 | * @return bool error if if successfull |
365 | */ | 363 | */ |
366 | bool Interfaces::removeAllInterfaceOptions(){ | 364 | bool Interfaces::removeAllInterfaceOptions(){ |
367 | return removeAllOptions(currentIface); | 365 | return removeAllOptions(currentIface); |
368 | } | 366 | } |
369 | 367 | ||
370 | /** | 368 | /** |
371 | * Set the current map to interface's map. This needs to be done before you | 369 | * Set the current map to interface's map. This needs to be done before you |
372 | * can call addMapping(), set/getMap(), and get/setScript(). | 370 | * can call addMapping(), set/getMap(), and get/setScript(). |
373 | * @param interface the name of the interface to set. All whitespace is | 371 | * @param interface the name of the interface to set. All whitespace is |
374 | * removed from the interface name. | 372 | * removed from the interface name. |
375 | * @return bool true if it is successfull. | 373 | * @return bool true if it is successfull. |
376 | */ | 374 | */ |
377 | bool Interfaces::setMapping(const QString &interface){ | 375 | bool Interfaces::setMapping(const QString &interface){ |
378 | QString interfaceName = interface.simplifyWhiteSpace(); | 376 | QString interfaceName = interface.simplifyWhiteSpace(); |
379 | interfaceName = interfaceName.replace(QRegExp(" "), ""); | 377 | interfaceName = interfaceName.replace(QRegExp(" "), ""); |
380 | return setStanza(MAPPING, interfaceName, currentMapping); | 378 | return setStanza(MAPPING, interfaceName, currentMapping); |
381 | } | 379 | } |
382 | 380 | ||
383 | /** | 381 | /** |
384 | * Adds a new Mapping to the interfaces file with interfaces. | 382 | * Adds a new Mapping to the interfaces file with interfaces. |
385 | * @param interface the name(s) of the interfaces to set to this mapping | 383 | * @param interface the name(s) of the interfaces to set to this mapping |
386 | */ | 384 | */ |
387 | void Interfaces::addMapping(const QString &option){ | 385 | void Interfaces::addMapping(const QString &option){ |
388 | interfaces.append(""); | 386 | interfaces.append(""); |
389 | interfaces.append(QString(MAPPING " %1").arg(option)); | 387 | interfaces.append(QString(MAPPING " %1").arg(option)); |
390 | } | 388 | } |
391 | 389 | ||
392 | /** | 390 | /** |
393 | * Remove the currently selected map and all of its options. | 391 | * Remove the currently selected map and all of its options. |
394 | * @return bool if successfull or not. | 392 | * @return bool if successfull or not. |
395 | */ | 393 | */ |
396 | bool Interfaces::removeMapping(){ | 394 | bool Interfaces::removeMapping(){ |
397 | return removeStanza(currentMapping); | 395 | return removeStanza(currentMapping); |
398 | } | 396 | } |
399 | 397 | ||
400 | /** | 398 | /** |
401 | * Set a map option within a mapping. | 399 | * Set a map option within a mapping. |
402 | * @param map map to use | 400 | * @param map map to use |
403 | * @param value value to go with map | 401 | * @param value value to go with map |
404 | * @return bool true if it is successfull. | 402 | * @return bool true if it is successfull. |
405 | */ | 403 | */ |
406 | bool Interfaces::setMap(const QString &map, const QString &value){ | 404 | bool Interfaces::setMap(const QString &map, const QString &value){ |
407 | return setOption(currentMapping, map, value); | 405 | return setOption(currentMapping, map, value); |
408 | } | 406 | } |
409 | 407 | ||
410 | /** | 408 | /** |
411 | * Removes a map option within a mapping. | 409 | * Removes a map option within a mapping. |
412 | * @param map map to use | 410 | * @param map map to use |
413 | * @param value value to go with map | 411 | * @param value value to go with map |
414 | * @return bool true if it is successfull. | 412 | * @return bool true if it is successfull. |
415 | */ | 413 | */ |
416 | bool Interfaces::removeMap(const QString &map, const QString &value){ | 414 | bool Interfaces::removeMap(const QString &map, const QString &value){ |
417 | return removeOption(currentMapping, map, value); | 415 | return removeOption(currentMapping, map, value); |
418 | } | 416 | } |
419 | 417 | ||
420 | /** | 418 | /** |
421 | * Get a map value within a mapping. | 419 | * Get a map value within a mapping. |
422 | * @param map map to get value of | 420 | * @param map map to get value of |
423 | * @param bool true if it is successfull. | 421 | * @param bool true if it is successfull. |
424 | * @return value that goes to the map | 422 | * @return value that goes to the map |
425 | */ | 423 | */ |
426 | QString Interfaces::getMap(const QString &map, bool &error){ | 424 | QString Interfaces::getMap(const QString &map, bool &error){ |
427 | return getOption(currentMapping, map, error); | 425 | return getOption(currentMapping, map, error); |
428 | } | 426 | } |
429 | 427 | ||
430 | /** | 428 | /** |
431 | * Sets a script value of the current mapping to argument. | 429 | * Sets a script value of the current mapping to argument. |
432 | * @param argument the script name. | 430 | * @param argument the script name. |
433 | * @return true if successfull. | 431 | * @return true if successfull. |
434 | */ | 432 | */ |
435 | bool Interfaces::setScript(const QString &argument){ | 433 | bool Interfaces::setScript(const QString &argument){ |
436 | return setOption(currentMapping, "script", argument); | 434 | return setOption(currentMapping, "script", argument); |
437 | } | 435 | } |
438 | 436 | ||
439 | /** | 437 | /** |
440 | * @param error true if could not retrieve the current script argument. | 438 | * @param error true if could not retrieve the current script argument. |
441 | * @return QString the argument of the script for the current mapping. | 439 | * @return QString the argument of the script for the current mapping. |
442 | */ | 440 | */ |
443 | QString Interfaces::getScript(bool &error){ | 441 | QString Interfaces::getScript(bool &error){ |
444 | return getOption(currentMapping, "script", error); | 442 | return getOption(currentMapping, "script", error); |
445 | } | 443 | } |
446 | 444 | ||
447 | 445 | ||
448 | 446 | ||
449 | /** | 447 | /** |
450 | * Helper function used to parse through the QStringList and put pointers in | 448 | * Helper function used to parse through the QStringList and put pointers in |
451 | * the correct place. | 449 | * the correct place. |
452 | * @param stanza The stanza (auto, iface, mapping) to look for. | 450 | * @param stanza The stanza (auto, iface, mapping) to look for. |
453 | * @param option string that must be in the stanza's main line. | 451 | * @param option string that must be in the stanza's main line. |
454 | * @param interator interator to place at location of stanza if successfull. | 452 | * @param interator interator to place at location of stanza if successfull. |
455 | * @return bool true if the stanza is found. | 453 | * @return bool true if the stanza is found. |
456 | */ | 454 | */ |
457 | bool Interfaces::setStanza(const QString &stanza, const QString &option, QStringList::Iterator &iterator){ | 455 | bool Interfaces::setStanza(const QString &stanza, const QString &option, QStringList::Iterator &iterator){ |
458 | bool found = false; | 456 | bool found = false; |
459 | iterator = interfaces.end(); | 457 | iterator = interfaces.end(); |
460 | for ( QStringList::Iterator it = interfaces.begin(); it != interfaces.end(); ++it ) { | 458 | for ( QStringList::Iterator it = interfaces.begin(); it != interfaces.end(); ++it ) { |
461 | QString line = (*it).simplifyWhiteSpace(); | 459 | QString line = (*it).simplifyWhiteSpace(); |
462 | if(line.contains(stanza) && line.contains(option) && line.at(0) != '#'){ | 460 | if(line.contains(stanza) && line.contains(option) && line.at(0) != '#'){ |
463 | uint point = line.find(option); | 461 | uint point = line.find(option); |
464 | bool valid = true; | 462 | bool valid = true; |
465 | if(point > 0){ | 463 | if(point > 0){ |
466 | // There are more chars in the line. check +1 | 464 | // There are more chars in the line. check +1 |
467 | if(line.at(point-1) != ' ') | 465 | if(line.at(point-1) != ' ') |
468 | valid = false; | 466 | valid = false; |
469 | } | 467 | } |
470 | point += option.length(); | 468 | point += option.length(); |
471 | if(point < line.length()-1){ | 469 | if(point < line.length()-1){ |
472 | // There are more chars in the line. check -1 | 470 | // There are more chars in the line. check -1 |
473 | if(line.at(point) != ' ') | 471 | if(line.at(point) != ' ') |
474 | valid = false; | 472 | valid = false; |
475 | } | 473 | } |
476 | if(valid){ | 474 | if(valid){ |
477 | if(found == true){ | 475 | if(found == true){ |
478 | qDebug(QString("Interfaces: Found multiple stanza's for search: %1 %2").arg(stanza).arg(option).latin1()); | 476 | qDebug(QString("Interfaces: Found multiple stanza's for search: %1 %2").arg(stanza).arg(option).latin1()); |
479 | } | 477 | } |
480 | found = true; | 478 | found = true; |
481 | iterator = it; | 479 | iterator = it; |
482 | } | 480 | } |
483 | } | 481 | } |
484 | } | 482 | } |
485 | return found; | 483 | return found; |
486 | } | 484 | } |
487 | 485 | ||
488 | /** | 486 | /** |
489 | * Sets a value of an option in a stanza | 487 | * Sets a value of an option in a stanza |
490 | * @param start the start of the stanza | 488 | * @param start the start of the stanza |
491 | * @param option the option to use when setting value. | 489 | * @param option the option to use when setting value. |
492 | * @return bool true if successfull, false otherwise. | 490 | * @return bool true if successfull, false otherwise. |
493 | */ | 491 | */ |
494 | bool Interfaces::setOption(const QStringList::Iterator &start, const QString &option, const QString &value){ | 492 | bool Interfaces::setOption(const QStringList::Iterator &start, const QString &option, const QString &value){ |
495 | if(start == interfaces.end()) | 493 | if(start == interfaces.end()) |
496 | return false; | 494 | return false; |
497 | 495 | ||
498 | bool found = false; | 496 | bool found = false; |
499 | for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) { | 497 | for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) { |
500 | if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){ | 498 | if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){ |
501 | if(!found && value != ""){ | 499 | if(!found && value != ""){ |
502 | // Got to the end of the stanza without finding it, so append it. | 500 | // 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)); | 501 | interfaces.insert(--it, QString("\t%1 %2").arg(option).arg(value)); |
504 | } | 502 | } |
505 | found = true; | 503 | found = true; |
506 | break; | 504 | break; |
507 | } | 505 | } |
508 | if((*it).contains(option) && it != start && (*it).at(0) != '#'){ | 506 | if((*it).contains(option) && it != start && (*it).at(0) != '#'){ |
509 | // Found it in stanza so replace it. | 507 | // Found it in stanza so replace it. |
510 | if(found) | 508 | if(found) |
511 | qDebug(QString("Interfaces: Set Options found more then one value for option: %1 in stanza: %1").arg(option).arg((*start)).latin1()); | 509 | qDebug(QString("Interfaces: Set Options found more then one value for option: %1 in stanza: %1").arg(option).arg((*start)).latin1()); |
512 | found = true; | 510 | found = true; |
513 | (*it) = QString("\t%1 %2").arg(option).arg(value); | 511 | (*it) = QString("\t%1 %2").arg(option).arg(value); |
514 | } | 512 | } |
515 | } | 513 | } |
516 | if(!found){ | 514 | if(!found){ |
517 | QStringList::Iterator p = start; | 515 | QStringList::Iterator p = start; |
518 | interfaces.insert(++p, QString("\t%1 %2").arg(option).arg(value)); | 516 | interfaces.insert(++p, QString("\t%1 %2").arg(option).arg(value)); |
519 | found = true; | 517 | found = true; |
520 | } | 518 | } |
521 | return found; | 519 | return found; |
522 | } | 520 | } |
523 | 521 | ||
524 | /** | 522 | /** |
525 | * Removes a stanza and all of its options | 523 | * Removes a stanza and all of its options |
526 | * @param stanza the stanza to remove | 524 | * @param stanza the stanza to remove |
527 | * @return bool true if successfull. | 525 | * @return bool true if successfull. |
528 | */ | 526 | */ |
529 | bool Interfaces::removeStanza(QStringList::Iterator &stanza){ | 527 | bool Interfaces::removeStanza(QStringList::Iterator &stanza){ |
530 | if(stanza == interfaces.end()) | 528 | if(stanza == interfaces.end()) |
531 | return false; | 529 | return false; |
532 | (*stanza) = ""; | 530 | (*stanza) = ""; |
533 | return removeAllOptions(stanza); | 531 | return removeAllOptions(stanza); |
534 | } | 532 | } |
535 | 533 | ||
536 | /** | 534 | /** |
537 | * Removes a option in a stanza | 535 | * Removes a option in a stanza |
538 | * @param start the start of the stanza | 536 | * @param start the start of the stanza |
539 | * @param option the option to remove | 537 | * @param option the option to remove |
540 | * @return bool true if successfull, false otherwise. | 538 | * @return bool true if successfull, false otherwise. |
541 | */ | 539 | */ |
542 | bool Interfaces::removeOption(const QStringList::Iterator &start, const QString &option){ | 540 | bool Interfaces::removeOption(const QStringList::Iterator &start, const QString &option){ |
543 | if(start == interfaces.end()) | 541 | if(start == interfaces.end()) |
544 | return false; | 542 | return false; |
545 | 543 | ||
546 | bool found = false; | 544 | bool found = false; |
547 | for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) { | 545 | for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) { |
548 | if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){ | 546 | if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){ |
549 | // got to the end without finding it | 547 | // got to the end without finding it |
550 | break; | 548 | break; |
551 | } | 549 | } |
552 | if((*it).contains(option) && it != start && (*it).at(0) != '#'){ | 550 | if((*it).contains(option) && it != start && (*it).at(0) != '#'){ |
553 | // Found it in stanza so replace it. | 551 | // Found it in stanza so replace it. |
554 | if(found) | 552 | if(found) |
555 | qDebug(QString("Interfaces: Set Options found more then one value for option: %1 in stanza: %1").arg(option).arg((*start)).latin1()); | 553 | qDebug(QString("Interfaces: Set Options found more then one value for option: %1 in stanza: %1").arg(option).arg((*start)).latin1()); |
556 | found = true; | 554 | found = true; |
557 | (*it) = ""; | 555 | (*it) = ""; |
558 | } | 556 | } |
559 | } | 557 | } |
560 | return found; | 558 | return found; |
561 | } | 559 | } |
562 | 560 | ||
563 | /** | 561 | /** |
564 | * Removes a option in a stanza | 562 | * Removes a option in a stanza |
565 | * @param start the start of the stanza | 563 | * @param start the start of the stanza |
566 | * @param option the option to use when setting value. | 564 | * @param option the option to use when setting value. |
567 | * @return bool true if successfull, false otherwise. | 565 | * @return bool true if successfull, false otherwise. |
568 | */ | 566 | */ |
569 | bool Interfaces::removeOption(const QStringList::Iterator &start, const QString &option, const QString &value){ | 567 | bool Interfaces::removeOption(const QStringList::Iterator &start, const QString &option, const QString &value){ |
570 | if(start == interfaces.end()) | 568 | if(start == interfaces.end()) |
571 | return false; | 569 | return false; |
572 | 570 | ||
573 | bool found = false; | 571 | bool found = false; |
574 | for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) { | 572 | for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) { |
575 | if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){ | 573 | if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){ |
576 | // got to the end without finding it | 574 | // got to the end without finding it |
577 | break; | 575 | break; |
578 | } | 576 | } |
579 | if((*it).contains(option) && (*it).contains(value) && it != start && (*it).at(0) != '#'){ | 577 | if((*it).contains(option) && (*it).contains(value) && it != start && (*it).at(0) != '#'){ |
580 | // Found it in stanza so replace it. | 578 | // Found it in stanza so replace it. |
581 | if(found) | 579 | if(found) |
582 | qDebug(QString("Interfaces: Set Options found more then one value for option: %1 in stanza: %1").arg(option).arg((*start)).latin1()); | 580 | qDebug(QString("Interfaces: Set Options found more then one value for option: %1 in stanza: %1").arg(option).arg((*start)).latin1()); |
583 | found = true; | 581 | found = true; |
584 | (*it) = ""; | 582 | (*it) = ""; |
585 | } | 583 | } |
586 | } | 584 | } |
587 | return found; | 585 | return found; |
588 | } | 586 | } |
589 | 587 | ||
590 | /** | 588 | /** |
591 | * Removes all options in a stanza | 589 | * Removes all options in a stanza |
592 | * @param start the start of the stanza | 590 | * @param start the start of the stanza |
593 | * @return bool true if successfull, false otherwise. | 591 | * @return bool true if successfull, false otherwise. |
594 | */ | 592 | */ |
595 | bool Interfaces::removeAllOptions(const QStringList::Iterator &start){ | 593 | bool Interfaces::removeAllOptions(const QStringList::Iterator &start){ |
596 | if(start == interfaces.end()) | 594 | if(start == interfaces.end()) |
597 | return false; | 595 | return false; |
598 | 596 | ||
599 | QStringList::Iterator it = start; | 597 | QStringList::Iterator it = start; |
600 | it = ++it; | 598 | it = ++it; |
601 | for (; it != interfaces.end(); ++it ) { | 599 | for (; it != interfaces.end(); ++it ) { |
602 | if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){ | 600 | if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){ |
603 | break; | 601 | break; |
604 | } | 602 | } |
605 | it = interfaces.remove(it); | 603 | it = interfaces.remove(it); |
606 | it = --it; | 604 | it = --it; |
607 | } | 605 | } |
608 | // Leave a space between this interface and the next. | 606 | // Leave a space between this interface and the next. |
609 | interfaces.insert(it, QString("")); | 607 | interfaces.insert(it, QString("")); |
610 | return true; | 608 | return true; |
611 | } | 609 | } |
612 | 610 | ||
613 | /** | 611 | /** |
614 | * Gets a value of an option in a stanza | 612 | * Gets a value of an option in a stanza |
615 | * @param start the start of the stanza | 613 | * @param start the start of the stanza |
616 | * @param option the option to use when getting the value. | 614 | * @param option the option to use when getting the value. |
617 | * @param bool true if errors false otherwise. | 615 | * @param bool true if errors false otherwise. |
618 | * @return QString the value of option QString::null() if error == true. | 616 | * @return QString the value of option QString::null() if error == true. |
619 | */ | 617 | */ |
620 | QString Interfaces::getOption(const QStringList::Iterator &start, const QString &option, bool &error){ | 618 | QString Interfaces::getOption(const QStringList::Iterator &start, const QString &option, bool &error){ |
621 | if(start == interfaces.end()){ | 619 | if(start == interfaces.end()){ |
622 | error = false; | 620 | error = false; |
623 | return QString(); | 621 | return QString(); |
624 | } | 622 | } |
625 | 623 | ||
626 | QString value; | 624 | QString value; |
627 | bool found = false; | 625 | bool found = false; |
628 | for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) { | 626 | for ( QStringList::Iterator it = start; it != interfaces.end(); ++it ) { |
629 | if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){ | 627 | if(((*it).contains(IFACE) || (*it).contains(MAPPING) || (*it).contains(AUTO)) && it != start){ |
630 | break; | 628 | break; |
631 | } | 629 | } |
632 | if((*it).contains(option) && (*it).at(0) != '#'){ | 630 | if((*it).contains(option) && (*it).at(0) != '#'){ |
633 | if(found) | 631 | if(found) |
634 | qDebug(QString("Interfaces: getOption found more then one value: %1 for option: %2 in stanza %3").arg((*it)).arg(option).arg((*start)).latin1()); | 632 | qDebug(QString("Interfaces: getOption found more then one value: %1 for option: %2 in stanza %3").arg((*it)).arg(option).arg((*start)).latin1()); |
635 | found = true; | 633 | found = true; |
636 | QString line = (*it).simplifyWhiteSpace(); | 634 | QString line = (*it).simplifyWhiteSpace(); |
637 | int space = line.find(" ", option.length()); | 635 | int space = line.find(" ", option.length()); |
638 | if(space != -1){ | 636 | if(space != -1){ |
639 | value = line.mid(space+1, line.length()); | 637 | value = line.mid(space+1, line.length()); |
640 | break; | 638 | break; |
641 | } | 639 | } |
642 | } | 640 | } |
643 | } | 641 | } |
644 | error = !found; | 642 | error = !found; |
645 | return value; | 643 | return value; |
646 | } | 644 | } |
647 | 645 | ||
648 | /** | 646 | /** |
649 | * Write out the interfaces file to the file passed into the constructor. | 647 | * Write out the interfaces file to the file passed into the constructor. |
650 | * Removes any excess blank lines over 1 line long. | 648 | * Removes any excess blank lines over 1 line long. |
651 | * @return bool true if successfull, false if not. | 649 | * @return bool true if successfull, false if not. |
652 | */ | 650 | */ |
653 | bool Interfaces::write(){ | 651 | bool Interfaces::write(){ |
654 | QFile::remove(interfacesFile); | 652 | QFile::remove(interfacesFile); |
655 | QFile file(interfacesFile); | 653 | QFile file(interfacesFile); |
656 | 654 | ||
657 | if (!file.open(IO_ReadWrite)){ | 655 | if (!file.open(IO_ReadWrite)){ |
658 | qDebug(QString("Interfaces: Can't open file: %1 for writing.").arg(interfacesFile).latin1()); | 656 | qDebug(QString("Interfaces: Can't open file: %1 for writing.").arg(interfacesFile).latin1()); |
659 | return false; | 657 | return false; |
660 | } | 658 | } |
661 | QTextStream stream( &file ); | 659 | QTextStream stream( &file ); |
662 | int whiteSpaceCount = 0; | 660 | int whiteSpaceCount = 0; |
663 | for ( QStringList::Iterator it = interfaces.begin(); it != interfaces.end(); ++it ) { | 661 | for ( QStringList::Iterator it = interfaces.begin(); it != interfaces.end(); ++it ) { |
664 | QString line = (*it).simplifyWhiteSpace(); | 662 | QString line = (*it).simplifyWhiteSpace(); |
665 | line = line.replace(QRegExp(" "),""); | 663 | line = line.replace(QRegExp(" "),""); |
666 | if(line.length() == 0) | 664 | if(line.length() == 0) |
667 | whiteSpaceCount++; | 665 | whiteSpaceCount++; |
668 | else | 666 | else |
669 | whiteSpaceCount = 0; | 667 | whiteSpaceCount = 0; |
670 | if(whiteSpaceCount < 2){ | 668 | if(whiteSpaceCount < 2){ |
671 | qDebug((*it).latin1()); | 669 | qDebug((*it).latin1()); |
672 | stream << (*it) << '\n'; | 670 | stream << (*it) << '\n'; |
673 | } | 671 | } |
674 | } | 672 | } |
675 | file.close(); | 673 | file.close(); |
676 | return true; | 674 | return true; |
677 | } | 675 | } |
678 | 676 | ||
679 | // interfaces.cpp | 677 | // interfaces.cpp |
680 | 678 | ||