2
** Copyright 2004-2010 - EdenWall Technologies
3
** Written by Eric Leblond <eric.leblond@inl.fr>
4
** Vincent Deffontaines <vincent@inl.fr>
5
** Pierre Chifflier <chifflier@edenwall.com>
6
** INL http://www.inl.fr/
8
** This program is free software; you can redistribute it and/or modify
9
** it under the terms of the GNU General Public License as published by
10
** the Free Software Foundation, version 2 of the License.
12
** This program is distributed in the hope that it will be useful,
13
** but WITHOUT ANY WARRANTY; without even the implied warranty of
14
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
** GNU General Public License for more details.
17
** You should have received a copy of the GNU General Public License
18
** along with this program; if not, write to the Free Software
19
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26
#include "systray.moc"
28
#include "preferences.h"
32
typedef void (*log_callback_t)(int area, int priority, char *format, va_list args);
34
extern log_callback_t nubase_log_set_callback(log_callback_t cb);
36
extern int log_engine;
37
extern int debug_level;
40
static NuLogsWindow *_logs_window = NULL;
42
static void log_callback(int area, int priority, char *format, va_list args)
44
if (_logs_window != NULL) {
45
_logs_window->addLine(area, priority, format, args);
50
* See http://doc.trolltech.com/4.3/mainwindows-application.html
53
NuAppSystray::NuAppSystray() : timer(this)
55
QCoreApplication* app = QCoreApplication::instance();
56
parse_cmdline_options(app->argc(), app->argv());
58
logs_dlg = new NuLogsWindow();
59
_logs_window = logs_dlg;
60
pref_dlg = new NuAppPreferences();
61
auth_dlg = new NuAppAuthDialog(this);
62
QObject::connect(this, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(onClick(QSystemTrayIcon::ActivationReason)));
63
QObject::connect(this, SIGNAL(showAuthDlg()), auth_dlg, SLOT(show()));
67
connectAct = new QAction(tr("&Connect"), this);
68
connectAct->setStatusTip(tr("Connect to the authentication server"));
69
connect(connectAct, SIGNAL(triggered()), auth_dlg, SLOT(show()));
72
disconnectAct = new QAction(tr("&Disconnect"), this);
73
disconnectAct->setStatusTip(tr("Disconnect from the authentication server"));
74
connect(disconnectAct, SIGNAL(triggered()), this, SLOT(stop_auth()));
77
preferencesAct = new QAction(tr("&Preferences"), this);
78
preferencesAct->setStatusTip(tr("Preferences"));
79
connect(preferencesAct, SIGNAL(triggered()), pref_dlg, SLOT(show()));
82
logsAct = new QAction(tr("&Log messages"), this);
83
logsAct->setStatusTip(tr("Show log messages"));
84
connect(logsAct, SIGNAL(triggered()), logs_dlg, SLOT(show()));
87
aboutAct = new QAction(tr("&About"), this);
88
aboutAct->setStatusTip(tr("About"));
89
connect(aboutAct, SIGNAL(triggered()), this, SLOT(popup_about()));
92
exitAct = new QAction(tr("&Quit"), this);
93
exitAct->setStatusTip(tr("Quit application"));
94
connect(exitAct, SIGNAL(triggered()), qApp, SLOT(quit()));
99
// Display the authentication dialog
102
nubase_log_set_callback(log_callback);
106
#ifdef HAVE_DEFAULT_TLS_FUNC
107
// Load defaults settings
108
if(strcmp(settings.value("hostname").toString().toStdString().c_str(), "") == 0)
110
printf("Loading default params\n");
111
//suppress_fqdn_verif |= nu_client_default_suppress_fqdn_verif();
112
const char* default_cafile = nu_client_default_tls_ca();
114
settings.setValue("ca", default_cafile);
115
const char* default_cert = nu_client_default_tls_cert();
117
settings.setValue("cert", default_cert);
118
const char* default_key = nu_client_default_tls_key();
120
settings.setValue("key", default_key);
121
const char* default_crl = nu_client_default_tls_crl();
123
settings.setValue("crl", default_crl);
124
const int default_suppress_fqdn = nu_client_default_suppress_fqdn_verif();
125
if (default_suppress_fqdn)
126
settings.setValue("suppress_fqdn", default_suppress_fqdn);
127
if(default_cafile || default_cert || default_key)
128
settings.setValue("use_certificate", true);
132
#ifdef HAVE_DEFAULT_HOSTNAME_FUNC
133
// Load defaults settings
134
if(strcmp(settings.value("hostname").toString().toStdString().c_str(), "") == 0)
136
const char* nuauth_ip = nu_client_default_hostname();
137
if(nuauth_ip != NULL)
138
settings.setValue("hostname", nuauth_ip);
140
const char* nuauth_port = nu_client_default_port();
141
if(nuauth_port != NULL)
142
settings.setValue("port", nuauth_port);
144
settings.setValue("port", DEFAULT_NUAUTH_PORT);
148
#if defined(HAVE_DEFAULT_HOSTNAME_FUNC) || !defined(HAVE_NUAUTH_IP)
149
if(strcmp(settings.value("hostname").toString().toStdString().c_str(), "") == 0)
154
NuAppSystray::~NuAppSystray()
159
void NuAppSystray::createTrayIcon()
161
if ( ! QSystemTrayIcon::isSystemTrayAvailable() ) {
162
fprintf(stderr, "Your environnement doesn't support a system tray icon...\n");
163
fprintf(stderr, "Exiting ...\n");
167
trayIconMenu = new QMenu(/*this*/);
168
trayIconMenu->addAction(connectAct);
169
connectAct->setEnabled(true);
170
trayIconMenu->addAction(disconnectAct);
171
disconnectAct->setEnabled(false);
172
trayIconMenu->addAction(logsAct);
173
trayIconMenu->addAction(preferencesAct);
174
trayIconMenu->addSeparator();
175
trayIconMenu->addAction(aboutAct);
176
trayIconMenu->addSeparator();
177
trayIconMenu->addAction(exitAct);
179
QIcon icon = QIcon(":/images/nuapplet2-stopped.png");
182
setContextMenu(trayIconMenu);
185
void NuAppSystray::usage(void)
187
fprintf(stderr, "usage: nuapplet2 [options...]\n");
188
fprintf(stderr, "\n");
189
fprintf(stderr, "Options:\n");
190
fprintf(stderr, " -H HOSTNAME: nuauth host address\n");
191
fprintf(stderr, " -p PORT: nuauth port number\n");
192
fprintf(stderr, " -U USERNAME: set default username\n");
193
fprintf(stderr, "\n");
194
fprintf(stderr, "Certificate options:\n");
195
fprintf(stderr, " -C CERTFILE: certificate filename\n");
196
fprintf(stderr, " -A AUTHFILE: authority certificate filename\n");
197
fprintf(stderr, " -K KEYFILE: key filename\n");
198
fprintf(stderr, " -R CRLFILE: crl filename\n");
199
fprintf(stderr, " -N: suppress error if server FQDN does not match certificate CN.\n");
200
fprintf(stderr, "\n");
204
void NuAppSystray::parse_cmdline_options(int argc, char **argv)
206
/* Parse all command line arguments */
209
while ((ch = getopt(argc, argv, "H:p:U:C:K:A:R:N")) != -1) {
212
settings.setValue("hostname", QString(optarg));
215
settings.setValue("port", QString(optarg));
218
settings.setValue("username", QString(optarg));
221
settings.setValue("cert", QString(optarg));
224
settings.setValue("key", QString(optarg));
227
settings.setValue("ca", QString(optarg));
230
settings.setValue("crl", QString(optarg));
233
settings.setValue("suppress_fqdn", 1);
240
void NuAppSystray::onClick(QSystemTrayIcon::ActivationReason reason)
242
if( reason == QSystemTrayIcon::Trigger && !timer.isRunning())
244
// Display the authentication dialog
249
void NuAppSystray::start_auth(QString username, QString password)
251
if(!timer.isRunning())
253
timer.setUserInfos(username, password);
255
showMessage(tr("Warning"), tr("Nuapplet isn't totally strict about TLS verifications.\nRun it from a terminal to see any warning that could happen."), Warning, 5000);
259
void NuAppSystray::stop_auth()
261
// Close Nuauth connection
263
timer.wait(ULONG_MAX);
266
void NuAppSystray::tray_set_connected()
268
disconnectAct->setEnabled(true);
269
connectAct->setEnabled(false);
271
//printf("Icon running\n");
272
QIcon icon = QIcon(":/images/nuapplet2-running.png");
276
void NuAppSystray::tray_set_trying()
278
QIcon icon = QIcon(":/images/nuapplet2-trying.png");
280
//printf("Icon trying\n");
283
void NuAppSystray::tray_set_stopped()
285
// Enable / disable menu entries
286
disconnectAct->setEnabled(false);
287
connectAct->setEnabled(true);
289
// Change the icon in the systray
290
QIcon icon = QIcon(":/images/nuapplet2-stopped.png");
292
//printf("Icon stopped\n");
298
const char *wrap_error_message(const char *str)
300
if (strncmp(str,"A TLS packet with unexpected length was received.",49) == 0) {
301
return("Access denied");
303
if (strncmp(str,"Success.",9) == 0) {
309
void NuAppSystray::tray_report_error()
311
QString msg = tr("Unable to connect.\nCheck your username and your password are correct.");
312
QString str_err = "";
313
if(wrap_error_message(timer.GetNuclientError()))
315
str_err = QString("\nError is: ") + QString(wrap_error_message(timer.GetNuclientError()));
318
QMessageBox::critical(NULL, tr("Error"), msg + str_err, QMessageBox::Ok);
321
void NuAppSystray::tray_report_connected()
323
showMessage(tr("Connection succeeded"), tr("You are now connected to ") + settings.value("hostname").toString(), Information,3000 );
324
printf("Connnected\n");
327
void NuAppSystray::tray_report_disconnected()
329
showMessage(tr("Warning"), tr("You have been disconnected\nReason: ") + QString(timer.GetNuclientError()), Warning,5000 );
330
printf("Disconnected\n");
333
void NuAppSystray::popup_about()
336
msgBox.setWindowTitle(tr("About"));
337
msgBox.setIcon(QMessageBox::Information);
338
msgBox.setTextFormat(Qt::RichText);
339
msgBox.setText(tr("NuApplet %1 edited by <a href=\"http://www.edenwall.com/\">EdenWall Technologies</a>.<br>Distributed under the <a href=\"http://www.gnu.org/copyleft/gpl.html\">GPL</a><br>").arg(PROJECT_VERSION_STR) + "<img src=\":/images/edenwall.png\"/>");