summaryrefslogtreecommitdiff
path: root/noncore/net/networksetup/mainwindowimp.cpp
blob: ad9362f04908b4d42abd1c3a1f103b1c8cc8ba8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include "mainwindowimp.h"
#include "addserviceimp.h"
#include "interfaceinformationimp.h"
#include "interfacesetupimp.h"
#include "interface.h"
#include "kprocess.h"
#include "module.h"

#include <qpushbutton.h>
#include <qdir.h>
#include <qtabwidget.h>
#include <qmainwindow.h>
#include <qmessagebox.h>
#include <qlistbox.h>
#include <qpe/config.h>
#include <qpe/qlibrary.h>
#include <qpe/resource.h>
#include <qlist.h>

#include <qfile.h>
#include <qtextstream.h>
#include <qlistview.h>
#include <qheader.h>
// For library loading.
#include <dlfcn.h>

#define TEMP_ALL "/tmp/ifconfig-a"
#define TEMP_UP "/tmp/ifconfig"

#define NO_SELECT_ERROR_AND_RETURN { \
  QMessageBox::information(this, "Error","Please select an interface.", "Ok"); \
  return; \
}

MainWindowImp::MainWindowImp(QWidget *parent, const char *name) : MainWindow(parent, name, true) {
  connect(addServiceButton, SIGNAL(clicked()), this, SLOT(addClicked()));
  connect(removeServiceButton, SIGNAL(clicked()), this, SLOT(removeClicked()));
  connect(informationServiceButton, SIGNAL(clicked()), this, SLOT(informationClicked()));
  connect(configureServiceButton, SIGNAL(clicked()), this, SLOT(configureClicked())); 
  // Make sure we have a plugin directory to scan.
  QString DirStr = QDir::homeDirPath() + "/.networksetup/" ;
  QDir pluginDir( DirStr );
  pluginDir.mkdir( DirStr );
  pluginDir.mkdir( ( DirStr + "plugins/" ) );
  QString path = DirStr + "plugins";
  pluginDir.setPath(path);
  if(!pluginDir.exists()){
    qDebug(QString("MainWindowImp: ERROR: %1 %2").arg(__FILE__).arg(__LINE__).latin1());
    return;
  }

  // Load any saved services.
  loadModules(path);
  getInterfaceList();
  serviceList->header()->hide();


  Config cfg("NetworkSetup");
  profiles = QStringList::split(" ", cfg.readEntry("Profiles", "All"));
}

/**
 * Deconstructor.  Unload libraries and save profile list.
 */
MainWindowImp::~MainWindowImp(){
  if(profiles.count() > 1){
    Config cfg("NetworkSetup");
    cfg.writeEntry("Profiles", profiles.join(" "));
  }
}

void MainWindowImp::loadModules(QString path){
}

/**
 * Attempt to load a function and resolve a function.
 * @param pluginFileName - the name of the file in which to attempt to load
 * @param resolveString - function pointer to resolve 
 * @return pointer to the function with name resolveString or NULL
 */ 
Module* MainWindowImp::loadPlugin(QString pluginFileName, QString resolveString){
  //qDebug(pluginFileName.latin1());
  QLibrary *lib = new QLibrary(pluginFileName);
  void *functionPointer = lib->resolve(resolveString);
  if( !functionPointer ){
    qDebug(QString("MainWindowImp: File: %1 is not a plugin, but though was.").arg(pluginFileName).latin1());
    delete lib;
    return NULL;
  }
  
  // Try to get an object.
  Module *object = ((Module* (*)()) functionPointer)();
  if(object == NULL){
    qDebug("MainWindowImp: Couldn't create object, but did load library!");
    delete lib;
    return NULL;
  }

  // Store for reference
  libraries.insert(object, lib);
  return object;
}

/*
QList<QString> MainWindowImp::retrieveUnloadedPluginList(){
  QString DirStr = QDir::homeDirPath() + "/.networksetup/" ;
  QString path = DirStr + "plugins";
  QDir d(path);
  d.setFilter( QDir::Files | QDir::Hidden );

  QMap<QObject*, QLibrary*>::Iterator libraryIt;
  QList<QString> rlist; 
  rlist.setAutoDelete(false);
	  
  const QFileInfoList *list = d.entryInfoList();
  QFileInfoListIterator it( *list );
  QFileInfo *fi;
  while ( (fi=it.current()) ) {
    if(fi->fileName().contains(".so")){
      for( libraryIt = libraries.begin(); libraryIt != libraries.end(); ++libraryIt )
        if((path + "/" + fi->fileName()) != (libraryIt.data())->library()){
          QString *s = new QString(path + "/" + fi->fileName());
          rlist.append(s);
	}
    }
    ++it;
  }
  return rlist;
}
*/

/**
 * The Add button was clicked.  Bring up the add dialog and if OK is hit
 * load the plugin and append it to the list
 */ 
void MainWindowImp::addClicked(){
  // Now that we have a list of all of the protocals, list them.
  {
    QMessageBox::information(this, "No Modules", "Nothing to add.", "Ok");
    return;
  }
  AddServiceImp service(this, "AddService", true);
  service.showMaximized();
  service.exec();
}

/**
 * Prompt the user to see if they really want to do this.
 * If they do then remove from the list and unload.
 */ 
void MainWindowImp::removeClicked(){
  QListViewItem *item = serviceList->currentItem();
  if(item == NULL) NO_SELECT_ERROR_AND_RETURN
  
  if(modules.find(interfaceItems[item]) == modules.end()){
    QMessageBox::information(this, "Can't remove interface.", "Interface is built in.", "Ok");
  }
  else{
    // Try to remove. 
  }

}

/**
 * See if there is a configuration for the selected protocal.
 * Prompt with errors.
 */ 
void MainWindowImp::configureClicked(){
  QListViewItem *item = serviceList->currentItem();
  if(item == NULL) NO_SELECT_ERROR_AND_RETURN
  
  if(modules.find(interfaceItems[item]) == modules.end()){
    InterfaceSetupImp *conf = new InterfaceSetupImp(0, "InterfaceConfiguration", interfaceItems[item]);
    conf->showMaximized();
    conf->show();
  }
  else{
    InterfaceSetupImp *conf = new InterfaceSetupImp(this, "InterfaceConfiguration");
    conf->show();
  }
}

/**
 * Pull up the information about the selected interface
 * Report an error
 */ 
void MainWindowImp::informationClicked(){
  QListViewItem *item = serviceList->currentItem();
  if(item == NULL)NO_SELECT_ERROR_AND_RETURN
	
  if(modules.find(interfaceItems[item]) == modules.end()){
    InterfaceInformationImp *i = new InterfaceInformationImp(0, "InterfaceInformationImp", interfaceItems[item]);
    i->showMaximized();
    i->show();
  }
  else{
    QTabWidget *t = new QTabWidget(this, "InterfaceInformationTAB");
    InterfaceInformationImp *i = new InterfaceInformationImp(t, "TCPIPInformation", interfaceItems[item], true);
    t->insertTab(i, "TCP/IP");
    t->show();
  }
}

/**
 * Aquire the list of active interfaces from ifconfig
 * Call ifconfig and ifconfig -a
 */ 
void MainWindowImp::getInterfaceList(){
  KShellProcess *processAll = new KShellProcess();
  *processAll << "/sbin/ifconfig" << "-a" << " > " TEMP_ALL;
  connect(processAll, SIGNAL(processExited(KProcess *)),
          this, SLOT(jobDone(KProcess *)));
  threads.insert(processAll, TEMP_ALL);
  processAll->start(KShellProcess::NotifyOnExit);

  KShellProcess *process = new KShellProcess();
  *process << "/sbin/ifconfig" << " > " TEMP_UP;
  connect(process, SIGNAL(processExited(KProcess *)),
          this, SLOT(jobDone(KProcess *)));
  threads.insert(process, TEMP_UP);
  process->start(KShellProcess::NotifyOnExit);
}

void MainWindowImp::jobDone(KProcess *process){
  QString fileName = threads[process];
  threads.remove(process);
  delete process;

  QFile file(fileName);
  if (!file.open(IO_ReadOnly)){
    qDebug(QString("MainWindowImp: Can't open file: %1").arg(fileName).latin1());
    return;
  }
  
  QTextStream stream( &file );
  QString line;
  while ( !stream.eof() ) {
    line = stream.readLine();
    int space = line.find(" ");
    if(space > 1){
      // We have found an interface
      QString interfaceName = line.mid(0, space);
      Interface *i;
      // See if we already have it
      if(interfaceNames.find(interfaceName) == interfaceNames.end()){
        if(fileName == TEMP_ALL)
          i = new Interface(interfaceName, false);
        else
          i = new Interface(interfaceName, true);
      }
      else{
        i = interfaceNames[interfaceName];
        if(fileName != TEMP_ALL)
          i->setStatus(true);
      }
      
      i->setAttached(true);
      i->setInterfaceName(interfaceName);

      QString hardName = "Ethernet";
      int hardwareName = line.find("Link encap:");
      int macAddress = line.find("HWaddr");
      if(macAddress == -1)
        macAddress = line.length();
      if(hardwareName != -1)
        i->setHardwareName(line.mid(hardwareName+11, macAddress-(hardwareName+11)) + QString(" (%1)").arg(i->getInterfaceName()));
      // We have found an interface
      //qDebug(QString("MainWindowImp: Found Interface: %1").arg(line).latin1());
      interfaceNames.insert(i->getInterfaceName(), i);
      updateInterface(i);
    }
  }
  file.close();
  QFile::remove(fileName);
} 

void MainWindowImp::updateInterface(Interface *i){
  QListViewItem *item = NULL;
  
  // See if we already have it
  if(items.find(i) == items.end()){
    item = new QListViewItem(serviceList, "", "", "");
    // See if you can't find a module owner for this interface
    //EmployeeMap::Iterator it;
    //for( it = map.begin(); it != map.end(); ++it )
    //        printf( "%s, %s earns %d\n", it.key().latin1(), it.data().name().latin1(), it.data().salary() );

    items.insert(i, item);
    interfaceItems.insert(item, i);
  }
  else
    item = items[i];
  
  QString statusImage = "down";
  if(i->getStatus())
    statusImage = "up";
  QPixmap status = (Resource::loadPixmap(statusImage));
  item->setPixmap(0, status);
 
  QString typeName = "lan";
  if(i->getHardwareName().contains("Local Loopback"))
    typeName = "lo";
  QPixmap type = (Resource::loadPixmap(typeName));
  item->setPixmap(1, type);

  item->setText(2, i->getHardwareName());
  
}

void MainWindowImp::addProfile(){

}

void MainWindowImp::removeProfile(){

}

// mainwindowimp.cpp