1 |
werner |
36 |
/* 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 |
twoaday |
111 |
#ifdef HAVE_CONFIG_H |
21 |
|
|
#include <config.h> |
22 |
|
|
#endif |
23 |
werner |
36 |
|
24 |
|
|
#include <windows.h> |
25 |
|
|
#include "w32gpgme.h" |
26 |
|
|
|
27 |
twoaday |
111 |
#define PGM_VERSION PACKAGE_VERSION |
28 |
|
|
|
29 |
werner |
36 |
int gpg_data_wrap_lines (gpgme_data_t *r_dh, size_t wraplen); |
30 |
|
|
|
31 |
|
|
|
32 |
|
|
/* Retrieve the thext of the clipboard. |
33 |
|
|
Return value: the text as a string, NULL otherwise. */ |
34 |
|
|
static char* |
35 |
|
|
get_w32_clip_text (void) |
36 |
|
|
{ |
37 |
|
|
char *private_clip = NULL; |
38 |
|
|
char *clip = NULL; |
39 |
|
|
size_t size; |
40 |
|
|
HANDLE cb; |
41 |
|
|
|
42 |
|
|
if (!OpenClipboard (NULL)) |
43 |
|
|
return NULL; |
44 |
|
|
cb = GetClipboardData (CF_TEXT); |
45 |
|
|
if (!cb) |
46 |
|
|
goto leave; |
47 |
|
|
private_clip = GlobalLock (cb); |
48 |
|
|
if (!private_clip) |
49 |
|
|
goto leave; |
50 |
|
|
size = strlen (private_clip); |
51 |
|
|
clip = malloc (size + 1); |
52 |
|
|
if (!clip) { |
53 |
|
|
GlobalUnlock (cb); |
54 |
|
|
goto leave; |
55 |
|
|
} |
56 |
|
|
memcpy (clip, private_clip, size); |
57 |
|
|
clip[size] = '\0'; |
58 |
|
|
GlobalUnlock(cb); |
59 |
|
|
|
60 |
|
|
leave: |
61 |
|
|
CloseClipboard (); |
62 |
|
|
return clip; |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
|
66 |
|
|
/* Set the new clipboard data to @data. */ |
67 |
|
|
static void |
68 |
|
|
set_w32_clip_text (const char *data, int size) |
69 |
|
|
{ |
70 |
|
|
HANDLE cb; |
71 |
|
|
char *private_data; |
72 |
|
|
|
73 |
|
|
if (!OpenClipboard(NULL)) |
74 |
|
|
return; |
75 |
|
|
EmptyClipboard (); |
76 |
|
|
cb = GlobalAlloc (GHND, size+1); |
77 |
|
|
if (!cb) |
78 |
|
|
goto leave; |
79 |
|
|
private_data = GlobalLock (cb); |
80 |
|
|
if (!private_data) |
81 |
|
|
goto leave; |
82 |
|
|
memcpy (private_data, data, size); |
83 |
|
|
private_data[size] = '\0'; |
84 |
|
|
SetClipboardData (CF_TEXT, cb); |
85 |
|
|
GlobalUnlock (cb); |
86 |
|
|
|
87 |
|
|
leave: |
88 |
|
|
CloseClipboard (); |
89 |
|
|
GlobalFree (cb); |
90 |
|
|
} |
91 |
|
|
|
92 |
|
|
|
93 |
|
|
/* Retrieve the clipboard data and create a new gpgme |
94 |
|
|
data object @r_dh and return it. If @wraplen is > 0 |
95 |
|
|
wrap lines of the buffer. |
96 |
|
|
Return value: 0 on success. */ |
97 |
|
|
gpgme_error_t |
98 |
|
|
gpg_data_new_from_clipboard (gpgme_data_t *r_dh, int wraplen) |
99 |
|
|
{ |
100 |
|
|
gpgme_error_t err = 0; |
101 |
|
|
gpgme_data_t dh; |
102 |
|
|
char *clip_text = NULL; |
103 |
|
|
|
104 |
|
|
if (!r_dh) |
105 |
|
|
return gpg_error (GPG_ERR_INV_ARG); |
106 |
|
|
*r_dh = NULL; |
107 |
|
|
clip_text = get_w32_clip_text (); |
108 |
|
|
if (!clip_text) |
109 |
|
|
return gpg_error (GPG_ERR_NO_DATA); |
110 |
|
|
err = gpgme_data_new_from_mem (&dh, clip_text, strlen (clip_text), 1); |
111 |
|
|
if (clip_text) |
112 |
|
|
free (clip_text); |
113 |
|
|
if (wraplen > 0) |
114 |
|
|
gpg_data_wrap_lines (&dh, wraplen); |
115 |
|
|
|
116 |
|
|
*r_dh = dh; |
117 |
|
|
return err; |
118 |
|
|
} |
119 |
|
|
|
120 |
|
|
|
121 |
|
|
/* Search for the armor header 'Version:' in @r_dh and |
122 |
|
|
add the WinPT version. On success @r_dh is replaced |
123 |
|
|
with the modified data. |
124 |
|
|
Return value: 0 on success. */ |
125 |
|
|
static gpgme_error_t |
126 |
|
|
gpg_data_change_version (gpgme_data_t *r_dh) |
127 |
|
|
{ |
128 |
|
|
gpgme_error_t err = 0; |
129 |
|
|
gpgme_data_t mdh; |
130 |
|
|
char line[128+32]; |
131 |
|
|
int n; |
132 |
|
|
|
133 |
|
|
if (!r_dh) |
134 |
|
|
return gpg_error (GPG_ERR_INV_ARG); |
135 |
|
|
|
136 |
|
|
err = gpgme_data_new (&mdh); |
137 |
|
|
if (err) |
138 |
|
|
return err; |
139 |
|
|
|
140 |
|
|
gpgme_data_rewind (*r_dh); |
141 |
|
|
while ((n=gpg_data_readline (*r_dh, line, 128)) > 0) { |
142 |
|
|
if (strlen (line) > 14 && |
143 |
|
|
!strncmp (line, "Version: GnuPG", 14) && |
144 |
|
|
!strstr (line, "WinPT "PGM_VERSION)) { |
145 |
|
|
line[strlen (line) - 2] = '\0'; |
146 |
|
|
strcat (line, " - " ); |
147 |
|
|
strcat (line, "WinPT "PGM_VERSION); |
148 |
|
|
strcat (line, "\r\n"); |
149 |
|
|
} |
150 |
|
|
gpgme_data_write (mdh, line, strlen (line)); |
151 |
|
|
} |
152 |
|
|
|
153 |
|
|
gpgme_data_write (mdh, "", 1); |
154 |
|
|
gpgme_data_rewind (mdh); |
155 |
|
|
gpgme_data_release (*r_dh); |
156 |
|
|
*r_dh = mdh; |
157 |
|
|
return err; |
158 |
|
|
} |
159 |
|
|
|
160 |
|
|
/* Release a gpgme data object @dh and write the contents of |
161 |
|
|
the object to the clipboard. If @chg_ver is set, modify the |
162 |
|
|
Version: header and add WinPT information. */ |
163 |
|
|
void |
164 |
|
|
gpg_data_release_and_set_clipboard (gpgme_data_t dh, int chg_ver) |
165 |
|
|
{ |
166 |
|
|
gpgme_data_t in; |
167 |
|
|
char *clip_text; |
168 |
|
|
size_t n; |
169 |
|
|
|
170 |
|
|
if (!dh) |
171 |
|
|
return; |
172 |
|
|
|
173 |
|
|
in = dh; |
174 |
|
|
if (chg_ver) |
175 |
|
|
gpg_data_change_version (&in); |
176 |
|
|
|
177 |
|
|
clip_text = gpgme_data_release_and_get_mem (in, &n); |
178 |
|
|
if (clip_text && *clip_text) { |
179 |
|
|
set_w32_clip_text (clip_text, n); |
180 |
|
|
memset (clip_text, 0xFF, n); |
181 |
|
|
gpgme_free (clip_text); |
182 |
|
|
} |
183 |
|
|
} |