Show invite menu in wlm chat window immediately
[kdenetwork.git] / filesharing / simple / groupconfigdlg.cpp
blob308f7656bf1823b92c69ed4ae02bf9f05739494c
1 /*
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>
24 #include <qlabel.h>
25 #include <qcheckbox.h>
26 #include <kvbox.h>
27 #include <khbox.h>
28 //Added by qt3to4:
30 #include <klocale.h>
31 #include <kuser.h>
32 #include <kdebug.h>
33 #include <kmessagebox.h>
34 #include <kinputdialog.h>
35 #include <k3listbox.h>
36 #include <kprocess.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 )
51 : QWidget( parent )
53 setupUi( this );
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)
65 : KDialog(parent),
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);
74 setDefaultButton(Ok);
75 setModal(true);
76 showButtonSeparator(true);
77 initGUI();
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);
93 setMainWidget(m_gui);
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)
128 // i j
129 int i = s.indexOf('(');
130 int j = s.indexOf(')');
131 QString loginName = s.mid(i+1,j-i-1);
132 return loginName;
135 bool GroupConfigDlg::restricted() const {
136 return m_restricted;
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()));
148 return;
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);
159 stringList.sort();
161 bool ok;
162 QString userName = KInputDialog::getItem(
163 i18n("Select User"),
164 i18n("Select a user:"),
165 stringList,
167 false,
168 &ok);
170 if (!ok)
171 return;
173 QString loginName = fromPrettyString(userName);
174 KUser user(loginName);
175 m_users.append(KUser(loginName));
176 updateListBox();
179 void removeList(QList<KUser> & from, const QList<KUser> & that) {
180 QList<KUser>::ConstIterator it;
181 for ( it = that.begin(); it != that.end(); ++it ) {
182 from.remove(*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()));
193 return false;
195 return true;
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()));
205 return false;
207 return true;
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."));
219 return;
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);
237 KDialog::accept();
240 bool userMod(const QString & user, const QList<KUserGroup> & groups) {
241 KProcess proc;
242 proc << "usermod" << "-G" << groupListToString(groups) << user;
243 return !proc.execute();
246 void GroupConfigDlg::slotRemoveUser() {
247 Q3ListBoxItem* item = m_gui->listBox->selectedItem();
248 if (!item)
249 return;
251 QString loginName = fromPrettyString(item->text());
252 KUser user(loginName);
253 m_users.remove(KUser(loginName));
254 updateListBox();
255 m_gui->removeBtn->setEnabled(false);
258 QString groupListToString(const QList<KUserGroup> & list) {
259 QList<KUserGroup>::ConstIterator it;
260 QString result;
262 for ( it = list.begin(); it != list.end(); ++it ) {
263 result+=(*it).name()+',';
266 // remove last ,
267 result.truncate(result.length()-1);
268 return result;
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);
282 stringList.sort();
284 KDialog* dlg = new KDialog(this);
285 dlg->setCaption(i18n("Allowed Users"));
286 dlg->setButtons(Ok|Cancel);
287 dlg->setDefaultButton(Ok);
288 dlg->setModal(true);
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"),
302 vbox);
304 QCheckBox* removeUsersChk = new QCheckBox(
305 i18n("Remove users from old file share group"),
306 vbox);
308 QCheckBox* removeGroupChk = new QCheckBox(
309 i18n("Delete the old file share group"),
310 vbox);
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));
318 else {
319 if (!createFileShareGroup(groupName)) {
320 delete dlg;
321 return;
325 if (removeGroupChk->isChecked())
326 deleteGroup(oldGroup);
327 else
328 if (removeUsersChk->isChecked())
329 emptyGroup(oldGroup);
331 if (addChk->isChecked()) {
332 addUsersToGroup(m_users,KUserGroup(groupName));
333 // reread the users
334 m_fileShareGroup = KUserGroup(groupName);
338 initUsers();
339 updateListBox();
344 delete dlg;
348 void GroupConfigDlg::setFileShareGroup(const KUserGroup & group) {
349 m_fileShareGroup = group;
351 if (m_fileShareGroup.isValid()) {
352 initUsers();
353 updateListBox();
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();
361 } else {
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;
373 bool result = true;
374 for ( it = users.constBegin(); it != users.constEnd(); ++it ) {
375 if (!addUser(*it, group))
376 result = false;
378 return result;
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())) {
384 return false;
387 QList<KUser> allUsers = KUser::allUsers();
388 bool result = true;
389 KUserGroup group(s);
390 QList<KUser>::ConstIterator it;
391 for ( it = allUsers.constBegin(); it != allUsers.constEnd(); ++it ) {
392 if (!removeUser(*it, group))
393 result = false;
395 return result;
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())) {
401 return false;
404 KProcess proc;
405 proc << "groupdel" << s;
406 if (proc.execute()) {
407 KMessageBox::sorry(this,i18n("Deleting group '%1' failed.", s));
408 return false;
411 return true;
414 bool GroupConfigDlg::createFileShareGroup(const QString & s) {
415 if (s.isEmpty()) {
416 KMessageBox::sorry(this,i18n("Please choose a valid group."));
417 return false;
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"))))
422 return false;
424 //debug("CreateFileShareGroup: "+s);
425 KProcess proc;
426 proc << "groupadd" << s;
427 if (proc.execute()) {
428 KMessageBox::sorry(this,i18n("Creation of group '%1' failed.", s));
429 return false;
430 } else {
431 setFileShareGroup(KUserGroup(s));
434 return true;
438 #include "groupconfigdlg.moc"