RouterKeygen::RouterKeygen(QWidget *parent) :
    QMainWindow(parent), ui(new Ui::RouterKeygen), manualWifi(NULL),matcher(new WirelessMatcher()),
    calculator(NULL), loading(NULL), loadingText(NULL), aboutDialog(NULL), welcomeDialog(NULL){
    ui->setupUi(this);
#if !defined(Q_OS_WIN) && !defined(Q_OS_MAC)
    setWindowIcon(QIcon(":/big_icon.png"));
#endif
    connect(ui->calculateButton, SIGNAL( clicked() ), this,
            SLOT( manualCalculation() ));
    connect(ui->refreshScan, SIGNAL( clicked() ), this,
            SLOT( refreshNetworks() ));
    connect(ui->supportedNetworkslist, SIGNAL( cellClicked(int,int) ), this,
            SLOT( supportedNetworkRowSelected(int,int) ));
    connect(ui->unlikelyNetworkslist, SIGNAL( cellClicked(int,int) ), this,
            SLOT( unlikelyNetworkRowSelected(int,int) ));
    connect(ui->unsupportedNetworkslist, SIGNAL( cellClicked(int,int) ), this,
            SLOT( unsupportedNetworkRowSelected(int,int) ));

    connect(ui->actionDonate,SIGNAL(triggered()), this, SLOT(donatePaypal()));
    connect(ui->actionDonate_Google_Play, SIGNAL(triggered()),this, SLOT(donateGooglePlay()) );
    connect(ui->actionFeedback, SIGNAL(triggered()), this,SLOT(feedback()));
    connect(ui->actionAbout, SIGNAL(triggered()), this,SLOT(showAboutDialog()) );
    connect(ui->actionCheck_for_Updates, SIGNAL(triggered()), this,SLOT(checkUpdates()));
    ui->actionCheck_for_Updates->setEnabled(false); //It is enabled after the automatic update check
#if defined(Q_OS_MAC)


#endif
#if !defined(Q_OS_WIN) && !defined(Q_OS_MAC)
    connect(ui->forceRefresh, SIGNAL( stateChanged(int) ), this,
            SLOT( forceRefreshToggle(int) ));
#else
    ui->forceRefresh->setVisible(false); // it is not needed in Windows or Mac
#endif

    wifiManager = new QWifiManager();
    connect(wifiManager, SIGNAL( scanFinished(int) ), this,
            SLOT( scanFinished(int) ));
    loadingAnim = new QMovie(":/loading.gif");
    loadingAnim->setParent(this);
    //Auto-Complete!
    wordList << "Alice-" <<  "Arcor-" << "AXTEL-" << "AXTEL-XTREMO-" << "Bbox-" <<
            "BigPond" << "Blink" << "Cabovisao-" << "CONN" << "CYTA" << "Discus--"<<
            "DLink-" << "DMAX" << "EasyBox-" << "eircom" << "FASTWEB-1-" << "INFINITUM" <<
            "InfostradaWiFi-" << "InterCable" << "JAZZTEL_" << "MAXCOM" << "Megared" <<
            "MEO-" << "O2Wireless" << "Optimus" << "OptimusFibra" << "Orange-" << "OTE" <<
            "Otenet" << "PBS" << "privat" << "ptv" << "SKY" << "SpeedTouch" << "TECOM-AH4222-" <<
            "TECOM-AH4021-" << "TeleTu" << "Thomson" << "TN_private_" << "Vodafone-" << "WiFi" <<
            "wifimedia_R-" << "WLAN_" << "WLAN" << "YaCom";
    completer = new QCompleter(wordList, this);
    completer->setCaseSensitivity(Qt::CaseInsensitive);
    completer->setCompletionMode(QCompleter::PopupCompletion);
    ui->ssidInput->setCompleter(completer);
    ui->passwordsList->installEventFilter(this);

    // build menu
    trayMenu = new QMenu(this);
    trayIcon = new QSystemTrayIcon(this);
    // set up and show the system tray icon
    trayIcon->setIcon(QIcon(":/tray_icon.png"));
    trayIcon->setContextMenu(trayMenu);
    trayIcon->show();
    
    //Set widget ration
    ui->splitterVertical->setStretchFactor(0, 3);
    ui->splitterVertical->setStretchFactor(1, 2);
    
    settings = new QSettings("Exobel", "RouterKeygen");
    bool forceRefresh = settings->value(FORCE_REFRESH, false).toBool();
    wifiManager->setForceScan(forceRefresh);
    ui->forceRefresh->setChecked(forceRefresh);
    runInBackground = settings->value(RUN_IN_BACKGROUND, false).toBool();
    runOnStartUp = settings->value(RUN_ON_START_UP, false).toBool();
    qApp->setQuitOnLastWindowClosed(!runInBackground);

    startUpAction = ui->menuPreferences->addAction(tr("Run on Start up"));
    startUpAction->setCheckable(true);
    startUpAction->setChecked(runOnStartUp);
    connect(startUpAction, SIGNAL(toggled(bool)), this,
            SLOT(startUpRunToggle(bool)));

    runInBackgroundAction = ui->menuPreferences->addAction(tr("Run in the background"));
    runInBackgroundAction->setCheckable(true);
    runInBackgroundAction->setChecked(runInBackground);
    connect(runInBackgroundAction, SIGNAL(toggled(bool)), this,
            SLOT(backgroundRunToggle(bool)));

    //Set up the tray icon
    connect(trayMenu->addAction(tr("Open")),
            SIGNAL(triggered()), this, SLOT(show()));
    trayMenu->addSeparator();
    connect(trayMenu->addAction(tr("Vulnerable networks")),
            SIGNAL(triggered()), this, SLOT(show()));
    trayMenu->addAction(tr("None were detected"))->setEnabled(false);
    trayMenu->addSeparator();
    trayMenu->addAction(startUpAction);
    trayMenu->addAction(runInBackgroundAction);
    trayMenu->addSeparator();
    connect(trayMenu->addAction(tr("Quit")), SIGNAL(triggered()), qApp, SLOT(quit()));

    //Do an initial scan
    wifiManager->startScan();

    //Check for application updates, it fails silently
    automaticUpdateCheck = true;
    checkUpdates();
}
RouterKeygen::RouterKeygen(QWidget *parent) :
		QMainWindow(parent), ui(new Ui::RouterKeygen), loading(NULL), loadingText(
				NULL) {
	ui->setupUi(this);
	connect(ui->calculateButton, SIGNAL( clicked() ), this,
			SLOT( manualCalculation() ));
	connect(ui->refreshScan, SIGNAL( clicked() ), this,
			SLOT( refreshNetworks() ));
	connect(ui->networkslist, SIGNAL( cellClicked(int,int) ), this,
			SLOT( tableRowSelected(int,int) ));
#if !defined(Q_OS_WIN) && !defined(Q_OS_MAC)
	connect(ui->forceRefresh, SIGNAL( stateChanged(int) ), this,
			SLOT( forceRefreshToggle(int) ));
#else
	ui->forceRefresh->setVisible(false); // it is not needed in Windows
#endif
	wifiManager = new QWifiManager();
	connect(wifiManager, SIGNAL( scanFinished(int) ), this,
			SLOT( scanFinished(int) ));
	loadingAnim = new QMovie(":/images/loading.gif");
	loadingAnim->setParent(this);
	/*Auto-Complete!*/
	QStringList wordList;
	wordList << "Thomson" << "Blink" << "SpeedTouch" << "O2Wireless"
			<< "Orange-" << "INFINITUM" << "BigPond" << "Otenet" << "Bbox-"
			<< "DMAX" << "privat" << "DLink-" << "Discus--" << "eircom"
			<< "FASTWEB-1-" << "Alice-" << "WLAN_" << "WLAN" << "JAZZTEL_"
			<< "WiFi" << "YaCom" << "SKY" << "TECOM-AH4222-" << "TECOM-AH4021-"
			<< "InfostradaWiFi-" << "TN_private_" << "CYTA" << "PBS" << "CONN"
			<< "OTE" << "Vodafone-" << "EasyBox-" << "Arcor-" << "Megared"
			<< "Optimus" << "OptimusFibra" << "MEO-";
	QCompleter *completer = new QCompleter(wordList, this);
	completer->setCaseSensitivity(Qt::CaseInsensitive);
	completer->setCompletionMode(QCompleter::PopupCompletion);
	ui->ssidInput->setCompleter(completer);
	ui->networkslist->setSelectionBehavior(QAbstractItemView::SelectRows);
	ui->networkslist->setEditTriggers(QAbstractItemView::NoEditTriggers);
	ui->passwordsList->installEventFilter(this);
	this->router = NULL;
	this->calculator = NULL;

	//Set widget ration
	ui->splitter->setStretchFactor(0, 2);
	ui->splitter->setStretchFactor(1, 1);

	// set up and show the system tray icon

	// build menu
	trayMenu = new QMenu(this);
	trayIcon = new QSystemTrayIcon(this);
	trayIcon->setIcon(windowIcon());
	trayIcon->setContextMenu(trayMenu);
	trayIcon->show();

	settings = new QSettings("Exobel", "RouterKeygen");
	bool forceRefresh = settings->value(FORCE_REFRESH, false).toBool();
	wifiManager->setForceScan(forceRefresh);
	ui->forceRefresh->setChecked(forceRefresh);
	runInBackground = settings->value(RUN_IN_BACKGROUND, true).toBool();
	runOnStartUp = settings->value(RUN_ON_START_UP, false).toBool();
	qApp->setQuitOnLastWindowClosed(!runInBackground);
    scanFinished(QWifiManager::SCAN_OK);
	wifiManager->startScan();

}