/[winpt]/trunk/w32gpgme/w32-clip.c
ViewVC logotype

Diff of /trunk/w32gpgme/w32-clip.c

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

revision 34 by twoaday, Wed Oct 26 11:20:09 2005 UTC revision 36 by werner, Thu Oct 27 15:25:13 2005 UTC
# Line 1  Line 1 
1  /* w32-clip.c - W32 API clipboard functions  /* w32-clip.c - W32 API clipboard functions
2   *      Copyright (C) 2001, 2002, 2003, 2005 Timo Schulz   *      Copyright (C) 2001, 2002, 2003, 2005 Timo Schulz
3   *   *
4   * This file is part of MyGPGME.   * This file is part of MyGPGME.
5   *   *
6   * MyGPGME is free software; you can redistribute it and/or modify   * MyGPGME is free software; you can redistribute it and/or modify
7   * it under the terms of the GNU General Public License as published by   * it under the terms of the GNU General Public License as published by
8   * the Free Software Foundation; either version 2 of the License, or   * the Free Software Foundation; either version 2 of the License, or
9   * (at your option) any later version.   * (at your option) any later version.
10   *   *
11   * MyGPGME is distributed in the hope that it will be useful,   * MyGPGME is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.   * GNU General Public License for more details.
15   *   *
16   * You should have received a copy of the GNU General Public License   * You should have received a copy of the GNU General Public License
17   * along with this program; if not, write to the Free Software   * along with this program; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19   */   */
20    
21  #include <windows.h>  #include <windows.h>
22  #include "w32gpgme.h"  #include "w32gpgme.h"
23    
24  int gpg_data_wrap_lines (gpgme_data_t *r_dh, size_t wraplen);  int gpg_data_wrap_lines (gpgme_data_t *r_dh, size_t wraplen);
25    
26  //FIXME #include "wptVersion.h"  //FIXME #include "wptVersion.h"
27  #define PGM_VERSION "0.10.3-beta"  #define PGM_VERSION "0.10.3-beta"
28    
29  /* Retrieve the thext of the clipboard.  /* Retrieve the thext of the clipboard.
30     Return value: the text as a string, NULL otherwise. */     Return value: the text as a string, NULL otherwise. */
31  static char*  static char*
32  get_w32_clip_text (void)  get_w32_clip_text (void)
33  {  {
34      char *private_clip = NULL;      char *private_clip = NULL;
35      char *clip = NULL;      char *clip = NULL;
36      size_t size;      size_t size;
37      HANDLE cb;      HANDLE cb;
38            
39      if (!OpenClipboard (NULL))      if (!OpenClipboard (NULL))
40          return NULL;          return NULL;
41      cb = GetClipboardData (CF_TEXT);      cb = GetClipboardData (CF_TEXT);
42      if (!cb)      if (!cb)
43          goto leave;          goto leave;
44      private_clip = GlobalLock (cb);      private_clip = GlobalLock (cb);
45      if (!private_clip)      if (!private_clip)
46          goto leave;          goto leave;
47      size = strlen (private_clip);      size = strlen (private_clip);
48      clip = malloc (size + 1);      clip = malloc (size + 1);
49      if (!clip) {      if (!clip) {
50          GlobalUnlock (cb);          GlobalUnlock (cb);
51          goto leave;          goto leave;
52      }      }
53      memcpy (clip, private_clip, size);      memcpy (clip, private_clip, size);
54      clip[size] = '\0';      clip[size] = '\0';
55      GlobalUnlock(cb);      GlobalUnlock(cb);
56            
57  leave:  leave:
58      CloseClipboard ();      CloseClipboard ();
59      return clip;      return clip;
60  }  }
61    
62    
63  /* Set the new clipboard data to @data. */  /* Set the new clipboard data to @data. */
64  static void  static void
65  set_w32_clip_text (const char *data, int size)  set_w32_clip_text (const char *data, int size)
66  {  {
67      HANDLE cb;      HANDLE cb;
68      char *private_data;      char *private_data;
69            
70      if (!OpenClipboard(NULL))      if (!OpenClipboard(NULL))
71          return;          return;
72      EmptyClipboard ();      EmptyClipboard ();
73      cb = GlobalAlloc (GHND, size+1);      cb = GlobalAlloc (GHND, size+1);
74      if (!cb)      if (!cb)
75          goto leave;          goto leave;
76      private_data = GlobalLock (cb);      private_data = GlobalLock (cb);
77      if (!private_data)      if (!private_data)
78          goto leave;          goto leave;
79      memcpy (private_data, data, size);      memcpy (private_data, data, size);
80      private_data[size] = '\0';      private_data[size] = '\0';
81      SetClipboardData (CF_TEXT, cb);      SetClipboardData (CF_TEXT, cb);
82      GlobalUnlock (cb);      GlobalUnlock (cb);
83            
84  leave:      leave:    
85      CloseClipboard ();      CloseClipboard ();
86      GlobalFree (cb);      GlobalFree (cb);
87  }  }
88    
89    
90  /* Retrieve the clipboard data and create a new gpgme  /* Retrieve the clipboard data and create a new gpgme
91     data object @r_dh and return it. If @wraplen is > 0     data object @r_dh and return it. If @wraplen is > 0
92     wrap lines of the buffer.     wrap lines of the buffer.
93     Return value: 0 on success. */     Return value: 0 on success. */
94  gpgme_error_t  gpgme_error_t
95  gpg_data_new_from_clipboard (gpgme_data_t *r_dh, int wraplen)  gpg_data_new_from_clipboard (gpgme_data_t *r_dh, int wraplen)
96  {  {
97      gpgme_error_t err = 0;      gpgme_error_t err = 0;
98      gpgme_data_t dh;      gpgme_data_t dh;
99      char *clip_text = NULL;      char *clip_text = NULL;
100    
101      if (!r_dh)      if (!r_dh)
102          return gpg_error (GPG_ERR_INV_ARG);          return gpg_error (GPG_ERR_INV_ARG);
103      *r_dh = NULL;      *r_dh = NULL;
104      clip_text = get_w32_clip_text ();      clip_text = get_w32_clip_text ();
105      if (!clip_text)      if (!clip_text)
106          return gpg_error (GPG_ERR_NO_DATA);          return gpg_error (GPG_ERR_NO_DATA);
107      err = gpgme_data_new_from_mem (&dh, clip_text, strlen (clip_text), 1);      err = gpgme_data_new_from_mem (&dh, clip_text, strlen (clip_text), 1);
108      if (clip_text)      if (clip_text)
109          free (clip_text);          free (clip_text);
110      if (wraplen > 0)      if (wraplen > 0)
111          gpg_data_wrap_lines (&dh, wraplen);          gpg_data_wrap_lines (&dh, wraplen);
112            
113      *r_dh = dh;      *r_dh = dh;
114      return err;      return err;
115  }  }
116    
117    
118  /* Search for the armor header 'Version:' in @r_dh and  /* Search for the armor header 'Version:' in @r_dh and
119     add the WinPT version. On success @r_dh is replaced     add the WinPT version. On success @r_dh is replaced
120     with the modified data.     with the modified data.
121     Return value: 0 on success. */     Return value: 0 on success. */
122  static gpgme_error_t  static gpgme_error_t
123  gpg_data_change_version (gpgme_data_t *r_dh)  gpg_data_change_version (gpgme_data_t *r_dh)
124  {  {
125      gpgme_error_t err = 0;      gpgme_error_t err = 0;
126      gpgme_data_t mdh;      gpgme_data_t mdh;
127      char line[128+32];      char line[128+32];
128      int n;      int n;
129    
130      if (!r_dh)      if (!r_dh)
131          return gpg_error (GPG_ERR_INV_ARG);          return gpg_error (GPG_ERR_INV_ARG);
132            
133      err = gpgme_data_new (&mdh);      err = gpgme_data_new (&mdh);
134      if (err)      if (err)
135          return err;          return err;
136    
137      gpgme_data_rewind (*r_dh);      gpgme_data_rewind (*r_dh);
138      while ((n=gpg_data_readline (*r_dh, line, 128)) > 0) {      while ((n=gpg_data_readline (*r_dh, line, 128)) > 0) {
139          line[n] = '\0';          line[n] = '\0';
140          if (strlen (line) > 14 &&          if (strlen (line) > 14 &&
141              !strncmp (line, "Version: GnuPG", 14)  &&              !strncmp (line, "Version: GnuPG", 14)  &&
142              !strstr (line, "WinPT "PGM_VERSION)) {              !strstr (line, "WinPT "PGM_VERSION)) {
143              line[strlen (line) - 2] = '\0';              line[strlen (line) - 2] = '\0';
144              strcat (line, " - " );              strcat (line, " - " );
145              strcat (line, "WinPT "PGM_VERSION);              strcat (line, "WinPT "PGM_VERSION);
146              strcat (line, "\r\n");              strcat (line, "\r\n");
147          }          }
148                    
149          line[strlen (line)-2] = 0; /* XXX: make sure we really have \r\n */          line[strlen (line)-2] = 0; /* XXX: make sure we really have \r\n */
150          gpgme_data_write (mdh, line, strlen (line));          gpgme_data_write (mdh, line, strlen (line));
151          gpgme_data_write (mdh, "\r\n", 2);          gpgme_data_write (mdh, "\r\n", 2);
152      }      }
153    
154      gpgme_data_write (mdh, "", 1);      gpgme_data_write (mdh, "", 1);
155      gpgme_data_rewind (mdh);      gpgme_data_rewind (mdh);
156      gpgme_data_release (*r_dh);      gpgme_data_release (*r_dh);
157      *r_dh = mdh;      *r_dh = mdh;
158      return err;      return err;
159  }  }
160    
161  /* Release a gpgme data object @dh and write the contents of  /* Release a gpgme data object @dh and write the contents of
162     the object to the clipboard. If @chg_ver is set, modify the     the object to the clipboard. If @chg_ver is set, modify the
163     Version: header and add WinPT information. */     Version: header and add WinPT information. */
164  void  void
165  gpg_data_release_and_set_clipboard (gpgme_data_t dh, int chg_ver)  gpg_data_release_and_set_clipboard (gpgme_data_t dh, int chg_ver)
166  {  {
167      gpgme_data_t in;      gpgme_data_t in;
168      char *clip_text;      char *clip_text;
169      size_t n;      size_t n;
170            
171      if (!dh)      if (!dh)
172          return;          return;
173    
174      in = dh;      in = dh;
175      if (chg_ver)      if (chg_ver)
176          gpg_data_change_version (&in);          gpg_data_change_version (&in);
177    
178      clip_text = gpgme_data_release_and_get_mem (in, &n);      clip_text = gpgme_data_release_and_get_mem (in, &n);
179      if (clip_text && *clip_text) {      if (clip_text && *clip_text) {
180          set_w32_clip_text (clip_text, n);          set_w32_clip_text (clip_text, n);
181          memset (clip_text, 0xFF, n);          memset (clip_text, 0xFF, n);
182          gpgme_free (clip_text);          gpgme_free (clip_text);
183      }      }
184  }  }

Legend:
Removed from v.34  
changed lines
  Added in v.36

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26