/** * @author Patrick Fries * * Mainwindow of the application. This is where the user interacts with the application */ package guiClasses; import helperClasses.*; import java.io.*; import org.gnu.glade.*; import org.gnu.gtk.*; import org.gnu.glib.CustomEvents; import wlanDriverConnectors.NICdriver; import wlanDriverConnectors.linux_wlan_ng; import wlanExceptions.*; public class Mainwindow { // 0. Declaration of objectariables private LibGlade gladeMainWindow; // To browse the glade-file of the main window private ConfigContainer configContainer; // Administates all configuration-stuff private Window mainWindow; // Refers to the main application window private TreeView foundAPTree; // Refers to the tree of found APs private Label labelStatus; // Label to show the NIC status private Label labelQualty; // Label to show the signal quality private Image imageQualty; // Image to show an icon as a function of the signal quality private Label labelConnAP; // Label to show SSID and BSSID of the connected AP private Label labelEncryption; // Label to show encryption type and key private Label labelIP; // Label to show IP and Subnet-Mask private Label labelMAC; // Label to show the MAC-Adress private StatusBar statusBar; // Refers to the statusbar at the bottom of the main window private NICdriver nicDriver; // Refers to the driver connector private StatusUpdater statusUpdater; // Instance of the local class StatusUpdater to keep statusinformations up to date private CustomEventAdder customEventAdder; private Thread CustomEventAdderThread; // 1. Konstructor public Mainwindow() { // 1.1 Call libglade to browse the gladefile of the main window try { gladeMainWindow = new LibGlade("gui/mainwindow.glade", this); } catch (Exception e) { e.printStackTrace(); System.exit(-1); } // 1.2 Connect the gtk widgets with the objectvariables this.mainWindow = (Window)gladeMainWindow.getWidget("window_MainWindow"); this.foundAPTree = (TreeView)gladeMainWindow.getWidget("treeview_listAP"); this.labelQualty = (Label)gladeMainWindow.getWidget("label_currentQuality"); this.imageQualty = (Image)gladeMainWindow.getWidget("image_signalQuality"); this.labelStatus = (Label)gladeMainWindow.getWidget("label_currentStatus"); this.labelConnAP = (Label)gladeMainWindow.getWidget("label_currentAP"); this.labelEncryption = (Label)gladeMainWindow.getWidget("label_Encrypion"); this.labelIP = (Label)gladeMainWindow.getWidget("label_currentIP"); this.labelMAC = (Label)gladeMainWindow.getWidget("label_currentMAC"); this.statusBar = (StatusBar)gladeMainWindow.getWidget("statusbar"); // 1.3 Generate the ConfugurationContainer this.configContainer = new ConfigContainer(); // 1.4 If drivertype is already specified then a driver connector is generated if ((this.configContainer.getDriver() != -1) && (!this.configContainer.getNIC().equals(""))) { switch (this.configContainer.getDriver()) { case ConfigContainer.DRIVER_LINUXWLANNG: this.nicDriver = new linux_wlan_ng(configContainer.getNIC(), configContainer.getKnownAPs()); break; default: System.out.println("Unknown nic-driver in configurationfile!"); } } else System.out.println("You do not have a driver specified!"); // Generate new instance of statusupdater and run it in an own thread this.statusUpdater = new StatusUpdater(); this.customEventAdder = new CustomEventAdder(); this.CustomEventAdderThread = new Thread(this.customEventAdder); this.CustomEventAdderThread.start(); //org.gnu.glib.CustomEvents.addEvent(this.statusUpdater); } // 2. Methods to response on menu-events // 2.1 Is called when "Main->Quit Application" is clicked public void on_quit_application_activate() { Gtk.mainQuit(); System.exit(0); } // 2.2 Is called when "Main->Configure NIC" is clicked public void on_configure_nic_activate() { new NICConfigurationWindow(this.configContainer); } // Local class that adds the StatusUpdater to the CustomEvents in an intervall of 5 seconds class CustomEventAdder implements Runnable { private boolean forceRefresh = false; public void run() { long nextRefresh; while (true) { CustomEvents.addEvent(statusUpdater); CustomEvents.runEvents(); nextRefresh = System.currentTimeMillis() + 5000; while (nextRefresh > System.currentTimeMillis()) { if (forceRefresh) { forceRefresh = false; break; } try { Thread.sleep(500); } catch (InterruptedException e) { } } } } } // Local class that keeps the statusinformation on the mainwindow up to date class StatusUpdater implements Runnable { // 1. Implementation of interface Runnable public void run() { // 1.0 Declaration of local variables boolean writeEmptyStatus = false; // If set to true then all textboxes will be set to "" boolean isPresent = false; boolean isActivated = false; boolean isConnected = false; long nextRefresh; // Stores the time in ms for the next refresh try { System.out.println("Refreshing..."); // 1.0 Buffer some information from the driverconnector that is needed quite often if (nicDriver != null) { isPresent = nicDriver.isPresent(); isActivated = nicDriver.isActivated(); isConnected = nicDriver.isConnected(); } // 1.1 Examine that driverconnector is configured and present if (nicDriver == null) { writeEmptyStatus=true; labelStatus.setText("Status of NIC (not configured!)"); } else { if (!isPresent) { writeEmptyStatus=true; labelStatus.setText("Status of NIC " + configContainer.getNIC() + " (not present)"); } else { if (!isActivated) { writeEmptyStatus=false; labelStatus.setText("Status of NIC " + configContainer.getNIC() + " (not activated)"); } else { writeEmptyStatus=false; labelStatus.setText("Status of NIC " + configContainer.getNIC()); } } } // 1.2 Update the connection quality if (writeEmptyStatus) { labelQualty.setText("Linkquality: not connected"); imageQualty.set("gui/signal_offline.png"); } else { if (!isConnected) { labelQualty.setText("Linkquality: not connected"); imageQualty.set("gui/signal_offline.png"); } else { int signalQuality = nicDriver.getSignalQuality(); labelQualty.setText("Linkquality: " + this.signalQuality2String(signalQuality) + " (" + signalQuality + "%)"); imageQualty.set("gui/signal_" + this.signalQuality2String(signalQuality) + ".png"); System.out.println(signalQuality); } } // 1.3 Update the "connected AP"-label and "encryption"-label if (writeEmptyStatus || !isPresent || !isActivated || !isConnected) { labelConnAP.setText("Connected to AP:"); labelEncryption.setText("Encryption:"); } else { AccessPoint ap = nicDriver.getConnectedAP(); labelConnAP.setText("Connected to AP: " + ap.getssid() + " (BSSID: " + ap.getbssid() + ")"); labelEncryption.setText("Encryption: " + this.encryptionInt2String(ap.getEncryption()) + (ap.getEncryption() > AccessPoint.UNENCRYPTED ? " (Key: " + ap.getEncryptionKey() + ")": "")); } // 1.4 Update the labels with IP information if (writeEmptyStatus || !isPresent) { labelIP.setText("Current IP:"); labelMAC.setText("MAC Adress:"); } else { IPData ip = nicDriver.getIPData(); labelIP.setText("Current IP: " + ip.getIP() + " (Subnet-Mask: " + ip.getSubnet() + ")"); labelMAC.setText("MAC Adress: " + ip.getMAC()); } // 1.5 Redraw the mainwindow mainWindow.draw(); } catch (Exception e) { new ExceptionWindow(e, ""); } } // Helpermethods // Method that converts the signalquality percentage to the string it belongs to private String signalQuality2String(int quality) { if (quality <= 10) return "down"; else if (quality <= 32) return "minimum"; else if (quality <= 55) return "marginal"; else if (quality <= 77) return "good"; else return "excellent"; } // Method that converts the given int value for the encryption to the correspondending string private String encryptionInt2String(int encryptionType) { switch (encryptionType) { case AccessPoint.DONTKNOW: return "unknown"; case AccessPoint.UNENCRYPTED: return "unencrypted"; case AccessPoint.WEP_ENCRYPTED: return "WEP"; case AccessPoint.WPA_ENCRYPTED: return "WPA"; } return "unknown"; } } public static void main(String[] args) { Gtk.init(args); Mainwindow g = new Mainwindow(); Gtk.main(); } }