Codebase list dillo / debian/0.8.6-3 src / nav.c
debian/0.8.6-3

Tree @debian/0.8.6-3 (Download .tar.gz)

nav.c @debian/0.8.6-3raw · history · 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
 * File: nav.c
 *
 * Copyright (C) 1999 James McCollough <jamesm@gtwn.net>
 * Copyright (C) 2000, 2001, 2002 Jorge Arellano Cid <jcid@dillo.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.
 */

/* Support for a navigation stack */

#include <stdio.h>
#include <gtk/gtk.h>
#include "msg.h"
#include "list.h"
#include "nav.h"
#include "history.h"
#include "web.h"
#include "menu.h"
#include "interface.h"
#include "dw_gtk_scrolled_window.h"
#include "prefs.h"
#include "capi.h"

/* #define DEBUG_LEVEL 3 */
#include "debug.h"

/*
 * Forward declarations
 */
static void Nav_reload(BrowserWindow *bw);


/*
 * Initialize the navigation structure with safe values
 */
void a_Nav_init(BrowserWindow *bw)
{
   bw->nav_stack_size = 0;
   bw->nav_stack_size_max = 16;
   bw->nav_stack = NULL;
   bw->nav_stack_ptr = -1;
   bw->nav_expecting = FALSE;
   bw->nav_expect_url = NULL;
}

/*
 * Free memory used by this module
 */
void a_Nav_free(BrowserWindow *bw)
{
   a_Nav_cancel_expect(bw);
   g_free(bw->nav_stack);
}


/* Navigation stack methods ------------------------------------------------ */

/*
 * Return current nav_stack pointer [0 based; -1 = empty]
 */
gint a_Nav_stack_ptr(BrowserWindow *bw)
{
   return bw->nav_stack_ptr;
}

/*
 * Move the nav_stack pointer
 */
static void Nav_stack_move_ptr(BrowserWindow *bw, gint offset)
{
   gint nptr;

   g_return_if_fail (bw != NULL);
   if (offset != 0) {
      nptr = bw->nav_stack_ptr + offset;
      g_return_if_fail (nptr >= 0 && nptr < bw->nav_stack_size);
      bw->nav_stack_ptr = nptr;
   }
}

/*
 * Return size of nav_stack [1 based]
 */
gint a_Nav_stack_size(BrowserWindow *bw)
{
   return bw->nav_stack_size;
}

/*
 * Add an URL-index in the navigation stack.
 */
static void Nav_stack_add(BrowserWindow *bw, gint idx)
{
   g_return_if_fail (bw != NULL);

   ++bw->nav_stack_ptr;
   if ( bw->nav_stack_ptr == bw->nav_stack_size) {
      a_List_add(bw->nav_stack, bw->nav_stack_size, bw->nav_stack_size_max);
      ++bw->nav_stack_size;
   } else {
      bw->nav_stack_size = bw->nav_stack_ptr + 1;
   }
   bw->nav_stack[bw->nav_stack_ptr] = idx;
}

/*
 * Remove an URL-index from the navigation stack.
 */
static void Nav_stack_remove(BrowserWindow *bw, gint idx)
{
   gint sz = a_Nav_stack_size(bw);

   g_return_if_fail (bw != NULL && idx >=0 && idx < sz);

   for (  ; idx < sz - 1; ++idx)
      bw->nav_stack[idx] = bw->nav_stack[idx + 1];
   if ( bw->nav_stack_ptr == --bw->nav_stack_size )
      --bw->nav_stack_ptr;
}

/*
 * Remove equal adyacent URLs at the top of the stack.
 * (It may happen with redirections)
 */
static void Nav_stack_clean(BrowserWindow *bw)
{
   gint i;

   g_return_if_fail (bw != NULL);

   if ((i = a_Nav_stack_size(bw)) >= 2 &&
       bw->nav_stack[i-2] == bw->nav_stack[i-1])
         Nav_stack_remove(bw, i - 1);
}


/* General methods --------------------------------------------------------- */

/*
 * Create a DilloWeb structure for 'url' and ask the cache to send it back.
 *  - Also set a few things related to the browser window.
 * This function requests the page's root-URL; images and related stuff
 * are fetched directly by the HTML module.
 */
static void Nav_open_url(BrowserWindow *bw, const DilloUrl *url, gint offset)
{
   DilloUrl *old_url = NULL;
   gboolean MustLoad;
   gint ClientKey;
   DilloWeb *Web;
   gboolean ForceReload = (URL_FLAGS(url) & URL_E2EReload);

   MSG("Nav_open_url: Url=>%s<\n", URL_STR_(url));

   /* Get the url of the current page */
   if ( a_Nav_stack_ptr(bw) != -1 )
      old_url = a_History_get_url(NAV_TOP(bw));

   /* Record current scrolling position
    * (the strcmp check is necessary because of redirections) */
   if (old_url &&
       !strcmp(URL_STR(old_url), a_Interface_get_location_text(bw))) {
      old_url->scrolling_position_x =
        a_Dw_gtk_scrolled_window_get_scrolling_position_x(
           GTK_DW_SCROLLED_WINDOW(bw->docwin));
      old_url->scrolling_position_y =
        a_Dw_gtk_scrolled_window_get_scrolling_position_y(
           GTK_DW_SCROLLED_WINDOW(bw->docwin));
   }

   /* Update navigation-stack-pointer (offset may be zero) */
   Nav_stack_move_ptr(bw, offset);

   /* Page must be reloaded, if old and new url (without anchor) differ */
   MustLoad = ForceReload || !old_url;
   if (old_url){
      MustLoad |= a_Url_cmp(old_url, url);
      MustLoad |= strcmp(URL_STR(old_url), a_Interface_get_location_text(bw));
   }

   if ( MustLoad ) {
      a_Interface_stop(bw);
      a_Interface_clean(bw);

      a_Menu_pagemarks_new(bw);

      Web = a_Web_new(url);
      Web->bw = bw;
      Web->flags |= WEB_RootUrl;
      if ((ClientKey = a_Capi_open_url(Web, NULL, NULL)) != 0) {
         a_Interface_add_client(bw, ClientKey, 1);
         a_Interface_add_url(bw, url, WEB_RootUrl);
      }
      a_Interface_set_cursor(bw, GDK_LEFT_PTR);
   }

   /* Jump to #anchor position */
   if (URL_FRAGMENT_(url)) {
      /* todo: push on stack */
      gchar *pf = a_Url_decode_hex_str(URL_FRAGMENT_(url));
      a_Dw_gtk_scrolled_window_set_anchor(
         GTK_DW_SCROLLED_WINDOW(bw->docwin), pf);
      g_free(pf);
   }
}

/*
 * Cancel the last expected url if present. The responsibility
 * for actually aborting the data stream remains with the caller.
 */
void a_Nav_cancel_expect(BrowserWindow *bw)
{
   if (bw->nav_expecting) {
      if (bw->nav_expect_url) {
         a_Url_free(bw->nav_expect_url);
         bw->nav_expect_url = NULL;
      }
      bw->nav_expecting = FALSE;
   }
}

/*
 * We have an answer! Set things accordingly.
 */
void a_Nav_expect_done(BrowserWindow *bw)
{
   gint idx;
   DilloUrl *url;

   g_return_if_fail(bw != NULL);

   if (bw->nav_expecting) {
      url = bw->nav_expect_url;
      /* unset E2EReload before adding this url to history */
      a_Url_set_flags(url, URL_FLAGS(url) & ~URL_E2EReload);
      idx = a_History_add_url(url);
      Nav_stack_add(bw, idx);

      a_Url_free(url);
      bw->nav_expect_url = NULL;
      bw->nav_expecting = FALSE;
   }
   Nav_stack_clean(bw);
   a_Interface_set_button_sens(bw);
}

/*
 * Make 'url' the current browsed page (upon data arrival)
 * - Set bw to expect the URL data
 * - Ask the cache to feed back the requested URL (via Nav_open_url)
 */
void a_Nav_push(BrowserWindow *bw, const DilloUrl *url)
{
   g_return_if_fail (bw != NULL);

   if (bw->nav_expecting && a_Url_cmp(bw->nav_expect_url, url) == 0 &&
       URL_STRCAMP_EQ(URL_FRAGMENT_(bw->nav_expect_url), URL_FRAGMENT_(url))) {
      /* we're already expecting that url (most probably a double-click) */
      return;
   }
   a_Nav_cancel_expect(bw);
   bw->nav_expect_url = a_Url_dup(url);
   bw->nav_expecting = TRUE;
   Nav_open_url(bw, url, 0);
}

/*
 * Same as a_Nav_push() but in a new window.
 */
void a_Nav_push_nw(BrowserWindow *bw, const DilloUrl *url)
{
   gint width, height;
   BrowserWindow *newbw;

   gdk_window_get_size (bw->main_window->window, &width, &height);
   newbw = a_Interface_browser_window_new(width, height, 0);
   a_Nav_push(newbw, url);
}

/*
 * Wraps a_Nav_push to match 'DwPage->link' function type
 */
void a_Nav_vpush(void *vbw, const DilloUrl *url)
{
   a_Nav_push(vbw, url);
}

/*
 * Send the browser back to previous page
 */
void a_Nav_back(BrowserWindow *bw)
{
   gint idx = a_Nav_stack_ptr(bw);

   a_Nav_cancel_expect(bw);
   if ( --idx >= 0 ){
      a_Interface_msg(bw, "");
      Nav_open_url(bw, a_History_get_url(NAV_IDX(bw,idx)), -1);
   }
}

/*
 * Send the browser to next page in the history list
 */
void a_Nav_forw(BrowserWindow *bw)
{
   gint idx = a_Nav_stack_ptr(bw);

   a_Nav_cancel_expect(bw);
   if (++idx < a_Nav_stack_size(bw)) {
      a_Interface_msg(bw, "");
      Nav_open_url(bw, a_History_get_url(NAV_IDX(bw,idx)), +1);
   }
}

/*
 * Redirect the browser to the HOME page!
 */
void a_Nav_home(BrowserWindow *bw)
{
   a_Nav_push(bw, prefs.home);
}

/*
 * Jump to an URL within the stack history
 * NewBw: {0 = same window, 1 = new window}
 */
void a_Nav_jump_callback(GtkWidget *widget, gpointer client_data, gint NewBw)
{
   int idx;
   BrowserWindow *bw = client_data;

   idx = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT (widget), "nav_idx"));
   if (idx >= 0 && idx < a_Nav_stack_size(bw)) {
      if (NewBw == 1) {
         a_Nav_push_nw(bw, a_History_get_url(NAV_IDX(bw,idx)));
      } else {
         Nav_open_url(bw, a_History_get_url(NAV_IDX(bw,idx)),
                      idx - a_Nav_stack_ptr(bw));
      }
   }
}

/*
 * Callback for reload confirmation
 */
static void Nav_reload_confirmation_cb(BrowserWindow *bw)
{
   DialogAnswer *answer = bw->question_dialog_answer;

   _MSG("Nav_reload_confirmation_cb %p\n", bw->question_dialog_window);

   if (answer->alt_num == 1) { /* "OK" */
      DEBUG_MSG(3, "Nav_reload_confirmed\n");
      if ( a_Nav_stack_size(bw) &&
           bw->question_dialog_data == a_History_get_url(NAV_TOP(bw)) ) {
         /* a genuine confirmation! */
         DEBUG_MSG(3, "Nav_reload_confirmed test: OK\n");
         Nav_reload(bw);
      }

   } else {  /* window closed or cancel button */
      DEBUG_MSG(3, "Nav_reload_refused\n");
   }

   /* cleanup */
   bw->question_dialog_data = NULL;
   g_free(answer->this);
   bw->question_dialog_answer = NULL;
}

/*
 * This one does a_Nav_reload's job!
 */
static void Nav_reload(BrowserWindow *bw)
{
   DilloUrl *url, *ReqURL;

   a_Nav_cancel_expect(bw);
   if ( a_Nav_stack_size(bw) ) {
      url = a_History_get_url(NAV_TOP(bw));
      ReqURL = a_Url_dup(a_History_get_url(NAV_TOP(bw)));
      /* Let's make reload be end-to-end */
      a_Url_set_flags(ReqURL, URL_FLAGS(ReqURL) | URL_E2EReload);
      /* This is an explicit reload, so clear the SpamSafe flag */
      a_Url_set_flags(ReqURL, URL_FLAGS(ReqURL) & ~URL_SpamSafe);
      Nav_open_url(bw, ReqURL, 0);
      a_Url_free(ReqURL);
   }
}

/*
 * Implement the RELOAD button functionality.
 * (We haven't defined it yet ;)
 */
void a_Nav_reload(BrowserWindow *bw)
{
   DilloUrl *url;

   a_Nav_cancel_expect(bw);
   if ( a_Nav_stack_size(bw) ) {
      url = a_History_get_url(NAV_TOP(bw));
      if (URL_FLAGS(url) & URL_Post) {
         /* Attempt to repost data, let's confirm... */
         bw->question_dialog_data = (gpointer)url;
         a_Interface_question_dialog(
            bw, "Repost form data?", TRUE,
            "OK", "Cancel", NULL, NULL, NULL,
            (GtkSignalFunc) Nav_reload_confirmation_cb);

      } else {
         Nav_reload(bw);
      }
   }
}