Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

usermanager.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                                                         *
00003  *   This program is free software; you can redistribute it and/or modify  *
00004  *   it under the terms of the GNU General Public License as published by  *
00005  *   the Free Software Foundation; either version 2 of the License, or     *
00006  *   (at your option) any later version.                                   *
00007  *                                                                         *
00008  ***************************************************************************/
00009 
00010 #include "usermanager.h"
00011 
00012 /* OPIE */
00013 #include <opie2/odebug.h>
00014 #include <opie2/oresource.h>
00015 using namespace Opie::Core;
00016 #include <qpe/qpeapplication.h>
00017 /* QT */
00018 #include <qlayout.h>
00019 #include <qmessagebox.h>
00020 #include <qfile.h>
00021 #include <qregexp.h>
00022 
00031 UserConfig::UserConfig(QWidget* parent, const char* name, WFlags fl) : QMainWindow(parent, name, fl) {
00032         setCaption(tr("Opie User Manager"));
00033         
00034         // Create an instance of the global object 'accounts'. This holds all user/group info, and functions to modify them.
00035         accounts=new Passwd();
00036         accounts->open();       // This actually loads the files /etc/passwd & /etc/group into memory.
00037 
00038         // Create the toolbar.
00039     setToolBarsMovable( false );
00040     bool useBigIcon = qApp->desktop()->size().width() > 330;
00041         QToolBar *toolbar = new QToolBar(this,"Toolbar");
00042     toolbar->setHorizontalStretchable( true );
00043         adduserToolButton = new QToolButton(Opie::Core::OResource::loadPixmap("usermanager/adduser", Opie::Core::OResource::SmallIcon),
00044                                         "Add User",0,this,SLOT(addUser()),toolbar,"Add User");
00045     adduserToolButton->setUsesBigPixmap( useBigIcon );
00046     edituserToolButton = new QToolButton(Opie::Core::OResource::loadPixmap("usermanager/edituser", Opie::Core::OResource::SmallIcon),
00047                                          "Edit User",0,this,SLOT(editUser()),toolbar,"Edit User");
00048     edituserToolButton->setUsesBigPixmap( useBigIcon );
00049         deleteuserToolButton = new QToolButton(Opie::Core::OResource::loadPixmap("usermanager/deleteuser", Opie::Core::OResource::SmallIcon),
00050                                            "Delete User",0,this,SLOT(delUser()),toolbar,"Delete User");
00051     deleteuserToolButton->setUsesBigPixmap( useBigIcon );
00052         //QToolButton *userstext = new QToolButton(0,"User",0,0,0,toolbar,"User");
00053         //userstext->setUsesTextLabel(true);
00054         toolbar->addSeparator();
00055         addgroupToolButton = new QToolButton(Opie::Core::OResource::loadPixmap("usermanager/addgroup", Opie::Core::OResource::SmallIcon),
00056                                          "Add Group",0,this,SLOT(addGroup()),toolbar,"Add Group");
00057         addgroupToolButton->setUsesBigPixmap( useBigIcon );
00058     editgroupToolButton = new QToolButton(Opie::Core::OResource::loadPixmap("usermanager/editgroup", Opie::Core::OResource::SmallIcon),
00059                                           "Edit Group",0,this,SLOT(editGroup()),toolbar,"Edit Group");
00060     editgroupToolButton->setUsesBigPixmap( useBigIcon );
00061         deletegroupToolButton = new QToolButton(Opie::Core::OResource::loadPixmap("usermanager/deletegroup", Opie::Core::OResource::SmallIcon),
00062                                             "Delete Group",0,this,SLOT(delGroup()),toolbar,"Delete Group");
00063     deletegroupToolButton->setUsesBigPixmap( useBigIcon );
00064         //QToolButton *groupstext = new QToolButton(0,"Group",0,0,0,toolbar,"Group");
00065         //groupstext->setUsesTextLabel(true);
00066         addToolBar(toolbar,"myToolBar");
00067 
00068         // Add a tabwidget and all the tabs.
00069         myTabWidget = new QTabWidget(this,"My Tab Widget");
00070         setupTabAccounts();
00071         setupTabAllUsers();
00072         setupTabAllGroups();
00073         userPopupMenu.insertItem("Copy",0);
00074         
00075         getUsers(); // Fill out the iconview & listview with all users.
00076         getGroups(); // Fill out the group listview with all groups.
00077         
00078         setCentralWidget(myTabWidget);
00079 }
00080 
00081 UserConfig::~UserConfig() {
00082         accounts->close();
00083         delete accounts;
00084 }
00085 
00086 void UserConfig::setupTabAccounts() {
00087         QWidget *tabpage = new QWidget(this);
00088         QVBoxLayout *layout = new QVBoxLayout(tabpage);
00089         layout->setMargin(5);
00090         
00091         usersIconView=new QListView(tabpage,"users");
00092         usersIconView->addColumn("Icon");
00093         usersIconView->addColumn("Username");
00094         usersIconView->setAllColumnsShowFocus(true);
00095         layout->addWidget(usersIconView);
00096         
00097         connect(usersIconView,SIGNAL(returnPressed(QListViewItem*)),this,SLOT(showUserMenu(QListViewItem*)));
00098         
00099         myTabWidget->addTab(tabpage,"Users");
00100 }
00101 
00102 void UserConfig::setupTabAllUsers() {
00103         QWidget *tabpage = new QWidget(this);
00104         QVBoxLayout *layout = new QVBoxLayout(tabpage);
00105         layout->setMargin(5);
00106         
00107         usersListView=new QListView(tabpage,"allusers");
00108         usersListView->addColumn("UID");
00109         usersListView->addColumn("Login");
00110         usersListView->addColumn("Username");
00111         layout->addWidget(usersListView);
00112         usersListView->setSorting(1,1);
00113         usersListView->setAllColumnsShowFocus(true);
00114 
00115         myTabWidget->addTab(tabpage,"All Users");
00116 }
00117 
00118 void UserConfig::setupTabAllGroups() {
00119         QWidget *tabpage = new QWidget(this);
00120         QVBoxLayout *layout = new QVBoxLayout(tabpage);
00121         layout->setMargin(5);
00122 
00123         groupsListView=new QListView(tabpage,"groups");
00124         groupsListView->addColumn("GID");
00125         groupsListView->addColumn("Groupname");
00126         layout->addWidget(groupsListView);
00127         groupsListView->setSorting(1,1);
00128         groupsListView->setAllColumnsShowFocus(true);
00129         
00130         myTabWidget->addTab(tabpage,"All Groups");
00131 }
00132 void UserConfig::getUsers() {
00133         QString mytext;
00134         QPixmap mypixmap;
00135         QListViewItem *listviewitem;
00136                         
00137         // Empty the iconview & the listview.
00138         usersIconView->clear();
00139         usersListView->clear();
00140         
00141         // availableUID is used as a deposite for the next available UID on the system, this should start at an ID over 500.
00142         availableUID=500;
00143         for(QStringList::Iterator it=accounts->passwdStringList.begin(); it!=accounts->passwdStringList.end(); ++it) {
00144                 accounts->splitPasswdEntry(*it); // Split the string into it's components and store in variables in the accounts object. ("pr_name" and so on.)
00145                 if(accounts->pw_name.find(QRegExp("^#"),0)) {   // Skip commented lines.
00146                         new QListViewItem(usersListView,QString::number(accounts->pw_uid),accounts->pw_name,accounts->pw_gecos);
00147                         if((accounts->pw_uid>=500) && (accounts->pw_uid<65000)) {       // Is this user a "normal" user ?
00148                                 mytext=QString(accounts->pw_name)+" - ("+QString(accounts->pw_gecos)+")"; // The string displayed next to the icon.
00149                                 if(!(mypixmap.load("/opt/QtPalmtop/pics/users/"+accounts->pw_name+".png"))) { //  Is there an icon for this user? Opie::Core::OResource::loadPixmap is caching, doesn't work.
00150                                         mypixmap=Opie::Core::OResource::loadPixmap("usermanager/usericon", Opie::Core::OResource::SmallIcon);   // If this user has no icon, load the default icon.
00151                                 }
00152                                 listviewitem=new QListViewItem(usersIconView,"",mytext);        // Add the icon+text to the qiconview.
00153                                 listviewitem->setPixmap(0,mypixmap);
00154                         }
00155                         if((accounts->pw_uid>=availableUID) && (accounts->pw_uid<65000)) availableUID=accounts->pw_uid+1; // Increase 1 to the latest know UID to get a free uid.
00156                 }
00157         }
00158         usersIconView->sort();
00159 }
00160 
00161 void UserConfig::addUser() {
00162         if(UserDialog::addUser(availableUID,availableGID)) {    // Add the user to the system, also send next available UID and GID.
00163                 getUsers(); // Update users views.
00164                 getGroups(); // Update groups view.
00165         }
00166 }
00167 
00168 void UserConfig::editUser() {
00169         QString username;
00170         if(myTabWidget->currentPageIndex()==0) {        // Users
00171                 if(usersIconView->currentItem()) {      // Any icon selected?
00172                         username=usersIconView->currentItem()->text(1); // Get the text associated with the icon.
00173                         username=username.left(username.find(" - (",0,true));   // Strip out the username.
00174                         if(UserDialog::editUser(username)) {    // Bring up the userinfo dialog.
00175                                 // If there were any changed also update the views.
00176                                 getUsers();
00177                                 getGroups();
00178                         }
00179                 } else {
00180                         QMessageBox::information(this,"No selection.","No user has been selected.");
00181                 }
00182         }
00183         if(myTabWidget->currentPageIndex()==1) {        // All users
00184                 if(usersListView->currentItem()) {      // Anything changed!?
00185                         username=usersListView->currentItem()->text(1); // Get the username.
00186                         if(UserDialog::editUser(username)) {    // Bring up the userinfo dialog.
00187                                 // And again update the views if there were any changes.
00188                                 getUsers();
00189                                 getGroups();
00190                         }
00191                 } else  {
00192                         QMessageBox::information(this,"No selection.","No user has been selected.");
00193                 }
00194         }
00195 }
00196 
00197 void UserConfig::delUser() {
00198         QString username;
00199         
00200         if(myTabWidget->currentPageIndex()==0) {        // Users, Iconview.
00201                 if(usersIconView->currentItem()) {      // Anything selected?
00202                         username=usersIconView->currentItem()->text(1); // Get string associated with icon.
00203                         username=username.left(username.find(" - (",0,true));   // Strip out the username.
00204                         if(QMessageBox::warning(this,"Delete user","Are you sure you want to\ndelete this user? \""+QString(username)+"\" ?","&No","&Yes",0,0,1)) {
00205                                 if(UserDialog::delUser(username)) {     // Delete the user if possible.
00206                                         // Update views.
00207                                         getUsers();
00208                                         getGroups();
00209                                 }
00210                         }
00211                 } else  {
00212                         QMessageBox::information(this,"No selection","No user has been selected.");
00213                 }
00214         }
00215         if(myTabWidget->currentPageIndex()==1) {        // All users
00216                 if(usersListView->currentItem()) {      // Anything changed!?
00217                         username=usersListView->currentItem()->text(1); // Get the username.
00218                         if(QMessageBox::warning(this,"Delete user","Are you sure you want to\ndelete this user? \""+QString(username)+"\" ?","&No","&Yes",0,0,1)) {
00219                                 if(UserDialog::delUser(username)) {     // Try to delete the user.
00220                                         // Update views.
00221                                         getUsers();
00222                                         getGroups();
00223                                 }
00224                         }
00225                 } else  {
00226                         QMessageBox::information(this,"No selection","No user has been selected.");
00227                 }
00228         }
00229         
00230 }
00231 
00232 void UserConfig::getGroups() {
00233         groupsListView->clear();        // Empty the listview.
00234         availableGID=500;       // We need to find the next free GID, and are only interested in values between 500 & 65000.
00235         for(QStringList::Iterator it=accounts->groupStringList.begin(); it!=accounts->groupStringList.end(); ++it) {    // Split the list into lines.
00236                 accounts->splitGroupEntry(*it); // Split the line into its components and fill the variables of 'accounts'. (gr_name, gr_uid & gr_mem).
00237                 if(accounts->gr_name.find(QRegExp("^#"),0)) {   // Skip commented lines.
00238                         new QListViewItem(groupsListView,QString::number(accounts->gr_gid),accounts->gr_name);
00239                         if((accounts->gr_gid>=availableGID) && (accounts->gr_gid<65000)) availableGID=accounts->gr_gid+1;       // Maybe a new free GID.
00240                 }
00241         }
00242 }
00243 
00244 void UserConfig::addGroup() {
00245         if(GroupDialog::addGroup(availableGID)) getGroups();    // Bring up the add group dialog.
00246 }
00247 
00248 void UserConfig::editGroup() {
00249         int gid;
00250         if(groupsListView->currentItem()) {     // Any group selected?
00251                 gid=groupsListView->currentItem()->text(0).toInt();     // Get the GID from the listview.
00252                 if(GroupDialog::editGroup(gid)) getGroups();    // Bring up the edit group dialog.
00253         } else  {
00254                 QMessageBox::information(this,"No selection","No group has been selected.");
00255         }
00256 }
00257 
00258 void UserConfig::delGroup() {
00259         const char *groupname;
00260         if(groupsListView->currentItem()) {     // Any group selected?
00261                 groupname=groupsListView->currentItem()->text(1);       // Get the groupname from the listview.
00262                 if(QMessageBox::warning(this,"Delete group","Are you sure you want to\ndelete the group \""+QString(groupname)+"\" ?","&No","&Yes",0,0,1)) {
00263                         // If confirmed, try to delete the group.
00264                         if(GroupDialog::delGroup(groupname)) getGroups(); // And also update the view afterwards if the user was deleted.
00265                 }
00266         } else  {
00267                 QMessageBox::information(this,"No selection","No group has been selected.");
00268         }
00269 }
00270 
00271 void UserConfig::showUserMenu(QListViewItem *item) {
00272 //      userPopupMenu.exec(item->mapToGlobal(QPoint(0,0)));
00273         owarn << "Pressed!" << oendl; 
00274 }

Generated on Sat Nov 5 16:17:58 2005 for OPIE by  doxygen 1.4.2