1 |
/* wptFileSaveDlg.cpp - Extract plaintext and save it to a file |
2 |
* Copyright (C) 2002, 2003, 2005, 2006 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 |
|
22 |
#include "resource.h" |
23 |
#include "wptTypes.h" |
24 |
#include "wptNLS.h" |
25 |
#include "wptGPG.h" |
26 |
#include "wptW32API.h" |
27 |
#include "wptErrors.h" |
28 |
|
29 |
|
30 |
static void |
31 |
switch_buttons (HWND dlg, int file) |
32 |
{ |
33 |
int check1 = file? BST_CHECKED : BST_UNCHECKED; |
34 |
int check2 = file? BST_UNCHECKED: BST_CHECKED; |
35 |
|
36 |
CheckDlgButton (dlg, IDC_FILE_SAVE_TOFILE, check1); |
37 |
CheckDlgButton (dlg, IDC_FILE_SAVE_CLIP, check2); |
38 |
} |
39 |
|
40 |
|
41 |
/* Dialog box procedure to save the plaintext of a signature |
42 |
plus some additional information. */ |
43 |
BOOL CALLBACK |
44 |
file_save_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
45 |
{ |
46 |
gpgme_data_t sig, plain; |
47 |
gpgme_error_t err; |
48 |
const char *filename; |
49 |
int has_data; |
50 |
char file[MAX_PATH+128]; |
51 |
|
52 |
switch (msg) { |
53 |
case WM_INITDIALOG: |
54 |
SetWindowText (dlg, _("Save Plaintext")); |
55 |
SetDlgItemText (dlg, IDCANCEL, _("&Cancel")); |
56 |
SetDlgItemText (dlg, IDC_FILE_SAVE, _("&Save additional information")); |
57 |
SetDlgItemText (dlg, IDC_FILE_SAVE_TOFILE, _("Save to &file")); |
58 |
SetDlgItemText (dlg, IDC_FILE_SAVE_CLIP, _("Send to &clipboard")); |
59 |
switch_buttons (dlg, 1); |
60 |
center_window (dlg, NULL); |
61 |
SetForegroundWindow (dlg); |
62 |
return TRUE; |
63 |
|
64 |
case WM_COMMAND: |
65 |
switch (LOWORD (wparam)) { |
66 |
case IDC_FILE_SAVE_SELECT: |
67 |
filename = get_filesave_dlg (dlg, _("Destination for Plaintext"), |
68 |
NULL, NULL); |
69 |
if (filename) { |
70 |
SetDlgItemText (dlg, IDC_FILE_SAVE_FILE, filename); |
71 |
switch_buttons (dlg, 1); |
72 |
} |
73 |
return TRUE; |
74 |
|
75 |
case IDOK: |
76 |
if (IsDlgButtonChecked (dlg, IDC_FILE_SAVE_TOFILE)) { |
77 |
if (!GetDlgItemText (dlg, IDC_FILE_SAVE_FILE, file, sizeof (file) - 1)) { |
78 |
msg_box (dlg, _("Please enter a filename."), _("Save Plaintext"), MB_INFO); |
79 |
return TRUE; |
80 |
} |
81 |
if (check_file_name (file, IS_PATH)) { |
82 |
msg_box (dlg, _("The file name contains one or more illegal characters."), |
83 |
_("Save Plaintext"), MB_ERR); |
84 |
return TRUE; |
85 |
} |
86 |
err = gpg_data_new_from_clipboard (&sig, 0); |
87 |
if (err) { |
88 |
msg_box (dlg, winpt_strerror (WPTERR_CLIP_GET), |
89 |
_("Save Plaintext"), MB_ERR); |
90 |
return TRUE; |
91 |
} |
92 |
gpg_data_extract_plaintext (sig, &plain); |
93 |
err = gpg_data_release_and_set_file (plain, file); |
94 |
if (!err) |
95 |
msg_box (dlg, _("Finished"), _("Save Plaintext"), MB_OK); |
96 |
else { |
97 |
msg_box (dlg, gpgme_strerror (err), _("Save Plaintext"), MB_ERR); |
98 |
gpgme_data_release (plain); |
99 |
} |
100 |
gpgme_data_release (sig); |
101 |
} |
102 |
else if (IsDlgButtonChecked (dlg, IDC_FILE_SAVE_CLIP)) { |
103 |
if (!gpg_clip_istext_avail (&has_data) && !has_data) { |
104 |
msg_box (dlg, winpt_strerror (WPTERR_CLIP_ISEMPTY), |
105 |
_("Save Plaintext"), MB_ERR); |
106 |
return FALSE; |
107 |
} |
108 |
err = gpg_data_new_from_clipboard (&sig, 0); |
109 |
if (err) { |
110 |
msg_box (dlg, winpt_strerror (WPTERR_CLIP_GET), |
111 |
_("Save Plaintext"), MB_ERR); |
112 |
return FALSE; |
113 |
} |
114 |
gpg_data_extract_plaintext (sig, &plain); |
115 |
gpg_data_release_to_clipboard (plain, 0); |
116 |
gpgme_data_release (sig); |
117 |
msg_box (dlg, _("Finished"), _("Save Plaintext"), MB_OK); |
118 |
} |
119 |
EndDialog (dlg, TRUE); |
120 |
return TRUE; |
121 |
|
122 |
case IDCANCEL: |
123 |
EndDialog (dlg, FALSE); |
124 |
return FALSE; |
125 |
} |
126 |
break; |
127 |
} |
128 |
|
129 |
return FALSE; |
130 |
} |