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