1 |
/* wptErrors.cpp - Error management |
2 |
* Copyright (C) 2000-2006 Timo Schulz |
3 |
* |
4 |
* This file is part of WinPT. |
5 |
* |
6 |
* WinPT is free software; you can redistribute it and/or |
7 |
* modify it under the terms of the GNU General Public License |
8 |
* as published by the Free Software Foundation; either version 2 |
9 |
* of the License, or (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 GNU |
14 |
* General Public License for more details. |
15 |
*/ |
16 |
#ifdef HAVE_CONFIG_H |
17 |
#include <config.h> |
18 |
#endif |
19 |
|
20 |
#include <windows.h> |
21 |
#include <stdio.h> |
22 |
|
23 |
#include "wptErrors.h" |
24 |
#include "wptTypes.h" |
25 |
#include "wptW32API.h" |
26 |
#include "wptVersion.h" |
27 |
#include "wptGPG.h" |
28 |
#include "wptNLS.h" |
29 |
|
30 |
static FILE *log_fp = NULL; |
31 |
|
32 |
|
33 |
/* Return a GPG specific error message. */ |
34 |
static char* |
35 |
gpg_strerror (int errid) |
36 |
{ |
37 |
static char buf[2*MAX_PATH+1]; |
38 |
|
39 |
char *path = get_gnupg_path (); |
40 |
switch (errid) { |
41 |
case WPTERR_GPG_EXEFILE: |
42 |
_snprintf (buf, DIM (buf) - 1, |
43 |
_("Could not locate GPG.exe in %s."), path); |
44 |
break; |
45 |
|
46 |
case WPTERR_GPG_OPT_KEYRINGS: |
47 |
case WPTERR_GPG_KEYRINGS: |
48 |
_snprintf (buf, DIM (buf) - 1, |
49 |
_("Could not find keyring entries in the config file in %s " |
50 |
"or the file does NOT exist."), path); |
51 |
break; |
52 |
} |
53 |
free_if_alloc (path); |
54 |
return buf; |
55 |
} |
56 |
|
57 |
|
58 |
static void |
59 |
w32_fmt_msg (char *buf, DWORD buflen) |
60 |
{ |
61 |
FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError (), |
62 |
MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), |
63 |
buf, buflen, NULL); |
64 |
} |
65 |
|
66 |
|
67 |
/* Return an error specific message. |
68 |
If there is a translation available, it returns the translated text. */ |
69 |
const char* |
70 |
winpt_strerror (int errid) |
71 |
{ |
72 |
static char buf[256]; |
73 |
|
74 |
switch (errid) { |
75 |
case -1: w32_fmt_msg (buf, DIM (buf)-1); return buf; |
76 |
case WPTERR_GENERAL: return _("General error occured"); |
77 |
case WPTERR_FILE_OPEN: return _("Could not open file"); |
78 |
case WPTERR_FILE_CREAT: return _("Could not create file"); |
79 |
case WPTERR_FILE_READ: return _("Could not read file"); |
80 |
case WPTERR_FILE_WRITE: return _("Could not write file"); |
81 |
case WPTERR_FILE_CLOSE: return _("Could not close file"); |
82 |
case WPTERR_FILE_EXIST: return _("File does not exist"); |
83 |
case WPTERR_FILE_REMOVE: return _("Could not delete file"); |
84 |
case WPTERR_CLIP_OPEN: return _("Could not open Clipboard"); |
85 |
case WPTERR_CLIP_CLOSE: return _("Could not close Clipboard"); |
86 |
case WPTERR_CLIP_EMPTY: return _("Could not empty Clipboard"); |
87 |
case WPTERR_CLIP_SET: return _("Could not set Clipboard data"); |
88 |
case WPTERR_CLIP_GET: return _("Could not get Clipboard data"); |
89 |
case WPTERR_CLIP_ISEMPTY: return _("There is no text in the Clipboard"); |
90 |
case WPTERR_CLIP_SECURED: return _("The Clipboard already contains GPG data"); |
91 |
case WPTERR_CLIP: return _("General Clipboard error"); |
92 |
case WPTERR_REGISTRY: _snprintf (buf, DIM (buf)-1, "%s", _("Registry error: ")); |
93 |
w32_fmt_msg (buf + strlen (buf), DIM (buf)/2); |
94 |
return buf; |
95 |
case WPTERR_WINSOCK_INIT: return _("Could not startup Winsock 2 interface"); |
96 |
case WPTERR_WINSOCK_RESOLVE: return _("Could not resolve hostname"); |
97 |
case WPTERR_WINSOCK_SOCKET: return _("Could not create new socket"); |
98 |
case WPTERR_WINSOCK_CONNECT: return _("Could not connect to the host"); |
99 |
case WPTERR_WINSOCK_SENDKEY: return _("Could not send the key to the keyserver"); |
100 |
case WPTERR_WINSOCK_RECVKEY: return _("Could not receive the key from the keyserver"); |
101 |
case WPTERR_WINSOCK_TIMEOUT: return _("Socket timed out, no data"); |
102 |
case WPTERR_WINSOCK_PROXY: return _("Could not forward request to proxy"); |
103 |
case WPTERR_KEYSERVER_NOTFOUND: return _("Keyserver returned: no matching keys in database"); |
104 |
case WPTERR_HOTKEY: _snprintf (buf, DIM (buf)-1, "%s", _("Could not register hotkey: ")); |
105 |
w32_fmt_msg (buf + strlen (buf), DIM (buf)/2); |
106 |
return buf; |
107 |
case WPTERR_DIR_OPEN: return _("Could not open directory"); |
108 |
case WPTERR_DIR_CREAT: return _("Could not create directory"); |
109 |
case WPTERR_CURR_WND: return _("Could not extract data from the current window"); |
110 |
case WPTERR_CONFIG_FILE: return _("Could not load config file"); |
111 |
case WPTERR_GPG_EXEFILE: |
112 |
case WPTERR_GPG_OPT_KEYRINGS: |
113 |
case WPTERR_GPG_KEYRINGS: return gpg_strerror (errid); |
114 |
case WPTERR_NODATA: return _("No data available"); |
115 |
case WPTERR_NOCARD: return _("There is no card in the reader"); |
116 |
case WPTERR_NOREADER: return _("There was no reader found"); |
117 |
case WPTERR_NOPGPCARD: return _("This is not an OpenPGP card"); |
118 |
default: _snprintf (buf, DIM (buf) - 1, |
119 |
_("Unknown error=%d"), errid); return buf; |
120 |
} |
121 |
|
122 |
return NULL; |
123 |
} |
124 |
|
125 |
|
126 |
/* Display a message box with the last system error. */ |
127 |
void |
128 |
winpt_errmsg (const char *name, int is_file) |
129 |
{ |
130 |
TCHAR buf[500], head[500]; |
131 |
DWORD last_err; |
132 |
|
133 |
memset (head, 0, sizeof head); |
134 |
last_err = GetLastError (); |
135 |
if (!name) |
136 |
strcpy (head, _("WinPT Error")); |
137 |
else if (is_file) |
138 |
_snprintf (head, DIM (head)-1, "%s: %d", name, last_err); |
139 |
else |
140 |
strncpy (head, name, DIM (head)-1); |
141 |
FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, last_err, |
142 |
MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), |
143 |
buf, DIM (buf)-1, NULL); |
144 |
MessageBox (NULL, buf, head, MB_OK); |
145 |
} |
146 |
|
147 |
|
148 |
/* Show debug message. */ |
149 |
void |
150 |
debug_box (const char *msg, const char *file, int line) |
151 |
{ |
152 |
#if _DEBUG |
153 |
char output[4096]; |
154 |
|
155 |
memset( output, 0, sizeof output ); |
156 |
_snprintf( output, sizeof output -1, |
157 |
"WinPT DBG:\n \"%s\", file %s, line %d\n", msg, file, line ); |
158 |
MessageBox( NULL, output, PGM_NAME, MB_OK | MB_ICONERROR ); |
159 |
#endif |
160 |
} |
161 |
|
162 |
|
163 |
void |
164 |
debug_f (const char *msg, const char *file, int line) |
165 |
{ |
166 |
#if _DEBUG |
167 |
log_debug ("DBG:\n \"%s\", file %s, line %d\n", msg, file, line); |
168 |
#endif |
169 |
} |
170 |
|
171 |
|
172 |
/* Log a message into the log file. */ |
173 |
void |
174 |
log_debug (const char *format, ...) |
175 |
{ |
176 |
char tmpdir[384]; |
177 |
va_list arg_ptr; |
178 |
|
179 |
if (!debug) |
180 |
return; |
181 |
|
182 |
if (log_fp == NULL) { |
183 |
GetTempPath (sizeof (tmpdir) - 32, tmpdir); |
184 |
strcat (tmpdir, "\\WinPT.LOG"); |
185 |
log_fp = fopen (tmpdir, "a+b"); |
186 |
if (!log_fp) |
187 |
return; |
188 |
} |
189 |
va_start (arg_ptr, format); |
190 |
vfprintf (log_fp, format, arg_ptr); |
191 |
va_end (arg_ptr); |
192 |
fflush (log_fp); |
193 |
} |
194 |
|
195 |
|
196 |
/* Cleanup */ |
197 |
void |
198 |
debug_end (void) |
199 |
{ |
200 |
if (log_fp != NULL) { |
201 |
fclose (log_fp); |
202 |
log_fp = NULL; |
203 |
} |
204 |
} |
205 |
|
206 |
|
207 |
/* Emulate an MessageBox with variable input. */ |
208 |
int |
209 |
printf_box (const char *title, int style, const char *format, ...) |
210 |
{ |
211 |
va_list arg_ptr; |
212 |
char log[4096]; |
213 |
|
214 |
va_start (arg_ptr, format); |
215 |
_vsnprintf (log, DIM (log)-1, format, arg_ptr); |
216 |
int id = msg_box (NULL, log, title, style); |
217 |
va_end (arg_ptr); |
218 |
|
219 |
return id; |
220 |
} |