1 |
/* wptFileCBS.cpp |
2 |
* Copyright (C) 2005 Timo Schulz |
3 |
* Copyright (C) 2005 g10 Code GmbH |
4 |
* |
5 |
* This file is part of WinPT. |
6 |
* |
7 |
* WinPT is free software; you can redistribute it and/or |
8 |
* modify it under the terms of the GNU General Public License |
9 |
* as published by the Free Software Foundation; either version 2 |
10 |
* of the License, or (at your option) any later version. |
11 |
* |
12 |
* WinPT is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 |
* General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with WinPT; if not, write to the Free Software Foundation, |
19 |
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
*/ |
21 |
#ifdef HAVE_CONFIG_H |
22 |
#include <config.h> |
23 |
#endif |
24 |
|
25 |
#include <windows.h> |
26 |
#include <stdio.h> |
27 |
#include <malloc.h> |
28 |
#include <errno.h> |
29 |
#include <stdlib.h> |
30 |
#include <sys/stat.h> |
31 |
|
32 |
#include "gpgme.h" |
33 |
#include "wptListView.h" |
34 |
#include "wptGPG.h" |
35 |
#include "wptFileManager.h" |
36 |
#include "wptErrors.h" |
37 |
#include "wptTypes.h" |
38 |
|
39 |
void progress_callback (void *opaque, const char *what, int type, int off, int max); |
40 |
|
41 |
|
42 |
/* Predefined read callback. */ |
43 |
static long |
44 |
read_cb (void *handle, void *buffer, size_t size) |
45 |
{ |
46 |
file_data_t cb = (file_data_t)handle; |
47 |
struct progress_filter_s *pfx = (struct progress_filter_s *)cb->cb_value; |
48 |
int n = fread (buffer, 1, size, cb->handle); |
49 |
|
50 |
/* XXX: there is a sync problem with the progress dialog. */ |
51 |
if (pfx) |
52 |
progress_callback (pfx, NULL, 0, cb->off, cb->size); |
53 |
cb->off += n; |
54 |
return n; |
55 |
} |
56 |
|
57 |
|
58 |
/* Predefined write callback. */ |
59 |
static long |
60 |
write_cb (void *handle, const void *buffer, size_t size) |
61 |
{ |
62 |
file_data_t cb = (file_data_t)handle; |
63 |
int n = fwrite (buffer, 1, size, cb->handle); |
64 |
|
65 |
return n; |
66 |
} |
67 |
|
68 |
|
69 |
/* Create a new data -> file association with a static callback. |
70 |
@fname is the file which is associated to the object. |
71 |
@r_cb is the context which holds all information. |
72 |
@for_read is 1 if the file is opened for read only. |
73 |
Return value: 0 on success. */ |
74 |
gpgme_error_t |
75 |
gpg_file_data_new (const char *fname, int for_read, file_data_t *r_cb) |
76 |
|
77 |
{ |
78 |
gpgme_error_t err; |
79 |
file_data_t cb; |
80 |
FILE *f; |
81 |
|
82 |
f = fopen (fname, for_read?"rb" : "wb"); |
83 |
if (!f) |
84 |
return gpgme_err_code_from_errno (errno); |
85 |
|
86 |
cb = (file_data_t)calloc (1, sizeof *cb); |
87 |
if (!cb) |
88 |
abort (); |
89 |
cb->cbs.read = read_cb; |
90 |
cb->cbs.write = write_cb; |
91 |
cb->handle = f; |
92 |
if (for_read) { |
93 |
struct stat st; |
94 |
if (fstat (fileno (f), &st)) |
95 |
BUG (NULL); |
96 |
cb->size = st.st_size; |
97 |
cb->off = 0; |
98 |
} |
99 |
|
100 |
err = gpgme_data_new_from_cbs (&cb->dat, &cb->cbs, cb); |
101 |
if (err) { |
102 |
fclose (f); |
103 |
free (cb); |
104 |
return err; |
105 |
} |
106 |
|
107 |
*r_cb = cb; |
108 |
return err; |
109 |
} |
110 |
|
111 |
|
112 |
/* Activate the progress callback for the given object @ctx. */ |
113 |
void |
114 |
gpg_file_data_set_cb (file_data_t ctx, struct progress_filter_s *pfx) |
115 |
{ |
116 |
ctx->cb_value = (void*)pfx; |
117 |
} |
118 |
|
119 |
|
120 |
/* Release the context in @cb. Close all internal handles if possible. */ |
121 |
void |
122 |
gpg_file_data_release (file_data_t cb) |
123 |
{ |
124 |
if (!cb) |
125 |
return; |
126 |
if (cb->handle) { |
127 |
FILE *f = (FILE *)cb->handle; |
128 |
fclose (f); |
129 |
} |
130 |
if (cb->dat) |
131 |
gpgme_data_release (cb->dat); |
132 |
free (cb); |
133 |
} |