2010-03-21 16:33:39 +02:00
|
|
|
/* -*- Mode: C ; c-basic-offset: 2 -*- */
|
|
|
|
/*
|
|
|
|
* LADI Session Handler (ladish)
|
|
|
|
*
|
2012-11-19 08:06:45 +02:00
|
|
|
* Copyright (C) 2010,2012 Nedko Arnaudov <nedko@arnaudov.name>
|
2010-03-21 16:33:39 +02:00
|
|
|
*
|
|
|
|
**************************************************************************
|
|
|
|
* This file contains implementation of the "create room" dialog
|
|
|
|
**************************************************************************
|
|
|
|
*
|
|
|
|
* LADI Session Handler is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* LADI Session Handler 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 LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
* or write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gdk/gdkkeysyms.h>
|
|
|
|
|
|
|
|
#include "create_room_dialog.h"
|
|
|
|
#include "gtk_builder.h"
|
|
|
|
#include "../proxies/control_proxy.h"
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
COL_NAME = 0,
|
|
|
|
NUM_COLS
|
|
|
|
};
|
|
|
|
|
|
|
|
static GtkWidget * g_dialog;
|
|
|
|
static GtkListStore * g_liststore;
|
|
|
|
static GtkTreeView * g_treeview;
|
|
|
|
|
2012-12-02 23:50:05 +02:00
|
|
|
bool on_key_press_event(GtkWidget * UNUSED(widget_ptr), GdkEventKey * event_ptr, gpointer UNUSED(user_data))
|
2010-03-21 16:33:39 +02:00
|
|
|
{
|
|
|
|
if (event_ptr->type == GDK_KEY_PRESS &&
|
|
|
|
(event_ptr->keyval == GDK_Return ||
|
|
|
|
event_ptr->keyval == GDK_KP_Enter))
|
|
|
|
{
|
|
|
|
gtk_dialog_response(GTK_DIALOG(g_dialog), 2);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-12-02 23:50:05 +02:00
|
|
|
static
|
|
|
|
void
|
|
|
|
on_row_activated(
|
|
|
|
GtkTreeView * UNUSED(treeview),
|
|
|
|
GtkTreePath * UNUSED(path),
|
|
|
|
GtkTreeViewColumn * UNUSED(col),
|
|
|
|
gpointer UNUSED(userdata))
|
2010-04-03 04:42:46 +03:00
|
|
|
{
|
|
|
|
gtk_dialog_response(GTK_DIALOG(g_dialog), 2);
|
|
|
|
}
|
|
|
|
|
2010-03-21 16:33:39 +02:00
|
|
|
void create_room_dialog_init(void)
|
|
|
|
{
|
|
|
|
GtkTreeViewColumn * col;
|
|
|
|
GtkCellRenderer * renderer;
|
|
|
|
|
|
|
|
g_dialog = get_gtk_builder_widget("create_room_dialog");
|
|
|
|
g_treeview = GTK_TREE_VIEW(get_gtk_builder_widget("room_templates_treeview"));
|
|
|
|
|
|
|
|
gtk_tree_view_set_headers_visible(g_treeview, FALSE);
|
|
|
|
|
|
|
|
col = gtk_tree_view_column_new();
|
2010-12-11 18:45:06 +02:00
|
|
|
gtk_tree_view_column_set_title(col, _("Name"));
|
2010-03-21 16:33:39 +02:00
|
|
|
gtk_tree_view_append_column(g_treeview, col);
|
|
|
|
|
|
|
|
renderer = gtk_cell_renderer_text_new();
|
|
|
|
gtk_tree_view_column_pack_start(col, renderer, TRUE);
|
|
|
|
gtk_tree_view_column_add_attribute(col, renderer, "text", COL_NAME);
|
|
|
|
|
|
|
|
g_liststore = gtk_list_store_new(NUM_COLS, G_TYPE_STRING);
|
|
|
|
gtk_tree_view_set_model(g_treeview, GTK_TREE_MODEL(g_liststore));
|
|
|
|
|
|
|
|
g_signal_connect(G_OBJECT(g_treeview), "key-press-event", G_CALLBACK(on_key_press_event), NULL);
|
2010-04-03 04:42:46 +03:00
|
|
|
g_signal_connect(G_OBJECT(g_treeview), "row-activated", G_CALLBACK(on_row_activated), NULL);
|
2010-03-21 16:33:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void create_room_dialog_uninit(void)
|
|
|
|
{
|
|
|
|
g_object_unref(g_liststore);
|
|
|
|
}
|
|
|
|
|
2012-12-02 23:50:05 +02:00
|
|
|
static void fill_templates_callback(void * UNUSED(context), const char * template_name)
|
2010-03-21 16:33:39 +02:00
|
|
|
{
|
|
|
|
GtkTreeIter iter;
|
|
|
|
|
|
|
|
gtk_list_store_append(g_liststore, &iter);
|
|
|
|
gtk_list_store_set(g_liststore, &iter, COL_NAME, template_name, -1);
|
|
|
|
|
|
|
|
/* select the first item */
|
|
|
|
if (gtk_tree_model_iter_n_children(GTK_TREE_MODEL(g_liststore), NULL) == 1)
|
|
|
|
{
|
|
|
|
gtk_tree_selection_select_iter(gtk_tree_view_get_selection(g_treeview), &iter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool fill_templates(void)
|
|
|
|
{
|
|
|
|
gtk_list_store_clear(g_liststore);
|
|
|
|
return control_proxy_get_room_template_list(fill_templates_callback, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool create_room_dialog_run(char ** name_ptr_ptr, char ** template_ptr_ptr)
|
|
|
|
{
|
|
|
|
guint result;
|
|
|
|
bool ok;
|
|
|
|
GtkWidget * dialog = get_gtk_builder_widget("create_room_dialog");
|
|
|
|
GtkEntry * entry = GTK_ENTRY(get_gtk_builder_widget("create_room_name_entry"));
|
|
|
|
GtkTreeSelection * selection;
|
|
|
|
GtkTreeIter iter;
|
|
|
|
char * template;
|
|
|
|
char * name;
|
|
|
|
|
2010-04-05 05:09:20 +03:00
|
|
|
/* text-select old name so it is cleared if user starts typing
|
|
|
|
* but keep it so user can use slighly modified name */
|
|
|
|
gtk_editable_select_region(GTK_EDITABLE(entry), 0, -1);
|
|
|
|
gtk_window_set_focus(GTK_WINDOW(dialog), GTK_WIDGET(entry));
|
|
|
|
|
2010-03-21 16:33:39 +02:00
|
|
|
if (!fill_templates())
|
|
|
|
{
|
|
|
|
log_error("Cannot fetch room template list");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
gtk_widget_show(dialog);
|
|
|
|
|
|
|
|
again:
|
|
|
|
result = gtk_dialog_run(GTK_DIALOG(dialog));
|
|
|
|
ok = result == 2;
|
|
|
|
if (ok)
|
|
|
|
{
|
|
|
|
selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(g_treeview));
|
|
|
|
if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
|
|
|
|
{
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
2012-11-19 08:06:45 +02:00
|
|
|
name = (char *)gtk_entry_get_text(entry);
|
|
|
|
if (strlen(name) == 0)
|
|
|
|
{
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
2010-03-21 16:33:39 +02:00
|
|
|
gtk_tree_model_get(GTK_TREE_MODEL(g_liststore), &iter, COL_NAME, &template, -1);
|
|
|
|
template = strdup(template);
|
|
|
|
if (template == NULL)
|
|
|
|
{
|
|
|
|
log_error("strdup failed for template name (create room dialog)");
|
|
|
|
ok = false;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
name = strdup(name);
|
|
|
|
if (name == NULL)
|
|
|
|
{
|
|
|
|
log_error("strdup failed for room name (create room dialog)");
|
|
|
|
free(template);
|
|
|
|
ok = false;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
*name_ptr_ptr = name;
|
|
|
|
*template_ptr_ptr = template;
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
gtk_widget_hide(dialog);
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|