1
# gcompris - group_edit.py
3
# Copyright (C) 2005 Bruno Coudoin and Yves Combe
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
from gettext import gettext as _
25
import group_user_list
30
from pysqlite2 import dbapi2 as sqlite
32
# User List Management
41
class GroupEdit(gtk.Window):
43
def __init__(self, db_connect, db_cursor,
45
group_id, group_name, group_description,
47
# Create the toplevel window
48
gtk.Window.__init__(self)
53
self.group_id = group_id
54
self.class_id = class_id
56
# A pointer to the group_user_list class
57
# Will be called to refresh the list when edit is done
58
self.group_user = group_user
60
self.set_title(_("Editing a Group"))
61
self.set_border_width(8)
62
self.set_default_size(320, 350)
64
self.group_name = group_name
66
frame = gtk.Frame(_("Editing group: ") + self.group_name +
67
_(" for class: ") + class_name)
68
self.new_group = False
70
frame = gtk.Frame(_("Editing a new group"))
73
group_description = ""
79
vbox = gtk.VBox(False, 8)
80
vbox.set_border_width(8)
83
# Label and Entry for the group and description
84
table = gtk.Table(2, 2, homogeneous=False)
85
table.set_border_width(0)
86
table.set_row_spacings(0)
87
table.set_col_spacings(20)
88
vbox.pack_start(table, True, True, 0)
90
label = gtk.Label(_('Group:'))
91
label.set_alignment(0, 0)
92
table.attach(label, 0, 1, 0, 1, xoptions=gtk.SHRINK, yoptions=gtk.EXPAND)
93
self.entry_group = gtk.Entry()
94
self.entry_group.set_max_length(20)
95
self.entry_group.insert_text(self.group_name, position=0)
96
table.attach(self.entry_group, 1, 2, 0, 1,
97
xoptions=gtk.SHRINK, yoptions=gtk.EXPAND)
99
# FIXME: How to remove the selection
101
# Label and Entry for the first name
102
label = gtk.Label(_('Description:'))
103
label.set_alignment(0, 0)
104
table.attach(label, 0, 1, 1, 2, xoptions=gtk.SHRINK, yoptions=gtk.EXPAND)
105
self.entry_description = gtk.Entry()
106
self.entry_description.set_max_length(30)
107
self.entry_description.insert_text(group_description, position=0)
108
table.attach(self.entry_description, 1, 2, 1, 2,
109
xoptions=gtk.SHRINK, yoptions=gtk.EXPAND)
112
# Top message gives instructions
113
label = gtk.Label(_('Assign all the users belonging to this group'))
114
vbox.pack_start(label, False, False, 0)
115
vbox.pack_start(gtk.HSeparator(), False, False, 0)
118
hbox = gtk.HBox(False, 8)
119
vbox.pack_start(hbox, True, True, 0)
125
sw = gtk.ScrolledWindow()
126
sw.set_shadow_type(gtk.SHADOW_ETCHED_IN)
127
sw.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
130
self.model_left = self.__create_model(False, class_id, group_id)
133
treeview = gtk.TreeView(self.model_left)
134
treeview.set_rules_hint(True)
135
treeview.set_search_column(COLUMN_FIRSTNAME)
136
treeview.get_selection().set_mode(gtk.SELECTION_MULTIPLE)
140
# add columns to the tree view
141
self.__add_columns(treeview)
143
hbox.pack_start(sw, True, True, 0)
148
vbox2 = gtk.VBox(False, 8)
149
vbox2.set_border_width(8)
150
hbox.pack_start(vbox2, True, True, 0)
152
button = gtk.Button(stock='gtk-add')
153
button.connect("clicked", self.add_user, treeview)
154
vbox2.pack_start(button, False, False, 0)
157
button_delete = gtk.Button(stock='gtk-remove')
158
vbox2.pack_start(button_delete, False, False, 0)
166
sw2 = gtk.ScrolledWindow()
167
sw2.set_shadow_type(gtk.SHADOW_ETCHED_IN)
168
sw2.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
171
self.model_right = self.__create_model(True, class_id, group_id)
174
treeview2 = gtk.TreeView(self.model_right)
175
treeview2.set_rules_hint(True)
176
treeview2.set_search_column(COLUMN_FIRSTNAME)
177
treeview2.get_selection().set_mode(gtk.SELECTION_MULTIPLE)
181
# add columns to the tree view
182
self.__add_columns(treeview2)
184
hbox.pack_start(sw2, True, True, 0)
186
# Confirmation Buttons
187
# --------------------
188
vbox.pack_start(gtk.HSeparator(), False, False, 0)
190
bbox = gtk.HBox(homogeneous=False, spacing=8)
192
button = gtk.Button(stock='gtk-help')
193
bbox.pack_start(button, expand=False, fill=False, padding=0)
195
button = gtk.Button(stock='gtk-ok')
196
bbox.pack_end(button, expand=False, fill=False, padding=0)
197
button.connect("clicked", self.ok)
199
button = gtk.Button(stock='gtk-close')
200
bbox.pack_end(button, expand=False, fill=False, padding=0)
201
button.connect("clicked", self.close)
203
vbox.pack_start(bbox, False, False, 0)
207
button_delete.connect("clicked", self.remove_user, treeview2)
213
# -------------------
215
# -------------------
217
# Add user in the model
218
def add_user_in_model(self, model, user):
219
iter = model.append()
221
COLUMN_USERID, user[COLUMN_USERID],
222
COLUMN_FIRSTNAME, user[COLUMN_FIRSTNAME],
223
COLUMN_LASTNAME, user[COLUMN_LASTNAME],
224
COLUMN_USER_EDITABLE, False
227
# class_id: only users in this class are inserted
228
# group_id: only users in this group are inserted
229
# If with = True, create a list only with user in the given class_id and group_id.
230
# False, create a list only with user in the given class_id but NOT this group_id
231
def __create_model(self, with, class_id, group_id):
233
model = gtk.ListStore(
237
gobject.TYPE_BOOLEAN)
239
# Grab the all the users from this class
240
self.cur.execute('SELECT user_id,firstname,lastname FROM users WHERE class_id=? ORDER BY login', (class_id,))
241
user_data = self.cur.fetchall()
243
for user in user_data:
245
# Check our user is already in the group
246
self.cur.execute('SELECT * FROM list_users_in_groups WHERE group_id=? AND user_id=?',
248
user_is_already = self.cur.fetchall()
250
if(with and user_is_already):
251
self.add_user_in_model(model, user)
252
elif(not with and not user_is_already):
253
self.add_user_in_model(model, user)
258
def __add_columns(self, treeview):
260
model = treeview.get_model()
262
# columns for first name
263
renderer = gtk.CellRendererText()
264
renderer.set_data("column", COLUMN_FIRSTNAME)
265
column = gtk.TreeViewColumn(_('First Name'), renderer,
266
text=COLUMN_FIRSTNAME,
267
editable=COLUMN_USER_EDITABLE)
268
column.set_sort_column_id(COLUMN_FIRSTNAME)
269
column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
270
column.set_fixed_width(constants.COLUMN_WIDTH_FIRSTNAME)
271
treeview.append_column(column)
273
# column for last name
274
renderer = gtk.CellRendererText()
275
renderer.set_data("column", COLUMN_LASTNAME)
276
column = gtk.TreeViewColumn(_('Last Name'), renderer,
277
text=COLUMN_LASTNAME,
278
editable=COLUMN_USER_EDITABLE)
279
column.set_sort_column_id(COLUMN_LASTNAME)
280
column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
281
column.set_fixed_width(constants.COLUMN_WIDTH_LASTNAME)
282
treeview.append_column(column)
285
# Add a user from the left list to the right list
287
def add_user(self, button, treeview):
289
model = treeview.get_model()
291
treestore, paths = treeview.get_selection().get_selected_rows()
297
iter = treestore.get_iter(path)
299
path = model.get_path(iter)[0]
300
user_id = model.get_value(iter, COLUMN_USERID)
301
user_firstname = model.get_value(iter, COLUMN_FIRSTNAME)
302
user_lastname = model.get_value(iter, COLUMN_LASTNAME)
305
# Add in the the right view
306
self.add_user_in_model(self.model_right, (user_id, user_firstname, user_lastname))
308
# Save the change in the base
309
self.cur.execute('INSERT OR REPLACE INTO list_users_in_groups (group_id, user_id) VALUES (?, ?)',
310
(self.group_id, user_id))
314
# Add a user from the left list to the right list
316
def remove_user(self, button, treeview):
318
model = treeview.get_model()
320
treestore, paths = treeview.get_selection().get_selected_rows()
326
iter = treestore.get_iter(path)
328
path = model.get_path(iter)[0]
329
user_id = model.get_value(iter, COLUMN_USERID)
330
user_firstname = model.get_value(iter, COLUMN_FIRSTNAME)
331
user_lastname = model.get_value(iter, COLUMN_LASTNAME)
334
# Add in the the left view
335
self.add_user_in_model(self.model_left, (user_id, user_firstname, user_lastname))
337
# Save the change in the base
338
self.cur.execute('delete from list_users_in_groups where group_id=? and user_id=?',
339
(self.group_id, user_id))
344
# Done, can quit this dialog
346
def close(self, button):
347
self.group_user.reload_group()
350
# Done, can quit this dialog with saving
352
def ok(self, button):
354
# Tell the user he must provide enough information
355
if(self.entry_group.get_text() == ""):
356
dialog = gtk.MessageDialog(None,
357
gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
358
gtk.MESSAGE_INFO, gtk.BUTTONS_OK,
359
_("You need to provide at least a name for your group"))
364
# If the group name as changed, check it does not exists already
365
if(self.entry_group.get_text() != self.group_name):
366
# Check the group does not exist already
367
self.cur.execute('SELECT name FROM groups WHERE name=?',
368
(self.entry_group.get_text(),))
369
if(self.cur.fetchone()):
370
dialog = gtk.MessageDialog(None,
371
gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
372
gtk.MESSAGE_INFO, gtk.BUTTONS_OK,
373
_("There is already a group with this name"))
379
# Now everything is correct, create the group
382
group_data = (self.group_id,
383
self.entry_group.get_text(),
385
self.entry_description.get_text()
389
# Create the new group
390
group_id = constants.get_next_group_id(self.con, self.cur)
391
self.cur.execute('INSERT INTO groups (group_id, name, class_id, description) ' +
392
'VALUES ( ?, ?, ?, ?)',
395
# Save the group changes
396
self.cur.execute('UPDATE groups SET name=?, description=? where group_id=?',
397
(self.entry_group.get_text(),
398
self.entry_description.get_text(),
402
# Close the dialog window now
403
self.group_user.reload_group()