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