~ubuntu-branches/ubuntu/natty/nuapplet/natty

« back to all changes in this revision

Viewing changes to src/systray.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Pierre Chifflier
  • Date: 2010-05-17 15:15:59 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20100517151559-lf4zajscmlwf2yef
Tags: 2.3.0-1
* Imported Upstream version 2.3.0
  Add support for new (>= 2.4) libnuclient API (Closes: #573667)
* Urgency high, RC bug
* Bump standards version to 3.8.4
* Update homepage location
* Update dependencies on libnuclient and libnussl >= 2.4.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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/
 
7
 **
 
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.
 
11
 **
 
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.
 
16
 **
 
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.
 
20
 */
 
21
 
 
22
#include <nuclient.h>
 
23
#include <QtGui>
 
24
 
 
25
#include "systray.h"
 
26
#include "systray.moc"
 
27
#include "auth_dlg.h"
 
28
#include "preferences.h"
 
29
#include "logs.h"
 
30
 
 
31
extern "C" {
 
32
typedef void (*log_callback_t)(int area, int priority, char *format, va_list args);
 
33
 
 
34
extern log_callback_t nubase_log_set_callback(log_callback_t cb);
 
35
 
 
36
extern int log_engine;
 
37
extern int debug_level;
 
38
}
 
39
 
 
40
static NuLogsWindow *_logs_window = NULL;
 
41
 
 
42
static void log_callback(int area, int priority, char *format, va_list args)
 
43
{
 
44
        if (_logs_window != NULL) {
 
45
                _logs_window->addLine(area, priority, format, args);
 
46
        }
 
47
}
 
48
 
 
49
/*
 
50
 * See http://doc.trolltech.com/4.3/mainwindows-application.html
 
51
 */
 
52
 
 
53
NuAppSystray::NuAppSystray() : timer(this)
 
54
{
 
55
        QCoreApplication* app = QCoreApplication::instance();
 
56
        parse_cmdline_options(app->argc(), app->argv());
 
57
 
 
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()));
 
64
 
 
65
        // Create actions
 
66
        //  connect
 
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()));
 
70
 
 
71
        //  disconnect
 
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()));
 
75
 
 
76
        //  preferences
 
77
        preferencesAct = new QAction(tr("&Preferences"), this);
 
78
        preferencesAct->setStatusTip(tr("Preferences"));
 
79
        connect(preferencesAct, SIGNAL(triggered()), pref_dlg, SLOT(show()));
 
80
 
 
81
        //  logs
 
82
        logsAct = new QAction(tr("&Log messages"), this);
 
83
        logsAct->setStatusTip(tr("Show log messages"));
 
84
        connect(logsAct, SIGNAL(triggered()), logs_dlg, SLOT(show()));
 
85
 
 
86
        //  about
 
87
        aboutAct = new QAction(tr("&About"), this);
 
88
        aboutAct->setStatusTip(tr("About"));
 
89
        connect(aboutAct, SIGNAL(triggered()), this, SLOT(popup_about()));
 
90
 
 
91
        //  exit
 
92
        exitAct = new QAction(tr("&Quit"), this);
 
93
        exitAct->setStatusTip(tr("Quit application"));
 
94
        connect(exitAct, SIGNAL(triggered()), qApp, SLOT(quit()));
 
95
 
 
96
        createTrayIcon();
 
97
        settings.sync();
 
98
 
 
99
        // Display the authentication dialog
 
100
        auth_dlg->show();
 
101
 
 
102
        nubase_log_set_callback(log_callback);
 
103
        log_engine = 3;
 
104
        debug_level = 9;
 
105
 
 
106
#ifdef HAVE_DEFAULT_TLS_FUNC
 
107
        // Load defaults settings
 
108
        if(strcmp(settings.value("hostname").toString().toStdString().c_str(), "") == 0)
 
109
        {
 
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();
 
113
                if (default_cafile)
 
114
                        settings.setValue("ca", default_cafile);
 
115
                const char* default_cert = nu_client_default_tls_cert();
 
116
                if (default_cert)
 
117
                        settings.setValue("cert", default_cert);
 
118
                const char* default_key = nu_client_default_tls_key();
 
119
                if (default_key)
 
120
                        settings.setValue("key", default_key);
 
121
                const char* default_crl = nu_client_default_tls_crl();
 
122
                if (default_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);
 
129
        }
 
130
#endif
 
131
 
 
132
#ifdef HAVE_DEFAULT_HOSTNAME_FUNC
 
133
        // Load defaults settings
 
134
        if(strcmp(settings.value("hostname").toString().toStdString().c_str(), "") == 0)
 
135
        {
 
136
                const char* nuauth_ip = nu_client_default_hostname();
 
137
                if(nuauth_ip != NULL)
 
138
                        settings.setValue("hostname", nuauth_ip);
 
139
 
 
140
                const char* nuauth_port = nu_client_default_port();
 
141
                if(nuauth_port != NULL)
 
142
                        settings.setValue("port", nuauth_port);
 
143
                else
 
144
                        settings.setValue("port", DEFAULT_NUAUTH_PORT);
 
145
        }
 
146
#endif
 
147
 
 
148
#if defined(HAVE_DEFAULT_HOSTNAME_FUNC) || !defined(HAVE_NUAUTH_IP)
 
149
        if(strcmp(settings.value("hostname").toString().toStdString().c_str(), "") == 0)
 
150
                pref_dlg->show();
 
151
#endif
 
152
}
 
153
 
 
154
NuAppSystray::~NuAppSystray()
 
155
{
 
156
        stop_auth();
 
157
}
 
158
 
 
159
void NuAppSystray::createTrayIcon()
 
160
{
 
161
        if ( ! QSystemTrayIcon::isSystemTrayAvailable() ) {
 
162
                fprintf(stderr, "Your environnement doesn't support a system tray icon...\n");
 
163
                fprintf(stderr, "Exiting ...\n");
 
164
                exit(EXIT_FAILURE);
 
165
        }
 
166
 
 
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);
 
178
 
 
179
        QIcon icon = QIcon(":/images/nuapplet2-stopped.png");
 
180
        setIcon(icon);
 
181
        show();
 
182
        setContextMenu(trayIconMenu);
 
183
}
 
184
 
 
185
void NuAppSystray::usage(void)
 
186
{
 
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");
 
201
        exit(EXIT_FAILURE);
 
202
}
 
203
 
 
204
void NuAppSystray::parse_cmdline_options(int argc, char **argv)
 
205
{
 
206
        /* Parse all command line arguments */
 
207
        int ch;
 
208
        int opterr = 0;
 
209
        while ((ch = getopt(argc, argv, "H:p:U:C:K:A:R:N")) != -1) {
 
210
                switch (ch) {
 
211
                case 'H':
 
212
                        settings.setValue("hostname", QString(optarg));
 
213
                        break;
 
214
                case 'p':
 
215
                        settings.setValue("port", QString(optarg));
 
216
                        break;
 
217
                case 'U':
 
218
                        settings.setValue("username", QString(optarg));
 
219
                        break;
 
220
                case 'C':
 
221
                        settings.setValue("cert", QString(optarg));
 
222
                        break;
 
223
                case 'K':
 
224
                        settings.setValue("key", QString(optarg));
 
225
                        break;
 
226
                case 'A':
 
227
                        settings.setValue("ca", QString(optarg));
 
228
                        break;
 
229
                case 'R':
 
230
                        settings.setValue("crl", QString(optarg));
 
231
                        break;
 
232
                case 'N':
 
233
                        settings.setValue("suppress_fqdn", 1);
 
234
                default:
 
235
                        usage();
 
236
                }
 
237
        }
 
238
}
 
239
 
 
240
void NuAppSystray::onClick(QSystemTrayIcon::ActivationReason reason)
 
241
{
 
242
        if( reason == QSystemTrayIcon::Trigger && !timer.isRunning())
 
243
        {
 
244
                // Display the authentication dialog
 
245
                emit showAuthDlg();
 
246
        }
 
247
}
 
248
 
 
249
void NuAppSystray::start_auth(QString username, QString password)
 
250
{
 
251
        if(!timer.isRunning())
 
252
        {
 
253
                timer.setUserInfos(username, password);
 
254
                timer.start();
 
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);
 
256
        }
 
257
}
 
258
 
 
259
void NuAppSystray::stop_auth()
 
260
{
 
261
        // Close Nuauth connection
 
262
        timer.askStop();
 
263
        timer.wait(ULONG_MAX);
 
264
}
 
265
 
 
266
void NuAppSystray::tray_set_connected()
 
267
{
 
268
        disconnectAct->setEnabled(true);
 
269
        connectAct->setEnabled(false);
 
270
 
 
271
        //printf("Icon running\n");
 
272
        QIcon icon = QIcon(":/images/nuapplet2-running.png");
 
273
        setIcon(icon);
 
274
}
 
275
 
 
276
void NuAppSystray::tray_set_trying()
 
277
{
 
278
        QIcon icon = QIcon(":/images/nuapplet2-trying.png");
 
279
        setIcon(icon);
 
280
        //printf("Icon trying\n");
 
281
}
 
282
 
 
283
void NuAppSystray::tray_set_stopped()
 
284
{
 
285
        // Enable / disable menu entries
 
286
        disconnectAct->setEnabled(false);
 
287
        connectAct->setEnabled(true);
 
288
 
 
289
        // Change the icon in the systray
 
290
        QIcon icon = QIcon(":/images/nuapplet2-stopped.png");
 
291
        setIcon(icon);
 
292
        //printf("Icon stopped\n");
 
293
}
 
294
 
 
295
/**
 
296
 * Stupid wrapper
 
297
 */
 
298
const char *wrap_error_message(const char *str)
 
299
{
 
300
        if (strncmp(str,"A TLS packet with unexpected length was received.",49) == 0) {
 
301
                return("Access denied");
 
302
        }
 
303
        if (strncmp(str,"Success.",9) == 0) {
 
304
                return NULL;
 
305
        }
 
306
        return(str);
 
307
}
 
308
 
 
309
void NuAppSystray::tray_report_error()
 
310
{
 
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()))
 
314
        {
 
315
                str_err = QString("\nError is: ") + QString(wrap_error_message(timer.GetNuclientError()));
 
316
        }
 
317
        logs_dlg->show();
 
318
        QMessageBox::critical(NULL, tr("Error"), msg + str_err, QMessageBox::Ok);
 
319
}
 
320
 
 
321
void NuAppSystray::tray_report_connected()
 
322
{
 
323
        showMessage(tr("Connection succeeded"), tr("You are now connected to ") + settings.value("hostname").toString(), Information,3000 );
 
324
        printf("Connnected\n");
 
325
}
 
326
 
 
327
void NuAppSystray::tray_report_disconnected()
 
328
{
 
329
        showMessage(tr("Warning"), tr("You have been disconnected\nReason: ") + QString(timer.GetNuclientError()), Warning,5000 );
 
330
        printf("Disconnected\n");
 
331
}
 
332
 
 
333
void NuAppSystray::popup_about()
 
334
{
 
335
        QMessageBox msgBox;
 
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\"/>");
 
340
        msgBox.exec();
 
341
}
 
342