/*************************************************************************** * groupswindow.cpp * * Thu Jun 30 01:25:01 2005 * Copyright 2005 User * Email ****************************************************************************/ #include "groupswindow.h" groupsWindow::groupsWindow(groupsWindowCallback _cb, gpointer _cbdata) { cb = _cb; cbdata = _cbdata; wType = UE_EDITGROUPS; } groupsWindow::~groupsWindow() { } GtkWidget* groupsWindow::createWindowContent() { GtkWidget *hbox1, *table1, *label1, *label2, *vbox1, *vbox2, *vbox3, *notebook, *groupsList; groupInfo *group; gchar *fstr; // create the groupsList groupsList = createGroupsList(); // create the toolbar addButton = u_createStockImageButton(GTK_STOCK_ADD); g_signal_connect_swapped(addButton, "clicked", G_CALLBACK(this->cb_addButtonClicked), this); removeButton = u_createStockImageButton(GTK_STOCK_REMOVE); g_signal_connect_swapped(removeButton, "clicked", G_CALLBACK(this->cb_removeButtonClicked), this); upButton = u_createStockImageButton(GTK_STOCK_GO_UP); g_signal_connect_swapped(upButton, "clicked", G_CALLBACK(this->cb_upButtonClicked), this); downButton = u_createStockImageButton(GTK_STOCK_GO_DOWN); g_signal_connect_swapped(downButton, "clicked", G_CALLBACK(this->cb_downButtonClicked), this); editButton = u_createStockImageButton(GTK_STOCK_JUSTIFY_LEFT); g_signal_connect_swapped(editButton, "clicked", G_CALLBACK(this->cb_editButtonClicked), this); hbox1 = gtk_hbox_new(FALSE, 2); gtk_box_pack_start(GTK_BOX(hbox1), addButton, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(hbox1), removeButton, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(hbox1), upButton, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(hbox1), downButton, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(hbox1), editButton, FALSE, FALSE, 0); vbox1 = gtk_vbox_new(FALSE, 3); gtk_box_pack_start(GTK_BOX(vbox1), groupsList, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, FALSE, 0); // create the special groups elements setDefaultButton = u_createStockImageButton(GTK_STOCK_JUMP_TO); g_signal_connect_swapped(setDefaultButton, "clicked", G_CALLBACK(this->cb_defaultGroupButtonClicked), this); setNewButton = u_createStockImageButton(GTK_STOCK_JUMP_TO); g_signal_connect_swapped(setNewButton, "clicked", G_CALLBACK(this->cb_newGroupButtonClicked), this); fstr = g_strdup_printf("%s", tr("Default Group")); label1 = gtk_label_new(fstr); g_free(fstr); gtk_label_set_use_markup(GTK_LABEL(label1), TRUE); gtk_misc_set_alignment(GTK_MISC(label1), 0.0f, 0.5f); fstr = g_strdup_printf("%s", tr("New Users Group")); label2 = gtk_label_new(fstr); g_free(fstr); gtk_label_set_use_markup(GTK_LABEL(label2), TRUE); gtk_misc_set_alignment(GTK_MISC(label2), 0.0f, 0.5f); group = IO_getGroupManager()->getGroupByNum(IO_getGroupManager()->getDefaultGroup()); defaultGroup = IO_getGroupManager()->getDefaultGroup(); if (group) defaultGroupLabel = gtk_label_new(group->name); else { defaultGroup = 0; defaultGroupLabel = gtk_label_new(IO_getGroupManager()->getNoGroup()->name); } gtk_misc_set_alignment(GTK_MISC(defaultGroupLabel), 0.0f, 0.5f); group = IO_getGroupManager()->getGroupByNum(IO_getGroupManager()->getNewUsersGroup()); newUsersGroup = IO_getGroupManager()->getNewUsersGroup(); if (group) newUsersGroupLabel = gtk_label_new(group->name); else { newUsersGroup = 0; newUsersGroupLabel = gtk_label_new(IO_getGroupManager()->getNoGroup()->name); } gtk_misc_set_alignment(GTK_MISC(newUsersGroupLabel), 0.0f, 0.5f); table1 = gtk_table_new(2, 4, FALSE); gtk_table_attach(GTK_TABLE(table1), setDefaultButton, 0, 1, 0, 1, GTK_SHRINK, GTK_SHRINK, 3, 1); gtk_table_attach(GTK_TABLE(table1), label1, 1, 2, 0, 1, (GtkAttachOptions)GTK_FILL, (GtkAttachOptions)0, 1, 1); gtk_table_attach(GTK_TABLE(table1), gtk_label_new(": "), 2, 3, 0, 1, GTK_SHRINK, GTK_SHRINK, 2, 0); gtk_table_attach_defaults(GTK_TABLE(table1), defaultGroupLabel, 3, 4, 0, 1); gtk_table_attach(GTK_TABLE(table1), setNewButton, 0, 1, 1, 2, GTK_SHRINK, GTK_SHRINK, 3, 1); gtk_table_attach(GTK_TABLE(table1), label2, 1, 2, 1, 2, (GtkAttachOptions)GTK_FILL, (GtkAttachOptions)0, 1, 1); gtk_table_attach(GTK_TABLE(table1), gtk_label_new(": "), 2, 3, 1, 2, GTK_SHRINK, GTK_SHRINK, 2, 0); gtk_table_attach_defaults(GTK_TABLE(table1), newUsersGroupLabel, 3, 4, 1, 2); // create another vbox containing both elements vbox2 = gtk_vbox_new(FALSE, 10); gtk_container_set_border_width(GTK_CONTAINER(vbox2), 5); gtk_box_pack_start(GTK_BOX(vbox2), vbox1, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(vbox2), table1, FALSE, FALSE, 0); // create a "notebook" around it notebook = gtk_notebook_new(); gtk_notebook_set_show_tabs(GTK_NOTEBOOK(notebook), FALSE); gtk_notebook_append_page(GTK_NOTEBOOK(notebook), vbox2, NULL); // now add everything into the main vbox vbox3 = gtk_vbox_new(FALSE, 10); gtk_container_set_border_width(GTK_CONTAINER(vbox3), 10); gtk_box_pack_start(GTK_BOX(vbox3), notebook, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(vbox3), createButtonbar(), FALSE, TRUE, 0); setWindowSize(350, 400); setWindowTitle(tr("Edit User Groups")); return vbox3; /* GtkWidget *groupsList, *vbox1, *vbox2, *eventBox, *hbox1, *hbox2, *buttonImg, *table, *label1, *label2, *alignment, *frame1, *frame2; GtkSizeGroup *buttonSizes; groupInfo *group; // create the groupsList groupsList = createGroupsList(); // ensure that all buttons have the same size buttonSizes = gtk_size_group_new(GTK_SIZE_GROUP_BOTH); // create the tool buttons addButton = u_createStockImageButton(GTK_STOCK_ADD); buttonImg = gtk_image_new_from_stock(GTK_STOCK_ADD, GTK_ICON_SIZE_BUTTON); addButton = gtk_button_new(); gtk_container_add(GTK_CONTAINER(addButton), buttonImg); gtk_size_group_add_widget(buttonSizes, addButton); g_signal_connect_swapped(addButton, "clicked", G_CALLBACK(this->cb_addButtonClicked), this); buttonImg = gtk_image_new_from_stock(GTK_STOCK_REMOVE, GTK_ICON_SIZE_BUTTON); removeButton = gtk_button_new(); gtk_container_add(GTK_CONTAINER(removeButton), buttonImg); gtk_size_group_add_widget(buttonSizes, removeButton); g_signal_connect_swapped(removeButton, "clicked", G_CALLBACK(this->cb_removeButtonClicked), this); buttonImg = gtk_image_new_from_stock(GTK_STOCK_GO_UP, GTK_ICON_SIZE_BUTTON); upButton = gtk_button_new(); gtk_container_add(GTK_CONTAINER(upButton), buttonImg); gtk_size_group_add_widget(buttonSizes, upButton); g_signal_connect_swapped(upButton, "clicked", G_CALLBACK(this->cb_upButtonClicked), this); buttonImg = gtk_image_new_from_stock(GTK_STOCK_GO_DOWN, GTK_ICON_SIZE_BUTTON); downButton = gtk_button_new(); gtk_container_add(GTK_CONTAINER(downButton), buttonImg); gtk_size_group_add_widget(buttonSizes, downButton); g_signal_connect_swapped(downButton, "clicked", G_CALLBACK(this->cb_downButtonClicked), this); buttonImg = gtk_image_new_from_stock(GTK_STOCK_JUSTIFY_LEFT, GTK_ICON_SIZE_SMALL_TOOLBAR); editButton = gtk_button_new(); gtk_container_add(GTK_CONTAINER(editButton), buttonImg); gtk_size_group_add_widget(buttonSizes, editButton); g_signal_connect_swapped(editButton, "clicked", G_CALLBACK(this->cb_editButtonClicked), this); buttonImg = gtk_image_new_from_stock(GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_SMALL_TOOLBAR); setDefaultButton = gtk_button_new(); gtk_container_add(GTK_CONTAINER(setDefaultButton), buttonImg); g_signal_connect_swapped(setDefaultButton, "clicked", G_CALLBACK(this->cb_defaultGroupButtonClicked), this); buttonImg = gtk_image_new_from_stock(GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_SMALL_TOOLBAR); setNewButton = gtk_button_new(); gtk_container_add(GTK_CONTAINER(setNewButton), buttonImg); g_signal_connect_swapped(setNewButton, "clicked", G_CALLBACK(this->cb_newGroupButtonClicked), this); vbox1 = gtk_vbox_new(FALSE, 2); gtk_box_pack_start(GTK_BOX(vbox1), addButton, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(vbox1), removeButton, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(vbox1), editButton, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(vbox1), gtk_hseparator_new(), FALSE, TRUE, 2); gtk_box_pack_start(GTK_BOX(vbox1), upButton, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(vbox1), downButton, FALSE, FALSE, 0); // create the standard group and new users group table label1 = gtk_label_new(tr("Default group")); gtk_label_set_use_markup(GTK_LABEL(label1), TRUE); gtk_misc_set_alignment(GTK_MISC(label1), 0.0f, 0.5f); label2 = gtk_label_new(tr("New Users group")); gtk_label_set_use_markup(GTK_LABEL(label2), TRUE); gtk_misc_set_alignment(GTK_MISC(label2), 0.0f, 0.5f); group = IO_getGroupManager()->getGroupByNum(IO_getGroupManager()->getDefaultGroup()); defaultGroup = IO_getGroupManager()->getDefaultGroup(); if (group) defaultGroupLabel = gtk_label_new(group->name); else { defaultGroup = 0; defaultGroupLabel = gtk_label_new(IO_getGroupManager()->getNoGroup()->name); } gtk_misc_set_alignment(GTK_MISC(defaultGroupLabel), 0.0f, 0.5f); group = IO_getGroupManager()->getGroupByNum(IO_getGroupManager()->getNewUsersGroup()); newUsersGroup = IO_getGroupManager()->getNewUsersGroup(); if (group) newUsersGroupLabel = gtk_label_new(group->name); else { newUsersGroup = 0; newUsersGroupLabel = gtk_label_new(IO_getGroupManager()->getNoGroup()->name); } gtk_misc_set_alignment(GTK_MISC(newUsersGroupLabel), 0.0f, 0.5f); table = gtk_table_new(2, 4, FALSE); gtk_table_attach(GTK_TABLE(table), setDefaultButton, 0, 1, 0, 1, GTK_SHRINK, GTK_SHRINK, 3, 1); gtk_table_attach(GTK_TABLE(table), label1, 1, 2, 0, 1, (GtkAttachOptions)GTK_FILL, (GtkAttachOptions)0, 1, 1); gtk_table_attach(GTK_TABLE(table), gtk_label_new(": "), 2, 3, 0, 1, GTK_SHRINK, GTK_SHRINK, 2, 0); gtk_table_attach_defaults(GTK_TABLE(table), defaultGroupLabel, 3, 4, 0, 1); gtk_table_attach(GTK_TABLE(table), setNewButton, 0, 1, 1, 2, GTK_SHRINK, GTK_SHRINK, 3, 1); gtk_table_attach(GTK_TABLE(table), label2, 1, 2, 1, 2, (GtkAttachOptions)GTK_FILL, (GtkAttachOptions)0, 1, 1); gtk_table_attach(GTK_TABLE(table), gtk_label_new(": "), 2, 3, 1, 2, GTK_SHRINK, GTK_SHRINK, 2, 0); gtk_table_attach_defaults(GTK_TABLE(table), newUsersGroupLabel, 3, 4, 1, 2); alignment = gtk_alignment_new(0.0f, 0.0f, 1.0f, 1.0f); gtk_alignment_set_padding(GTK_ALIGNMENT(alignment), 5, 0, 0, 0); gtk_container_add(GTK_CONTAINER(alignment), table); // connect groups list and tool buttons hbox1 = gtk_hbox_new(FALSE, 2); gtk_box_pack_start(GTK_BOX(hbox1), groupsList, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(hbox1), vbox1, FALSE, TRUE, 0); // create the main box frame1 = gtk_notebook_new(); gtk_container_add(GTK_CONTAINER(frame1), hbox1); //frame1 = gtk_frame_new(NULL); //gtk_frame_set_shadow_type(GTK_FRAME(frame1), GTK_SHADOW_OUT); //gtk_container_add(GTK_CONTAINER(frame1), eventBox); frame2 = gtk_frame_new(NULL); gtk_frame_set_shadow_type(GTK_FRAME(frame2), GTK_SHADOW_OUT); gtk_container_add(GTK_CONTAINER(frame2), alignment); vbox2 = gtk_vbox_new(FALSE, 2); gtk_container_set_border_width(GTK_CONTAINER(vbox2), 4); gtk_box_pack_start(GTK_BOX(vbox2), frame1, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(vbox2), frame2, FALSE, TRUE, 2); gtk_box_pack_start(GTK_BOX(vbox2), createButtonbar(), FALSE, TRUE, 0); setWindowSize(300, 270); return vbox2; */ } GtkWidget* groupsWindow::createButtonbar() { GtkWidget *bbar; closeButton = gtk_button_new_from_stock(GTK_STOCK_CLOSE); g_signal_connect_swapped(closeButton, "clicked", G_CALLBACK(cb_closeButtonClicked), this); saveButton = gtk_button_new_from_stock(GTK_STOCK_APPLY); g_signal_connect_swapped(saveButton, "clicked", G_CALLBACK(cb_saveButtonClicked), this); gtk_widget_set_sensitive(saveButton, FALSE); bbar = gtk_hbutton_box_new(); gtk_button_box_set_layout(GTK_BUTTON_BOX(bbar), GTK_BUTTONBOX_END); gtk_box_set_spacing(GTK_BOX(bbar), 2); gtk_box_pack_start(GTK_BOX(bbar), saveButton, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(bbar), closeButton, FALSE, FALSE, 0); return bbar; } GtkWidget* groupsWindow::createGroupsList() { GtkTreeViewColumn *column; GtkCellRenderer *renderer; GtkWidget *scrolled; GList *groups; groupInfo *currentGroup; GtkTreeIter iter; // create the store store = gtk_list_store_new(5, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT, G_TYPE_BOOLEAN, G_TYPE_INT); ownID = 0; // fill the store groups = IO_getGroupManager()->groups; while(groups) { currentGroup = (groupInfo*)groups->data; gtk_list_store_append(store, &iter); gtk_list_store_set(store, &iter, 0, currentGroup->name, 1, currentGroup->numUsers, 2, ownID++, 3, currentGroup->id != 0xFFFF, 4, currentGroup->id, -1); groups = groups->next; } tv = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store)); g_signal_connect_swapped(gtk_tree_view_get_selection(GTK_TREE_VIEW(tv)), "changed", G_CALLBACK(this->cb_groupListCursorChanged), this); // create the columns textRenderer = gtk_cell_renderer_text_new(); g_signal_connect(textRenderer, "edited", G_CALLBACK(this->cb_groupEntryEdited), this); textColumn = gtk_tree_view_column_new_with_attributes(tr("Name"), textRenderer, "markup", 0, "editable", 3, NULL); gtk_tree_view_column_set_expand(textColumn, TRUE); gtk_tree_view_append_column(GTK_TREE_VIEW(tv), textColumn); renderer = gtk_cell_renderer_text_new(); column = gtk_tree_view_column_new_with_attributes(tr("Users"), renderer, "text", 1, NULL); // gtk_tree_view_column_set_fixed_width(column, 100); gtk_tree_view_append_column(GTK_TREE_VIEW(tv), column); scrolled = gtk_scrolled_window_new(NULL, NULL); gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(scrolled), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN); gtk_container_add(GTK_CONTAINER(scrolled), tv); return scrolled; } void groupsWindow::destroyWindowContent() { if (cb) cb(0, cbdata); } #define GW_SETTOOLBUTTONS(b1, b2, b3, b4, b5) \ gtk_widget_set_sensitive(self->upButton, b1); \ gtk_widget_set_sensitive(self->downButton, b2); \ gtk_widget_set_sensitive(self->editButton, b3); \ gtk_widget_set_sensitive(self->addButton, b4); \ gtk_widget_set_sensitive(self->removeButton, b5); void groupsWindow::cb_groupListCursorChanged(groupsWindow* self) { GtkTreePath *path; gint pos; gboolean upButton, downButton; gtk_tree_view_get_cursor(GTK_TREE_VIEW(self->tv), &path, NULL); if (!path) return; pos = gtk_tree_path_get_indices(path)[0]; switch(pos) { case 0: GW_SETTOOLBUTTONS(FALSE, FALSE, FALSE, TRUE, FALSE); break; default: upButton = TRUE; downButton = TRUE; if (pos == 1) upButton = FALSE; if (gtk_tree_model_iter_n_children(GTK_TREE_MODEL(self->store), NULL)-1 == pos) downButton = FALSE; GW_SETTOOLBUTTONS(upButton, downButton, TRUE, TRUE, TRUE); break; } // gtk_tree_model_get_iter(GTK_TREE_MODEL(store), &iter, path); gtk_tree_path_free(path); } void groupsWindow::cb_addButtonClicked(groupsWindow* self) { GtkTreeIter iter, newIter; GtkTreePath *path; // good, we now have an id that isn set yet. gtk_tree_view_get_cursor(GTK_TREE_VIEW(self->tv), &path, NULL); if (!path) path = gtk_tree_path_new_from_string("0"); gtk_tree_model_get_iter(GTK_TREE_MODEL(self->store), &iter, path); gtk_list_store_insert_after(GTK_LIST_STORE(self->store), &newIter, &iter); gtk_list_store_set(GTK_LIST_STORE(self->store), &newIter, 0, "", 1, 0, 2, self->ownID++, 3, TRUE, 4, -1, -1); gtk_tree_path_free(path); gtk_widget_grab_focus(self->tv); path = gtk_tree_model_get_path(GTK_TREE_MODEL(self->store), &newIter); gtk_tree_view_set_cursor(GTK_TREE_VIEW(self->tv), path, self->textColumn, TRUE); gtk_tree_path_free(path); cb_groupListCursorChanged(self); gtk_widget_set_sensitive(self->saveButton, TRUE); } void groupsWindow::cb_removeButtonClicked(groupsWindow* self) { GtkTreePath *path; GtkTreeIter iter; gint num; gtk_tree_view_get_cursor(GTK_TREE_VIEW(self->tv), &path, NULL); if (!path) return; gtk_tree_model_get_iter(GTK_TREE_MODEL(self->store), &iter, path); gtk_tree_model_get(GTK_TREE_MODEL(self->store), &iter, 2, &num, -1); if (num == self->defaultGroup) { self->defaultGroup = 0; gtk_label_set_label(GTK_LABEL(self->defaultGroupLabel), IO_getGroupManager()->getNoGroup()->name); } if (num == self->newUsersGroup) { self->newUsersGroup = 0; gtk_label_set_label(GTK_LABEL(self->newUsersGroupLabel), IO_getGroupManager()->getNoGroup()->name); } gtk_list_store_remove(GTK_LIST_STORE(self->store), &iter); gtk_widget_set_sensitive(self->saveButton, TRUE); } void groupsWindow::cb_upButtonClicked(groupsWindow* self) { GtkTreePath *path; GtkTreeIter iter1, iter2; gtk_tree_view_get_cursor(GTK_TREE_VIEW(self->tv), &path, NULL); if (!path) return; gtk_tree_model_get_iter(GTK_TREE_MODEL(self->store), &iter1, path); gtk_tree_path_prev(path); gtk_tree_model_get_iter(GTK_TREE_MODEL(self->store), &iter2, path); gtk_list_store_swap(GTK_LIST_STORE(self->store), &iter1, &iter2); gtk_tree_view_set_cursor(GTK_TREE_VIEW(self->tv), path, self->textColumn, FALSE); gtk_tree_path_free(path); gtk_widget_set_sensitive(self->saveButton, TRUE); self->cb_groupListCursorChanged(self); } void groupsWindow::cb_downButtonClicked(groupsWindow* self) { GtkTreePath *path; GtkTreeIter iter1, iter2; gtk_tree_view_get_cursor(GTK_TREE_VIEW(self->tv), &path, NULL); if (!path) return; gtk_tree_model_get_iter(GTK_TREE_MODEL(self->store), &iter1, path); gtk_tree_path_next(path); gtk_tree_model_get_iter(GTK_TREE_MODEL(self->store), &iter2, path); gtk_list_store_swap(GTK_LIST_STORE(self->store), &iter1, &iter2); gtk_tree_view_set_cursor(GTK_TREE_VIEW(self->tv), path, self->textColumn, FALSE); gtk_tree_path_free(path); gtk_widget_set_sensitive(self->saveButton, TRUE); self->cb_groupListCursorChanged(self); } void groupsWindow::cb_editButtonClicked(groupsWindow* self) { GtkTreePath *path; gtk_tree_view_get_cursor(GTK_TREE_VIEW(self->tv), &path, NULL); if (!path) return; gtk_tree_view_set_cursor(GTK_TREE_VIEW(self->tv), path, self->textColumn, TRUE); gtk_tree_path_free(path); } void groupsWindow::cb_defaultGroupButtonClicked(groupsWindow* self) { GtkTreePath *path; GtkTreeIter iter; gchar *name; guint id; gtk_tree_view_get_cursor(GTK_TREE_VIEW(self->tv), &path, NULL); if (!path) return; gtk_tree_model_get_iter(GTK_TREE_MODEL(self->store), &iter, path); gtk_tree_model_get(GTK_TREE_MODEL(self->store), &iter, 0, &name, 2, &id, -1); self->defaultGroup = id; gtk_label_set_label(GTK_LABEL(self->defaultGroupLabel), name); gtk_tree_path_free(path); gtk_widget_set_sensitive(self->saveButton, TRUE); } void groupsWindow::cb_newGroupButtonClicked(groupsWindow* self) { GtkTreePath *path; GtkTreeIter iter; gchar *name; guint id; gtk_tree_view_get_cursor(GTK_TREE_VIEW(self->tv), &path, NULL); if (!path) return; gtk_tree_model_get_iter(GTK_TREE_MODEL(self->store), &iter, path); gtk_tree_model_get(GTK_TREE_MODEL(self->store), &iter, 0, &name, 2, &id, -1); self->newUsersGroup = id; gtk_label_set_label(GTK_LABEL(self->newUsersGroupLabel), name); gtk_tree_path_free(path); gtk_widget_set_sensitive(self->saveButton, TRUE); } void groupsWindow::cb_groupEntryEdited(GtkCellRendererText *cell, gchar *pathStr, gchar *newText, groupsWindow* self) { GtkTreePath *path; GtkTreeIter iter, iter2; guint id, oldId; gchar *oldText, *tmpText; path = gtk_tree_path_new_from_string(pathStr); gtk_tree_model_get_iter(GTK_TREE_MODEL(self->store), &iter, path); gtk_tree_model_get(GTK_TREE_MODEL(self->store), &iter, 0, &oldText, 2, &id, -1); // check whether there is already an entry with that name if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(self->store), &iter2)) { do { gtk_tree_model_get(GTK_TREE_MODEL(self->store), &iter2, 0, &tmpText, 2, &oldId, -1); if (!strcmp(tmpText, newText) && id != oldId) { gtk_tree_path_free(path); u_showAlertMessage(tr("Similar Names"), tr("Every group has to have a different name.\n" "There is already a group with the name you\n" "wrote. Please select another name."), GTK_STOCK_DIALOG_ERROR); return; } } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(self->store), &iter2)); } // apply changes (only if the new name doesn't equal the old name) if (strcmp(oldText, newText) != 0) { gtk_list_store_set(GTK_LIST_STORE(self->store), &iter, 0, newText, -1); if (id == self->defaultGroup) gtk_label_set_text(GTK_LABEL(self->defaultGroupLabel), newText); if (id == self->newUsersGroup) gtk_label_set_text(GTK_LABEL(self->newUsersGroupLabel), newText); gtk_widget_set_sensitive(self->saveButton, TRUE); } gtk_tree_path_free(path); } void groupsWindow::cb_saveButtonClicked(groupsWindow* self) { GList *groupsCopy, *newGroups, *search1, *search2; IMGroupManager *groupMan; groupInfo *newGroup, *oldGroup; GtkTreeIter iter; gchar *newName; gint newID, newNum, oldCount, i; // okay, here comes the tricky part groupMan = IO_getGroupManager(); // 1. get the new list newGroups = NULL; if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(self->store), &iter)) { do { gtk_tree_model_get(GTK_TREE_MODEL(self->store), &iter, 0, &newName, 2, &newNum, 4, &newID, -1); newGroup = (groupInfo*)g_malloc0(sizeof(groupInfo)); newGroup->name = newName; newGroup->id = newID; newGroup->num = newNum; newGroups = g_list_append(newGroups, newGroup); } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(self->store), &iter)); } oldCount = g_list_length(groupMan->groups); // 2. remove groups groupsCopy = g_list_copy(groupMan->groups); search1 = newGroups; while(search1) { search2 = groupsCopy; while(search2) { if (((groupInfo*)search1->data)->id == ((groupInfo*)search2->data)->id) { groupsCopy = g_list_remove(groupsCopy, search2->data); break; } search2 = search2->next; } search1 = search1->next; } search1 = groupsCopy; while(search1) { if (((groupInfo*)search1->data)->id != -1) groupMan->removeGroup(((groupInfo*)search1->data)->id); search1 = search1->next; } g_list_free(groupsCopy); // 3. rename groups search1 = groupMan->groups; while(search1) { oldGroup = (groupInfo*)search1->data; search2 = newGroups; while(search2) { newGroup = (groupInfo*)search2->data; if (oldGroup->id == newGroup->id) if (strcmp(oldGroup->name, newGroup->name) != 0) groupMan->renameGroup(newGroup->id, newGroup->name); search2 = search2->next; } search1 = search1->next; } // 4. add new groups search1 = newGroups; i=0; while(search1) { oldGroup = (groupInfo*)search1->data; if (oldGroup->num >= oldCount) { // FIXME: this will crash if group cannot be created if (!(newGroup = groupMan->addGroup(oldGroup->name, -1))) fprintf(stderr, "groupsWindow::cb_saveButtonClicked(): Couldn't create group %s\n", oldGroup->name); else { oldGroup->id = newGroup->id; // update our id in the groupwindows list if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(self->store), &iter)) { do { gtk_tree_model_get(GTK_TREE_MODEL(self->store), &iter, 0, &newName, 4, &newID, -1); if (newID == -1 && !strcmp(newName, newGroup->name)) { gtk_list_store_set(GTK_LIST_STORE(self->store), &iter, 4, newGroup->id, -1); break; } } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(self->store), &iter)); } } i++; } search1 = search1->next; } // 5. resort all users groupMan->resortGroups(newGroups); // 6. reset the standard groups newNum = 0; search1 = newGroups; while(search1) { newGroup = (groupInfo*)search1->data; if (newGroup->num == self->newUsersGroup) groupMan->setNewUsersGroup(newNum); if (newGroup->num == self->defaultGroup) groupMan->setDefaultGroup(newNum); newNum++; search1 = search1->next; } // 7. call the owner of this window if (self->cb) self->cb(1, self->cbdata); gtk_widget_set_sensitive(self->saveButton, FALSE); } void groupsWindow::cb_closeButtonClicked(groupsWindow* self) { self->destroyWindow(); delete self; }