2
** Copyright 2004-2007 - INL
3
** Written by Eric Leblond <eric.leblond@inl.fr>
4
** Vincent Deffontaines <vincent@inl.fr>
5
** INL http://www.inl.fr/
7
** This program is free software; you can redistribute it and/or modify
8
** it under the terms of the GNU General Public License as published by
9
** the Free Software Foundation, version 2 of the License.
11
** This program is distributed in the hope that it will be useful,
12
** but WITHOUT ANY WARRANTY; without even the implied warranty of
13
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
** GNU General Public License for more details.
16
** You should have received a copy of the GNU General Public License
17
** along with this program; if not, write to the Free Software
18
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
#include "systray.moc"
27
#include "preferences.h"
29
extern nuclient_error_t *err;
32
* See http://doc.trolltech.com/4.3/mainwindows-application.html
35
NuAppSystray::NuAppSystray() : timer(this)
37
QCoreApplication* app = QCoreApplication::instance();
38
parse_cmdline_options(app->argc(), app->argv());
40
pref_dlg = new NuAppPreferences();
41
auth_dlg = new NuAppAuthDialog(this);
42
QObject::connect(this, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(onClick(QSystemTrayIcon::ActivationReason)));
43
QObject::connect(this, SIGNAL(showAuthDlg()), auth_dlg, SLOT(show()));
47
connectAct = new QAction(tr("&Connect"), this);
48
connectAct->setShortcut(tr("Ctrl+C"));
49
connectAct->setStatusTip(tr("Connect to the authentication server"));
50
connect(connectAct, SIGNAL(triggered()), auth_dlg, SLOT(show()));
53
disconnectAct = new QAction(tr("&Disconnect"), this);
54
disconnectAct->setShortcut(tr("Ctrl+D"));
55
disconnectAct->setStatusTip(tr("Disconnect from the authentication server"));
56
connect(disconnectAct, SIGNAL(triggered()), this, SLOT(stop_auth()));
59
preferencesAct = new QAction(tr("&Preferences"), this);
60
preferencesAct->setShortcut(tr("Ctrl+C"));
61
preferencesAct->setStatusTip(tr("Preferences"));
62
connect(preferencesAct, SIGNAL(triggered()), pref_dlg, SLOT(show()));
65
aboutAct = new QAction(tr("&About"), this);
66
aboutAct->setShortcut(tr("Ctrl+A"));
67
aboutAct->setStatusTip(tr("About"));
68
connect(aboutAct, SIGNAL(triggered()), this, SLOT(popup_about()));
71
exitAct = new QAction(tr("&Quit"), this);
72
exitAct->setShortcut(tr("Ctrl+Q"));
73
exitAct->setStatusTip(tr("Quit application"));
74
connect(exitAct, SIGNAL(triggered()), qApp, SLOT(quit()));
79
// Launched for the first time -> display the preferences menu
80
if(settings.value("hostname").toString() == "")
86
NuAppSystray::~NuAppSystray()
91
void NuAppSystray::createTrayIcon()
93
if ( ! QSystemTrayIcon::isSystemTrayAvailable() ) {
94
fprintf(stderr, "Your environnement doesn't support a system tray icon...\n");
95
fprintf(stderr, "Exiting ...\n");
99
trayIconMenu = new QMenu(/*this*/);
100
trayIconMenu->addAction(connectAct);
101
connectAct->setEnabled(true);
102
trayIconMenu->addAction(disconnectAct);
103
disconnectAct->setEnabled(false);
104
trayIconMenu->addAction(preferencesAct);
105
trayIconMenu->addSeparator();
106
trayIconMenu->addAction(aboutAct);
107
trayIconMenu->addSeparator();
108
trayIconMenu->addAction(exitAct);
110
QIcon icon = QIcon(":/images/nuapplet2-stopped.png");
113
setContextMenu(trayIconMenu);
116
void NuAppSystray::usage(void)
118
fprintf(stderr, "usage: nuapplet2 [options...]\n");
119
fprintf(stderr, "\n");
120
fprintf(stderr, "Options:\n");
121
fprintf(stderr, " -H HOSTNAME: nuauth host address\n");
122
fprintf(stderr, " -p PORT: nuauth port number\n");
123
fprintf(stderr, " -U USERNAME: set default username\n");
124
fprintf(stderr, "\n");
125
fprintf(stderr, "Certificate options:\n");
126
fprintf(stderr, " -C CERTFILE: certificate filename\n");
127
fprintf(stderr, " -A AUTHFILE: authority certificate filename\n");
128
fprintf(stderr, " -K KEYFILE: key filename\n");
129
fprintf(stderr, "\n");
133
void NuAppSystray::parse_cmdline_options(int argc, char **argv)
135
/* Parse all command line arguments */
138
while ((ch = getopt(argc, argv, "H:p:U:C:K:A:")) != -1) {
141
settings.setValue("hostname", QString(optarg));
144
settings.setValue("port", QString(optarg));
147
settings.setValue("username", QString(optarg));
150
settings.setValue("cert", QString(optarg));
153
settings.setValue("key", QString(optarg));
156
settings.setValue("ca", QString(optarg));
164
void NuAppSystray::onClick(QSystemTrayIcon::ActivationReason reason)
166
if( reason == QSystemTrayIcon::Trigger && !timer.isRunning())
168
// Display the authentication dialog
173
void NuAppSystray::start_auth(QString username, QString password)
175
if(!timer.isRunning())
177
timer.setUserInfos(username, password);
182
void NuAppSystray::stop_auth()
184
// Close Nuauth connection
186
timer.wait(ULONG_MAX);
189
void NuAppSystray::tray_set_connected()
191
disconnectAct->setEnabled(true);
192
connectAct->setEnabled(false);
194
//printf("Icon running\n");
195
QIcon icon = QIcon(":/images/nuapplet2-running.png");
199
void NuAppSystray::tray_set_trying()
201
QIcon icon = QIcon(":/images/nuapplet2-trying.png");
203
//printf("Icon trying\n");
206
void NuAppSystray::tray_set_stopped()
208
// Enable / disable menu entries
209
disconnectAct->setEnabled(false);
210
connectAct->setEnabled(true);
212
// Change the icon in the systray
213
QIcon icon = QIcon(":/images/nuapplet2-stopped.png");
215
//printf("Icon stopped\n");
218
void NuAppSystray::tray_report_error()
220
QMessageBox::critical(NULL, tr("Error"), tr("Unable to connect.\nCheck your username and your password are alright.\nError was:\n") + QString(nu_client_strerror(err)), QMessageBox::Ok);
221
printf("Error: %s\n", nu_client_strerror(err));
224
void NuAppSystray::tray_report_connected()
226
showMessage(tr("Connection succeeded"), tr("You are now connected to ") + settings.value("hostname").toString(), Information,3000 );
227
printf("Connnected\n");
230
void NuAppSystray::tray_report_disconnected()
232
showMessage(tr("Warning"), tr("You have been disconnected\nReason: ") + QString(nu_client_strerror(err)), Warning,5000 );
233
printf("Disconnected\n");
236
void NuAppSystray::popup_about()
239
msgBox.setWindowTitle(tr("About"));
240
msgBox.setIcon(QMessageBox::Information);
241
msgBox.setTextFormat(Qt::RichText);
242
msgBox.setText(tr("NuApplet 2.0 edited by <a href=\"http://www.inl.fr/\">INL</a>.<br>Distributed under the <a href=\"http://www.gnu.org/copyleft/gpl.html\">GPL</a><br>") + "<img src=\"" + QString(DATA_DIR) + "inl.png\"/>");