- Documentation
[qt_rdw125klib.git] / RFIDScreenLocker / screenlocker.cpp
blob51813e36971cee080a64fcfab94ea1ced552ce79
1 /***************************************************************************
2 * Copyright (C) 2007 by Juan González Aguilera *
3 * (kde_devel@opsiland.info) *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU Library General Public License as *
7 * published by the Free Software Foundation; either version 2 of the *
8 * License, or (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU Library General Public *
16 * License along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 #include "screenlocker.h"
21 #include <QDebug>
22 #include <QSystemTrayIcon>
23 #include <QIcon>
24 #include <QMenu>
25 #include <QAction>
26 #include <QTimer>
27 #include <QProcess>
28 #include <unistd.h>
29 #include <pwd.h>
31 #define TRAY_ICON_NORMAL ":/images/oxygen-system-lock-screen.svg"
32 #define TRAY_ICON_BAD ":/images/bad.svg"
34 ScreenLocker::ScreenLocker(QWidget *parent)
35 : QWidget(parent),
36 trayIcon(new QSystemTrayIcon(this)),
37 contextMenu(new QMenu(this)),
38 enableAction(0),
39 requirePasswordAction(0),
40 closeAction(0),
41 settings(new QSettings("opsiland","RFIDScreenLocker",this)),
42 keepLockedTimer(new QTimer(this))
44 keepLockedTimer->setInterval(50);
45 QObject::connect(keepLockedTimer,SIGNAL(timeout()),this,SLOT(slotKeepLocked()));
47 enableAction = contextMenu->addAction("Enabled");
48 enableAction->setCheckable(true);
49 enableAction->setChecked(settings->value("enabled",true).toBool());
50 QObject::connect(enableAction,SIGNAL(toggled(bool)),this,SLOT(enableToggled(bool)));
51 QObject::connect(enableAction,SIGNAL(toggled(bool)),this,SLOT(updateTrayTooltip()));
53 requirePasswordAction = contextMenu->addAction("Password required");
54 requirePasswordAction->setToolTip("Indicates if the user will have to supply a password to unlock the screen");
55 requirePasswordAction->setCheckable(true);
56 requirePasswordAction->setChecked(settings->value("passwordRequired",true).toBool());
57 QObject::connect(requirePasswordAction,SIGNAL(toggled(bool)),this,SLOT(updateTrayTooltip()));
59 closeAction = contextMenu->addAction("Close");
60 QObject::connect(closeAction,SIGNAL(triggered(bool)),this,SLOT(close()));
62 trayIcon->setContextMenu(contextMenu);
63 trayIcon->setIcon(QIcon(TRAY_ICON_NORMAL));
64 trayIcon->setVisible(true);
65 QTimer::singleShot(100,this,SLOT(startupNotify()));
67 QObject::connect(&control,SIGNAL(testNodeLinkDone(int)),this,SLOT(slotTestNodeLinkDone(int)));
68 QObject::connect(&control,SIGNAL(readPublicModeDone(int, const QString&, const QString&)),this,SLOT(slotReadPublicModeDone(int, const QString&, const QString&)));
69 control.start();
70 control.setName("/dev/ttyS0");
71 if(enableAction->isChecked()) {
72 control.open();
73 control.testNodeLink();
76 updateTrayTooltip();
80 ScreenLocker::~ScreenLocker()
82 settings->setValue("enabled",enableAction->isChecked());
83 settings->setValue("passwordRequired",requirePasswordAction->isChecked());
84 settings->sync();
87 void ScreenLocker::startupNotify()
89 QString msg;
90 if(enableAction->isChecked())
91 msg="Remember that the screen will be locked if you take your RFID card away";
92 else
93 msg="The RFID Screen Locker is disabled, use the system tray icon to enable it";
94 trayIcon->showMessage("RFID Screen Locker Running",msg,QSystemTrayIcon::Information);
97 void ScreenLocker::enableToggled(bool enabled)
99 trayIcon->showMessage(QString("RFID Screen Locker %1").arg(enabled ? "Enabled":"Disabled"),QString("Use the system tray icon to %1 it").arg(enabled ? "disable":"enable"),QSystemTrayIcon::Information,6000);
100 if (enabled) {
101 trayIcon->setIcon(QIcon(TRAY_ICON_NORMAL));
102 control.open();
103 control.testNodeLink();
105 //FIXME The commented code produces a segfault with stack corruption
106 // else {
107 // control.close();
108 // }
110 void ScreenLocker::slotTestNodeLinkDone(int result)
112 switch(result)
114 case Rdw125Control::Ok:
115 control.readPublicModeA();
116 break;
117 default:
118 control.close();
119 enableAction->setChecked(false);
120 trayIcon->setIcon(QIcon(TRAY_ICON_BAD));
121 trayIcon->showMessage("Unable to open card reader","It was impossible to open the RFID reader\nThe Screen Locker won't work",QSystemTrayIcon::Warning);
125 void ScreenLocker::slotReadPublicModeDone(int correct, const QString & hexData, const QString & decData)
127 Q_UNUSED(decData)
128 Q_UNUSED(hexData)
129 QString userName(getpwuid(getuid())->pw_name);
130 if (userName.endsWith(hexData)) {
131 QProcess proc;
132 QStringList args;
133 args << "kdesktop" << "KScreensaverIface";
134 switch(correct)
136 case Rdw125Control::Ok:
137 keepLockedTimer->stop();
138 if (!requirePasswordAction->isChecked()) {
139 args << "quit";
140 proc.start("dcop",args);
141 proc.waitForFinished();
143 break;
144 default:
145 keepLockedTimer->start();
147 } else
148 qDebug() << "That's not the active user: " << hexData;
149 if(enableAction->isChecked())
150 control.readPublicModeA();
153 void ScreenLocker::updateTrayTooltip()
155 bool enabled = enableAction->isChecked();
156 bool passReq = requirePasswordAction->isChecked();
157 QString passMsg = "";
158 if (enabled) {
159 passMsg = passReq ? "You will have to put your card on range and provide your password to unlock the screen":"The screen will be unlocked as soon as your card is back on range";
161 trayIcon->setToolTip(QString("<HTML><P>The screen locker is %1</P><P>%2</P></HTML>").arg(enabled ? "enabled":"disabled",passMsg));
162 requirePasswordAction->setEnabled(enabled);
165 void ScreenLocker::slotKeepLocked()
167 QProcess proc;
168 QStringList args;
169 args << "kdesktop" << "KScreensaverIface" << "lock";
170 proc.start("dcop",args);
171 proc.waitForFinished();