~ubuntu-branches/ubuntu/vivid/openbox-menu/vivid

« back to all changes in this revision

Viewing changes to .pc/fix_missing_double_quote.patch/ob_display.c

  • Committer: Package Import Robot
  • Author(s): Mateusz Łukasik
  • Date: 2014-11-02 11:05:44 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20141102110544-ff0g1xovwvpg9s2z
Tags: 0.7.0-1
* New upstream release.
* debian/control:
  + Bump minimal menu-cache version to 0.7.
  + Bump Standards-Version to 3.9.6. (no changes needed)
* Remove debian/patches/fix_missing_double_quote.patch -- included
  upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//      ob_display.c - this file is part of openbox-menu
2
 
//      Copyright (C) 2010-13 mimas <mimasgpc@free.fr>
3
 
//
4
 
//      This program is free software; you can redistribute it and/or modify
5
 
//      it under the terms of the GNU General Public License as published by
6
 
//      the Free Software Foundation; version 3 of the License.
7
 
//
8
 
//      This program is distributed in the hope that it will be useful,
9
 
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
//      GNU General Public License for more details.
12
 
//
13
 
//      You should have received a copy of the GNU General Public License
14
 
//      along with this program; if not, write to the Free Software
15
 
//      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
16
 
//      MA 02110-1301, USA.
17
 
 
18
 
#include "openbox-menu.h"
19
 
 
20
 
const gchar *default_template =
21
 
    "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
22
 
    "<openbox_pipe_menu xmlns=\"http://openbox.org/\""
23
 
    "  xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
24
 
    "  xsi:schemaLocation=\"http://openbox.org/ >"
25
 
    "%MENU%</openbox_pipe_menu>\n";
26
 
 
27
 
/****f* ob_display/menu_directory
28
 
 * FUNCTION
29
 
 *   create a menu entry for a directory.
30
 
 *
31
 
 * NOTES
32
 
 *   this menu entry has to be closed by "</menu>".
33
 
 ****/
34
 
void
35
 
menu_directory (MenuCacheApp *dir, OB_Menu *context)
36
 
{
37
 
        gchar *dir_id = safe_name (menu_cache_item_get_id (MENU_CACHE_ITEM(dir)));
38
 
        gchar *dir_name = safe_name (menu_cache_item_get_name (MENU_CACHE_ITEM(dir)));
39
 
 
40
 
#ifdef WITH_ICONS
41
 
        if (!context->no_icons)
42
 
        {
43
 
                gchar *dir_icon = item_icon_path (MENU_CACHE_ITEM(dir));
44
 
 
45
 
                g_string_append_printf (context->builder,
46
 
                    "<menu id=\"openbox-%s\" label=\"%s\" icon=\"%s\">\n",
47
 
                    dir_id, dir_name, dir_icon);
48
 
                g_free (dir_icon);
49
 
        }
50
 
        else
51
 
#endif
52
 
        {
53
 
                g_string_append_printf (context->builder,
54
 
              "<menu id=\"openbox-%s\" label=\"%s\">\n",
55
 
              dir_id, dir_name);
56
 
        }
57
 
 
58
 
        g_free (dir_id);
59
 
        g_free (dir_name);
60
 
}
61
 
 
62
 
/****f* ob_display/menu_application
63
 
 * FUNCTION
64
 
 *   create a menu entry for an application.
65
 
 ****/
66
 
void
67
 
menu_application (MenuCacheApp *app, OB_Menu *context)
68
 
{
69
 
        gchar *exec_name = NULL;
70
 
        gchar *exec_icon = NULL;
71
 
        gchar *exec_cmd = NULL;
72
 
 
73
 
        /* is comment (description) or name displayed ? */
74
 
        if (context->comment && menu_cache_item_get_comment (MENU_CACHE_ITEM(app)))
75
 
                exec_name = safe_name (menu_cache_item_get_comment (MENU_CACHE_ITEM(app)));
76
 
        else
77
 
                exec_name = safe_name (menu_cache_item_get_name (MENU_CACHE_ITEM(app)));
78
 
 
79
 
        exec_cmd = clean_exec (app);
80
 
 
81
 
#ifdef WITH_ICONS
82
 
        if (!context->no_icons)
83
 
        {
84
 
                exec_icon = item_icon_path (MENU_CACHE_ITEM(app));
85
 
                g_string_append_printf (context->builder,
86
 
              "<item label=\"%s\" icon=\"%s\"><action name=\"Execute\">",
87
 
              exec_name,
88
 
              exec_icon);
89
 
        }
90
 
        else
91
 
#endif
92
 
        {
93
 
                g_string_append_printf (context->builder,
94
 
              "<item label=\"%s\"><action name=\"Execute\">",
95
 
              exec_name);
96
 
        }
97
 
 
98
 
        if (context->sn && menu_cache_app_get_use_sn (app))
99
 
                g_string_append (context->builder,
100
 
                "<startupnotify><enabled>yes</enabled></startupnotify>");
101
 
 
102
 
        if (menu_cache_app_get_use_terminal (app))
103
 
                g_string_append_printf (context->builder,
104
 
                "<command><![CDATA[%s %s]]></command>\n</action></item>\n",
105
 
                context->terminal_cmd,
106
 
                exec_cmd);
107
 
        else
108
 
                g_string_append_printf (context->builder,
109
 
                "<command><![CDATA[%s]]></command>\n</action></item>\n",
110
 
                exec_cmd);
111
 
 
112
 
        g_free (exec_name);
113
 
        g_free (exec_icon);
114
 
        g_free (exec_cmd);
115
 
}
116
 
 
117
 
/****f* ob_display/menu_generate
118
 
 * FUNCTION
119
 
 *   main routine of menu creation.
120
 
 *
121
 
 * NOTES
122
 
 *   It calls itself when 'dir' type is MENU_CACHE_TYPE_DIR.
123
 
 ****/
124
 
void
125
 
menu_generate (MenuCacheDir *dir, OB_Menu *context)
126
 
{
127
 
        GSList *l = NULL;
128
 
 
129
 
        for (l = menu_cache_dir_get_children (dir); l; l = l->next)
130
 
                switch ((guint) menu_cache_item_get_type (MENU_CACHE_ITEM(l->data)))
131
 
                {
132
 
                        case MENU_CACHE_TYPE_DIR:
133
 
                                menu_directory (l->data, context);
134
 
                                menu_generate (MENU_CACHE_DIR(l->data), context);
135
 
                                g_string_append (context->builder, "</menu>\n");
136
 
                                break;
137
 
 
138
 
                        case MENU_CACHE_TYPE_SEP:
139
 
                                g_string_append (context->builder, "<separator />\n");
140
 
                                break;
141
 
 
142
 
                        case MENU_CACHE_TYPE_APP:
143
 
                                if (app_is_visible (MENU_CACHE_APP(l->data), 0))
144
 
                                        menu_application (l->data, context);
145
 
                }
146
 
}
147
 
 
148
 
 
149
 
/****f* ob_display/get_header_footer_from_template
150
 
 * FUNCTION
151
 
 *   Get header and footer string from a template file. If no template
152
 
 *   file provided, the default template will bu used.
153
 
 *
154
 
 * INPUTS
155
 
 *   * template
156
 
 *
157
 
 * RETURN VALUE
158
 
 *   * a pointer to an array of strings that needs to be freed with g_strfreev.
159
 
 ****/
160
 
gchar **get_header_footer_from_template (gchar *template)
161
 
{
162
 
        gchar *content = NULL;
163
 
        gchar **tokens = NULL;
164
 
 
165
 
        if (template && g_file_get_contents (template, &content, NULL, NULL))
166
 
        {
167
 
                tokens = g_strsplit (content, "%MENU%", 2);
168
 
                g_free (content);
169
 
        }
170
 
        else
171
 
        {
172
 
                tokens = g_strsplit (default_template, "%MENU%", 2);
173
 
        }
174
 
        return tokens;
175
 
}
176
 
 
177
 
/****f* ob_display/menu_display
178
 
 * FUNCTION
179
 
 *   it begins and closes the menu content, write it into a file or
180
 
 *   display it.
181
 
 *
182
 
 * INPUTS
183
 
 *   * menu
184
 
 *   * file, the filename where the menu content should be written to.
185
 
 *     If file is 'NULL' then the menu content is displayed.
186
 
 *
187
 
 * RETURN VALUE
188
 
 *    Nothing. A MenuCacheReloadNotify callback returns void.
189
 
 *
190
 
 * NOTES
191
 
 *   A 16 KiB GString is allocated for the content of the pipemenu.
192
 
 *   This should be enough prevent too many allocations while building
193
 
 *   the XML.
194
 
 *
195
 
 *   The size of the XML file created is around 8 KB in my computer but
196
 
 *   I don't have a lot of applications installed.
197
 
 ****/
198
 
void
199
 
menu_display (MenuCache *menu, OB_Menu *context)
200
 
{
201
 
        gchar **template_parts = NULL;
202
 
 
203
 
        MenuCacheDir *dir = menu_cache_dup_root_dir (menu);
204
 
        if (G_UNLIKELY(dir == NULL))
205
 
        {
206
 
                g_warning ("Can't get menu root directory");
207
 
                context->code = MENU_DIR_ERROR;
208
 
                return;
209
 
        }
210
 
 
211
 
        GSList *l = menu_cache_dir_get_children (dir);
212
 
 
213
 
        if (g_slist_length (l) != 0) {
214
 
                context->builder = g_string_sized_new (16 * 1024);
215
 
 
216
 
                template_parts = get_header_footer_from_template (context->template);
217
 
                // TODO: check if template_parts array contains 2 strings.
218
 
 
219
 
                g_string_append (context->builder, template_parts[0]); // add header
220
 
                menu_generate (dir, context);
221
 
                g_string_append (context->builder, template_parts[1]); // add footer
222
 
 
223
 
                g_strfreev (template_parts);
224
 
 
225
 
                gchar *buff = g_string_free (context->builder, FALSE);
226
 
 
227
 
                /* Has menu content to be saved in a file ? */
228
 
                if (context->output)
229
 
                {
230
 
                        if (!g_file_set_contents (context->output, buff, -1, NULL))
231
 
                                g_warning ("Can't write to %s\n", context->output);
232
 
                        else
233
 
                                g_message ("wrote to %s", context->output);
234
 
                }
235
 
                else /* No, so it's displayed on screen */
236
 
                        g_print ("%s", buff);
237
 
 
238
 
                g_free (buff);
239
 
        }
240
 
        else
241
 
        {
242
 
                g_warning ("Menu seems to be empty. Check openbox-menu parameters.");
243
 
                context->code = MENU_EMPTY_ERROR;
244
 
        }
245
 
 
246
 
        menu_cache_item_unref (MENU_CACHE_ITEM(dir));
247
 
}