summaryrefslogtreecommitdiff
path: root/noncore/settings/networksettings/wlan/wlanimp.cpp
blob: 01d7e366143e810ac2efedb69c75247316d8ec9e (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
#include "wlanimp.h"

/* Config class */
#include <qpe/config.h>
/* Global namespace */
#include <qpe/global.h>
/* system() */
#include <stdlib.h>
#include <qfile.h>
#include <qdir.h>
#include <qtextstream.h>
#include <qmessagebox.h>
#include <qlineedit.h>
#include <qspinbox.h>
#include <qradiobutton.h>
#include <qcheckbox.h>
#include <qregexp.h>
#include <qpe/config.h>
#include <qtabwidget.h>
#include "interfacesetupimp.h"

WLANImp::WLANImp( QWidget* parent, const char* name, Interface *i, bool modal, WFlags fl):WLAN(parent, name, modal, fl){
  config = new Config("wireless");
  interfaceSetup = new InterfaceSetupImp(tabWidget, "InterfaceSetupImp", i);//, Qt::WDestructiveClose);
  //configure->setProfile(currentProfile);
  tabWidget->insertTab(interfaceSetup, "TCP/IP");

  readConfig();

}

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

void WLANImp::setProfile(QString &profile){
  interfaceSetup->setProfile(profile);
}

void WLANImp::readConfig()
{
    qWarning( "WLANImp::readConfig() called." );
    config->setGroup( "Properties" );
    QString ssid = config->readEntry( "SSID", "any" );
    if( ssid == "any" || ssid == "ANY" ){
        essNon->setChecked( true );
    } else {
        essSpecific->setChecked( true );
        essSpecificLineEdit->setText( ssid );
    }
    QString mode = config->readEntry( "Mode", "Managed" );
    if( mode == "adhoc" ) {
        network802->setChecked( true );
    } else {
        networkInfrastructure->setChecked( true );
    }
    networkChannel->setValue( config->readNumEntry( "CHANNEL", 1 ) );
//    config->readEntry( "RATE", "auto" );
    config->readEntry( "dot11PrivacyInvoked" ) == "true" ? wepEnabled->setChecked( true ) : wepEnabled->setChecked( false );
    config->readEntry( "AuthType", "opensystem" );
    config->readEntry( "PRIV_KEY128", "false" ) == "false" ? key40->setChecked( true ) : key128->setChecked( true );
    int defaultkey = config->readNumEntry( "dot11WEPDefaultKeyID", 0 );
    switch( defaultkey ){
    case 0:
        keyRadio0->setChecked( true );
	break;
    case 1:
        keyRadio1->setChecked( true );
	break;
    case 2:
        keyRadio2->setChecked( true );
	break;
    case 3:
        keyRadio3->setChecked( true );
	break;
    }
    keyLineEdit0->setText(config->readEntry( "dot11WEPDefaultKey0" ));
    keyLineEdit1->setText(config->readEntry( "dot11WEPDefaultKey1" ));
    keyLineEdit2->setText(config->readEntry( "dot11WEPDefaultKey2" ));
    keyLineEdit3->setText(config->readEntry( "dot11WEPDefaultKey3" ));
    return;
}

bool WLANImp::writeConfig()
{
    qWarning( "WLANImp::writeConfig() called." );
    config->setGroup( "Properties" );
    if( essNon->isChecked() ) {
        config->writeEntry( "SSID", "any" );
    } else {
        config->writeEntry( "SSID", essSpecificLineEdit->text() );
    }
    if( networkInfrastructure->isChecked() ){
        config->writeEntry( "Mode", "Managed" );
    } else if( network802->isChecked() ){
        config->writeEntry( "Mode", "adhoc" );
    }
    config->writeEntry( "CHANNEL", networkChannel->value() );
//    config->readEntry( "RATE", "auto" );
    wepEnabled->isChecked() ? config->writeEntry( "dot11PrivacyInvoked", "true" ) : config->writeEntry( "dot11PrivacyInvoked", "false" );
    authOpen->isChecked() ? config->writeEntry( "AuthType", "opensystem" ) : config->writeEntry( "AuthType", "sharedkey" );
    key40->isChecked() ? config->writeEntry( "PRIV_KEY128", "false" ) : config->writeEntry( "PRIV_KEY128", "true" );
    if( keyRadio0->isChecked() ){
        config->writeEntry( "dot11WEPDefaultKeyID", 0 );
    } else if( keyRadio1->isChecked() ){
        config->writeEntry( "dot11WEPDefaultKeyID", 1 );
    } else if( keyRadio2->isChecked() ){
        config->writeEntry( "dot11WEPDefaultKeyID", 2 );
    } else if( keyRadio3->isChecked() ){
        config->writeEntry( "dot11WEPDefaultKeyID", 3 );
    }
    config->writeEntry( "dot11WEPDefaultKey0", keyLineEdit0->text() );
    config->writeEntry( "dot11WEPDefaultKey1", keyLineEdit1->text() );
    config->writeEntry( "dot11WEPDefaultKey2", keyLineEdit2->text() );
    config->writeEntry( "dot11WEPDefaultKey3", keyLineEdit3->text() );
    return writeWirelessOpts( );
}

/**
 */
void WLANImp::accept()
{
  if ( writeConfig() ){
    if(interfaceSetup->saveChanges())
      QDialog::accept();
  }
}

bool WLANImp::writeWirelessOpts( QString scheme )
{
    qWarning( "WLANImp::writeWirelessOpts entered." );
    QString prev = "/etc/pcmcia/wireless.opts";
    QFile prevFile(prev);
    if ( !prevFile.open( IO_ReadOnly ) )
	return false;

    QString tmp = "/etc/pcmcia/wireless.opts-qpe-new";
    QFile tmpFile(tmp);
    if ( !tmpFile.open( IO_WriteOnly ) )
	return false;

    bool retval = true;
    
    QTextStream in( &prevFile );
    QTextStream out( &tmpFile );

    config->setGroup("Properties");
    
    QString line;
    bool found=false;
    bool done=false;
    while ( !in.atEnd() ) {
	QString line = in.readLine();
	QString wline = line.simplifyWhiteSpace();
	if ( !done ) {
	    if ( found ) {
		// skip existing entry for this scheme, and write our own.
		if ( wline == ";;" ) {
		    found = false;
		    continue;
		} else {
		    continue;
		}
	    } else {
		if ( wline.left(scheme.length()+7) == scheme + ",*,*,*)" ) {
		    found=true;
		    continue; // skip this line
		} else if ( wline == "esac" || wline == "*,*,*,*)" ) {
		    // end - add new entry
		    // Not all fields have a GUI, but all are supported
		    // in the letwork configuration files.
		    static const char* txtfields[] = {
			0
		    };
		    QString readmode = config->readEntry( "Mode", "Managed" );
		    QString mode;
		    if( readmode == "Managed" ){
			mode = readmode;
		    } else if( readmode == "adhoc" ){
			mode = "Ad-Hoc";
		    }
		    QString key;
		    if( wepEnabled->isChecked() ){
			int defaultkey = config->readNumEntry( "dot11WEPDefaultKeyID", 0 );
			switch( defaultkey ){
			    case 0:
				key += keyLineEdit0->text();
				break;
			    case 1:
				key += keyLineEdit1->text();
				break;
			    case 2:
				key += keyLineEdit2->text();
				break;
			    case 3:
				key += keyLineEdit3->text();
				break;
			}
		        if( config->readEntry( "AuthType", "opensystem" ) == "opensystem")
			    key += " open";
		    }
		    out << scheme << ",*,*,*)" << "\n"
			<< "    ESSID=" << Global::shellQuote( config->readEntry( "SSID", "any" ) ) << "\n"
			<< "    MODE=" << mode << "\n"
			<< "    KEY=" << Global::shellQuote( key ) << "\n"
			<< "    RATE=" << "auto" << "\n"
			;
			if( mode != "Managed"  )
				out << "    CHANNEL=" << config->readNumEntry( "CHANNEL", 1 ) << "\n";
		    const char** f = txtfields;
		    while (*f) {
			out << "    " << *f << "=" << config->readEntry(*f,"") << "\n";
			++f;
		    }
		    out << "    ;;\n";
		    done = true;
		}
	    }
	}
	out << line << "\n";
    }

    prevFile.close();
    tmpFile.close();
    QString initpath;
    //system("cardctl suspend");
    if( QDir("/etc/rc.d/init.d").exists() ){
	initpath = "/etc/rc.d/init.d";
    } else if( QDir("/etc/init.d").exists() ){
	initpath = "/etc/init.d";
    }
    if( initpath )
	    system(QString("%1/pcmcia stop").arg(initpath));

    if( system( "mv " + tmp + " " + prev ) )
	retval = false;
//#ifdef USE_SCHEMES
//    if ( retval )
//	SchemeChanger::changeScheme(scheme);
//#endif

    //system("cardctl resume");
    if( initpath )
	    system(QString("%1/pcmcia start").arg(initpath));

    return retval;
}