Fixed adding, removal, saving
This commit is contained in:
parent
d34c09499f
commit
5804aa3f77
|
@ -23,7 +23,19 @@
|
|||
#include "../debug.h"
|
||||
#include "../gm-app.h"
|
||||
#include "../gm-support.h"
|
||||
#include "../gm-app-view.h"
|
||||
#include "../widgets/gm-app-view.h"
|
||||
#include "../widgets/gm-text-scroller.h"
|
||||
|
||||
#define SCRIPT_TEMPLATE \
|
||||
_("=begin\n" \
|
||||
"\t<title>\n" \
|
||||
"\t<copyright>\n" \
|
||||
"\n\tRegister functions in the register_functions method defined below.\n" \
|
||||
"\tRegister functions with " \
|
||||
"$scripts.register(<name>, <description>[, <alias>])\n=end" \
|
||||
"\n\ndef register_functions\n" \
|
||||
"\t$scripts.register(\"myscript\", \"use /myscript\")\nend\n\n" \
|
||||
"def myscript(argstr)\n\t# Insert code here\nend\n")
|
||||
|
||||
void on_gm_scripts_dialog_script_added(GmScripts *scripts, GmScript *script,
|
||||
gpointer user_data);
|
||||
|
@ -74,6 +86,7 @@ typedef struct _GmScriptsDialog {
|
|||
GtkWidget *tree_view_scripts;
|
||||
GtkWidget *tree_view_files;
|
||||
GtkWidget *button_delete;
|
||||
GmTextScroller *text_scroller;
|
||||
|
||||
SelectionInfo info;
|
||||
} GmScriptsDialog;
|
||||
|
@ -134,17 +147,22 @@ gm_scripts_dialog_editor_save(gchar *filename) {
|
|||
gm_scripts_dialog_set_status(msg);
|
||||
g_free(msg);
|
||||
|
||||
if (scripts_dialog->current_edit &&
|
||||
filename != scripts_dialog->current_edit) {
|
||||
if (scripts_dialog->current_edit != filename) {
|
||||
g_free(scripts_dialog->current_edit);
|
||||
scripts_dialog->current_edit = g_strdup(filename);
|
||||
}
|
||||
|
||||
gtk_text_buffer_set_modified(scripts_dialog->text_buffer_editor,
|
||||
FALSE);
|
||||
|
||||
fclose(f);
|
||||
return TRUE;
|
||||
} else {
|
||||
msg = g_strconcat(_("Saving failed: "), strerror(errno), NULL);
|
||||
text = g_filename_to_utf8(filename, -1, NULL, NULL, NULL);
|
||||
msg = g_strconcat(_("Saving failed: "), strerror(errno), " (",
|
||||
text, ")", NULL);
|
||||
gm_error_dialog(msg, GTK_WINDOW(scripts_dialog->dialog));
|
||||
g_free(text);
|
||||
g_free(msg);
|
||||
|
||||
return FALSE;
|
||||
|
@ -158,10 +176,10 @@ gm_scripts_dialog_editor_load(SelectionInfo *info) {
|
|||
GtkTextIter end;
|
||||
gchar *lline, *msg;
|
||||
|
||||
gtk_text_buffer_set_text(scripts_dialog->text_buffer_editor, "", 0);
|
||||
gtk_text_buffer_get_end_iter(scripts_dialog->text_buffer_editor, &end);
|
||||
|
||||
if (info) {
|
||||
gtk_text_buffer_set_text(scripts_dialog->text_buffer_editor, "", 0);
|
||||
gtk_text_buffer_get_end_iter(scripts_dialog->text_buffer_editor, &end);
|
||||
|
||||
f = fopen(info->filename, "r");
|
||||
|
||||
if (f) {
|
||||
|
@ -186,6 +204,9 @@ gm_scripts_dialog_editor_load(SelectionInfo *info) {
|
|||
|
||||
g_free(scripts_dialog->current_edit);
|
||||
scripts_dialog->current_edit = g_strdup(info->filename);
|
||||
|
||||
g_free(scripts_dialog->current_edit);
|
||||
scripts_dialog->current_edit = g_strdup(info->filename);
|
||||
|
||||
msg = g_strconcat(_("Loaded "), info->filename, NULL);
|
||||
gm_scripts_dialog_set_status(msg);
|
||||
|
@ -195,6 +216,8 @@ gm_scripts_dialog_editor_load(SelectionInfo *info) {
|
|||
scripts_dialog->current_edit, strerror(errno));
|
||||
}
|
||||
} else {
|
||||
gtk_text_buffer_set_text(scripts_dialog->text_buffer_editor,
|
||||
SCRIPT_TEMPLATE, -1);
|
||||
g_free(scripts_dialog->current_edit);
|
||||
scripts_dialog->current_edit = NULL;
|
||||
|
||||
|
@ -257,23 +280,45 @@ gm_scripts_dialog_tree_update_script_item(GmScript *script, GtkTreeIter *iter) {
|
|||
(GmScriptFunction *)(list->data));
|
||||
}
|
||||
}
|
||||
|
||||
gboolean
|
||||
gm_scripts_dialog_model_find_script(GtkTreeModel *model,
|
||||
GmScript *script, GtkTreeIter *item, GtkTreeIter *parent) {
|
||||
GmScript *obj;
|
||||
|
||||
if (gtk_tree_model_iter_children(model, item, parent)) {
|
||||
do {
|
||||
gtk_tree_model_get(model, item, SCRIPTS_OBJECT,
|
||||
&obj, -1);
|
||||
|
||||
if (obj == script) {
|
||||
return TRUE;
|
||||
}
|
||||
} while (gtk_tree_model_iter_next(model, item));
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
void
|
||||
gm_scripts_dialog_tree_update_script(GmScript *script) {
|
||||
GtkTreeModel *model = gtk_tree_view_get_model(
|
||||
GTK_TREE_VIEW(scripts_dialog->tree_view_scripts));
|
||||
GtkTreeIter item;
|
||||
GmScript *obj;
|
||||
|
||||
if (gtk_tree_model_iter_children(model, &item, NULL)) {
|
||||
do {
|
||||
gtk_tree_model_get(model, &item, SCRIPTS_OBJECT,
|
||||
&obj, -1);
|
||||
|
||||
if (obj == script) {
|
||||
gm_scripts_dialog_tree_update_script_item(script, &item);
|
||||
break;
|
||||
}
|
||||
} while (gtk_tree_model_iter_next(model, &item));
|
||||
if (gm_scripts_dialog_model_find_script(model, script, &item, NULL)) {
|
||||
gm_scripts_dialog_tree_update_script_item(script, &item);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
gm_scripts_dialog_tree_remove_script(GmScript *script) {
|
||||
GtkTreeModel *model = gtk_tree_view_get_model(
|
||||
GTK_TREE_VIEW(scripts_dialog->tree_view_scripts));
|
||||
GtkTreeIter item;
|
||||
|
||||
if (gm_scripts_dialog_model_find_script(model, script, &item, NULL)) {
|
||||
gtk_tree_store_remove(GTK_TREE_STORE(model), &item);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -336,8 +381,32 @@ gm_scripts_dialog_init_tree() {
|
|||
gm_scripts_dialog_fill_tree();
|
||||
}
|
||||
|
||||
void
|
||||
gm_scripts_dialog_remove_file(GmScript *script) {
|
||||
GtkTreeView *view = GTK_TREE_VIEW(scripts_dialog->tree_view_files);
|
||||
GtkTreeStore *model = GTK_TREE_STORE(gtk_tree_view_get_model(view));
|
||||
const gchar *sharedir = PACKAGE_DATA_DIR "/" PACKAGE "/scripts";
|
||||
GtkTreeIter parent;
|
||||
GtkTreeIter item;
|
||||
gchar *name = g_filename_to_utf8(script->filename,
|
||||
strlen(script->filename), NULL, NULL, NULL);
|
||||
|
||||
if (strncmp(sharedir, name, strlen(sharedir)) == 0) {
|
||||
gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(model), &parent, "0");
|
||||
} else {
|
||||
gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(model), &parent, "1");
|
||||
}
|
||||
|
||||
g_free(name);
|
||||
|
||||
if (gm_scripts_dialog_model_find_script(GTK_TREE_MODEL(model), script,
|
||||
&item, &parent)) {
|
||||
gtk_tree_store_remove(model, &item);
|
||||
}
|
||||
}
|
||||
|
||||
gboolean
|
||||
gm_scripts_dialog_add_file(gchar *filename) {
|
||||
gm_scripts_dialog_add_file(GmScript *script) {
|
||||
GtkTreeView *view = GTK_TREE_VIEW(scripts_dialog->tree_view_files);
|
||||
GtkTreeStore *model = GTK_TREE_STORE(gtk_tree_view_get_model(view));
|
||||
GtkTreeIter share, home, parent, item;
|
||||
|
@ -349,7 +418,7 @@ gm_scripts_dialog_add_file(gchar *filename) {
|
|||
gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(model), &share, "0");
|
||||
gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(model), &home, "1");
|
||||
|
||||
name = g_filename_to_utf8(filename, strlen(filename), NULL,
|
||||
name = g_filename_to_utf8(script->filename, strlen(script->filename), NULL,
|
||||
NULL, NULL);
|
||||
|
||||
if (strncmp(sharedir, name, strlen(sharedir)) == 0) {
|
||||
|
@ -365,7 +434,7 @@ gm_scripts_dialog_add_file(gchar *filename) {
|
|||
|
||||
gtk_tree_store_append(model, &item, &parent);
|
||||
gtk_tree_store_set(model, &item, SCRIPTS_NAME, g_strrstr(name, "/") + 1,
|
||||
-1);
|
||||
SCRIPTS_OBJECT, script, -1);
|
||||
|
||||
g_free(name);
|
||||
g_free(sharedir);
|
||||
|
@ -378,14 +447,10 @@ void
|
|||
gm_scripts_dialog_fill_files() {
|
||||
GtkTreeView *view = GTK_TREE_VIEW(scripts_dialog->tree_view_files);
|
||||
GtkTreeStore *model = GTK_TREE_STORE(gtk_tree_view_get_model(view));
|
||||
GtkTreeIter item, share, home, parent;
|
||||
GtkTreeIter share, home;
|
||||
GtkTreePath *path;
|
||||
gchar *sharedir;
|
||||
gchar *name;
|
||||
GList *scripts;
|
||||
GmScript *script;
|
||||
|
||||
sharedir = g_strdup(PACKAGE_DATA_DIR "/" PACKAGE "/scripts");
|
||||
|
||||
gtk_tree_store_clear(model);
|
||||
|
||||
|
@ -398,21 +463,7 @@ gm_scripts_dialog_fill_files() {
|
|||
for (scripts = gm_scripts_scripts(gm_app_scripts(gm_app_instance()));
|
||||
scripts; scripts = scripts->next) {
|
||||
script = (GmScript *)(scripts->data);
|
||||
|
||||
name = g_filename_to_utf8(script->filename, strlen(script->filename),
|
||||
NULL, NULL, NULL);
|
||||
|
||||
if (strncmp(sharedir, name, strlen(sharedir)) == 0) {
|
||||
parent = share;
|
||||
} else {
|
||||
parent = home;
|
||||
}
|
||||
|
||||
gtk_tree_store_append(model, &item, &parent);
|
||||
gtk_tree_store_set(model, &item, SCRIPTS_NAME,
|
||||
g_strrstr(name, "/") + 1, -1);
|
||||
|
||||
g_free(name);
|
||||
gm_scripts_dialog_add_file(script);
|
||||
}
|
||||
|
||||
path = gtk_tree_model_get_path(GTK_TREE_MODEL(model), &home);
|
||||
|
@ -422,8 +473,6 @@ gm_scripts_dialog_fill_files() {
|
|||
path = gtk_tree_model_get_path(GTK_TREE_MODEL(model), &share);
|
||||
gtk_tree_view_expand_to_path(view, path);
|
||||
gtk_tree_path_free(path);
|
||||
|
||||
g_free(sharedir);
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -474,15 +523,23 @@ gm_scripts_dialog_init_editor() {
|
|||
gtk_widget_modify_font(view, f);
|
||||
|
||||
gtk_source_view_set_insert_spaces_instead_of_tabs(GTK_SOURCE_VIEW(view),
|
||||
TRUE);
|
||||
FALSE);
|
||||
gtk_source_view_set_auto_indent(GTK_SOURCE_VIEW(view), TRUE);
|
||||
gtk_source_view_set_show_line_numbers(GTK_SOURCE_VIEW(view), TRUE);
|
||||
gtk_source_view_set_smart_home_end(GTK_SOURCE_VIEW(view), TRUE);
|
||||
gtk_source_view_set_tabs_width(GTK_SOURCE_VIEW(view), 4);
|
||||
|
||||
gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view), GTK_WRAP_WORD_CHAR);
|
||||
gtk_text_view_set_left_margin(GTK_TEXT_VIEW(view), 6);
|
||||
gtk_text_view_set_right_margin(GTK_TEXT_VIEW(view), 6);
|
||||
|
||||
gtk_source_buffer_set_highlight(GTK_SOURCE_BUFFER(
|
||||
scripts_dialog->text_buffer_editor), TRUE);
|
||||
|
||||
// Load default template
|
||||
gtk_text_buffer_set_text(scripts_dialog->text_buffer_editor,
|
||||
SCRIPT_TEMPLATE, -1);
|
||||
|
||||
gtk_widget_show(view);
|
||||
|
||||
return view;
|
||||
|
@ -540,12 +597,18 @@ gm_scripts_dialog_fini() {
|
|||
g_free(scripts_dialog);
|
||||
}
|
||||
|
||||
gboolean
|
||||
gm_scripts_scroll_end_idle(gpointer user_data) {
|
||||
//gm_text_scroller_scroll_end(scripts_dialog->text_scroller);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
void
|
||||
gm_scripts_dialog_run(GmAppView *view) {
|
||||
GtkWidget *editor;
|
||||
|
||||
if (scripts_dialog->dialog != NULL) {
|
||||
gtk_widget_show(scripts_dialog->dialog);
|
||||
gtk_window_present(GTK_WINDOW(scripts_dialog->dialog));
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -565,6 +628,11 @@ gm_scripts_dialog_run(GmAppView *view) {
|
|||
scripts_dialog->button_delete = glade_xml_get_widget(scripts_dialog->xml,
|
||||
"tool_button_delete");
|
||||
|
||||
// Create new text scroller, this object will take care of itself and will
|
||||
// destroy itself when the view dies, neat!
|
||||
scripts_dialog->text_scroller = gm_text_scroller_new(GTK_TEXT_VIEW(
|
||||
scripts_dialog->text_view_console));
|
||||
|
||||
gm_scripts_dialog_init_console();
|
||||
gm_scripts_dialog_init_tree();
|
||||
gm_scripts_dialog_init_files();
|
||||
|
@ -597,6 +665,8 @@ gm_scripts_dialog_run(GmAppView *view) {
|
|||
gm_scripts_dialog_set_status(_("New <untitled>"));
|
||||
|
||||
g_object_unref(scripts_dialog->xml);
|
||||
|
||||
g_idle_add((GSourceFunc)(gm_scripts_scroll_end_idle), NULL);
|
||||
}
|
||||
|
||||
|
||||
|
@ -647,7 +717,7 @@ gm_scripts_dialog_selection_info(SelectionInfo *info) {
|
|||
GtkTreeView *view = GTK_TREE_VIEW(scripts_dialog->tree_view_files);
|
||||
GtkTreeModel *model = gtk_tree_view_get_model(view);
|
||||
GtkTreeSelection *selection = gtk_tree_view_get_selection(view);
|
||||
gchar *parentName, *name;
|
||||
gchar *parentName, *name, *filename;
|
||||
|
||||
info->view = view;
|
||||
info->model = model;
|
||||
|
@ -663,15 +733,19 @@ gm_scripts_dialog_selection_info(SelectionInfo *info) {
|
|||
&name, -1);
|
||||
|
||||
if (strcmp(parentName, _("Share")) == 0) {
|
||||
info->filename = g_strconcat(
|
||||
filename = g_strconcat(
|
||||
PACKAGE_DATA_DIR "/" PACKAGE "/scripts/", name, NULL);
|
||||
} else {
|
||||
info->filename = g_strconcat(gm_app_path(gm_app_instance()),
|
||||
filename = g_strconcat(gm_app_path(gm_app_instance()),
|
||||
"/scripts/", name, NULL);
|
||||
}
|
||||
|
||||
info->filename = g_filename_from_utf8(filename, -1,
|
||||
NULL, NULL, NULL);
|
||||
|
||||
g_free(name);
|
||||
g_free(parentName);
|
||||
g_free(filename);
|
||||
info->can_save = gm_scripts_dialog_can_write(info->filename);
|
||||
} else {
|
||||
info->filename = NULL;
|
||||
|
@ -724,15 +798,18 @@ void
|
|||
on_text_buffer_editor_modified_changed(GtkTextBuffer *buffer,
|
||||
gpointer user_data) {
|
||||
gchar *msg;
|
||||
|
||||
if (scripts_dialog->current_edit) {
|
||||
msg = g_strconcat(_("Changed "), scripts_dialog->current_edit, NULL);
|
||||
} else {
|
||||
msg = g_strdup(_("Changed <untitled>"));
|
||||
gboolean modified = gtk_text_buffer_get_modified(buffer);
|
||||
|
||||
if (modified) {
|
||||
if (scripts_dialog->current_edit) {
|
||||
msg = g_strconcat(_("Changed "), scripts_dialog->current_edit, NULL);
|
||||
} else {
|
||||
msg = g_strdup(_("Changed <untitled>"));
|
||||
}
|
||||
|
||||
gm_scripts_dialog_set_status(msg);
|
||||
g_free(msg);
|
||||
}
|
||||
|
||||
gm_scripts_dialog_set_status(msg);
|
||||
g_free(msg);
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -744,7 +821,6 @@ void
|
|||
on_tool_button_save_clicked(GtkToolButton *button, gpointer user_data) {
|
||||
if (scripts_dialog->current_edit) {
|
||||
gm_scripts_dialog_editor_save(scripts_dialog->current_edit);
|
||||
gtk_text_buffer_set_modified(scripts_dialog->text_buffer_editor, FALSE);
|
||||
} else {
|
||||
on_tool_button_save_as_clicked(button, user_data);
|
||||
}
|
||||
|
@ -776,11 +852,7 @@ on_tool_button_save_as_clicked(GtkToolButton *button, gpointer user_data) {
|
|||
if (gtk_dialog_run(GTK_DIALOG(dlg)) == GTK_RESPONSE_ACCEPT) {
|
||||
filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dlg));
|
||||
|
||||
if (gm_scripts_dialog_editor_save(filename)) {
|
||||
gtk_text_buffer_set_modified(scripts_dialog->text_buffer_editor,
|
||||
FALSE);
|
||||
}
|
||||
|
||||
gm_scripts_dialog_editor_save(filename);
|
||||
g_free(filename);
|
||||
}
|
||||
|
||||
|
@ -800,14 +872,12 @@ on_tool_button_delete_clicked(GtkToolButton *button, gpointer user_data) {
|
|||
}
|
||||
}
|
||||
|
||||
// Callbacks
|
||||
|
||||
void
|
||||
on_gm_scripts_dialog_script_added(GmScripts *scripts, GmScript *script,
|
||||
gpointer user_data) {
|
||||
if (scripts_dialog->dialog != NULL) {
|
||||
gm_scripts_dialog_tree_add_script(script);
|
||||
gm_scripts_dialog_add_file(script->filename);
|
||||
gm_scripts_dialog_add_file(script);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -823,6 +893,12 @@ void
|
|||
on_gm_scripts_dialog_script_removed(GmScripts *scripts, GmScript *script,
|
||||
gpointer user_data) {
|
||||
if (scripts_dialog->dialog != NULL) {
|
||||
gm_scripts_dialog_tree_remove_script(script);
|
||||
gm_scripts_dialog_remove_file(script);
|
||||
|
||||
if (strcmp(script->filename, scripts_dialog->current_edit) == 0) {
|
||||
gm_scripts_dialog_editor_load(NULL);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue