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

Annotation of /trunk/Src/wptClipEncryptDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 25 - (hide annotations)
Wed Oct 12 10:04:26 2005 UTC (19 years, 4 months ago) by twoaday
File size: 6792 byte(s)
First testing phase finished.
Provide bug fixes for a lot of (minor) problems.

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26