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