summaryrefslogtreecommitdiff
path: root/noncore/net/networksetup/mainwindowimp.cpp
Unidiff
Diffstat (limited to 'noncore/net/networksetup/mainwindowimp.cpp') (more/less context) (show whitespace changes)
-rw-r--r--noncore/net/networksetup/mainwindowimp.cpp308
1 files changed, 308 insertions, 0 deletions
diff --git a/noncore/net/networksetup/mainwindowimp.cpp b/noncore/net/networksetup/mainwindowimp.cpp
new file mode 100644
index 0000000..3c13143
--- a/dev/null
+++ b/noncore/net/networksetup/mainwindowimp.cpp
@@ -0,0 +1,308 @@
1#include "mainwindowimp.h"
2#include "addserviceimp.h"
3#include "interfaceinformationimp.h"
4#include "interfacesetupimp.h"
5
6#include <qpushbutton.h>
7#include <qdir.h>
8#include <qtabwidget.h>
9#include <qmainwindow.h>
10#include <qmessagebox.h>
11#include <qlistbox.h>
12#include <qpe/config.h>
13#include <qpe/qlibrary.h>
14#include <qpe/resource.h>
15#include <qlist.h>
16
17#include <qfile.h>
18#include <qtextstream.h>
19#include <qlistview.h>
20#include <qheader.h>
21// For library loading.
22#include <dlfcn.h>
23
24#define TEMP_ALL "/tmp/ifconfig-a"
25#define TEMP_UP "/tmp/ifconfig"
26
27#define NO_SELECT_ERROR_AND_RETURN { \
28 QMessageBox::information(this, "Error","Please select an interface.", "Ok"); \
29 return; \
30}
31
32MainWindowImp::MainWindowImp(QWidget *parent, const char *name) : MainWindow(parent, name, true) {
33 connect(addServiceButton, SIGNAL(clicked()), this, SLOT(addClicked()));
34 connect(removeServiceButton, SIGNAL(clicked()), this, SLOT(removeClicked()));
35 connect(informationServiceButton, SIGNAL(clicked()), this, SLOT(informationClicked()));
36 connect(configureServiceButton, SIGNAL(clicked()), this, SLOT(configureClicked()));
37 // Make sure we have a plugin directory to scan.
38 QString DirStr = QDir::homeDirPath() + "/.networksetup/" ;
39 QDir pluginDir( DirStr );
40 pluginDir.mkdir( DirStr );
41 pluginDir.mkdir( ( DirStr + "plugins/" ) );
42 QString path = DirStr + "plugins";
43 pluginDir.setPath(path);
44 if(!pluginDir.exists()){
45 qDebug(QString("MainWindowImp: ERROR: %1 %2").arg(__FILE__).arg(__LINE__).latin1());
46 return;
47 }
48
49 // Load any saved services.
50 loadModules(path);
51 getInterfaceList();
52 serviceList->header()->hide();
53}
54
55/**
56 * Deconstructor. Unload libraries and save.
57 */
58MainWindowImp::~MainWindowImp(){
59}
60
61void MainWindowImp::loadModules(QString path){
62}
63
64/**
65 * Attempt to load a function and resolve a function.
66 * @param pluginFileName - the name of the file in which to attempt to load
67 * @param resolveString - function pointer to resolve
68 * @return pointer to the function with name resolveString or NULL
69 */
70Module* MainWindowImp::loadPlugin(QString pluginFileName, QString resolveString){
71 //qDebug(pluginFileName.latin1());
72 QLibrary *lib = new QLibrary(pluginFileName);
73 void *functionPointer = lib->resolve(resolveString);
74 if( !functionPointer ){
75 qDebug(QString("MainWindowImp: File: %1 is not a plugin, but though was.").arg(pluginFileName).latin1());
76 delete lib;
77 return NULL;
78 }
79
80 // Try to get an object.
81 Module *object = ((Module* (*)()) functionPointer)();
82 if(object == NULL){
83 qDebug("MainWindowImp: Couldn't create object, but did load library!");
84 delete lib;
85 return NULL;
86 }
87
88 // Store for reference
89 libraries.insert(object, lib);
90 return object;
91}
92
93/*
94QList<QString> MainWindowImp::retrieveUnloadedPluginList(){
95 QString DirStr = QDir::homeDirPath() + "/.networksetup/" ;
96 QString path = DirStr + "plugins";
97 QDir d(path);
98 d.setFilter( QDir::Files | QDir::Hidden );
99
100 QMap<QObject*, QLibrary*>::Iterator libraryIt;
101 QList<QString> rlist;
102 rlist.setAutoDelete(false);
103
104 const QFileInfoList *list = d.entryInfoList();
105 QFileInfoListIterator it( *list );
106 QFileInfo *fi;
107 while ( (fi=it.current()) ) {
108 if(fi->fileName().contains(".so")){
109 for( libraryIt = libraries.begin(); libraryIt != libraries.end(); ++libraryIt )
110 if((path + "/" + fi->fileName()) != (libraryIt.data())->library()){
111 QString *s = new QString(path + "/" + fi->fileName());
112 rlist.append(s);
113 }
114 }
115 ++it;
116 }
117 return rlist;
118}
119*/
120
121/**
122 * The Add button was clicked. Bring up the add dialog and if OK is hit
123 * load the plugin and append it to the list
124 */
125void MainWindowImp::addClicked(){
126 // Now that we have a list of all of the protocals, list them.
127 {
128 QMessageBox::information(this, "No Modules", "Nothing to add.", "Ok");
129 return;
130 }
131 AddServiceImp service(this, "AddService", true);
132 service.showMaximized();
133 service.exec();
134}
135
136/**
137 * Prompt the user to see if they really want to do this.
138 * If they do then remove from the list and unload.
139 */
140void MainWindowImp::removeClicked(){
141 QListViewItem *item = serviceList->currentItem();
142 if(item == NULL) NO_SELECT_ERROR_AND_RETURN
143
144 if(modules.find(interfaceItems[item]) == modules.end()){
145 QMessageBox::information(this, "Can't remove interface.", "Interface is built in.", "Ok");
146 }
147 else{
148 // Try to remove.
149 }
150
151}
152
153/**
154 * See if there is a configuration for the selected protocal.
155 * Prompt with errors.
156 */
157void MainWindowImp::configureClicked(){
158 QListViewItem *item = serviceList->currentItem();
159 if(item == NULL) NO_SELECT_ERROR_AND_RETURN
160
161 if(modules.find(interfaceItems[item]) == modules.end()){
162 InterfaceSetupImp *conf = new InterfaceSetupImp(0, "InterfaceConfiguration", interfaceItems[item]);
163 conf->showMaximized();
164 conf->show();
165 }
166 else{
167 InterfaceSetupImp *conf = new InterfaceSetupImp(this, "InterfaceConfiguration");
168 conf->show();
169 }
170}
171
172/**
173 * Pull up the information about the selected interface
174 * Report an error
175 */
176void MainWindowImp::informationClicked(){
177 QListViewItem *item = serviceList->currentItem();
178 if(item == NULL)NO_SELECT_ERROR_AND_RETURN
179
180 if(modules.find(interfaceItems[item]) == modules.end()){
181 InterfaceInformationImp *i = new InterfaceInformationImp(0, "InterfaceInformationImp", interfaceItems[item]);
182 i->showMaximized();
183 i->show();
184 }
185 else{
186 QTabWidget *t = new QTabWidget(this, "InterfaceInformationTAB");
187 InterfaceInformationImp *i = new InterfaceInformationImp(t, "TCPIPInformation", interfaceItems[item], true);
188 t->insertTab(i, "TCP/IP");
189 t->show();
190 }
191}
192
193/**
194 * Aquire the list of active interfaces from ifconfig
195 * Call ifconfig and ifconfig -a
196 */
197void MainWindowImp::getInterfaceList(){
198 KShellProcess *processAll = new KShellProcess();
199 *processAll << "/sbin/ifconfig" << "-a" << " > " TEMP_ALL;
200 connect(processAll, SIGNAL(processExited(KProcess *)),
201 this, SLOT(jobDone(KProcess *)));
202 threads.insert(processAll, TEMP_ALL);
203 processAll->start(KShellProcess::NotifyOnExit);
204
205 KShellProcess *process = new KShellProcess();
206 *process << "/sbin/ifconfig" << " > " TEMP_UP;
207 connect(process, SIGNAL(processExited(KProcess *)),
208 this, SLOT(jobDone(KProcess *)));
209 threads.insert(process, TEMP_UP);
210 process->start(KShellProcess::NotifyOnExit);
211}
212
213void MainWindowImp::jobDone(KProcess *process){
214 QString fileName = threads[process];
215 threads.remove(process);
216 delete process;
217
218 QFile file(fileName);
219 if (!file.open(IO_ReadOnly)){
220 qDebug(QString("MainWindowImp: Can't open file: %1").arg(fileName).latin1());
221 return;
222 }
223
224 QTextStream stream( &file );
225 QString line;
226 while ( !stream.eof() ) {
227 line = stream.readLine();
228 int space = line.find(" ");
229 if(space > 1){
230 // We have found an interface
231 QString interfaceName = line.mid(0, space);
232 Interface *i;
233 // See if we already have it
234 if(interfaceNames.find(interfaceName) == interfaceNames.end()){
235 if(fileName == TEMP_ALL)
236 i = new Interface(interfaceName, false);
237 else
238 i = new Interface(interfaceName, true);
239 }
240 else{
241 i = interfaceNames[interfaceName];
242 if(fileName != TEMP_ALL)
243 i->setStatus(true);
244 }
245
246 i->setAttached(true);
247 i->setInterfaceName(interfaceName);
248
249 QString hardName = "Ethernet";
250 int hardwareName = line.find("Link encap:");
251 int macAddress = line.find("HWaddr");
252 if(macAddress == -1)
253 macAddress = line.length();
254 if(hardwareName != -1)
255 i->setHardwareName(line.mid(hardwareName+11, macAddress-(hardwareName+11)));
256 // We have found an interface
257 //qDebug(QString("MainWindowImp: Found Interface: %1").arg(line).latin1());
258 interfaceNames.insert(i->getInterfaceName(), i);
259 updateInterface(i);
260 }
261 }
262 file.close();
263 QFile::remove(fileName);
264}
265
266void MainWindowImp::updateInterface(Interface *i){
267 QListViewItem *item = NULL;
268
269 // See if we already have it
270 if(items.find(i) == items.end()){
271 item = new QListViewItem(serviceList, "", "", "");
272 // See if you can't find a module owner for this interface
273 //EmployeeMap::Iterator it;
274 //for( it = map.begin(); it != map.end(); ++it )
275 // printf( "%s, %s earns %d\n", it.key().latin1(), it.data().name().latin1(), it.data().salary() );
276
277 items.insert(i, item);
278 interfaceItems.insert(item, i);
279 }
280 else
281 item = items[i];
282
283 QString statusImage = "down";
284 if(i->getStatus())
285 statusImage = "up";
286 QPixmap status = (Resource::loadPixmap(statusImage));
287 item->setPixmap(0, status);
288
289 QString typeName = "lan";
290 if(i->getHardwareName().contains("Local Loopback"))
291 typeName = "lo";
292 QPixmap type = (Resource::loadPixmap(typeName));
293 item->setPixmap(1, type);
294
295 item->setText(2, i->getHardwareName());
296
297}
298
299void MainWindowImp::addProfile(){
300
301}
302
303void MainWindowImp::removeProfile(){
304
305}
306
307// mainwindowimp.cpp
308