summaryrefslogtreecommitdiff
path: root/src/download_thread.h
blob: 6fc431ea35f6e61f89ef52988680162253b70128 (plain)
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
// download_thread                              -*-c++-*-
//
//   Copyright (C) 2005, 2008-2009 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.

#ifndef DOWNLOAD_THREAD_H
#define DOWNLOAD_THREAD_H

#include <aptitude.h>

#include <apt-pkg/acquire.h>
#include <apt-pkg/error.h>

#include <cwidget/generic/threads/threads.h>

#include <generic/util/safe_slot.h>

#include <sigc++/slot.h>

#include <boost/shared_ptr.hpp>

/** \file download_thread.h
 */

class download_manager;
class download_signal_log;

/** \brief The type of a callback that posts a thunk to the
 *  main thread.
 *
 *  We use a function pointer and not a slot to avoid any unexpected
 *  problems with slots and threads.
 */
typedef void (*post_thunk_func)(const safe_slot0<void> &thunk);

/** A proxy status object that posts messages to the main thread.
 *  Each message "blocks" the download until the user deals with it,
 *  just as if it was running in the main thread (I'm not sure
 *  anything else is safe).
 *
 *  This object does NOT own the encapsulated status object and will
 *  NOT delete it.
 */
class background_status : public pkgAcquireStatus
{
  download_signal_log *real_status;
  post_thunk_func post_thunk;

public:
  void Fetched(unsigned long long Size, unsigned long long ResumePoint);
  bool MediaChange(std::string Media, std::string Drive);
  void IMSHit(pkgAcquire::ItemDesc &);
  void Fetch(pkgAcquire::ItemDesc &);
  void Done(pkgAcquire::ItemDesc &);
  void Fail(pkgAcquire::ItemDesc &);
  bool Pulse(pkgAcquire *Owner);
  void Start();
  void Stop();
  void Complete();

  /** \brief Create a background-status object.
   *
   *  This should be created in the foreground thread.
   *
   *  \param _real_status  The status object that will be used to
   *                       broadcast messages in the foreground thread.
   *
   *  \param _post_thunk   How to schedule a thunk for execution in the
   *                       foreground thread.
   */
  background_status(download_signal_log *_real_status,
		    post_thunk_func _post_thunk)
    : real_status(_real_status),
      post_thunk(_post_thunk)
  {
  }
};

/** The thread that performs the download. */
class download_thread
{
  cwidget::threads::box<bool> cancelled;

  /** A callback that posts thunks to be run in the main thread. */
  post_thunk_func post_thunk;

  /** The bundled download_manager object.  It should have been
   *  initialized using a background_status wrapper as above, and you
   *  should join() this thread before deleting it.  (it is also OK
   *  to join() the thread first thing in the object's destructor)
   */
  boost::shared_ptr<download_manager> m;

  /** The continuation of this download, invoked in the main thread
   *  with this thread and the result of the run as parameters.  The
   *  thread will be automatically join()ed before the continuation is
   *  deleted.
   */
  safe_slot2<void, download_thread *, pkgAcquire::RunResult> continuation;

  cwidget::threads::thread *t;

  download_thread(const download_thread &other);
  download_thread &operator=(const download_thread &other);
public:
  download_thread(const boost::shared_ptr<download_manager> &manager,
		  post_thunk_func _post_thunk,
		  const safe_slot2<void, download_thread *, pkgAcquire::RunResult> &_continuation)
    : cancelled(false), post_thunk(_post_thunk),
      m(manager), continuation(_continuation), t(NULL)
  {
  }

  ~download_thread()
  {
    delete t;
  }

  void operator()();

  void start()
  {
    if(t != NULL)
      _error->Error(_("Attempt to start a download thread twice!"));
    else
      t = new cwidget::threads::thread(cwidget::threads::noncopy_bootstrap<download_thread>(*this));
  }

  void join()
  {
    t->join();
  }
};

#endif // DOWNLOAD_THREAD_H