- Documentation
[qt_rdw125klib.git] / RFIDUserManager / usermanager.cpp
blobd3f031411e91f03d8a634cfbcb9826c6ed1b6472
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 ***************************************************************************/
21 #include "optionsdialog.h"
22 #include "usermanager.h"
23 #include "userpreferences.h"
24 #include <QDebug>
25 #include <QMessageBox>
26 #include <QProcess>
27 #include <pwd.h>
28 #include <grp.h>
29 #include <unistd.h>
31 UserManager::UserManager ( QWidget* parent, Qt::WFlags fl )
32 : QMainWindow ( parent, fl ), Ui::MainWindow(),settings(new QSettings("opsiland","RFIDUserManager",this))
34 setupUi ( this );
35 if(getuid()!=0) {
36 QMessageBox::warning(this,"Not the superuser","Sorry, you must be the superuser to run this program");
37 exit(-1);
39 //Buttons
40 QObject::connect(addUser,SIGNAL(clicked( bool )),this,SLOT(addUserSlot()));
41 QObject::connect(editUser,SIGNAL(clicked( bool )),this,SLOT(editUserSlot()));
42 QObject::connect(rmUser,SIGNAL(clicked( bool )),this,SLOT(rmUserSlot()));
43 QObject::connect(prefs,SIGNAL(clicked( bool )),this,SLOT(preferencesSlot()));
44 QObject::connect(userListWidget,SIGNAL(itemSelectionChanged()),this,SLOT(activarBotonesSlot()));
46 //Check if the app has been already configured
47 settings->beginGroup("RFID");
48 bool configured = settings->value("configured",false).toBool();
49 settings->endGroup();
50 if(!configured)
52 OptionsDialog ops;
53 ops.setWindowTitle("First run setup!");
54 if(ops.exec() == QDialog::Accepted)
56 settings->beginGroup("RFID");
57 settings->setValue("configured",true);
58 settings->endGroup();
61 updateUserList();
64 UserManager::~UserManager()
68 /*$SPECIALIZATION$*/
70 void UserManager::addUserSlot()
72 UserPreferences prefs;
73 prefs.exec();
74 updateUserList();
77 void UserManager::editUserSlot()
79 QList<QListWidgetItem *> items = userListWidget->selectedItems();
80 UserPreferences prefs(items[0]->text());
81 prefs.exec();
82 updateUserList();
85 void UserManager::rmUserSlot()
87 QString userName = userListWidget->selectedItems()[0]->text();
88 int res = QMessageBox::question(this,QString("Deleting user %1").arg(userName),QString("Are you sure you want to remove the %1 user?\n\nTHIS CAN NOT BE UNDONE!!").arg(userName),QMessageBox::Ok|QMessageBox::Cancel);
89 if (res == QMessageBox::Ok) {
90 QProcess proc;
91 QStringList args;
92 int res = QMessageBox::question(this,"Remove home directory?","Do you want to remove the user's home directory too?",QMessageBox::Yes|QMessageBox::No);
93 if (res == QMessageBox::Yes) {
94 args << "-r";
96 args << userName;
97 proc.start("userdel",args);
98 proc.waitForFinished();
99 if (proc.exitCode()!=0) {
100 QMessageBox::warning(this,"Failure",QString("Impossible to delete the user:\n%1").arg(QString(proc.readAllStandardError())));
103 updateUserList();
106 void UserManager::preferencesSlot()
108 OptionsDialog ops;
109 ops.exec();
110 updateUserList();
113 void UserManager::activateButtonsSlot()
115 bool enable = !userListWidget->selectedItems().isEmpty();
116 rmUser->setEnabled(enable);
117 editUser->setEnabled(enable);
120 void UserManager::updateUserList()
122 QStringList usersList;
123 struct passwd *p;
124 struct group *g;
125 settings->beginGroup("RFID");
126 QString rfidGroupName(settings->value("defaultGroup","rfid").toString());
127 settings->endGroup();
129 setpwent();
130 while((p=getpwent())!=NULL)
132 QString userName(p->pw_name);
133 g = getgrgid(p->pw_gid);
134 QString groupName(g->gr_name);
135 if(groupName == rfidGroupName) {
136 usersList << userName;
139 endpwent();
140 userListWidget->clear();
141 userListWidget->addItems(usersList);