1 |
/* wptCommonDlg.cpp |
2 |
* Copyright (C) 2004, 2005, 2006, 2009 Timo Schulz |
3 |
* |
4 |
* This file is part of WinPT. |
5 |
* |
6 |
* WinPT 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; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* WinPT is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
*/ |
16 |
#ifdef HAVE_CONFIG_H |
17 |
#include <config.h> |
18 |
#endif |
19 |
|
20 |
#include <windows.h> |
21 |
#include <ctype.h> |
22 |
|
23 |
#include "resource.h" |
24 |
#include "wptTypes.h" |
25 |
#include "wptW32API.h" |
26 |
#include "wptVersion.h" |
27 |
#include "wptGPG.h" |
28 |
#include "wptCommonCtl.h" |
29 |
#include "wptContext.h" |
30 |
#include "wptRegistry.h" |
31 |
|
32 |
|
33 |
/* check if the given buf contains a valid URL. |
34 |
for the moment we hardcode HTTP urls. */ |
35 |
static int |
36 |
check_URL (const char *buf) |
37 |
{ |
38 |
size_t i; |
39 |
const char *allowed = "-./#_:~&?="; |
40 |
|
41 |
if (strncmp (buf, "http", 4) && strncmp (buf, "x-hkp", 5)) |
42 |
return -1; |
43 |
if (!strchr (buf, ':')) |
44 |
return -1; |
45 |
if (!strstr (buf, "//")) |
46 |
return -1; |
47 |
if (strstr (buf, " ")) |
48 |
return -1; |
49 |
buf += 7; |
50 |
for (i=0; i < strlen (buf); i++) { |
51 |
if (isalpha (buf[i]) || isdigit (buf[i])) |
52 |
continue; |
53 |
if (!strchr (allowed, buf[i])) |
54 |
return -1; |
55 |
} |
56 |
|
57 |
return 0; |
58 |
} |
59 |
|
60 |
|
61 |
static BOOL CALLBACK |
62 |
http_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
63 |
{ |
64 |
static URL_ctx_t ctx; |
65 |
int n; |
66 |
|
67 |
switch (msg) { |
68 |
case WM_INITDIALOG: |
69 |
ctx = (struct URL_ctx_s*)lparam; |
70 |
if (!ctx) |
71 |
BUG (0); |
72 |
if (ctx->desc != NULL) |
73 |
SetWindowText (dlg, ctx->desc); |
74 |
if (ctx->title != NULL) |
75 |
SetDlgItemText (dlg, IDC_HTTP_TITLE, ctx->title); |
76 |
SetDlgItemText (dlg, IDCANCEL, _("&Cancel")); |
77 |
SetForegroundWindow (dlg); |
78 |
break; |
79 |
|
80 |
case WM_COMMAND: |
81 |
switch (LOWORD (wparam)) { |
82 |
case IDOK: |
83 |
n = GetDlgItemText (dlg, IDC_HTTP_URL, ctx->url, DIM (ctx->url)-1); |
84 |
if (ctx->check && (!n || check_URL (ctx->url))) { |
85 |
msg_box (dlg, _("Please enter a valid URL."), "HTTP", MB_ERR); |
86 |
return FALSE; |
87 |
} |
88 |
if (ctx->check && ctx->url[strlen (ctx->url)-1] == '/') |
89 |
ctx->url[strlen (ctx->url)-1]=0; |
90 |
ctx->cancel = 0; |
91 |
EndDialog (dlg, TRUE); |
92 |
break; |
93 |
|
94 |
case IDCANCEL: |
95 |
ctx->cancel = 1; |
96 |
EndDialog (dlg, FALSE); |
97 |
break; |
98 |
} |
99 |
} |
100 |
|
101 |
return FALSE; |
102 |
} |
103 |
|
104 |
|
105 |
/* Common dialog to request a URL from the user. |
106 |
Return value is the URL context. */ |
107 |
void* |
108 |
get_http_file_dlg (HWND root) |
109 |
{ |
110 |
URL_ctx_t ctx; |
111 |
|
112 |
ctx = new URL_ctx_s; |
113 |
if (!ctx) |
114 |
BUG (0); |
115 |
memset (ctx, 0, sizeof *ctx); |
116 |
ctx->check = 1; |
117 |
ctx->desc = _("HTTP Key Import"); |
118 |
ctx->title = _("Enter URL to retrieve the public key"); |
119 |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_HTTP, root, |
120 |
http_dlg_proc, (LPARAM)ctx); |
121 |
return ctx; |
122 |
} |
123 |
|
124 |
|
125 |
/* Wrapper to request a preferred keyserver from the user. |
126 |
Return value is the URL context. */ |
127 |
void* |
128 |
get_keyserver_URL_dlg (HWND root) |
129 |
{ |
130 |
URL_ctx_t ctx; |
131 |
|
132 |
ctx = new URL_ctx_s; |
133 |
if (!ctx) |
134 |
BUG (0); |
135 |
memset (ctx, 0, sizeof *ctx); |
136 |
ctx->check = 1; |
137 |
ctx->desc = _("Key Edit"); |
138 |
ctx->title = _("Enter preferred keyserver URL"); |
139 |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_HTTP, root, |
140 |
http_dlg_proc, (LPARAM)ctx); |
141 |
return ctx; |
142 |
} |
143 |
|
144 |
|
145 |
/* Generic text input dialog. */ |
146 |
char* |
147 |
get_input_dialog (HWND root, const char *title, const char *desc) |
148 |
{ |
149 |
struct URL_ctx_s ctx; |
150 |
|
151 |
memset (&ctx, 0, sizeof (ctx)); |
152 |
ctx.check = 0; /* Do not perform any checks on the data. */ |
153 |
ctx.title = title; |
154 |
ctx.desc = desc; |
155 |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_HTTP, root, |
156 |
http_dlg_proc, (LPARAM)&ctx); |
157 |
if (ctx.cancel || strlen (ctx.url) == 0) |
158 |
return NULL; |
159 |
return m_strdup (ctx.url); |
160 |
} |