1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
// download_thread.cc
//
// Copyright (C) 2005 Daniel Burrows
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; see the file COPYING. If not, write to
// the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
#include "download_thread.h"
#include <generic/apt/download_manager.h>
#include <generic/apt/download_signal_log.h>
#include <cwidget/toplevel.h>
#include <sigc++/bind.h>
#include <sigc++/slot.h>
#include <sigc++/functors/mem_fun.h>
namespace cw = cwidget;
template<typename RVal>
class background_execute : public cw::toplevel::event
{
sigc::slot0<RVal> slot;
cw::threads::box<RVal> &return_box;
public:
background_execute(const sigc::slot0<RVal> &_slot,
cw::threads::box<RVal> &_return_box)
:slot(_slot), return_box(_return_box)
{
}
void dispatch()
{
return_box.put(slot());
}
};
template<>
class background_execute<void> : public cw::toplevel::event
{
sigc::slot0<void> slot;
cw::threads::box<void> &return_box;
public:
background_execute(const sigc::slot0<void> &_slot,
cw::threads::box<void> &_return_box)
:slot(_slot), return_box(_return_box)
{
}
void dispatch()
{
slot();
return_box.put();
}
};
/** Run the given method call in the foreground and return its value. */
template<typename C, typename RVal>
static
RVal do_foreground_execute(C *inst,
RVal (C::* fun) ())
{
cw::threads::box<RVal> return_box;
cw::toplevel::post_event(new background_execute<RVal>(sigc::mem_fun(inst, fun),
return_box));
return return_box.take();
}
/** Run the given method call in the foreground and return its value. */
template<typename C, typename RVal, typename Arg0>
static
RVal do_foreground_execute(C *inst,
Arg0 arg0,
RVal (C::* fun) (Arg0))
{
cw::threads::box<RVal> return_box;
cw::toplevel::post_event(new background_execute<RVal>(bind(sigc::mem_fun(inst, fun), arg0),
return_box));
return return_box.take();
}
/** Run the given method call in the foreground and return its value. */
template<typename C, typename RVal, typename Arg0, typename Arg1>
static
RVal do_foreground_execute(C *inst,
Arg0 arg0,
Arg1 arg1,
RVal (C::* fun) (Arg0, Arg1))
{
cw::threads::box<RVal> return_box;
cw::toplevel::post_event(new background_execute<RVal>(bind(sigc::mem_fun(inst, fun), arg0, arg1),
return_box));
return return_box.take();
}
/** Run the given function call in the foreground and return its value. */
template<typename C, typename RVal, typename Arg0, typename Arg1, typename Arg2>
static
RVal do_foreground_execute(C *inst,
Arg0 arg0,
Arg1 arg1,
Arg2 arg2,
RVal (C::* fun) (Arg0, Arg1, Arg2))
{
cw::threads::box<RVal> return_box;
cw::toplevel::post_event(new background_execute<RVal>(bind(sigc::mem_fun(inst, fun), arg0, arg1, arg2),
return_box));
return return_box.take();
}
void background_status::Fetched(unsigned long Size,
unsigned long ResumePoint)
{
do_foreground_execute(real_status, Size, ResumePoint,
&download_signal_log::Fetched);
}
bool background_status::MediaChange(std::string Media, std::string Drive)
{
cw::threads::box<bool> return_box;
do_foreground_execute<download_signal_log,
void, const std::string &, const std::string &,
const sigc::slot1<void, bool> &> (real_status, Media, Drive,
sigc::mem_fun(return_box,
&cw::threads::box<bool>::put),
&download_signal_log::MediaChange);
return return_box.take();
}
void background_status::IMSHit(pkgAcquire::ItemDesc &item)
{
do_foreground_execute<download_signal_log, void, pkgAcquire::ItemDesc &>(real_status, item, &download_signal_log::IMSHit);
}
void background_status::Fetch(pkgAcquire::ItemDesc &item)
{
do_foreground_execute<download_signal_log, void, pkgAcquire::ItemDesc &>(real_status, item, &download_signal_log::Fetch);
}
void background_status::Done(pkgAcquire::ItemDesc &item)
{
do_foreground_execute<download_signal_log, void, pkgAcquire::ItemDesc &>(real_status, item, &download_signal_log::Done);
}
void background_status::Fail(pkgAcquire::ItemDesc &item)
{
do_foreground_execute<download_signal_log, void, pkgAcquire::ItemDesc &>(real_status, item, &download_signal_log::Fail);
}
bool background_status::Pulse(pkgAcquire *Owner)
{
cw::threads::box<bool> return_box;
do_foreground_execute<download_signal_log, void,
pkgAcquire *,
const sigc::slot1<void, bool> &>(real_status, Owner,
sigc::mem_fun(&return_box,
&cw::threads::box<bool>::put),
&download_signal_log::Pulse);
return return_box.take();
}
void background_status::Start()
{
do_foreground_execute(real_status, &download_signal_log::Start);
}
void background_status::Stop()
{
cw::threads::box<void> return_box;
do_foreground_execute<download_signal_log,
void,
const sigc::slot0<void> &>(real_status,
sigc::mem_fun(&return_box,
&cw::threads::box<void>::put),
&download_signal_log::Stop);
return_box.take();
}
class download_thread_complete_event : public cw::toplevel::event
{
download_thread *t;
pkgAcquire::RunResult res;
sigc::slot2<void, download_thread *, pkgAcquire::RunResult> continuation;
public:
download_thread_complete_event(download_thread *_t,
pkgAcquire::RunResult _res,
sigc::slot2<void, download_thread *, pkgAcquire::RunResult> &_continuation)
:t(_t), res(_res), continuation(_continuation)
{
}
void dispatch()
{
t->join();
continuation(t, res);
}
};
void download_thread::operator()()
{
cw::toplevel::post_event(new download_thread_complete_event(this, m->do_download(),
continuation));
}
|