1 |
werner |
36 |
/* wptProgressDlg.cpp - Support for the GPG progress status |
2 |
|
|
* Copyright (C) 2003, 2004, 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 |
|
|
#ifdef HAVE_CONFIG_H |
21 |
|
|
#include <config.h> |
22 |
|
|
#endif |
23 |
|
|
|
24 |
|
|
#include <windows.h> |
25 |
|
|
#include <commctrl.h> |
26 |
|
|
|
27 |
werner |
47 |
#include "resource.h" |
28 |
werner |
36 |
#include "wptGPG.h" |
29 |
|
|
#include "wptTypes.h" |
30 |
|
|
#include "wptCommonCtl.h" |
31 |
|
|
#include "wptW32API.h" |
32 |
|
|
#include "wptContext.h" |
33 |
|
|
#include "wptVersion.h" |
34 |
|
|
#include "wptErrors.h" |
35 |
|
|
|
36 |
twoaday |
229 |
/* It is not possible that there are two instance of a progress |
37 |
|
|
thread, thus we use a 'const' handle for external synchronisation. */ |
38 |
|
|
static HANDLE curr_thread = NULL; |
39 |
werner |
36 |
|
40 |
|
|
/* Dialog procedure to show the progress of an operation. */ |
41 |
|
|
static BOOL CALLBACK |
42 |
|
|
progress_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
43 |
|
|
{ |
44 |
twoaday |
229 |
static progress_filter_s *pfx = NULL; |
45 |
werner |
36 |
|
46 |
|
|
switch (msg) { |
47 |
|
|
case WM_INITDIALOG: |
48 |
|
|
pfx = (struct progress_filter_s *)lparam; |
49 |
|
|
if (!pfx) |
50 |
|
|
BUG (0); |
51 |
|
|
pfx->dlg = dlg; |
52 |
|
|
SendDlgItemMessage (dlg, IDC_PROGRESS_BAR, |
53 |
|
|
PBM_SETRANGE, 0, MAKELPARAM (0, 100)); |
54 |
|
|
SetDlgItemInt (pfx->dlg, IDC_PROGRESS_TOTAL, pfx->total, FALSE); |
55 |
|
|
SetDlgItemInt (pfx->dlg, IDC_PROGRESS_CURR, pfx->curr, FALSE); |
56 |
|
|
SetDlgItemText (pfx->dlg, IDC_PROGRESS_WHAT, pfx->what); |
57 |
|
|
center_window2 (dlg, NULL, HWND_TOPMOST); |
58 |
|
|
center_window (dlg, NULL); |
59 |
|
|
SetForegroundWindow (dlg); |
60 |
|
|
return TRUE; |
61 |
|
|
} |
62 |
|
|
return FALSE; |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
|
66 |
|
|
/* Thread to start the progress dialog. |
67 |
|
|
@opaque contains the context for the dialog. */ |
68 |
|
|
static DWORD WINAPI |
69 |
|
|
progress_cb_thread (void *opaque) |
70 |
|
|
{ |
71 |
|
|
struct progress_filter_s *pfx = (struct progress_filter_s *)opaque; |
72 |
|
|
|
73 |
|
|
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_PROGRESS, |
74 |
|
|
pfx? pfx->hwnd : GetActiveWindow (), |
75 |
|
|
progress_dlg_proc, (LPARAM)opaque); |
76 |
|
|
ExitThread (0); |
77 |
|
|
return TRUE; |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
|
81 |
|
|
/* Close the progress dialog (if still open) and close all handles in @pfx. */ |
82 |
|
|
void |
83 |
|
|
progress_cleanup (progress_filter_s *pfx) |
84 |
|
|
{ |
85 |
|
|
if (!pfx) |
86 |
|
|
return; |
87 |
|
|
if (pfx->dlg) { |
88 |
|
|
EndDialog (pfx->dlg, TRUE); |
89 |
|
|
pfx->dlg = NULL; |
90 |
|
|
} |
91 |
|
|
if (pfx->thread_hd) { |
92 |
|
|
CloseHandle (pfx->thread_hd); |
93 |
|
|
pfx->thread_hd = NULL; |
94 |
|
|
} |
95 |
twoaday |
229 |
curr_thread = NULL; |
96 |
werner |
36 |
pfx->curr = 0; |
97 |
|
|
pfx->total = 0; |
98 |
|
|
pfx->error = 0; |
99 |
twoaday |
229 |
memset (pfx, 0, sizeof *pfx); |
100 |
werner |
36 |
} |
101 |
|
|
|
102 |
|
|
|
103 |
twoaday |
229 |
/* Wait until the progress thread terminates. */ |
104 |
|
|
void |
105 |
|
|
wait_for_progress_cb (void) |
106 |
|
|
{ |
107 |
|
|
if (!curr_thread) |
108 |
|
|
return; |
109 |
|
|
WaitForSingleObject (curr_thread, INFINITE); |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
|
113 |
werner |
36 |
/* Generic callback for operations with a progress bar. */ |
114 |
|
|
void |
115 |
|
|
progress_callback (void *opaque, const char *what, int type, int off, int max) |
116 |
|
|
{ |
117 |
|
|
struct progress_filter_s *pfx = (struct progress_filter_s *)opaque; |
118 |
|
|
DWORD tid; |
119 |
|
|
|
120 |
|
|
log_debug ("progress enter %d %d\r\n", off, max); |
121 |
|
|
if (!pfx || pfx->error) |
122 |
|
|
return; |
123 |
|
|
if (what) |
124 |
|
|
pfx->what = what; |
125 |
|
|
pfx->curr = off; |
126 |
|
|
pfx->total = max; |
127 |
|
|
if (!off && !pfx->thread_hd) { |
128 |
twoaday |
229 |
pfx->thread_hd = CreateThread (NULL, 0, progress_cb_thread, |
129 |
|
|
(void *)pfx, 0, &tid); |
130 |
werner |
36 |
if (!pfx->thread_hd) { |
131 |
|
|
msg_box (NULL, _("Could not create progress thread."), _("WinPT"), MB_ERR); |
132 |
|
|
pfx->error = 1; |
133 |
|
|
return; |
134 |
|
|
} |
135 |
twoaday |
229 |
curr_thread = pfx->thread_hd; |
136 |
werner |
36 |
log_debug ("progress_callback: started thread %08lX\r\n", tid); |
137 |
|
|
} |
138 |
|
|
else if (pfx->dlg != NULL) { |
139 |
|
|
size_t n = pfx->curr * 100 / pfx->total; |
140 |
twoaday |
229 |
SendDlgItemMessage (pfx->dlg, IDC_PROGRESS_BAR, |
141 |
|
|
PBM_SETPOS, (WPARAM)n, 0); |
142 |
werner |
36 |
SetDlgItemInt (pfx->dlg, IDC_PROGRESS_CURR, pfx->curr, FALSE); |
143 |
|
|
} |
144 |
twoaday |
229 |
if (off >= max) |
145 |
werner |
36 |
progress_cleanup (pfx); |
146 |
|
|
} |