1 /***************************************************************************
2 * Copyright (C) 2007 by Juan González Aguilera *
3 * (kde_devel@opsiland.info) *
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. *
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. *
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"
22 #include <QSystemTrayIcon>
31 #define TRAY_ICON_NORMAL ":/images/oxygen-system-lock-screen.svg"
32 #define TRAY_ICON_BAD ":/images/bad.svg"
34 ScreenLocker::ScreenLocker(QWidget
*parent
)
36 trayIcon(new QSystemTrayIcon(this)),
37 contextMenu(new QMenu(this)),
39 requirePasswordAction(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
&)));
70 control
.setName("/dev/ttyS0");
71 if(enableAction
->isChecked()) {
73 control
.testNodeLink();
80 ScreenLocker::~ScreenLocker()
82 settings
->setValue("enabled",enableAction
->isChecked());
83 settings
->setValue("passwordRequired",requirePasswordAction
->isChecked());
87 void ScreenLocker::startupNotify()
90 if(enableAction
->isChecked())
91 msg
="Remember that the screen will be locked if you take your RFID card away";
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);
101 trayIcon
->setIcon(QIcon(TRAY_ICON_NORMAL
));
103 control
.testNodeLink();
105 //FIXME The commented code produces a segfault with stack corruption
110 void ScreenLocker::slotTestNodeLinkDone(int result
)
114 case Rdw125Control::Ok
:
115 control
.readPublicModeA();
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
)
129 QString
userName(getpwuid(getuid())->pw_name
);
130 if (userName
.endsWith(hexData
)) {
133 args
<< "kdesktop" << "KScreensaverIface";
136 case Rdw125Control::Ok
:
137 keepLockedTimer
->stop();
138 if (!requirePasswordAction
->isChecked()) {
140 proc
.start("dcop",args
);
141 proc
.waitForFinished();
145 keepLockedTimer
->start();
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
= "";
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()
169 args
<< "kdesktop" << "KScreensaverIface" << "lock";
170 proc
.start("dcop",args
);
171 proc
.waitForFinished();