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

Annotation of /trunk/Src/wptClipEncryptDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 105 - (hide annotations)
Wed Nov 30 10:22:00 2005 UTC (19 years, 3 months ago) by twoaday
File size: 6645 byte(s)
Several cleanups and improved translation.


1 werner 36 /* wptClipEncryptDlg.cpp - Clipboard encrypt dialog
2     * Copyright (C) 2000-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     #ifdef HAVE_CONFIG_H
22     #include <config.h>
23     #endif
24    
25     #include <windows.h>
26     #include <commctrl.h>
27    
28 werner 47 #include "resource.h"
29 werner 36 #include "wptTypes.h"
30     #include "wptW32API.h"
31     #include "wptVersion.h"
32     #include "wptErrors.h"
33     #include "wptCommonCtl.h"
34     #include "wptGPG.h"
35     #include "wptKeylist.h"
36     #include "wptNLS.h"
37     #include "wptContext.h" /* for passphrase_s */
38     #include "wptRegistry.h"
39     #include "wptDlgs.h"
40    
41    
42     /* Encrypt the contents of the clipboard with the keys in @rset.
43     If @always_trust is set, GPG is forced to trust any recipients.
44     The context is returned in @r_ctx.
45     Return value: 0 on success. */
46     gpgme_error_t
47     gpg_clip_encrypt (gpgme_key_t rset[], int always_trust, gpgme_ctx_t *r_ctx)
48     {
49     gpgme_error_t err;
50     gpgme_ctx_t ctx = NULL;
51     gpgme_data_t plain = NULL;
52     gpgme_data_t ciph = NULL;
53    
54     err = gpgme_new (&ctx);
55     if (err)
56     return err;
57    
58     gpgme_set_armor (ctx, 1);
59    
60     err = gpg_data_new_from_clipboard (&plain, 0);
61     if (err)
62     goto leave;
63     err = gpgme_data_new (&ciph);
64     if (err)
65     goto leave;
66     err = gpgme_op_encrypt (ctx, rset,
67     always_trust?GPGME_ENCRYPT_ALWAYS_TRUST : (gpgme_encrypt_flags_t)0,
68     plain, ciph);
69     *r_ctx = ctx;
70     if (err)
71     goto leave;
72    
73     gpg_data_release_and_set_clipboard (ciph, 1);
74     ciph = NULL;
75    
76     leave:
77     if (ciph)
78     gpgme_data_release (ciph);
79     gpgme_data_release (plain);
80     return err;
81     }
82    
83    
84     /* Show all invalid recipients if possible. @dlg is the
85     handle to the calling dialog and @ctx is the context
86     used for encryption.
87     Return value: 0 if invalid recipients exist -1 otherwise. */
88     static int
89     show_invalid_recipients (HWND dlg, gpgme_ctx_t ctx)
90     {
91     gpgme_encrypt_result_t res;
92     gpgme_invalid_key_t k;
93     gpgme_key_t key;
94     size_t len=0;
95     char *p;
96    
97     if (!ctx)
98     return -1;
99     res = gpgme_op_encrypt_result (ctx);
100     if (!res || !res->invalid_recipients)
101     return -1;
102    
103     for (k=res->invalid_recipients; k; k = k->next) {
104     get_pubkey (k->fpr, &key);
105     len += (32 + 16 + strlen (key->uids->name) + 2) + 4;
106     }
107    
108     p = (char *)calloc (1, len+64);
109     if (!p)
110     BUG (NULL);
111 twoaday 68 strcpy (p, _("Recipients unsuable for encryption:\n"));
112 werner 36 for (k = res->invalid_recipients; k; k = k->next) {
113     get_pubkey (k->fpr, &key);
114     strcat (p, key->subkeys->keyid+8);
115     strcat (p, " : ");
116     strcat (p, key->uids->name);
117     strcat (p, "\n");
118     }
119     msg_box (dlg, p, _("Encryption"), MB_ERR);
120     free (p);
121     return 0;
122     }
123    
124    
125     /* Dialog procedure for the clipboard encryption. */
126     BOOL CALLBACK
127     clip_encrypt_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
128     {
129     static listview_ctrl_t lv = NULL;
130     gpg_keycache_t kc;
131     gpgme_key_t *rset;
132     gpgme_ctx_t ctx=NULL;
133     gpgme_error_t err;
134 twoaday 68 int force_trust = 0;
135 werner 36 int kmode = reg_prefs.keylist_mode? KEYLIST_ENCRYPT_MIN : KEYLIST_ENCRYPT;
136     int n;
137    
138     switch( msg ) {
139     case WM_INITDIALOG:
140 twoaday 105 SetWindowText (dlg, _("Encryption"));
141     SetDlgItemText (dlg, IDC_ENCRYPT_FNDCMD, _("&Find"));
142     SetDlgItemText (dlg, IDCANCEL, _("&Cancel"));
143 werner 36 kc = keycache_get_ctx( KEYCACHE_PUB );
144     if( !kc )
145     BUG( NULL );
146     lv = keylist_load (GetDlgItem( dlg, IDC_ENCRYPT_KEYLIST ), kc, NULL,
147     kmode, KEY_SORT_USERID);
148     center_window( dlg, NULL );
149     SetForegroundWindow( dlg );
150     set_active_window( dlg );
151     return TRUE;
152    
153     case WM_DESTROY:
154     reset_active_window( );
155     if( lv ) {
156     keylist_delete( lv );
157     lv = NULL;
158     }
159     return FALSE;
160    
161     case WM_NOTIFY:
162     NMHDR *notify;
163    
164     notify = (NMHDR *)lparam;
165     if (notify && notify->code == NM_DBLCLK &&
166     notify->idFrom == IDC_ENCRYPT_KEYLIST)
167 twoaday 105 PostMessage( dlg, WM_COMMAND, MAKEWPARAM( IDOK, 0 ), 0 );
168 werner 36 if (notify && notify->code == LVN_COLUMNCLICK &&
169     notify->idFrom == IDC_ENCRYPT_KEYLIST ) {
170     NMLISTVIEW *p = (LPNMLISTVIEW) lparam;
171     int sortby = 0;
172    
173     switch( p->iSubItem ) {
174     case 0: sortby = KEY_SORT_USERID; break;
175     case 1: sortby = KEY_SORT_KEYID; break;
176     case 2: sortby = KEY_SORT_LEN; break;
177     case 4: sortby = KEY_SORT_VALIDITY; break;
178     default: sortby = KEY_SORT_USERID; break;
179     }
180     keylist_sort( lv, sortby );
181     }
182     return TRUE;
183    
184     case WM_SYSCOMMAND:
185     if( LOWORD( wparam ) == SC_CLOSE )
186     EndDialog( dlg, TRUE );
187     return FALSE;
188    
189     case WM_COMMAND:
190     switch( LOWORD( wparam ) ) {
191     case IDOK:
192     rset = keylist_get_recipients (lv, &force_trust, &n);
193     if (!n) {
194     msg_box (dlg, _("You must select at least one key."), _("Encryption"), MB_ERR);
195     free (rset);
196     return FALSE;
197     }
198     err = gpg_clip_encrypt (rset, force_trust, &ctx);
199     if (err) {
200     if (show_invalid_recipients (dlg, ctx))
201     msg_box (dlg, gpgme_strerror (err), _("Encryption"), MB_ERR);
202     if (ctx)
203     gpgme_release (ctx);
204     free (rset);
205     return FALSE;
206     }
207     else
208     show_msg( dlg, 1500, _("GnuPG Status: Finished") );
209     free (rset);
210     gpgme_release (ctx);
211     EndDialog (dlg, TRUE);
212     return TRUE;
213    
214     case IDCANCEL:
215     EndDialog( dlg, FALSE );
216     return FALSE;
217    
218     case IDC_ENCRYPT_FNDCMD:
219     {
220     char tmpbuf[64];
221    
222     n = GetDlgItemText (dlg, IDC_ENCRYPT_FIND, tmpbuf, DIM (tmpbuf)-1);
223     if (!n)
224     break;
225     n = listview_find (lv, tmpbuf);
226     if (n != -1) {
227     int oldpos = listview_get_curr_pos (lv);
228     listview_select_one (lv, n);
229     listview_scroll (lv, oldpos, n);
230     }
231     else
232     log_box (_("Encryption"), MB_ERR, _("No recipient found with '%s'"), tmpbuf);
233     break;
234     }
235     }
236     break;
237     }
238    
239     return FALSE;
240     }
241    

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26