-comment conflicting code that produces a segfault which lead to a stack corruption...
[qt_rdw125klib.git] / RFIDScreenLocker / screenlocker.cpp
blob66e8a23eef4f2e1b0a1cbcd1f2ac369171e06f5b
1 /***************************************************************************
2 * Copyright (C) 2007 by Opsidao,,, *
3 * opsi@ka-tet *
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))
43 enableAction = contextMenu->addAction("Enabled");
44 enableAction->setCheckable(true);
45 enableAction->setChecked(settings->value("enabled",true).toBool());
46 QObject::connect(enableAction,SIGNAL(toggled(bool)),this,SLOT(enableToggled(bool)));
48 requirePasswordAction = contextMenu->addAction("Password required");
49 requirePasswordAction->setToolTip("Indicates if the user will have to supply a password to unlock the screen");
50 requirePasswordAction->setCheckable(true);
51 requirePasswordAction->setChecked(settings->value("passwordRequired",true).toBool());
53 closeAction = contextMenu->addAction("Close");
54 QObject::connect(closeAction,SIGNAL(triggered(bool)),this,SLOT(close()));
56 trayIcon->setContextMenu(contextMenu);
57 trayIcon->setIcon(QIcon(TRAY_ICON_NORMAL));
58 trayIcon->setVisible(true);
59 QTimer::singleShot(100,this,SLOT(startupNotify()));
61 QObject::connect(&control,SIGNAL(testNodeLinkDone(int)),this,SLOT(slotTestNodeLinkDone(int)));
62 QObject::connect(&control,SIGNAL(readPublicModeDone(int, const QString&, const QString&)),this,SLOT(slotReadPublicModeDone(int, const QString&, const QString&)));
63 control.start();
64 control.setName("/dev/ttyS0");
65 if(enableAction->isChecked()) {
66 control.open();
67 control.testNodeLink();
72 ScreenLocker::~ScreenLocker()
74 settings->setValue("enabled",enableAction->isChecked());
75 settings->setValue("passwordRequired",requirePasswordAction->isChecked());
76 settings->sync();
79 void ScreenLocker::startupNotify()
81 QString msg;
82 if(enableAction->isChecked())
83 msg="Remember that the screen will be locked if you take your RFID card away";
84 else
85 msg="The RFID Screen Locker is disabled, use the system tray icon to enable it";
86 trayIcon->showMessage("RFID Screen Locker Running",msg,QSystemTrayIcon::Information);
89 void ScreenLocker::enableToggled(bool enabled)
91 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);
92 if (enabled) {
93 trayIcon->setIcon(QIcon(TRAY_ICON_NORMAL));
94 control.open();
95 control.testNodeLink();
97 //FIXME The commented code produces a segfault with stack corruption
98 // else {
99 // control.close();
100 // }
102 void ScreenLocker::slotTestNodeLinkDone(int result)
104 switch(result)
106 case Rdw125Control::Ok:
107 control.readPublicModeA();
108 break;
109 default:
110 control.close();
111 enableAction->setChecked(false);
112 trayIcon->setIcon(QIcon(TRAY_ICON_BAD));
113 trayIcon->showMessage("Unable to open card reader","It was impossible to open the RFID reader\nThe Screen Locker won't work",QSystemTrayIcon::Warning);
117 void ScreenLocker::slotReadPublicModeDone(int correct, const QString & hexData, const QString & decData)
119 Q_UNUSED(decData)
120 Q_UNUSED(hexData)
121 QString userName(getpwuid(getuid())->pw_name);
122 if (userName.endsWith(hexData)) {
123 QProcess proc;
124 QStringList args;
125 args << "kdesktop" << "KScreensaverIface";
126 switch(correct)
128 case Rdw125Control::Ok:
129 if (!requirePasswordAction->isChecked()) {
130 args << "quit";
131 proc.start("dcop",args);
132 proc.waitForFinished();
134 break;
135 default:
136 args << "lock";
137 proc.start("dcop",args);
138 proc.waitForFinished();
140 } else
141 qDebug() << "That's not the active user: " << hexData;
142 if(enableAction->isChecked())
143 control.readPublicModeA();