* do not add a KStandardAction manually to actionCollection()
[kdenetwork.git] / krfb / manageinvitationsdialog.cpp
blobb4e8110b5b793db58092dfa203e2e9840e123d54
1 /* This file is part of the KDE project
2 Copyright (C) 2007 Alessandro Praduroux <pradu@pradu.it>
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8 */
9 #include "manageinvitationsdialog.h"
10 #include "manageinvitationsdialog.moc"
12 #include "personalinvitedialog.h"
13 #include "invitationmanager.h"
14 #include "invitation.h"
15 #include "krfbconfig.h"
16 #include "krfbserver.h"
18 #include <QWidget>
19 #include <QToolTip>
20 #include <QCursor>
21 #include <QDateTime>
22 #include <QNetworkInterface>
24 #include <KStandardGuiItem>
25 #include <KIconLoader>
26 #include <KLocale>
27 #include <KGlobal>
28 #include <KConfigDialog>
29 #include <KMessageBox>
30 #include <KToolInvocation>
32 // settings dialog
33 #include "ui_configtcp.h"
34 #include "ui_configsecurity.h"
36 class TCP: public QWidget, public Ui::TCP {
37 public:
38 TCP(QWidget *parent=0) :QWidget(parent)
40 setupUi(this);
44 class Security: public QWidget, public Ui::Security {
45 public:
46 Security(QWidget *parent=0) :QWidget(parent)
48 setupUi(this);
53 ManageInvitationsDialog::ManageInvitationsDialog(QWidget *parent)
54 : KDialog(parent)
56 setCaption(i18n("Invitation"));
57 setButtons(User1|Close|Help);
58 setHelp(QString(), "krfb");
59 setDefaultButton(NoDefault);
61 setMinimumSize(500, 330);
63 setupUi(mainWidget());
64 pixmapLabel->setPixmap(KIcon("krfb").pixmap(128));
66 setButtonGuiItem( User1, KStandardGuiItem::configure() );
68 connect( helpLabel, SIGNAL( linkActivated ( QString ) ),
69 SLOT( showWhatsthis() ));
70 connect( newPersonalInvitationButton, SIGNAL( clicked() ),
71 SLOT( inviteManually() ));
72 connect( newEmailInvitationButton, SIGNAL( clicked() ),
73 SLOT( inviteByMail() ));
74 connect( InvitationManager::self(), SIGNAL( invitationNumChanged( int )),
75 SLOT( reloadInvitations() ));
76 connect( this, SIGNAL(user1Clicked()),SLOT(showConfiguration()));
77 connect( deleteAllButton, SIGNAL( clicked() ),
78 SLOT( deleteAll() ));
79 connect( deleteOneButton, SIGNAL( clicked() ),
80 SLOT( deleteCurrent() ));
81 connect( invitationWidget, SIGNAL(itemSelectionChanged ()),
82 SLOT( selectionChanged() ));
84 reloadInvitations();
87 ManageInvitationsDialog::~ManageInvitationsDialog()
91 void ManageInvitationsDialog::showWhatsthis()
93 QToolTip::showText(QCursor::pos(),
94 i18n("An invitation creates a one-time password that allows the receiver to connect to your desktop.\n"
95 "It is valid for only one successful connection and will expire after an hour if it has not been used. \n"
96 "When somebody connects to your computer a dialog will appear and ask you for permission.\n"
97 "The connection will not be established before you accept it. In this dialog you can also\nrestrict "
98 "the other person to view your desktop only, without the ability to move your\nmouse pointer or press "
99 "keys.\nIf you want to create a permanent password for Desktop Sharing, allow 'Uninvited Connections' \n"
100 "in the configuration."));
104 void ManageInvitationsDialog::inviteManually()
106 Invitation inv = InvitationManager::self()->addInvitation();
107 PersonalInviteDialog *pid = new PersonalInviteDialog(this);
108 pid->setPassword(inv.password());
109 pid->setExpiration(inv.expirationTime());
110 pid->show();
113 void ManageInvitationsDialog::inviteByMail()
115 int r = KMessageBox::warningContinueCancel(this,
116 i18n("When sending an invitation by email, note that everybody who reads this email "
117 "will be able to connect to your computer for one hour, or until the first "
118 "successful connection took place, whichever comes first. \n"
119 "You should either encrypt the email or at least send it only in a "
120 "secure network, but not over the Internet."),
121 i18n("Send Invitation via Email"),
122 KStandardGuiItem::cont(),
123 KStandardGuiItem::cancel(),
124 "showEmailInvitationWarning");
125 if (r == KMessageBox::Cancel)
126 return;
128 QList<QNetworkInterface> ifl = QNetworkInterface::allInterfaces();
129 QString host;
130 int port = KrfbConfig::port();
131 foreach (const QNetworkInterface &nif, ifl) {
132 if (nif.flags() & QNetworkInterface::IsLoopBack) continue;
133 if (nif.flags() & QNetworkInterface::IsRunning) {
134 host = nif.addressEntries()[0].ip().toString();
138 Invitation inv = InvitationManager::self()->addInvitation();
139 KUrl invUrl(QString("vnc://invitation:%1@%2:%3").arg(inv.password()).arg(host).arg(port));
140 KToolInvocation::invokeMailer(QString(), QString(), QString(),
141 i18n("Desktop Sharing (VNC) invitation"),
142 ki18n("You have been invited to a VNC session. If you have the KDE Remote "
143 "Desktop Connection installed, just click on the link below.\n\n"
144 "%1\n\n"
145 "Otherwise you can use any VNC client with the following parameters:\n\n"
146 "Host: %2:%3\n"
147 "Password: %4\n\n"
148 "For security reasons this invitation will expire at %5.")
149 .subs(invUrl.url())
150 .subs(host)
151 .subs(QString::number(port))
152 .subs(inv.password())
153 .subs(KGlobal::locale()->formatDateTime(inv.expirationTime()))
154 .toString());
158 void ManageInvitationsDialog::reloadInvitations()
160 invitationWidget->clear();
161 KLocale *loc = KGlobal::locale();
162 foreach(const Invitation &inv, InvitationManager::self()->invitations()) {
163 QStringList strs;
164 strs << loc->formatDateTime(inv.creationTime()) << loc->formatDateTime(inv.expirationTime());
165 QTreeWidgetItem *it = new QTreeWidgetItem(strs);
166 invitationWidget->addTopLevelItem(it);
167 it->setData(0,Qt::UserRole+1, inv.creationTime());
169 invitationWidget->resizeColumnToContents(0);
170 deleteAllButton->setEnabled(InvitationManager::self()->activeInvitations() > 0);
173 void ManageInvitationsDialog::showConfiguration()
175 if(KConfigDialog::showDialog("settings"))
176 return;
178 KConfigDialog *dialog = new KConfigDialog(this, "settings", KrfbConfig::self());
179 dialog->addPage(new TCP, i18n("Network"), "network-workgroup");
180 dialog->addPage(new Security, i18n("Security"), "security-high");
181 dialog->setHelp(QString(),"krfb");
182 connect(dialog, SIGNAL(settingsChanged(QString)),KrfbServer::self(),SLOT(updateSettings()));
183 dialog->show();
186 void ManageInvitationsDialog::deleteAll()
188 if (KMessageBox::warningContinueCancel(this,
189 i18n("<qt>Are you sure you want to delete all invitations?</qt>"),
190 i18n("Confirm delete Invitations"),
191 KStandardGuiItem::ok(),
192 KStandardGuiItem::cancel(),
193 QString("krfbdeleteallinv")) !=
194 KMessageBox::Continue)
196 return;
199 InvitationManager::self()->removeAllInvitations();
202 void ManageInvitationsDialog::deleteCurrent()
204 if (KMessageBox::warningContinueCancel(this,
205 i18n("<qt>Are you sure you want to delete this invitation?</qt>"),
206 i18n("Confirm delete Invitations"),
207 KStandardGuiItem::ok(),
208 KStandardGuiItem::cancel(),
209 QString("krfbdeleteoneinv")) !=
210 KMessageBox::Continue)
212 return;
215 // disable updates while deleting items, otherwise the list would invalidate itself
216 disconnect(InvitationManager::self(), SIGNAL(invitationNumChanged(int)),
217 this, SLOT(reloadInvitations()));
219 QList<QTreeWidgetItem *> itl = invitationWidget->selectedItems();
220 foreach(QTreeWidgetItem *itm, itl) {
221 foreach(const Invitation &inv, InvitationManager::self()->invitations()) {
222 if (inv.creationTime() == itm->data(0,Qt::UserRole+1)) {
223 InvitationManager::self()->removeInvitation(inv);
228 // update it manually
229 reloadInvitations();
231 connect(InvitationManager::self(), SIGNAL(invitationNumChanged(int)),
232 SLOT(reloadInvitations()));
235 void ManageInvitationsDialog::selectionChanged()
237 deleteOneButton->setEnabled(invitationWidget->selectedItems().size() > 0);