NetSurf
Data Structures | Macros | Functions | Variables
viewdata.c File Reference

generic data viewer implementation. More...

#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <unistd.h>
#include <gtk/gtk.h>
#include "utils/log.h"
#include "utils/nsoption.h"
#include "utils/utf8.h"
#include "utils/messages.h"
#include "utils/utils.h"
#include "utils/file.h"
#include "utils/filepath.h"
#include "utils/nsurl.h"
#include "netsurf/browser_window.h"
#include "gtk/warn.h"
#include "gtk/about.h"
#include "gtk/fetch.h"
#include "gtk/compat.h"
#include "gtk/resources.h"
#include "gtk/viewdata.h"
Include dependency graph for viewdata.c:

Go to the source code of this file.

Data Structures

struct  nsgtk_viewdata_ctx
 
struct  menu_events
 

Macros

#define _WITH_GETLINE   /* necessary for FreeBSD */
 
#define MENUEVENT(x)   { #x, G_CALLBACK(nsgtk_on_##x##_activate) }
 
#define MENUPROTO(x)
 

Functions

 MENUPROTO (viewdata_save_as)
 
 MENUPROTO (viewdata_print)
 
 MENUPROTO (viewdata_close)
 
 MENUPROTO (viewdata_select_all)
 
 MENUPROTO (viewdata_cut)
 
 MENUPROTO (viewdata_copy)
 
 MENUPROTO (viewdata_paste)
 
 MENUPROTO (viewdata_delete)
 
 MENUPROTO (viewdata_zoom_in)
 
 MENUPROTO (viewdata_zoom_out)
 
 MENUPROTO (viewdata_zoom_normal)
 
 MENUPROTO (viewdata_about)
 
static void nsgtk_attach_viewdata_menu_handlers (GtkBuilder *xml, gpointer g)
 
static gboolean nsgtk_viewdata_destroy_event (GtkBuilder *window, gpointer g)
 
static gboolean nsgtk_viewdata_delete_event (GtkWindow *window, gpointer g)
 
static void nsgtk_viewdata_file_save (GtkWindow *parent, const char *filename, const char *data, size_t data_size)
 
gboolean nsgtk_on_viewdata_save_as_activate (GtkMenuItem *widget, gpointer g)
 
gboolean nsgtk_on_viewdata_print_activate (GtkMenuItem *widget, gpointer g)
 
gboolean nsgtk_on_viewdata_close_activate (GtkMenuItem *widget, gpointer g)
 
gboolean nsgtk_on_viewdata_select_all_activate (GtkMenuItem *widget, gpointer g)
 
gboolean nsgtk_on_viewdata_cut_activate (GtkMenuItem *widget, gpointer g)
 
gboolean nsgtk_on_viewdata_copy_activate (GtkMenuItem *widget, gpointer g)
 
gboolean nsgtk_on_viewdata_paste_activate (GtkMenuItem *widget, gpointer g)
 
gboolean nsgtk_on_viewdata_delete_activate (GtkMenuItem *widget, gpointer g)
 
static void nsgtk_viewdata_update_zoomlevel (gpointer g)
 
gboolean nsgtk_on_viewdata_zoom_in_activate (GtkMenuItem *widget, gpointer g)
 
gboolean nsgtk_on_viewdata_zoom_out_activate (GtkMenuItem *widget, gpointer g)
 
gboolean nsgtk_on_viewdata_zoom_normal_activate (GtkMenuItem *widget, gpointer g)
 
gboolean nsgtk_on_viewdata_about_activate (GtkMenuItem *widget, gpointer g)
 
static nserror window_init (const char *title, const char *filename, char *ndata, size_t ndata_len)
 View the data in a gtk text window. More...
 
static nserror window_init_fname (const char *title, const char *leafname, const char *filename)
 open a window to dispaly an existing file. More...
 
static nserror tab_init_fname (const char *title, const char *leafname, const char *fname)
 open a new tab from an existing file. More...
 
static nserror tab_init (const char *title, const char *leafname, char *ndata, size_t ndata_len)
 create a new tab from data. More...
 
static char ** xdg_data_strvec (void)
 Build string vector of search path. More...
 
static char * xdg_get_default_app (const char *path, const char *mimetype)
 Search application defaults file for matching mime type. More...
 
static char * xdg_get_exec_cmd (const char *path, const char *desktop)
 Search desktop file for an Exec line. More...
 
static char * exec_arg (const char *arg, int len, const char *fname)
 
static char ** build_exec_argv (const char *fname, const char *exec_cmd)
 Build vector for executing app. More...
 
static nserror editor_init_fname (const char *title, const char *leafname, const char *fname)
 open an editor from an existing file. More...
 
static nserror editor_init (const char *title, const char *leafname, char *ndata, size_t ndata_len)
 open an editor with data. More...
 
nserror nsgtk_viewdata (const char *title, const char *filename, char *ndata, size_t ndata_len)
 Display text to a user. More...
 
nserror nsgtk_viewfile (const char *title, const char *leafname, const char *filename)
 Display file to a user. More...
 

Variables

static struct nsgtk_viewdata_ctxnsgtk_viewdata_list = NULL
 
static char viewdata_zoomlevel = 10
 
static struct menu_events viewdata_menu_events []
 

Detailed Description

generic data viewer implementation.

This viewer can be used for utf-8 encoded chunk of data. Thie data might be page source or the debugging of dom or box trees. It will show the data in a tab, window or editor as per user configuration.

Definition in file viewdata.c.

Macro Definition Documentation

◆ _WITH_GETLINE

#define _WITH_GETLINE   /* necessary for FreeBSD */

Definition at line 31 of file viewdata.c.

◆ MENUEVENT

#define MENUEVENT (   x)    { #x, G_CALLBACK(nsgtk_on_##x##_activate) }

Definition at line 74 of file viewdata.c.

◆ MENUPROTO

#define MENUPROTO (   x)
Value:
static gboolean nsgtk_on_##x##_activate( \
GtkMenuItem *widget, gpointer g)

Definition at line 75 of file viewdata.c.

Function Documentation

◆ build_exec_argv()

static char ** build_exec_argv ( const char *  fname,
const char *  exec_cmd 
)
static

Build vector for executing app.

Definition at line 777 of file viewdata.c.

References exec_arg(), and NSLOG.

Referenced by editor_init_fname().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ editor_init()

static nserror editor_init ( const char *  title,
const char *  leafname,
char *  ndata,
size_t  ndata_len 
)
static

open an editor with data.

Definition at line 892 of file viewdata.c.

References editor_init_fname(), messages_get(), NSERROR_OK, NSERROR_SAVE_FAILED, and nsgtk_warning().

Referenced by nsgtk_viewdata().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ editor_init_fname()

static nserror editor_init_fname ( const char *  title,
const char *  leafname,
const char *  fname 
)
static

open an editor from an existing file.

Definition at line 826 of file viewdata.c.

References build_exec_argv(), filepath_free_strvec(), NSERROR_NOT_FOUND, NSERROR_OK, xdg_data_strvec(), xdg_get_default_app(), and xdg_get_exec_cmd().

Referenced by editor_init(), and nsgtk_viewfile().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ exec_arg()

static char * exec_arg ( const char *  arg,
int  len,
const char *  fname 
)
static

Definition at line 754 of file viewdata.c.

Referenced by build_exec_argv().

Here is the caller graph for this function:

◆ MENUPROTO() [1/12]

MENUPROTO ( viewdata_about  )

◆ MENUPROTO() [2/12]

MENUPROTO ( viewdata_close  )

◆ MENUPROTO() [3/12]

MENUPROTO ( viewdata_copy  )

◆ MENUPROTO() [4/12]

MENUPROTO ( viewdata_cut  )

◆ MENUPROTO() [5/12]

MENUPROTO ( viewdata_delete  )

◆ MENUPROTO() [6/12]

MENUPROTO ( viewdata_paste  )

◆ MENUPROTO() [7/12]

MENUPROTO ( viewdata_print  )

◆ MENUPROTO() [8/12]

MENUPROTO ( viewdata_save_as  )

◆ MENUPROTO() [9/12]

MENUPROTO ( viewdata_select_all  )

◆ MENUPROTO() [10/12]

MENUPROTO ( viewdata_zoom_in  )

◆ MENUPROTO() [11/12]

MENUPROTO ( viewdata_zoom_normal  )

◆ MENUPROTO() [12/12]

MENUPROTO ( viewdata_zoom_out  )

◆ nsgtk_attach_viewdata_menu_handlers()

static void nsgtk_attach_viewdata_menu_handlers ( GtkBuilder *  xml,
gpointer  g 
)
static

Definition at line 107 of file viewdata.c.

References viewdata_menu_events.

Referenced by window_init().

Here is the caller graph for this function:

◆ nsgtk_on_viewdata_about_activate()

gboolean nsgtk_on_viewdata_about_activate ( GtkMenuItem *  widget,
gpointer  g 
)

Definition at line 338 of file viewdata.c.

References nsgtk_about_dialog_init(), and nsgtk_viewdata_ctx::window.

Here is the call graph for this function:

◆ nsgtk_on_viewdata_close_activate()

gboolean nsgtk_on_viewdata_close_activate ( GtkMenuItem *  widget,
gpointer  g 
)

Definition at line 223 of file viewdata.c.

References nsgtk_viewdata_ctx::window.

◆ nsgtk_on_viewdata_copy_activate()

gboolean nsgtk_on_viewdata_copy_activate ( GtkMenuItem *  widget,
gpointer  g 
)

Definition at line 252 of file viewdata.c.

References nsgtk_viewdata_ctx::gv.

◆ nsgtk_on_viewdata_cut_activate()

gboolean nsgtk_on_viewdata_cut_activate ( GtkMenuItem *  widget,
gpointer  g 
)

Definition at line 247 of file viewdata.c.

◆ nsgtk_on_viewdata_delete_activate()

gboolean nsgtk_on_viewdata_delete_activate ( GtkMenuItem *  widget,
gpointer  g 
)

Definition at line 268 of file viewdata.c.

◆ nsgtk_on_viewdata_paste_activate()

gboolean nsgtk_on_viewdata_paste_activate ( GtkMenuItem *  widget,
gpointer  g 
)

Definition at line 263 of file viewdata.c.

◆ nsgtk_on_viewdata_print_activate()

gboolean nsgtk_on_viewdata_print_activate ( GtkMenuItem *  widget,
gpointer  g 
)

Definition at line 216 of file viewdata.c.

◆ nsgtk_on_viewdata_save_as_activate()

gboolean nsgtk_on_viewdata_save_as_activate ( GtkMenuItem *  widget,
gpointer  g 
)

Definition at line 184 of file viewdata.c.

References nsgtk_viewdata_ctx::data, nsgtk_viewdata_ctx::data_len, nsgtk_viewdata_ctx::filename, messages_get(), NSGTK_STOCK_CANCEL, NSGTK_STOCK_SAVE, nsgtk_viewdata_file_save(), and nsgtk_viewdata_ctx::window.

Here is the call graph for this function:

◆ nsgtk_on_viewdata_select_all_activate()

gboolean nsgtk_on_viewdata_select_all_activate ( GtkMenuItem *  widget,
gpointer  g 
)

Definition at line 234 of file viewdata.c.

References nsgtk_viewdata_ctx::gv.

◆ nsgtk_on_viewdata_zoom_in_activate()

gboolean nsgtk_on_viewdata_zoom_in_activate ( GtkMenuItem *  widget,
gpointer  g 
)

Definition at line 311 of file viewdata.c.

References nsgtk_viewdata_update_zoomlevel(), and viewdata_zoomlevel.

Here is the call graph for this function:

◆ nsgtk_on_viewdata_zoom_normal_activate()

gboolean nsgtk_on_viewdata_zoom_normal_activate ( GtkMenuItem *  widget,
gpointer  g 
)

Definition at line 330 of file viewdata.c.

References nsgtk_viewdata_update_zoomlevel(), and viewdata_zoomlevel.

Here is the call graph for this function:

◆ nsgtk_on_viewdata_zoom_out_activate()

gboolean nsgtk_on_viewdata_zoom_out_activate ( GtkMenuItem *  widget,
gpointer  g 
)

Definition at line 319 of file viewdata.c.

References nsgtk_viewdata_update_zoomlevel(), and viewdata_zoomlevel.

Here is the call graph for this function:

◆ nsgtk_viewdata()

nserror nsgtk_viewdata ( const char *  title,
const char *  filename,
char *  data,
size_t  data_size 
)

Display text to a user.

The data is utf-8 encoded text and will be presented in a window, a tab or an editor as per the user configuration.

Parameters
titleThe title of the data being displayed.
filenameThe suggested filename to be used.
dataThe data to be shown. This data will be freed once the display is complete, the caller no longer owns the allocation.
data_sizeThe size of the data in data.

Definition at line 931 of file viewdata.c.

References editor_init(), nsgtk_viewdata_ctx::filename, NSERROR_BAD_PARAMETER, NSERROR_OK, nsoption_int, tab_init(), and window_init().

Referenced by nsgtk_viewsource().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ nsgtk_viewdata_delete_event()

static gboolean nsgtk_viewdata_delete_event ( GtkWindow *  window,
gpointer  g 
)
static

Definition at line 145 of file viewdata.c.

Referenced by window_init().

Here is the caller graph for this function:

◆ nsgtk_viewdata_destroy_event()

static gboolean nsgtk_viewdata_destroy_event ( GtkBuilder *  window,
gpointer  g 
)
static

Definition at line 119 of file viewdata.c.

References nsgtk_viewdata_ctx::builder, nsgtk_viewdata_ctx::data, nsgtk_viewdata_ctx::next, nsgtk_viewdata_list, and nsgtk_viewdata_ctx::prev.

Referenced by window_init().

Here is the caller graph for this function:

◆ nsgtk_viewdata_file_save()

static void nsgtk_viewdata_file_save ( GtkWindow *  parent,
const char *  filename,
const char *  data,
size_t  data_size 
)
static

Definition at line 152 of file viewdata.c.

References nsgtk_viewdata_ctx::data, nsgtk_viewdata_ctx::filename, messages_get(), nsgtk_dialog_get_content_area(), NSGTK_STOCK_OK, and parent.

Referenced by nsgtk_on_viewdata_save_as_activate().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ nsgtk_viewdata_update_zoomlevel()

static void nsgtk_viewdata_update_zoomlevel ( gpointer  g)
static

Definition at line 273 of file viewdata.c.

References nsgtk_viewdata_ctx::gv, nsgtk_viewdata_ctx::next, nsgtk_viewdata_list, and viewdata_zoomlevel.

Referenced by nsgtk_on_viewdata_zoom_in_activate(), nsgtk_on_viewdata_zoom_normal_activate(), and nsgtk_on_viewdata_zoom_out_activate().

Here is the caller graph for this function:

◆ nsgtk_viewfile()

nserror nsgtk_viewfile ( const char *  title,
const char *  leafname,
const char *  filename 
)

Display file to a user.

The file is interpreted as utf-8 encoded text and will be presented in a window, a tab or an editor as per the user configuration.

Parameters
titleThe title of the data being displayed.
leafnameThe suggested leafname to be used.
filenameThe filename of the data to be viewed.

Definition at line 966 of file viewdata.c.

References editor_init_fname(), nsgtk_viewdata_ctx::filename, NSERROR_BAD_PARAMETER, nsoption_int, tab_init_fname(), and window_init_fname().

Referenced by debugboxtree_button_clicked_cb(), and debugdomtree_button_clicked_cb().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tab_init()

static nserror tab_init ( const char *  title,
const char *  leafname,
char *  ndata,
size_t  ndata_len 
)
static

create a new tab from data.

Definition at line 527 of file viewdata.c.

References messages_get(), NSERROR_OK, NSERROR_SAVE_FAILED, nsgtk_warning(), and tab_init_fname().

Referenced by nsgtk_viewdata().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tab_init_fname()

static nserror tab_init_fname ( const char *  title,
const char *  leafname,
const char *  fname 
)
static

open a new tab from an existing file.

Definition at line 500 of file viewdata.c.

References browser_window_create(), BW_CREATE_HISTORY, BW_CREATE_TAB, netsurf_path_to_nsurl(), NSERROR_OK, and nsurl_unref().

Referenced by nsgtk_viewfile(), and tab_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ window_init()

static nserror window_init ( const char *  title,
const char *  filename,
char *  ndata,
size_t  ndata_len 
)
static

View the data in a gtk text window.

Definition at line 351 of file viewdata.c.

References nsgtk_viewdata_ctx::builder, nsgtk_viewdata_ctx::data, nsgtk_viewdata_ctx::data_len, nsgtk_viewdata_ctx::filename, nsgtk_viewdata_ctx::gv, nsgtk_viewdata_ctx::next, NSERROR_INIT_FAILED, NSERROR_NOMEM, NSERROR_OK, nsgtk_attach_viewdata_menu_handlers(), nsgtk_builder_new_from_resname(), nsgtk_viewdata_delete_event(), nsgtk_viewdata_destroy_event(), nsgtk_viewdata_list, nsgtk_widget_modify_font(), NSLOG, nsgtk_viewdata_ctx::prev, and nsgtk_viewdata_ctx::window.

Referenced by nsgtk_viewdata(), and window_init_fname().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ window_init_fname()

static nserror window_init_fname ( const char *  title,
const char *  leafname,
const char *  filename 
)
static

open a window to dispaly an existing file.

Definition at line 451 of file viewdata.c.

References nsgtk_viewdata_ctx::filename, NSERROR_BAD_SIZE, NSERROR_NOT_FOUND, NSERROR_OK, and window_init().

Referenced by nsgtk_viewfile().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ xdg_data_strvec()

static char ** xdg_data_strvec ( void  )
static

Build string vector of search path.

${XDG_DATA_HOME:-$HOME/.local/share}:${XDG_DATA_DIRS:-/usr/local/share:/usr/share}

$XDG_DATA_HOME if empty use $HOME/.local/share

XDG_DATA_DIRS if empty use /usr/local/share/:/usr/share/

Returns
string vector of search pathnames or NULL on error.

Definition at line 575 of file viewdata.c.

References filepath_path_to_strvec(), NSLOG, and SLEN.

Referenced by editor_init_fname().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ xdg_get_default_app()

static char * xdg_get_default_app ( const char *  path,
const char *  mimetype 
)
static

Search application defaults file for matching mime type.

create filename form path and applications/defaults.list

look for [Default Applications] search lines looking like mime/type=Desktop

Parameters
pathThe base path.
mimetypeThe mimetype to search for.
Returns
The desktop file associated with the mime type or NULL if not found.

Definition at line 639 of file viewdata.c.

References line(), NSLOG, path(), and SLEN.

Referenced by editor_init_fname().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ xdg_get_exec_cmd()

static char * xdg_get_exec_cmd ( const char *  path,
const char *  desktop 
)
static

Search desktop file for an Exec line.

search path is combined with applications/application.desktop to create a filename.

Desktop file format http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html

Todo:
The parsing of the desktop file is badly incomplete and needs improving. For example the handling of the = delimiter is wrong and selection from the "Desktop Entry" group is completely absent.

Definition at line 706 of file viewdata.c.

References line(), NSLOG, path(), and SLEN.

Referenced by editor_init_fname().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ nsgtk_viewdata_list

struct nsgtk_viewdata_ctx* nsgtk_viewdata_list = NULL
static

◆ viewdata_menu_events

struct menu_events viewdata_menu_events[]
static
Initial value:
= {
MENUEVENT(viewdata_save_as),
MENUEVENT(viewdata_print),
MENUEVENT(viewdata_close),
MENUEVENT(viewdata_select_all),
MENUEVENT(viewdata_cut),
MENUEVENT(viewdata_copy),
MENUEVENT(viewdata_paste),
MENUEVENT(viewdata_delete),
MENUEVENT(viewdata_zoom_in),
MENUEVENT(viewdata_zoom_out),
MENUEVENT(viewdata_zoom_normal),
MENUEVENT(viewdata_about),
{NULL, NULL}
}
#define MENUEVENT(x)
Definition: viewdata.c:74

Definition at line 91 of file viewdata.c.

Referenced by nsgtk_attach_viewdata_menu_handlers().

◆ viewdata_zoomlevel

char viewdata_zoomlevel = 10
static