summaryrefslogtreecommitdiffstats
path: root/kuser/addUser.cpp
blob: ffb7b21dd891915511b1f05397fe6a3b2ed8861d (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
/*
 *  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 <errno.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <sys/types.h>
#include <sys/file.h>
#include <sys/stat.h>

#include <tqdir.h>
#include <tqgroupbox.h>

#include <kdebug.h>

#include "kglobal_.h"
#include "misc.h"

#include "addUser.h"
#include <kmessagebox.h>

addUser::addUser( KU::KUser *AUser, bool useprivategroup,
  TQWidget *tqparent, const char *name ) :
  propdlg( AUser, useprivategroup, tqparent, name )
{
  TQGroupBox *group = new TQGroupBox(frontpage);
  group->setTitle(i18n("New Account Options"));
  TQVBoxLayout *groupLayout = new TQVBoxLayout(group, marginHint(), spacingHint());
  groupLayout->addSpacing(group->fontMetrics().lineSpacing());
  groupLayout->setAutoAdd(true);
  createhome = new TQCheckBox(i18n("Create home folder"), group);
  createhome->setChecked(true);
  copyskel = new TQCheckBox(i18n("Copy skeleton"), group);
  connect(createhome, TQT_SIGNAL(toggled(bool)), copyskel, TQT_SLOT(setEnabled(bool)));
  fronttqlayout->addMultiCellWidget(group, frontrow, frontrow, 0, 2);

  if ( useprivategroup ) pbprigr->setEnabled( false );
}

void addUser::slotOk()
{
  KU::KUser *user = mUsers.first();

  if ( !check() ) return;

  mergeUser( user, user );

  if ( ( user->getCaps() & KU::KUser::Cap_POSIX ) && 
    kug->getUsers().lookup( user->getUID() ) ) {
    KMessageBox::sorry( 0, i18n("User with UID %1 already exists.").tqarg( user->getUID() ) );
    return;
  }

  if ( ( kug->getUsers().getCaps() & KU::KUsers::Cap_Samba ) && 
     ( user->getCaps() & KU::KUser::Cap_Samba ) ) {
    if ( kug->getUsers().lookup_sam( user->getSID().getRID() ) ) {
      KMessageBox::sorry( 0, i18n("User with RID %1 already exists.").tqarg( user->getSID().getRID() ) );
      return;
    }
  }

  if (createhome->isChecked())
  {
    user->setCreateHome(true);
    user->setCreateMailBox(true);
  }
  if (copyskel->isChecked())
  {
    user->setCopySkel(true);
  }

  if (user->getCreateHome() && !checkHome())
     return;

  if (user->getCreateMailBox() && !checkMailBox())
     user->setCreateMailBox(false);

  saveg();
  accept();
}

bool addUser::checkHome()
{
  KU::KUser *user = mUsers.first();

  struct stat s;
  int r;

  TQString h_dir = user->getHomeDir();
  r = stat( TQFile::encodeName(h_dir), &s );

  if ( (r == -1) && (errno == ENOENT) )
    return true;

  if (r == 0) {
    if (S_ISDIR(s.st_mode)) {
       if ( KMessageBox::
         warningContinueCancel ( 0,
           i18n("Folder %1 already exists!\n%2 may become owner and permissions may change.\nDo you really want to use %3?").
           tqarg(h_dir).tqarg(user->getName()).tqarg(h_dir), TQString(), KStdGuiItem::cont() ) == KMessageBox::Cancel )

         return false;
       else
         return true;
    } else
      KMessageBox::error( 0, i18n("%1 is not a folder.").tqarg(h_dir) );
  } else
    KMessageBox::error( 0, i18n("stat() failed on %1.").tqarg(h_dir) );

  return false;
}

bool addUser::checkMailBox()
{
  TQString mailboxpath;
  KU::KUser *user = mUsers.first();

  struct stat s;
  int r;

  mailboxpath = TQFile::decodeName(MAIL_SPOOL_DIR) + "/" + user->getName();
  r = stat(TQFile::encodeName(mailboxpath), &s);

  if ((r == -1) && (errno == ENOENT))
    return true;

  if (r == 0)
    if (S_ISREG(s.st_mode))
      KMessageBox::error( 0, i18n("Mailbox %1 already exists (uid=%2).")
                 .tqarg(mailboxpath)
                 .tqarg(s.st_uid) );
    else
      KMessageBox::error( 0, i18n("%1 exists but is not a regular file.")
                 .tqarg(mailboxpath) );
  else
    KMessageBox::error( 0, i18n("stat() failed on %1.").tqarg(mailboxpath) );

  return false;
}
#include "addUser.moc"