1 /***************************************************************************
2 kwifimanager.cpp - description
4 begin : Sam Apr 7 11:44:20 CEST 2001
5 copyright : (C) 2001-05 by Stefan Winter
6 email : mail@stefan-winter.de
7 ***************************************************************************/
9 /***************************************************************************
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 ***************************************************************************/
18 // include files for C++
22 // include files for QT
30 #include <QPushButton>
34 #include <QTextStream>
35 #include <QGridLayout>
37 #include <QApplication>
38 #include <QStringList>
39 // include files for KDE
40 #include <kapplication.h>
44 #include <kstandardaction.h>
45 #include <kstandarddirs.h>
49 #include <KStandardGuiItem>
50 #include <kmessagebox.h>
51 #include <ksystemtrayicon.h>
52 #include <k3process.h>
54 #include <kconfigdialog.h>
55 #include <ktoggleaction.h>
56 #include <kactioncollection.h>
57 // application specific includes
59 #include "networkscanning.h"
60 #include "kwifimanager.h"
61 #include "interface_wireless.h"
62 #include "interface_wireless_wirelessextensions.h"
63 #include "statistics.h"
64 #include "kwifi_settings.h"
65 #include "ui_config_behaviour.h"
66 #include "ui_config_appearance.h"
67 #include "kwifi_ifaceimpl.h"
68 #include "wifiadaptor.h"
70 #define ID_STATUS_MSG 1
72 QPixmap
* KWiFiManagerApp::NO_CARD
= 0;
73 QPixmap
* KWiFiManagerApp::AD_HOC
= 0;
74 QPixmap
* KWiFiManagerApp::ALL_ALONE
= 0;
75 QPixmap
* KWiFiManagerApp::AP_CONNECT
= 0;
77 QPixmap
* KWiFiManagerApp::STRENGTHPIC
[6];
78 QString
KWiFiManagerApp::strengthClassName
[6];
80 QStringList
KWiFiManagerApp::APs
;
82 KWiFiManagerApp::KWiFiManagerApp ( QWidget
*, const char *name
):
83 KXmlGuiWindow ( 0 ), device ( 0 ), m_shuttingDown ( false )
86 new WifiAdaptor(this);
87 QDBusConnection::sessionBus().registerObject("/KWifiManager", this);
88 initAccessPointDatabase ( );
90 KWiFiSettings::self ( )->readConfig ( );
93 QString kdedir
= finder
.findResourceDir ( "data", "kwifimanager/pics/no_card.png" );
94 kdedir
+= "kwifimanager/pics/";
96 kDebug() << kdedir
<< endl
;
98 // FIXME: the following paths are QLineEdits in the GUI, but should be KUrlRequesters
100 if ( KWiFiSettings::self ( )->useCustomStatusIcons ( ) )
102 NO_CARD
= new QPixmap ( KWiFiSettings::self ( )->noCardPicture ( ) );
103 AD_HOC
= new QPixmap ( KWiFiSettings::self ( )->adHocPicture ( ) );
104 ALL_ALONE
= new QPixmap ( KWiFiSettings::self ( )->noAPPicture ( ) );
105 AP_CONNECT
= new QPixmap ( KWiFiSettings::self ( )->aPConnectionPicture ( ) );
109 NO_CARD
= new QPixmap ( kdedir
+ "no_card.png" );
110 AD_HOC
= new QPixmap ( kdedir
+ "ad_hoc.png" );
111 ALL_ALONE
= new QPixmap ( kdedir
+ "all_alone.png" );
112 AP_CONNECT
= new QPixmap ( kdedir
+ "ap_connect.png" );
115 if ( KWiFiSettings::self ( )->useCustomStrengthIcons ( ) )
117 STRENGTHPIC
[5] = new QPixmap ( KWiFiSettings::self ( )->veryGoodPicture ( ) );
118 STRENGTHPIC
[4] = new QPixmap ( KWiFiSettings::self ( )->goodPicture ( ) );
119 STRENGTHPIC
[3] = new QPixmap ( KWiFiSettings::self ( )->weakPicture ( ) );
120 STRENGTHPIC
[2] = new QPixmap ( KWiFiSettings::self ( )->minimumPicture ( ) );
121 STRENGTHPIC
[1] = new QPixmap ( KWiFiSettings::self ( )->noConnectionPicture ( ) );
122 STRENGTHPIC
[0] = new QPixmap ( KWiFiSettings::self ( )->noInformationAvailablePicture ( ) );
126 STRENGTHPIC
[5] = new QPixmap ( kdedir
+ "excellent.png" );
127 STRENGTHPIC
[4] = new QPixmap ( kdedir
+ "good.png" );
128 STRENGTHPIC
[3] = new QPixmap ( kdedir
+ "marginal.png" );
129 STRENGTHPIC
[2] = new QPixmap ( kdedir
+ "oor_minimum.png" );
130 STRENGTHPIC
[1] = new QPixmap ( kdedir
+ "oor_down.png" );
131 STRENGTHPIC
[0] = new QPixmap ( kdedir
+ "offline.png" );
133 strengthClassName
[0] = i18n ( "N/A" );
134 strengthClassName
[1] = i18n ( "OUT OF RANGE" );
135 strengthClassName
[2] = i18n ( "MINIMUM" );
136 strengthClassName
[3] = i18n ( "WEAK" );
137 strengthClassName
[4] = i18n ( "GOOD" );
138 strengthClassName
[5] = i18n ( "EXCELLENT" );
144 setAutoSaveSettings ( );
146 resize ( sizeHint ( ) );
151 KWiFiManagerApp::~KWiFiManagerApp ( )
153 kDebug ( ) << "Thank you for using KWiFiManager. You have made a simple program very happy." << endl
;
157 KWiFiManagerApp::initAccessPointDatabase ( )
159 KStandardDirs finder
;
160 QString kdedir
= finder
.findResourceDir ( "data", "kwifimanager/locations/NOWHERE.loc" );
161 kdedir
+= "kwifimanager/locations/";
162 QDir
*locations
= new QDir ( kdedir
);
163 if ( !locations
->exists ( ) )
164 kDebug ( ) << "A grave error occurred while searching for files with AccessPoint information." << endl
;
165 QStringList
*entries
= new QStringList ( locations
->entryList ( ) );
168 for ( QStringList::Iterator it
= entries
->begin ( ); it
!= entries
->end ( ); it
++ )
170 if ( ( *it
) != "." && ( *it
) != ".." )
172 QFile
datei ( kdedir
+ ( *it
) );
173 datei
.open ( QIODevice::ReadOnly
);
174 while ( !( content
= QString::fromUtf8 ( datei
.readLine ( ) ) ).isEmpty ( ) )
176 QString
temp ( content
);
177 content
.truncate ( content
.indexOf ( ' ' ) );
178 content
= content
.toUpper ( );
179 content2
= temp
.right ( temp
.length ( ) - temp
.indexOf ( ' ' ) - 1 );
180 content2
.truncate ( content2
.length ( ) - 1 );
181 APs
<< content
<< content2
;
191 KWiFiManagerApp::slotLogESSID ( QString essid
)
193 static bool alreadyWarned
= false;
194 if ( !KWiFiSettings::self ( )->logHistory ( ) )
196 QFile
log_file ( KStandardDirs::locateLocal ( "data", "kwifimanager/kwifimanager.log", true ) );
197 if (!alreadyWarned
) {
198 kDebug ( ) << "Log file is " << log_file
.fileName ( ) << endl
;
199 alreadyWarned
= true;
201 if ( !log_file
.open ( QIODevice::WriteOnly
| QIODevice::Append
) )
202 kDebug ( ) << "Logging failed!" << endl
;
203 QTextStream
stream ( &log_file
);
204 stream
<< QDateTime::currentDateTime ( ).toString ( ).toLatin1 ( );
205 stream
<< " Entering network: " << essid
.toLatin1 ( ) << endl
;
210 KWiFiManagerApp::initActions ( )
212 KSharedConfig::Ptr config
= KGlobal::config( );
213 if ( config
->hasGroup ( "General" ) )
214 config
->setGroup ( "General" );
216 KStandardAction::quit ( this, SLOT ( slotFileQuit ( ) ), actionCollection ( ) );
218 KStandardAction::preferences ( this, SLOT ( showSettings ( ) ), actionCollection ( ) );
224 KWiFiManagerApp::initView ( )
226 view
= new QWidget ( this );
227 view
->setBackgroundRole ( QPalette::Background
);
228 setCentralWidget ( view
);
229 zentrallayout
= new QGridLayout ( view
);
230 iface_tabs
= new QTabWidget( view
);
231 zentrallayout
->addWidget(iface_tabs
);
233 setCaption (windowTitle( ).prepend( " - " ) );
237 KWiFi_Iface
* newIface
;
239 // create instances of a wireless interface. When instances return a careOfInterface,
240 // fire anoher instance and so all interfaces are iterated
243 newIface
= new KWiFi_Iface( this );
244 connect(newIface
,SIGNAL( interfaceChanged( KWiFi_Iface
* ) ), this, SLOT( slotChangeInterfaceList( KWiFi_Iface
* ) ) );
245 slotChangeInterfaceList( newIface
);
247 while (newIface
->careOfInterface() != empty
);
254 KWiFiManagerApp::slotChangeInterfaceList ( KWiFi_Iface
* iface
)
256 kDebug() << "slotChangeInterfaceList: careOf is " << iface
->careOfInterface() << endl
;
257 if (iface
->careOfInterface() != "")
259 iface_tabs
->addTab( iface
, iface
->careOfInterface( ) );
260 setCaption ( windowTitle( ).prepend( " " + iface
->careOfInterface( ) ) );
264 if (iface_tabs
->indexOf( iface
) >= 0 ) iface_tabs
->removeTab( iface_tabs
->indexOf( iface
) );
265 setCaption ( windowTitle( ).replace( " " + iface
->careOfInterface( ), "" ) );
270 KWiFiManagerApp::slotFileQuit ( )
272 m_shuttingDown
= true;
277 KWiFiManagerApp::slotStartConfigEditor ( )
280 startConf
<< "kdesu" << "kcmshell" << "kcmwifi";
281 startConf
.start ( K3Process::DontCare
);
285 KWiFiManagerApp::saveProperties ( KConfig
* conf
)
287 m_startDocked
= !isVisible ( );
288 conf
->writeEntry ( "startDocked", m_startDocked
);
289 conf
->writeEntry ( "wifidevice", device
->get_interface_name ( ) );
294 KWiFiManagerApp::readProperties ( KConfig
* conf
)
296 m_startDocked
= conf
->readEntry ( "startDocked", false );
302 QString dev
= conf
->readEntry ( "wifidevice" );
303 if ( !dev
.isEmpty ( ) )
304 device
->setActiveDevice ( dev
);
307 bool KWiFiManagerApp::queryClose ( )
309 if ( !m_shuttingDown
&& !kapp
->sessionSaving ( ) && KWiFiSettings::self()->stayInTrayOnClose() )
317 bool KWiFiManagerApp::queryExit ( )
319 // Save settings if auto-save is enabled, and settings have changed
320 if ( settingsDirty ( ) && autoSaveSettings ( ) )
321 saveAutoSaveSettings ( );
326 KWiFiManagerApp::showSettings ( )
328 if ( KConfigDialog::showDialog ( "settings" ) )
331 appearance_container
= new QWidget ( 0 );
332 appearance_ui
.setupUi ( appearance_container
);
334 behaviour_container
= new QWidget ( 0 );
335 behaviour_ui
.setupUi ( behaviour_container
);
337 KConfigDialog
*dialog
= new KConfigDialog ( this, "settings", KWiFiSettings::self ( ),KPageDialog::List
);
338 dialog
->addPage ( behaviour_container
, i18n ( "General" ), 0 );
339 dialog
->addPage ( appearance_container
, i18n ( "Appearance" ), 0 );
340 connect ( dialog
, SIGNAL ( settingsChanged ( const QString
& ) ), this, SLOT ( loadSettings ( ) ) );
345 KWiFiManagerApp::loadSettings ( )
349 #include "kwifimanager.moc"