/[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 48 by werner, Mon Oct 31 21:14:11 2005 UTC revision 451 by twoaday, Sat May 5 14:06:10 2012 UTC
# Line 1  Line 1 
1  /* wptClipEncryptDlg.cpp - Clipboard encrypt dialog  /* wptClipEncryptDlg.cpp - Clipboard encrypt dialog
2   *      Copyright (C) 2000-2005 Timo Schulz   *      Copyright (C) 2000-2006, 2009 Timo Schulz
3   *      Copyright (C) 2005 g10 Code GmbH   *      Copyright (C) 2005 g10 Code GmbH
4   *   *
5   * This file is part of WinPT.   * This file is part of WinPT.
# Line 13  Line 13 
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.   * 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  
16   */   */
   
17  #ifdef HAVE_CONFIG_H  #ifdef HAVE_CONFIG_H
18  #include <config.h>  #include <config.h>
19  #endif  #endif
# Line 38  Line 33 
33  #include "wptContext.h" /* for passphrase_s */  #include "wptContext.h" /* for passphrase_s */
34  #include "wptRegistry.h"  #include "wptRegistry.h"
35  #include "wptDlgs.h"  #include "wptDlgs.h"
36    #include "wptUTF8.h"
37    #include "StringBuffer.h"
38    
39    
40  /* Encrypt the contents of the clipboard with the keys in @rset.  /* Encrypt the contents of the clipboard with the keys in @rset.
# Line 48  gpgme_error_t Line 45  gpgme_error_t
45  gpg_clip_encrypt (gpgme_key_t rset[], int always_trust, gpgme_ctx_t *r_ctx)  gpg_clip_encrypt (gpgme_key_t rset[], int always_trust, gpgme_ctx_t *r_ctx)
46  {  {
47      gpgme_error_t err;      gpgme_error_t err;
48        gpgme_encrypt_flags_t flags;
49      gpgme_ctx_t ctx = NULL;      gpgme_ctx_t ctx = NULL;
50      gpgme_data_t plain = NULL;      gpgme_data_t plain = NULL;
51      gpgme_data_t ciph = NULL;      gpgme_data_t ciph = NULL;
# Line 55  gpg_clip_encrypt (gpgme_key_t rset[], in Line 53  gpg_clip_encrypt (gpgme_key_t rset[], in
53      err = gpgme_new (&ctx);      err = gpgme_new (&ctx);
54      if (err)      if (err)
55          return err;          return err;
56        
57        /* Recently a lot of users got in trouble because they enabled
58           the textmode even for non-text (binary) data. This leads to
59           serious trouble. But because our clipboard functions only support
60           the text format, the returned data is pure text or at least a
61           conversion of it, so we can safely use the textmode here. */
62        gpgme_set_textmode (ctx, 1);
63      gpgme_set_armor (ctx, 1);      gpgme_set_armor (ctx, 1);
64            
65      err = gpg_data_new_from_clipboard (&plain, 0);      err = gpg_data_utf8_new_from_clipboard (&plain, 0, NULL);
66      if (err)      if (err)
67          goto leave;          goto leave;
68      err = gpgme_data_new (&ciph);      err = gpgme_data_new (&ciph);
69      if (err)      if (err)
70          goto leave;          goto leave;
71      err = gpgme_op_encrypt (ctx, rset,      flags = always_trust? GPGME_ENCRYPT_ALWAYS_TRUST :
72                              always_trust?GPGME_ENCRYPT_ALWAYS_TRUST : (gpgme_encrypt_flags_t)0,        (gpgme_encrypt_flags_t)0;
73                              plain, ciph);      err = gpgme_op_encrypt (ctx, rset, flags, plain, ciph);
74      *r_ctx = ctx;      *r_ctx = ctx;
75      if (err)      if (err)
76          goto leave;          goto leave;
77            
78      gpg_data_release_and_set_clipboard (ciph, 1);      gpg_data_release_to_clipboard (ciph, 1);
79      ciph = NULL;      ciph = NULL;
80    
81  leave:  leave:
# Line 92  show_invalid_recipients (HWND dlg, gpgme Line 96  show_invalid_recipients (HWND dlg, gpgme
96      gpgme_encrypt_result_t res;      gpgme_encrypt_result_t res;
97      gpgme_invalid_key_t k;      gpgme_invalid_key_t k;
98      gpgme_key_t key;      gpgme_key_t key;
99      size_t len=0;      StringBuffer p;
100      char *p;      const char *keyid;
101        const char *warn = _("key not found");
102        char *uid;
103    
104      if (!ctx)      if (!ctx)
105          return -1;          return -1;
# Line 101  show_invalid_recipients (HWND dlg, gpgme Line 107  show_invalid_recipients (HWND dlg, gpgme
107      if (!res || !res->invalid_recipients)      if (!res || !res->invalid_recipients)
108          return -1;          return -1;
109    
110      for (k=res->invalid_recipients; k; k = k->next) {      p = _("Recipients unusable for encryption:\n");
         get_pubkey (k->fpr, &key);  
         len += (32 + 16 + strlen (key->uids->name) + 2) + 4;  
     }  
   
     p = (char *)calloc (1, len+64);  
     if (!p)  
         BUG (NULL);  
     sprintf (p, _("Recipients unsuable for encryption:\n"));  
111      for (k = res->invalid_recipients; k; k = k->next) {      for (k = res->invalid_recipients; k; k = k->next) {
112          get_pubkey (k->fpr, &key);          if (!get_pubkey (k->fpr, &key)) {
113          strcat (p, key->subkeys->keyid+8);              uid = utf8_to_native (key->uids->name);
114          strcat (p, " : ");              keyid = key->subkeys->keyid+8;
115          strcat (p, key->uids->name);          }
116          strcat (p, "\n");          else {
117                uid = strdup (warn);
118                keyid = k->fpr;
119            }
120            p = p + keyid + " : " + uid + "\n";
121            safe_free (uid);
122      }      }
123      msg_box (dlg, p, _("Encryption"), MB_ERR);      msg_box (dlg, p.getBuffer (), _("Encryption"), MB_ERR);
     free (p);  
124      return 0;      return 0;
125  }  }
126    
127    
128    struct dlg_ctx_s {
129        int keys_sortby;
130        int enable_redraw;
131        keylist_ctrl_t kl;
132    };
133    // FIXME: associate the context with the window
134    static struct dlg_ctx_s state = {0, 0, NULL};
135    
136  /* Dialog procedure for the clipboard encryption. */  /* Dialog procedure for the clipboard encryption. */
137  BOOL CALLBACK  BOOL CALLBACK
138  clip_encrypt_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)  clip_encrypt_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
139  {  {    
     static listview_ctrl_t lv = NULL;  
140      gpg_keycache_t kc;      gpg_keycache_t kc;
141      gpgme_key_t *rset;      gpgme_key_t *rset;
142      gpgme_ctx_t ctx=NULL;      gpgme_ctx_t ctx=NULL;
143      gpgme_error_t err;      gpgme_error_t err;
144      refresh_cache_s rcs = {0};      char tmpbuf[64];
145      int force_trust = 0, opt = 0;      size_t n;    
     int kmode = reg_prefs.keylist_mode? KEYLIST_ENCRYPT_MIN : KEYLIST_ENCRYPT;  
     int n;  
146            
147      switch( msg ) {      switch (msg) {
148      case WM_INITDIALOG:      case WM_INITDIALOG:
149          SetWindowText( dlg, _("Encryption") );          SetWindowText (dlg, _("Encryption"));
150          kc = keycache_get_ctx( KEYCACHE_PUB );          SetDlgItemText (dlg, IDC_ENCRYPT_FINDKEY, _("&Find Key:"));
151          if( !kc )          SetDlgItemText (dlg, IDCANCEL, _("&Cancel"));
152              BUG( NULL );          SetDlgItemText (dlg, IDC_ENCRYPT_FNDCMD, _("&Find"));
153          lv = keylist_load (GetDlgItem( dlg, IDC_ENCRYPT_KEYLIST ), kc, NULL,          kc = keycache_get_ctx (KEYCACHE_PUB);
154                             kmode, KEY_SORT_USERID);          state.kl = keylist_load (GetDlgItem (dlg, IDC_ENCRYPT_KEYLIST), kc, NULL,
155          center_window( dlg, NULL );                                   KEYLIST_ENCRYPT_MIN, KEY_SORT_USERID);
156          SetForegroundWindow( dlg );  
157          set_active_window( dlg );          center_window (dlg, NULL);
158            SetForegroundWindow (dlg);
159    
160            state.enable_redraw = 1;
161            InvalidateRect(GetDlgItem (dlg, IDC_ENCRYPT_KEYLIST), NULL, TRUE);
162          return TRUE;          return TRUE;
163                    
164      case WM_DESTROY:      case WM_DESTROY:
165          reset_active_window( );          if (state.kl) {
166          if( lv ) {              keylist_delete (state.kl);
167              keylist_delete( lv );              state.kl = NULL;
             lv = NULL;  
168          }          }
169          return FALSE;          break;
170                    
171      case WM_NOTIFY:      case WM_NOTIFY:
172          NMHDR *notify;          
173                    /* Wait until the dialog initialization is done */
174            if (state.kl == NULL)
175                break;
176            
177            NMHDR *notify;
178          notify = (NMHDR *)lparam;          notify = (NMHDR *)lparam;
179            if (!notify)
180                break;
181    
182            if (state.enable_redraw) {
183                int ret;
184                ret = keylist_listview_notify (dlg, state.kl->keys,
185                                                IDC_ENCRYPT_KEYLIST, lparam);
186                if (ret != 0) {
187                    SetWindowLong (dlg, DWL_MSGRESULT, ret);
188                    return TRUE;
189                }
190            }
191            
192          if (notify && notify->code == NM_DBLCLK &&          if (notify && notify->code == NM_DBLCLK &&
193              notify->idFrom == IDC_ENCRYPT_KEYLIST)              notify->idFrom == IDC_ENCRYPT_KEYLIST)
194              PostMessage( dlg, WM_COMMAND, MAKEWPARAM( IDOK, 0 ), NULL );              PostMessage( dlg, WM_COMMAND, MAKEWPARAM (IDOK, 0), 0);
195          if (notify && notify->code == LVN_COLUMNCLICK &&          if (notify && notify->code == LVN_COLUMNCLICK &&
196              notify->idFrom == IDC_ENCRYPT_KEYLIST ) {              notify->idFrom == IDC_ENCRYPT_KEYLIST) {
197              NMLISTVIEW *p = (LPNMLISTVIEW) lparam;              NMLISTVIEW *p = (LPNMLISTVIEW) lparam;
198              int sortby = 0;              int sortby = 0;
199    
200              switch( p->iSubItem ) {              switch (p->iSubItem) {
201              case  0: sortby = KEY_SORT_USERID; break;              case  0: sortby = KEY_SORT_USERID; break;
202              case  1: sortby = KEY_SORT_KEYID; break;              case  1: sortby = KEY_SORT_KEYID; break;
203              case  2: sortby = KEY_SORT_LEN; break;              case  2: sortby = KEY_SORT_LEN; break;
204              case  4: sortby = KEY_SORT_VALIDITY; break;              case  4: sortby = KEY_SORT_VALIDITY; break;
205              default: sortby = KEY_SORT_USERID; break;              default: sortby = KEY_SORT_USERID; break;
206              }              }
207              keylist_sort( lv, sortby );              if ((state.keys_sortby & ~KEYLIST_SORT_DESC) == sortby)
208                    state.keys_sortby ^= KEYLIST_SORT_DESC;
209                else
210                    state.keys_sortby = sortby;
211                keylist_sort (state.kl, state.keys_sortby);
212                return TRUE;
213          }          }
214          return TRUE;          break;
           
     case WM_SYSCOMMAND:  
         if( LOWORD( wparam ) == SC_CLOSE )  
             EndDialog( dlg, TRUE );  
         return FALSE;  
215                    
216      case WM_COMMAND:      case WM_COMMAND:
217          switch( LOWORD( wparam ) ) {          state.enable_redraw = 0;
218    
219            switch (LOWORD (wparam)) {
220          case IDOK:          case IDOK:
221              rset = keylist_get_recipients (lv, &force_trust, &n);              int force_trust;
222                rset = keylist_get_recipients (state.kl, &force_trust, &n);
223              if (!n) {              if (!n) {
224                  msg_box (dlg, _("You must select at least one key."), _("Encryption"), MB_ERR);                  msg_box (dlg, _("You must select at least one key."),
225                  free (rset);                           _("Encryption"), MB_ERR);
226                    safe_free (rset);
227                  return FALSE;                  return FALSE;
228              }              }
229              err = gpg_clip_encrypt (rset, force_trust, &ctx);              err = gpg_clip_encrypt (rset, force_trust, &ctx);
230              if (err) {              if (err) {
231                  if (show_invalid_recipients (dlg, ctx))                  if (show_invalid_recipients (dlg, ctx))
232                      msg_box (dlg, gpgme_strerror (err), _("Encryption"), MB_ERR);                      msg_box (dlg, gpgme_strerror (err), _("Encryption"), MB_ERR);
                 if (ctx)  
                     gpgme_release (ctx);  
                 free (rset);  
                 return FALSE;  
233              }              }
234              else              else
235                  show_msg( dlg, 1500, _("GnuPG Status: Finished") );                  show_msg (dlg, 1500, _("GnuPG Status: Finished"));
236              free (rset);              safe_free (rset);
237              gpgme_release (ctx);              if (ctx)
238              EndDialog (dlg, TRUE);                  gpgme_release (ctx);
239              return TRUE;              if (!err)
240                    EndDialog (dlg, TRUE);
241                break;
242                            
243          case IDCANCEL:          case IDCANCEL:
244              EndDialog( dlg, FALSE );              EndDialog (dlg, FALSE);
245              return FALSE;              break;
246    
247          case IDC_ENCRYPT_FNDCMD:          case IDC_ENCRYPT_FNDCMD:
         {  
             char tmpbuf[64];  
             int n;  
   
248              n = GetDlgItemText (dlg, IDC_ENCRYPT_FIND, tmpbuf, DIM (tmpbuf)-1);              n = GetDlgItemText (dlg, IDC_ENCRYPT_FIND, tmpbuf, DIM (tmpbuf)-1);
249              if (!n)              if (!n)
250                  break;                  break;
251              n = listview_find (lv, tmpbuf);              n = listview_find (state.kl->lv, tmpbuf, 0);
252              if (n != -1) {                            if (n != -1) {
253                  int oldpos = listview_get_curr_pos (lv);                  int oldpos = listview_get_curr_pos (state.kl->lv);
254                  listview_select_one (lv, n);                  listview_select_one (state.kl->lv, n);
255                  listview_scroll (lv, oldpos, n);                  listview_scroll (state.kl->lv, oldpos, n);
256              }              }
257              else              else
258                  log_box (_("Encryption"), MB_ERR, _("No recipient found with '%s'"), tmpbuf);                  log_box (_("Encryption"), MB_ERR,
259                             _("No recipient found with '%s'"), tmpbuf);
260              break;              break;
         }  
261          }          }
262          break;          break;
263      }      }

Legend:
Removed from v.48  
changed lines
  Added in v.451

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26