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

« back to all changes in this revision

Viewing changes to 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-2007 - INL
3
 
 ** Written by Eric Leblond <eric.leblond@inl.fr>
4
 
 **            Vincent Deffontaines <vincent@inl.fr>
5
 
 ** INL http://www.inl.fr/
6
 
 **
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.
10
 
 **
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.
15
 
 **
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.
19
 
 */
20
 
 
21
 
#include <nuclient.h>
22
 
#include <QtGui>
23
 
 
24
 
#include "systray.h"
25
 
#include "systray.moc"
26
 
#include "auth_dlg.h"
27
 
#include "preferences.h"
28
 
 
29
 
extern nuclient_error_t *err;
30
 
 
31
 
/*
32
 
 * See http://doc.trolltech.com/4.3/mainwindows-application.html
33
 
 */
34
 
 
35
 
NuAppSystray::NuAppSystray() : timer(this)
36
 
{
37
 
        QCoreApplication* app = QCoreApplication::instance();
38
 
        parse_cmdline_options(app->argc(), app->argv());
39
 
 
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()));
44
 
 
45
 
        // Create actions
46
 
        //  connect
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()));
51
 
 
52
 
        //  disconnect
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()));
57
 
 
58
 
        //  preferences
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()));
63
 
 
64
 
        //  about
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()));
69
 
 
70
 
        //  exit
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()));
75
 
 
76
 
        createTrayIcon();
77
 
        settings.sync();
78
 
 
79
 
        // Launched for the first time -> display the preferences menu
80
 
        if(settings.value("hostname").toString() == "")
81
 
                pref_dlg->show();
82
 
        else
83
 
                auth_dlg->show();
84
 
}
85
 
 
86
 
NuAppSystray::~NuAppSystray()
87
 
{
88
 
        stop_auth();
89
 
}
90
 
 
91
 
void NuAppSystray::createTrayIcon()
92
 
{
93
 
        if ( ! QSystemTrayIcon::isSystemTrayAvailable() ) {
94
 
                fprintf(stderr, "Your environnement doesn't support a system tray icon...\n");
95
 
                fprintf(stderr, "Exiting ...\n");
96
 
                exit(EXIT_FAILURE);
97
 
        }
98
 
 
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);
109
 
 
110
 
        QIcon icon = QIcon(":/images/nuapplet2-stopped.png");
111
 
        setIcon(icon);
112
 
        show();
113
 
        setContextMenu(trayIconMenu);
114
 
}
115
 
 
116
 
void NuAppSystray::usage(void)
117
 
{
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");
130
 
        exit(EXIT_FAILURE);
131
 
}
132
 
 
133
 
void NuAppSystray::parse_cmdline_options(int argc, char **argv)
134
 
{
135
 
        /* Parse all command line arguments */
136
 
        int ch;
137
 
        int opterr = 0;
138
 
        while ((ch = getopt(argc, argv, "H:p:U:C:K:A:")) != -1) {
139
 
                switch (ch) {
140
 
                case 'H':
141
 
                        settings.setValue("hostname", QString(optarg));
142
 
                        break;
143
 
                case 'p':
144
 
                        settings.setValue("port", QString(optarg));
145
 
                        break;
146
 
                case 'U':
147
 
                        settings.setValue("username", QString(optarg));
148
 
                        break;
149
 
                case 'C':
150
 
                        settings.setValue("cert", QString(optarg));
151
 
                        break;
152
 
                case 'K':
153
 
                        settings.setValue("key", QString(optarg));
154
 
                        break;
155
 
                case 'A':
156
 
                        settings.setValue("ca", QString(optarg));
157
 
                        break;
158
 
                default:
159
 
                        usage();
160
 
                }
161
 
        }
162
 
}
163
 
 
164
 
void NuAppSystray::onClick(QSystemTrayIcon::ActivationReason reason)
165
 
{
166
 
        if( reason == QSystemTrayIcon::Trigger && !timer.isRunning())
167
 
        {
168
 
                // Display the authentication dialog
169
 
                emit showAuthDlg();
170
 
        }
171
 
}
172
 
 
173
 
void NuAppSystray::start_auth(QString username, QString password)
174
 
{
175
 
        if(!timer.isRunning())
176
 
        {
177
 
                timer.setUserInfos(username, password);
178
 
                timer.start();
179
 
        }
180
 
}
181
 
 
182
 
void NuAppSystray::stop_auth()
183
 
{
184
 
        // Close Nuauth connection
185
 
        timer.askStop();
186
 
        timer.wait(ULONG_MAX);
187
 
}
188
 
 
189
 
void NuAppSystray::tray_set_connected()
190
 
{
191
 
        disconnectAct->setEnabled(true);
192
 
        connectAct->setEnabled(false);
193
 
 
194
 
        //printf("Icon running\n");
195
 
        QIcon icon = QIcon(":/images/nuapplet2-running.png");
196
 
        setIcon(icon);
197
 
}
198
 
 
199
 
void NuAppSystray::tray_set_trying()
200
 
{
201
 
        QIcon icon = QIcon(":/images/nuapplet2-trying.png");
202
 
        setIcon(icon);
203
 
        //printf("Icon trying\n");
204
 
}
205
 
 
206
 
void NuAppSystray::tray_set_stopped()
207
 
{
208
 
        // Enable / disable menu entries
209
 
        disconnectAct->setEnabled(false);
210
 
        connectAct->setEnabled(true);
211
 
 
212
 
        // Change the icon in the systray
213
 
        QIcon icon = QIcon(":/images/nuapplet2-stopped.png");
214
 
        setIcon(icon);
215
 
        //printf("Icon stopped\n");
216
 
}
217
 
 
218
 
void NuAppSystray::tray_report_error()
219
 
{
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));
222
 
}
223
 
 
224
 
void NuAppSystray::tray_report_connected()
225
 
{
226
 
        showMessage(tr("Connection succeeded"), tr("You are now connected to ") + settings.value("hostname").toString(), Information,3000 );
227
 
        printf("Connnected\n");
228
 
}
229
 
 
230
 
void NuAppSystray::tray_report_disconnected()
231
 
{
232
 
        showMessage(tr("Warning"), tr("You have been disconnected\nReason: ") + QString(nu_client_strerror(err)), Warning,5000 );
233
 
        printf("Disconnected\n");
234
 
}
235
 
 
236
 
void NuAppSystray::popup_about()
237
 
{
238
 
        QMessageBox msgBox;
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\"/>");
243
 
        msgBox.exec();
244
 
}
245