GTK+ plugin: show errors, activate row starts edit.
authorAndriy Grytsenko <andrej@rep.kiev.ua>
Wed, 2 Nov 2016 14:07:33 +0000 (16:07 +0200)
committerAndriy Grytsenko <andrej@rep.kiev.ua>
Wed, 2 Nov 2016 14:07:33 +0000 (16:07 +0200)
plugins/gtk/edit.c
plugins/gtk/edit.h
plugins/gtk/gtk.c

index 167f530..84dce05 100644 (file)
@@ -174,12 +174,14 @@ static void on_cancel(GtkAction *action, PluginData *data)
 
 static void on_save(GtkAction *action, PluginData *data)
 {
+    GError *error = NULL;
     LXHotkeyGlobal *act = NULL;
     LXHotkeyApp *app = NULL;
     GtkTreeModel *model;
     GtkTreeIter iter;
     LXHotkeyGlobal new_act;
     LXHotkeyApp new_app;
+    gboolean ok = FALSE;
 
     if (data->current_page == data->acts)
     {
@@ -204,8 +206,8 @@ static void on_save(GtkAction *action, PluginData *data)
                 LXHotkeyGlobal rem_act = *act;
 
                 rem_act.accel1 = rem_act.accel2 = NULL;
-                if (!data->cb->set_wm_key(*data->config, &rem_act, NULL))
-                    goto _exit; //FIXME: show error?
+                if (!data->cb->set_wm_key(*data->config, &rem_act, &error))
+                    goto _exit;
             }
             else if (g_strcmp0(act->accel1, new_act.accel1) == 0 &&
                      g_strcmp0(act->accel2, new_act.accel2) == 0)
@@ -213,8 +215,7 @@ static void on_save(GtkAction *action, PluginData *data)
                 goto _exit;
         }
         /* else it was added */
-        data->cb->set_wm_key(*data->config, &new_act, NULL);
-             //FIXME: show if error?
+        ok = data->cb->set_wm_key(*data->config, &new_act, &error);
     }
     else
     {
@@ -241,8 +242,8 @@ static void on_save(GtkAction *action, PluginData *data)
                 LXHotkeyApp rem_app = *app;
 
                 rem_app.accel1 = rem_app.accel2 = NULL;
-                if (!data->cb->set_app_key(*data->config, &rem_app, NULL))
-                    goto _exit; //FIXME: show error?
+                if (!data->cb->set_app_key(*data->config, &rem_app, &error))
+                    goto _exit;
             }
             else if (g_strcmp0(app->accel1, new_app.accel1) == 0 &&
                      g_strcmp0(app->accel2, new_app.accel2) == 0)
@@ -250,18 +251,21 @@ static void on_save(GtkAction *action, PluginData *data)
                 goto _exit;
         }
         /* else it was added */
-        data->cb->set_app_key(*data->config, &new_app, NULL);
-             //FIXME: show if error?
+        ok = data->cb->set_app_key(*data->config, &new_app, &error);
     }
 
-    /* success, update main window */
-    gtk_action_set_sensitive(data->save_action, TRUE);
-    gtk_widget_destroy(GTK_WIDGET(data->edit_window));
-    _main_refresh(data);
-    return;
-
 _exit:
+    if (error)
+    {
+        _show_error(_("Apply error: "), error);
+        g_error_free(error);
+    }
+    /* update main window */
+    if (ok)
+        gtk_action_set_sensitive(data->save_action, TRUE);
     gtk_widget_destroy(GTK_WIDGET(data->edit_window));
+    if (ok)
+        _main_refresh(data);
 }
 
 static void on_add_action(GtkAction *action, PluginData *data)
@@ -321,25 +325,19 @@ static void on_remove(GtkAction *act, PluginData *data)
     gtk_action_set_sensitive(data->edit_apply_button, TRUE);
 }
 
-static void on_edit(GtkAction *act, PluginData *data)
+static void start_edit(GtkTreeModel *model, GtkTreeIter *iter, PluginData *data)
 {
     const LXHotkeyAttr *opt;
     const GList *tmpl_list;
-    GtkTreeModel *model;
-    GtkTreeIter iter;
     GList single = { .prev = NULL, .next = NULL };
 
-    if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(data->edit_tree),
-                                         &model, &iter))
-        /* no item selected */
-        return;
     /* name - only current from selection */
-    gtk_tree_model_get(model, &iter, 2, &opt, -1);
+    gtk_tree_model_get(model, iter, 2, &opt, -1);
     /* values - from template */
-    tmpl_list = get_parent_template_list(model, &iter, data);
+    tmpl_list = get_parent_template_list(model, iter, data);
     if (tmpl_list == data->edit_template) /* it's action */
         return;
-    single.data = (gpointer)find_template_for_option(model, &iter, tmpl_list);
+    single.data = (gpointer)find_template_for_option(model, iter, tmpl_list);
     if (single.data == NULL)
     {
         g_warning("no template found for option '%s'", opt->name);
@@ -353,6 +351,18 @@ static void on_edit(GtkAction *act, PluginData *data)
     gtk_widget_grab_focus(data->edit_frame);
 }
 
+static void on_edit(GtkAction *act, PluginData *data)
+{
+    GtkTreeModel *model;
+    GtkTreeIter iter;
+
+    if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(data->edit_tree),
+                                         &model, &iter))
+        /* no item selected */
+        return;
+    start_edit(model, &iter, data);
+}
+
 static void on_add_suboption(GtkAction *act, PluginData *data)
 {
     const LXHotkeyAttr *opt, *tmpl;
@@ -598,6 +608,19 @@ static void on_exec_changed(GtkEntry *exec, PluginData *data)
         gtk_action_set_sensitive(data->edit_apply_button, TRUE);
 }
 
+static void on_row_activated(GtkTreeView *view, GtkTreePath *path,
+                             GtkTreeViewColumn *column, PluginData *data)
+{
+    GtkTreeModel *model;
+    GtkTreeIter iter;
+
+    model = gtk_tree_view_get_model(view);
+    if (!gtk_tree_model_get_iter(model, &iter, path))
+        /* invalid path */
+        return;
+    start_edit(model, &iter, data);
+}
+
 static void on_selection_changed(GtkTreeSelection *selection, PluginData *data)
 {
     if (edit_is_active(data))
@@ -1068,7 +1091,7 @@ void _edit_action(PluginData *data, GError **error)
                                                 gtk_cell_renderer_text_new(),
                                                 "text", 1, NULL);
     gtk_tree_view_set_headers_visible(data->edit_tree, FALSE);
-    //FIXME: connect "row-activated" for Edit
+    g_signal_connect(data->edit_tree, "row-activated", G_CALLBACK(on_row_activated), data);
 
     /* frame with fields for editing, hidden for now */
     data->edit_frame = gtk_frame_new(NULL);
index ad94118..e80479d 100644 (file)
@@ -69,4 +69,6 @@ void _edit_action(PluginData *data, GError **error);
 
 void _edit_cleanup(PluginData *data);
 
+void _show_error(const char *prefix, GError *error);
+
 #endif /* _EDIT_H_ */
index e032426..5a28cee 100644 (file)
@@ -72,8 +72,7 @@ static void on_reload(GtkAction *act, PluginData *data)
     *data->config = data->cb->load(*data->config, &error);
     if (error)
     {
-        g_warning("error loading config: %s",error->message);
-        //FIXME: show errors instead
+        _show_error(_("Error loading config: "), error);
         g_error_free(error);
     }
     _main_refresh(data);
@@ -82,9 +81,15 @@ static void on_reload(GtkAction *act, PluginData *data)
 
 static void on_save(GtkAction *act, PluginData *data)
 {
-    if (data->cb->save(*data->config, NULL))
+    GError *error = NULL;
+
+    if (data->cb->save(*data->config, &error))
         gtk_action_set_sensitive(data->save_action, FALSE);
-    //FIXME: else show errors
+    else
+    {
+        _show_error(_("Error saving config: "), error);
+        g_error_free(error);
+    }
 }
 
 static void on_quit(GtkAction *act, PluginData *data)
@@ -102,6 +107,7 @@ static void on_del(GtkAction *act, PluginData *data)
 {
     LXHotkeyGlobal *gl = NULL;
     LXHotkeyApp *app = NULL;
+    GError *error = NULL;
     GtkTreeModel *model;
     GtkTreeIter iter;
 
@@ -117,12 +123,16 @@ static void on_del(GtkAction *act, PluginData *data)
             LXHotkeyGlobal rem_act = *gl;
 
             rem_act.accel1 = rem_act.accel2 = NULL;
-            if (data->cb->set_wm_key(*data->config, &rem_act, NULL))
+            if (data->cb->set_wm_key(*data->config, &rem_act, &error))
             {
                 gtk_action_set_sensitive(data->save_action, TRUE);
                 _main_refresh(data);
             }
-            //FIXME: handle errors
+            else
+            {
+                _show_error(_("Cannot delete keybinding: "), error);
+                g_error_free(error);
+            }
         }
     }
     else
@@ -136,12 +146,16 @@ static void on_del(GtkAction *act, PluginData *data)
             LXHotkeyApp rem_app = *app;
 
             rem_app.accel1 = rem_app.accel2 = NULL;
-            if (data->cb->set_app_key(*data->config, &rem_app, NULL))
+            if (data->cb->set_app_key(*data->config, &rem_app, &error))
             {
                 gtk_action_set_sensitive(data->save_action, TRUE);
                 _main_refresh(data);
             }
-            //FIXME: handle errors
+            else
+            {
+                _show_error(_("Cannot delete keybinding: "), error);
+                g_error_free(error);
+            }
         }
     }
 }
@@ -242,6 +256,12 @@ static void on_selection_changed(GtkTreeSelection *selection, PluginData *data)
     gtk_action_set_sensitive(data->edit_action, has_selection);
 }
 
+static void on_row_activated(GtkTreeView *view, GtkTreePath *path,
+                             GtkTreeViewColumn *column, PluginData *data)
+{
+    _edit_action(data, NULL);
+}
+
 static void set_actions_list(PluginData *data)
 {
     GtkListStore *model = gtk_list_store_new(5, G_TYPE_STRING, G_TYPE_STRING,
@@ -266,7 +286,14 @@ static void set_actions_list(PluginData *data)
         {
             opt = attr->subopts->data;
             if (opt->values)
-                _val = val = g_strdup_printf("%s:%s", opt->name, (char *)opt->values->data);
+            {
+                if (attr->subopts->next)
+                    _val = val = g_strdup_printf("%s:%s, ...", opt->name, (char *)opt->values->data);
+                else
+                    _val = val = g_strdup_printf("%s:%s", opt->name, (char *)opt->values->data);
+            }
+            else if (attr->subopts->next)
+                _val = val = g_strdup_printf("%s, ...", opt->name);
             else
                 val = opt->name;
         }
@@ -409,7 +436,7 @@ static void module_gtk_run(const gchar *wm, const LXHotkeyPluginInit *cb,
                                                     gtk_cell_renderer_text_new(),
                                                     "text", 3, NULL);
         set_actions_list(&data);
-        //FIXME: connect "row-activated" for Edit
+        g_signal_connect(data.acts, "row-activated", G_CALLBACK(on_row_activated), &data);
         g_signal_connect(gtk_tree_view_get_selection(data.acts), "changed",
                          G_CALLBACK(on_selection_changed), &data);
         gtk_notebook_append_page(data.notebook, GTK_WIDGET(data.acts),
@@ -429,7 +456,7 @@ static void module_gtk_run(const gchar *wm, const LXHotkeyPluginInit *cb,
                                                     gtk_cell_renderer_text_new(),
                                                     "text", 2, NULL);
         set_apps_list(&data);
-        //FIXME: connect "row-activated" for Edit
+        g_signal_connect(data.apps, "row-activated", G_CALLBACK(on_row_activated), &data);
         g_signal_connect(gtk_tree_view_get_selection(data.apps), "changed",
                          G_CALLBACK(on_selection_changed), &data);
         gtk_notebook_append_page(data.notebook, GTK_WIDGET(data.apps),
@@ -444,8 +471,24 @@ static void module_gtk_run(const gchar *wm, const LXHotkeyPluginInit *cb,
     _edit_cleanup(&data);
 }
 
+void _show_error(const char *prefix, GError *error)
+{
+    GtkWidget *dlg;
+
+    if (error && error->message)
+    {
+        dlg = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_ERROR,
+                                     GTK_BUTTONS_OK, "%s%s", prefix, error->message);
+        gtk_window_set_title(GTK_WINDOW(dlg), _("Error"));
+        gtk_window_set_keep_above(GTK_WINDOW(dlg), TRUE);
+        gtk_dialog_run(GTK_DIALOG(dlg));
+        gtk_widget_destroy(dlg);
+    }
+}
+
 static void module_gtk_alert(GError *error)
 {
+    _show_error("", error);
 }
 
 static void module_gtk_init(int argc, char **argv)