Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

wlanimp.cpp

Go to the documentation of this file.
00001 #include "wlanimp.h"
00002 #include "interfacesetupimp.h"
00003 
00004 #include <qfile.h>
00005 #include <qdir.h>
00006 #include <qtextstream.h>
00007 #include <qmessagebox.h>
00008 #include <qlineedit.h>
00009 #include <qlabel.h>
00010 #include <qspinbox.h>
00011 #include <qradiobutton.h>
00012 #include <qcheckbox.h>
00013 #include <qtabwidget.h>
00014 #include <qcombobox.h>
00015 
00016 #ifdef QWS
00017  #include <opie2/oprocess.h>
00018 #else
00019  #define OProcess KProcess
00020  #include <kprocess.h>
00021 #endif
00022 
00023 #define WIRELESS_OPTS "/etc/pcmcia/wireless.opts"
00024 
00028 using namespace Opie::Core;
00029 WLANImp::WLANImp( QWidget* parent, const char* name, Interface *i, bool modal, WFlags fl):WLAN(parent, name, modal, fl), currentProfile("*") {
00030   interfaceSetup = new InterfaceSetupImp(tabWidget, "InterfaceSetupImp", i);
00031   tabWidget->insertTab(interfaceSetup, "TCP/IP");
00032 
00033   // Read in the config file.
00034   QString wlanFile = WIRELESS_OPTS;
00035   QFile file(wlanFile);
00036   if (file.open(IO_ReadOnly)){
00037     QTextStream stream( &file );
00038     QString line = "";
00039     while ( !stream.eof() ) {
00040       line += stream.readLine();
00041       line += "\n";
00042     }
00043     file.close();
00044     settingsFileText = QStringList::split("\n", line, true);
00045     parseSettingFile();
00046   }
00047   else
00048     odebug << QString("WLANImp: Can't open file: %1 for reading.").arg(wlanFile).latin1() << oendl; 
00049   connect(networkType, SIGNAL(activated(int)), this, SLOT(typeChanged(int)));
00050 }
00051 
00052 void WLANImp::typeChanged(int mod){
00053   networkChannel->setEnabled(mod);
00054   channelLabel->setEnabled(mod);
00055 }
00056 
00060 void WLANImp::setProfile(const QString &profile){
00061   interfaceSetup->setProfile(profile);
00062   parseSettingFile();
00063 }
00064 
00068 void WLANImp::parseSettingFile(){
00069   bool foundCase = false;
00070   bool found = false;
00071   for ( QStringList::Iterator it = settingsFileText.begin(); it != settingsFileText.end(); ++it ) {
00072     QString line = (*it).simplifyWhiteSpace();
00073     if(line.contains("case"))
00074       foundCase = true;
00075     // See if we found our scheme to write or the sceme couldn't be found
00076     if((foundCase && line.contains("esac")) ||
00077    (foundCase && line.left(currentProfile.length()+7) == currentProfile + ",*,*,*)" && line.at(0) != '#'))
00078       found = true;
00079 
00080     if(line.contains(";;"))
00081       found = false;
00082     if(found){
00083       // write out scheme
00084       if(line.contains("ESSID=")){
00085         QString id = line.mid(line.find("ESSID=")+6, line.length());
00086         if(id == "any"){
00087           essAny->setChecked(false);
00088         }else{
00089           essAny->setChecked(true);
00090       essSpecificLineEdit->setText(id);
00091     }
00092       }
00093       if(line.contains("MODE=")){
00094         QString mode = line.mid(line.find("MODE=")+5, line.length());
00095         if(mode == "Managed"){
00096           networkType->setCurrentItem(0);
00097       channelLabel->setEnabled(false);
00098       networkChannel->setEnabled(false);
00099     }
00100     else{
00101       networkType->setCurrentItem(1);
00102           networkChannel->setEnabled(true);
00103       channelLabel->setEnabled(true);
00104     }
00105       }
00106       if(line.contains("#KEY0="))
00107         keyLineEdit0->setText(line.mid(6, line.length()));
00108       if(line.contains("#KEY1="))
00109         keyLineEdit1->setText(line.mid(6, line.length()));
00110       if(line.contains("#KEY2="))
00111         keyLineEdit2->setText(line.mid(6, line.length()));
00112       if(line.contains("#KEY3="))
00113         keyLineEdit3->setText(line.mid(6, line.length()));
00114 
00115       if(line.contains("KEY=")){
00116         wepEnabled->setChecked(true);
00117         QString key;
00118     if(line.right(5) == (" open")){
00119       key = line.mid(4, line.length()-5);
00120       authOpen->setChecked(true);
00121       authShared->setChecked(false);
00122     }
00123     else{
00124       authOpen->setChecked(false);
00125       authShared->setChecked(true);
00126       key = line.mid(4, line.length());
00127     }
00128         if(key == keyLineEdit0->text()) keyRadio0->setChecked(true);
00129     if(key == keyLineEdit1->text()) keyRadio1->setChecked(true);
00130     if(key == keyLineEdit2->text()) keyRadio2->setChecked(true);
00131     if(key == keyLineEdit3->text()) keyRadio3->setChecked(true);
00132       }
00133       if(line.contains("CHANNEL=")){
00134         networkChannel->setValue(line.mid(line.find("CHANNEL=")+8, line.length()).toInt());
00135       }
00136     }
00137   }
00138 }
00139 
00143 void WLANImp::changeAndSaveSettingFile(){
00144   QString wlanFile = WIRELESS_OPTS;
00145   QFile::remove(wlanFile);
00146   QFile file(wlanFile);
00147 
00148   if (!file.open(IO_ReadWrite)){
00149     odebug << QString("WLANImp::changeAndSaveSettingFile(): Can't open file: %1 for writing.").arg(wlanFile).latin1() << oendl; 
00150     return;
00151   }
00152 
00153   QTextStream stream( &file );
00154   bool foundCase = false;
00155   bool found = false;
00156   bool output = true;
00157   for ( QStringList::Iterator it = settingsFileText.begin(); it != settingsFileText.end(); ++it ) {
00158     QString line = (*it).simplifyWhiteSpace();
00159     if(line.contains("case"))
00160       foundCase = true;
00161     // See if we found our scheme to write or the sceme couldn't be found
00162     if((foundCase && line.contains("esac") && !found) ||
00163    (foundCase && line.left(currentProfile.length()+7) == currentProfile + ",*,*,*)" && line.at(0) != '#')){
00164       // write out scheme
00165       found = true;
00166       output = false;
00167 
00168       if(!line.contains("esac"))
00169         stream << line << "\n";
00170       if(!essAny->isChecked() == true){
00171         stream << "\tESSID=any\n";
00172     stream << "\tMODE=Managed\n";
00173       }
00174       else{
00175         stream << "\tESSID=" << essSpecificLineEdit->text() << '\n';
00176         stream << "\tMODE=" << ( networkType->currentItem() == 0  ? "Managed" : "ad-hoc") << '\n';
00177         stream << "\tCHANNEL=" << networkChannel->value() << "\n";
00178       }
00179 
00180       stream << "\t#KEY0=" << keyLineEdit0->text() << "\n";
00181       stream << "\t#KEY1=" << keyLineEdit1->text() << "\n";
00182       stream << "\t#KEY2=" << keyLineEdit2->text() << "\n";
00183       stream << "\t#KEY3=" << keyLineEdit3->text() << "\n";
00184 
00185       if(wepEnabled->isChecked()){
00186         stream << "\tKEY=\"";
00187         if(keyRadio0->isChecked()) stream << keyLineEdit0->text();
00188         if(keyRadio1->isChecked()) stream << keyLineEdit1->text();
00189         if(keyRadio2->isChecked()) stream << keyLineEdit2->text();
00190         if(keyRadio3->isChecked()) stream << keyLineEdit3->text();
00191     if(authOpen->isChecked())
00192       stream << " open";
00193     else
00194       stream << " restricted";
00195     stream << "\"\n";
00196       }
00197       stream << "\tRATE=auto\n";
00198       if(line.contains("esac"))
00199         stream << line << "\n";
00200     }
00201     if(line.contains(";;"))
00202       output = true;
00203     if(output && (*it).length() )
00204       stream << (*it) << '\n';
00205   }
00206   file.close();
00207 }
00208 
00213 void WLANImp::accept(){
00214   if(wepEnabled->isChecked()){
00215     if(keyLineEdit0->text().isEmpty() && keyLineEdit1->text().isEmpty() && keyLineEdit2->text().isEmpty() && keyLineEdit3->text().isEmpty() ){
00216       QMessageBox::information(this, "Error", "Please enter a key for WEP.", QMessageBox::Ok);
00217       return;
00218     }
00219   }
00220 
00221   if(essAny->isChecked() && essSpecificLineEdit->text().isEmpty()){
00222     QMessageBox::information(this, "Error", "Please enter a SSID.", QMessageBox::Ok);
00223     return;
00224   }
00225 
00226   // Ok settings are good here, save
00227   changeAndSaveSettingFile();
00228 
00229   // Try to save the interfaces settings.
00230   if(!interfaceSetup->saveChanges())
00231     return;
00232 
00233   OProcess insert;
00234   insert << "sh";
00235   insert << "-c";
00236   insert << "cardctl eject && cardctl insert";
00237 
00238   if (!insert.start(OProcess::DontCare, OProcess::NoCommunication) ) {
00239     owarn << "could not start cardctl" << oendl; 
00240   }
00241 
00242   // Close out the dialog
00243   QDialog::accept();
00244 }
00245 

Generated on Sat Nov 5 16:17:51 2005 for OPIE by  doxygen 1.4.2