1 |
/* wptFileCBS.cpp |
/* wptFileCBS.cpp |
2 |
* Copyright (C) 2001-2005 Timo Schulz |
* Copyright (C) 2005 Timo Schulz |
3 |
* Copyright (C) 2005 g10 Code GmbH |
* Copyright (C) 2005 g10 Code GmbH |
4 |
* |
* |
5 |
* This file is part of WinPT. |
* This file is part of WinPT. |
23 |
#include <malloc.h> |
#include <malloc.h> |
24 |
#include <errno.h> |
#include <errno.h> |
25 |
#include <stdlib.h> |
#include <stdlib.h> |
26 |
|
#include <sys/stat.h> |
27 |
|
|
28 |
|
#include "gpgme.h" |
29 |
#include "wptListView.h" |
#include "wptListView.h" |
30 |
#include "wptGPG.h" |
#include "wptGPG.h" |
|
#include "w32gpgme.h" |
|
31 |
#include "wptFileManager.h" |
#include "wptFileManager.h" |
32 |
|
#include "wptErrors.h" |
33 |
|
#include "wptTypes.h" |
34 |
|
|
35 |
|
void progress_callback (void *opaque, const char *what, int type, int off, int max); |
36 |
|
|
37 |
|
|
38 |
/* Predefined read callback. */ |
/* Predefined read callback. */ |
39 |
static long |
static long |
40 |
read_cb (void *handle, void *buffer, size_t size) |
read_cb (void *handle, void *buffer, size_t size) |
41 |
{ |
{ |
42 |
file_data_t cb = (file_data_t)handle; |
file_data_t cb = (file_data_t)handle; |
43 |
return fread (buffer, 1, size, cb->handle); |
struct progress_filter_s *pfx = (struct progress_filter_s *)cb->cb_value; |
44 |
|
int n = fread (buffer, 1, size, cb->handle); |
45 |
|
|
46 |
|
/* XXX: there is a sync problem with the progress dialog. */ |
47 |
|
if (pfx) |
48 |
|
progress_callback (pfx, NULL, 0, cb->off, cb->size); |
49 |
|
cb->off += n; |
50 |
|
return n; |
51 |
} |
} |
52 |
|
|
53 |
|
|
56 |
write_cb (void *handle, const void *buffer, size_t size) |
write_cb (void *handle, const void *buffer, size_t size) |
57 |
{ |
{ |
58 |
file_data_t cb = (file_data_t)handle; |
file_data_t cb = (file_data_t)handle; |
59 |
return fwrite (buffer, 1, size, cb->handle); |
int n = fwrite (buffer, 1, size, cb->handle); |
60 |
|
|
61 |
|
return n; |
62 |
} |
} |
63 |
|
|
64 |
|
|
85 |
cb->cbs.read = read_cb; |
cb->cbs.read = read_cb; |
86 |
cb->cbs.write = write_cb; |
cb->cbs.write = write_cb; |
87 |
cb->handle = f; |
cb->handle = f; |
88 |
|
if (for_read) { |
89 |
|
struct stat st; |
90 |
|
if (fstat (fileno (f), &st)) |
91 |
|
BUG (NULL); |
92 |
|
cb->size = st.st_size; |
93 |
|
cb->off = 0; |
94 |
|
} |
95 |
|
|
96 |
err = gpgme_data_new_from_cbs (&cb->dat, &cb->cbs, cb); |
err = gpgme_data_new_from_cbs (&cb->dat, &cb->cbs, cb); |
97 |
if (err) { |
if (err) { |
105 |
} |
} |
106 |
|
|
107 |
|
|
108 |
|
/* Activate the progress callback for the given object @ctx. */ |
109 |
|
void |
110 |
|
gpg_file_data_set_cb (file_data_t ctx, struct progress_filter_s *pfx) |
111 |
|
{ |
112 |
|
ctx->cb_value = (void*)pfx; |
113 |
|
} |
114 |
|
|
115 |
|
|
116 |
/* Release the context in @cb. Close all internal handles if possible. */ |
/* Release the context in @cb. Close all internal handles if possible. */ |
117 |
void |
void |
118 |
gpg_file_data_release (file_data_t cb) |
gpg_file_data_release (file_data_t cb) |