summaryrefslogtreecommitdiff
path: root/noncore/settings/networksettings/wlan/wlanimp2.cpp
blob: e7b842bfe35fb1189195265147f6a5519b6d95c3 (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
#include "wlanimp2.h"
#include "keyedit.h"
#include "interfacesetupimp.h"

#include <qfile.h>
#include <qdir.h>
#include <qtextstream.h>
#include <qmessagebox.h>
#include <qlineedit.h>
#include <qlabel.h>
#include <qspinbox.h>
#include <qradiobutton.h>
#include <qcheckbox.h>
#include <qtabwidget.h>
#include <qcombobox.h>

#ifdef QWS 
 #include <opie/oprocess.h>
#else
 #define OProcess KProcess
 #include <kprocess.h>
#endif

#define WIRELESS_OPTS "/etc/pcmcia/wireless.opts"
#define PREUP "/etc/netwrok/if-pre-up.d/wireless-tools"

/**
 * Constructor, read in the wireless.opts file for parsing later.
 */ 
WLANImp::WLANImp( QWidget* parent, const char* name, Interface *i, bool modal, WFlags fl) : WLAN(parent, name, modal, fl), currentProfile("*"), interface(i) {
  interfaces = new Interfaces;
  interfaceSetup = new InterfaceSetupImp(tabWidget, "InterfaceSetupImp", i, interfaces);
  tabWidget->insertTab(interfaceSetup, "TCP/IP");

  // Check sanity - the existance of the wireless-tools if-pre-up script
  QFile file(QString(PREUP));
  if (file.exists()) {
    qWarning(QString("WLANImp: Unable to open /etc/network/if-pre-up.d/wireless-tools"));
  }
}

WLANImp::~WLANImp() {
  delete interfaces;
}

/**
 * Change the profile for both wireless settings and network settings.
 */ 
void WLANImp::setProfile(const QString &profile){
  interfaceSetup->setProfile(profile);
  parseOpts();
}

void WLANImp::parseOpts() {
  bool error;
  QString opt;

  if (! interfaces->isInterfaceSet())
    return;

  
  opt = interfaces->getInterfaceOption("wireless_essid", error);
  if(opt == "any" || opt == "off" || opt.isNull()){
    essid->setEditText("any");
  } else {
    essid->setEditText(opt);
  }

  opt = interfaces->getInterfaceOption("wireless_mode", error).simplifyWhiteSpace();
  if (opt == "Auto") {
    mode->setCurrentItem(0);
  } else if (opt == "Ad-Hoc") {
    mode->setCurrentItem(2);
  } else {
    // Managed/Infrastructure mode
    mode->setCurrentItem(1);
  }
  
  opt = interfaces->getInterfaceOption("wireless_ap", error).simplifyWhiteSpace();
  if (! opt.isNull()) {
    specifyAp->setChecked(true);
    macEdit->setText(opt);
  }

  opt = interfaces->getInterfaceOption("wireless_channel", error).simplifyWhiteSpace();
  if (! opt.isNull()) {
    specifyChan->setChecked(true);
    networkChannel->setValue(opt.toInt());
  }

  opt = interfaces->getInterfaceOption("wireless_key", error).simplifyWhiteSpace();
  if (opt.isNull())
    opt = interfaces->getInterfaceOption("wireless_enc", error).simplifyWhiteSpace();
  parseKeyStr(opt);
}

void WLANImp::parseKeyStr(QString keystr) {
  int loc = 0;
  int index = 1;
  QString key;
  QStringList keys = QStringList::split(QRegExp("\\s+"), keystr);
  int enc = -1; // encryption state

  for (QStringList::Iterator it = keys.begin(); it != keys.end(); ++it) {
    if ((*it).left(3) == "off") {
      // encryption disabled
      enc = 0;
    } else if ((*it).left(2) == "on") {
      // encryption enabled
      enc = 1;
    } else if ((*it).left(4) == "open") {
      // open mode, accept non encrypted packets
      acceptNonEnc->setChecked(true);
    } else if ((*it).left(10) == "restricted") {
      // restricted mode, only accept encrypted packets
      rejectNonEnc->setChecked(true);
    } else if ((*it).left(3) == "key") {
      // new set of options
    } else if ((*it).left(1) == "[") {
      index = (*it).mid(1, 1).toInt();
      // switch current key to index
      switch (index) {
        case 1:
          keyRadio0->setChecked(true);
	  break;
        case 2:
          keyRadio1->setChecked(true);
	  break;
        case 3:
          keyRadio2->setChecked(true);
	  break;
        case 4:
          keyRadio3->setChecked(true);
	  break;
      }   
    } else {
      // key
      key = (*it);
    }
    if (! key.isNull()) {
      if (enc == -1)
        enc = 1;
      QStringList::Iterator next = ++it;   
      if (it == keys.end()) {
        break;
      }
      if ((*(next)).left(1) == "[") {
        // set key at index
        index = (*(next)).mid(1, 1).toInt();
      } else {
        index = 1;
      }
      switch (index) {
          case 1:
            keyLineEdit0->setText(key);
	    break;
          case 2:
            keyLineEdit1->setText(key);
	    break;
          case 3:
            keyLineEdit2->setText(key);
	    break;
          case 4:
            keyLineEdit3->setText(key);
	    break;
      }
      key = QString::null;
    }
  }
  if (enc == 1) {
    wepEnabled->setChecked(true);
  } else {
    wepEnabled->setChecked(false);
  }
}

/**
 * Check to see if the current config is valid
 * Save interfaces
 */
void WLANImp::accept() {
  if (wepEnabled->isChecked()) {
    if ((keyRadio0->isChecked() && keyLineEdit0->text().isEmpty()) ||
        (keyRadio1->isChecked() && keyLineEdit1->text().isEmpty()) ||
        (keyRadio2->isChecked() && keyLineEdit2->text().isEmpty()) ||
        (keyRadio3->isChecked() && keyLineEdit3->text().isEmpty())) {
      QMessageBox::information(this, "Error", "Please enter a WEP key.", QMessageBox::Ok);
      return;
    }
  }	
  
  if (essid->currentText().isEmpty()) {
    QMessageBox::information(this, "Error", "Please select/enter an ESSID.", QMessageBox::Ok);
    return;
  }

  if (specifyAp->isChecked() && macEdit->text().isEmpty()) {
    QMessageBox::information(this, "Error", "Please enter the MAC address of the Access Point.", QMessageBox::Ok);
    return;
  }
  
  // Try to save the interfaces settings.
  writeOpts();

  // Close out the dialog
  QDialog::accept();
}

void WLANImp::writeOpts() {
  bool error = false;

  interfaces->setInterfaceOption(QString("wireless_mode"), mode->currentText());
  interfaces->setInterfaceOption(QString("wireless_essid"), essid->currentText());

  if (specifyAp->isChecked()) {
    interfaces->setInterfaceOption(QString("wireless_ap"), macEdit->text());
  } else {
    interfaces->removeInterfaceOption(QString("wireless_ap"));
  }

  if (specifyChan->isChecked()) {
    interfaces->setInterfaceOption(QString("wireless_channel"), networkChannel->text());
  } else {
    interfaces->removeInterfaceOption(QString("wireless_channel"));
  }

  if (wepEnabled->isChecked()) {
    QStringList keyList;

    if (! keyLineEdit0->text().isNull()) {
      keyList += keyLineEdit0->text();
      keyList += "[1]";
    } else if (! keyLineEdit1->text().isNull()) {
      keyList += keyLineEdit1->text();
      keyList += "[2]";
    } else if (! keyLineEdit2->text().isNull()) {
      keyList += keyLineEdit2->text();
      keyList += "[3]";
    } else if (! keyLineEdit3->text().isNull()) {
      keyList += keyLineEdit3->text();
      keyList += "[4]";
    }
    if (acceptNonEnc->isChecked()) {
      keyList += "open";
    } else {
      keyList += "restricted";
    }

    keyList += "key";
    if (keyRadio0->isChecked()) {
      keyList += "[1]";
    } else if (keyRadio1->isChecked()) {
      keyList += "[2]";
    } else if (keyRadio2->isChecked()) {
      keyList += "[3]";
    } else if (keyRadio3->isChecked()) {
      keyList += "[4]";
    }
    interfaces->setInterfaceOption(QString("wireless_key"), keyList.join(QString(" ")));
  } else {
    interfaces->removeInterfaceOption(QString("wireless_key"));
  }
  interfaces->removeInterfaceOption(QString("wireless_enc"));

  if(!interfaceSetup->saveChanges())
    return;

  QDialog::accept();
}