NetSurf
misc.c
Go to the documentation of this file.
1/*
2 * Copyright 2021 Vincemt Sanders <vince@netsurf-browser.org>
3 *
4 * This file is part of NetSurf, http://www.netsurf-browser.org/
5 *
6 * NetSurf is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * NetSurf is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19/**
20 * \file
21 * Implementation of netsurf miscellaneous operations table
22 */
23
24#include <string.h>
25#include <stdbool.h>
26#include <gtk/gtk.h>
27
28#include "utils/config.h"
29#include "utils/errors.h"
30#include "utils/log.h"
31#include "utils/messages.h"
32#include "utils/nsurl.h"
33#include "netsurf/misc.h"
34#include "desktop/save_pdf.h"
35
36#include "gtk/compat.h"
37#include "gtk/warn.h"
38#include "gtk/schedule.h"
39#include "gtk/resources.h"
40#include "gtk/cookies.h"
41#include "gtk/misc.h"
42
43
44static nserror gui_launch_url(struct nsurl *url)
45{
46 gboolean ok;
47 GError *error = NULL;
48
49 ok = nsgtk_show_uri(NULL, nsurl_access(url), GDK_CURRENT_TIME, &error);
50 if (ok == TRUE) {
51 return NSERROR_OK;
52 }
53
54 if (error) {
55 nsgtk_warning(messages_get("URIOpenError"), error->message);
56 g_error_free(error);
57 }
59}
60
61static void nsgtk_PDF_set_pass(GtkButton *w, gpointer data)
62{
63 char **owner_pass = ((void **)data)[0];
64 char **user_pass = ((void **)data)[1];
65 GtkWindow *wnd = ((void **)data)[2];
66 GtkBuilder *password_builder = ((void **)data)[3];
67 char *path = ((void **)data)[4];
68
69 char *op, *op1;
70 char *up, *up1;
71
72 op = strdup(gtk_entry_get_text(
73 GTK_ENTRY(gtk_builder_get_object(password_builder,
74 "entryPDFOwnerPassword"))));
75 op1 = strdup(gtk_entry_get_text(
76 GTK_ENTRY(gtk_builder_get_object(password_builder,
77 "entryPDFOwnerPassword1"))));
78 up = strdup(gtk_entry_get_text(
79 GTK_ENTRY(gtk_builder_get_object(password_builder,
80 "entryPDFUserPassword"))));
81 up1 = strdup(gtk_entry_get_text(
82 GTK_ENTRY(gtk_builder_get_object(password_builder,
83 "entryPDFUserPassword1"))));
84
85
86 if (op[0] == '\0') {
87 gtk_label_set_text(GTK_LABEL(gtk_builder_get_object(password_builder,
88 "labelInfo")),
89 "Owner password must be at least 1 character long:");
90 free(op);
91 free(up);
92 } else if (!strcmp(op, up)) {
93 gtk_label_set_text(GTK_LABEL(gtk_builder_get_object(password_builder,
94 "labelInfo")),
95 "User and owner passwords must be different:");
96 free(op);
97 free(up);
98 } else if (!strcmp(op, op1) && !strcmp(up, up1)) {
99
100 *owner_pass = op;
101 if (up[0] == '\0')
102 free(up);
103 else
104 *user_pass = up;
105
106 free(data);
107 gtk_widget_destroy(GTK_WIDGET(wnd));
108 g_object_unref(G_OBJECT(password_builder));
109
110 save_pdf(path);
111
112 free(path);
113 } else {
114 gtk_label_set_text(GTK_LABEL(gtk_builder_get_object(password_builder,
115 "labelInfo")), "Passwords not confirmed:");
116 free(op);
117 free(up);
118 }
119
120 free(op1);
121 free(up1);
122}
123
124static void nsgtk_PDF_no_pass(GtkButton *w, gpointer data)
125{
126 GtkWindow *wnd = ((void **)data)[2];
127 GtkBuilder *password_builder = ((void **)data)[3];
128 char *path = ((void **)data)[4];
129
130 free(data);
131
132 gtk_widget_destroy(GTK_WIDGET(wnd));
133 g_object_unref(G_OBJECT(password_builder));
134
135 save_pdf(path);
136
137 free(path);
138}
139
140static void nsgtk_pdf_password(char **owner_pass, char **user_pass, char *path)
141{
142 GtkButton *ok, *no;
143 GtkWindow *wnd;
144 void **data;
145 GtkBuilder *password_builder;
146 nserror res;
147
148 res = nsgtk_builder_new_from_resname("password", &password_builder);
149 if (res != NSERROR_OK) {
150 NSLOG(netsurf, INFO, "Password UI builder init failed");
151 return;
152 }
153
154 gtk_builder_connect_signals(password_builder, NULL);
155
156 wnd = GTK_WINDOW(gtk_builder_get_object(password_builder,
157 "wndPDFPassword"));
158
159 data = malloc(5 * sizeof(void *));
160
161 *owner_pass = NULL;
162 *user_pass = NULL;
163
164 data[0] = owner_pass;
165 data[1] = user_pass;
166 data[2] = wnd;
167 data[3] = password_builder;
168 data[4] = path;
169
170 ok = GTK_BUTTON(gtk_builder_get_object(password_builder,
171 "buttonPDFSetPassword"));
172 no = GTK_BUTTON(gtk_builder_get_object(password_builder,
173 "buttonPDFNoPassword"));
174
175 g_signal_connect(G_OBJECT(ok), "clicked",
176 G_CALLBACK(nsgtk_PDF_set_pass), (gpointer)data);
177 g_signal_connect(G_OBJECT(no), "clicked",
178 G_CALLBACK(nsgtk_PDF_no_pass), (gpointer)data);
179
180 gtk_widget_show(GTK_WIDGET(wnd));
181}
182
183
184static struct gui_misc_table misc_table = {
186
187 .launch_url = gui_launch_url,
188 .pdf_password = nsgtk_pdf_password,
189 .present_cookies = nsgtk_cookies_present,
190};
191
gboolean nsgtk_show_uri(GdkScreen *screen, const gchar *uri, guint32 timestamp, GError **error)
Definition: compat.c:426
Compatibility functions for older GTK versions (interface)
nserror save_pdf(const char *path)
Definition: save_pdf.c:992
PDF Plotting.
Error codes.
nserror
Enumeration of error codes.
Definition: errors.h:29
@ NSERROR_NO_FETCH_HANDLER
No fetch handler for URL scheme.
Definition: errors.h:33
@ NSERROR_OK
No error.
Definition: errors.h:30
nserror nsgtk_cookies_present(const char *search_term)
make the cookie window visible.
Definition: cookies.c:311
Cookies (interface).
nserror nsgtk_warning(const char *warning, const char *detail)
Warn the user of an event.
Definition: gui.c:96
static void nsgtk_PDF_no_pass(GtkButton *w, gpointer data)
Definition: misc.c:124
static nserror gui_launch_url(struct nsurl *url)
Definition: misc.c:44
static struct gui_misc_table misc_table
Definition: misc.c:184
static void nsgtk_PDF_set_pass(GtkButton *w, gpointer data)
Definition: misc.c:61
struct gui_misc_table * nsgtk_misc_table
Definition: misc.c:192
static void nsgtk_pdf_password(char **owner_pass, char **user_pass, char *path)
Definition: misc.c:140
nserror nsgtk_schedule(int t, void(*callback)(void *p), void *cbctx)
Definition: schedule.c:103
Interface to platform-specific miscellaneous browser operation table.
#define NSLOG(catname, level, logmsg, args...)
Definition: log.h:116
const char * messages_get(const char *key)
Fast lookup of a message by key from the standard Messages hash.
Definition: messages.c:241
Localised message support (interface).
NetSurf URL handling (interface).
const char * nsurl_access(const nsurl *url)
Access a NetSurf URL object as a string.
struct nsurl nsurl
NetSurf URL object.
Definition: nsurl.h:31
nserror nsgtk_builder_new_from_resname(const char *resname, GtkBuilder **builder_out)
Create gtk builder object for the named ui resource.
Definition: resources.c:526
Interface to gtk builtin resource handling.
Interface to utility string handling.
Graphical user interface browser misc function table.
Definition: misc.h:39
nserror(* schedule)(int t, void(*callback)(void *p), void *p)
Schedule a callback.
Definition: misc.h:58
static nserror path(const struct redraw_context *ctx, const plot_style_t *pstyle, const float *p, unsigned int n, const float transform[6])
Plots a path.
Definition: plot.c:821