SVN_SILENT made messages (.desktop file)
[kdeadmin.git] / kuser / ku_mainview.cpp
blobee2abcde2d7644d8bd0cb64078e02b3aa876364f
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 **/
23 #include <stdio.h>
26 #include <kinputdialog.h>
27 #include <ktoolbar.h>
28 #include <kiconloader.h>
29 #include <kmessagebox.h>
30 #include <klocale.h>
31 #include <kdebug.h>
33 #include "ku_misc.h"
34 #include "ku_edituser.h"
35 #include "ku_adduser.h"
36 #include "ku_deluser.h"
37 #include "ku_pwdlg.h"
38 #include "ku_editgroup.h"
39 #include "ku_global.h"
41 #include "ku_mainview.h"
43 KU_MainView::KU_MainView(QWidget *parent) : KTabWidget(parent)
45 init();
48 void KU_MainView::init() {
50 users = 0;
51 groups = 0;
52 usermodel = 0;
53 groupmodel = 0;
55 userview = new QTreeView( 0 );
56 userview->setSelectionMode( QAbstractItemView::ExtendedSelection );
57 userview->setItemsExpandable( false );
58 userview->setRootIsDecorated( false );
59 userview->setSortingEnabled( true );
60 userview->setUniformRowHeights( true );
61 addTab( userview, i18n("Users") );
63 groupview = new QTreeView( 0 );
64 groupview->setSelectionMode( QAbstractItemView::ExtendedSelection );
65 groupview->setItemsExpandable( false );
66 groupview->setRootIsDecorated( false );
67 groupview->setSortingEnabled( true );
68 groupview->setUniformRowHeights( true );
69 addTab( groupview, i18n("Groups") );
71 connect(userview, SIGNAL(activated(const QModelIndex&)), this, SLOT(userSelected()));
72 connect(groupview, SIGNAL(activated(const QModelIndex&)), this, SLOT(groupSelected()));
74 connect(this, SIGNAL(currentChanged(QWidget *)), this, SLOT(slotTabChanged()));
77 KU_MainView::~KU_MainView()
81 void KU_MainView::slotTabChanged()
83 if (currentWidget() == userview)
85 emit userSelected(true);
86 emit groupSelected(false);
88 else
90 emit userSelected(false);
91 emit groupSelected(true);
95 void KU_MainView::clearUsers()
97 // lbusers->clear();
100 void KU_MainView::clearGroups()
102 // lbgroups->clear();
105 void KU_MainView::reloadUsers()
107 users = KU_Global::users();
108 if ( usermodel == 0 ) {
109 usermodel = new KU_UserModel;
110 userproxymodel.setSourceModel( usermodel );
111 userview->setModel( &userproxymodel );
113 userproxymodel.setFirstUser( mShowSys ? 0 : KU_Global::kcfg()->firstUID() );
114 usermodel->init();
115 userview->sortByColumn( 0, Qt::AscendingOrder );
118 void KU_MainView::reloadGroups()
120 groups = KU_Global::groups();
121 if ( groupmodel == 0 ) {
122 groupmodel = new KU_GroupModel;
123 groupproxymodel.setSourceModel( groupmodel );
124 groupview->setModel( &groupproxymodel );
126 groupproxymodel.setFirstGroup( mShowSys ? 0 : KU_Global::kcfg()->firstGID() );
127 groupmodel->init();
128 groupview->sortByColumn( 0, Qt::AscendingOrder );
131 bool KU_MainView::queryClose()
133 return true;
136 void KU_MainView::setpwd()
138 QModelIndexList selectedindexes = userview->selectionModel()->selectedIndexes();
139 int count = selectedindexes.count() / usermodel->columnCount();
141 if ( count == 0 ) return;
142 if ( count > 1 ) {
143 if ( KMessageBox::questionYesNo( 0,
144 i18n("You have selected %1 users. Do you really want to change the password for all the selected users?",
145 count ), QString(), KGuiItem(i18n("Change")), KGuiItem(i18n("Do Not Change")) ) == KMessageBox::No ) return;
147 KU_PwDlg d( this );
148 if ( d.exec() != QDialog::Accepted ) return;
150 KU_User user;
151 int index;
153 foreach( const QModelIndex &selectedindex, selectedindexes ) {
154 if ( selectedindex.column() != 0 ) continue;
155 index = userproxymodel.mapToSource(selectedindex).row();
156 user = users->at( index );
157 kDebug() << "Changing password for '" << user.getName() << "'";
158 users->createPassword( user, d.getPassword() );
159 user.setLastChange( now() );
160 user.setDisabled( false );
161 users->mod( index, user );
163 updateUsers();
166 void KU_MainView::userSelected()
168 useredit();
171 void KU_MainView::groupSelected()
173 grpedit();
176 void KU_MainView::useradd()
178 KU_User user;
180 setCurrentIndex(0);
182 uid_t uid, rid = 0;
183 bool samba = users->getCaps() & KU_Users::Cap_Samba;
185 if ((uid = users->first_free()) == KU_Users::NO_FREE) {
186 KMessageBox::sorry( 0, i18n("You have run out of uid space.") );
187 return;
190 if ( samba && (rid = users->first_free_sam()) == 0) {
191 KMessageBox::sorry( 0, i18n("You have run out of user RID space.") );
192 return;
195 if ( samba ) rid = SID::uid2rid( uid );
196 bool ok;
197 QString name = KInputDialog::getText( QString::null, //krazy:exclude=nullstrassign for old broken gcc
198 i18n("Please type the name of the new user:"),
199 QString(), &ok );
201 if ( !ok ) return;
203 if ( users->lookup( name ) != -1 ) {
204 KMessageBox::sorry( 0, i18n("User with name %1 already exists.", name ) );
205 return;
208 user.setCaps( samba ? KU_User::Cap_POSIX | KU_User::Cap_Samba : KU_User::Cap_POSIX );
209 user.setUID( uid );
210 user.setName( name );
212 if ( samba ) {
213 SID sid;
214 sid.setDOM( users->getDOMSID() );
215 sid.setRID( rid );
216 user.setSID( sid );
217 user.setProfilePath( KU_Global::kcfg()->samprofilepath().replace( "%U",name,Qt::CaseInsensitive ) );
218 user.setHomePath( KU_Global::kcfg()->samhomepath().replace( "%U", name,Qt::CaseInsensitive ) );
219 user.setHomeDrive( KU_Global::kcfg()->samhomedrive() );
220 user.setLoginScript( KU_Global::kcfg()->samloginscript() );
221 user.setDomain( KU_Global::kcfg()->samdomain() );
224 user.setShell( KU_Global::kcfg()->shell() );
225 user.setHomeDir( KU_Global::kcfg()->homepath().replace( "%U", name,Qt::CaseInsensitive ) );
226 if ( users->getCaps() & KU_Users::Cap_Shadow || samba ) {
227 user.setLastChange( now() );
230 user.setMin( KU_Global::kcfg()->smin() );
231 user.setMax( KU_Global::kcfg()->smax() );
232 user.setWarn( KU_Global::kcfg()->swarn() );
233 user.setInactive( KU_Global::kcfg()->sinact() );
234 user.setExpire( KU_Global::kcfg()->sneverexpire() ? (uint) -1 :
235 (KU_Global::kcfg()->sexpire()).toTime_t() );
237 bool privgroup = KU_Global::kcfg()->userPrivateGroup();
239 if ( !privgroup ) user.setGID( KU_Global::kcfg()->defaultgroup() );
241 KU_AddUser au( user, privgroup, this );
243 au.setCreateHomeDir( KU_Global::kcfg()->createHomeDir() );
244 au.setCopySkel( KU_Global::kcfg()->copySkel() );
246 if ( au.exec() == QDialog::Rejected ) {
247 return;
249 user = au.getNewUser();
250 kDebug() << " surname: " << user.getSurname();
251 if ( privgroup ) {
252 KU_Group group;
253 int index;
255 index = groups->lookup(user.getName());
256 //if no group exists with the user's name, create one
257 if ( index == -1 ) {
258 gid_t gid;
259 if ( groups->lookup( user.getUID() ) == -1 ) {
260 gid = user.getUID();
261 } else {
262 gid = groups->first_free();
264 kDebug() << "private group GID: " << gid;
265 uid_t rid = 0;
266 // if ( samba ) rid = KU_Global::getGroups().first_free_sam();
267 if ( samba ) rid = SID::gid2rid( gid );
268 if ( gid == KU_Groups::NO_FREE || ( samba && rid == 0 ) ) {
269 groups->cancelMods();
270 return;
272 group.setGID( gid );
273 if ( samba && ( user.getCaps() & KU_User::Cap_Samba ) ) {
274 SID sid;
275 sid.setDOM( groups->getDOMSID() );
276 sid.setRID( rid );
277 group.setSID( sid );
278 group.setDisplayName( user.getName() );
279 group.setCaps( KU_Group::Cap_Samba );
281 group.setName( user.getName() );
282 groups->add( group );
283 } else {
284 group = groups->at(index);
286 user.setGID( group.getGID() );
287 user.setPGSID( group.getSID() );
289 users->doCreate(&user);
290 users->add( user );
291 if ( !updateUsers() ) {
292 groups->cancelMods();
293 return;
295 updateGroups();
298 void KU_MainView::useredit()
300 QList<int> selected;
301 QModelIndexList selectedindexes = userview->selectionModel()->selectedIndexes();
303 foreach( const QModelIndex &selectedindex, selectedindexes ) {
304 if ( selectedindex.column() == 0 )
305 selected.append( userproxymodel.mapToSource(selectedindex).row() );
307 if ( selected.isEmpty() ) return;
309 KU_EditUser editUser( selected, this );
310 if ( editUser.exec() == QDialog::Rejected ) return;
312 KU_User user;
313 foreach(int i, selected) {
314 editUser.mergeUser( users->at(i), user );
315 users->mod( i, user );
317 updateUsers();
318 updateGroups();
321 void KU_MainView::userdel()
323 QModelIndex currentindex = userview->selectionModel()->currentIndex();
324 if ( !currentindex.isValid() ) return;
326 int index = userproxymodel.mapToSource(currentindex).row();
327 kDebug() << "selected index: " << index;
329 KU_User user = users->at(index);
330 QString username = user.getName();
331 gid_t gid = user.getGID();
332 KU_DelUser dlg(&user, this);
334 if ( dlg.exec() == QDialog::Rejected )
335 return;
337 user.setDeleteHome( dlg.getDeleteHomeDir() );
338 user.setDeleteMailBox( dlg.getDeleteMailBox() );
341 users->doDelete(&user);
342 users->del( index );
343 if ( !updateUsers() ) return;
345 for ( int i = 0; i < groups->count(); i++ ) {
346 KU_Group group = groups->at(i);
347 kDebug() << "group: " << group.getName();
348 if ( group.lookup_user( username ) ) {
349 kDebug() << "group: " << group.getName() << " found user: " << username;
350 group.removeUser( username );
351 groups->mod( i, group );
355 if ( KU_Global::kcfg()->userPrivateGroup() ) {
357 int i = groups->lookup( gid );
358 if ( i != -1 &&
359 KMessageBox::questionYesNo( 0, i18n("You are using private groups.\n"
360 "Do you want to delete the user's private group '%1'?",
361 groups->at(i).getName()), QString(),
362 KStandardGuiItem::del(), KGuiItem(i18n("Do Not Delete"))) == KMessageBox::Yes) {
363 kDebug() << "del private group";
364 groups->del( i );
367 kDebug() << "update groups";
368 updateGroups();
372 void KU_MainView::grpadd()
374 setCurrentIndex(1);
376 gid_t gid;
378 if ( (gid = groups->first_free()) == KU_Groups::NO_FREE )
380 KMessageBox::sorry( 0, i18n("You have run out of gid space.") );
381 return;
384 if ( samba && (rid = KU_Global::getGroups().first_free_sam()) == 0 )
386 KMessageBox::sorry( 0, i18n("You have run out of group RID space.") );
387 return;
390 KU_Group group;
391 group.setGID(gid);
392 if ( groups->getCaps() & KU_Groups::Cap_Samba ) {
393 uid_t rid = SID::gid2rid( gid );
394 SID sid;
395 sid.setRID( rid );
396 sid.setDOM( groups->getDOMSID() );
397 group.setSID( sid );
399 KU_EditGroup egdlg( group, true );
401 if ( egdlg.exec() == QDialog::Rejected ) {
402 return;
404 groups->add(egdlg.getGroup());
405 updateGroups();
408 void KU_MainView::grpedit()
410 QModelIndex currentindex = groupview->selectionModel()->currentIndex();
411 if ( !currentindex.isValid() ) return;
413 int index = groupproxymodel.mapToSource(currentindex).row();
414 kDebug() << "selected index: " << index;
416 KU_Group group = groups->at(index);
418 kDebug() << "The SID for group " << group.getName() << " is: '" << group.getSID().getSID() << "'";
419 if ( ( groups->getCaps() & KU_Groups::Cap_Samba ) &&
420 ( group.getCaps() & KU_Group::Cap_Samba ) &&
421 group.getSID().isEmpty() ) {
422 SID sid;
423 sid.setDOM( groups->getDOMSID() );
424 // sid.setRID( KU_Global::getGroups().first_free_sam() );
425 sid.setRID( SID::gid2rid( group.getGID() ) );
426 group.setSID( sid );
427 kDebug() << "The new SID for group " << group.getName() << " is: " << sid.getSID();
429 KU_EditGroup egdlg( group, false );
431 if ( egdlg.exec() == QDialog::Accepted ) {
432 groups->mod( index, egdlg.getGroup() );
433 updateGroups();
437 void KU_MainView::grpdel()
439 QList<int> selected;
440 QModelIndexList selectedindexes = groupview->selectionModel()->selectedIndexes();
442 foreach( const QModelIndex &selectedindex, selectedindexes ) {
443 if ( selectedindex.column() == 0 )
444 selected.append( groupproxymodel.mapToSource(selectedindex).row() );
446 if ( selected.isEmpty() ) return;
448 KU_Group group;
449 int index;
451 for ( index = 0; index < selected.count(); index++ ) {
452 group = groups->at( selected.at( index ) );
454 KU_Users::const_iterator it = users->constBegin();
455 while ( it != users->constEnd() ) {
456 if ( it->getGID() == group.getGID() ) {
457 KMessageBox::error( 0, i18n( "The group '%1' is the primary group of one or more users (such as '%2'); it cannot be deleted.", group.getName(), it->getName() ) );
458 return;
460 ++it;
464 if ( selected.count() == 1 ) {
465 if (KMessageBox::warningContinueCancel( 0,
466 i18n("Do you really want to delete the group '%1'?", group.getName()),
467 QString(), KStandardGuiItem::del()) != KMessageBox::Continue) return;
468 } else {
469 if (KMessageBox::warningContinueCancel( 0,
470 i18n("Do you really want to delete the %1 selected groups?", selected.count()),
471 QString(), KStandardGuiItem::del()) != KMessageBox::Continue) return;
474 for ( index = 0; index < selected.count(); index++ ) {
475 groups->del( selected.at( index ) );
477 updateGroups();
480 bool KU_MainView::updateUsers()
482 bool ret;
483 kDebug() << "updateUsers() ";
484 ret = users->dbcommit();
486 if ( !ret ) {
487 KU_Global::displayUsersError();
490 usermodel->commitMod();
491 usermodel->commitDel();
492 usermodel->commitAdd();
493 users->cancelMods();
495 return ret;
498 bool KU_MainView::updateGroups()
500 bool ret;
501 kDebug() << "updateGroups() ";
502 ret = groups->dbcommit();
504 if ( !ret ) {
505 KU_Global::displayGroupsError();
508 groupmodel->commitMod();
509 groupmodel->commitDel();
510 groupmodel->commitAdd();
511 groups->cancelMods();
513 return ret;
516 #include "ku_mainview.moc"