-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,273 +1,271 @@ | |||
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. |