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

Diff of /trunk/Src/wptClipEncryptDlg.cpp

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

revision 20 by twoaday, Wed Jul 27 11:17:22 2005 UTC revision 260 by twoaday, Wed Aug 16 10:01:30 2006 UTC
# Line 1  Line 1 
1  /* wptClipEncryptDlg.cpp - Clipboard encrypt dialog  /* wptClipEncryptDlg.cpp - Clipboard encrypt dialog
2   *      Copyright (C) 2000-2004 Timo Schulz   *      Copyright (C) 2000-2006 Timo Schulz
3   *   *      Copyright (C) 2005 g10 Code GmbH
4   * This file is part of WinPT.   *
5   *   * This file is part of WinPT.
6   * WinPT is free software; you can redistribute it and/or modify   *
7   * it under the terms of the GNU General Public License as published by   * WinPT is free software; you can redistribute it and/or modify
8   * the Free Software Foundation; either version 2 of the License, or   * it under the terms of the GNU General Public License as published by
9   * (at your option) any later version.   * the Free Software Foundation; either version 2 of the License, or
10   *   * (at your option) any later version.
11   * WinPT is distributed in the hope that it will be useful,   *
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of   * WinPT is distributed in the hope that it will be useful,
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * GNU General Public License for more details.   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   *   * GNU General Public License for more details.
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,   * You should have received a copy of the GNU General Public License
18   * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA   * 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  #include <windows.h>  
22  #include <commctrl.h>  #ifdef HAVE_CONFIG_H
23    #include <config.h>
24  #include "../resource.h"  #endif
25  #include "wptTypes.h"  
26  #include "wptW32API.h"  #include <windows.h>
27  #include "wptVersion.h"  #include <commctrl.h>
28  #include "wptErrors.h"  
29  #include "wptCommonCtl.h"  #include "resource.h"
30  #include "wptGPG.h"  #include "wptTypes.h"
31  #include "wptKeylist.h"  #include "wptW32API.h"
32  #include "wptNLS.h"  #include "wptVersion.h"
33  #include "wptContext.h" /* for passphrase_s */  #include "wptErrors.h"
34  #include "wptRegistry.h"  #include "wptCommonCtl.h"
35  #include "wptDlgs.h"  #include "wptGPG.h"
36    #include "wptKeylist.h"
37    #include "wptNLS.h"
38  BOOL CALLBACK  #include "wptContext.h" /* for passphrase_s */
39  clip_encrypt_dlg_proc( HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam )  #include "wptRegistry.h"
40  {  #include "wptDlgs.h"
41      static listview_ctrl_t lv = NULL;  #include "wptUTF8.h"
42      gpgme_keycache_t kc;  
43      gpgme_recipients_t rset;  
44      gpgme_ctx_t ctx;  /* Encrypt the contents of the clipboard with the keys in @rset.
45      gpgme_error_t err;     If @always_trust is set, GPG is forced to trust any recipients.
46      refresh_cache_s rcs = {0};     The context is returned in @r_ctx.
47      int force_trust = 0, opt = 0;     Return value: 0 on success. */
48      int kmode = reg_prefs.keylist_mode? KEYLIST_ENCRYPT_MIN : KEYLIST_ENCRYPT;  gpgme_error_t
49        gpg_clip_encrypt (gpgme_key_t rset[], int always_trust, gpgme_ctx_t *r_ctx)
50      switch( msg ) {  {
51      case WM_INITDIALOG:      gpgme_error_t err;
52          SetWindowText( dlg, _("Encryption") );      gpgme_encrypt_flags_t flags;
53                        gpgme_ctx_t ctx = NULL;
54          if( keycache_get_reload() ) {      gpgme_data_t plain = NULL;
55              rcs.kr_reload = rcs.kr_update = 1;      gpgme_data_t ciph = NULL;
56              rcs.tr_update = 0;              
57              DialogBoxParam( glob_hinst, (LPCSTR)IDD_WINPT_KEYCACHE, GetDesktopWindow(),      err = gpgme_new (&ctx);
58                              keycache_dlg_proc, (LPARAM)&rcs );      if (err)
59          }          return err;
60          kc = keycache_get_ctx( KEYCACHE_PUB );      
61          if( !kc )      gpgme_set_armor (ctx, 1);
62              BUG( NULL );      gpgme_set_textmode (ctx, 1);
63          lv = keylist_load( GetDlgItem( dlg, IDC_ENCRYPT_KEYLIST ), kc, NULL,      
64                              kmode, GPGME_ATTR_USERID );      err = gpg_data_new_from_clipboard (&plain, 0);
65          center_window( dlg );      if (err)
66          SetForegroundWindow( dlg );          goto leave;
67          set_active_window( dlg );      err = gpgme_data_new (&ciph);
68          return TRUE;      if (err)
69                    goto leave;
70      case WM_DESTROY:      flags = always_trust? GPGME_ENCRYPT_ALWAYS_TRUST : (gpgme_encrypt_flags_t)0;
71          reset_active_window( );      err = gpgme_op_encrypt (ctx, rset, flags, plain, ciph);
72          if( lv ) {      *r_ctx = ctx;
73              keylist_delete( lv );      if (err)
74              lv = NULL;          goto leave;
75          }      
76          return FALSE;      gpg_data_release_and_set_clipboard (ciph, 1);
77                ciph = NULL;
78      case WM_NOTIFY:  
79          NMHDR *notify;  leave:
80                if (ciph)
81          notify = (NMHDR *)lparam;          gpgme_data_release (ciph);
82          if( notify && notify->code == NM_DBLCLK && notify->idFrom == IDC_ENCRYPT_KEYLIST )      gpgme_data_release (plain);
83              PostMessage( dlg, WM_COMMAND, MAKEWPARAM( IDOK, 0 ), NULL );      return err;
84          if( notify && notify->code == LVN_COLUMNCLICK && notify->idFrom == IDC_ENCRYPT_KEYLIST ) {  }
85              NMLISTVIEW *p = (LPNMLISTVIEW) lparam;  
86              int sortby = 0;  
87    /* Show all invalid recipients if possible. @dlg is the
88              switch( p->iSubItem ) {     handle to the calling dialog and @ctx is the context
89              case  0: sortby = GPGME_ATTR_USERID; break;     used for encryption.
90              case  1: sortby = GPGME_ATTR_KEYID; break;     Return value: 0 if invalid recipients exist -1 otherwise. */
91              case  2: sortby = GPGME_ATTR_LEN; break;  static int
92              case  4: sortby = GPGME_ATTR_VALIDITY; break;  show_invalid_recipients (HWND dlg, gpgme_ctx_t ctx)
93              default: sortby = GPGME_ATTR_USERID; break;  {
94              }      gpgme_encrypt_result_t res;
95              keylist_sort( lv, sortby );      gpgme_invalid_key_t k;
96          }      gpgme_key_t key;
97          return TRUE;      size_t len=0;
98                const char *keyid;
99      case WM_SYSCOMMAND:      const char *warn = _("key not found");
100          if( LOWORD( wparam ) == SC_CLOSE )      char *uid, *p;
101              EndDialog( dlg, TRUE );  
102          return FALSE;      if (!ctx)
103                    return -1;
104      case WM_COMMAND:      res = gpgme_op_encrypt_result (ctx);
105          switch( LOWORD( wparam ) ) {      if (!res || !res->invalid_recipients)
106          case IDOK:          return -1;
107              rset = keylist_get_recipients (lv, &force_trust, NULL);  
108              if( !gpgme_recipients_count( rset ) ) {      for (k=res->invalid_recipients; k; k = k->next) {
109                  msg_box( dlg, _("You must select at least one key."), _("Encryption"), MB_ERR );          if (!get_pubkey (k->fpr, &key))
110                  gpgme_recipients_release( rset );              len += (32 + strlen (k->fpr)+8 + strlen (key->uids->name) + 2) + 4;
111                  return FALSE;          else
112              }              len += strlen (warn) + strlen (k->fpr)+8 + 2 + 4;
113              if( force_trust )      }
114                  opt |= GPGME_CTRL_FORCETRUST;  
115              if( reg_prefs.use_tmpfiles )      p = (char *)calloc (1, len+64);
116                  opt |= GPGME_CTRL_TMPFILES;      if (!p)
117              err = gpgme_op_clip_encrypt( rset, opt, &ctx );          BUG (NULL);
118              if( err && err != GPGME_Inv_Recipients ) {      strcpy (p, _("Recipients unsuable for encryption:\n"));
119                  if( err == GPGME_Internal_GPG_Problem )      for (k = res->invalid_recipients; k; k = k->next) {
120                      gnupg_display_error ();          if (!get_pubkey (k->fpr, &key)) {
121                  else              uid = utf8_to_native (key->uids->name);
122                      msg_box( dlg, gpgme_strerror( err ), _("Encryption"), MB_ERR );              keyid = key->subkeys->keyid+8;
123                  gpgme_release( ctx );          }
124                  gpgme_recipients_release( rset );          else {
125                  return FALSE;              uid = strdup (warn);
126              }              keyid = k->fpr;
127              else if( err == GPGME_Inv_Recipients ) {          }
128                  int ncount = gpgme_recperr_count_items( ctx );          strcat (p, keyid);
129                  gpgme_error_t code = GPGME_No_Error;          strcat (p, " : ");
130            strcat (p, uid);
131                  while( ncount-- ) {          strcat (p, "\n");
132                      const char *s = gpgme_recperr_get( ctx, ncount, &code );          safe_free (uid);
133                      if( s )      }
134                          log_box( _("Encryption"), MB_ERR,      msg_box (dlg, p, _("Encryption"), MB_ERR);
135                                   _("Unusable recipient \"%s\": %s"), s,      safe_free (p);
136                                   gpgme_strerror( code ) );      return 0;
137                  }  }
138              }  
139              else  
140                  show_msg( dlg, 1500, _("GnuPG Status: Finished") );  /* Dialog procedure for the clipboard encryption. */
141              gpgme_recipients_release( rset );  BOOL CALLBACK
142              gpgme_release( ctx );  clip_encrypt_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
143              EndDialog( dlg, TRUE );  {
144              return TRUE;      static listview_ctrl_t lv = NULL;
145                    static int keys_sortby = 0;
146          case IDCANCEL:      gpg_keycache_t kc;
147              EndDialog( dlg, FALSE );      gpgme_key_t *rset;
148              return FALSE;      gpgme_ctx_t ctx=NULL;
149        gpgme_error_t err;
150          case IDC_ENCRYPT_FNDCMD:      char tmpbuf[64];
151          {      size_t n;
152              char tmpbuf[64];      int force_trust = 0;    
153              int n;      
154        switch (msg) {
155              n = GetDlgItemText (dlg, IDC_ENCRYPT_FIND, tmpbuf, DIM (tmpbuf)-1);      case WM_INITDIALOG:
156              if (!n)          SetWindowText (dlg, _("Encryption"));
157                  break;          SetDlgItemText (dlg, IDC_ENCRYPT_FNDCMD, _("&Find"));
158              n = listview_find (lv, tmpbuf);          SetDlgItemText (dlg, IDCANCEL, _("&Cancel"));
159              if (n != -1)          kc = keycache_get_ctx (KEYCACHE_PUB);
160                  listview_select_one (lv, n);          lv = keylist_load (GetDlgItem (dlg, IDC_ENCRYPT_KEYLIST), kc, NULL,
161              else                             KEYLIST_ENCRYPT_MIN, KEY_SORT_USERID);
162                  log_box (_("Encryption"), MB_ERR, _("No recipient found with '%s'"), tmpbuf);          center_window (dlg, NULL);
163              break;          SetForegroundWindow (dlg);
164          }          return TRUE;
165          }          
166          break;      case WM_DESTROY:
167      }          if (lv) {
168                    keylist_delete (lv);
169      return FALSE;              lv = NULL;
170  } /* clip_encrypt_dlg_proc */          }
171            return FALSE;
172            
173        case WM_NOTIFY:
174            NMHDR *notify;
175            
176            notify = (NMHDR *)lparam;
177            if (notify && notify->code == NM_DBLCLK &&
178                notify->idFrom == IDC_ENCRYPT_KEYLIST)
179                PostMessage( dlg, WM_COMMAND, MAKEWPARAM (IDOK, 0), 0);
180            if (notify && notify->code == LVN_COLUMNCLICK &&
181                notify->idFrom == IDC_ENCRYPT_KEYLIST) {
182                NMLISTVIEW *p = (LPNMLISTVIEW) lparam;
183                int sortby = 0;
184    
185                switch (p->iSubItem) {
186                case  0: sortby = KEY_SORT_USERID; break;
187                case  1: sortby = KEY_SORT_KEYID; break;
188                case  2: sortby = KEY_SORT_LEN; break;
189                case  4: sortby = KEY_SORT_VALIDITY; break;
190                default: sortby = KEY_SORT_USERID; break;
191                }
192                if ((keys_sortby & ~KEYLIST_SORT_DESC) == sortby)
193                    keys_sortby ^= KEYLIST_SORT_DESC;
194                else
195                    keys_sortby = sortby;
196                keylist_sort (lv, keys_sortby);
197            }
198            return TRUE;
199            
200        case WM_COMMAND:
201            switch (LOWORD (wparam)) {
202            case IDOK:
203                rset = keylist_get_recipients (lv, &force_trust, &n);
204                if (!n) {
205                    msg_box (dlg, _("You must select at least one key."),
206                             _("Encryption"), MB_ERR);
207                    safe_free (rset);
208                    return FALSE;
209                }
210                err = gpg_clip_encrypt (rset, force_trust, &ctx);
211                if (err) {
212                    if (show_invalid_recipients (dlg, ctx))
213                        msg_box (dlg, gpgme_strerror (err), _("Encryption"), MB_ERR);
214                }
215                else
216                    show_msg (dlg, 1500, _("GnuPG Status: Finished"));
217                safe_free (rset);
218                if (ctx)
219                    gpgme_release (ctx);
220                if (!err)
221                    EndDialog (dlg, TRUE);
222                return TRUE;
223                
224            case IDCANCEL:
225                EndDialog (dlg, FALSE);
226                return FALSE;
227    
228            case IDC_ENCRYPT_FNDCMD:
229                n = GetDlgItemText (dlg, IDC_ENCRYPT_FIND, tmpbuf, DIM (tmpbuf)-1);
230                if (!n)
231                    break;
232                n = listview_find (lv, tmpbuf, 0);
233                if (n != -1) {
234                    int oldpos = listview_get_curr_pos (lv);
235                    listview_select_one (lv, n);
236                    listview_scroll (lv, oldpos, n);
237                }
238                else
239                    log_box (_("Encryption"), MB_ERR,
240                             _("No recipient found with '%s'"), tmpbuf);
241                break;
242            }
243            break;
244        }
245        
246        return FALSE;
247    }
248    

Legend:
Removed from v.20  
changed lines
  Added in v.260

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26