1 |
/* 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 |
/* Search for the armor header 'Version:' in @r_dh and |
119 |
add the WinPT version. On success @r_dh is replaced |
120 |
with the modified data. |
121 |
Return value: 0 on success. */ |
122 |
static gpgme_error_t |
123 |
gpg_data_change_version (gpgme_data_t *r_dh) |
124 |
{ |
125 |
gpgme_error_t err = 0; |
126 |
gpgme_data_t mdh; |
127 |
char line[128+32]; |
128 |
int n; |
129 |
|
130 |
if (!r_dh) |
131 |
return gpg_error (GPG_ERR_INV_ARG); |
132 |
|
133 |
err = gpgme_data_new (&mdh); |
134 |
if (err) |
135 |
return err; |
136 |
|
137 |
gpgme_data_rewind (*r_dh); |
138 |
while ((n=gpg_data_readline (*r_dh, line, 128)) > 0) { |
139 |
line[n] = '\0'; |
140 |
if (strlen (line) > 14 && |
141 |
!strncmp (line, "Version: GnuPG", 14) && |
142 |
!strstr (line, "WinPT "PGM_VERSION)) { |
143 |
line[strlen (line) - 2] = '\0'; |
144 |
strcat (line, " - " ); |
145 |
strcat (line, "WinPT "PGM_VERSION); |
146 |
strcat (line, "\r\n"); |
147 |
} |
148 |
gpgme_data_write (mdh, line, strlen (line)); |
149 |
} |
150 |
|
151 |
gpgme_data_write (mdh, "", 1); |
152 |
gpgme_data_rewind (mdh); |
153 |
gpgme_data_release (*r_dh); |
154 |
*r_dh = mdh; |
155 |
return err; |
156 |
} |
157 |
|
158 |
/* Release a gpgme data object @dh and write the contents of |
159 |
the object to the clipboard. If @chg_ver is set, modify the |
160 |
Version: header and add WinPT information. */ |
161 |
void |
162 |
gpg_data_release_and_set_clipboard (gpgme_data_t dh, int chg_ver) |
163 |
{ |
164 |
gpgme_data_t in; |
165 |
char *clip_text; |
166 |
int size = 0; |
167 |
size_t n; |
168 |
|
169 |
if (!dh) |
170 |
return; |
171 |
|
172 |
in = dh; |
173 |
if (chg_ver) |
174 |
gpg_data_change_version (&in); |
175 |
|
176 |
clip_text = gpgme_data_release_and_get_mem (in, &n); |
177 |
if (clip_text && *clip_text) { |
178 |
clip_text[n] = 0; |
179 |
size = strlen (clip_text); |
180 |
set_w32_clip_text (clip_text, size); |
181 |
memset (clip_text, 0xFF, size); |
182 |
if (clip_text) |
183 |
free (clip_text); |
184 |
} |
185 |
} |