/
lawnmower.cpp
188 lines (163 loc) · 3.95 KB
/
lawnmower.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <QTimer>
#include <QFile>
#include <QDir>
#include <QTextStream>
#include <QMenu>
#include <QApplication>
#include <QNetworkReply>
#include "lawnmower.h"
LAWNMower::LAWNMower()
{
_net = new QNetworkAccessManager(this);
connect(_net, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyReceived(QNetworkReply*)));
_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(_net, 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(_net, username, password);
return true;
}
void LAWNMower::toggleMower(bool enabled)
{
if (_mower) {
if (enabled) {
_isEnabled = true;
_mower->getStatus();
} else {
_isEnabled = false;
setTrayIcon(LAWNMower::Disabled);
}
}
_aToggle->setChecked(_isEnabled);
}
void LAWNMower::mow(Mower::Status status)
{
if (!_isEnabled) {
return;
}
int ms = REFRESH;
switch (status) {
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, _mower, SLOT(getStatus()));
}
void LAWNMower::setTrayIcon(Icon icon)
{
QIcon *q = 0;
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()
{
// TODO Implement...
toggleMower(false);
_mower->logout();
}
void LAWNMower::replyReceived(QNetworkReply *reply)
{
if (!_mower) {
return;
}
if (_mower->getState() == Mower::GettingStatus) {
std::string s(QString(reply->readAll()).toStdString());
mow(Mower::strToStatus(s));
}
delete reply;
}