summaryrefslogtreecommitdiff
path: root/thunar/thunar-menu-util.c
blob: 5caacc4160ab5432698c6a33cf251530882f108f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* vi:set et ai sw=2 sts=2 ts=2: */
/*-
 * Copyright (c) 2017 Andre Miranda <andreldm@xfce.org>
 *
 * This program 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.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include <thunar/thunar-menu-util.h>

#include <thunarx/thunarx.h>



static void
extension_action_callback (GtkAction *action,
                           gpointer callback_data)
{
  thunarx_menu_item_activate (THUNARX_MENU_ITEM (callback_data));
}



static GtkAction *
action_from_menu_item (GObject *item)
{
  gchar *name, *label, *tooltip, *icon_str;
  gboolean  sensitive, priority;
  GtkAction *action;

  g_return_val_if_fail (THUNARX_IS_MENU_ITEM (item), NULL);

  g_object_get (G_OBJECT (item),
                "name", &name,
                "label", &label,
                "tooltip", &tooltip,
                "icon", &icon_str,
                "sensitive", &sensitive,
                "priority", &priority,
                NULL);

G_GNUC_BEGIN_IGNORE_DEPRECATIONS
  action = gtk_action_new (name, label, tooltip, NULL);

  if (icon_str != NULL)
    {
      GIcon *icon = g_icon_new_for_string (icon_str, NULL);

      if (icon)
        {
          gtk_action_set_gicon (action, icon);
          g_object_unref (icon);
        }
    }

  gtk_action_set_sensitive (action, sensitive);
G_GNUC_END_IGNORE_DEPRECATIONS
  g_object_set (action, "is-important", priority, NULL);

  g_signal_connect_data (action, "activate",
                         G_CALLBACK (extension_action_callback),
                         g_object_ref (item),
                         (GClosureNotify) (void (*)(void)) g_object_unref, 0);

  g_free (name);
  g_free (label);
  g_free (tooltip);
  g_free (icon_str);

  return action;
}



void
thunar_menu_util_add_items_to_ui_manager (GtkUIManager   *ui_manager,
                                          GtkActionGroup *action_group,
                                          gint            merge_id,
                                          const gchar    *path,
                                          GList          *items)
{
  GList           *lp;
  GtkAction       *action;
  ThunarxMenu     *menu;
  char            *subpath;
  char            *action_path;
  GList           *children;

G_GNUC_BEGIN_IGNORE_DEPRECATIONS
  /* add the menu items to the UI manager */
  for (lp = items; lp != NULL; lp = lp->next)
    {
      action = action_from_menu_item (G_OBJECT (lp->data));
      g_object_get (G_OBJECT (lp->data), "menu", &menu, NULL);

      /* add the action to the action group */
      gtk_action_group_add_action (action_group, action);

      /* add the action to the UI manager */
      gtk_ui_manager_add_ui (ui_manager, merge_id, path,
                             gtk_action_get_name (GTK_ACTION (action)),
                             gtk_action_get_name (GTK_ACTION (action)),
                             (menu != NULL) ? GTK_UI_MANAGER_MENU : GTK_UI_MANAGER_MENUITEM, FALSE);

      /* TODO: Receive action path from plugin as generic data as below or create a property in ThunarxMenuItem? */
      action_path = g_object_steal_data (G_OBJECT (lp->data), "action_path");
      if (action_path)
        {
          gtk_action_set_accel_path (action, action_path);
          g_free (action_path);
        }

      /* add submenu items if any */
      if (menu != NULL) {
        children = thunarx_menu_get_items (menu);
        subpath = g_build_path ("/", path, gtk_action_get_name (action), NULL);

        thunar_menu_util_add_items_to_ui_manager (ui_manager, action_group, merge_id,
                                                  subpath, children);

        thunarx_menu_item_list_free (children);
        g_free (subpath);
      }
G_GNUC_END_IGNORE_DEPRECATIONS

      /* release the reference on item and action */
      g_object_unref (G_OBJECT (lp->data));
      g_object_unref (G_OBJECT (action));
    }
}



void
thunar_menu_util_add_items_to_menu (GtkWidget *menu,
                                    GList     *items)
{
  GList           *lp;
  GtkAction       *action;
  GtkWidget       *item;
  GtkWidget       *submenu;
  ThunarxMenu     *thunarx_menu;
  GList           *children;

  /* add the menu items to the UI manager */
  for (lp = items; lp != NULL; lp = lp->next)
    {
      action = action_from_menu_item (G_OBJECT (lp->data));

G_GNUC_BEGIN_IGNORE_DEPRECATIONS
      item = gtk_action_create_menu_item (action);
G_GNUC_END_IGNORE_DEPRECATIONS
      gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
      gtk_widget_show (item);

      /* add submenu items if any */
      g_object_get (G_OBJECT (lp->data), "menu", &thunarx_menu, NULL);
      if (thunarx_menu != NULL) {
        children = thunarx_menu_get_items (thunarx_menu);

        submenu = gtk_menu_new ();
        gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);

        thunar_menu_util_add_items_to_menu (submenu, children);

        thunarx_menu_item_list_free (children);
      }

      /* release the reference on item and action */
      g_object_unref (G_OBJECT (lp->data));
      g_object_unref (G_OBJECT (action));
    }
}