aboutsummaryrefslogtreecommitdiffstats
path: root/src/bookmarks/ephy-bookmarks-menu.c
blob: 15126fd113d715bac87ecef597ee89c07b108372 (plain) (blame)
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
 *  Copyright (C) 2003, 2004 Marco Pesenti Gritti
 *  Copyright (C) 2003, 2004 Christian Persch
 *  Copyright (C) 2004 Peter Harvey
 *
 *  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, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 *  $Id$
 */

#include "config.h"

#include "ephy-bookmarks-menu.h"
#include "ephy-bookmarks-ui.h"
#include "ephy-bookmark-action.h"
#include "ephy-open-tabs-action.h"
#include "ephy-topic-action.h"
#include "ephy-nodes-cover.h"
#include "ephy-node-common.h"
#include "ephy-link.h"
#include "ephy-shell.h"
#include "ephy-string.h"
#include "ephy-gui.h"
#include "ephy-debug.h"

#include <string.h>

#define MIN_MENU_SIZE 7
#define MAX_MENU_SIZE 21

enum
{
    BUILD_SUBDIVIS       = 1 << 0,
    BUILD_SUBMENUS       = 1 << 1,
    BUILD_CHILD_SUBDIVIS = 1 << 2,
    BUILD_CHILD_SUBMENUS = 1 << 3
};

/* Construct a block of bookmark actions, postfixing the names with a topic id
 * to allow different. */
static void
append_bookmarks (GString *string, const GPtrArray *bookmarks, gint instance)
{
    char *name;
    long i;
    
    for (i = 0; i < bookmarks->len; i++)
    {
        name = ephy_bookmark_action_name (g_ptr_array_index (bookmarks, i));
        if (name)
        {
            g_string_append_printf (string, "<menuitem action=\"%s\" name=\"%s-%d\"/>",
                        name, name, instance);
            g_free (name);
        }
    }
}

/* Build a menu of the given bookmarks categorised by the given topics.
 * Shows categorisation using subdivisions, submenus, or a mix of both. */
static void
append_menu (GString *string, const GPtrArray *topics, const GPtrArray *bookmarks, guint flags)
{
    GPtrArray *uncovered;
    guint i, j;
    
    gboolean use_subdivis = flags & BUILD_SUBDIVIS;
    gboolean use_submenus = flags & BUILD_SUBMENUS;        

    if (use_subdivis || use_submenus)
    {
        GPtrArray *subset, *covering, *subdivisions, *submenus, *unused;
        GArray *sizes = 0;
        EphyNode *topic;
        gint size, total;
        gboolean separate = FALSE;
        char *name;
        
        /* Get the subtopics, uncovered bookmarks, and subtopic sizes. */
        sizes = g_array_sized_new (FALSE, FALSE, sizeof(int), topics->len);
        uncovered = g_ptr_array_sized_new (bookmarks->len);
        covering = ephy_nodes_get_covering (topics, bookmarks, 0, uncovered, sizes);

        /* Preallocate arrays for submenus, subdivisions, and bookmark subsets. */
        subdivisions = g_ptr_array_sized_new (topics->len);
        submenus = g_ptr_array_sized_new (topics->len);
        subset = g_ptr_array_sized_new (bookmarks->len);
        unused = g_ptr_array_sized_new (bookmarks->len);

        /* Get the total number of items in the menu. */
        total = uncovered->len;
        for (i = 0; i < covering->len; i++)
          total += g_array_index (sizes, int, i);
        
        /* Seperate covering into list of submenus and subdivisions */
        for (i = 0; i < covering->len; i++)
        {
            topic = g_ptr_array_index (covering, i);
            size = g_array_index (sizes, int, i);
            
            if (!use_submenus || (use_subdivis && (size < MIN_MENU_SIZE || total < MAX_MENU_SIZE)))
            {
                g_ptr_array_add (subdivisions, topic);
            }
            else
            {
                g_ptr_array_add (submenus, topic);
                total = total - size + 1;
            }
        }
        
        /* Sort the list of submenus and subdivisions. */
        g_ptr_array_sort (submenus, ephy_bookmarks_compare_topic_pointers);
        g_ptr_array_sort (subdivisions, ephy_bookmarks_compare_topic_pointers);
        
        if (flags & BUILD_CHILD_SUBDIVIS) flags |= BUILD_SUBDIVIS;
        if (flags & BUILD_CHILD_SUBMENUS) flags |= BUILD_SUBMENUS;
        
        /* Create each of the submenus. */
        for (i = 0; i < submenus->len; i++)
        {
            topic = g_ptr_array_index (submenus, i);
            ephy_nodes_get_covered (topic, bookmarks, subset);
            
            name = ephy_topic_action_name (topic);
            if (name)
            {
                g_string_append_printf (string, "<menu name=\"%s\" action=\"%s\">",
                            name, name);
                append_menu (string, topics, subset, flags);
                g_string_append (string, "</menu>");
                separate = TRUE;
                g_free (name);
            }
        }
        
        /* Build a list of bookmarks which don't appear in any subdivision yet. */
        for (i = 0; i < bookmarks->len; i++)
        {
            g_ptr_array_add (unused, g_ptr_array_index (bookmarks, i));
        }

        /* Create each of the subdivisions. */
        for (i = 0; i < subdivisions->len; i++)
        {
            topic = g_ptr_array_index (subdivisions, i);
            ephy_nodes_get_covered (topic, unused, subset);
            g_ptr_array_sort (subset, ephy_bookmarks_compare_bookmark_pointers);
            
            if (separate) g_string_append (string, "<separator/>");
            append_bookmarks (string, subset, i+1);
            separate = TRUE;
            
            /* Record that each bookmark has been added. */
            for (j = 0; j < subset->len; j++)
            {
                g_ptr_array_remove_fast (unused, g_ptr_array_index (subset, j));
            }
        }

        g_array_free (sizes, TRUE);
        g_ptr_array_free (covering, TRUE);
        g_ptr_array_free (subdivisions, TRUE);
        g_ptr_array_free (submenus, TRUE);
        g_ptr_array_free (subset, TRUE);
        g_ptr_array_free (unused, TRUE);
        
        if (separate && uncovered->len) g_string_append (string, "<separator/>");
    }
    else
    {
        uncovered = g_ptr_array_sized_new (bookmarks->len);
        for (i = 0; i < bookmarks->len; i++)
          g_ptr_array_add (uncovered, g_ptr_array_index (bookmarks, i));
        g_ptr_array_sort (uncovered, ephy_bookmarks_compare_bookmark_pointers);
    }
    
    /* Create the final subdivision (uncovered bookmarks). */
    g_ptr_array_sort (uncovered, ephy_bookmarks_compare_bookmark_pointers);
    append_bookmarks (string, uncovered, 0);
    g_ptr_array_free (uncovered, TRUE);        
}

void
ephy_bookmarks_menu_build (GString *string, EphyNode *parent)
{
    GPtrArray *children, *topics;
    EphyBookmarks *eb;
    EphyNode *node;
    gint priority;
    guint flags, id, i;
    
    eb = ephy_shell_get_bookmarks (ephy_shell);

    children = ephy_node_get_children (ephy_bookmarks_get_keywords (eb));        
    topics = g_ptr_array_sized_new (children->len);
    for (i = 0; i < children->len; i++)
    {
        node = g_ptr_array_index (children, i);
        priority = ephy_node_get_property_int (node, EPHY_NODE_KEYWORD_PROP_PRIORITY);
        if (priority == EPHY_NODE_NORMAL_PRIORITY)
          g_ptr_array_add (topics, node);
    }

    /* If no parent was supplied, use the default 'All' */
    node = parent ? parent : ephy_bookmarks_get_bookmarks(eb);
    children = ephy_node_get_children (node);

    /* Determine what kind of menu we want. */
    id = ephy_node_get_id (node);
        switch(id)
        {
         case FAVORITES_NODE_ID:
                flags = 0;
                break;
         case BOOKMARKS_NODE_ID:
                flags = BUILD_SUBMENUS | BUILD_CHILD_SUBDIVIS;
                break;
         default:
                flags = BUILD_SUBMENUS | BUILD_SUBDIVIS | BUILD_CHILD_SUBDIVIS;
                /* flags = BUILD_SUBDIVIS; */
                break;
        }

    /* If this menu is the 'All' menu, be sure to include the 'local' topic. */
    if (id == BOOKMARKS_NODE_ID)
    {
        EphyNode *local_node;
        
        local_node = ephy_bookmarks_get_local (eb);
        if (local_node != NULL)
        {
            g_ptr_array_add (topics, ephy_bookmarks_get_local (eb));
        }
        
        append_menu (string, topics, children, flags);
        g_ptr_array_free (topics, TRUE);
    }
    
    /* Otherwise, build the menu with "Open in tabs". */
    else
    {
        append_menu (string, topics, children, flags);
        g_ptr_array_free (topics, TRUE);
    
        char *name = ephy_open_tabs_action_name (node);
        g_string_append_printf
          (string, "<separator/><menuitem action=\"%s\" name=\"OpenTabs\"/>", name);
        g_free (name);
    }
}