summaryrefslogtreecommitdiffstats
path: root/kuser/propdlg.cpp
blob: b273d8d20872545e5678d5b03a13742b1d036027 (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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
/*
 *  Copyright (c) 1998 Denis Perchine <[email protected]>
 *  Copyright (c) 2004 Szombathelyi György <[email protected]>
 *  Former maintainer: Adriaan de Groot <[email protected]>
 *
 *  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 <stdio.h>
#include <stdlib.h>

#include <tqdatetime.h>
#include <tqwhatsthis.h>
#include <tqlayout.h>
#include <tqgroupbox.h>
#include <tqfile.h>

#include <kseparator.h>
#include <kmessagebox.h>
#include <kdebug.h>

#include <tqvalidator.h>

#include "propdlg.h"
#include "pwddlg.h"
#include "kglobal_.h"
#include "misc.h"

void propdlg::addRow(TQWidget *tqparent, TQGridLayout *tqlayout, int row,
  TQWidget *widget, const TQString &label, const TQString &what,
  bool two_column, bool nochange)
{
   TQLabel *lab = new TQLabel(widget, label, tqparent);
   lab->setMinimumSize(lab->tqsizeHint());
   widget->setMinimumSize(widget->tqsizeHint());
   tqlayout->addWidget(lab, row, 0);
   if (!what.isEmpty())
   {
      TQWhatsThis::add(lab, what);
      TQWhatsThis::add(widget, what);
   }
   if (two_column)
      tqlayout->addMultiCellWidget(widget, row, row, 1, 2);
   else
      tqlayout->addWidget(widget, row, 1);

   if ( !nochange || ro ) return;
   TQCheckBox *nc = new TQCheckBox( i18n("Do not change"), tqparent );
   tqlayout->addWidget( nc, row, 3 );
   nc->hide();
   mNoChanges[ widget ] = nc;
}

KIntSpinBox *propdlg::addDaysGroup(TQWidget *tqparent, TQGridLayout *tqlayout, int row,
  const TQString &title, bool never)
{
    KIntSpinBox *days;

    TQLabel *label = new TQLabel( title, tqparent );
    tqlayout->addMultiCellWidget( label, row, row, 0, 1, AlignRight );

    days = new KIntSpinBox( tqparent );
    label->setBuddy( days );
    days->setSuffix( i18n(" days") );
    days->setMaxValue( 99999 );
    if (never)
    {
      days->setMinValue( -1 );
      days->setSpecialValueText(i18n("Never"));
    }
    else
    {
      days->setMinValue( 0 );
    }
    tqlayout->addWidget( days, row, 2 );

    connect(days, TQT_SIGNAL(valueChanged(int)), this, TQT_SLOT(changed()));

    TQCheckBox *nc = new TQCheckBox( i18n("Do not change"), tqparent );
    tqlayout->addWidget( nc, row, 3 );
    nc->hide();
    mNoChanges[ days ] = nc;

    return days;
}

void propdlg::initDlg()
{
  ro = kug->getUsers().getCaps() & KU::KUsers::Cap_ReadOnly;

  TQString whatstr;

  // Tab 1: User Info
  {
    TQFrame *frame = addPage(i18n("User Info"));
    TQGridLayout *tqlayout = new TQGridLayout(frame, 20, 4, marginHint(), spacingHint());
    int row = 0;

    frontpage = frame;
    fronttqlayout = tqlayout;

    lbuser = new TQLabel(frame);
//    whatstr = i18n("WHAT IS THIS: User login");
    addRow(frame, tqlayout, row++, lbuser, i18n("User login:"), whatstr, false, false);

    leid = new KLineEdit(frame);
//    whatstr = i18n("WHAT IS THIS: User Id");
    leid->setValidator(new TQIntValidator(TQT_TQOBJECT(frame)));
    addRow(frame, tqlayout, row++, leid, i18n("&User ID:"), whatstr);
    connect(leid, TQT_SIGNAL(textChanged(const TQString &)), this, TQT_SLOT(changed()));

    if ( !ro ) {
      pbsetpwd = new TQPushButton(i18n("Set &Password..."), frame);
      tqlayout->addWidget(pbsetpwd, 0, 2);
      connect(pbsetpwd, TQT_SIGNAL(clicked()), this, TQT_SLOT(setpwd()));
    }


    lefname = new KLineEdit(frame);
//    whatstr = i18n("WHAT IS THIS: Full Name");
    addRow(frame, tqlayout, row++, lefname, i18n("Full &name:"), whatstr);
    connect(lefname, TQT_SIGNAL(textChanged(const TQString &)), this, TQT_SLOT(changed()));
    lefname->setFocus();

    if ( kug->getUsers().getCaps() & KU::KUsers::Cap_InetOrg ) {
      lesurname = new KLineEdit(frame);
//    whatstr = i18n("WHAT IS THIS: Surname");
      addRow(frame, tqlayout, row++, lesurname, i18n("Surname:"), whatstr);
      connect(lesurname, TQT_SIGNAL(textChanged(const TQString &)), this, TQT_SLOT(changed()));

      lemail = new KLineEdit(frame);
//    whatstr = i18n("WHAT IS THIS: Email");
      addRow(frame, tqlayout, row++, lemail, i18n("Email address:"), whatstr);
      connect(lemail, TQT_SIGNAL(textChanged(const TQString &)), this, TQT_SLOT(changed()));
    }

    leshell = new KComboBox(true, frame);
    leshell->clear();
    leshell->insertItem(i18n("<Empty>"));

    TQStringList shells = readShells();
    shells.sort();
    leshell->insertStringList(shells);
    connect(leshell, TQT_SIGNAL(activated(const TQString &)), this, TQT_SLOT(changed()));
    connect(leshell, TQT_SIGNAL(textChanged(const TQString &)), this, TQT_SLOT(changed()));
//    whatstr = i18n("WHAT IS THIS: Login Shell");
    addRow(frame, tqlayout, row++, leshell, i18n("&Login shell:"), whatstr);

    lehome = new KLineEdit(frame);
    connect(lehome, TQT_SIGNAL(textChanged(const TQString &)), this, TQT_SLOT(changed()));
//    whatstr = i18n("WHAT IS THIS: Home Directory");
    addRow(frame, tqlayout, row++, lehome, i18n("&Home folder:"), whatstr);

    // FreeBSD appears to use the comma separated fields in the GECOS entry
    // differently than Linux.
    if ( kug->getUsers().getCaps() & KU::KUsers::Cap_BSD ) {
      leoffice = new KLineEdit(frame);
      connect(leoffice, TQT_SIGNAL(textChanged(const TQString &)), this, TQT_SLOT(changed()));
//    whatstr = i18n("WHAT IS THIS: Office");
      addRow(frame, tqlayout, row++, leoffice, i18n("&Office:"), whatstr);

      leophone = new KLineEdit(frame);
      connect(leophone, TQT_SIGNAL(textChanged(const TQString &)), this, TQT_SLOT(changed()));
//    whatstr = i18n("WHAT IS THIS: Office Phone");
      addRow(frame, tqlayout, row++, leophone, i18n("Offi&ce Phone:"), whatstr);

      lehphone = new KLineEdit(frame);
      connect(lehphone, TQT_SIGNAL(textChanged(const TQString &)), this, TQT_SLOT(changed()));
//    whatstr = i18n("WHAT IS THIS: Home Phone");
      addRow(frame, tqlayout, row++, lehphone, i18n("Ho&me Phone:"), whatstr);

      leclass = new KLineEdit(frame);
      connect(leclass, TQT_SIGNAL(textChanged(const TQString &)), this, TQT_SLOT(changed()));
//    whatstr = i18n("WHAT IS THIS: Login class");
      addRow(frame, tqlayout, row++, leclass, i18n("Login class:"), whatstr, true);
    } else {
      leoffice1 = new KLineEdit(frame);
      connect(leoffice1, TQT_SIGNAL(textChanged(const TQString &)), this, TQT_SLOT(changed()));
//    whatstr = i18n("WHAT IS THIS: Office1");
      addRow(frame, tqlayout, row++, leoffice1, i18n("&Office #1:"), whatstr);

      leoffice2 = new KLineEdit(frame);
      connect(leoffice2, TQT_SIGNAL(textChanged(const TQString &)), this, TQT_SLOT(changed()));
//    whatstr = i18n("WHAT IS THIS: Office2");
      addRow(frame, tqlayout, row++, leoffice2, i18n("O&ffice #2:"), whatstr);

      leaddress = new KLineEdit(frame);
      connect(leaddress, TQT_SIGNAL(textChanged(const TQString &)), this, TQT_SLOT(changed()));
//    whatstr = i18n("WHAT IS THIS: Address");
      addRow(frame, tqlayout, row++, leaddress, i18n("&Address:"), whatstr);
    }
    cbdisabled = new TQCheckBox(frame);
    connect(cbdisabled, TQT_SIGNAL(stateChanged(int)), this, TQT_SLOT(changed()));
    addRow(frame, tqlayout, row++, cbdisabled, i18n("Account &disabled"), whatstr);

    if ( kug->getUsers().getCaps() & KU::KUsers::Cap_Disable_POSIX ) {
      cbposix = new TQCheckBox(frame);
      connect(cbposix, TQT_SIGNAL(stateChanged(int)), this, TQT_SLOT(changed()));
      connect(cbposix, TQT_SIGNAL(stateChanged(int)), this, TQT_SLOT(cbposixChanged()));
      addRow(frame, tqlayout, row++, cbposix, i18n("Disable &POSIX account information"), whatstr);
    } else {
      cbposix = 0;
    }
    frontrow = row;
  }

  if ( kug->getUsers().getCaps() & KU::KUsers::Cap_Shadow ||
       kug->getUsers().getCaps() & KU::KUsers::Cap_Samba ||
       kug->getUsers().getCaps() & KU::KUsers::Cap_BSD ) {

  // Tab 2 : Password Management
    TQFrame *frame = addPage(i18n("Password Management"));
    TQGridLayout *tqlayout = new TQGridLayout(frame, 20, 4, marginHint(), spacingHint());
    int row = 0;

    TQDateTime time;
    leslstchg = new TQLabel(frame);
    addRow(frame, tqlayout, row++, leslstchg, i18n("Last password change:"), TQString(), true);

    tqlayout->addMultiCellWidget(new KSeparator(KSeparator::HLine, frame), row, row, 0, 3);
    row++;

    if ( kug->getUsers().getCaps() & KU::KUsers::Cap_Shadow ) {
      tqlayout->addWidget( new TQLabel( i18n("POSIX parameters:"), frame ), row++, 0 );
      lesmin = addDaysGroup(frame, tqlayout, row++, i18n("Time before password may &not be changed after last password change:"), false);
      lesmax = addDaysGroup(frame, tqlayout, row++, i18n("Time when password &expires after last password change:") );
      leswarn = addDaysGroup(frame, tqlayout, row++, i18n("Time before password expires to &issue an expire warning:"));
      lesinact = addDaysGroup(frame, tqlayout, row++, i18n("Time when account will be &disabled after expiration of password:"));
      tqlayout->addMultiCellWidget(new KSeparator(KSeparator::HLine, frame), row, row, 0, 3);
      row++;
    }
    /*
    if ( kug->getUsers().getCaps() & KU::KUsers::Cap_Samba ) {
      tqlayout->addWidget( new TQLabel( "SAMBA parameters:", frame ), row++, 0 );
      tqlayout->addMultiCellWidget(new KSeparator(KSeparator::HLine, frame), row, row, 0, 3);
      row++;
    }
    */
    TQLabel *label = new TQLabel( i18n("&Account will expire on:"), frame );
    tqlayout->addWidget( label, row, 0 );
    lesexpire = new KDateTimeWidget( frame );
    label->setBuddy( lesexpire );
    tqlayout->addMultiCellWidget( lesexpire, row, row, 1, 2);

    cbexpire = new TQCheckBox( i18n("Never"), frame );
    tqlayout->addWidget( cbexpire, row++, 3 );

    connect( lesexpire, TQT_SIGNAL(valueChanged(const TQDateTime&)), this, TQT_SLOT(changed()) );
    connect( cbexpire, TQT_SIGNAL(stateChanged(int)), this, TQT_SLOT(changed()) );
    connect( cbexpire, TQT_SIGNAL(toggled(bool)), lesexpire, TQT_SLOT(setDisabled(bool)) );
  }

  // Tab 3: Samba
  if ( kug->getUsers().getCaps() & KU::KUsers::Cap_Samba ) {
    TQFrame *frame = addPage(i18n("Samba"));
    TQGridLayout *tqlayout = new TQGridLayout(frame, 10, 4, marginHint(), spacingHint());
    int row = 0;

    lerid = new KLineEdit(frame);
//  whatstr = i18n("WHAT IS THIS: Rid");
    lerid->setValidator(new TQIntValidator(TQT_TQOBJECT(frame)));
    addRow(frame, tqlayout, row++, lerid, i18n("RID:"), whatstr);
    connect(lerid, TQT_SIGNAL(textChanged(const TQString &)), this, TQT_SLOT(changed()));

    leliscript = new KLineEdit(frame);
//  whatstr = i18n("WHAT IS THIS: Login script");
    addRow(frame, tqlayout, row++, leliscript, i18n("Login script:"), whatstr);
    connect(leliscript, TQT_SIGNAL(textChanged(const TQString &)), this, TQT_SLOT(changed()));

    leprofile = new KLineEdit(frame);
//  whatstr = i18n("WHAT IS THIS: Login script");
    addRow(frame, tqlayout, row++, leprofile, i18n("Profile path:"), whatstr);
    connect(leprofile, TQT_SIGNAL(textChanged(const TQString &)), this, TQT_SLOT(changed()));

    lehomedrive = new KLineEdit(frame);
//  whatstr = i18n("WHAT IS THIS: Login script");
    addRow(frame, tqlayout, row++, lehomedrive, i18n("Home drive:"), whatstr);
    connect(lehomedrive, TQT_SIGNAL(textChanged(const TQString &)), this, TQT_SLOT(changed()));

    lehomepath = new KLineEdit(frame);
//  whatstr = i18n("WHAT IS THIS: Login script");
    addRow(frame, tqlayout, row++, lehomepath, i18n("Home path:"), whatstr);
    connect(lehomepath, TQT_SIGNAL(textChanged(const TQString &)), this, TQT_SLOT(changed()));

    leworkstations = new KLineEdit(frame);
//  whatstr = i18n("WHAT IS THIS: Login script");
    addRow(frame, tqlayout, row++, leworkstations, i18n("User workstations:"), whatstr);
    connect(leworkstations, TQT_SIGNAL(textChanged(const TQString &)), this, TQT_SLOT(changed()));

    ledomain = new KLineEdit(frame);
//  whatstr = i18n("WHAT IS THIS: Login script");
    addRow(frame, tqlayout, row++, ledomain, i18n("Domain name:"), whatstr);
    connect(ledomain, TQT_SIGNAL(textChanged(const TQString &)), this, TQT_SLOT(changed()));

    ledomsid = new KLineEdit(frame);
//  whatstr = i18n("WHAT IS THIS: Login script");
    addRow(frame, tqlayout, row++, ledomsid, i18n("Domain SID:"), whatstr);
    connect(ledomsid, TQT_SIGNAL(textChanged(const TQString &)), this, TQT_SLOT(changed()));

    cbsamba = new TQCheckBox(frame);
    connect(cbsamba, TQT_SIGNAL(stateChanged(int)), this, TQT_SLOT(changed()));
    connect(cbsamba, TQT_SIGNAL(stateChanged(int)), this, TQT_SLOT(cbsambaChanged()));
    addRow(frame, tqlayout, row++, cbsamba, i18n("Disable &Samba account information"), whatstr);
  }

  // Tab 4: Groups
  {
    TQFrame *frame = addPage(i18n("Groups"));
    TQGridLayout *tqlayout = new TQGridLayout(frame, 2, 2, marginHint(), spacingHint());

    lstgrp = new KListView(frame);
    lstgrp->setFullWidth(true); // Single column, full widget width.
    lstgrp->addColumn( i18n("Groups") );
    if ( ro ) lstgrp->setSelectionMode( TQListView::NoSelection );
//    TQString whatstr = i18n("Select the groups that this user belongs to.");
    TQWhatsThis::add(lstgrp, whatstr);
    tqlayout->addMultiCellWidget(lstgrp, 0, 0, 0, 1);
    leprigr = new TQLabel( i18n("Primary group: "), frame );
    tqlayout->addWidget( leprigr, 1, 0 );
    if ( !ro ) {
      pbprigr = new TQPushButton( i18n("Set as Primary"), frame );
      tqlayout->addWidget( pbprigr, 1, 1 );
      connect( pbprigr, TQT_SIGNAL(clicked()), this, TQT_SLOT(setpgroup()) );
    }
    connect( lstgrp, TQT_SIGNAL(clicked(TQListViewItem *)), this, TQT_SLOT(gchanged()) );
  }

}

propdlg::propdlg( const TQPtrList<KU::KUser> &users,
  TQWidget *tqparent, const char *name ) :
  KDialogBase(Tabbed, i18n("User Properties"), Ok | Cancel, Ok, tqparent, name, true)

{
  mUsers = users;
  if ( mUsers.getFirst() != mUsers.getLast() )
    setCaption( i18n("User Properties - %1 Selected Users").tqarg( mUsers.count() ) );
  initDlg();
  loadgroups( false );
  selectuser();
  ischanged = false;
  isgchanged = false;
}

propdlg::propdlg( KU::KUser *AUser, bool fixedprivgroup,
  TQWidget *tqparent, const char *name ) :
  KDialogBase(Tabbed, i18n("User Properties"), Ok | Cancel, Ok, tqparent, name, true)

{
  mUsers.append( AUser );
  initDlg();
  loadgroups( fixedprivgroup );
  selectuser();
  ischanged = false;
  isgchanged = false;
}

propdlg::~propdlg()
{
}

void propdlg::cbposixChanged()
{
  bool posix = !( cbposix->state() == TQButton::On );
  leid->setEnabled( posix  & ( mUsers.getFirst() == mUsers.getLast() ) );
  leshell->setEnabled( posix );
  lehome->setEnabled( posix );
  if ( kug->getUsers().getCaps() & KU::KUsers::Cap_Shadow ) {
    lesmin->setEnabled( posix );
    lesmax->setEnabled( posix );
    leswarn->setEnabled( posix );
    lesinact->setEnabled( posix );
  }
}

void propdlg::cbsambaChanged()
{
  bool samba = !( cbsamba->state() == TQButton::On );
  lerid->setEnabled( samba & ( mUsers.getFirst() == mUsers.getLast() ) );
  leliscript->setEnabled( samba );
  leprofile->setEnabled( samba );
  lehomedrive->setEnabled( samba );
  lehomepath->setEnabled( samba );
  leworkstations->setEnabled( samba );
  ledomain->setEnabled( samba );
  ledomsid->setEnabled( samba );
}

void propdlg::setLE( KLineEdit *le, const TQString &val, bool first )
{
  if ( first ) {
    le->setText( val );
    if ( ro ) le->setReadOnly( true );
    return;
  }
  if ( val.isEmpty() && le->text().isEmpty() ) return;
  if ( le->text() != val ) {
    le->setText( "" );
    if ( !ro && mNoChanges.contains( le ) ) {
      mNoChanges[ le ]->show();
      mNoChanges[ le ]->setChecked( true );
    }
  }
}

void propdlg::setCB( TQCheckBox *cb, bool val, bool first )
{
  if ( first ) {
    cb->setChecked( val );
    if ( ro ) cb->setEnabled( false );
    return;
  }
  if ( cb->isChecked() != val ) {
    cb->setTristate();
    cb->setNoChange();
  }
}

void propdlg::setSB( KIntSpinBox *sb, int val, bool first )
{
  if ( first ) {
    sb->setValue( val );
    if ( ro ) sb->setEnabled( false );
    return;
  }
  if ( sb->value() != val ) {
    sb->setValue( 0 );
    if ( !ro && mNoChanges.contains( sb ) ) {
      mNoChanges[ sb ]->show();
      mNoChanges[ sb ]->setChecked( true );
    }
  }
}

void propdlg::selectuser()
{
  KU::KUser *user;
  bool first = true, one = ( mUsers.getFirst() == mUsers.getLast() );

  ismoreshells = false;
  user = mUsers.first();
  olduid = user->getUID();
  oldrid = user->getSID().getRID();
  oldshell = user->getShell();
  lstchg = user->getLastChange();
  TQDateTime datetime;
  datetime.setTime_t( lstchg );
  if ( kug->getUsers().getCaps() & KU::KUsers::Cap_Shadow ||
       kug->getUsers().getCaps() & KU::KUsers::Cap_Samba ||
       kug->getUsers().getCaps() & KU::KUsers::Cap_BSD ) {

    leslstchg->setText( KGlobal::locale()->formatDateTime( datetime, false ) );
  }

  if ( one ) {
    lbuser->setText( user->getName() );
    leid->setText( TQString::number( user->getUID() ) );
    if ( ro ) leid->setReadOnly( true );
    if ( kug->getUsers().getCaps() & KU::KUsers::Cap_Samba ) {
      lerid->setText( TQString::number( user->getSID().getRID() ) );
      if ( ro ) lerid->setReadOnly( true );
    }
  } else {
    leid->setEnabled( false );
    if ( kug->getUsers().getCaps() & KU::KUsers::Cap_Samba ) {
      lerid->setEnabled( false );
    }
  }
  if ( ro ) leshell->setEditable( false );

  while ( user ) {

    setLE( lefname, user->getFullName(), first );
    TQString home;
    home = user->getHomeDir();
    if ( !one ) home.replace( user->getName(), "%U" );
    setLE( lehome, home, first );

    TQString shell = user->getShell();
    if ( first ) {
      if ( !shell.isEmpty() ) {
        bool tested = false;
        for ( int i=0; i<leshell->count(); i++ )
          if ( leshell->text(i) == shell ) {
            tested = true;
            leshell->setCurrentItem(i);
            break;
          }
          if ( !tested ) {
            leshell->insertItem( shell );
            leshell->setCurrentItem( leshell->count()-1 );
          }
      } else
        leshell->setCurrentItem(0);
    } else {
      if ( leshell->currentText() != shell ) {
        if ( !ismoreshells ) {
          leshell->insertItem( i18n("Do Not Change"), 0 );
          ismoreshells = true;
        }
        leshell->setCurrentItem( 0 );
      }
    }

    setCB( cbdisabled, user->getDisabled(), first );
    if ( kug->getUsers().getCaps() & KU::KUsers::Cap_Disable_POSIX ) {
      setCB( cbposix, !(user->getCaps() & KU::KUser::Cap_POSIX), first );
    }

    if ( kug->getUsers().getCaps() & KU::KUsers::Cap_Samba ) {
      setLE( leliscript, user->getLoginScript(), first );
      TQString profile;
      profile = user->getProfilePath();
      if ( !one ) profile.replace( user->getName(), "%U" );
      setLE( leprofile, profile, first );
      setLE( lehomedrive, user->getHomeDrive(), first );
      home = user->getHomePath();
      if ( !one ) home.replace( user->getName(), "%U" );
      setLE( lehomepath, home, first );
      setLE( leworkstations, user->getWorkstations(), first );
      setLE( ledomain, user->getDomain(), first );
      setLE( ledomsid, user->getSID().getDOM(), first );
      setCB( cbsamba, !(user->getCaps() & KU::KUser::Cap_Samba), first );
    }

    if ( kug->getUsers().getCaps() & KU::KUsers::Cap_Shadow ||
         kug->getUsers().getCaps() & KU::KUsers::Cap_Samba ||
         kug->getUsers().getCaps() & KU::KUsers::Cap_BSD ) {

      if ( user->getLastChange() != lstchg ) {
        leslstchg->setText( "" );
        lstchg = 0;
      }

      TQDateTime expire;
      expire.setTime_t( user->getExpire() );
      kdDebug() << "expiration: " << user->getExpire() << endl;
      setCB( cbexpire, (int) expire.toTime_t() == -1, first );
      if ( (int) expire.toTime_t() == -1 ) expire.setTime_t( 0 );
      if ( first ) {
        lesexpire->setDateTime( expire );
      } else {
        if ( lesexpire->dateTime() != expire ) {
          expire.setTime_t( 0 );
          lesexpire->setDateTime( expire );
        }
      }
    }

    if ( kug->getUsers().getCaps() & KU::KUsers::Cap_Shadow ) {
      setSB( lesmin, user->getMin(), first );
      setSB( lesmax, user->getMax(), first );
      setSB( leswarn, user->getWarn(), first );
      setSB( lesinact, user->getInactive(), first );
    }

    if ( kug->getUsers().getCaps() & KU::KUsers::Cap_InetOrg ) {
      setLE( lesurname, user->getSurname(), first );
      setLE( lemail, user->getEmail(), first );
    }
    if ( kug->getUsers().getCaps() & KU::KUsers::Cap_BSD ) {
      setLE( leoffice, user->getOffice(), first );
      setLE( leophone, user->getWorkPhone(), first );
      setLE( lehphone, user->getHomePhone(), first );
      setLE( leclass, user->getClass(), first );
    } else {
      setLE( leoffice1, user->getOffice1(), first );
      setLE( leoffice2, user->getOffice2(), first );
      setLE( leaddress, user->getAddress(), first );
    }

    first = false;
    user = mUsers.next();
  }
}

void propdlg::loadgroups( bool fixedprivgroup )
{
  bool wasprivgr = false;

  primaryGroupWasOn = false;

  KU::KGroup *group = kug->getGroups().first();
  while ( group ) {
    TQString groupName = group->getName();
    TQCheckListItem *item = new TQCheckListItem(lstgrp, groupName, TQCheckListItem::CheckBox);
    KU::KUser *user = mUsers.first();
    while ( user ) {
      bool prigr =
        ( !fixedprivgroup && group->getGID() == user->getGID() ) ||
        ( fixedprivgroup && groupName == user->getName() );
      bool on = group->lookup_user( user->getName() ) || prigr;

      if ( prigr ) {
        item->setEnabled( false );
        if ( !wasprivgr )
          primaryGroup = groupName;
        else
          if ( primaryGroup != groupName ) primaryGroup = "";
//      primaryGroupWasOn = group->lookup_user(user->getName());
        wasprivgr = true;
      }

      if ( mUsers.getFirst() == user )
        item->setOn( on );
      else
        if ( item->isOn() != on ) {
          item->setTristate( true );
          item->setState( TQCheckListItem::NoChange );
        }
      user = mUsers.next();
    }
    group = kug->getGroups().next();
  }

  if ( fixedprivgroup ) {
    KU::KUser *user = mUsers.first();
    kdDebug() << "privgroup: " << user->getName() << endl;
    if ( !wasprivgr ) {
      TQCheckListItem *item = new TQCheckListItem(lstgrp, user->getName(), TQCheckListItem::CheckBox);
      item->setOn(true);
      item->setEnabled(false);
      primaryGroup = user->getName();
    }
  }
  leprigr->setText( i18n("Primary group: ") + primaryGroup );
}

void propdlg::setpgroup()
{
  isgchanged = true;
  TQCheckListItem *item;
  item = (TQCheckListItem *) lstgrp->selectedItem();
  if ( item == 0 || item->text() == primaryGroup )
     return;

  bool prevPrimaryGroupWasOn = primaryGroupWasOn;
  primaryGroup = ((TQCheckListItem *) lstgrp->selectedItem())->text();

  item = (TQCheckListItem *) lstgrp->firstChild();

  while(item)
  {
     TQString groupName = item->text();
     if ( !item->isEnabled() )
     {
        item->setEnabled(true);
        item->setOn(prevPrimaryGroupWasOn);
        item->tqrepaint();
     }
     if ( groupName == primaryGroup )
     {
        primaryGroupWasOn = item->isOn();
        item->setEnabled(false);
        item->setOn(true);
        item->tqrepaint();
     }

     item = (TQCheckListItem *) item->nextSibling();
  }
  leprigr->setText( i18n("Primary group: ") + primaryGroup );
}

void propdlg::changed()
{
  TQWidget *widget = (TQWidget*) sender();
  if ( mNoChanges.contains( widget ) ) mNoChanges[ widget ]->setChecked( false );
  ischanged = true;
  kdDebug() << "changed" << endl;
}

void propdlg::gchanged()
{
  isgchanged = true;
}

TQString propdlg::mergeLE( KLineEdit *le, const TQString &val, bool one )
{
  TQCheckBox *cb = 0;
  if ( mNoChanges.contains( le ) ) cb = mNoChanges[ le ];
  return ( one || ( cb && !cb->isChecked() ) ) ? le->text() : val;
}

int propdlg::mergeSB( KIntSpinBox *sb, int val, bool one )
{
  TQCheckBox *cb = 0;
  if ( mNoChanges.contains( sb ) ) cb = mNoChanges[ sb ];
  return ( one || ( cb && !cb->isChecked() ) ) ? sb->value() : val;
}

void propdlg::mergeUser( KU::KUser *user, KU::KUser *newuser )
{
  TQDateTime epoch ;
  epoch.setTime_t(0);
  bool one = ( mUsers.getFirst() == mUsers.getLast() );
  bool posix, samba = false;

  newuser->copy( user );

  if ( kug->getUsers().getCaps() & KU::KUsers::Cap_Disable_POSIX && cbposix->state() != TQButton::NoChange ) {
    if ( cbposix->isChecked() )
      newuser->setCaps( newuser->getCaps() & ~KU::KUser::Cap_POSIX );
    else
      newuser->setCaps( newuser->getCaps() | KU::KUser::Cap_POSIX );
  }
  posix = newuser->getCaps() & KU::KUser::Cap_POSIX;
  kdDebug() << "posix: " << posix << endl;
  if ( one ) {
//    newuser->setName( leuser->text() );
    newuser->setUID( posix ? leid->text().toInt() : 0 );
  }
  if ( !newpass.isNull() ) {
    kug->getUsers().createPassword( newuser, newpass );
    newuser->setLastChange( lstchg );
  }
  newuser->setFullName( mergeLE( lefname, user->getFullName(), one ) );
  if ( kug->getUsers().getCaps() & KU::KUsers::Cap_Samba ) {
    if ( cbsamba->state() != TQButton::NoChange ) {
      if ( cbsamba->isChecked() )
        newuser->setCaps( newuser->getCaps() & ~KU::KUser::Cap_Samba );
      else
        newuser->setCaps( newuser->getCaps() | KU::KUser::Cap_Samba );
    }
    samba = newuser->getCaps() & KU::KUser::Cap_Samba;
    kdDebug() << "user : " << newuser->getName() << " caps: " << newuser->getCaps() << " samba: " << samba << endl;

    SID sid;
    if ( samba ) {
      sid.setRID( one ? lerid->text().toUInt() : user->getSID().getRID() );
      sid.setDOM( mergeLE( ledomsid, user->getSID().getDOM(), one ) );
    }
    newuser->setSID( sid );
    newuser->setLoginScript( samba ? 
      mergeLE( leliscript, user->getLoginScript(), one ) : TQString()  );
    newuser->setProfilePath( samba ? 
      mergeLE( leprofile, user->getProfilePath(), one ).replace( "%U", newuser->getName() ) : TQString() );
    newuser->setHomeDrive( samba ? 
      mergeLE( lehomedrive, user->getHomeDrive(), one ) : TQString() );
    newuser->setHomePath( samba ? 
      mergeLE( lehomepath, user->getHomePath(), one ).replace( "%U", newuser->getName() ) : TQString() );
    newuser->setWorkstations( samba ? 
      mergeLE( leworkstations, user->getWorkstations(), one ) : TQString() );
    newuser->setDomain( samba ? 
      mergeLE( ledomain, user->getDomain(), one ) : TQString() );
  }

  if ( kug->getUsers().getCaps() & KU::KUsers::Cap_BSD ) {
    newuser->setOffice( mergeLE( leoffice, user->getOffice(), one ) );
    newuser->setWorkPhone( mergeLE( leophone, user->getWorkPhone(), one ) );
    newuser->setHomePhone( mergeLE( lehphone, user->getHomePhone(), one ) );
    newuser->setClass( mergeLE( leclass, user->getClass(), one ) );
  } else {
    newuser->setOffice1( mergeLE( leoffice1, user->getOffice1(), one ) );
    newuser->setOffice2( mergeLE( leoffice2, user->getOffice2(), one ) );
    newuser->setAddress( mergeLE( leaddress, user->getAddress(), one ) );
  }

  newuser->setHomeDir( posix ? 
    mergeLE( lehome, user->getHomeDir(), one ).replace( "%U", newuser->getName() ) : 
    TQString() );
  if ( posix ) {
    if ( leshell->currentItem() == 0 && ismoreshells ) {
      newuser->setShell( user->getShell() );
    } else if  (
      ( leshell->currentItem() == 0 && !ismoreshells ) ||
      ( leshell->currentItem() == 1 && ismoreshells ) ) {

      newuser->setShell( TQString() );
    } else {
  // TODO: Check shell.
      newuser->setShell( leshell->currentText() );
    }
  } else
    newuser->setShell( TQString() );

  newuser->setDisabled( (cbdisabled->state() == TQButton::NoChange) ? user->getDisabled() : cbdisabled->isChecked() );

  if ( kug->getUsers().getCaps() & KU::KUsers::Cap_InetOrg ) {
    newuser->setSurname( mergeLE( lesurname, user->getSurname(), one ) );
    newuser->setEmail( mergeLE( lemail, user->getEmail(), one ) );
  }

  if ( kug->getUsers().getCaps() & KU::KUsers::Cap_Shadow ) {
    newuser->setMin( posix ? mergeSB( lesmin, user->getMin(), one ) : 0 );
    newuser->setMax( posix ? mergeSB( lesmax, user->getMax(), one ) : 0 );
    newuser->setWarn( posix ? mergeSB( leswarn, user->getWarn(), one ) : 0 );
    newuser->setInactive( posix ? mergeSB( lesinact, user->getInactive(), one ) : 0 );
  }

  if ( ( (kug->getUsers().getCaps() & KU::KUsers::Cap_Shadow) && posix ) ||
       ( (kug->getUsers().getCaps() & KU::KUsers::Cap_Samba) && samba ) ||
       ( (kug->getUsers().getCaps() & KU::KUsers::Cap_BSD) && posix ) ) {

    switch ( cbexpire->state() ) {
      case TQButton::NoChange:
        newuser->setExpire( user->getExpire() );
        break;
      case TQButton::On:
        newuser->setExpire( -1 );
        break;
      case TQButton::Off:
        newuser->setExpire( !one && lesexpire->dateTime().toTime_t() == 0 ?
          user->getExpire() : lesexpire->dateTime().toTime_t() );
        break;
    }
  } else {
    newuser->setExpire( -1 );
  }

  if ( !primaryGroup.isEmpty() ) {
    KU::KGroup *group = kug->getGroups().lookup( primaryGroup );
    if ( group ) {
      newuser->setGID( group->getGID() );
      if ( kug->getUsers().getCaps() & KU::KUsers::Cap_Samba ) {
        newuser->setPGSID( group->getSID() );
      }
    }
  }
}

bool propdlg::saveg()
{
  if ( !isgchanged ) return true;

  TQCheckListItem *item = (TQCheckListItem *) lstgrp->firstChild();
  KU::KGroup *group;

  while(item)
  {
    kdDebug() << "saveg: group name: " << item->text() << endl;
    group = kug->getGroups().lookup(item->text());
    if ( group && item->state() != TQCheckListItem::NoChange ) {

      KU::KGroup newgroup( group );
      bool mod = false;
      bool on = item->isOn();
      KU::KUser *user = mUsers.first();

      while ( user ) {
        if ( on && (( !primaryGroup.isEmpty() && primaryGroup != group->getName() ) ||
                    ( primaryGroup.isEmpty() && user->getGID() != group->getGID() )) ) {
          if ( newgroup.addUser( user->getName() ) ) mod = true;
        } else {
          if ( newgroup.removeUser( user->getName() ) ) mod = true;
        }
        user = mUsers.next();
      }

      if ( mod ) kug->getGroups().mod( group, newgroup );
    }
    item = (TQCheckListItem *) item->nextSibling();
  }
  return true;
}

bool propdlg::checkShell(const TQString &shell)
{
   if (shell.isEmpty())
      return true;
   TQStringList shells = readShells();
   return shells.contains(shell);
}

bool propdlg::check()
{
  bool one = ( mUsers.getFirst() == mUsers.getLast() );
  bool posix = !( kug->getUsers().getCaps() & KU::KUsers::Cap_Disable_POSIX ) || !( cbposix->isChecked() );

  if ( one && posix && leid->text().isEmpty() ) {
    KMessageBox::sorry( 0, i18n("You need to specify an UID.") );
    return false;
  }

  if ( one && posix && lehome->text().isEmpty() ) {
    KMessageBox::sorry( 0, i18n("You must specify a home directory.") );
    return false;
  }

  if ( kug->getUsers().getCaps() & KU::KUsers::Cap_InetOrg ) {
    if ( one && lesurname->text().isEmpty() ) {
      KMessageBox::sorry( 0, i18n("You must fill the surname field.") );
      return false;
    }
  }

  if ( kug->getUsers().getCaps() & KU::KUsers::Cap_Samba ) {
    if ( one && lerid->text().isEmpty() && !( cbsamba->isChecked() ) ) {
      KMessageBox::sorry( 0, i18n("You need to specify a samba RID.") );
      return false;
    }
  }

  return true;
}

void propdlg::setpwd()
{
  pwddlg pd( this );

  if ( pd.exec() == TQDialog::Accepted ) {
    ischanged = true;
    newpass = pd.getPassword();
    lstchg = now();
    TQDateTime datetime;
    datetime.setTime_t( lstchg );
    if ( kug->getUsers().getCaps() & KU::KUsers::Cap_Shadow ||
        kug->getUsers().getCaps() & KU::KUsers::Cap_Samba ||
        kug->getUsers().getCaps() & KU::KUsers::Cap_BSD ) {

        leslstchg->setText( KGlobal::locale()->formatDateTime( datetime, false ) );
    }
    cbdisabled->setChecked( false );
  }
}

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

  bool one = ( mUsers.getFirst() == mUsers.getLast() );

  uid_t newuid = leid->text().toULong();

  if ( one && ( !( kug->getUsers().getCaps() & KU::KUsers::Cap_Disable_POSIX ) || !cbposix->isChecked() )
               && olduid != newuid )
  {
    if (kug->getUsers().lookup(newuid)) {
      KMessageBox::sorry( 0,
        i18n("User with UID %1 already exists").tqarg(newuid) );
      return;
    }
  }

  if ( one && ( kug->getUsers().getCaps() & KU::KUsers::Cap_Samba ) && !cbsamba->isChecked() ) {
    uint newrid = lerid->text().toInt();
    if ( oldrid != newrid ) {
      if (kug->getUsers().lookup_sam(newrid)) {
        KMessageBox::sorry( 0,
          i18n("User with RID %1 already exists").tqarg(newrid) );
        return;
      }
    }
  }

  TQString newshell;
  if (leshell->currentItem() != 0)
    newshell = leshell->currentText();

  if (oldshell != newshell)
  {
    if (!checkShell(newshell)) {
      int result = KMessageBox::warningYesNoCancel( 0,
      		i18n("<p>The shell %1 is not yet listed in the file %2. "
      		     "In order to use this shell you must add it to "
      		     "this file first."
      		     "<p>Do you want to add it now?").tqarg(newshell).tqarg(TQFile::decodeName(SHELL_FILE)),
      		i18n("Unlisted Shell"),
      		i18n("&Add Shell"),
      		i18n("Do &Not Add"));
      	if (result == KMessageBox::Cancel)
      	  return;

      	if (result == KMessageBox::Yes)
      	  addShell(newshell);
    }
  }

  if ( !ischanged && !isgchanged ) {
    reject();
  } else if ( check() ) {
    saveg();
    accept();
  }
}

#include "propdlg.moc"