master/main.cpp
Commiter: gramakri
Author: gramakri
Revision: 3942789c48
File Size: 6.33 KB
(October 20, 2008 22:21 UTC) Over 3 years ago
Show stats
/*
* Copyright (C) 2008 Girish Ramakrishnan <girish@forwardbias.in>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <QtGui>
#include <QtWebKit>
class KeepAlive : public QWebView
{
Q_OBJECT
public:
KeepAlive(const QString &username, const QString &domain, const QString &password, QWidget *parent = 0)
: QWebView(parent), m_autoLogCount(0)
{
setAttribute(Qt::WA_QuitOnClose, false);
connect(this, SIGNAL(loadFinished(bool)), this, SLOT(loadFinished(bool)));
connect(this, SIGNAL(urlChanged(QUrl)), this, SLOT(urlChanged(QUrl)));
connect(this, SIGNAL(loadProgress(int)), this, SLOT(loadProgress(int)));
m_loginTimer = new QTimer(this);
m_loginTimer->setInterval(15000);
m_loginTimer->setSingleShot(true);
connect(m_loginTimer, SIGNAL(timeout()), this, SLOT(login()));
m_watchDogTimer = new QTimer(this);
m_watchDogTimer->setInterval(60000);
connect(m_watchDogTimer, SIGNAL(timeout()), this, SLOT(refresh()));
m_watchDogTimer->start();
m_trayIcon = new QSystemTrayIcon(this);
m_trayIcon->setIcon(QPixmap(":/happy.png"));
m_trayIcon->setVisible(true);
connect(m_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this, SLOT(toggleWebView(QSystemTrayIcon::ActivationReason)));
QMenu *menu = new QMenu(this);
QAction *showAction = menu->addAction("Show browser");
connect(showAction, SIGNAL(triggered()), this, SLOT(show()));
QAction *statsAction = menu->addAction("Show statistics");
connect(statsAction, SIGNAL(triggered()), this, SLOT(showStatistics()));
menu->addSeparator();
QAction *exitAction = menu->addAction("Exit");
connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
m_trayIcon->setContextMenu(menu);
m_loginScript = QString("var USERNAME=\"%1\"; var DOMAIN = \"%2\"; var PASSWORD = \"%3\";")
.arg(QString(username).replace("\"", "\\\""))
.arg(QString(domain).replace("\"", "\\\""))
.arg(QString(password).replace("\"", "\\\""));
QFile f(":/login.js");
f.open(QFile::ReadOnly);
m_loginScript.append(QTextStream(&f).readAll());
refresh();
}
~KeepAlive()
{
}
private slots:
void login()
{
qDebug() << "login() " << url();
QVariant result = page()->mainFrame()->evaluateJavaScript(m_loginScript);
qDebug() << result;
if (result.toBool()) { // script's return value seems to be always false
qDebug() << "login successful";
++m_autoLogCount;
}
m_watchDogTimer->start();
}
void refresh()
{
if (m_loginTimer->isActive())
return;
qDebug() << "refresh()";
setUrl(QUrl("https://loginban.tataindicombroadband.in:8443/home"));
}
void loadFinished(bool ok)
{
qDebug() << "loadFinished " << ok << url();
m_trayIcon->setIcon(QPixmap(ok ? ":/happy.png" : ":/sad.png"));
m_loginTimer->start();
}
void urlChanged(const QUrl &url)
{
qDebug() << "urlChanged " << url;
m_loginTimer->start();
}
void toggleWebView(QSystemTrayIcon::ActivationReason reason)
{
if (reason != QSystemTrayIcon::Trigger)
return;
setVisible(!isVisible());
}
void loadProgress(int progress)
{
qDebug() << "progress " << progress;
m_loginTimer->start();
}
void showStatistics()
{
QMessageBox::information(this, tr("Statistics"), tr("Auto logged in %1 times").arg(m_autoLogCount));
}
private:
QTimer *m_loginTimer, *m_watchDogTimer;
QString m_loginScript;
QSystemTrayIcon *m_trayIcon;
int m_autoLogCount;
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
app.setOrganizationName("ForwardBias");
app.setOrganizationDomain("forwardbias.in");
app.setApplicationName("TataWimaxKeepAlive");
QSettings settings;
QString username = settings.value("login/username").toString();
QString domain = settings.value("login/domain").toString();
QString password = settings.value("login/password").toString();
if (!username.isEmpty() && !password.isEmpty()) {
qDebug() << "Using settings from " << settings.fileName();
} else {
while (username.isEmpty() || password.isEmpty()) {
char u[50], d[50], p[50];
printf("Enter username : ");
scanf("%50s", u);
username = QString::fromLocal8Bit(u).trimmed();
printf("Enter domain: ");
scanf("%50s", d);
domain = QString::fromLocal8Bit(d).trimmed();
printf("Enter password : ");
scanf("%50s", p);
password = QString::fromLocal8Bit(p).trimmed();
}
settings.setValue("login/username", username);
settings.setValue("login/domain", domain);
settings.setValue("login/password", password);
}
QWebSettings::setMaximumPagesInCache(10);
#if 1
KeepAlive keepAlive(username, domain, password);
keepAlive.show();
#else
QWebView *webView = new QWebView;
webView->setUrl(QUrl("https://loginban.tataindicombroadband.in:8443/home"));
webView->show();
QEventLoop l;
QTimer::singleShot(40000, &l, SLOT(quit()));
l.exec();
qDebug() << "evaluating now..";
QFile f("script.js");
f.open(QFile::ReadOnly);
QString script = QTextStream(&f).readAll();
QVariant result = webView->page()->mainFrame()->evaluateJavaScript(script);
#endif
return app.exec();
}
#include "main.moc" |