/*************************************************************************** * gpgmanager.cpp * * Wed Mar 22 03:26:17 2006 * Copyright 2006 User * Email ****************************************************************************/ #include "gpgmanager.h" #ifdef HAVE_LIBGPGME #include GPGManagerWindow::GPGManagerWindow() { wType = UE_GPGMANAGER; addUsersWindow = NULL; gpgUsers = NULL; gpgSelectWindow = NULL; } GPGManagerWindow::~GPGManagerWindow() { } GtkWidget* GPGManagerWindow::createWindowContent() { GtkWidget *vbox1, *vbox2, *vbox3, *hbox1, *myKeyVBox, *userKeysVBox, *mainVBox, *notebook1, *notebook2, *label1, *label2, *alignment1, *selectOwnKeyButton, *img1; gchar *fstr; // create own key area ownKeyEntry = gtk_entry_new(); img1 = gtk_image_new_from_stock(ICON_KEYRING, GTK_ICON_SIZE_MENU); selectOwnKeyButton = gtk_button_new(); gtk_container_add(GTK_CONTAINER(selectOwnKeyButton), img1); g_signal_connect_swapped(selectOwnKeyButton, "clicked", G_CALLBACK(cb_selectOwnKeyButtonClicked), this); hbox1 = gtk_hbox_new(FALSE, 2); gtk_box_pack_start(GTK_BOX(hbox1), ownKeyEntry, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(hbox1), selectOwnKeyButton, FALSE, TRUE, 0); fstr = g_strdup_printf("%s", tr("Your GPG key")); 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, 0); fstr = g_strdup_printf("%s", tr("This is the key you have the private component for and use it for decrypting. The partner must have the public component to encrypt the message.")); 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, 0); gtk_label_set_line_wrap(GTK_LABEL(label2), TRUE); gtk_widget_set_size_request(label2, 350, -1); vbox1 = gtk_vbox_new(FALSE, 2); gtk_box_pack_start(GTK_BOX(vbox1), label2, FALSE, TRUE, 0); gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, TRUE, 0); alignment1 = gtk_alignment_new(0.0f, 0.0f, 1.0f, 1.0f); \ gtk_alignment_set_padding(GTK_ALIGNMENT(alignment1), 0, 12, 20, 0); \ gtk_container_add(GTK_CONTAINER(alignment1), vbox1); myKeyVBox = gtk_vbox_new(FALSE, 2); gtk_box_pack_start(GTK_BOX(myKeyVBox), label1, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(myKeyVBox), alignment1, FALSE, TRUE, 0); // create the users keys area fstr = g_strdup_printf("%s", tr("Users and their GPG keys")); 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, 0); fstr = g_strdup_printf("%s", tr("The keys you select for the users are their public keys you encrypt the message with.")); 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, 0); gtk_label_set_line_wrap(GTK_LABEL(label2), TRUE); gtk_widget_set_size_request(label2, 350, -1); vbox1 = gtk_vbox_new(FALSE, 2); gtk_box_pack_start(GTK_BOX(vbox1), label2, FALSE, TRUE, 0); gtk_box_pack_start(GTK_BOX(vbox1), createUsersList(), TRUE, TRUE, 0); alignment1 = gtk_alignment_new(0.0f, 0.0f, 1.0f, 1.0f); \ gtk_alignment_set_padding(GTK_ALIGNMENT(alignment1), 0, 12, 20, 0); \ gtk_container_add(GTK_CONTAINER(alignment1), vbox1); userKeysVBox = gtk_vbox_new(FALSE, 2); gtk_box_pack_start(GTK_BOX(userKeysVBox), label1, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(userKeysVBox), alignment1, TRUE, TRUE, 0); // create the main vbox mainVBox = gtk_vbox_new(FALSE, 2); gtk_container_set_border_width(GTK_CONTAINER(mainVBox), 10); gtk_box_pack_start(GTK_BOX(mainVBox), myKeyVBox, FALSE, TRUE, 0); gtk_box_pack_start(GTK_BOX(mainVBox), userKeysVBox, TRUE, TRUE, 0); notebook1 = gtk_notebook_new(); gtk_notebook_set_show_tabs(GTK_NOTEBOOK(notebook1), FALSE); gtk_notebook_append_page(GTK_NOTEBOOK(notebook1), mainVBox, NULL); vbox3 = gtk_vbox_new(FALSE, 10); gtk_container_set_border_width(GTK_CONTAINER(vbox3), 10); gtk_box_pack_start(GTK_BOX(vbox3), notebook1, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(vbox3), createButtonbar(), FALSE, TRUE, 0); setWindowSize(400, 400); setWindowTitle(tr("GPG Key Manager")); return vbox3; } void GPGManagerWindow::destroyWindowContent() { if (addUsersWindow) { addUsersWindow->destroyWindow(); delete addUsersWindow; } } GtkWidget* GPGManagerWindow::createButtonbar() { GtkWidget *hbox, *closeButton; closeButton = gtk_button_new_from_stock(GTK_STOCK_CLOSE); g_signal_connect_swapped(closeButton, "clicked", G_CALLBACK(cb_destroyWindow), this); hbox = gtk_hbox_new(FALSE, 2); gtk_box_pack_end(GTK_BOX(hbox), closeButton, FALSE, FALSE, 0); return hbox; } GtkWidget* GPGManagerWindow::createUsersList() { GtkWidget *scrolled, *hbox, *vbox; GtkCellRenderer *renderer; GtkTreeViewColumn *column; GList *ownerList, *userList; IMGPGManager *GPGMan; IMUserDaemon *user; GtkTreeIter iter; gchar *gpgKey; // Create the users store gpgUsersStore = gtk_list_store_new(4, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_POINTER); gpgUsersView = gtk_tree_view_new_with_model(GTK_TREE_MODEL(gpgUsersStore)); g_signal_connect_swapped(gtk_tree_view_get_selection(GTK_TREE_VIEW(gpgUsersView)), "changed", G_CALLBACK(cb_selectionChanged), this); // the "user" column renderer = gtk_cell_renderer_text_new(); column = gtk_tree_view_column_new_with_attributes("User", renderer, "text", 0, NULL); gtk_tree_view_column_set_expand(column, TRUE); gtk_tree_view_append_column(GTK_TREE_VIEW(gpgUsersView), column); //the "key ID" column renderer = gtk_cell_renderer_text_new(); column = gtk_tree_view_column_new_with_attributes("Key ID", renderer, "text", 1, NULL); gtk_tree_view_column_set_expand(column, TRUE); gtk_tree_view_append_column(GTK_TREE_VIEW(gpgUsersView), column); // the "active" column renderer = gtk_cell_renderer_toggle_new(); column = gtk_tree_view_column_new_with_attributes("Active", renderer, "active", 2, NULL); gtk_tree_view_append_column(GTK_TREE_VIEW(gpgUsersView), column); g_signal_connect_swapped(renderer, "toggled", G_CALLBACK(cb_activeToggled), this); 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), gpgUsersView); // create the icons addButton = u_createStockImageButton(GTK_STOCK_ADD); g_signal_connect_swapped(addButton, "clicked", G_CALLBACK(cb_addButtonClicked), this); removeButton = u_createStockImageButton(GTK_STOCK_REMOVE); g_signal_connect_swapped(removeButton, "clicked", G_CALLBACK(cb_removeButtonClicked), this); setKeyButton = u_createStockImageButton(ICON_KEYRING); g_signal_connect_swapped(setKeyButton, "clicked", G_CALLBACK(cb_selectUserKeyButtonClicked), this); getInfoButton = u_createStockImageButton(ICON_INFO); g_signal_connect_swapped(getInfoButton, "clicked", G_CALLBACK(cb_getInfoButtonClicked), this); hbox = gtk_hbox_new(FALSE, 2); gtk_box_pack_start(GTK_BOX(hbox), addButton, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(hbox), removeButton, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(hbox), setKeyButton, FALSE, FALSE, 0); gtk_box_pack_end(GTK_BOX(hbox), getInfoButton, FALSE, FALSE, 0); // add it to a vbox vbox = gtk_vbox_new(FALSE, 2); gtk_box_pack_start(GTK_BOX(vbox), scrolled, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0); cb_selectionChanged(this); // fill the list for (ownerList = IO_getOwnerList(); ownerList; ownerList = ownerList->next) for (userList = ((IMOwnerDaemon*)ownerList->data)->users; userList; userList = userList->next) { user = (IMUserDaemon*)userList->data; GPGMan = new IMGPGManager(user->info); user->addManager(GPGMan); gpgKey = GPGMan->getGPGKey(); if (GPGMan->getGPGActive() || (gpgKey && *gpgKey)) { gtk_list_store_append(gpgUsersStore, &iter); gtk_list_store_set(gpgUsersStore, &iter, 0, user->info->alias, 1, gpgKey, 2, GPGMan->getGPGActive(), 3, user, -1); } delete GPGMan; } return vbox; } // --------------------------------------------------------------- void GPGManagerWindow::cb_addButtonClicked(GPGManagerWindow* self) { if (self->addUsersWindow) self->addUsersWindow->focusWindow(); else { self->addUsersWindow = new selectUserWindow(tr("Please select the users that you want to add. You can select more than one user."), TRUE); self->addUsersWindow->addCallback((IMEventCallback)self->cb_addWindowClosedCallback, self); self->addUsersWindow->setDestroyCallback((IMEventCallback)self->cb_addWindowClosedCallback, self); self->addUsersWindow->createWindow(); } } void GPGManagerWindow::cb_removeButtonClicked(GPGManagerWindow* self) { GList *selectedRows, *search; GtkTreeIter iter; GtkTreePath *path; IMUserDaemon *user; selectedRows = gtk_tree_selection_get_selected_rows(gtk_tree_view_get_selection(GTK_TREE_VIEW(self->gpgUsersView)), NULL); for (search = selectedRows; search; search = search->next) { path = (GtkTreePath*)search->data; gtk_tree_model_get_iter(GTK_TREE_MODEL(self->gpgUsersStore), &iter, path); gtk_tree_model_get(GTK_TREE_MODEL(self->gpgUsersStore), &iter, 3, &user, -1); gtk_list_store_remove(self->gpgUsersStore, &iter); self->gpgUsers = g_list_remove(self->gpgUsers, user); } } void GPGManagerWindow::cb_selectUserKeyButtonClicked(GPGManagerWindow* self) { } void GPGManagerWindow::cb_selectOwnKeyButtonClicked(GPGManagerWindow* self) { if (!self->gpgSelectWindow) { self->gpgSelectWindow = new GPGKeyWindow(); self->gpgSelectWindow->createWindow(); } else self->gpgSelectWindow->focusWindow(); } void GPGManagerWindow::cb_selectionChanged(GPGManagerWindow* self) { if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(self->gpgUsersView)), NULL, NULL)) { gtk_widget_set_sensitive(self->removeButton, FALSE); gtk_widget_set_sensitive(self->setKeyButton, FALSE); gtk_widget_set_sensitive(self->getInfoButton, FALSE); } else { gtk_widget_set_sensitive(self->removeButton, TRUE); gtk_widget_set_sensitive(self->setKeyButton, TRUE); gtk_widget_set_sensitive(self->getInfoButton, TRUE); } } gboolean GPGManagerWindow::cb_addWindowClosedCallback(userEventType type, gint command, gint lastResult, gpointer info, GPGManagerWindow* self) { GList *search; GtkTreeIter iter; IMUserDaemon *user; // the window was destroyed if (command == WC_DESTROYED) self->addUsersWindow = NULL; // the okay button was clicked else { for (search = (GList*)info; search; search = search->next) { // check whether this entry is already in our list if (!g_list_find(self->gpgUsers, search->data)) { user = (IMUserDaemon*)search->data; self->gpgUsers = g_list_append(self->gpgUsers, user); gtk_list_store_append(self->gpgUsersStore, &iter); gtk_list_store_set(self->gpgUsersStore, &iter, 0, user->info->alias, 1, "12345", 2, FALSE, 3, user, -1); } } } return TRUE; } void GPGManagerWindow::cb_activeToggled(GPGManagerWindow* self, gchar* path_str) { GtkTreePath *path; gboolean active; GtkTreeIter iter; path = gtk_tree_path_new_from_string(path_str); gtk_tree_model_get_iter(GTK_TREE_MODEL(self->gpgUsersStore), &iter, path); gtk_tree_model_get(GTK_TREE_MODEL(self->gpgUsersStore), &iter, 2, &active, -1); active = !active; gtk_list_store_set(self->gpgUsersStore, &iter, 2, active, -1); gtk_tree_path_free(path); } void GPGManagerWindow::cb_getInfoButtonClicked(GPGManagerWindow* self) { GtkTreePath *path; GtkTreeIter iter; IMUserDaemon *user; gtk_tree_view_get_cursor(GTK_TREE_VIEW(self->gpgUsersView), &path, NULL); if (!path) return; gtk_tree_model_get_iter(GTK_TREE_MODEL(self->gpgUsersStore), &iter, path); gtk_tree_model_get(GTK_TREE_MODEL(self->gpgUsersStore), &iter, 3, &user, -1); if (user) user->startCallback(EC_FROMMANAGER, UE_USERINFO, NULL); gtk_tree_path_free(path); } // ----------------------------------------------------------------------------- GPGKeyWindow::GPGKeyWindow() { } GPGKeyWindow::~GPGKeyWindow() { } GtkWidget* GPGKeyWindow::createWindowContent() { GtkWidget *notebook, *vbox1; return gtk_label_new("gpg key window\n"); } void GPGKeyWindow::destroyWindowContent() { } #endif