Diffstat (limited to 'noncore/settings/usermanager/usermanager.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/settings/usermanager/usermanager.cpp | 242 |
1 files changed, 242 insertions, 0 deletions
diff --git a/noncore/settings/usermanager/usermanager.cpp b/noncore/settings/usermanager/usermanager.cpp new file mode 100644 index 0000000..883e5d7 --- a/dev/null +++ b/noncore/settings/usermanager/usermanager.cpp | |||
@@ -0,0 +1,242 @@ | |||
1 | /*************************************************************************** | ||
2 | * * | ||
3 | * This program is free software; you can redistribute it and/or modify * | ||
4 | * it under the terms of the GNU General Public License as published by * | ||
5 | * the Free Software Foundation; either version 2 of the License, or * | ||
6 | * (at your option) any later version. * | ||
7 | * * | ||
8 | ***************************************************************************/ | ||
9 | |||
10 | #include "usermanager.h" | ||
11 | |||
12 | #include <qlayout.h> | ||
13 | #include <stdio.h> | ||
14 | |||
15 | #include <qmessagebox.h> | ||
16 | #include <qfile.h> | ||
17 | #include <qpe/resource.h> | ||
18 | |||
19 | #include <qregexp.h> | ||
20 | |||
21 | /** | ||
22 | * The mainwindow constructor. | ||
23 | * | ||
24 | * @param QWidget *parent | ||
25 | * @param const char *name | ||
26 | * @ param WFlags fl | ||
27 | * | ||
28 | */ | ||
29 | UserConfig::UserConfig(QWidget* parent, const char* name, WFlags fl) : QMainWindow(parent, name, fl) { | ||
30 | setCaption(tr("OPIE User Manager")); | ||
31 | |||
32 | // Create an instance of the global object 'accounts'. This holds all user/group info, and functions to modify them. | ||
33 | accounts=new Passwd(); | ||
34 | accounts->open();// This actually loads the files /etc/passwd & /etc/group into memory. | ||
35 | |||
36 | // Create the toolbar. | ||
37 | QPEToolBar *toolbar = new QPEToolBar(this,"Toolbar"); | ||
38 | toolbar->setHorizontalStretchable(1); // Is there any other way to get the toolbar to stretch of the full screen!? | ||
39 | adduserToolButton = new QToolButton(Resource::loadPixmap("userconfig/adduser"),"Add User",0,this,SLOT(addUser()),toolbar,"Add User"); | ||
40 | edituserToolButton = new QToolButton(Resource::loadPixmap("userconfig/edituser"),"Edit User",0,this,SLOT(editUser()),toolbar,"Edit User"); | ||
41 | deleteuserToolButton = new QToolButton(Resource::loadPixmap("userconfig/deleteuser"),"Delete User",0,this,SLOT(delUser()),toolbar,"Delete User"); | ||
42 | QToolButton *userstext = new QToolButton(0,"User",0,0,0,toolbar,"User"); | ||
43 | userstext->setUsesTextLabel(true); | ||
44 | toolbar->addSeparator(); | ||
45 | addgroupToolButton = new QToolButton(Resource::loadPixmap("userconfig/addgroup"),"Add Group",0,this,SLOT(addGroup()),toolbar,"Add Group"); | ||
46 | editgroupToolButton = new QToolButton(Resource::loadPixmap("userconfig/editgroup"),"Edit Group",0,this,SLOT(editGroup()),toolbar,"Edit Group"); | ||
47 | deletegroupToolButton = new QToolButton(Resource::loadPixmap("userconfig/deletegroup"),"Delete Group",0,this,SLOT(delGroup()),toolbar,"Delete Group"); | ||
48 | QToolButton *groupstext = new QToolButton(0,"Group",0,0,0,toolbar,"Group"); | ||
49 | groupstext->setUsesTextLabel(true); | ||
50 | addToolBar(toolbar,"myToolBar"); | ||
51 | |||
52 | // Add a tabwidget and all the tabs. | ||
53 | myTabWidget = new QTabWidget(this,"My Tab Widget"); | ||
54 | setupTabAccounts(); | ||
55 | setupTabAllUsers(); | ||
56 | setupTabAllGroups(); | ||
57 | |||
58 | getUsers(); // Fill out the iconview & listview with all users. | ||
59 | getGroups(); // Fill out the group listview with all groups. | ||
60 | |||
61 | setCentralWidget(myTabWidget); | ||
62 | } | ||
63 | |||
64 | UserConfig::~UserConfig() { | ||
65 | accounts->close(); | ||
66 | delete accounts; | ||
67 | } | ||
68 | |||
69 | void UserConfig::setupTabAccounts() { | ||
70 | QWidget *tabpage = new QWidget(this); | ||
71 | QVBoxLayout *layout = new QVBoxLayout(tabpage); | ||
72 | layout->setMargin(5); | ||
73 | |||
74 | usersIconView=new QIconView(tabpage,"users"); | ||
75 | usersIconView->setItemTextPos(QIconView::Right); | ||
76 | layout->addWidget(usersIconView); | ||
77 | |||
78 | myTabWidget->addTab(tabpage,"Users"); | ||
79 | } | ||
80 | |||
81 | void UserConfig::setupTabAllUsers() { | ||
82 | QWidget *tabpage = new QWidget(this); | ||
83 | QVBoxLayout *layout = new QVBoxLayout(tabpage); | ||
84 | layout->setMargin(5); | ||
85 | |||
86 | usersListView=new QListView(tabpage,"allusers"); | ||
87 | usersListView->addColumn("UID"); | ||
88 | usersListView->addColumn("Login"); | ||
89 | usersListView->addColumn("Username"); | ||
90 | layout->addWidget(usersListView); | ||
91 | usersListView->setSorting(1,1); | ||
92 | usersListView->setAllColumnsShowFocus(true); | ||
93 | |||
94 | myTabWidget->addTab(tabpage,"All Users"); | ||
95 | } | ||
96 | |||
97 | void UserConfig::setupTabAllGroups() { | ||
98 | QWidget *tabpage = new QWidget(this); | ||
99 | QVBoxLayout *layout = new QVBoxLayout(tabpage); | ||
100 | layout->setMargin(5); | ||
101 | |||
102 | groupsListView=new QListView(tabpage,"groups"); | ||
103 | groupsListView->addColumn("GID"); | ||
104 | groupsListView->addColumn("Groupname"); | ||
105 | layout->addWidget(groupsListView); | ||
106 | groupsListView->setSorting(1,1); | ||
107 | groupsListView->setAllColumnsShowFocus(true); | ||
108 | |||
109 | myTabWidget->addTab(tabpage,"All Groups"); | ||
110 | } | ||
111 | void UserConfig::getUsers() { | ||
112 | QString mytext; | ||
113 | QPixmap mypixmap; | ||
114 | |||
115 | // Empty the iconview & the listview. | ||
116 | usersIconView->clear(); | ||
117 | usersListView->clear(); | ||
118 | |||
119 | // availableUID is used as a deposite for the next available UID on the system, this should start at an ID over 500. | ||
120 | availableUID=500; | ||
121 | for(QStringList::Iterator it=accounts->passwdStringList.begin(); it!=accounts->passwdStringList.end(); ++it) { | ||
122 | accounts->splitPasswdEntry(*it); // Split the string into it's components and store in variables in the accounts object. ("pr_name" and so on.) | ||
123 | new QListViewItem(usersListView,QString::number(accounts->pw_uid),accounts->pw_name,accounts->pw_gecos); | ||
124 | if((accounts->pw_uid>=500) && (accounts->pw_uid<65000)) {// Is this user a "normal" user ? | ||
125 | mytext=QString(accounts->pw_name)+" - ("+QString(accounts->pw_gecos)+")"; // The string displayed next to the icon. | ||
126 | mypixmap=Resource::loadPixmap(QString("users/"+accounts->pw_name));// Is there an icon for this user? | ||
127 | if(mypixmap.isNull()) { | ||
128 | mypixmap=Resource::loadPixmap(QString("userconfig/usericon"));// If this user has no icon, load the default icon. | ||
129 | } | ||
130 | new QIconViewItem(usersIconView,mytext,mypixmap);// Add the icon+text to the qiconview. | ||
131 | } | ||
132 | 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. | ||
133 | } | ||
134 | } | ||
135 | |||
136 | void UserConfig::addUser() { | ||
137 | if(UserDialog::addUser(availableUID,availableGID)) {// Add the user to the system, also send next available UID and GID. | ||
138 | getUsers(); // Update users views. | ||
139 | getGroups(); // Update groups view. | ||
140 | } | ||
141 | } | ||
142 | |||
143 | void UserConfig::editUser() { | ||
144 | QString username; | ||
145 | if(myTabWidget->currentPageIndex()==0) {// Users | ||
146 | if(usersIconView->currentItem()) {// Any icon selected? | ||
147 | username=usersIconView->currentItem()->text();// Get the text associated with the icon. | ||
148 | username=username.left(username.find(" - (",0,true));// Strip out the username. | ||
149 | if(UserDialog::editUser(username)) {// Bring up the userinfo dialog. | ||
150 | // If there were any changed also update the views. | ||
151 | getUsers(); | ||
152 | getGroups(); | ||
153 | } | ||
154 | } else { | ||
155 | QMessageBox::information(this,"No selection.","No user has been selected."); | ||
156 | } | ||
157 | } | ||
158 | if(myTabWidget->currentPageIndex()==1) {// All users | ||
159 | if(usersListView->currentItem()) {// Anything changed!? | ||
160 | username=usersListView->currentItem()->text(1);// Get the username. | ||
161 | if(UserDialog::editUser(username)) {// Bring up the userinfo dialog. | ||
162 | // And again update the views if there were any changes. | ||
163 | getUsers(); | ||
164 | getGroups(); | ||
165 | } | ||
166 | } else { | ||
167 | QMessageBox::information(this,"No selection.","No user has been selected."); | ||
168 | } | ||
169 | } | ||
170 | } | ||
171 | |||
172 | void UserConfig::delUser() { | ||
173 | QString username; | ||
174 | |||
175 | if(myTabWidget->currentPageIndex()==0) {// Users, Iconview. | ||
176 | if(usersIconView->currentItem()) {// Anything selected? | ||
177 | username=usersIconView->currentItem()->text();// Get string associated with icon. | ||
178 | username=username.left(username.find(" - (",0,true));// Strip out the username. | ||
179 | if(QMessageBox::warning(this,"Delete user","Are you sure you want to\ndelete this user? \""+QString(username)+"\" ?","&No","&Yes",0,0,1)) { | ||
180 | if(UserDialog::delUser(username)) {// Delete the user if possible. | ||
181 | // Update views. | ||
182 | getUsers(); | ||
183 | getGroups(); | ||
184 | } | ||
185 | } | ||
186 | } else { | ||
187 | QMessageBox::information(this,"No selection","No user has been selected."); | ||
188 | } | ||
189 | } | ||
190 | if(myTabWidget->currentPageIndex()==1) {// All users | ||
191 | if(usersListView->currentItem()) {// Anything changed!? | ||
192 | username=usersListView->currentItem()->text(1);// Get the username. | ||
193 | if(QMessageBox::warning(this,"Delete user","Are you sure you want to\ndelete this user? \""+QString(username)+"\" ?","&No","&Yes",0,0,1)) { | ||
194 | if(UserDialog::delUser(username)) {// Try to delete the user. | ||
195 | // Update views. | ||
196 | getUsers(); | ||
197 | getGroups(); | ||
198 | } | ||
199 | } | ||
200 | } else { | ||
201 | QMessageBox::information(this,"No selection","No user has been selected."); | ||
202 | } | ||
203 | } | ||
204 | |||
205 | } | ||
206 | |||
207 | void UserConfig::getGroups() { | ||
208 | groupsListView->clear();// Empty the listview. | ||
209 | availableGID=500;// We need to find the next free GID, and are only interested in values between 500 & 65000. | ||
210 | for(QStringList::Iterator it=accounts->groupStringList.begin(); it!=accounts->groupStringList.end(); ++it) {// Split the list into lines. | ||
211 | accounts->splitGroupEntry(*it);// Split the line into its components and fill the variables of 'accounts'. (gr_name, gr_uid & gr_mem). | ||
212 | new QListViewItem(groupsListView,QString::number(accounts->gr_gid),accounts->gr_name); | ||
213 | if((accounts->gr_gid>=availableGID) && (accounts->gr_gid<65000)) availableGID=accounts->gr_gid+1;// Maybe a new free GID. | ||
214 | } | ||
215 | } | ||
216 | |||
217 | void UserConfig::addGroup() { | ||
218 | if(GroupDialog::addGroup(availableGID)) getGroups();// Bring up the add group dialog. | ||
219 | } | ||
220 | |||
221 | void UserConfig::editGroup() { | ||
222 | int gid; | ||
223 | if(groupsListView->currentItem()) {// Any group selected? | ||
224 | gid=groupsListView->currentItem()->text(0).toInt();// Get the GID from the listview. | ||
225 | if(GroupDialog::editGroup(gid)) getGroups();// Bring up the edit group dialog. | ||
226 | } else { | ||
227 | QMessageBox::information(this,"No selection","No group has been selected."); | ||
228 | } | ||
229 | } | ||
230 | |||
231 | void UserConfig::delGroup() { | ||
232 | const char *groupname; | ||
233 | if(groupsListView->currentItem()) {// Any group selected? | ||
234 | groupname=groupsListView->currentItem()->text(1);// Get the groupname from the listview. | ||
235 | if(QMessageBox::warning(this,"Delete group","Are you sure you want to\ndelete the group \""+QString(groupname)+"\" ?","&No","&Yes",0,0,1)) { | ||
236 | // If confirmed, try to delete the group. | ||
237 | if(GroupDialog::delGroup(groupname)) getGroups(); // And also update the view afterwards if the user was deleted. | ||
238 | } | ||
239 | } else { | ||
240 | QMessageBox::information(this,"No selection","No group has been selected."); | ||
241 | } | ||
242 | } | ||