summaryrefslogtreecommitdiff
path: root/src/download_screen.cc
blob: 1f02ee4c4c69b194d31ca798421145862d587610 (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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// download_screen.cc
//
//  Copyright 1999-2001, 2004-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.
//
//  Handles a download by acting as a progress meter.

#include "aptitude.h"

#include "download_screen.h"
#include "download_item.h"
#include "ui.h"

#include <generic/util/eassert.h>
#include <signal.h>

#include <apt-pkg/strutl.h>
#include <apt-pkg/acquire-worker.h>

#include <cwidget/generic/util/transcode.h>
#include <cwidget/toplevel.h>
#include <cwidget/dialogs.h>

#include <sigc++/adaptors/bind.h>
#include <sigc++/functors/ptr_fun.h>

#include <generic/util/mut_fun.h>

static void set_and_exit(bool &target, bool val)
{
  target=val;
  toplevel::exitmain();
}

bool download_screen::MediaChange(string Media, string Drive)
{
  widgets::widget_ref tmpref(this);

  char buf[512];

  snprintf(buf, 512,
	   _("Please insert the disc labeled \"%s\" into the drive \"%s\""),
	   Media.c_str(), Drive.c_str());

  bool rval=true;

  popup_widget(widgets::dialog_yesno(cw::util::transcode(buf),
			       arg(sigc::bind(sigc::ptr_fun(set_and_exit),
					      rval, true)),
			       cw::util::transcode(_("Continue")),
			       arg(sigc::bind(sigc::ptr_fun(set_and_exit),
					      rval, false)),
			       cw::util::transcode(_("Abort")),
			       get_style("MediaChange")));

  toplevel::mainloop();  // Eeeeeek!  Recursive mainloop!  I'm afraid..

  return rval;
}

void download_screen::IMSHit(pkgAcquire::ItemDesc &itmdesc)
{
  widgets::widget_ref tmpref(this);

  downloadmap::iterator found=active_items.find(itmdesc.Owner);

  if(found==active_items.end())
    {
      download_item *newitm=new download_item(itmdesc);
      newitm->download_done(true);
      contents->add_child(newitm);
      sync_bounds();
    }
  else
    found->second->download_done(true);
  toplevel::update();
  toplevel::tryupdate();
}

void download_screen::Fetch(pkgAcquire::ItemDesc &itmdesc)
{
  widgets::widget_ref tmpref(this);

  downloadmap::iterator found=active_items.find(itmdesc.Owner);

  if(found==active_items.end())
    {
      download_item *newitm=new download_item(itmdesc);
      active_items[itmdesc.Owner]=newitm;
      contents->add_child(newitm);
      sync_bounds();
    }

  toplevel::update();
  toplevel::tryupdate();
}

void download_screen::Done(pkgAcquire::ItemDesc &itmdesc)
{
  widgets::widget_ref tmpref(this);

  downloadmap::iterator found=active_items.find(itmdesc.Owner);
  if(found==active_items.end())
    {
      download_item *newitm=new download_item(itmdesc);
      newitm->download_done(false);
      contents->add_child(newitm);
      sync_bounds();
    }
  else
    {
      found->second->download_done(false);
      found->second->set_worker(NULL);
    }

  toplevel::update();
  toplevel::tryupdate();
}

void download_screen::Fail(pkgAcquire::ItemDesc &itmdesc)
{
  widgets::widget_ref tmpref(this);

  downloadmap::iterator found=active_items.find(itmdesc.Owner);
  if(found!=active_items.end())
    found->second->set_worker(NULL);

  // Nothing really to do??
  toplevel::update();
  toplevel::tryupdate();
}

bool download_screen::Pulse(pkgAcquire *Owner)
{
  widgets::widget_ref tmpref(this);

  pkgAcquireStatus::Pulse(Owner);

  for(pkgAcquire::Worker *i=Owner->WorkersBegin(); i; i=Owner->WorkerStep(i))
    {
      if(i->CurrentItem)
	get_itm(*i->CurrentItem)->set_worker(i);
    }

  toplevel::poll();

  toplevel::update();
  toplevel::tryupdate();

  return !cancelled;
}

void download_screen::Start()
{
  widgets::widget_ref tmpref(this);

  pkgAcquireStatus::Start();
}

void download_screen::Stop()
{
  widgets::widget_ref tmpref(this);

  char buf[256];

  pkgAcquireStatus::Stop();

  snprintf(buf, 256, _("Downloaded %sB in %ss (%sB/s)."), SizeToStr(FetchedBytes).c_str(), TimeToStr(ElapsedTime).c_str(), SizeToStr(CurrentCPS).c_str());

  popup_widget(widgets::dialog_ok(cw::util::transcode(buf),
			    arg(sigc::ptr_fun(toplevel::exitmain))));

  toplevel::mainloop();

  destroy();
}

#if 0
void download_screen::paint_status()
{
  if(finished)
    widgets::tree::paint_status();
  else
    {
      int width,height;
      getmaxyx(height,width);
      string output="";

      output+=_("Total Progress: ");

      int barsize=0;

      if((TotalBytes+TotalItems)>0)
	{
	  char progress_string[50]; // More'n enough chars.
	  unsigned long ETA=(unsigned long)((TotalBytes-CurrentBytes)/CurrentCPS);
	  // Straight from acqprogress.cc, that is..

	  barsize=int((double(width*(CurrentBytes+CurrentItems)))/(TotalBytes+TotalItems));
	  if(barsize>width)
	    barsize=width;

	  attrset(get_status_color());
	  snprintf(progress_string, 50, _(" [ %i%% ] (%sB/s, %s remaining)"), int(double((100.0*(CurrentBytes+CurrentItems)))/(TotalBytes+TotalItems)), SizeToStr(CurrentCPS).c_str(), TimeToStr(ETA).c_str());
	  output+=progress_string;
	}

      show_string_as_progbar(0,
			     height-1,
			     output,
			     get_color("Progress"),
			     get_status_color(),
			     barsize,
			     width);
    }
}
#endif

bool download_screen::handle_key(const cwi::key &k)
{
  widgets::widget_ref tmpref(this);

  if(cw::global_bindings.key_matches(k, "Quit"))
    cancelled=true;
  else
    return widgets::tree::handle_key(k);

  return true;
}

download_screen::~download_screen()
{
}