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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 23 - (hide annotations)
Fri Sep 30 10:10:16 2005 UTC (19 years, 5 months ago) by twoaday
File MIME type: text/plain
File size: 5520 byte(s)
Almost finished phase 1 of the WinPT GPGME port.
Still need more cleanup, comments and tests.


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26