Proof-reading - fixed one usage of the i18n plural form (it wasn't doing before,...
[kdeadmin.git] / kuser / ku_adduser.cpp
blob3f769856a59f8f2502ea20db682b02b07fc7e430
1 /*
2 * Copyright (c) 1998 Denis Perchine <dyp@perchine.com>
3 * Copyright (c) 2004 Szombathelyi György <gyurco@freemail.hu>
4 * Former maintainer: Adriaan de Groot <groot@kde.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public
8 * License version 2 or at your option version 3 as published by
9 * the Free Software Foundation.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 **/
22 #include "globals.h"
24 #include <errno.h>
25 #ifdef HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28 #include <sys/types.h>
29 #include <sys/file.h>
30 #include <sys/stat.h>
32 #include <QVBoxLayout>
33 #include <QGroupBox>
35 #include <kmessagebox.h>
36 #include <kdebug.h>
37 #include <klocale.h>
39 #include "ku_global.h"
40 #include "ku_adduser.h"
42 KU_AddUser::KU_AddUser( KU_User &user, bool useprivategroup,
43 QWidget *parent ) :
44 KU_EditUser( user, useprivategroup, parent )
46 QGroupBox *group = new QGroupBox(frontpage);
47 group->setTitle(i18n("New Account Options"));
48 QVBoxLayout *grouplayout = new QVBoxLayout( group );
49 grouplayout->setMargin( marginHint() );
50 grouplayout->setSpacing( spacingHint() );
51 // grouplayout->addSpacing( group->fontMetrics().lineSpacing() );
53 createhome = new QCheckBox(i18n("Create home folder"), 0);
54 createhome->setChecked(true);
55 grouplayout->addWidget( createhome );
56 copyskel = new QCheckBox(i18n("Copy skeleton"), 0);
57 grouplayout->addWidget( copyskel );
59 connect(createhome, SIGNAL(toggled(bool)), copyskel, SLOT(setEnabled(bool)));
60 frontlayout->addWidget(group, frontrow, 0, 1, 3);
62 if ( useprivategroup ) pbprigr->setEnabled( false );
64 mNewUser = user;
67 void KU_AddUser::accept()
69 if ( !check() ) return;
71 mergeUser( mNewUser, mNewUser );
72 if ( ( mNewUser.getCaps() & KU_User::Cap_POSIX ) &&
73 KU_Global::users()->lookup( mNewUser.getUID() ) != -1 ) {
74 KMessageBox::sorry( 0, i18n("User with UID %1 already exists.", mNewUser.getUID() ) );
75 return;
78 if ( ( KU_Global::users()->getCaps() & KU_Users::Cap_Samba ) &&
79 ( mNewUser.getCaps() & KU_User::Cap_Samba ) ) {
80 if ( KU_Global::users()->lookup_sam( mNewUser.getSID().getRID() ) != -1 ) {
81 KMessageBox::sorry( 0, i18n("User with RID %1 already exists.", mNewUser.getSID().getRID() ) );
82 return;
86 if (createhome->isChecked())
88 mNewUser.setCreateHome(true);
89 mNewUser.setCreateMailBox(true);
91 if (copyskel->isChecked())
93 mNewUser.setCopySkel(true);
96 if (mNewUser.getCreateHome() && !checkHome())
97 return;
99 if (mNewUser.getCreateMailBox() && !checkMailBox())
100 mNewUser.setCreateMailBox(false);
102 saveg();
103 done( Accepted );
104 kDebug() << "slotOk name: " << mNewUser.getName();
108 bool KU_AddUser::checkHome()
110 struct stat s;
111 int r;
113 QString h_dir = mNewUser.getHomeDir();
114 r = stat( QFile::encodeName(h_dir), &s );
116 if ( (r == -1) && (errno == ENOENT) )
117 return true;
119 if (r == 0) {
120 if (S_ISDIR(s.st_mode)) {
121 if ( KMessageBox::
122 warningContinueCancel ( 0,
123 i18n("Folder %1 already exists.\n%2 may become owner and permissions may change.\nDo you really want to use %3?",
124 h_dir, mNewUser.getName(), h_dir), QString(), KStandardGuiItem::cont() ) == KMessageBox::Cancel )
126 return false;
127 else
128 return true;
129 } else
130 KMessageBox::error( 0, i18n("%1 is not a folder.", h_dir) );
131 } else
132 KMessageBox::error( 0, i18n("stat() failed on %1.", h_dir) );
134 return false;
137 bool KU_AddUser::checkMailBox()
139 QString mailboxpath;
141 struct stat s;
142 int r;
144 mailboxpath = QFile::decodeName(MAIL_SPOOL_DIR) + '/' + mNewUser.getName();
145 r = stat(QFile::encodeName(mailboxpath), &s);
147 if ((r == -1) && (errno == ENOENT))
148 return true;
150 if (r == 0)
151 if (S_ISREG(s.st_mode))
152 KMessageBox::error( 0, i18n("Mailbox %1 already exists (uid=%2).",
153 mailboxpath,
154 s.st_uid) );
155 else
156 KMessageBox::error( 0, i18n("%1 exists but is not a regular file.",
157 mailboxpath) );
158 else
159 KMessageBox::error( 0, i18n("stat() failed on %1.", mailboxpath) );
161 return false;
163 #include "ku_adduser.moc"