summaryrefslogtreecommitdiffstats
path: root/kiostdetool/userManagement.cpp
blob: 000e00e41107f30ca05c7a5dd8930d3bc207283c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
 *   userManagement.cpp
 *
 *   Copyright (C) 2004 Waldo Bastian <bastian@kde.org>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License version 2 as
 *   published by the Free Software Foundation.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#include "userManagement.h"

#include <unistd.h>
#include <grp.h>
#include <pwd.h>
#include <sys/types.h>

#include <tqcombobox.h>
#include <tqlabel.h>
#include <tqpushbutton.h>

#include <kapplication.h>
#include <tdeconfig.h>
#include <klistview.h>
#include <klocale.h>
#include <kmessagebox.h>

#include "kioskrun.h"

#include "userManagement_ui.h"
#include "userManagementGroup_ui.h"
#include "userManagementUser_ui.h"

#define AVAILABLE_SINCE "KDE 3.2.3"

UserManagementPage::UserManagementPage(TQWidget* parent, const char* name, WFlags fl)
 : UserManagementUI(parent, name, fl), PageWidget(this)
{
  setCaption(i18n("Assign Profiles"));
  listGroups->setSorting(-1); // Disable sorting
  listGroups->setDragEnabled(true);
  listGroups->setAcceptDrops(true);
  
//  actionButton(KDialogBase::Ok)->setFocus();
  
  connect(buttonAddGroup, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotAddGroup()));
  connect(buttonDeleteGroup, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotDeleteGroup()));
  connect(buttonAddUser, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotAddUser()));
  connect(buttonDeleteUser, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotDeleteUser()));

  connect(listGroups, TQT_SIGNAL(selectionChanged()), this, TQT_SLOT(slotUpdateButtons()));
  connect(listUsers, TQT_SIGNAL(selectionChanged()), this, TQT_SLOT(slotUpdateButtons()));
  
//  init();
  static bool firstTime = true;
  
  if (firstTime)
  {
     firstTime = false;
     TQTimer::singleShot(0, this, TQT_SLOT(slotShowNotice()));
  }
}

UserManagementPage::~UserManagementPage()
{
}

void UserManagementPage::slotShowNotice()
{
  KMessageBox::information(this, 
         i18n("<qt>The profiles that you define here are automatically applied when the "
              "user logs in to %1 or newer.<p>"
              "If you want to use these profiles in combination with older versions you need "
              "to manually set the $TDEDIRS environment variable from the <b>starttde</b> "
              "script by adding the following line:<br><br>"
              "<i>export TDEDIRS=$(kiosktool-tdedirs)</i><br><br>").arg(AVAILABLE_SINCE),
         i18n("Attention"), "user-profiles");
}

void UserManagementPage::load()
{
   listGroups->clear();
   listUsers->clear();

   m_allProfiles = KioskRun::self()->allProfiles();
   m_allProfiles.sort();

   KioskRun::ProfileMapping groups;
   KioskRun::ProfileMapping users;
   TQStringList groupOrder;
   
   KioskRun::self()->getUserProfileMappings(groups, users, groupOrder);
   
   for ( TQStringList::ConstIterator it = groupOrder.begin();
         it != groupOrder.end(); ++it )
   {
      TQString group = *it;
      TQString profile = groups[group].join(",");
      new TQListViewItem(listGroups, group, profile);
   }

   for ( KioskRun::ProfileMapping::Iterator it = users.begin();
         it != users.end(); ++it )
   {
      TQString user = it.key();
      TQString profile = it.data().join(",");
      new TQListViewItem(listUsers, user, profile);
   }
   slotUpdateButtons();
}

void UserManagementPage::slotUpdateButtons()
{
   buttonDeleteGroup->setEnabled(listGroups->selectedItem() != 0);
   buttonDeleteUser->setEnabled(listUsers->selectedItem() != 0);
}

bool UserManagementPage::save()
{
   KioskRun::ProfileMapping groups;
   KioskRun::ProfileMapping users;
   TQStringList groupOrder;
   
   TQListViewItem *item = listGroups->firstChild();
   for(; item; item = item->nextSibling())
   {
      TQString group = item->text(0);
      TQStringList profiles = TQStringList::split(",", item->text(1));
      groups.insert(group, profiles);
      groupOrder.prepend(group);
   }

   item = listUsers->firstChild();
   for(; item; item = item->nextSibling())
   {
      TQString user = item->text(0);
      TQStringList profiles = TQStringList::split(",", item->text(1));
      users.insert(user, profiles);
   }
   
   return KioskRun::self()->setUserProfileMappings(groups, users, groupOrder);
}

void UserManagementPage::slotAddGroup()
{
   TDEConfig *config = kapp->config();
   config->setGroup("General");
   TQStringList groupBlacklist = config->readListEntry("GroupBlacklist");

   m_allGroups.clear();
   setgrent();
   for (struct group *grp; (grp = getgrent()); )
   {
       TQString group = TQString::fromUtf8(grp->gr_name);
       if (!groupBlacklist.contains(group))
           m_allGroups.append(group);
   }
   endgrent();
   m_allGroups.sort();

   KDialogBase dlg(this, "addGroup", true, i18n("Add Group Policy"),
      KDialogBase::Ok|KDialogBase::Cancel, KDialogBase::Ok, true );
      
   UserManagementGroupUI *wid = new UserManagementGroupUI(&dlg);
   wid->labelCaption->setFixedSize(wid->labelCaption->sizeHint());
   wid->comboGroup->insertStringList(m_allGroups);
   wid->comboProfile->insertStringList(m_allProfiles);
   wid->setFixedSize(wid->sizeHint());
   dlg.setMainWidget(wid);
   dlg.setFixedSize(dlg.sizeHint());
   while (dlg.exec() == KDialogBase::Accepted)
   {
      TQString group = wid->comboGroup->currentText();
      TQString profile = wid->comboProfile->currentText();
      
      // Check for dupes
      TQListViewItem *item = listGroups->firstChild();
      for( ;item; item = item->nextSibling())
      {
         if (item->text(0) == group)
            break;
      }
      if (item)
      {
         int result = KMessageBox::warningContinueCancel(this,
                   i18n("<qt>You already have a profile defined for group <b>%1</b>. "
                        "Do you want to replace it?</qt>").arg(group), 
                   i18n("Duplicate Warning"),
                   i18n("&Replace"));
         if (result != KMessageBox::Continue)
            continue; // Go back to edit dialog
         delete item;
      }
      
      item = new TQListViewItem(listGroups, group, profile);
      listGroups->setSelected(item, true);
      slotUpdateButtons();
      return;
   }
}

void UserManagementPage::slotDeleteGroup()
{
   TQListViewItem *item = listGroups->selectedItem();
   if (!item)
      return;
      
   delete item;

   item = listGroups->currentItem();
   if (item)
      listGroups->setSelected(item, true);
   slotUpdateButtons();
}

void UserManagementPage::slotAddUser()
{
   TDEConfig *config = kapp->config();
   config->setGroup("General");
   int minUID = config->readNumEntry("FirstUIDShown", 500);

   m_allUsers.clear();
   setpwent();
   for (struct passwd *user; (user = getpwent()); )
   {
       if ((user->pw_uid >= (uid_t) minUID) || (user->pw_uid == 0))
           m_allUsers.append(TQString::fromUtf8(user->pw_name));
   }
   endpwent();
   m_allUsers.sort();

   KDialogBase dlg(this, "addUser", true, i18n("Add User Policy"),
      KDialogBase::Ok|KDialogBase::Cancel, KDialogBase::Ok, true );
      
   UserManagementUserUI *wid = new UserManagementUserUI(&dlg);
   wid->labelCaption->setFixedSize(wid->labelCaption->sizeHint());
   wid->comboUser->insertStringList(m_allUsers);
   wid->comboProfile->insertStringList(m_allProfiles);
   wid->setFixedSize(wid->sizeHint());
   dlg.setMainWidget(wid);
   dlg.setFixedSize(dlg.sizeHint());
   while (dlg.exec() == KDialogBase::Accepted)
   {
      TQString user = wid->comboUser->currentText();
      TQString profile = wid->comboProfile->currentText();

      // Check for dupes
      TQListViewItem *item = listUsers->firstChild();
      for( ;item; item = item->nextSibling())
      {
         if (item->text(0) == user)
            break;
      }
      if (item)
      {
         int result = KMessageBox::warningContinueCancel(this,
                   i18n("<qt>You already have a profile defined for user <b>%1</b>. "
                        "Do you want to replace it?</<qt>").arg(user), 
                   i18n("Duplicate Warning"),
                   i18n("&Replace"));
         if (result != KMessageBox::Continue)
            continue; // Go back to edit dialog
         delete item;
      }

      item = new TQListViewItem(listUsers, user, profile);
      listUsers->setSelected(item, true);
      slotUpdateButtons();
      return;
   }
}

void UserManagementPage::slotDeleteUser()
{
   TQListViewItem *item = listUsers->selectedItem();
   if (!item)
      return;
      
   delete item;

   item = listUsers->currentItem();
   if (item)
      listUsers->setSelected(item, true);
   slotUpdateButtons();
}

void UserManagementPage::setFocus()
{
   // TODO
}

TQString UserManagementPage::subCaption()
{
   return i18n("Assign Profiles");
}

#include "userManagement.moc"