2 Copyright (c) 2004 Jan Schaefer <j_schaef@informatik.uni-kl.de>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include <qradiobutton.h>
22 #include <qpushbutton.h>
23 #include <q3groupbox.h>
25 #include <qcheckbox.h>
33 #include <kmessagebox.h>
34 #include <kinputdialog.h>
35 #include <k3listbox.h>
37 #include <kpushbutton.h>
38 #include <kcombobox.h>
40 #include "groupconfigdlg.h"
43 static QString
groupListToString(const QList
<KUserGroup
> & list
);
44 static QString
prettyString(const KUser
&user
);
45 static QString
fromPrettyString(const QString
& s
);
46 static void removeList(QList
<KUser
> & from
, const QList
<KUser
> & that
);
47 static bool userMod(const QString
& user
, const QList
<KUserGroup
> & groups
);
50 GroupConfigGUI::GroupConfigGUI( QWidget
*parent
)
54 connect( listBox
, SIGNAL( selectionChanged(Q3ListBoxItem
*) ), this, SLOT(listBox_selectionChanged(Q3ListBoxItem
*) ) );
57 void GroupConfigGUI::listBox_selectionChanged( Q3ListBoxItem
* i
)
59 removeBtn
->setEnabled(i
);
62 GroupConfigDlg::GroupConfigDlg(QWidget
* parent
,
63 const QString
& fileShareGroup
, bool restricted
,
64 bool rootPassNeeded
, bool simpleSharing
)
66 m_fileShareGroup(fileShareGroup
),
67 m_restricted(restricted
) ,
68 m_rootPassNeeded(rootPassNeeded
),
69 m_simpleSharing(simpleSharing
)
72 setCaption(i18n("Allowed Users"));
73 setButtons(Ok
|Cancel
);
76 showButtonSeparator(true);
79 setFileShareGroup(m_fileShareGroup
);
80 connect(this,SIGNAL(okClicked()),this,SLOT(slotOk()));
83 GroupConfigDlg::~GroupConfigDlg() {
86 void GroupConfigDlg::initUsers() {
87 m_origUsers
= m_fileShareGroup
.users();
88 m_users
= m_origUsers
;
91 void GroupConfigDlg::initGUI() {
92 m_gui
= new GroupConfigGUI(this);
94 setFileShareGroup(m_fileShareGroup
);
96 m_gui
->allUsersRadio
->setChecked(!m_restricted
);
97 m_gui
->groupUsersRadio
->setChecked(m_restricted
);
98 m_gui
->writeAccessChk
->setChecked(!m_rootPassNeeded
);
100 connect( m_gui
->addBtn
, SIGNAL(clicked()),
101 this, SLOT(slotAddUser()));
102 connect( m_gui
->removeBtn
, SIGNAL(clicked()),
103 this, SLOT(slotRemoveUser()));
104 connect( m_gui
->otherGroupBtn
, SIGNAL(clicked()),
105 this, SLOT(slotChangeGroup()));
107 if (m_simpleSharing
) {
108 // if simple sharing users never need the root password
109 m_gui
->writeAccessChk
->setDisabled(true);
113 void GroupConfigDlg::updateListBox() {
114 m_gui
->listBox
->clear();
115 QList
<KUser
>::iterator it
;
116 for ( it
= m_users
.begin(); it
!= m_users
.end(); ++it
) {
117 m_gui
->listBox
->insertItem(prettyString(*it
));
118 kDebug(5009) << "GroupConfigDlg::updateListBox: " << (*it
).loginName();
122 QString
prettyString(const KUser
&user
) {
123 return user
.fullName()+" ("+user
.loginName()+')';
126 QString
fromPrettyString(const QString
& s
) {
127 // Jan Schaefer (jan)
129 int i
= s
.indexOf('(');
130 int j
= s
.indexOf(')');
131 QString loginName
= s
.mid(i
+1,j
-i
-1);
135 bool GroupConfigDlg::restricted() const {
139 void GroupConfigDlg::slotAddUser() {
140 QList
<KUser
> allUsers
= KUser::allUsers();
142 removeList(allUsers
,m_users
);
144 if (allUsers
.count()==0) {
145 KMessageBox::information(this,
146 i18n("All users are in the %1 group already.",
147 m_fileShareGroup
.name()));
151 QStringList stringList
;
153 QList
<KUser
>::iterator it
;
154 for ( it
= allUsers
.begin(); it
!= allUsers
.end(); ++it
) {
155 QString s
= (*it
).fullName()+" ("+(*it
).loginName()+')';
156 stringList
.append(s
);
162 QString userName
= KInputDialog::getItem(
164 i18n("Select a user:"),
173 QString loginName
= fromPrettyString(userName
);
174 KUser
user(loginName
);
175 m_users
.append(KUser(loginName
));
179 void removeList(QList
<KUser
> & from
, const QList
<KUser
> & that
) {
180 QList
<KUser
>::ConstIterator it
;
181 for ( it
= that
.begin(); it
!= that
.end(); ++it
) {
187 bool GroupConfigDlg::addUser(const KUser
& user
, const KUserGroup
& group
) {
188 QList
<KUserGroup
> groups
= user
.groups();
189 groups
.append(group
);
190 if (!userMod(user
.loginName(),groups
)) {
191 KMessageBox::sorry(this,i18n("Could not add user '%1' to group '%2'",
192 user
.loginName(), group
.name()));
199 bool GroupConfigDlg::removeUser(const KUser
& user
, const KUserGroup
& group
) {
200 QList
<KUserGroup
> groups
= user
.groups();
201 groups
.remove(group
);
202 if (!userMod(user
.loginName(),groups
)) {
203 KMessageBox::sorry(this,i18n("Could not remove user '%1' from group '%2'",
204 user
.loginName(), group
.name()));
210 bool GroupConfigDlg::rootPassNeeded() const {
211 return m_rootPassNeeded
;
214 void GroupConfigDlg::slotOk() {
215 m_restricted
= m_gui
->groupUsersRadio
->isChecked();
216 m_rootPassNeeded
= ! m_gui
->writeAccessChk
->isChecked();
217 if (m_restricted
&& !m_fileShareGroup
.isValid()) {
218 KMessageBox::sorry(this,i18n("You have to choose a valid group."));
222 QList
<KUser
> addedUsers
= m_users
;
223 removeList(addedUsers
,m_origUsers
);
224 QList
<KUser
> removedUsers
= m_origUsers
;
225 removeList(removedUsers
,m_users
);
227 QList
<KUser
>::ConstIterator it
;
228 for ( it
= addedUsers
.constBegin(); it
!= addedUsers
.constEnd(); ++it
) {
229 addUser(*it
, m_fileShareGroup
);
232 for ( it
= removedUsers
.constBegin(); it
!= removedUsers
.constEnd(); ++it
) {
233 removeUser(*it
, m_fileShareGroup
);
240 bool userMod(const QString
& user
, const QList
<KUserGroup
> & groups
) {
242 proc
<< "usermod" << "-G" << groupListToString(groups
) << user
;
243 return !proc
.execute();
246 void GroupConfigDlg::slotRemoveUser() {
247 Q3ListBoxItem
* item
= m_gui
->listBox
->selectedItem();
251 QString loginName
= fromPrettyString(item
->text());
252 KUser
user(loginName
);
253 m_users
.remove(KUser(loginName
));
255 m_gui
->removeBtn
->setEnabled(false);
258 QString
groupListToString(const QList
<KUserGroup
> & list
) {
259 QList
<KUserGroup
>::ConstIterator it
;
262 for ( it
= list
.begin(); it
!= list
.end(); ++it
) {
263 result
+=(*it
).name()+',';
267 result
.truncate(result
.length()-1);
271 void GroupConfigDlg::slotChangeGroup() {
272 QList
<KUserGroup
> allGroups
= KUserGroup::allGroups();
274 QStringList stringList
;
276 QList
<KUserGroup
>::iterator it
;
277 for ( it
= allGroups
.begin(); it
!= allGroups
.end(); ++it
) {
278 QString s
= (*it
).name();
279 stringList
.append(s
);
284 KDialog
* dlg
= new KDialog(this);
285 dlg
->setCaption(i18n("Allowed Users"));
286 dlg
->setButtons(Ok
|Cancel
);
287 dlg
->setDefaultButton(Ok
);
289 dlg
->showButtonSeparator(true);
290 KVBox
* vbox
= new KVBox(this);
291 dlg
->setMainWidget(vbox
);
293 KHBox
* hbox
= new KHBox(vbox
);
294 QLabel
* lbl
= new QLabel(i18n("New file share group:"),hbox
);
295 KComboBox
* combo
= new KComboBox(hbox
);
296 combo
->insertStringList(stringList
);
297 combo
->setEditable(true);
298 combo
->setCurrentText(m_fileShareGroup
.name());
300 QCheckBox
* addChk
= new QCheckBox(
301 i18n("Add users from the old file share group to the new one"),
304 QCheckBox
* removeUsersChk
= new QCheckBox(
305 i18n("Remove users from old file share group"),
308 QCheckBox
* removeGroupChk
= new QCheckBox(
309 i18n("Delete the old file share group"),
312 if (dlg
->exec() == QDialog::Accepted
) {
313 QString groupName
= combo
->currentText();
314 if (groupName
!= m_fileShareGroup
.name()) {
315 QString oldGroup
= m_fileShareGroup
.name();
316 if (allGroups
.contains(KUserGroup(groupName
)))
317 setFileShareGroup(KUserGroup(groupName
));
319 if (!createFileShareGroup(groupName
)) {
325 if (removeGroupChk
->isChecked())
326 deleteGroup(oldGroup
);
328 if (removeUsersChk
->isChecked())
329 emptyGroup(oldGroup
);
331 if (addChk
->isChecked()) {
332 addUsersToGroup(m_users
,KUserGroup(groupName
));
334 m_fileShareGroup
= KUserGroup(groupName
);
348 void GroupConfigDlg::setFileShareGroup(const KUserGroup
& group
) {
349 m_fileShareGroup
= group
;
351 if (m_fileShareGroup
.isValid()) {
354 m_gui
->groupUsersRadio
->setText(
355 i18n("Only users of the '%1' group are allowed to share folders",
356 m_fileShareGroup
.name()));
357 m_gui
->usersGrpBx
->setTitle(i18n("Users of '%1' Group",
358 m_fileShareGroup
.name()));
359 m_gui
->otherGroupBtn
->setText(i18n("Change Group..."));
360 m_gui
->usersGrpBx
->show();
362 m_gui
->groupUsersRadio
->setText(i18n("Only users of a certain group are allowed to share folders"));
363 m_gui
->otherGroupBtn
->setText(i18n("Choose Group..."));
364 m_gui
->usersGrpBx
->hide();
371 bool GroupConfigDlg::addUsersToGroup(QList
<KUser
> users
,const KUserGroup
& group
) {
372 QList
<KUser
>::ConstIterator it
;
374 for ( it
= users
.constBegin(); it
!= users
.constEnd(); ++it
) {
375 if (!addUser(*it
, group
))
381 bool GroupConfigDlg::emptyGroup(const QString
& s
) {
382 if (KMessageBox::No
== KMessageBox::questionYesNo(this,
383 i18n("Do you really want to remove all users from group '%1'?", s
), QString(), KStandardGuiItem::del(), KStandardGuiItem::cancel())) {
387 QList
<KUser
> allUsers
= KUser::allUsers();
390 QList
<KUser
>::ConstIterator it
;
391 for ( it
= allUsers
.constBegin(); it
!= allUsers
.constEnd(); ++it
) {
392 if (!removeUser(*it
, group
))
398 bool GroupConfigDlg::deleteGroup(const QString
& s
) {
399 if (KMessageBox::No
== KMessageBox::questionYesNo(this,
400 i18n("Do you really want to delete group '%1'?", s
), QString(), KStandardGuiItem::del(), KStandardGuiItem::cancel())) {
405 proc
<< "groupdel" << s
;
406 if (proc
.execute()) {
407 KMessageBox::sorry(this,i18n("Deleting group '%1' failed.", s
));
414 bool GroupConfigDlg::createFileShareGroup(const QString
& s
) {
416 KMessageBox::sorry(this,i18n("Please choose a valid group."));
420 if (KMessageBox::No
== KMessageBox::questionYesNo(this,
421 i18n("This group '%1' does not exist. Should it be created?", s
), QString(), KGuiItem(i18n("Create")), KGuiItem(i18n("Do Not Create"))))
424 //debug("CreateFileShareGroup: "+s);
426 proc
<< "groupadd" << s
;
427 if (proc
.execute()) {
428 KMessageBox::sorry(this,i18n("Creation of group '%1' failed.", s
));
431 setFileShareGroup(KUserGroup(s
));
438 #include "groupconfigdlg.moc"