1 |
werner |
36 |
/* wptClipEditDlg.cpp - Clipboard Editor dialog |
2 |
twoaday |
225 |
* Copyright (C) 2000-2006 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 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with WinPT; if not, write to the Free Software Foundation, |
18 |
|
|
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
19 |
|
|
*/ |
20 |
|
|
#ifdef HAVE_CONFIG_H |
21 |
|
|
#include <config.h> |
22 |
|
|
#endif |
23 |
|
|
|
24 |
|
|
#include <windows.h> |
25 |
|
|
#include <stdio.h> |
26 |
|
|
|
27 |
|
|
#include "wptTypes.h" |
28 |
|
|
#include "wptW32API.h" |
29 |
|
|
#include "wptVersion.h" |
30 |
|
|
#include "wptErrors.h" |
31 |
|
|
#include "wptGPG.h" |
32 |
|
|
#include "wptNLS.h" |
33 |
werner |
47 |
#include "resource.h" |
34 |
twoaday |
229 |
#include "wptCommonCtl.h" |
35 |
|
|
#include "wptContext.h" |
36 |
|
|
#include "wptKeylist.h" |
37 |
werner |
36 |
|
38 |
twoaday |
120 |
#ifdef _MSC_VER |
39 |
|
|
#include "winpt_header.h" |
40 |
twoaday |
121 |
|
41 |
twoaday |
120 |
static DWORD help_arr[] = { |
42 |
twoaday |
121 |
IDC_CLIPEDIT_SEND, WPT_CLIPEDIT_COPY, |
43 |
|
|
IDC_CLIPEDIT_CLEAR, WPT_CLIPEDIT_CLEAR, |
44 |
|
|
IDC_CLIPEDIT_LOAD, WPT_CLIPEDIT_LOAD, |
45 |
|
|
IDC_CLIPEDIT_SAVE, WPT_CLIPEDIT_SAVE, |
46 |
|
|
IDC_CLIPEDIT_QUOTE, WPT_CLIPEDIT_QUOTE, |
47 |
|
|
0, 0}; |
48 |
twoaday |
120 |
#endif |
49 |
werner |
36 |
|
50 |
twoaday |
225 |
|
51 |
werner |
36 |
/* Load the clipboard contents into the text field in @dlg. |
52 |
twoaday |
229 |
Optinally @header and @footer can be used to set a header and footer. |
53 |
werner |
36 |
Return value: 0 on success. */ |
54 |
|
|
static int |
55 |
twoaday |
229 |
load_clipboard (HWND dlg, |
56 |
|
|
const char *header, const char *footer) |
57 |
werner |
36 |
{ |
58 |
|
|
HANDLE clipmem; |
59 |
|
|
char *cliptext; |
60 |
twoaday |
229 |
char *p, *pp; |
61 |
werner |
36 |
|
62 |
|
|
CloseClipboard (); /* make sure it is closed. */ |
63 |
|
|
if (OpenClipboard (NULL) == FALSE) { |
64 |
|
|
msg_box (dlg, winpt_strerror (WPTERR_CLIP_OPEN), _("Clipboard"), MB_ERR); |
65 |
|
|
return -1; |
66 |
|
|
} |
67 |
|
|
clipmem = GetClipboardData (CF_TEXT); |
68 |
|
|
if (clipmem == NULL) { |
69 |
|
|
CloseClipboard (); |
70 |
|
|
return -1; |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
cliptext = (char *) GlobalLock (clipmem); |
74 |
|
|
if (cliptext == NULL || !strlen (cliptext)) { |
75 |
|
|
CloseClipboard (); |
76 |
|
|
return -1; |
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
p = new char [strlen (cliptext)+2]; |
80 |
|
|
if (!p) |
81 |
|
|
BUG (0); |
82 |
|
|
strcpy (p, cliptext); |
83 |
|
|
|
84 |
|
|
GlobalUnlock (clipmem); |
85 |
twoaday |
225 |
CloseClipboard (); |
86 |
twoaday |
229 |
|
87 |
|
|
if (header && footer) { |
88 |
|
|
const char *fmt = "%s\r\n%s\r\n%s"; |
89 |
twoaday |
231 |
int len; |
90 |
twoaday |
229 |
|
91 |
twoaday |
231 |
len = strlen (header) + strlen (footer) + strlen (fmt) + strlen (p); |
92 |
twoaday |
229 |
pp = new char[len + 1]; |
93 |
|
|
if (!pp) |
94 |
|
|
BUG (0); |
95 |
|
|
_snprintf (pp, len, fmt, header, p, footer); |
96 |
|
|
SetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, pp); |
97 |
|
|
free_if_alloc (pp); |
98 |
|
|
} |
99 |
|
|
else |
100 |
|
|
SetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, p); |
101 |
werner |
36 |
free_if_alloc (p); |
102 |
|
|
return 0; |
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
|
106 |
|
|
/* Load the clipboard from the selected file. */ |
107 |
|
|
static int |
108 |
|
|
load_clipboard_from_file (HWND dlg) |
109 |
|
|
{ |
110 |
|
|
OPENFILENAME open; |
111 |
|
|
FILE *fp; |
112 |
|
|
char *p; |
113 |
|
|
char file[300] = ""; |
114 |
|
|
DWORD size; |
115 |
|
|
int id; |
116 |
|
|
|
117 |
|
|
memset (&open, 0, sizeof (open)); |
118 |
twoaday |
225 |
open.lStructSize = sizeof (OPENFILENAME); |
119 |
|
|
open.hInstance = glob_hinst; |
120 |
|
|
open.lpstrTitle = _("File Open"); |
121 |
|
|
open.lpstrFilter = "All Files (*.*)\0*.*\0\0"; |
122 |
werner |
36 |
open.hwndOwner = dlg; |
123 |
|
|
open.lpstrFile = file; |
124 |
|
|
open.nMaxFile = sizeof (file)-1; |
125 |
|
|
open.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST; |
126 |
|
|
|
127 |
|
|
if (GetOpenFileName (&open)) { |
128 |
twoaday |
225 |
if( (size=get_file_size (file)) > MAX_CLIPTEXT_SIZE) { |
129 |
werner |
36 |
id = msg_box (dlg, _("The file you want to add is very large.\n" |
130 |
twoaday |
105 |
"Still proceed?"), _("Clipboard"), |
131 |
|
|
MB_INFO|MB_YESNO); |
132 |
|
|
if (id == IDNO) |
133 |
werner |
36 |
return -1; |
134 |
|
|
} |
135 |
|
|
fp = fopen (file, "rb"); |
136 |
|
|
if (!fp) { |
137 |
twoaday |
105 |
msg_box (dlg, winpt_strerror (WPTERR_FILE_OPEN), |
138 |
|
|
_("Clipboard"), MB_ERR); |
139 |
werner |
36 |
return FALSE; |
140 |
|
|
} |
141 |
|
|
p = new char[size+1]; |
142 |
|
|
if (!p) |
143 |
|
|
BUG (0); |
144 |
twoaday |
225 |
memset (p, 0, size+1); |
145 |
|
|
size = fread (p, 1, size, fp); |
146 |
werner |
36 |
fclose (fp); |
147 |
|
|
SetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, p); |
148 |
|
|
free_if_alloc (p); |
149 |
|
|
} |
150 |
|
|
return 0; |
151 |
|
|
} |
152 |
|
|
|
153 |
|
|
|
154 |
|
|
/* Save the contents of the clipboard to the selected file. */ |
155 |
|
|
static int |
156 |
|
|
save_clipboard_to_file (HWND dlg) |
157 |
|
|
{ |
158 |
|
|
OPENFILENAME save; |
159 |
|
|
FILE *fp; |
160 |
twoaday |
65 |
char *p=NULL; |
161 |
werner |
36 |
char file[300] = ""; |
162 |
|
|
DWORD nbytes; |
163 |
|
|
int id; |
164 |
|
|
|
165 |
|
|
memset (&save, 0, sizeof (save)); |
166 |
|
|
save.lStructSize = sizeof (OPENFILENAME); |
167 |
|
|
save.hInstance = glob_hinst; |
168 |
|
|
save.lpstrTitle = _("File Save"); |
169 |
twoaday |
167 |
save.lpstrFile = (char *)"All Files (*.*)\0*.*\0\0"; |
170 |
werner |
36 |
save.hwndOwner = dlg; |
171 |
|
|
save.lpstrFile = file; |
172 |
|
|
save.nMaxFile = sizeof (file) - 1; |
173 |
|
|
save.Flags = OFN_OVERWRITEPROMPT; |
174 |
twoaday |
225 |
|
175 |
|
|
if (GetSaveFileName (&save)) { |
176 |
|
|
if (file_exist_check (file) == 0) { |
177 |
werner |
36 |
id = log_box (_("Clipboard"), MB_YESNO, |
178 |
|
|
_("\"%s\" already exists.\n" |
179 |
|
|
"Replace existing file?"), file); |
180 |
|
|
if( id == IDNO ) |
181 |
|
|
return FALSE; |
182 |
|
|
} |
183 |
|
|
fp = fopen (file, "wb"); |
184 |
|
|
if (!fp) { |
185 |
twoaday |
105 |
msg_box (dlg, winpt_strerror (WPTERR_FILE_CREAT), |
186 |
|
|
_("Clipboard"), MB_ERR); |
187 |
werner |
36 |
return FALSE; |
188 |
|
|
} |
189 |
|
|
/* XXX: there is a NUL-byte at the end. */ |
190 |
twoaday |
105 |
nbytes = SendDlgItemMessage (dlg, IDC_CLIPEDIT_CLIPTEXT2, |
191 |
|
|
WM_GETTEXTLENGTH, 0, 0); |
192 |
werner |
36 |
if (nbytes > 0) { |
193 |
|
|
p = new char[nbytes+1]; |
194 |
|
|
if (!p) |
195 |
|
|
BUG (NULL); |
196 |
|
|
GetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, p, nbytes); |
197 |
|
|
fwrite (p, 1, nbytes, fp); |
198 |
twoaday |
105 |
|
199 |
werner |
36 |
} |
200 |
|
|
fclose (fp); |
201 |
|
|
free_if_alloc (p); |
202 |
|
|
} |
203 |
|
|
return 0; |
204 |
|
|
} |
205 |
|
|
|
206 |
|
|
|
207 |
twoaday |
229 |
void verify_get_clip_info (gpgme_signature_t sig, |
208 |
|
|
char **r_header, char **r_footer); |
209 |
|
|
|
210 |
werner |
36 |
/* Dialog box procedure for the clipboard editor. */ |
211 |
|
|
BOOL CALLBACK |
212 |
|
|
clip_edit_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
213 |
|
|
{ |
214 |
twoaday |
229 |
char *head=NULL, *foot=NULL; |
215 |
|
|
|
216 |
werner |
36 |
switch (msg) { |
217 |
|
|
case WM_INITDIALOG: |
218 |
twoaday |
231 |
/* XXX: it would be really nice to have tempest prevention fonts. */ |
219 |
werner |
36 |
SetWindowText (dlg, _("Clipboard Editor")); |
220 |
|
|
SetDlgItemText (dlg, IDC_CLIPEDIT_SEND, _("&Copy")); |
221 |
|
|
SetDlgItemText (dlg, IDC_CLIPEDIT_CLEAR, _("Clea&r")); |
222 |
|
|
SetDlgItemText (dlg, IDC_CLIPEDIT_LOAD, _("&Load")); |
223 |
|
|
SetDlgItemText (dlg, IDC_CLIPEDIT_SAVE, _("&Save")); |
224 |
twoaday |
105 |
SetDlgItemText (dlg, IDC_CLIPEDIT_QUOTE, _("Add quotes")); |
225 |
twoaday |
121 |
SetDlgItemText (dlg, IDOK, _("&Close")); |
226 |
twoaday |
229 |
/* If there is a param, we expect a signature and initialize |
227 |
|
|
the header and footer variable accordingly. */ |
228 |
|
|
if (lparam) |
229 |
|
|
verify_get_clip_info ((gpgme_signature_t)lparam, &head, &foot); |
230 |
|
|
load_clipboard (dlg, head, foot); |
231 |
|
|
free_if_alloc (head); |
232 |
|
|
free_if_alloc (foot); |
233 |
werner |
36 |
center_window (dlg, NULL); |
234 |
|
|
SetForegroundWindow (dlg); |
235 |
|
|
return TRUE; |
236 |
twoaday |
201 |
|
237 |
twoaday |
121 |
case WM_HELP: |
238 |
|
|
html_help_dispatch (lparam, "winpt.chm::winpt_texts.txt", help_arr); |
239 |
|
|
break; |
240 |
werner |
36 |
|
241 |
|
|
case WM_COMMAND: |
242 |
|
|
switch (LOWORD (wparam)) { |
243 |
twoaday |
201 |
case IDCANCEL: |
244 |
|
|
EndDialog (dlg, FALSE); |
245 |
|
|
return TRUE; |
246 |
|
|
|
247 |
werner |
36 |
case IDOK: |
248 |
|
|
EndDialog (dlg, TRUE); |
249 |
|
|
return TRUE; |
250 |
|
|
|
251 |
|
|
case IDC_CLIPEDIT_SEND: |
252 |
|
|
if (IsDlgButtonChecked (dlg, IDC_CLIPEDIT_QUOTE)) { |
253 |
|
|
gpgme_data_t txt; |
254 |
|
|
gpg_data_new_from_clipboard (&txt, 0); |
255 |
|
|
gpg_data_mail_quote (&txt); |
256 |
|
|
gpg_data_release_and_set_clipboard (txt, 0); |
257 |
|
|
SendMessage (dlg, WM_INITDIALOG, 0, 0); |
258 |
|
|
return TRUE; |
259 |
|
|
} |
260 |
|
|
SendDlgItemMessage (dlg, IDC_CLIPEDIT_CLIPTEXT2, EM_SETSEL, 0, -1); |
261 |
|
|
SendDlgItemMessage (dlg, IDC_CLIPEDIT_CLIPTEXT2, WM_COPY, 0, 0); |
262 |
|
|
return TRUE; |
263 |
|
|
|
264 |
|
|
case IDC_CLIPEDIT_CLEAR: |
265 |
|
|
if (OpenClipboard (NULL) == FALSE) { |
266 |
|
|
msg_box (dlg, winpt_strerror (WPTERR_CLIP_OPEN), |
267 |
|
|
_("Clipboard"), MB_ERR); |
268 |
|
|
return TRUE; |
269 |
|
|
} |
270 |
|
|
if (EmptyClipboard () == FALSE) |
271 |
|
|
msg_box (dlg, winpt_strerror (WPTERR_CLIP_EMPTY), |
272 |
|
|
_("Clipboard"), MB_ERR); |
273 |
|
|
else |
274 |
|
|
SetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, ""); |
275 |
|
|
CloseClipboard (); |
276 |
|
|
return TRUE; |
277 |
|
|
|
278 |
|
|
case IDC_CLIPEDIT_LOAD: |
279 |
|
|
if (!load_clipboard_from_file (dlg)) { |
280 |
twoaday |
225 |
CheckDlgButton (dlg, IDC_CLIPEDIT_QUOTE, BST_UNCHECKED); |
281 |
|
|
PostMessage (dlg, WM_COMMAND, MAKEWPARAM (IDC_CLIPEDIT_SEND, 0), 0); |
282 |
werner |
36 |
} |
283 |
|
|
return TRUE; |
284 |
|
|
|
285 |
|
|
case IDC_CLIPEDIT_SAVE: |
286 |
|
|
save_clipboard_to_file (dlg); |
287 |
|
|
return TRUE; |
288 |
|
|
} |
289 |
|
|
break; |
290 |
|
|
} |
291 |
|
|
|
292 |
|
|
return FALSE; |
293 |
|
|
} |