[e57275]: switchview / switchview.cpp Maximize Restore History

Download this file

switchview.cpp    272 lines (225 with data), 7.3 kB

  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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Copyright (C) 2011 David Sugar, Haakon Eriksen, GNU Free Call Foundation
//
// This file is part of SwitchView.
//
// SwitchView is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// SwitchView 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SwitchView. If not, see <http://www.gnu.org/licenses/>.
#include "switchview.h"
#include <QTimer>
SwitchView *switchview = NULL;
QSystemTrayIcon *trayicon = NULL;
bool testing = false;
bool startup = false;
bool alwaysopen = false;
static QAction *optionsAction;
static QAction *mappedAction;
static QAction *reconnectAction;
static QAction *reloadAction;
static bool connected = false;
static bool stopping = false;
static bool initial = true;
static void warning(void)
{
if(mapped->isVisible())
return;
QString text = SwitchView::tr("No server connected");
QMessageBox::warning(switchview, SwitchView::tr("SwitchView"), text);
}
SwitchView::SwitchView() :
QMainWindow(NULL)
{
// singleton...
Q_ASSERT(switchview == NULL);
switchview = this;
QAction *aboutAction = new QAction(tr("About"), this);
aboutAction->setIcon(QIcon::fromTheme("help-about"));
aboutAction->setIconVisibleInMenu(true);
connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
optionsAction = new QAction(tr("&Options"), this);
optionsAction->setIcon(QIcon::fromTheme("preferences-other"));
optionsAction->setIconVisibleInMenu(true);
connect(optionsAction, SIGNAL(triggered()), this, SLOT(openOptions()));
reconnectAction = new QAction(tr("Reconnect"), this);
reconnectAction->setIcon(QIcon::fromTheme("connect_creating"));
reconnectAction->setIconVisibleInMenu(true);
connect(reconnectAction, SIGNAL(triggered()), this, SLOT(reconnect()));
reloadAction = new QAction(tr("Reload"), this);
reloadAction->setIcon(QIcon::fromTheme("reload"));
reloadAction->setIconVisibleInMenu(true);
mappedAction = new QAction(tr("&Status"), this);
mappedAction->setIcon(QIcon::fromTheme("dialog-information"));
mappedAction->setIconVisibleInMenu(true);
connect(mappedAction, SIGNAL(triggered()), this, SLOT(openMapped()));
QAction *quitAction = new QAction(tr("&Quit"), this);
quitAction->setIcon(QIcon::fromTheme("exit"));
quitAction->setIconVisibleInMenu(true);
connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
QApplication::setQuitOnLastWindowClosed(false);
setWindowIcon(QIcon(":/live.png"));
setWindowFlags(Qt::Window);
QMenu *traymenu = new QMenu(NULL);
traymenu->addAction(aboutAction);
traymenu->addAction(optionsAction);
traymenu->addSeparator();
traymenu->addAction(reconnectAction);
traymenu->addAction(reloadAction);
traymenu->addAction(mappedAction);
traymenu->addSeparator();
traymenu->addAction(quitAction);
Options::start();
trayicon = new QSystemTrayIcon(this);
if(Options::isTray()) {
trayicon->setContextMenu(traymenu);
trayicon->setIcon(QIcon(":/down.png"));
trayicon->show();
}
connect(qApp, SIGNAL(aboutToQuit()),
this, SLOT(stop()));
if(Options::isTray())
connect(trayicon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this, SLOT(action(QSystemTrayIcon::ActivationReason)));
Events::start();
Mapped::start();
if(!Options::isTray())
QApplication::setQuitOnLastWindowClosed(true);
if(alwaysopen)
openMapped();
}
SwitchView::~SwitchView()
{
options->setParent(NULL); // kill connection to mapped before close...
mapped->setParent(NULL);
stopping = true;
Mapped::stop();
Events::stop();
Options::stop();
}
void SwitchView::stop(void)
{
delete this;
switchview = NULL;
}
void SwitchView::online(void)
{
if(connected)
return;
if(!initial && options->notify[NOTIFY_CONNECTING] && trayicon)
trayicon->showMessage("SwitchView", "Reconnected to the server");
initial = false;
if(Options::isTray())
trayicon->setIcon(QIcon(":/live.png"));
#if defined(AF_UNIX) && !defined(_MSWINDOWS_)
reconnectAction->setVisible(false);
#endif
mappedAction->setVisible(true);
connected = true;
}
void SwitchView::reconnect(void)
{
#if defined(AF_UNIX) && !defined(_MSWINDOWS_)
if(connected)
return;
#else
if(connected) {
mapped->close();
mappedAction->setEnabled(false);
if(Options::isTray())
trayicon->setIcon(QIcon(":/down.png"));
connected = false;
}
#endif
Events::reconnect();
}
void SwitchView::offline(void)
{
if(initial) {
if(!startup)
warning();
initial = false;
return;
}
if(!connected)
return;
mapped->close();
if(trayicon && trayicon->supportsMessages() && options->notify[NOTIFY_DISCONNECT])
trayicon->showMessage("SwitchView", "Server connection lost", QSystemTrayIcon::Critical);
else if(!alwaysopen)
QMessageBox::critical(NULL, tr("SwitchView"),
tr("Server connection lost\n\n"));
if(Options::isTray())
trayicon->setIcon(QIcon(":/down.png"));
reconnectAction->setVisible(true);
connected = false;
}
void SwitchView::openOptions(void)
{
optionsAction->setEnabled(false);
mapped->setEnabled(false);
mappedAction->setEnabled(false); // kill menu entry...
options->open();
options->setEnabled(true);
options->show();
}
void SwitchView::openMapped(void)
{
if(!connected && !testing && !alwaysopen)
warning();
else if(!mapped->isVisible() && mapped->isEnabled()) {
mappedAction->setEnabled(false);
mapped->open();
}
}
void SwitchView::mappedClosed(void)
{
if(stopping)
return;
mappedAction->setEnabled(true);
}
void SwitchView::optionsClosed(void)
{
if(stopping)
return;
optionsAction->setEnabled(true);
mapped->setEnabled(true);
if(!mapped->isVisible() && connected)
mappedAction->setEnabled(true);
}
void SwitchView::about(void)
{
QString text = tr("Version") + " " VERSION "\n\n";
QWidget *parent = this;
if(mapped->isVisible())
parent = mapped;
mapped->setEnabled(false);
QMessageBox::about(parent, tr("About SwitchView"), text);
mapped->setEnabled(true);
}
void SwitchView::action(QSystemTrayIcon::ActivationReason reason)
{
switch(reason) {
case QSystemTrayIcon::Trigger:
case QSystemTrayIcon::DoubleClick:
if(!connected && !testing) {
warning();
return;
}
mappedAction->setEnabled(false);
if(mapped->isVisible())
mapped->close();
else if(mapped->isEnabled())
mapped->open();
return;
default:
return;
}
}