Diffstat (limited to 'noncore/settings/networksettings/mainwindowimp.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/settings/networksettings/mainwindowimp.cpp | 196 |
1 files changed, 92 insertions, 104 deletions
diff --git a/noncore/settings/networksettings/mainwindowimp.cpp b/noncore/settings/networksettings/mainwindowimp.cpp index 7261c58..48ef9b3 100644 --- a/noncore/settings/networksettings/mainwindowimp.cpp +++ b/noncore/settings/networksettings/mainwindowimp.cpp | |||
@@ -5,7 +5,4 @@ | |||
5 | #include "interfaces.h" | 5 | #include "interfaces.h" |
6 | |||
7 | #include "module.h" | 6 | #include "module.h" |
8 | 7 | ||
9 | #include "kprocess.h" | ||
10 | |||
11 | #include <qpushbutton.h> | 8 | #include <qpushbutton.h> |
@@ -30,4 +27,4 @@ | |||
30 | 27 | ||
31 | #define TEMP_ALL "/tmp/ifconfig-a" | 28 | #include <net/if.h> |
32 | #define TEMP_UP "/tmp/ifconfig" | 29 | #include <sys/ioctl.h> |
33 | 30 | ||
@@ -48,5 +45,27 @@ MainWindowImp::MainWindowImp(QWidget *parent, const char *name) : MainWindow(par | |||
48 | loadModules(QPEApplication::qpeDir() + "/plugins/networksetup"); | 45 | loadModules(QPEApplication::qpeDir() + "/plugins/networksetup"); |
49 | getInterfaceList(); | 46 | getAllInterfaces(); |
50 | connectionList->header()->hide(); | ||
51 | 47 | ||
48 | Interfaces i; | ||
49 | QStringList list = i.getInterfaceList(); | ||
50 | QMap<QString, Interface*>::Iterator it; | ||
51 | for ( QStringList::Iterator ni = list.begin(); ni != list.end(); ++ni ) { | ||
52 | bool found = false; | ||
53 | for( it = interfaceNames.begin(); it != interfaceNames.end(); ++it ){ | ||
54 | if(it.key() == (*ni)) | ||
55 | found = true; | ||
56 | } | ||
57 | if(!found){ | ||
58 | if(!(*ni).contains("_")){ | ||
59 | Interface *i = new Interface(this, *ni, false); | ||
60 | i->setAttached(false); | ||
61 | i->setHardwareName("Disconnected"); | ||
62 | interfaceNames.insert(i->getInterfaceName(), i); | ||
63 | updateInterface(i); | ||
64 | connect(i, SIGNAL(updateInterface(Interface *)), this, SLOT(updateInterface(Interface *))); | ||
65 | } | ||
66 | } | ||
67 | } | ||
68 | |||
69 | //getInterfaceList(); | ||
70 | connectionList->header()->hide(); | ||
52 | 71 | ||
@@ -102,2 +121,68 @@ MainWindowImp::~MainWindowImp(){ | |||
102 | /** | 121 | /** |
122 | * Query the kernel for all of the interfaces. | ||
123 | */ | ||
124 | void MainWindowImp::getAllInterfaces(){ | ||
125 | int sockfd = socket(AF_INET, SOCK_DGRAM, 0); | ||
126 | if(sockfd == -1) | ||
127 | return; | ||
128 | |||
129 | char buf[8*1024]; | ||
130 | struct ifconf ifc; | ||
131 | ifc.ifc_len = sizeof(buf); | ||
132 | ifc.ifc_req = (struct ifreq *) buf; | ||
133 | int result=ioctl(sockfd, SIOCGIFCONF, &ifc); | ||
134 | |||
135 | for (char* ptr = buf; ptr < buf + ifc.ifc_len; ){ | ||
136 | struct ifreq *ifr =(struct ifreq *) ptr; | ||
137 | int len = sizeof(struct sockaddr); | ||
138 | #ifdef HAVE_SOCKADDR_SA_LEN | ||
139 | if (ifr->ifr_addr.sa_len > len) | ||
140 | len = ifr->ifr_addr.sa_len; /* length > 16 */ | ||
141 | #endif | ||
142 | ptr += sizeof(ifr->ifr_name) + len; /* for next one in buffer */ | ||
143 | |||
144 | int flags; | ||
145 | struct sockaddr_in *sinptr; | ||
146 | Interface *i = NULL; | ||
147 | switch (ifr->ifr_addr.sa_family){ | ||
148 | case AF_INET: | ||
149 | sinptr = (struct sockaddr_in *) &ifr->ifr_addr; | ||
150 | flags=0; | ||
151 | |||
152 | struct ifreq ifcopy; | ||
153 | ifcopy=*ifr; | ||
154 | result=ioctl(sockfd,SIOCGIFFLAGS,&ifcopy); | ||
155 | flags=ifcopy.ifr_flags; | ||
156 | i = new Interface(this, ifr->ifr_name, false); | ||
157 | i->setAttached(true); | ||
158 | if ((flags & IFF_UP) == IFF_UP) | ||
159 | i->setStatus(true); | ||
160 | else | ||
161 | i->setStatus(false); | ||
162 | |||
163 | if ((flags & IFF_BROADCAST) == IFF_BROADCAST) | ||
164 | i->setHardwareName("Ethernet"); | ||
165 | else if ((flags & IFF_POINTOPOINT) == IFF_POINTOPOINT) | ||
166 | i->setHardwareName("Point to Point"); | ||
167 | else if ((flags & IFF_MULTICAST) == IFF_MULTICAST) | ||
168 | i->setHardwareName("Multicast"); | ||
169 | else if ((flags & IFF_LOOPBACK) == IFF_LOOPBACK) | ||
170 | i->setHardwareName("Loopback"); | ||
171 | else | ||
172 | i->setHardwareName("Unknown"); | ||
173 | |||
174 | interfaceNames.insert(i->getInterfaceName(), i); | ||
175 | updateInterface(i); | ||
176 | connect(i, SIGNAL(updateInterface(Interface *)), this, SLOT(updateInterface(Interface *))); | ||
177 | break; | ||
178 | |||
179 | default: | ||
180 | qDebug(ifr->ifr_name); | ||
181 | qDebug(QString("%1").arg(ifr->ifr_addr.sa_family).latin1()); | ||
182 | break; | ||
183 | } | ||
184 | } | ||
185 | } | ||
186 | |||
187 | /** | ||
103 | * Load all modules that are found in the path | 188 | * Load all modules that are found in the path |
@@ -283,99 +368,2 @@ void MainWindowImp::informationClicked(){ | |||
283 | /** | 368 | /** |
284 | * Aquire the list of active interfaces from ifconfig | ||
285 | * Call ifconfig and ifconfig -a | ||
286 | */ | ||
287 | void MainWindowImp::getInterfaceList(){ | ||
288 | KShellProcess *processAll = new KShellProcess(); | ||
289 | *processAll << "/sbin/ifconfig" << "-a" << " > " TEMP_ALL; | ||
290 | connect(processAll, SIGNAL(processExited(KProcess *)), | ||
291 | this, SLOT(jobDone(KProcess *))); | ||
292 | threads.insert(processAll, TEMP_ALL); | ||
293 | |||
294 | KShellProcess *process = new KShellProcess(); | ||
295 | *process << "/sbin/ifconfig" << " > " TEMP_UP; | ||
296 | connect(process, SIGNAL(processExited(KProcess *)), | ||
297 | this, SLOT(jobDone(KProcess *))); | ||
298 | threads.insert(process, TEMP_UP); | ||
299 | |||
300 | processAll->start(KShellProcess::NotifyOnExit); | ||
301 | process->start(KShellProcess::NotifyOnExit); | ||
302 | } | ||
303 | |||
304 | void MainWindowImp::jobDone(KProcess *process){ | ||
305 | QString fileName = threads[process]; | ||
306 | threads.remove(process); | ||
307 | delete process; | ||
308 | |||
309 | QFile file(fileName); | ||
310 | if (!file.open(IO_ReadOnly)){ | ||
311 | qDebug(QString("MainWindowImp: Can't open file: %1").arg(fileName).latin1()); | ||
312 | return; | ||
313 | } | ||
314 | |||
315 | QTextStream stream( &file ); | ||
316 | QString line; | ||
317 | while ( !stream.eof() ) { | ||
318 | line = stream.readLine(); | ||
319 | int space = line.find(" "); | ||
320 | if(space > 1){ | ||
321 | // We have found an interface | ||
322 | QString interfaceName = line.mid(0, space); | ||
323 | Interface *i; | ||
324 | // We have found an interface | ||
325 | //qDebug(QString("MainWindowImp: Found Interface: %1").arg(line).latin1()); | ||
326 | // See if we already have it | ||
327 | if(interfaceNames.find(interfaceName) == interfaceNames.end()){ | ||
328 | if(fileName == TEMP_ALL) | ||
329 | i = new Interface(this, interfaceName, false); | ||
330 | else | ||
331 | i = new Interface(this, interfaceName, true); | ||
332 | i->setAttached(true); | ||
333 | |||
334 | QString hardName = "Ethernet"; | ||
335 | int hardwareName = line.find("Link encap:"); | ||
336 | int macAddress = line.find("HWaddr"); | ||
337 | if(macAddress == -1) | ||
338 | macAddress = line.length(); | ||
339 | if(hardwareName != -1) | ||
340 | i->setHardwareName(line.mid(hardwareName+11, macAddress-(hardwareName+11)) ); | ||
341 | |||
342 | interfaceNames.insert(i->getInterfaceName(), i); | ||
343 | updateInterface(i); | ||
344 | connect(i, SIGNAL(updateInterface(Interface *)), this, SLOT(updateInterface(Interface *))); | ||
345 | } | ||
346 | // It was an interface we already had. | ||
347 | else{ | ||
348 | if(fileName != TEMP_ALL) | ||
349 | (interfaceNames[interfaceName])->setStatus(true); | ||
350 | } | ||
351 | } | ||
352 | } | ||
353 | file.close(); | ||
354 | QFile::remove(fileName); | ||
355 | |||
356 | if(threads.count() == 0){ | ||
357 | Interfaces i; | ||
358 | QStringList list = i.getInterfaceList(); | ||
359 | QMap<QString, Interface*>::Iterator it; | ||
360 | for ( QStringList::Iterator ni = list.begin(); ni != list.end(); ++ni ) { | ||
361 | bool found = false; | ||
362 | for( it = interfaceNames.begin(); it != interfaceNames.end(); ++it ){ | ||
363 | if(it.key() == (*ni)) | ||
364 | found = true; | ||
365 | } | ||
366 | if(!found){ | ||
367 | if(!(*ni).contains("_")){ | ||
368 | Interface *i = new Interface(this, *ni, false); | ||
369 | i->setAttached(false); | ||
370 | i->setHardwareName("Disconnected"); | ||
371 | interfaceNames.insert(i->getInterfaceName(), i); | ||
372 | updateInterface(i); | ||
373 | connect(i, SIGNAL(updateInterface(Interface *)), this, SLOT(updateInterface(Interface *))); | ||
374 | } | ||
375 | } | ||
376 | } | ||
377 | } | ||
378 | } | ||
379 | |||
380 | /** | ||
381 | * Update this interface. If no QListViewItem exists create one. | 369 | * Update this interface. If no QListViewItem exists create one. |