/[winpt]/trunk/Src/wptErrors.cpp
ViewVC logotype

Diff of /trunk/Src/wptErrors.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 24 by twoaday, Sat Oct 8 10:43:08 2005 UTC revision 278 by twoaday, Mon Jan 15 22:02:04 2007 UTC
# Line 1  Line 1 
 /* wptErrors.cpp - Error management  
  *      Copyright (C) 2000-2004 Timo Schulz  
  *  
  * This file is part of WinPT.  
  *  
  * WinPT is free software; you can redistribute it and/or  
  * modify it under the terms of the GNU General Public License  
  * as published by the Free Software Foundation; either version 2  
  * of the License, or (at your option) any later version.  
  *    
  * WinPT is distributed in the hope that it will be useful,  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU  
  * General Public License for more details.  
  *  
  * You should have received a copy of the GNU General Public License  
  * along with WinPT; if not, write to the Free Software Foundation,  
  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA  
  */  
   
 #include <windows.h>  
 #include <stdio.h>  
   
 #include "wptErrors.h"  
 #include "wptTypes.h"  
 #include "wptW32API.h"  
 #include "wptVersion.h"  
 #include "wptGPG.h"  
 #include "wptNLS.h"  
   
   
 static char *  
 gpg_strerror( int errid )  
 {  
     static char buf[512];  
     char * path = NULL;  
   
     path = get_gnupg_path();  
     if( !path )  
         BUG( NULL );  
     switch( errid ) {  
     case WPTERR_GPG_EXEFILE:  
         _snprintf( buf, sizeof buf - 1, _("Could not locate GPG.exe in %s."), path );    
         break;  
     case WPTERR_GPG_OPT_KEYRINGS:  
     case WPTERR_GPG_KEYRINGS:  
         _snprintf( buf, sizeof buf - 1,  
                    _("Could not find keyring entries into the options file in %s "  
                      "or the file does NOT exist."), path );  
         break;  
     }  
     free_if_alloc( path );  
     return buf;  
 } /* gpg_strerror */  
   
   
 static void  
 w32_fmt_msg (char *buf, size_t buflen)  
 {  
     FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError (),  
                    MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),  
                    buf, buflen, NULL);  
 }  
   
 /* Return an error specific message.  
    If there is a translation available, it returns the translated text. */  
 const char*  
 winpt_strerror (int errid)  
 {        
     static char buf[256];  
           
     switch( errid ) {  
     case WPTERR_GENERAL: return _("General error occured");  
     case WPTERR_FILE_OPEN: return _("Could not open file");  
     case WPTERR_FILE_CREAT: return _("Could not create file");  
     case WPTERR_FILE_READ: return _("Could not read file");  
     case WPTERR_FILE_WRITE: return _("Could not write file");  
     case WPTERR_FILE_CLOSE: return _("Could not close file");  
     case WPTERR_FILE_EXIST: return _("File does not exist");  
     case WPTERR_FILE_REMOVE: return _("Could not delete file");  
     case WPTERR_CLIP_OPEN: return _("Could not open Clipboard");  
     case WPTERR_CLIP_CLOSE: return _("Could not close Clipboard");  
     case WPTERR_CLIP_EMPTY: return _("Could not empty Clipboard");  
     case WPTERR_CLIP_SET: return _("Could not set Clipboard data");  
     case WPTERR_CLIP_GET: return _("Could not get Clipboard data");  
     case WPTERR_CLIP_ISEMPTY: return _("There is no text in the Clipboard");  
     case WPTERR_CLIP_SECURED: return _("The Clipboard already contains GPG data");  
     case WPTERR_CLIP: return _("General Clipboard error");  
     case WPTERR_REGISTRY: sprintf (buf, "%s", _("Registry error: "));  
                           w32_fmt_msg (buf + strlen (buf), 128);  
                           break;  
     case WPTERR_WINSOCK_INIT: return _("Could not startup Winsock 2 interface");  
     case WPTERR_WINSOCK_RESOLVE: return _("Could not resolve hostname");  
     case WPTERR_WINSOCK_SOCKET: return _("Could not create new socket");  
     case WPTERR_WINSOCK_CONNECT: return _("Could not connect to the host");  
     case WPTERR_WINSOCK_SENDKEY: return _("Could not send the key to the keyserver");  
     case WPTERR_WINSOCK_RECVKEY: return _("Could not receive the key from the keyserver");  
     case WPTERR_WINSOCK_TIMEOUT: return _("Socket timed out, no data");  
     case WPTERR_KEYSERVER_NOTFOUND: return _("Keyserver returned: no matching keys in database");  
     case WPTERR_HOTKEY: sprintf (buf, "%s", _("Could not register hotkey: "));  
                         w32_fmt_msg (buf + strlen (buf), 128);  
                         break;  
     case WPTERR_DIR_OPEN: return _("Could not open directory");  
     case WPTERR_DIR_CREAT: return _("Could not create directory");  
     case WPTERR_CURR_WND: return _("Could not extract data from the current window");  
     case WPTERR_CONFIG_FILE: return _("Could not load config file");      
     case WPTERR_GPG_EXEFILE:      
     case WPTERR_GPG_OPT_KEYRINGS:  
     case WPTERR_GPG_KEYRINGS: return gpg_strerror (errid);  
     case WPTERR_NOCARD: return _("There is no card in the reader");  
     case WPTERR_NOREADER: return _("There was no reader found");  
     case WPTERR_NOPGPCARD: return _("This is not an OpenPGP card");  
     case WPTERR_CDISK_LOCK: return _("Could not lock or unlock volume");  
     case WPTERR_CDISK_MOUNT: return _("Could not mount volume");  
     case WPTERR_CDISK_UMOUNT: return _("Could not unmount volume");  
     case WPTERR_CDISK_OPEN: return _("Could not  open volume");  
     case WPTERR_CDISK_BUSY: return _("Drive that belongs to the volume is busy");  
     case WPTERR_CDISK_QUERY: return _("Could not query volume information");  
     default: _snprintf (buf, sizeof (buf) - 1, _("Unknown error=%d"), errid); return buf;  
     }  
   
     return NULL;  
 } /* winpt_strerror */  
   
   
 void  
 winpt_errmsg (const char * name, int is_file)  
 {        
     TCHAR buf[500], head[500];  
     DWORD last_err;  
   
     memset (head, 0, sizeof head);  
     last_err = GetLastError ();  
     if (!name)  
         strcpy (head, _("WinPT Error"));  
     else if (is_file)  
         _snprintf (head, DIM (head)-1, "%s: %d", name, last_err);  
     else  
         strncpy (head, name, DIM (head)-1);  
     FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, last_err,  
                    MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),  
                    buf, DIM (buf)-1, NULL);  
     MessageBox (NULL, buf, head, MB_OK);  
 } /* winpt_errmsg */  
   
   
 void  
 winpt_debug_msg( void )  
 {        
     char output[768], temp[384];  
           
     GetTempPath( sizeof temp -1, temp );  
     _snprintf( output, sizeof output - 1,  
         "The GPGME output file is gpgme.dbg\n"  
         "The WinPT output file is %swinpt.log\n", temp );  
     MessageBox( NULL, output, "WinPT now runs in DEBUG MODE", MB_INFO );  
 } /* winpt_debug_msg */  
   
   
 void  
 debug_box( const char *msg, const char *file, int line )  
 {  
 #if _DEBUG  
     char output[4096];  
   
     memset( output, 0, sizeof output );  
     _snprintf( output, sizeof output -1,  
         "WinPT DBG:\n \"%s\", file %s, line %d\n", msg, file, line );    
     MessageBox( NULL, output, PGM_NAME, MB_OK | MB_ICONERROR );  
 #endif  
 } /* debug_box */  
   
   
 void  
 debug_f( const char *msg, const char *file, int line )  
 {  
 #if _DEBUG  
     FILE *fp;  
     char temp_path[ 1024 ];  
     char output[ 4096 ];  
   
     GetTempPath( sizeof temp_path - 1, temp_path );      
     strcat( temp_path, "\\WinPT.DEBUG" );        
     fp = fopen( temp_path, "a+b" );      
     if( !fp )  
         return;  
     memset( output, 0, sizeof output );  
     _snprintf( output, sizeof output -1,  
                "WinPT DBG:\n \"%s\", file %s, line %d\n", msg, file, line );  
     fwrite( output, 1, strlen( output ), fp );  
     fflush( fp );  
     fclose( fp );  
 #endif  
 } /* debug_f */  
   
   
 void  
 log_debug (const char *format, ...)  
 {  
     FILE *fp;  
     char temp_path[384];  
     va_list arg_ptr;  
   
     if (!debug)  
         return;  
   
     GetTempPath (sizeof temp_path - 1, temp_path);  
     strcat (temp_path, "\\WinPT.LOG");  
     fp = fopen (temp_path, "a+b");  
     if (!fp)  
         return;  
     va_start (arg_ptr, format);  
     vfprintf (fp, format, arg_ptr);  
     va_end (arg_ptr);  
     fflush (fp);  
     fclose (fp);  
 }  
   
   
 int  
 log_box (const char *title, int style, const char *format, ...)  
 {  
     va_list arg_ptr;  
     char log[8192];  
     int id;  
   
     va_start (arg_ptr, format);  
     _vsnprintf (log, sizeof (log)-1, format, arg_ptr);      
     id = msg_box (NULL, log, title? title : "Log Output", style);  
     va_end( arg_ptr );  
   
     return id;  
 } /* log_box */  
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     * 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 <stdio.h>
27    
28    #include "wptErrors.h"
29    #include "wptTypes.h"
30    #include "wptW32API.h"
31    #include "wptVersion.h"
32    #include "wptGPG.h"
33    #include "wptNLS.h"
34    
35    static FILE *log_fp = NULL;
36    
37    
38    /* Return a GPG specific error message. */
39    static char*
40    gpg_strerror (int errid)
41    {
42        static char buf[2*MAX_PATH+1];
43        char *path;
44    
45        path = get_gnupg_path ();
46        if (!path)
47            BUG (NULL);
48        switch (errid) {
49        case WPTERR_GPG_EXEFILE:
50            _snprintf (buf, DIM (buf) - 1,
51                        _("Could not locate GPG.exe in %s."), path);
52            break;
53        case WPTERR_GPG_OPT_KEYRINGS:
54        case WPTERR_GPG_KEYRINGS:
55            _snprintf (buf, DIM (buf) - 1,
56                       _("Could not find keyring entries in the config file in %s "
57                         "or the file does NOT exist."), path);
58            break;
59        }
60        free_if_alloc (path);
61        return buf;
62    }
63    
64    
65    static void
66    w32_fmt_msg (char *buf, size_t buflen)
67    {
68        FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError (),
69                       MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
70                       buf, buflen, NULL);
71    }
72    
73    
74    /* Return an error specific message.
75       If there is a translation available, it returns the translated text. */
76    const char*
77    winpt_strerror (int errid)
78    {      
79        static char buf[256];
80            
81        switch (errid) {
82        case -1: w32_fmt_msg (buf, DIM (buf)-1); return buf;
83        case WPTERR_GENERAL: return _("General error occured");
84        case WPTERR_FILE_OPEN: return _("Could not open file");
85        case WPTERR_FILE_CREAT: return _("Could not create file");
86        case WPTERR_FILE_READ: return _("Could not read file");
87        case WPTERR_FILE_WRITE: return _("Could not write file");
88        case WPTERR_FILE_CLOSE: return _("Could not close file");
89        case WPTERR_FILE_EXIST: return _("File does not exist");
90        case WPTERR_FILE_REMOVE: return _("Could not delete file");
91        case WPTERR_CLIP_OPEN: return _("Could not open Clipboard");
92        case WPTERR_CLIP_CLOSE: return _("Could not close Clipboard");
93        case WPTERR_CLIP_EMPTY: return _("Could not empty Clipboard");
94        case WPTERR_CLIP_SET: return _("Could not set Clipboard data");
95        case WPTERR_CLIP_GET: return _("Could not get Clipboard data");
96        case WPTERR_CLIP_ISEMPTY: return _("There is no text in the Clipboard");
97        case WPTERR_CLIP_SECURED: return _("The Clipboard already contains GPG data");
98        case WPTERR_CLIP: return _("General Clipboard error");
99        case WPTERR_REGISTRY: _snprintf (buf, DIM (buf)-1, "%s", _("Registry error: "));
100                              w32_fmt_msg (buf + strlen (buf), DIM (buf)/2);
101                              return buf;
102        case WPTERR_WINSOCK_INIT: return _("Could not startup Winsock 2 interface");
103        case WPTERR_WINSOCK_RESOLVE: return _("Could not resolve hostname");
104        case WPTERR_WINSOCK_SOCKET: return _("Could not create new socket");
105        case WPTERR_WINSOCK_CONNECT: return _("Could not connect to the host");
106        case WPTERR_WINSOCK_SENDKEY: return _("Could not send the key to the keyserver");
107        case WPTERR_WINSOCK_RECVKEY: return _("Could not receive the key from the keyserver");
108        case WPTERR_WINSOCK_TIMEOUT: return _("Socket timed out, no data");
109        case WPTERR_WINSOCK_PROXY: return _("Could not forward request to proxy");
110        case WPTERR_KEYSERVER_NOTFOUND: return _("Keyserver returned: no matching keys in database");
111        case WPTERR_HOTKEY: _snprintf (buf, DIM (buf)-1, "%s", _("Could not register hotkey: "));
112                            w32_fmt_msg (buf + strlen (buf), DIM (buf)/2);
113                            return buf;
114        case WPTERR_DIR_OPEN: return _("Could not open directory");
115        case WPTERR_DIR_CREAT: return _("Could not create directory");
116        case WPTERR_CURR_WND: return _("Could not extract data from the current window");
117        case WPTERR_CONFIG_FILE: return _("Could not load config file");    
118        case WPTERR_GPG_EXEFILE:    
119        case WPTERR_GPG_OPT_KEYRINGS:
120        case WPTERR_GPG_KEYRINGS: return gpg_strerror (errid);
121        case WPTERR_NODATA: return _("No data available");
122        case WPTERR_NOCARD: return _("There is no card in the reader");
123        case WPTERR_NOREADER: return _("There was no reader found");
124        case WPTERR_NOPGPCARD: return _("This is not an OpenPGP card");
125            /*
126        case WPTERR_CDISK_LOCK: return _("Could not lock or unlock volume");
127        case WPTERR_CDISK_MOUNT: return _("Could not mount volume");
128        case WPTERR_CDISK_UMOUNT: return _("Could not unmount volume");
129        case WPTERR_CDISK_OPEN: return _("Could not  open volume");
130        case WPTERR_CDISK_BUSY: return _("Drive that belongs to the volume is busy");
131        case WPTERR_CDISK_QUERY: return _("Could not query volume information");
132            */
133        default: _snprintf (buf, DIM (buf) - 1,
134                            _("Unknown error=%d"), errid); return buf;
135        }
136    
137        return NULL;
138    }
139    
140    
141    /* Display a message box with the last system error. */
142    void
143    winpt_errmsg (const char *name, int is_file)
144    {      
145        TCHAR buf[500], head[500];
146        DWORD last_err;
147    
148        memset (head, 0, sizeof head);
149        last_err = GetLastError ();
150        if (!name)
151            strcpy (head, _("WinPT Error"));
152        else if (is_file)
153            _snprintf (head, DIM (head)-1, "%s: %d", name, last_err);
154        else
155            strncpy (head, name, DIM (head)-1);
156        FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, last_err,
157                       MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
158                       buf, DIM (buf)-1, NULL);
159        MessageBox (NULL, buf, head, MB_OK);
160    }
161    
162    
163    /* Show debug message. */
164    void
165    debug_box (const char *msg, const char *file, int line)
166    {
167        #if _DEBUG
168        char output[4096];
169    
170        memset( output, 0, sizeof output );
171        _snprintf( output, sizeof output -1,
172            "WinPT DBG:\n \"%s\", file %s, line %d\n", msg, file, line );  
173        MessageBox( NULL, output, PGM_NAME, MB_OK | MB_ICONERROR );
174        #endif
175    }
176    
177    
178    void
179    debug_f (const char *msg, const char *file, int line)
180    {
181        #if _DEBUG
182        log_debug ("DBG:\n \"%s\", file %s, line %d\n", msg, file, line);
183        #endif
184    }
185    
186    
187    /* Log a message into the log file. */
188    void
189    log_debug (const char *format, ...)
190    {    
191        char tmpdir[384];
192        va_list arg_ptr;
193    
194        if (!debug)
195            return;
196    
197        if (log_fp == NULL) {
198            GetTempPath (sizeof (tmpdir) - 32, tmpdir);
199            strcat (tmpdir, "\\WinPT.LOG");
200            log_fp = fopen (tmpdir, "a+b");
201            if (!log_fp)
202                return;
203        }
204        va_start (arg_ptr, format);
205        vfprintf (log_fp, format, arg_ptr);
206        va_end (arg_ptr);
207        fflush (log_fp);
208    }
209    
210    
211    /* Cleanup */
212    void
213    debug_end (void)
214    {
215        if (log_fp != NULL)
216            fclose (log_fp);
217    }
218    
219    
220    /* Emulate an MessageBox with variable input. */
221    int
222    printf_box (const char *title, int style, const char *format, ...)
223    {
224        va_list arg_ptr;
225        char log[4096];
226        int id;
227    
228        va_start (arg_ptr, format);
229        _vsnprintf (log, sizeof (log)-1, format, arg_ptr);    
230        id = msg_box (NULL, log, title, style);
231        va_end (arg_ptr);
232    
233        return id;
234    }

Legend:
Removed from v.24  
changed lines
  Added in v.278

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26