1 |
werner |
36 |
/* wptCommonDlg.cpp |
2 |
twoaday |
328 |
* Copyright (C) 2004, 2005, 2006, 2009 Timo Schulz |
3 |
werner |
36 |
* |
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 |
twoaday |
262 |
|
20 |
|
|
#include <windows.h> |
21 |
twoaday |
211 |
#include <ctype.h> |
22 |
werner |
36 |
|
23 |
werner |
47 |
#include "resource.h" |
24 |
werner |
36 |
#include "wptTypes.h" |
25 |
|
|
#include "wptW32API.h" |
26 |
|
|
#include "wptVersion.h" |
27 |
|
|
#include "wptGPG.h" |
28 |
|
|
#include "wptCommonCtl.h" |
29 |
|
|
#include "wptContext.h" |
30 |
twoaday |
193 |
#include "wptRegistry.h" |
31 |
werner |
36 |
|
32 |
|
|
|
33 |
twoaday |
262 |
/* check if the given buf contains a valid URL. |
34 |
|
|
for the moment we hardcode HTTP urls. */ |
35 |
werner |
36 |
static int |
36 |
|
|
check_URL (const char *buf) |
37 |
|
|
{ |
38 |
twoaday |
181 |
size_t i; |
39 |
twoaday |
211 |
const char *allowed = "-./#_:~&?="; |
40 |
twoaday |
181 |
|
41 |
twoaday |
328 |
if (strncmp (buf, "http", 4) && strncmp (buf, "x-hkp", 5)) |
42 |
twoaday |
262 |
return -1; |
43 |
werner |
36 |
if (!strchr (buf, ':')) |
44 |
|
|
return -1; |
45 |
|
|
if (!strstr (buf, "//")) |
46 |
|
|
return -1; |
47 |
twoaday |
181 |
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 |
werner |
36 |
return 0; |
58 |
|
|
} |
59 |
|
|
|
60 |
|
|
|
61 |
|
|
static BOOL CALLBACK |
62 |
|
|
http_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
63 |
|
|
{ |
64 |
twoaday |
328 |
static URL_ctx_t ctx; |
65 |
werner |
36 |
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 |
twoaday |
123 |
SetDlgItemText (dlg, IDCANCEL, _("&Cancel")); |
77 |
werner |
36 |
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 |
twoaday |
328 |
return FALSE; |
87 |
werner |
36 |
} |
88 |
twoaday |
328 |
if (ctx->check && ctx->url[strlen (ctx->url)-1] == '/') |
89 |
|
|
ctx->url[strlen (ctx->url)-1]=0; |
90 |
werner |
36 |
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 |
twoaday |
328 |
URL_ctx_t ctx; |
111 |
werner |
36 |
|
112 |
|
|
ctx = new URL_ctx_s; |
113 |
|
|
if (!ctx) |
114 |
|
|
BUG (0); |
115 |
|
|
memset (ctx, 0, sizeof *ctx); |
116 |
twoaday |
181 |
ctx->check = 1; |
117 |
|
|
ctx->desc = _("HTTP Key Import"); |
118 |
|
|
ctx->title = _("Enter URL to retrieve the public key"); |
119 |
werner |
36 |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_HTTP, root, |
120 |
|
|
http_dlg_proc, (LPARAM)ctx); |
121 |
|
|
return ctx; |
122 |
|
|
} |
123 |
|
|
|
124 |
twoaday |
328 |
|
125 |
werner |
36 |
/* 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 |
twoaday |
328 |
URL_ctx_t ctx; |
131 |
werner |
36 |
|
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 |
twoaday |
328 |
ctx.check = 0; /* Do not perform any checks on the data. */ |
153 |
werner |
36 |
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 |
|
|
} |