summaryrefslogtreecommitdiffstats
path: root/kuser/editGroup.cpp
blob: 2f6a4cfb2a63586b1d042e6a9802e9c95de62347 (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
/*
 *  Copyright (c) 1998 Denis Perchine <dyp@perchine.com>
 *  Copyright (c) 2004 Szombathelyi Gy├Ârgy <gyurco@freemail.hu>
 *  Former maintainer: Adriaan de Groot <groot@kde.org>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public
 *  License version 2 as published by the Free Software Foundation.
 *
 *  This library 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
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 **/


#include <tqlabel.h>
#include <tqlayout.h>
#include <tqvbox.h>
#include <tqvalidator.h>
#include <tqwhatsthis.h>

#include <kapplication.h>
#include <klocale.h>
#include <kpushbutton.h>
#include <kdebug.h>
#include <kmessagebox.h>

#include "kglobal_.h"
#include "editGroup.h"

editGroup::editGroup(KU::KGroup *akg, bool samba, bool add,
   TQWidget* parent, const char* name)
  : KDialogBase(parent, name, true, i18n("Group Properties"), Ok | Cancel, Ok, true)
{
  kg = akg;
  mAdd = add;
  mSamba = samba;
  mOldName = kg->getName();
  SID sid = kg->getSID();
  ro = kug->getGroups().getCaps() & KU::KGroups::Cap_ReadOnly;

  RID rid;
  rid.rid = 512; rid.name = i18n("Domain Admins"); rid.desc = i18n("Admins"); mRids.append( rid );
  rid.rid = 513; rid.name = i18n("Domain Users"); rid.desc = i18n("Users");  mRids.append( rid );
  rid.rid = 514; rid.name = i18n("Domain Guests"); rid.desc = i18n("Guests"); mRids.append( rid );

  TQFrame *page = makeMainWidget();
  TQGridLayout *layout = new TQGridLayout( page, 10, 3, marginHint(), spacingHint() );
  TQLabel *lb;

  lb = new TQLabel( page );
  lb->setText(i18n("Group number:"));
  legid = new KLineEdit(page);
  // ensure it fits at least 20 characters
  legid->setText( "XXXXXXXXXXXXXXXXXXX" );
  legid->setText( TQString::number(kg->getGID()) );
  legid->setValidator( new TQIntValidator(this) );
  legid->setEnabled( mAdd );
  legid->setReadOnly( ro );
  lb->setBuddy( legid );
  layout->addWidget( lb, 0, 0 );
  layout->addMultiCellWidget( legid, 0, 0, 1, 2 );

  if ( mSamba ) {
    lb = new TQLabel( page );
    lb->setText(i18n("Group rid:"));
    lerid = new KComboBox( page );
    lerid->setEditable( !ro );
    TQValueList<RID>::Iterator it;
    for ( it = mRids.begin(); it != mRids.end(); ++it ) {
      lerid->insertItem( TQString::number( (*it).rid ) + " - " + (*it).name );
    }

    lerid->setCurrentText( TQString::number( sid.getRID() ) );
    lerid->setValidator (new TQIntValidator(this) );
    lerid->setEnabled( mAdd );
    connect( lerid, TQT_SIGNAL(activated(int)), TQT_SLOT(ridSelected(int)) );
    lb->setBuddy( lerid );
    layout->addWidget( lb, 1, 0 );
    layout->addMultiCellWidget( lerid, 1, 1, 1, 2 );
  }

  lb = new TQLabel( page );
  lb->setText(i18n("Group name:"));

  legrpname = new KLineEdit( page );
  // ensure it fits at least 20 characters
  legrpname->setText( "XXXXXXXXXXXXXXXXXXX" );
  legrpname->setText( kg->getName() );
  legrpname->setReadOnly( ro );
  legrpname->setFocus();
  lb->setBuddy( legrpname );
  layout->addWidget( lb, 2, 0 );
  layout->addMultiCellWidget( legrpname, 2, 2, 1, 2 );

  if ( mSamba ) {
    lb = new TQLabel( page );
    lb->setText(i18n("Description:"));
    ledesc = new KLineEdit(page);
    ledesc->setText( kg->getDesc() );
    ledesc->setReadOnly( ro );
    lb->setBuddy( ledesc );
    layout->addWidget( lb, 3, 0 );
    layout->addMultiCellWidget( ledesc, 3, 3, 1, 2 );

    lb = new TQLabel( page );
    lb->setText(i18n("Display name:"));
    ledispname = new KLineEdit(page);
    ledispname->setText( kg->getDisplayName() );
    ledispname->setReadOnly( ro );
    lb->setBuddy( ledispname );
    layout->addWidget( lb, 4, 0 );
    layout->addMultiCellWidget( ledispname, 4, 4, 1, 2 );

    lb = new TQLabel( page );
    lb->setText(i18n("Type:"));
    letype = new KComboBox( page );
    letype->insertItem( i18n("Domain") );
    letype->insertItem( i18n("Local") );
    letype->insertItem( i18n("Builtin") );
    switch ( kg->getType() ) {
      case 2:
        letype->setCurrentItem( 0 );
        break;
      case 4:
        letype->setCurrentItem( 1 );
        break;
      case 5:
        letype->setCurrentItem( 2 );
        break;
    }
    lb->setBuddy( letype );
    layout->addWidget( lb, 5, 0 );
    layout->addMultiCellWidget( letype, 5, 5, 1, 2 );

    lb = new TQLabel( page );
    lb->setText(i18n("Domain SID:"));
    ledomsid = new KLineEdit(page);
    ledomsid->setText( sid.getDOM() );
    ledomsid->setReadOnly( ro );
    lb->setBuddy( ledomsid );
    layout->addWidget( lb, 6, 0 );
    layout->addMultiCellWidget( ledomsid, 6, 6, 1, 2 );

    cbsamba = new TQCheckBox( i18n("Disable Samba group information"), page );
    layout->addMultiCellWidget( cbsamba, 7, 7, 0, 2 );
    connect( cbsamba, TQT_SIGNAL(toggled(bool)), ledesc, TQT_SLOT(setDisabled(bool)) );
    connect( cbsamba, TQT_SIGNAL(toggled(bool)), ledispname, TQT_SLOT(setDisabled(bool)) );
    connect( cbsamba, TQT_SIGNAL(toggled(bool)), letype, TQT_SLOT(setDisabled(bool)) );
    connect( cbsamba, TQT_SIGNAL(toggled(bool)), ledomsid, TQT_SLOT(setDisabled(bool)) );
    if ( mAdd ) connect( cbsamba, TQT_SIGNAL(toggled(bool)), lerid, TQT_SLOT(setDisabled(bool)) );
    if ( !mAdd ) cbsamba->setChecked( !( kg->getCaps() & KU::KGroup::Cap_Samba ) );
  }

  m_list_in = new KListView(page);
  m_list_in->setFullWidth(true); // Single column, full widget width.
  m_list_in->addColumn(i18n("Users in Group"));
  m_list_in->setSelectionMode( TQListView::Extended );
  layout->addWidget( m_list_in, 8, 0 );

  TQVBox *vbox = new TQVBox(page);
  TQPushButton *btadd = new TQPushButton(i18n("Add <-"), vbox);
  TQPushButton *btdel = new TQPushButton(i18n("Remove ->"), vbox);
  layout->addWidget( vbox, 8, 1 );

  m_list_notin = new KListView(page);
  m_list_notin->setFullWidth(true); // Single column, full widget width.
  m_list_notin->addColumn(i18n("Users NOT in Group"));
  m_list_notin->setSelectionMode(TQListView::Extended);
  layout->addWidget( m_list_notin, 8, 2 );
//  TQString whatstr = i18n("Select the users that should be in this kg->");
//  TQWhatsThis::add(m_list, whatstr);
//  connect(this,TQT_SIGNAL(okClicked(void)),
          //this,TQT_SLOT(okClicked()));


  for (unsigned int i = 0; i<kug->getUsers().count(); i++) {
    KU::KUser *user;
    user = kug->getUsers()[i];
    TQString userName = user->getName();
    if ( kg->lookup_user(userName) || user->getGID() == kg->getGID() ) {
      KListViewItem *item = new KListViewItem(m_list_in, userName);
      if ( user->getGID() == kg->getGID() ) item->setSelectable( false );
    } else {
      new KListViewItem(m_list_notin, userName);
    }
  }

  connect(btadd, TQT_SIGNAL(clicked()), TQT_SLOT(addClicked()));
  connect(btdel, TQT_SIGNAL(clicked()), TQT_SLOT(delClicked()));

  if ( ro ) {
    btadd->setEnabled( false );
    btdel->setEnabled( false );
  }
}

editGroup::~editGroup() 
{
}

void editGroup::ridSelected( int index )
{
  lerid->setCurrentText( TQString::number( mRids[ index ].rid ) );
  legrpname->setText( mRids[ index ].name );
  ledesc->setText( mRids[ index ].desc );
  ledispname->setText( mRids[ index ].name );
}

void editGroup::addClicked()
{
  TQListViewItem *item, *next;
  TQString name;

  item = m_list_notin->firstChild();
  while ( item ) {
    next = item->nextSibling();
    if ( item->isSelected() ) {
      name = item->text( 0 );
      delete item;
      item = new KListViewItem( m_list_in, name );
    }
    item = next;
  }
}

void editGroup::delClicked()
{
  TQListViewItem *item, *next;
  TQString name;

  item = m_list_in->firstChild();
  while ( item ) {
    next = item->nextSibling();
    if ( item->isSelected() ) {
      name = item->text( 0 );
      delete item;
      item = new KListViewItem( m_list_notin, name );
    }
    item = next;
  }
}

void editGroup::slotOk()
{
  if ( ro ) {
    reject();
    return;
  }

  SID sid;
  kg->clear();
  TQString s;
  s = legid->text();

  if ( mSamba && !cbsamba->isChecked() ) {
    sid.setDOM( ledomsid->text() );
    sid.setRID( lerid->currentText() );
  }

  if ( legrpname->text().isEmpty() ) {
    KMessageBox::sorry( 0,
      i18n("You need to type a group name.") );
    return;
  }

  if ( legrpname->text() != mOldName && 
    kug->getGroups().lookup( legrpname->text() ) ) {

    KMessageBox::sorry( 0,
      i18n("Group with name %1 already exists.").arg(legrpname->text()) );
    return;
  }

  if ( mAdd ) {
    if ( mSamba && !cbsamba->isChecked() && kug->getGroups().lookup_sam( sid ) ) {
      KMessageBox::sorry( 0,
        i18n("Group with SID %1 already exists.").arg( sid.getSID() ) );
      return;
    }
    if (kug->getGroups().lookup(s.toInt())) {
      KMessageBox::sorry( 0,
        i18n("Group with gid %1 already exists.").arg(s.toInt()) );
      return;
    }
  }

  kg->setName(legrpname->text());
  kg->setGID(s.toInt());
  if ( mSamba && !cbsamba->isChecked() ) {
    kg->setCaps ( KU::KGroup::Cap_Samba );
    kg->setSID( sid );
    switch ( letype->currentItem() ) {
      case 0:
        kg->setType( 2 );
        break;
      case 1:
        kg->setType( 4 );
        break;
      case 2:
        kg->setType( 5 );
        break;
    }
    kg->setDesc( ledesc->text() );
    kg->setDisplayName( ledispname->text() );
  } else {
    kg->setCaps( 0 );
    kg->setSID( TQString::null );
    kg->setDesc( TQString::null );
    kg->setDisplayName( TQString::null );
    kg->setType( 0 );
  }

  TQListViewItem *item;
  item = m_list_in->firstChild();
  while ( item ) {
    kg->addUser( item->text( 0 ) );
    item = item->nextSibling();
  }
  accept();
}

#include "editGroup.moc"