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 "groupconfiggui.h"
41 #include "groupconfigdlg.h"
44 static QString
groupListToString(const QList
<KUserGroup
> & list
);
45 static QString
prettyString(const KUser
&user
);
46 static QString
fromPrettyString(const QString
& s
);
47 static void removeList(QList
<KUser
> & from
, const QList
<KUser
> & that
);
48 static bool userMod(const QString
& user
, const QList
<KUserGroup
> & groups
);
52 GroupConfigDlg::GroupConfigDlg(QWidget
* parent
,
53 const QString
& fileShareGroup
, bool restricted
,
54 bool rootPassNeeded
, bool simpleSharing
)
56 m_fileShareGroup(fileShareGroup
),
57 m_restricted(restricted
) ,
58 m_rootPassNeeded(rootPassNeeded
),
59 m_simpleSharing(simpleSharing
)
62 setCaption(i18n("Allowed Users"));
63 setButtons(Ok
|Cancel
);
66 showButtonSeparator(true);
69 setFileShareGroup(m_fileShareGroup
);
70 connect(this,SIGNAL(okClicked()),this,SLOT(slotOk()));
73 GroupConfigDlg::~GroupConfigDlg() {
76 void GroupConfigDlg::initUsers() {
77 m_origUsers
= m_fileShareGroup
.users();
78 m_users
= m_origUsers
;
81 void GroupConfigDlg::initGUI() {
82 m_gui
= new GroupConfigGUI(this);
84 setFileShareGroup(m_fileShareGroup
);
86 m_gui
->allUsersRadio
->setChecked(!m_restricted
);
87 m_gui
->groupUsersRadio
->setChecked(m_restricted
);
88 m_gui
->writeAccessChk
->setChecked(!m_rootPassNeeded
);
90 connect( m_gui
->addBtn
, SIGNAL(clicked()),
91 this, SLOT(slotAddUser()));
92 connect( m_gui
->removeBtn
, SIGNAL(clicked()),
93 this, SLOT(slotRemoveUser()));
94 connect( m_gui
->otherGroupBtn
, SIGNAL(clicked()),
95 this, SLOT(slotChangeGroup()));
97 if (m_simpleSharing
) {
98 // if simple sharing users never need the root password
99 m_gui
->writeAccessChk
->setDisabled(true);
103 void GroupConfigDlg::updateListBox() {
104 m_gui
->listBox
->clear();
105 QList
<KUser
>::iterator it
;
106 for ( it
= m_users
.begin(); it
!= m_users
.end(); ++it
) {
107 m_gui
->listBox
->insertItem(prettyString(*it
));
108 kDebug(5009) << "GroupConfigDlg::updateListBox: " << (*it
).loginName();
112 QString
prettyString(const KUser
&user
) {
113 return user
.fullName()+" ("+user
.loginName()+')';
116 QString
fromPrettyString(const QString
& s
) {
117 // Jan Schaefer (jan)
119 int i
= s
.indexOf('(');
120 int j
= s
.indexOf(')');
121 QString loginName
= s
.mid(i
+1,j
-i
-1);
125 bool GroupConfigDlg::restricted() const {
129 void GroupConfigDlg::slotAddUser() {
130 QList
<KUser
> allUsers
= KUser::allUsers();
132 removeList(allUsers
,m_users
);
134 if (allUsers
.count()==0) {
135 KMessageBox::information(this,
136 i18n("All users are in the %1 group already.",
137 m_fileShareGroup
.name()));
141 QStringList stringList
;
143 QList
<KUser
>::iterator it
;
144 for ( it
= allUsers
.begin(); it
!= allUsers
.end(); ++it
) {
145 QString s
= (*it
).fullName()+" ("+(*it
).loginName()+')';
146 stringList
.append(s
);
152 QString userName
= KInputDialog::getItem(
154 i18n("Select a user:"),
163 QString loginName
= fromPrettyString(userName
);
164 KUser
user(loginName
);
165 m_users
.append(KUser(loginName
));
169 void removeList(QList
<KUser
> & from
, const QList
<KUser
> & that
) {
170 QList
<KUser
>::ConstIterator it
;
171 for ( it
= that
.begin(); it
!= that
.end(); ++it
) {
177 bool GroupConfigDlg::addUser(const KUser
& user
, const KUserGroup
& group
) {
178 QList
<KUserGroup
> groups
= user
.groups();
179 groups
.append(group
);
180 if (!userMod(user
.loginName(),groups
)) {
181 KMessageBox::sorry(this,i18n("Could not add user '%1' to group '%2'",
182 user
.loginName(), group
.name()));
189 bool GroupConfigDlg::removeUser(const KUser
& user
, const KUserGroup
& group
) {
190 QList
<KUserGroup
> groups
= user
.groups();
191 groups
.remove(group
);
192 if (!userMod(user
.loginName(),groups
)) {
193 KMessageBox::sorry(this,i18n("Could not remove user '%1' from group '%2'",
194 user
.loginName(), group
.name()));
200 bool GroupConfigDlg::rootPassNeeded() const {
201 return m_rootPassNeeded
;
204 void GroupConfigDlg::slotOk() {
205 m_restricted
= m_gui
->groupUsersRadio
->isChecked();
206 m_rootPassNeeded
= ! m_gui
->writeAccessChk
->isChecked();
207 if (m_restricted
&& !m_fileShareGroup
.isValid()) {
208 KMessageBox::sorry(this,i18n("You have to choose a valid group."));
212 QList
<KUser
> addedUsers
= m_users
;
213 removeList(addedUsers
,m_origUsers
);
214 QList
<KUser
> removedUsers
= m_origUsers
;
215 removeList(removedUsers
,m_users
);
217 QList
<KUser
>::ConstIterator it
;
218 for ( it
= addedUsers
.begin(); it
!= addedUsers
.end(); ++it
) {
219 addUser(*it
, m_fileShareGroup
);
222 for ( it
= removedUsers
.begin(); it
!= removedUsers
.end(); ++it
) {
223 removeUser(*it
, m_fileShareGroup
);
230 bool userMod(const QString
& user
, const QList
<KUserGroup
> & groups
) {
232 proc
<< "usermod" << "-G" << groupListToString(groups
) << user
;
233 return !proc
.execute();
236 void GroupConfigDlg::slotRemoveUser() {
237 Q3ListBoxItem
* item
= m_gui
->listBox
->selectedItem();
241 QString loginName
= fromPrettyString(item
->text());
242 KUser
user(loginName
);
243 m_users
.remove(KUser(loginName
));
245 m_gui
->removeBtn
->setEnabled(false);
248 QString
groupListToString(const QList
<KUserGroup
> & list
) {
249 QList
<KUserGroup
>::ConstIterator it
;
252 for ( it
= list
.begin(); it
!= list
.end(); ++it
) {
253 result
+=(*it
).name()+',';
257 result
.truncate(result
.length()-1);
261 void GroupConfigDlg::slotChangeGroup() {
262 QList
<KUserGroup
> allGroups
= KUserGroup::allGroups();
264 QStringList stringList
;
266 QList
<KUserGroup
>::iterator it
;
267 for ( it
= allGroups
.begin(); it
!= allGroups
.end(); ++it
) {
268 QString s
= (*it
).name();
269 stringList
.append(s
);
274 KDialog
* dlg
= new KDialog(this);
275 dlg
->setCaption(i18n("Allowed Users"));
276 dlg
->setButtons(Ok
|Cancel
);
277 dlg
->setDefaultButton(Ok
);
279 dlg
->showButtonSeparator(true);
280 KVBox
* vbox
= new KVBox(this);
281 dlg
->setMainWidget(vbox
);
283 KHBox
* hbox
= new KHBox(vbox
);
284 QLabel
* lbl
= new QLabel(i18n("New file share group:"),hbox
);
285 KComboBox
* combo
= new KComboBox(hbox
);
286 combo
->insertStringList(stringList
);
287 combo
->setEditable(true);
288 combo
->setCurrentText(m_fileShareGroup
.name());
290 QCheckBox
* addChk
= new QCheckBox(
291 i18n("Add users from the old file share group to the new one"),
294 QCheckBox
* removeUsersChk
= new QCheckBox(
295 i18n("Remove users from old file share group"),
298 QCheckBox
* removeGroupChk
= new QCheckBox(
299 i18n("Delete the old file share group"),
302 if (dlg
->exec() == QDialog::Accepted
) {
303 QString groupName
= combo
->currentText();
304 if (groupName
!= m_fileShareGroup
.name()) {
305 QString oldGroup
= m_fileShareGroup
.name();
306 if (allGroups
.contains(KUserGroup(groupName
)))
307 setFileShareGroup(KUserGroup(groupName
));
309 if (!createFileShareGroup(groupName
)) {
315 if (removeGroupChk
->isChecked())
316 deleteGroup(oldGroup
);
318 if (removeUsersChk
->isChecked())
319 emptyGroup(oldGroup
);
321 if (addChk
->isChecked()) {
322 addUsersToGroup(m_users
,KUserGroup(groupName
));
324 m_fileShareGroup
= KUserGroup(groupName
);
338 void GroupConfigDlg::setFileShareGroup(const KUserGroup
& group
) {
339 m_fileShareGroup
= group
;
341 if (m_fileShareGroup
.isValid()) {
344 m_gui
->groupUsersRadio
->setText(
345 i18n("Only users of the '%1' group are allowed to share folders",
346 m_fileShareGroup
.name()));
347 m_gui
->usersGrpBx
->setTitle(i18n("Users of '%1' Group",
348 m_fileShareGroup
.name()));
349 m_gui
->otherGroupBtn
->setText(i18n("Change Group..."));
350 m_gui
->usersGrpBx
->show();
352 m_gui
->groupUsersRadio
->setText(i18n("Only users of a certain group are allowed to share folders"));
353 m_gui
->otherGroupBtn
->setText(i18n("Choose Group..."));
354 m_gui
->usersGrpBx
->hide();
361 bool GroupConfigDlg::addUsersToGroup(QList
<KUser
> users
,const KUserGroup
& group
) {
362 QList
<KUser
>::ConstIterator it
;
364 for ( it
= users
.begin(); it
!= users
.end(); ++it
) {
365 if (!addUser(*it
, group
))
371 bool GroupConfigDlg::emptyGroup(const QString
& s
) {
372 if (KMessageBox::No
== KMessageBox::questionYesNo(this,
373 i18n("Do you really want to remove all users from group '%1'?", s
), QString(), KStandardGuiItem::del(), KStandardGuiItem::cancel())) {
377 QList
<KUser
> allUsers
= KUser::allUsers();
380 QList
<KUser
>::ConstIterator it
;
381 for ( it
= allUsers
.begin(); it
!= allUsers
.end(); ++it
) {
382 if (!removeUser(*it
, group
))
388 bool GroupConfigDlg::deleteGroup(const QString
& s
) {
389 if (KMessageBox::No
== KMessageBox::questionYesNo(this,
390 i18n("Do you really want to delete group '%1'?", s
), QString(), KStandardGuiItem::del(), KStandardGuiItem::cancel())) {
395 proc
<< "groupdel" << s
;
396 if (proc
.execute()) {
397 KMessageBox::sorry(this,i18n("Deleting group '%1' failed.", s
));
404 bool GroupConfigDlg::createFileShareGroup(const QString
& s
) {
406 KMessageBox::sorry(this,i18n("Please choose a valid group."));
410 if (KMessageBox::No
== KMessageBox::questionYesNo(this,
411 i18n("This group '%1' does not exist. Should it be created?", s
), QString(), KGuiItem(i18n("Create")), KGuiItem(i18n("Do Not Create"))))
414 //debug("CreateFileShareGroup: "+s);
416 proc
<< "groupadd" << s
;
417 if (proc
.execute()) {
418 KMessageBox::sorry(this,i18n("Creation of group '%1' failed.", s
));
421 setFileShareGroup(KUserGroup(s
));
428 #include "groupconfigdlg.moc"