summaryrefslogtreecommitdiffstats
path: root/filesharing/simple/groupconfigdlg.cpp
blob: 86173f2bda4a370c98b891fee1a933b29ddad1a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
  Copyright (c) 2004 Jan Schaefer <j_schaef@informatik.uni-kl.de>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

*/


#include <tqradiobutton.h>
#include <tqpushbutton.h>
#include <tqgroupbox.h>
#include <tqlabel.h>
#include <tqcheckbox.h>
#include <tqvbox.h>
#include <tqhbox.h>

#include <klocale.h>
#include <kuser.h>
#include <kdebug.h>
#include <kmessagebox.h>
#include <kinputdialog.h>
#include <klistbox.h>
#include <kprocess.h>
#include <kpushbutton.h>
#include <kcombobox.h>

#include "groupconfiggui.h"
#include "groupconfigdlg.h"

  
static TQString groupListToString(const TQValueList<KUserGroup> & list);
static TQString prettyString(const KUser &user);
static TQString fromPrettyString(const TQString & s);
static void removeList(TQValueList<KUser> & from, const TQValueList<KUser> & that);
static bool userMod(const TQString & user, const TQValueList<KUserGroup> & groups);



GroupConfigDlg::GroupConfigDlg(TQWidget * parent,
      const TQString & fileShareGroup, bool restricted,
      bool rootPassNeeded, bool simpleSharing) 
  : KDialogBase(parent,"groupconfigdlg", true,
                i18n("Allowed Users"), Ok|Cancel, Ok, true) ,
  m_fileShareGroup(fileShareGroup),
  m_restricted(restricted) ,
  m_rootPassNeeded(rootPassNeeded),
  m_simpleSharing(simpleSharing)              
   
{
  initGUI();

  setFileShareGroup(m_fileShareGroup);
}

GroupConfigDlg::~GroupConfigDlg() {
}

void GroupConfigDlg::initUsers() {
  m_origUsers = m_fileShareGroup.users();
  m_users = m_origUsers;
}

void GroupConfigDlg::initGUI() {
  m_gui = new GroupConfigGUI(this);
  setMainWidget(m_gui);
  setFileShareGroup(m_fileShareGroup);
  
  m_gui->allUsersRadio->setChecked(!m_restricted);
  m_gui->groupUsersRadio->setChecked(m_restricted);
  m_gui->writeAccessChk->setChecked(!m_rootPassNeeded);            
          
  connect( m_gui->addBtn, TQT_SIGNAL(clicked()),
           this, TQT_SLOT(slotAddUser()));
  connect( m_gui->removeBtn, TQT_SIGNAL(clicked()),
           this, TQT_SLOT(slotRemoveUser()));
  connect( m_gui->otherGroupBtn, TQT_SIGNAL(clicked()),
           this, TQT_SLOT(slotChangeGroup()));
           
  if (m_simpleSharing) {
      // if simple sharing users never need the root password
      m_gui->writeAccessChk->setDisabled(true);
  }          
}

void GroupConfigDlg::updateListBox() {
  m_gui->listBox->clear();
  TQValueList<KUser>::iterator it;
  for ( it = m_users.begin(); it != m_users.end(); ++it ) {
     m_gui->listBox->insertItem(prettyString(*it));
     kdDebug(5009) << "GroupConfigDlg::updateListBox: " << (*it).loginName() << endl;
  }
}

TQString prettyString(const KUser &user) {
  return user.fullName()+" ("+user.loginName()+")";
}

TQString fromPrettyString(const TQString & s) {
  // Jan Schaefer (jan)
  //              i   j
  int i = s.find('(');
  int j = s.find(')');
  TQString loginName = s.mid(i+1,j-i-1);
  return loginName;
}

bool GroupConfigDlg::restricted() {
  return m_restricted;
}

void GroupConfigDlg::slotAddUser() {
  TQValueList<KUser> allUsers = KUser::allUsers();
  
  removeList(allUsers,m_users);
  
  if (allUsers.count()==0) {
     KMessageBox::information(this,
           i18n("All users are in the %1 group already.")
           .arg(m_fileShareGroup.name()));
     return;           
  }

  TQStringList stringList;
  
  TQValueList<KUser>::iterator it;
  for ( it = allUsers.begin(); it != allUsers.end(); ++it ) {
    TQString s = (*it).fullName()+" ("+(*it).loginName()+")";
    stringList.append(s);
  }
  
  stringList.sort();
  
  bool ok;
  TQString userName = KInputDialog::getItem(
            i18n("Select User"),
            i18n("Select a user:"),
            stringList,
            0,
            false,
            &ok);

  if (!ok)
      return;            
                        
  TQString loginName = fromPrettyString(userName);
  KUser user(loginName);
  m_users.append(KUser(loginName));
  updateListBox();
}

void removeList(TQValueList<KUser> & from, const TQValueList<KUser> & that) {
  TQValueList<KUser>::ConstIterator it;
  for ( it = that.begin(); it != that.end(); ++it ) {
    from.remove(*it);
  } 

}

bool GroupConfigDlg::addUser(const KUser & user, const KUserGroup & group) {
  TQValueList<KUserGroup> groups = user.groups();
  groups.append(group);
  if (!userMod(user.loginName(),groups)) {
    KMessageBox::sorry(this,i18n("Could not add user '%1' to group '%2'")
            .arg(user.loginName()).arg(group.name()));
    return false;            
  } 
  return true;
}


bool GroupConfigDlg::removeUser(const KUser & user, const KUserGroup & group) {
  TQValueList<KUserGroup> groups = user.groups();
  groups.remove(group);
  if (!userMod(user.loginName(),groups)) {
    KMessageBox::sorry(this,i18n("Could not remove user '%1' from group '%2'")
            .arg(user.loginName()).arg(group.name()));
    return false;            
  } 
  return true;
}

bool GroupConfigDlg::rootPassNeeded() {
  return m_rootPassNeeded;
}

void GroupConfigDlg::slotOk() {
  m_restricted = m_gui->groupUsersRadio->isChecked();
  m_rootPassNeeded = ! m_gui->writeAccessChk->isChecked();
  if (m_restricted && !m_fileShareGroup.isValid()) {
    KMessageBox::sorry(this,i18n("You have to choose a valid group."));
    return;
  }

  TQValueList<KUser> addedUsers = m_users;
  removeList(addedUsers,m_origUsers);
  TQValueList<KUser> removedUsers = m_origUsers;
  removeList(removedUsers,m_users);
  
  TQValueList<KUser>::ConstIterator it;
  for ( it = addedUsers.begin(); it != addedUsers.end(); ++it ) {
    addUser(*it, m_fileShareGroup);
  }
  
  for ( it = removedUsers.begin(); it != removedUsers.end(); ++it ) {
    removeUser(*it, m_fileShareGroup);
  }
              
  
  KDialogBase::slotOk();
}

bool userMod(const TQString & user, const TQValueList<KUserGroup> & groups) {
  TDEProcess proc;
  proc << "usermod" << "-G" << groupListToString(groups) << user;
  return proc.start(TDEProcess::Block) && proc.normalExit();
}

void GroupConfigDlg::slotRemoveUser() {
  TQListBoxItem* item = m_gui->listBox->selectedItem();
  if (!item)
      return;
      
  TQString loginName = fromPrettyString(item->text());
  KUser user(loginName);
  m_users.remove(KUser(loginName));
  updateListBox();  
  m_gui->removeBtn->setEnabled(false);
}

TQString groupListToString(const TQValueList<KUserGroup> & list) {
  TQValueList<KUserGroup>::ConstIterator it;
  TQString result;
  
  for ( it = list.begin(); it != list.end(); ++it ) {
    result+=(*it).name()+",";
  }
  
  // remove last ,
  result.truncate(result.length()-1);
  return result;
}

void GroupConfigDlg::slotChangeGroup() {
  TQValueList<KUserGroup> allGroups = KUserGroup::allGroups();
  
  TQStringList stringList;
  
  TQValueList<KUserGroup>::iterator it;
  for ( it = allGroups.begin(); it != allGroups.end(); ++it ) {
    TQString s = (*it).name();
    stringList.append(s);
  }
  
  stringList.sort();
  
  KDialogBase* dlg = new KDialogBase(this,"groupconfigdlg", true,
                i18n("Allowed Users"), Ok|Cancel, Ok, true);
                
  TQVBox* vbox = dlg->makeVBoxMainWidget();
    
  TQHBox* hbox = new TQHBox(vbox);
  TQLabel* lbl = new TQLabel(i18n("New file share group:"),hbox);
  KComboBox* combo = new KComboBox(hbox);
  combo->insertStringList(stringList);
  combo->setEditable(true);
  combo->setCurrentText(m_fileShareGroup.name());
  
  TQCheckBox* addChk = new TQCheckBox(
        i18n("Add users from the old file share group to the new one"),
        vbox);

  TQCheckBox* removeUsersChk = new TQCheckBox(
        i18n("Remove users from old file share group"),
        vbox);

  TQCheckBox* removeGroupChk = new TQCheckBox(
        i18n("Delete the old file share group"),
        vbox);
        
  if (dlg->exec() == TQDialog::Accepted) {
    TQString groupName = combo->currentText();             
    if (groupName != m_fileShareGroup.name()) {
      TQString oldGroup = m_fileShareGroup.name();
      if (allGroups.contains(KUserGroup(groupName)))                           
        setFileShareGroup(KUserGroup(groupName));
      else {
        if (!createFileShareGroup(groupName)) {
          delete dlg;
          return;
        }
      }        

      if (removeGroupChk->isChecked())
        deleteGroup(oldGroup);
      else
      if (removeUsersChk->isChecked())
        emptyGroup(oldGroup);
        
      if (addChk->isChecked()) {
          addUsersToGroup(m_users,KUserGroup(groupName));
          // reread the users
          m_fileShareGroup = KUserGroup(groupName);
      }          
      
          
      initUsers();
      updateListBox();
      
    }        
  }                

  delete dlg;
  
}

void GroupConfigDlg::setFileShareGroup(const KUserGroup & group) {
  m_fileShareGroup = group;
  
  if (m_fileShareGroup.isValid()) {
    initUsers();
    updateListBox();
    m_gui->groupUsersRadio->setText(
          i18n("Only users of the '%1' group are allowed to share folders")
          .arg(m_fileShareGroup.name()));
    m_gui->usersGrpBx->setTitle(i18n("Users of '%1' Group")
          .arg(m_fileShareGroup.name()));
    m_gui->otherGroupBtn->setText(i18n("Change Group..."));          
    m_gui->usersGrpBx->show();
  } else {
    m_gui->groupUsersRadio->setText(i18n("Only users of a certain group are allowed to share folders"));
    m_gui->otherGroupBtn->setText(i18n("Choose Group..."));
    m_gui->usersGrpBx->hide();
  }    

  
  
}

bool GroupConfigDlg::addUsersToGroup(TQValueList<KUser> users,const KUserGroup & group) {
  TQValueList<KUser>::ConstIterator it;
  bool result = true;
  for ( it = users.begin(); it != users.end(); ++it ) {
    if (!addUser(*it, group))
      result  = false;
  }
  return result;
}

bool GroupConfigDlg::emptyGroup(const TQString & s) {
  if (KMessageBox::No == KMessageBox::questionYesNo(this, 
        i18n("Do you really want to remove all users from group '%1'?").arg(s), TQString(), KStdGuiItem::del(), KStdGuiItem::cancel())) {
      return false;        
  }

  TQValueList<KUser> allUsers = KUser::allUsers();
  bool result = true;
  KUserGroup group(s);
  TQValueList<KUser>::ConstIterator it;
  for ( it = allUsers.begin(); it != allUsers.end(); ++it ) {
    if (!removeUser(*it, group)) 
      result = false;
  }
  return result;
}

bool GroupConfigDlg::deleteGroup(const TQString & s) {
  if (KMessageBox::No == KMessageBox::questionYesNo(this, 
        i18n("Do you really want to delete group '%1'?").arg(s), TQString(), KStdGuiItem::del(), KStdGuiItem::cancel())) {
      return false;        
  }
        
  TDEProcess proc;
  proc << "groupdel" << s;
  bool result = proc.start(TDEProcess::Block) && proc.normalExit();
  if (!result) {
    KMessageBox::sorry(this,i18n("Deleting group '%1' failed.").arg(s));
  } 
  
  return result;
}

bool GroupConfigDlg::createFileShareGroup(const TQString & s) {
  if (s.isEmpty()) {
      KMessageBox::sorry(this,i18n("Please choose a valid group."));
      return false;
  }

  if (KMessageBox::No == KMessageBox::questionYesNo(this,
      i18n("This group '%1' does not exist. Should it be created?").arg(s), TQString(), i18n("Create"), i18n("Do Not Create")))
      return false;
      
  //debug("CreateFileShareGroup: "+s);
  TDEProcess proc;
  proc << "groupadd" << s;
  bool result = proc.start(TDEProcess::Block) && proc.normalExit();
  if (!result) {
    KMessageBox::sorry(this,i18n("Creation of group '%1' failed.").arg(s));
  } else {
    setFileShareGroup(KUserGroup(s));
  }
  
  return result;
}


#include "groupconfigdlg.moc"