Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
LAWNMower uses Qt4 and libcurl to automatically log into LAWN. To
compile, run qmake and then make.
  • Loading branch information
Ankit Shankar committed Sep 26, 2009
0 parents commit 1e1be5d
Show file tree
Hide file tree
Showing 15 changed files with 578 additions and 0 deletions.
15 changes: 15 additions & 0 deletions LAWNMower.pro
@@ -0,0 +1,15 @@
# -------------------------------------------------
# Project created by QtCreator 2009-09-25T09:13:40
# -------------------------------------------------
TARGET = LAWNMower
TEMPLATE = app
LIBS += -lcurl
SOURCES += main.cpp \
dialog.cpp \
mower.cpp \
lawnmower.cpp
HEADERS += dialog.h \
mower.h \
lawnmower.h
FORMS += dialog.ui
RESOURCES += systray.qrc
24 changes: 24 additions & 0 deletions dialog.cpp
@@ -0,0 +1,24 @@
#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent)
: QDialog(parent), ui(new Ui::DialogClass)
{
ui->setupUi(this);
}

Dialog::~Dialog()
{
delete ui;
}

void Dialog::on_buttonBox_accepted()
{
this->hide();
emit accepted(ui->username->text().toStdString(), ui->password->text().toStdString(), ui->rememberMe->isChecked());
}

void Dialog::on_buttonBox_rejected()
{
this->hide();
}
32 changes: 32 additions & 0 deletions dialog.h
@@ -0,0 +1,32 @@
#ifndef DIALOG_H
#define DIALOG_H

#include <string>

#include <QtGui/QDialog>

namespace Ui
{
class DialogClass;
}

class Dialog : public QDialog
{
Q_OBJECT

public:
Dialog(QWidget *parent = 0);
~Dialog();

signals:
void accepted(std::string username, std::string password, bool remember);

private slots:
void on_buttonBox_accepted();
void on_buttonBox_rejected();

private:
Ui::DialogClass *ui;
};

#endif // DIALOG_H
98 changes: 98 additions & 0 deletions dialog.ui
@@ -0,0 +1,98 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>DialogClass</class>
<widget class="QDialog" name="DialogClass">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>273</width>
<height>143</height>
</rect>
</property>
<property name="windowTitle">
<string>LAWNMower</string>
</property>
<property name="windowIcon">
<iconset resource="systray.qrc">
<normaloff>:/images/pending.png</normaloff>:/images/pending.png</iconset>
</property>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLabel" name="uLabel">
<property name="text">
<string>&amp;Username:</string>
</property>
<property name="buddy">
<cstring>username</cstring>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="username"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="pLabel">
<property name="text">
<string>&amp;Password:</string>
</property>
<property name="buddy">
<cstring>password</cstring>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="password">
<property name="echoMode">
<enum>QLineEdit::Password</enum>
</property>
</widget>
</item>
<item row="2" column="0">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="2" column="1">
<widget class="QCheckBox" name="rememberMe">
<property name="text">
<string>&amp;Remember Me</string>
</property>
</widget>
</item>
<item row="3" column="0">
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>29</height>
</size>
</property>
</spacer>
</item>
<item row="3" column="1">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources>
<include location="systray.qrc"/>
</resources>
<connections/>
</ui>
Binary file added images/disabled.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/error.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/logged_in.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/logged_out.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/pending.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
174 changes: 174 additions & 0 deletions lawnmower.cpp
@@ -0,0 +1,174 @@
#include <QTimer>
#include <QFile>
#include <QDir>
#include <QTextStream>
#include <QMenu>
#include <QApplication>

#include "lawnmower.h"

LAWNMower::LAWNMower()
{
_dialog = new Dialog;
connect(_dialog, SIGNAL(accepted(std::string,std::string, bool)), this, SLOT(loadConfig(std::string,std::string, bool)));
_mower = 0;

enableSystemTray();
setTrayIcon(LAWNMower::Disabled);
_tray->show();

if (readConfig()) {
toggleMower(true);
} else {
_dialog->show();

}
}

LAWNMower::~LAWNMower()
{
delete _mower;
}

void LAWNMower::loadConfig(std::string username, std::string password, bool remember)
{
if (_mower) {
delete _mower;
}
_mower = new Mower(username, password);
if (remember) {
writeConfig(username, password);
}
this->toggleMower(true);
}

void LAWNMower::writeConfig(std::string username, std::string password)
{
QFile configFile(QDir::homePath() + "/.lawnmower");
if (configFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&configFile);
out << username.c_str() << "\n" << password.c_str();
}
}


bool LAWNMower::readConfig()
{
QFile configFile(QDir::homePath() + "/.lawnmower");
if (!configFile.open(QIODevice::ReadOnly | QIODevice::Text))
return false;

QTextStream in(&configFile);
std::string username = in.readLine().toStdString();
std::string password = in.readLine().toStdString();

_mower = new Mower(username, password);

return true;
}

void LAWNMower::toggleMower(bool enabled)
{
if (_mower) {
if (enabled) {
_isEnabled = true;
QTimer::singleShot(0, this, SLOT(mow()));
} else {
_isEnabled = false;
setTrayIcon(LAWNMower::Disabled);
}
}
_aToggle->setChecked(_isEnabled);
}

void LAWNMower::mow()
{
if (!_isEnabled) {
return;
}
int ms = REFRESH;
switch (_mower->getStatus()) {
case Mower::LoggedIn:
setTrayIcon(LAWNMower::LoggedIn);
break;
case Mower::LoggedOut:
setTrayIcon(LAWNMower::LoggedOut);
_mower->login();
break;
case Mower::PendingLogIn:
setTrayIcon(LAWNMower::Pending);
break;
case Mower::PendingLogOut:
setTrayIcon(LAWNMower::Pending);
break;
case Mower::NotOnLAWN:
ms = ERROR_REFRESH;
setTrayIcon(LAWNMower::Disabled);
break;
case Mower::Error:
ms = ERROR_REFRESH;
setTrayIcon(LAWNMower::Error);
break;
}
QTimer::singleShot(ms, this, SLOT(mow()));
}

void LAWNMower::setTrayIcon(Icon icon)
{
QIcon *q;
switch (icon) {
case Disabled:
q = new QIcon(":/images/disabled.png");
break;
case Error:
q = new QIcon(":/images/error.png");
break;
case LoggedIn:
q = new QIcon(":/images/logged_in.png");
break;
case LoggedOut:
q = new QIcon(":/images/logged_out.png");
break;
case Pending:
q = new QIcon(":/images/pending.png");
break;
}
_tray->setIcon(QIcon(*q));
}

void LAWNMower::enableSystemTray()
{
_tray = new QSystemTrayIcon;
QMenu *menu = new QMenu;

QAction *aConfig = menu->addAction(tr("Configure..."));
connect(aConfig, SIGNAL(triggered()), this, SLOT(showDialog()));

_aToggle = menu->addAction(tr("Toggle Mower"));
_aToggle->setCheckable(true);
_aToggle->setChecked(false);
connect(_aToggle, SIGNAL(triggered(bool)), this, SLOT(toggleMower(bool)));

/* // WTF THIS DOESN'T WORK FOR SOME REASON.
QAction *aLogout = menu->addAction(tr("Log Out"));
connect(aLogout, SIGNAL(triggered()), this, SLOT(logout()));
*/

QAction *aQuit = menu->addAction(tr("Quit"));
connect(aQuit, SIGNAL(triggered()), qApp, SLOT(quit()));

_tray->setContextMenu(menu);
}

void LAWNMower::showDialog()
{
_dialog->show();
}

void LAWNMower::logout()
{
if (!_mower->logout()) {
setTrayIcon(LAWNMower::Error);
}
toggleMower(false);
}
47 changes: 47 additions & 0 deletions lawnmower.h
@@ -0,0 +1,47 @@
#ifndef LAWNMOWER_H
#define LAWNMOWER_H

#include <string>

#include <QObject>
#include <QSystemTrayIcon>
#include <QAction>

#include "dialog.h"
#include "mower.h"

class LAWNMower : public QObject
{
Q_OBJECT

public:
LAWNMower();
virtual ~LAWNMower();

public slots:
void loadConfig(std::string username, std::string password, bool remember);
void showDialog();
void toggleMower(bool enabled);
void logout();

private slots:
void mow();

private:
static const int REFRESH = 3000;
static const int ERROR_REFRESH = 10000;
bool _isEnabled;
QSystemTrayIcon *_tray;
QAction *_aToggle;
Dialog *_dialog;
Mower *_mower;

enum Icon { Disabled, Error, LoggedIn, LoggedOut, Pending };

bool readConfig();
void writeConfig(std::string username, std::string password);
void enableSystemTray();
void setTrayIcon(Icon icon);
};

#endif // LAWNMOWER_H

0 comments on commit 1e1be5d

Please sign in to comment.