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.
26 #include <kinputdialog.h>
28 #include <kiconloader.h>
29 #include <kmessagebox.h>
34 #include "ku_edituser.h"
35 #include "ku_adduser.h"
36 #include "ku_deluser.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
)
48 void KU_MainView::init() {
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);
90 emit
userSelected(false);
91 emit
groupSelected(true);
95 void KU_MainView::clearUsers()
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() );
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() );
128 groupview
->sortByColumn( 0, Qt::AscendingOrder
);
131 bool KU_MainView::queryClose()
136 void KU_MainView::setpwd()
138 QModelIndexList selectedindexes
= userview
->selectionModel()->selectedIndexes();
139 int count
= selectedindexes
.count() / usermodel
->columnCount();
141 if ( count
== 0 ) return;
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;
148 if ( d
.exec() != QDialog::Accepted
) return;
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
);
166 void KU_MainView::userSelected()
171 void KU_MainView::groupSelected()
176 void KU_MainView::useradd()
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.") );
190 if ( samba && (rid = users->first_free_sam()) == 0) {
191 KMessageBox::sorry( 0, i18n("You have run out of user RID space.") );
195 if ( samba
) rid
= SID::uid2rid( uid
);
197 QString name
= KInputDialog::getText( QString::null
, //krazy:exclude=nullstrassign for old broken gcc
198 i18n("Please type the name of the new user:"),
203 if ( users
->lookup( name
) != -1 ) {
204 KMessageBox::sorry( 0, i18n("User with name %1 already exists.", name
) );
208 user
.setCaps( samba
? KU_User::Cap_POSIX
| KU_User::Cap_Samba
: KU_User::Cap_POSIX
);
210 user
.setName( name
);
214 sid
.setDOM( users
->getDOMSID() );
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
) {
249 user
= au
.getNewUser();
250 kDebug() << " surname: " << user
.getSurname();
255 index
= groups
->lookup(user
.getName());
256 //if no group exists with the user's name, create one
259 if ( groups
->lookup( user
.getUID() ) == -1 ) {
262 gid
= groups
->first_free();
264 kDebug() << "private group GID: " << gid
;
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();
273 if ( samba
&& ( user
.getCaps() & KU_User::Cap_Samba
) ) {
275 sid
.setDOM( groups
->getDOMSID() );
278 group
.setDisplayName( user
.getName() );
279 group
.setCaps( KU_Group::Cap_Samba
);
281 group
.setName( user
.getName() );
282 groups
->add( group
);
284 group
= groups
->at(index
);
286 user
.setGID( group
.getGID() );
287 user
.setPGSID( group
.getSID() );
289 users
->doCreate(&user
);
291 if ( !updateUsers() ) {
292 groups
->cancelMods();
298 void KU_MainView::useredit()
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;
313 foreach(int i
, selected
) {
314 editUser
.mergeUser( users
->at(i
), user
);
315 users
->mod( i
, user
);
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
)
337 user
.setDeleteHome( dlg
.getDeleteHomeDir() );
338 user
.setDeleteMailBox( dlg
.getDeleteMailBox() );
341 users
->doDelete(&user
);
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
);
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";
367 kDebug() << "update groups";
372 void KU_MainView::grpadd()
378 if ( (gid
= groups
->first_free()) == KU_Groups::NO_FREE
)
380 KMessageBox::sorry( 0, i18n("You have run out of gid space.") );
384 if ( samba && (rid = KU_Global::getGroups().first_free_sam()) == 0 )
386 KMessageBox::sorry( 0, i18n("You have run out of group RID space.") );
392 if ( groups
->getCaps() & KU_Groups::Cap_Samba
) {
393 uid_t rid
= SID::gid2rid( gid
);
396 sid
.setDOM( groups
->getDOMSID() );
399 KU_EditGroup
egdlg( group
, true );
401 if ( egdlg
.exec() == QDialog::Rejected
) {
404 groups
->add(egdlg
.getGroup());
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() ) {
423 sid
.setDOM( groups
->getDOMSID() );
424 // sid.setRID( KU_Global::getGroups().first_free_sam() );
425 sid
.setRID( SID::gid2rid( group
.getGID() ) );
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() );
437 void KU_MainView::grpdel()
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;
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() ) );
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;
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
) );
480 bool KU_MainView::updateUsers()
483 kDebug() << "updateUsers() ";
484 ret
= users
->dbcommit();
487 KU_Global::displayUsersError();
490 usermodel
->commitMod();
491 usermodel
->commitDel();
492 usermodel
->commitAdd();
498 bool KU_MainView::updateGroups()
501 kDebug() << "updateGroups() ";
502 ret
= groups
->dbcommit();
505 KU_Global::displayGroupsError();
508 groupmodel
->commitMod();
509 groupmodel
->commitDel();
510 groupmodel
->commitAdd();
511 groups
->cancelMods();
516 #include "ku_mainview.moc"