1 |
/* wptFileMangerDlg.cpp - File Manager |
2 |
* Copyright (C) 2001-2005 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 |
* 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 |
|
21 |
#ifdef HAVE_CONFIG_H |
22 |
#include <config.h> |
23 |
#endif |
24 |
|
25 |
#include <windows.h> |
26 |
#include <commctrl.h> |
27 |
#include <sys/stat.h> |
28 |
#include <stdio.h> |
29 |
|
30 |
#include "resource.h" |
31 |
#include "wptCommonCtl.h" |
32 |
#include "wptGPG.h" |
33 |
#include "wptW32API.h" |
34 |
#include "wptKeylist.h" |
35 |
#include "wptErrors.h" |
36 |
#include "wptContext.h" /* for passphrase_s */ |
37 |
#include "wptDlgs.h" |
38 |
#include "wptTypes.h" |
39 |
#include "wptNLS.h" |
40 |
#include "wptVersion.h" |
41 |
#include "wptFileManager.h" |
42 |
#include "wptRegistry.h" |
43 |
#include "wptCrypto.h" |
44 |
|
45 |
#define FM_SEPARATOR_ID 10001 |
46 |
|
47 |
#define send_cmd_id(hwnd, id) \ |
48 |
PostMessage ((hwnd), WM_COMMAND, MAKEWPARAM( (id), 0), 0) |
49 |
|
50 |
static subclass_s filelist_proc; |
51 |
|
52 |
struct thread_ctx_s { |
53 |
HWND dlg; |
54 |
HANDLE fd; |
55 |
char *drive; |
56 |
}; |
57 |
typedef struct thread_ctx_s *thread_ctx_t; |
58 |
|
59 |
|
60 |
int is_openpgp_ext (const char *name); |
61 |
|
62 |
/* Return 1 if the selected file in the listview has a valid GPG extension. */ |
63 |
static int |
64 |
is_openpgp_file (listview_ctrl_t lv) |
65 |
{ |
66 |
char name[MAX_PATH+32]; |
67 |
|
68 |
listview_get_item_text (lv, listview_get_curr_pos (lv), 1, |
69 |
name, sizeof (name)-1); |
70 |
return is_openpgp_ext (name); |
71 |
} |
72 |
|
73 |
|
74 |
/* Dialog procedure for selecting recipients for encryption. */ |
75 |
BOOL CALLBACK |
76 |
file_encrypt_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
77 |
{ |
78 |
static listview_ctrl_t lv = NULL; |
79 |
static fm_state_t c; |
80 |
static keylist_t list = NULL; |
81 |
gpg_keycache_t kc; |
82 |
gpgme_ctx_t ctx; |
83 |
int force_trust = 0; |
84 |
|
85 |
switch (msg) { |
86 |
case WM_INITDIALOG: |
87 |
c = (fm_state_t)lparam; |
88 |
if (!c) |
89 |
dlg_fatal_error( dlg, "Could not get dialog state!" ); |
90 |
kc = keycache_get_ctx (KEYCACHE_PUB); |
91 |
SetWindowText (dlg, _("File Encrypt")); |
92 |
SetDlgItemText (dlg, IDC_ENCRYPT_ARMOR, _("&Text Output")); |
93 |
SetDlgItemText (dlg, IDC_ENCRYPT_WIPE, _("&Wipe Original")); |
94 |
SetDlgItemText (dlg, IDC_ENCRYPT_ANON, _("Anonymize (throw keyid)")); |
95 |
SetDlgItemText (dlg, IDC_ENCRYPT_SIGN, _("&Select Key for signing")); |
96 |
SetDlgItemText (dlg, IDC_ENCRYPT_OPTINF, _("Options")); |
97 |
SetDlgItemText (dlg, IDCANCEL, _("&Cancel")); |
98 |
|
99 |
/* XXX: support --hidden-recipient */ |
100 |
EnableWindow (GetDlgItem (dlg, IDC_ENCRYPT_ANON), FALSE); |
101 |
|
102 |
if (c->req_signer) { |
103 |
EnableWindow (GetDlgItem (dlg, IDC_ENCRYPT_SIGN), TRUE); |
104 |
seclist_init (dlg, IDC_ENCRYPT_SECLIST, KEYLIST_FLAG_SHORT, &list); |
105 |
} |
106 |
lv = keylist_load (GetDlgItem (dlg, IDC_ENCRYPT_LIST), kc, NULL, |
107 |
KEYLIST_ENCRYPT_MIN|KEYLIST_FLAG_FILE, |
108 |
KEY_SORT_USERID); |
109 |
SetForegroundWindow (dlg); |
110 |
return TRUE; |
111 |
|
112 |
case WM_DESTROY: |
113 |
seclist_destroy (&list); |
114 |
if (lv) { |
115 |
keylist_delete (lv); |
116 |
lv = NULL; |
117 |
} |
118 |
return FALSE; |
119 |
|
120 |
case WM_NOTIFY: |
121 |
NMHDR *notify; |
122 |
notify = (NMHDR *)lparam; |
123 |
if (notify && notify->code == LVN_COLUMNCLICK |
124 |
&& notify->idFrom == IDC_ENCRYPT_LIST) { |
125 |
NMLISTVIEW *p = (LPNMLISTVIEW) lparam; |
126 |
int sortby = 0; |
127 |
|
128 |
switch (p->iSubItem) { |
129 |
case 0: sortby = KEY_SORT_USERID; break; |
130 |
case 1: sortby = KEY_SORT_KEYID; break; |
131 |
case 2: sortby = KEY_SORT_LEN; break; |
132 |
case 4: sortby = KEY_SORT_VALIDITY; break; |
133 |
default:sortby = KEY_SORT_USERID; break; |
134 |
} |
135 |
keylist_sort (lv, sortby); |
136 |
} |
137 |
|
138 |
case WM_SYSCOMMAND: |
139 |
if (LOWORD (wparam) == SC_CLOSE) { |
140 |
c->cancel = 1; |
141 |
EndDialog (dlg, TRUE); |
142 |
} |
143 |
return FALSE; |
144 |
|
145 |
case WM_COMMAND: |
146 |
if (HIWORD (wparam) == BN_CLICKED |
147 |
&& LOWORD (wparam) == IDC_ENCRYPT_SIGN) { |
148 |
int req_signer = IsDlgButtonChecked (dlg, IDC_ENCRYPT_SIGN); |
149 |
EnableWindow (GetDlgItem (dlg, IDC_ENCRYPT_SECLIST), |
150 |
req_signer? TRUE : FALSE); |
151 |
} |
152 |
switch( LOWORD( wparam ) ) { |
153 |
case IDOK: |
154 |
ctx = c->ctx; |
155 |
safe_free (c->recp); |
156 |
c->recp = keylist_get_recipients (lv, &force_trust, &c->n_recp); |
157 |
if (!c->n_recp) { |
158 |
msg_box (dlg, _("Please select at least one recipient."), |
159 |
_("File Encrypt"), MB_ERR); |
160 |
safe_free (c->recp); |
161 |
c->recp = NULL; |
162 |
c->cancel = 1; |
163 |
return TRUE; |
164 |
} |
165 |
if (IsDlgButtonChecked (dlg, IDC_ENCRYPT_SIGN)) { |
166 |
gpgme_key_t key; |
167 |
|
168 |
if (seclist_select_key (dlg, IDC_ENCRYPT_SECLIST, &key)) |
169 |
return TRUE; |
170 |
else |
171 |
gpgme_signers_add (c->ctx, key); |
172 |
} |
173 |
|
174 |
if (IsDlgButtonChecked (dlg, IDC_ENCRYPT_ARMOR)) |
175 |
gpgme_set_armor (ctx, 1); |
176 |
if (IsDlgButtonChecked (dlg, IDC_ENCRYPT_WIPE)) |
177 |
c->wipe = 1; |
178 |
/* XXX: implement this |
179 |
if (IsDlgButtonChecked (dlg, IDC_ENCRYPT_ANON)) |
180 |
; |
181 |
*/ |
182 |
c->cancel = 0; |
183 |
EndDialog (dlg, TRUE); |
184 |
return TRUE; |
185 |
|
186 |
case IDCANCEL: |
187 |
c->cancel = 1; |
188 |
EndDialog (dlg, FALSE); |
189 |
return TRUE; |
190 |
} |
191 |
return TRUE; |
192 |
} |
193 |
|
194 |
return FALSE; |
195 |
} |
196 |
|
197 |
|
198 |
/* Dialog box procedure for importing keys. */ |
199 |
BOOL CALLBACK |
200 |
file_import_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
201 |
{ |
202 |
static listview_ctrl_t lv = NULL; |
203 |
static fm_state_t c; |
204 |
static int is_revcert = 0, has_seckeys = 0; |
205 |
|
206 |
switch (msg) { |
207 |
case WM_INITDIALOG: |
208 |
c = (fm_state_s *)lparam; |
209 |
if (!c) |
210 |
dlg_fatal_error( dlg, "Could not get dialog param!" ); |
211 |
ShowWindow (GetDlgItem( dlg, IDC_IMPORT_KEYMNGT ), WS_DISABLED); |
212 |
implist_build (&lv, GetDlgItem (dlg, IDC_IMPORT_KEYLIST)); |
213 |
implist_load (lv, c->import.is_clip? NULL: c->opaque, |
214 |
&is_revcert, &has_seckeys); |
215 |
if (!listview_count_items (lv, 0)) { |
216 |
msg_box (dlg, _("No valid OpenPGP data found."), _("Key Import"), MB_ERR); |
217 |
c->cancel = 1; |
218 |
EndDialog (dlg, FALSE); |
219 |
} |
220 |
else { |
221 |
const char *s = _("%s contain(s) %d key(s)."); |
222 |
char *p = new char[strlen (s) + 32]; |
223 |
if (!p) |
224 |
BUG (NULL); |
225 |
sprintf (p, s, c->import.is_clip? _("Clipboard") : _("File"), |
226 |
listview_count_items (lv, 0)); |
227 |
SetDlgItemText (dlg, IDC_IMPORT_INFO, p); |
228 |
free_if_alloc (p); |
229 |
} |
230 |
SetWindowText (dlg, _("Key Import")); |
231 |
SetDlgItemText (dlg, IDCANCEL, _("&Cancel")); |
232 |
center_window (dlg, NULL); |
233 |
SetForegroundWindow (dlg); |
234 |
return TRUE; |
235 |
|
236 |
case WM_DESTROY: |
237 |
if (lv) { |
238 |
listview_release (lv); |
239 |
lv = NULL; |
240 |
} |
241 |
return FALSE; |
242 |
|
243 |
case WM_SYSCOMMAND: |
244 |
if (LOWORD (wparam) == SC_CLOSE) { |
245 |
c->cancel = 1; |
246 |
EndDialog (dlg, FALSE); |
247 |
} |
248 |
return FALSE; |
249 |
|
250 |
case WM_COMMAND: |
251 |
switch (LOWORD (wparam)) { |
252 |
case IDC_IMPORT_DOIT: |
253 |
if (has_seckeys > 0) { |
254 |
msg_box (dlg, _("Some of the imported keys are secret keys.\n\n" |
255 |
"The ownertrust values of these keys must be\n" |
256 |
"set manually via the Key Properties dialog."), |
257 |
_("Import"), MB_INFO); |
258 |
} |
259 |
c->cancel = 0; |
260 |
c->import.revcert = is_revcert? 1 : 0; |
261 |
c->import.has_seckey = has_seckeys? 1 : 0; |
262 |
EndDialog (dlg, TRUE); |
263 |
return TRUE; |
264 |
|
265 |
case IDCANCEL: |
266 |
c->cancel = 1; |
267 |
EndDialog (dlg, FALSE); |
268 |
return FALSE; |
269 |
} |
270 |
break; |
271 |
} |
272 |
|
273 |
return FALSE; |
274 |
} |
275 |
|
276 |
|
277 |
/* Dialog box procedure for file signing. */ |
278 |
BOOL CALLBACK |
279 |
file_sign_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
280 |
{ |
281 |
static fm_state_t c; |
282 |
static listview_ctrl_t lv; |
283 |
gpg_keycache_t kc, sec_kc; |
284 |
gpgme_key_t key; |
285 |
int lv_idx; |
286 |
char signer[32]; |
287 |
|
288 |
switch (msg) { |
289 |
case WM_INITDIALOG: |
290 |
c = (fm_state_s *)lparam; |
291 |
if (c == NULL) |
292 |
dlg_fatal_error (dlg, "Could not get dialog state!"); |
293 |
SetWindowText (dlg, _("File Sign")); |
294 |
SetDlgItemText (dlg, IDC_FILE_SIG_ARMOR, _("&Text Output") ); |
295 |
SetDlgItemText (dlg, IDC_FILE_SIG_NORMAL, _("&Normal Signature")); |
296 |
SetDlgItemText (dlg, IDC_FILE_SIG_DETACH, _("&Detached Signature")); |
297 |
SetDlgItemText (dlg, IDC_FILE_SIG_CLEAR, _("&Cleartext Signature")); |
298 |
SetDlgItemText (dlg, IDC_FILE_SIG_SIGINF, _("Signature mode")); |
299 |
SetDlgItemText (dlg, IDC_FILE_SIG_OPTINF, _("Options")); |
300 |
SetDlgItemText (dlg, IDCANCEL, _("&Cancel")); |
301 |
CheckDlgButton (dlg, IDC_FILE_SIG_DETACH, BST_CHECKED); |
302 |
kc = keycache_get_ctx (KEYCACHE_PUB); |
303 |
if (!kc) |
304 |
BUG (dlg); |
305 |
sec_kc = keycache_get_ctx (KEYCACHE_PRV); |
306 |
if (!sec_kc) |
307 |
BUG (dlg); |
308 |
lv = keylist_load (GetDlgItem (dlg, IDC_FILE_SIG_LIST), kc, sec_kc, |
309 |
KEYLIST_SIGN, KEY_SORT_USERID); |
310 |
SetForegroundWindow (dlg); |
311 |
return TRUE; |
312 |
|
313 |
case WM_DESTROY: |
314 |
if (lv) { |
315 |
listview_release (lv); |
316 |
lv = NULL; |
317 |
} |
318 |
return FALSE; |
319 |
|
320 |
case WM_SYSCOMMAND: |
321 |
if (LOWORD (wparam) == SC_CLOSE) |
322 |
EndDialog (dlg, TRUE); |
323 |
return FALSE; |
324 |
|
325 |
case WM_COMMAND: |
326 |
switch (LOWORD (wparam)) { |
327 |
case IDOK: |
328 |
if (IsDlgButtonChecked (dlg, IDC_FILE_SIG_ARMOR)) |
329 |
gpgme_set_armor (c->ctx, 1); |
330 |
if (IsDlgButtonChecked (dlg, IDC_FILE_SIG_NORMAL)) |
331 |
c->sigmode = GPGME_SIG_MODE_NORMAL; |
332 |
else if (IsDlgButtonChecked (dlg, IDC_FILE_SIG_DETACH)) |
333 |
c->sigmode = GPGME_SIG_MODE_DETACH; |
334 |
else if (IsDlgButtonChecked( dlg, IDC_FILE_SIG_CLEAR)) |
335 |
c->sigmode = GPGME_SIG_MODE_CLEAR; |
336 |
else { |
337 |
/* default is to produce detached signatures */ |
338 |
c->sigmode = GPGME_SIG_MODE_DETACH; |
339 |
} |
340 |
lv_idx = listview_get_selected_item (lv); |
341 |
if (lv_idx == -1) { |
342 |
if (listview_count_items (lv, 0) == 1) |
343 |
lv_idx = 0; |
344 |
else { |
345 |
msg_box (dlg, _("Please select a key."), _("File Sign"), MB_ERR); |
346 |
return FALSE; |
347 |
} |
348 |
} |
349 |
listview_get_item_text( lv, lv_idx, 1, signer, sizeof signer-1 ); |
350 |
if( get_seckey( signer, &key ) ) |
351 |
BUG( NULL ); |
352 |
gpgme_signers_clear (c->ctx); |
353 |
gpgme_signers_add (c->ctx, key); |
354 |
free_if_alloc (c->opaque); |
355 |
c->opaque = m_strdup (signer); /* store for later use */ |
356 |
c->cancel = 0; |
357 |
if (!c->opaque) |
358 |
BUG (0); |
359 |
EndDialog (dlg, TRUE); |
360 |
return TRUE; |
361 |
|
362 |
case IDCANCEL: |
363 |
c->cancel = 1; |
364 |
EndDialog (dlg, FALSE); |
365 |
return FALSE; |
366 |
} |
367 |
break; |
368 |
} |
369 |
|
370 |
return FALSE; |
371 |
} |
372 |
|
373 |
|
374 |
/* Setup status bar for the main window @dlg. */ |
375 |
static HWND |
376 |
setup_status_bar (HWND dlg) |
377 |
{ |
378 |
HWND statbar; |
379 |
RECT r; |
380 |
int partpos[3]; |
381 |
int i; |
382 |
|
383 |
GetClientRect (dlg, &r); |
384 |
for (i=1; i <= 3; i++) |
385 |
partpos[i-1] = r.right/3*i; |
386 |
|
387 |
statbar = CreateStatusWindow (WS_CHILD | WS_VISIBLE | CCS_BOTTOM, |
388 |
"", dlg, FM_SEPARATOR_ID); |
389 |
ShowWindow (statbar, SW_SHOW); |
390 |
SendMessage (statbar, SB_SETPARTS, (WPARAM)3, (LPARAM)partpos); |
391 |
return statbar; |
392 |
} |
393 |
|
394 |
|
395 |
/* Refresh the status bar text with the selected item from @lv. */ |
396 |
static void |
397 |
update_status_bar (HWND statbar, listview_ctrl_t lv) |
398 |
{ |
399 |
struct stat st; |
400 |
char buf[128]; |
401 |
int n; |
402 |
|
403 |
if (!lv) { /* flush */ |
404 |
SendMessage (statbar, SB_SETTEXT, 1, (LPARAM)"0 Byte"); |
405 |
SendMessage (statbar, SB_SETTEXT, 0, (LPARAM)_("0 Objects marked")); |
406 |
return; |
407 |
} |
408 |
n = listview_count_items (lv, 1); |
409 |
_snprintf (buf, sizeof (buf)-1, _("%d Object(s) marked"), n); |
410 |
SendMessage (statbar, SB_SETTEXT, 0, (LPARAM)buf); |
411 |
listview_get_item_text (lv, listview_get_curr_pos (lv), 1, buf, 127); |
412 |
if (stat (buf, &st) == 0) { |
413 |
if (st.st_size < 1024) |
414 |
_snprintf (buf, sizeof (buf)-1, "%d Byte", st.st_size); |
415 |
else if (st.st_size < 1048576) |
416 |
_snprintf (buf, sizeof (buf)-1, "%d KB", st.st_size/1024); |
417 |
else |
418 |
_snprintf (buf, sizeof (buf)-1, "%d MB", st.st_size/1024/1024); |
419 |
SendMessage (statbar, SB_SETTEXT, 1, (LPARAM)buf); |
420 |
} |
421 |
else |
422 |
SendMessage (statbar, SB_SETTEXT, 1, (LPARAM)"0 Byte"); |
423 |
} |
424 |
|
425 |
|
426 |
/* Subclass procedure to detect common hotkeys. */ |
427 |
static BOOL CALLBACK |
428 |
filelist_subclass_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
429 |
{ |
430 |
switch (msg) { |
431 |
case WM_KEYUP: |
432 |
int virt_key = (int)wparam; |
433 |
if (virt_key == VK_DELETE) |
434 |
send_cmd_id (filelist_proc.dlg, ID_FILEMISC_WIPE); |
435 |
break; |
436 |
} |
437 |
return CallWindowProc (filelist_proc.old, dlg, msg, wparam, lparam); |
438 |
} |
439 |
|
440 |
|
441 |
/* Enable or disabled menu entries from the dialog window @hwnd. |
442 |
@lv is used to find out the state of the selected file entry. */ |
443 |
static void |
444 |
update_ui_items (HWND hwnd, HMENU hm, bool is_popup, listview_ctrl_t lv) |
445 |
{ |
446 |
int ids[2][7] = { |
447 |
{ID_FILEMISC_SEND, ID_FILEMISC_DECRYPT, ID_FILEMISC_VERIFY, |
448 |
ID_FILEMISC_ENCRYPT, ID_FILEMISC_SIGN, ID_FILEMISC_SIGNENC, |
449 |
ID_FILEMISC_ENCRYPT_ZIP |
450 |
}, |
451 |
{ID_FILECTX_SEND, ID_FILECTX_DECRYPT, ID_FILECTX_VERIFY, |
452 |
ID_FILECTX_ENCRYPT, ID_FILECTX_SIGN, ID_FILECTX_SIGNENC, |
453 |
ID_FILECTX_ENCRYPT_ZIP |
454 |
} |
455 |
}; |
456 |
enum id_type_t { SEND, DECRYPT, VERIFY, ENCR, SIGN, SIGENC, ENCRZIP }; |
457 |
int pos, idx = is_popup? 1: 0; |
458 |
|
459 |
pos = listview_get_curr_pos (lv); |
460 |
if (fm_check_file_type (lv, pos, FM_DECRYPT)) |
461 |
set_menu_state (hm, ids[idx][SEND], MF_ENABLED); |
462 |
|
463 |
if (fm_check_file_type (lv, pos, FM_ENCRYPT)) { |
464 |
set_menu_state (hm, ids[idx][DECRYPT], MF_GRAYED|MF_DISABLED); |
465 |
set_menu_state (hm, ids[idx][VERIFY], MF_GRAYED|MF_DISABLED); |
466 |
} |
467 |
if (fm_check_file_type (lv, pos, FM_DECRYPT)) { |
468 |
set_menu_state (hm, ids[idx][ENCR], MF_GRAYED|MF_DISABLED); |
469 |
set_menu_state (hm, ids[idx][ENCRZIP], MF_GRAYED|MF_DISABLED); |
470 |
set_menu_state (hm, ids[idx][SIGN], MF_GRAYED|MF_DISABLED); |
471 |
set_menu_state (hm, ids[idx][SIGENC], MF_GRAYED|MF_DISABLED); |
472 |
} |
473 |
if (fm_check_file_type (lv, pos, FM_VERIFY)) { |
474 |
set_menu_state (hm, ids[idx][VERIFY], MF_ENABLED); |
475 |
set_menu_state (hm, ids[idx][SIGN], MF_GRAYED|MF_DISABLED); |
476 |
set_menu_state (hm, ids[idx][SIGENC], MF_GRAYED|MF_DISABLED); |
477 |
} |
478 |
|
479 |
/* disable all operation which need a secret key. */ |
480 |
if (!secret_key_available ()) { |
481 |
set_menu_state (hm, ids[idx][DECRYPT], MF_GRAYED|MF_DISABLED); |
482 |
set_menu_state (hm, ids[idx][SIGN], MF_GRAYED|MF_DISABLED); |
483 |
set_menu_state (hm, ids[idx][SIGENC], MF_GRAYED|MF_DISABLED); |
484 |
} |
485 |
} |
486 |
|
487 |
|
488 |
/* Dialog box procedure for the File Manager. */ |
489 |
BOOL CALLBACK |
490 |
file_manager_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
491 |
{ |
492 |
static HWND statbar; |
493 |
static listview_ctrl_t lv = NULL; |
494 |
static HMENU menu = NULL; |
495 |
static int always_ontop = 0; |
496 |
HWND fl; |
497 |
|
498 |
switch (msg) { |
499 |
case WM_INITDIALOG: |
500 |
always_ontop = get_reg_winpt_flag ("AlwaysOnTop"); |
501 |
if (always_ontop == -1) |
502 |
always_ontop = 0; |
503 |
SetWindowText (dlg, _("File Manager (use drag & drop to add files)")); |
504 |
menu = LoadMenu (glob_hinst, (LPCSTR)IDR_WINPT_FILEMISC); |
505 |
set_menu_text_bypos (menu, 0, _("File")); |
506 |
set_menu_text_bypos (menu, 1, _("Edit")); |
507 |
set_menu_text_bypos (menu, 2, _("View")); |
508 |
set_menu_text (menu, ID_FILEMISC_OPEN, _("&Open...") ); |
509 |
set_menu_text (menu, ID_FILEMISC_ENCRYPT, _("&Encrypt") ); |
510 |
set_menu_text (menu, ID_FILEMISC_ENCRYPT_ZIP, _("Encrypt into ZIP")); |
511 |
set_menu_text (menu, ID_FILEMISC_DECRYPT, _("&Decrypt") ); |
512 |
set_menu_text (menu, ID_FILEMISC_SIGN, _("&Sign") ); |
513 |
set_menu_text (menu, ID_FILEMISC_SIGNENC, _("Sign && Encrypt") ); |
514 |
set_menu_text (menu, ID_FILEMISC_VERIFY, _("&Verify") ); |
515 |
set_menu_text (menu, ID_FILEMISC_SYMENC, _("S&ymmetric") ); |
516 |
set_menu_text (menu, ID_FILEMISC_IMPORT, _("&Import") ); |
517 |
set_menu_text (menu, ID_FILEMISC_EXPORT, _("E&xport") ); |
518 |
set_menu_text (menu, ID_FILEMISC_QUIT, _("Exit")); |
519 |
set_menu_text (menu, ID_FILEMISC_CLEAR, _("&Reset") ); |
520 |
set_menu_text (menu, ID_FILEMISC_ONTOP, _("Always on Top")); |
521 |
set_menu_text (menu, ID_FILEMISC_PASTE, _("&Paste")); |
522 |
set_menu_text (menu, ID_FILEMISC_SELALL, _("&Select All")); |
523 |
set_menu_text (menu, ID_FILEMISC_PREFS, _("&Preferences")); |
524 |
set_menu_text (menu, ID_FILEMISC_SEND, _("Send as Mail")); |
525 |
set_menu_text (menu, ID_FILEMISC_LIST, _("&List Packets")); |
526 |
set_menu_text (menu, ID_FILEMISC_WIPE_FREES, _("Wipe Free Space")); |
527 |
set_menu_text (menu, ID_FILEMISC_WIPE, _("&Wipe")); |
528 |
|
529 |
fl = GetDlgItem (dlg, IDC_FILE_LIST); |
530 |
filelist_proc.dlg = dlg; |
531 |
filelist_proc.current = (WNDPROC)filelist_subclass_proc; |
532 |
filelist_proc.old = (WNDPROC)GetWindowLong (fl, GWL_WNDPROC); |
533 |
if (filelist_proc.old) { |
534 |
if (!SetWindowLong (fl, GWL_WNDPROC, (LONG)filelist_proc.current)) { |
535 |
msg_box (dlg, "Could not set filelist window procedure.", |
536 |
_("File Manager"), MB_ERR); |
537 |
BUG (NULL); |
538 |
} |
539 |
} |
540 |
SetMenu (dlg, menu); |
541 |
if (!always_ontop) |
542 |
set_menu_state (menu, ID_FILEMISC_ONTOP, MFS_UNCHECKED); |
543 |
else |
544 |
center_window2 (dlg, NULL, HWND_TOPMOST); |
545 |
fm_build (&lv, GetDlgItem (dlg, IDC_FILE_LIST)); |
546 |
center_window (dlg, NULL); |
547 |
SetForegroundWindow (dlg); |
548 |
statbar = setup_status_bar (dlg); |
549 |
filelist_proc.opaque = (void*)statbar; |
550 |
return TRUE; |
551 |
|
552 |
case WM_DESTROY: |
553 |
if (lv) { |
554 |
fm_delete (lv); |
555 |
lv = NULL; |
556 |
} |
557 |
if (menu) { |
558 |
DestroyMenu (menu); |
559 |
menu = NULL; |
560 |
} |
561 |
set_reg_winpt_flag ("AlwaysOnTop", always_ontop); |
562 |
return FALSE; |
563 |
|
564 |
case WM_DROPFILES: |
565 |
fm_add_dropped_files (lv, (HDROP)wparam); |
566 |
return TRUE; |
567 |
|
568 |
case WM_NOTIFY: |
569 |
NMHDR *notify; |
570 |
int pos; |
571 |
HMENU hm; |
572 |
|
573 |
notify = (NMHDR *)lparam; |
574 |
if (notify && notify->code == NM_CLICK && |
575 |
notify->idFrom == IDC_FILE_LIST) |
576 |
update_status_bar (statbar, lv); |
577 |
if (notify && notify->code == NM_RCLICK |
578 |
&& notify->idFrom == IDC_FILE_LIST) { |
579 |
POINT p; |
580 |
HMENU popup; |
581 |
|
582 |
GetCursorPos (&p); |
583 |
hm = LoadMenu (glob_hinst, MAKEINTRESOURCE (IDR_WINPT_FILEMISC_CTX)); |
584 |
popup = GetSubMenu (hm, 0); |
585 |
set_menu_text (popup, ID_FILECTX_ENCRYPT_ZIP, _("Encrypt into ZIP")); |
586 |
set_menu_text (popup, ID_FILECTX_ENCRYPT, _("Encrypt")); |
587 |
set_menu_text (popup, ID_FILECTX_DECRYPT, _("Decrypt")); |
588 |
set_menu_text (popup, ID_FILECTX_SIGN, _("Sign")); |
589 |
set_menu_text (popup, ID_FILECTX_SIGNENC, _("Sign && Encrypt")); |
590 |
set_menu_text (popup, ID_FILECTX_VERIFY, _("Verify")); |
591 |
set_menu_text (popup, ID_FILECTX_WIPE, _("Wipe")); |
592 |
set_menu_text (popup, ID_FILECTX_SEND, _("Send as Mail")); |
593 |
set_menu_text (popup, ID_FILECTX_LIST, _("List Packets")); |
594 |
if (!is_openpgp_file (lv)) |
595 |
set_menu_state (popup, ID_FILECTX_LIST, MF_GRAYED|MF_DISABLED); |
596 |
update_ui_items (dlg, popup, true, lv); |
597 |
TrackPopupMenu (popup, TPM_RIGHTALIGN, p.x, p.y, 0, dlg, NULL); |
598 |
DestroyMenu (popup); |
599 |
DestroyMenu (hm); |
600 |
} |
601 |
if (notify && notify->code == NM_CLICK |
602 |
&& notify->idFrom == IDC_FILE_LIST) { |
603 |
hm = GetMenu (dlg); |
604 |
pos = listview_get_curr_pos (lv); |
605 |
if (!fm_check_file_type (lv, pos, FM_DECRYPT)) |
606 |
set_menu_state (hm, ID_FILEMISC_SEND, MF_GRAYED|MF_DISABLED); |
607 |
else |
608 |
set_menu_state (hm, ID_FILEMISC_SEND, MF_ENABLED); |
609 |
} |
610 |
if (notify && notify->code == LVN_ITEMCHANGED && |
611 |
((LPNMLISTVIEW)lparam)->uNewState) /* item selected? */ |
612 |
update_ui_items (dlg, GetMenu (dlg), false, lv); |
613 |
return TRUE; |
614 |
|
615 |
case WM_SYSCOMMAND: |
616 |
if (LOWORD (wparam) == SC_CLOSE) |
617 |
EndDialog (dlg, TRUE); |
618 |
return FALSE; |
619 |
|
620 |
case WM_INITMENUPOPUP: |
621 |
if ((UINT)LOWORD (lparam) == 0 && |
622 |
GetMenuItemCount ((HMENU)wparam) == 19) /* XXX: do not hard code values */ |
623 |
set_menu_text_bypos ((HMENU)wparam, 2, _("&Calc Digest")); |
624 |
return FALSE; |
625 |
|
626 |
case WM_COMMAND: |
627 |
switch (LOWORD (wparam)) { |
628 |
case ID_FILEMISC_QUIT: |
629 |
EndDialog (dlg, TRUE); |
630 |
return TRUE; |
631 |
|
632 |
case ID_FILEMISC_WIPE_FREES: |
633 |
if (!admin_user) { |
634 |
msg_box (dlg, _("This command requires admin privileges.\n"), |
635 |
_("File Manager"), MB_ERR); |
636 |
break; |
637 |
} |
638 |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_SPACE_SECDEL, |
639 |
dlg, space_wipefrees_dlg_proc, 0); |
640 |
break; |
641 |
|
642 |
case ID_FILEMISC_SELALL: |
643 |
listview_select_all (lv); |
644 |
break; |
645 |
|
646 |
case ID_FILEMISC_PASTE: |
647 |
fm_verify_pasted_detsig (lv, dlg); |
648 |
break; |
649 |
|
650 |
case ID_FILEMISC_ONTOP: |
651 |
always_ontop ^= 1; |
652 |
set_menu_state (menu, ID_FILEMISC_ONTOP, |
653 |
always_ontop == 0? MFS_UNCHECKED : MFS_CHECKED); |
654 |
center_window2 (dlg, NULL, |
655 |
always_ontop == 0? HWND_NOTOPMOST : HWND_TOPMOST); |
656 |
break; |
657 |
|
658 |
case ID_FILEMISC_SEND: |
659 |
fm_send_file (lv); |
660 |
break; |
661 |
|
662 |
case ID_FILEMISC_PREFS: |
663 |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_FILE_PREFS, dlg, |
664 |
file_preferences_dlg_proc, 0); |
665 |
break; |
666 |
|
667 |
case ID_FILEMISC_LIST: |
668 |
case ID_FILEMISC_ENCRYPT: |
669 |
case ID_FILEMISC_SYMENC: |
670 |
case ID_FILEMISC_DECRYPT: |
671 |
case ID_FILEMISC_SIGN: |
672 |
case ID_FILEMISC_SIGNENC: |
673 |
case ID_FILEMISC_VERIFY: |
674 |
case ID_FILEMISC_WIPE: |
675 |
case ID_FILEMISC_ENCRYPT_ZIP: |
676 |
fm_parse_files (lv, dlg, LOWORD (wparam)); |
677 |
SetForegroundWindow (dlg); |
678 |
return TRUE; |
679 |
|
680 |
case ID_FILEMISC_MD_MD5: |
681 |
case ID_FILEMISC_MD_SHA1: |
682 |
case ID_FILEMISC_MD_RMD160: |
683 |
case ID_FILEMISC_MD_SHA256: |
684 |
int algo; |
685 |
|
686 |
switch (LOWORD (wparam)) { |
687 |
case ID_FILEMISC_MD_MD5: algo = GPGME_MD_MD5; break; |
688 |
case ID_FILEMISC_MD_SHA1: algo = GPGME_MD_SHA1; break; |
689 |
case ID_FILEMISC_MD_RMD160: algo = GPGME_MD_RMD160; break; |
690 |
case ID_FILEMISC_MD_SHA256: algo = GPGME_MD_SHA256; break; |
691 |
default: algo = GPGME_MD_SHA1; break; |
692 |
} |
693 |
fm_print_md (lv, dlg, algo); |
694 |
SetForegroundWindow (dlg); |
695 |
return TRUE; |
696 |
|
697 |
case ID_FILEMISC_CLEAR: |
698 |
listview_del_all_items (lv); |
699 |
return TRUE; |
700 |
|
701 |
case ID_FILEMISC_OPEN: |
702 |
fm_add_opened_files( lv, dlg ); |
703 |
return TRUE; |
704 |
|
705 |
/* context menu entries */ |
706 |
case ID_FILECTX_ENCRYPT: send_cmd_id (dlg, ID_FILEMISC_ENCRYPT); break; |
707 |
case ID_FILECTX_DECRYPT: send_cmd_id (dlg, ID_FILEMISC_DECRYPT);break; |
708 |
case ID_FILECTX_SIGN: send_cmd_id (dlg, ID_FILEMISC_SIGN); break; |
709 |
case ID_FILECTX_SIGNENC: send_cmd_id (dlg, ID_FILEMISC_SIGNENC);break; |
710 |
case ID_FILECTX_VERIFY: send_cmd_id (dlg, ID_FILEMISC_VERIFY); break; |
711 |
case ID_FILECTX_WIPE: send_cmd_id (dlg, ID_FILEMISC_WIPE); break; |
712 |
case ID_FILECTX_LIST: send_cmd_id (dlg, ID_FILEMISC_LIST); break; |
713 |
case ID_FILECTX_SEND: send_cmd_id (dlg, ID_FILEMISC_SEND); break; |
714 |
case ID_FILECTX_ENCRYPT_ZIP:send_cmd_id (dlg, ID_FILEMISC_ENCRYPT_ZIP); break; |
715 |
} |
716 |
break; |
717 |
} |
718 |
|
719 |
return FALSE; |
720 |
} |
721 |
|
722 |
|
723 |
/* Dialog box procedure for the FM preferences. */ |
724 |
BOOL CALLBACK |
725 |
file_preferences_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
726 |
{ |
727 |
switch (msg) { |
728 |
case WM_INITDIALOG: |
729 |
CheckDlgButton (dlg, IDC_FPREFS_PROGRESS, |
730 |
reg_prefs.fm.progress? BST_CHECKED : BST_UNCHECKED); |
731 |
SetForegroundWindow (dlg); |
732 |
center_window (dlg, NULL); |
733 |
break; |
734 |
|
735 |
case WM_COMMAND: |
736 |
switch (LOWORD (wparam)) { |
737 |
case IDOK: |
738 |
reg_prefs.fm.progress = IsDlgButtonChecked (dlg, IDC_FPREFS_PROGRESS)? 1 : 0; |
739 |
set_reg_winpt_prefs (®_prefs); |
740 |
EndDialog (dlg, TRUE); |
741 |
break; |
742 |
|
743 |
case IDCANCEL: |
744 |
EndDialog (dlg, FALSE); |
745 |
break; |
746 |
} |
747 |
break; |
748 |
} |
749 |
|
750 |
return FALSE; |
751 |
} |
752 |
|
753 |
|
754 |
/* Add the selected items from @lv into the combo box. */ |
755 |
static void |
756 |
add_files_from_lv (HWND dlg, listview_ctrl_t lv) |
757 |
{ |
758 |
char fname[384+1]; |
759 |
int i; |
760 |
|
761 |
for (i = 0; i < listview_count_items (lv, 0); i++) { |
762 |
if (!listview_get_item_state (lv, i)) |
763 |
continue; |
764 |
listview_get_item_text (lv, i, 1, fname, sizeof (fname)-1); |
765 |
SendDlgItemMessage (dlg, IDC_SECDEL_FILES, LB_ADDSTRING, |
766 |
0, (WPARAM)(char *)fname); |
767 |
} |
768 |
} |
769 |
|
770 |
|
771 |
/* Dialog box procedure to confirm that files shall be deleted. */ |
772 |
BOOL CALLBACK |
773 |
file_secdel_confirm_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
774 |
{ |
775 |
static secdel_confirm_s * ctx; |
776 |
|
777 |
switch (msg) { |
778 |
case WM_INITDIALOG: |
779 |
ctx = (struct secdel_confirm_s *)lparam; |
780 |
if (!ctx) |
781 |
BUG (NULL); |
782 |
add_files_from_lv (dlg, ctx->lv_files); |
783 |
SetWindowText (dlg, _("Are you sure you want to secure delete these files?")); |
784 |
SetDlgItemText (dlg, IDYES, _("&Yes")); |
785 |
SetDlgItemText (dlg, IDNO, _("&No")); |
786 |
break; |
787 |
|
788 |
case WM_COMMAND: |
789 |
switch (LOWORD (wparam)) { |
790 |
case IDYES: |
791 |
ctx->yes = 1; |
792 |
EndDialog (dlg, TRUE); |
793 |
break; |
794 |
|
795 |
case IDNO: |
796 |
ctx->yes = 0; |
797 |
EndDialog (dlg, FALSE); |
798 |
break; |
799 |
} |
800 |
break; |
801 |
} |
802 |
|
803 |
return FALSE; |
804 |
} |
805 |
|
806 |
|
807 |
/* Wipe callback. @ctx is a window handle @off is the current offset |
808 |
and @n is the total size. */ |
809 |
static void |
810 |
wipe_cb (void *ctx, ui64 off, ui64 n) |
811 |
{ |
812 |
SendDlgItemMessage ((HWND)ctx, IDC_SPACE_PROGRESS, PBM_SETPOS, |
813 |
(WPARAM) (off*100/n), 0); |
814 |
SetDlgItemInt ((HWND)ctx, IDC_SPACE_OFF, (UINT)(off/1024), FALSE); |
815 |
SetDlgItemInt ((HWND)ctx, IDC_SPACE_N, (UINT)(n/1024), FALSE); |
816 |
} |
817 |
|
818 |
|
819 |
/* Call the wipe free space method asynchron. */ |
820 |
static DWORD WINAPI |
821 |
wipe_thread (void *ctx) |
822 |
{ |
823 |
thread_ctx_t a = (thread_ctx_t)ctx; |
824 |
int rc; |
825 |
|
826 |
rc = wipe_freespace (a->drive, &a->fd, wipe_cb, a->dlg); |
827 |
SetDlgItemText (a->dlg, IDC_SPACE_INFO, |
828 |
rc? _("Operation Status: Error") : |
829 |
_("Operation Status: Done.")); |
830 |
if (rc) |
831 |
msg_box (a->dlg, winpt_strerror (rc), _("Wipe Free Space"), MB_ERR); |
832 |
else |
833 |
MessageBeep (MB_OK); |
834 |
ExitThread (0); |
835 |
return 0; |
836 |
} |
837 |
|
838 |
|
839 |
static void |
840 |
fill_drive_combobox (HWND dlg) |
841 |
{ |
842 |
char buf[384], p[16]; |
843 |
int n, i, j = 0; |
844 |
|
845 |
n = GetLogicalDriveStrings (DIM (buf)-1, buf); |
846 |
while (j < n) { |
847 |
for (i = 0; buf[j] != '\0'; i++, j++) |
848 |
p[i] = buf[j]; |
849 |
p[i] = '\0'; |
850 |
j++; |
851 |
if (GetDriveType (p) != DRIVE_CDROM) |
852 |
SendDlgItemMessage (dlg, IDC_SPACE_DRIVES, CB_ADDSTRING, |
853 |
0, (LPARAM)(const char *)p); |
854 |
} |
855 |
SendDlgItemMessage (dlg, IDC_SPACE_DRIVES, CB_SETCURSEL, 0, 0); |
856 |
} /* fill_drive_combobox */ |
857 |
|
858 |
|
859 |
static void |
860 |
update_disc_stats (HWND dlg) |
861 |
{ |
862 |
ULARGE_INTEGER caller = {0}, total = {0}, free = {0}; |
863 |
char buf[128], drive[32], a_total[64] = {0}, a_free[64] = {0}; |
864 |
int idx; |
865 |
|
866 |
idx = SendDlgItemMessage (dlg, IDC_SPACE_DRIVES, CB_GETCURSEL, 0, 0); |
867 |
GetDlgItemText (dlg, IDC_SPACE_DRIVES, drive, DIM (drive)-1); |
868 |
if (GetDiskFreeSpaceEx (drive, &caller, &total, &free)) { |
869 |
_ui64toa (total.QuadPart/1024, a_total, 10); |
870 |
_ui64toa (free.QuadPart/1024, a_free, 10); |
871 |
} |
872 |
_snprintf (buf, DIM (buf)-1, _("Total Capacity: %12sk\n" |
873 |
"Free Space : %12sk"), a_total, a_free); |
874 |
SetDlgItemText(dlg, IDC_SPACE_DISC_INFO, buf); |
875 |
SetDlgItemInt (dlg, IDC_SPACE_OFF, 0, FALSE); |
876 |
SetDlgItemInt (dlg, IDC_SPACE_N, 0, FALSE); |
877 |
} /* update_disc_stats */ |
878 |
|
879 |
|
880 |
BOOL CALLBACK |
881 |
space_wipefrees_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
882 |
{ |
883 |
static HANDLE thread_hd = NULL; |
884 |
static thread_ctx_s ctx; |
885 |
char drive[32], * p; |
886 |
int idx; |
887 |
DWORD tid; |
888 |
|
889 |
switch (msg) { |
890 |
case WM_INITDIALOG: |
891 |
fill_drive_combobox (dlg); |
892 |
update_disc_stats (dlg); |
893 |
center_window (dlg, NULL); |
894 |
SetForegroundWindow (dlg); |
895 |
break; |
896 |
|
897 |
case WM_DESTROY: |
898 |
thread_hd = NULL; |
899 |
free_if_alloc (ctx.drive); |
900 |
memset (&ctx, 0, sizeof ctx); |
901 |
break; |
902 |
|
903 |
case WM_COMMAND: |
904 |
if (HIWORD (wparam) == CBN_SELCHANGE) |
905 |
update_disc_stats (dlg); |
906 |
switch (LOWORD (wparam)) { |
907 |
case IDOK: |
908 |
idx = SendDlgItemMessage (dlg, IDC_SPACE_DRIVES, CB_GETCURSEL, 0, 0); |
909 |
if (idx != CB_ERR && !thread_hd) { |
910 |
GetDlgItemText (dlg, IDC_SPACE_DRIVES, drive, DIM (drive)-1); |
911 |
ctx.dlg = dlg; |
912 |
ctx.drive = m_strdup (drive); |
913 |
ctx.fd = NULL; |
914 |
thread_hd = CreateThread (NULL, 0, wipe_thread, &ctx, 0, &tid); |
915 |
} |
916 |
if (WaitForSingleObject (thread_hd, 0) == WAIT_OBJECT_0) { |
917 |
CloseHandle (thread_hd); |
918 |
thread_hd = NULL; |
919 |
EndDialog (dlg, TRUE); |
920 |
} |
921 |
break; |
922 |
|
923 |
case IDCANCEL: |
924 |
if (thread_hd) { |
925 |
WaitForSingleObject (thread_hd, 2000); |
926 |
TerminateThread (thread_hd, 1); |
927 |
CloseHandle (thread_hd); |
928 |
CloseHandle (ctx.fd); |
929 |
p = make_filename (ctx.drive, "temp_winpt.tmp", NULL); |
930 |
if (p && !file_exist_check (p)) |
931 |
remove (p); |
932 |
free_if_alloc (p); |
933 |
} |
934 |
EndDialog( dlg, FALSE ); |
935 |
break; |
936 |
} |
937 |
break; |
938 |
} |
939 |
|
940 |
return FALSE; |
941 |
} /* space_wipefrees_dlg_proc */ |