summaryrefslogtreecommitdiff
path: root/src/ui_download_manager.cc
blob: ea1f9d177c6e57481095e59f25659a8c90bf70b3 (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
// ui_download_manager.cc
//
//   Copyright (C) 2005, 2007-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.

#include "ui_download_manager.h"

#include "aptitude.h"
#include "ui.h"
#include "progress.h"

#include <generic/apt/apt.h>
#include <generic/apt/download_manager.h>
#include <generic/apt/download_signal_log.h>

#include <sigc++/functors/mem_fun.h>

#include <cwidget/widgets/widget.h> // For cw::widget_ref

namespace cw = cwidget;
namespace cwidget
{
  using namespace widgets;
}

ui_download_manager::~ui_download_manager()
{
  log->Complete();

  download_stops();

  abort_state.abort();

  delete log;
  delete st;
}

void ui_download_manager::done(download_thread *t, pkgAcquire::RunResult res)
{
  delete t;

  progress_with_destructor pair = make_progress_bar();
  done_progress = pair.first;
  done_progress_destructor = pair.second;

  if(!abort_state.get_aborted())
    {
      manager->finish(res, done_progress.unsafe_get_ref(),
		      sigc::mem_fun(*this, &ui_download_manager::finish_done));
      return;
    }
  else
    {
      finish_done(download_manager::failure);
      return;
    }
}

void ui_download_manager::finish_done(download_manager::result run_res)
{
  apt_load_cache(done_progress.unsafe_get_ref(), true);

  done_progress_destructor();
  done_progress = NULL;

  if(run_res == download_manager::do_again && !abort_state.get_aborted())
    {
      sigc::slot2<void, download_thread *, pkgAcquire::RunResult>
	done_slot = sigc::mem_fun(this, &ui_download_manager::done);
      (new download_thread(manager,
			   post_thunk,
			   make_safe_slot(done_slot)))->start();
    }
  else
    {
      download_complete(run_res == download_manager::success);
      delete this;
    }
}

void ui_download_manager::start()
{
  download_starts();

  progress_with_destructor pair = make_progress_bar();
  refcounted_progress_ref p = pair.first;
  sigc::slot0<void> p_destructor = pair.second;

  bool ok = manager->prepare(*p.unsafe_get_ref(), *st, log);

  p_destructor();

  if(ok)
    {
      sigc::slot2<void, download_thread *, pkgAcquire::RunResult>
	done_slot = sigc::mem_fun(this, &ui_download_manager::done);
      (new download_thread(manager,
			   post_thunk,
			   make_safe_slot(done_slot)))->start();
    }
  else
    delete this;
}