1 |
/* wptOwnertrust.cpp - Ownertrust im- and export |
2 |
* Copyright (C) 2001, 2002, 2003, 2005 Timo Schulz |
3 |
* Copyright (C) 2005 g10 Code GmbH |
4 |
* |
5 |
* This file is part of WinPT. |
6 |
* |
7 |
* WinPT is free software; you can redistribute it and/or modify |
8 |
* it under the terms of the GNU General Public License as published by |
9 |
* the Free Software Foundation; either version 2 of the License, or |
10 |
* (at your option) any later version. |
11 |
* |
12 |
* WinPT is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with WinPT; if not, write to the Free Software Foundation, |
19 |
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
20 |
*/ |
21 |
|
22 |
#include <windows.h> |
23 |
#include <sys/stat.h> |
24 |
|
25 |
#include "../resource.h" |
26 |
#include "wptTypes.h" |
27 |
#include "wptGPG.h" |
28 |
#include "wptErrors.h" |
29 |
#include "wptNLS.h" |
30 |
#include "wptW32API.h" |
31 |
|
32 |
|
33 |
/* Export the ownertrust to the file @fname. |
34 |
Return value: 0 on success. */ |
35 |
static gpgme_error_t |
36 |
gpg_export_ownertrust (const char *fname) |
37 |
{ |
38 |
gpgme_error_t err; |
39 |
char *ot_data = NULL; |
40 |
FILE *f; |
41 |
|
42 |
err = gpg_manage_ownertrust (&ot_data, 1); |
43 |
if (err) |
44 |
return err; |
45 |
f = fopen (fname, "wb"); |
46 |
if (f) { |
47 |
fwrite (ot_data, 1, strlen (ot_data), f); |
48 |
fclose (f); |
49 |
} |
50 |
else |
51 |
err = gpg_error (GPG_ERR_ENOENT); |
52 |
free (ot_data); |
53 |
return 0; |
54 |
} |
55 |
|
56 |
|
57 |
/* Import the ownertrust from file @fname. |
58 |
Return value: 0 on success. */ |
59 |
static gpgme_error_t |
60 |
gpg_import_ownertrust (const char *fname) |
61 |
{ |
62 |
gpgme_error_t err; |
63 |
FILE *f; |
64 |
struct stat st; |
65 |
char *ot_data; |
66 |
|
67 |
if (stat (fname, &st)) |
68 |
return gpg_error (GPG_ERR_ENOENT); |
69 |
f = fopen (fname, "rb"); |
70 |
if (!f) |
71 |
return gpg_error (GPG_ERR_ENOENT); |
72 |
ot_data = (char*)calloc (1, st.st_size+1); |
73 |
if (!ot_data) |
74 |
BUG (NULL); |
75 |
fread (ot_data, 1, st.st_size, f); |
76 |
fclose (f); |
77 |
|
78 |
err = gpg_manage_ownertrust (&ot_data, 0); |
79 |
|
80 |
free (ot_data); |
81 |
return 0; |
82 |
} |
83 |
|
84 |
|
85 |
/* Dialog procedure to manage the ownertrust. */ |
86 |
BOOL CALLBACK |
87 |
ownertrust_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
88 |
{ |
89 |
gpgme_error_t err; |
90 |
const char *fname; |
91 |
|
92 |
switch( msg ) { |
93 |
case WM_INITDIALOG: |
94 |
#ifndef LANG_DE |
95 |
SetWindowText( dlg, _("Ownertrust") ); |
96 |
SetDlgItemText( dlg, IDC_OWNERTRUST_INFO, _("File") ); |
97 |
SetDlgItemText( dlg, IDC_OWNERTRUST_DESC, |
98 |
_("Here it is possible to save or restore the ownertrust from the " |
99 |
"'trustdb' file. This could be very useful because the values are NOT " |
100 |
"stored in the keyring.") ); |
101 |
#endif |
102 |
center_window( dlg, NULL ); |
103 |
SetForegroundWindow( dlg ); |
104 |
return TRUE; |
105 |
|
106 |
case WM_SYSCOMMAND: |
107 |
if (LOWORD (wparam) == SC_CLOSE) |
108 |
EndDialog (dlg, TRUE); |
109 |
return TRUE; |
110 |
|
111 |
case WM_COMMAND: |
112 |
switch (LOWORD (wparam)) { |
113 |
case IDC_OWNERTRUST_EXPORT: |
114 |
fname = get_filesave_dlg (dlg, _("Select file name for output"), NULL, NULL); |
115 |
if (!fname) |
116 |
return TRUE; |
117 |
err = gpg_export_ownertrust (fname); |
118 |
if (err) { |
119 |
msg_box (dlg, gpgme_strerror( err ), _("Ownertrust"), MB_ERR ); |
120 |
return TRUE; |
121 |
} |
122 |
status_box (dlg, _("Ownertrust successfully exported."), _("GnuPG Status")); |
123 |
return TRUE; |
124 |
|
125 |
case IDC_OWNERTRUST_IMPORT: |
126 |
fname = get_fileopen_dlg (dlg, _("Select file name for input"), NULL, NULL); |
127 |
if (!fname) |
128 |
return TRUE; |
129 |
err = gpg_import_ownertrust (fname); |
130 |
if (err) { |
131 |
msg_box (dlg, gpgme_strerror (err), _("Ownertrust"), MB_ERR); |
132 |
return TRUE; |
133 |
} |
134 |
status_box (dlg, _("Ownertrust succefully imported."), _("GnuPG Status")); |
135 |
return TRUE; |
136 |
|
137 |
case IDCANCEL: |
138 |
EndDialog (dlg, FALSE); |
139 |
return TRUE; |
140 |
} |
141 |
break; |
142 |
} |
143 |
|
144 |
return FALSE; |
145 |
} |