summaryrefslogtreecommitdiff
path: root/platform/gl/gl-form.c
blob: 123e8769bc54876dc4d89058b8648bd22f25fc77 (plain)
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
#include "gl-app.h"

#include <string.h>
#include <stdio.h>

#ifndef PATH_MAX
#define PATH_MAX 2048
#endif

#include "mupdf/helpers/pkcs7-check.h"
#include "mupdf/helpers/pkcs7-openssl.h"

static char cert_filename[PATH_MAX];
static struct input cert_password;

static void do_sign(void)
{
#ifdef HAVE_LIBCRYPTO
	pdf_pkcs7_signer *signer = NULL;

	fz_var(signer);

	fz_try(ctx)
	{
		signer = pkcs7_openssl_read_pfx(ctx, cert_filename, cert_password.text);
		pdf_sign_signature(ctx, pdf, selected_annot, signer);
		ui_show_warning_dialog("Signed document successfully.");
	}
	fz_always(ctx)
	{
		if (signer)
			signer->drop(signer);
	}
	fz_catch(ctx)
		ui_show_warning_dialog("%s", fz_caught_message(ctx));

	if (pdf_update_page(ctx, selected_annot->page))
		render_page();
#else
	ui_show_warning_dialog("Document not signed as no LIBCRYPTO.");
#endif
}

static void cert_password_dialog(void)
{
	int is;
	ui_dialog_begin(400, (ui.gridsize+4)*3);
	{
		ui_layout(T, X, NW, 2, 2);
		ui_label("Password:");
		is = ui_input(&cert_password, 200, 1);

		ui_layout(B, X, NW, 2, 2);
		ui_panel_begin(0, ui.gridsize, 0, 0, 0);
		{
			ui_layout(R, NONE, S, 0, 0);
			if (ui_button("Cancel"))
				ui.dialog = NULL;
			ui_spacer();
			if (ui_button("Okay") || is == UI_INPUT_ACCEPT)
			{
				ui.dialog = NULL;
				do_sign();
			}
		}
		ui_panel_end();
	}
	ui_dialog_end();
}

static int cert_file_filter(const char *fn)
{
	return !!strstr(fn, ".pfx");
}

static void cert_file_dialog(void)
{
	if (ui_open_file(cert_filename))
	{
		if (cert_filename[0] != 0)
		{
			ui_input_init(&cert_password, "");
			ui.focus = &cert_password;
			ui.dialog = cert_password_dialog;
		}
		else
			ui.dialog = NULL;
	}
}

void do_widget_panel(void)
{
	int ff, type;
	char *value;

	ff = pdf_get_field_flags(ctx, selected_annot->page->doc, selected_annot->obj);
	type = pdf_field_type(ctx, selected_annot->page->doc, selected_annot->obj);

	if (type == PDF_WIDGET_TYPE_TEXT)
	{
		static pdf_annot *last_annot = NULL;
		static struct input input;
		ui_label("Value:");
		if (selected_annot != last_annot)
		{
			last_annot = selected_annot;
			value = pdf_field_value(ctx, selected_annot->page->doc, selected_annot->obj);
			ui_input_init(&input, value);
			fz_free(ctx, value);
		}
		if (ui_input(&input, 0, (ff & Ff_Multiline) ? 5 : 1) >= UI_INPUT_EDIT)
		{
			pdf_field_set_value(ctx, selected_annot->page->doc, selected_annot->obj, input.text);
			if (pdf_update_page(ctx, selected_annot->page))
				render_page();
		}
	}
	else if (type == PDF_WIDGET_TYPE_COMBOBOX || type == PDF_WIDGET_TYPE_LISTBOX)
	{
		const char **options;
		int n, choice;
		ui_label("Value:");
		n = pdf_choice_widget_options(ctx, selected_annot->page->doc, selected_annot, 0, NULL);
		options = fz_malloc_array(ctx, n, sizeof(char*));
		pdf_choice_widget_options(ctx, selected_annot->page->doc, selected_annot, 0, options);
		value = pdf_field_value(ctx, selected_annot->page->doc, selected_annot->obj);
		choice = ui_select("Widget/Ch", value, options, n);
		if (choice >= 0)
		{
			pdf_field_set_value(ctx, selected_annot->page->doc, selected_annot->obj, options[choice]);
			if (pdf_update_page(ctx, selected_annot->page))
				render_page();
		}
		fz_free(ctx, value);
		fz_free(ctx, options);
	}
	else if (type == PDF_WIDGET_TYPE_SIGNATURE)
	{
		if (ui_button("Verify"))
		{
			char status[100];
			int result;
			result = pdf_check_signature(ctx, pdf, selected_annot, status, sizeof status);
			if (result)
				ui_show_warning_dialog("Signature is valid.\n%s", status);
			else
				ui_show_warning_dialog("Could not verify signature:\n%s", status);
		}
		if (ui_button("Sign"))
		{
			fz_strlcpy(cert_filename, filename, sizeof cert_filename);
			ui_init_open_file(".", cert_file_filter);
			ui.dialog = cert_file_dialog;
		}
	}
	else
	{
		value = pdf_field_value(ctx, pdf, selected_annot->obj);
		ui_label("Value: %s", value);
		fz_free(ctx, value);
	}
}