~ubuntu-branches/ubuntu/raring/rtorrent/raring

« back to all changes in this revision

Viewing changes to src/display/window_download_chunks_seen.cc

  • Committer: Bazaar Package Importer
  • Author(s): Dmitry E. Oboukhov
  • Date: 2009-08-16 09:12:32 UTC
  • mfrom: (1.1.12 upstream) (5.1.6 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090816091232-fkf317r620hqsrz1
Tags: 0.8.5-2
* Add patch to fix crash by SCGI, closes: 541487, thanks for jdrexler.
* Standards-Version was bumped to 3.8.3 (no changes required).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// rTorrent - BitTorrent client
2
 
// Copyright (C) 2005-2007, Jari Sundell
3
 
//
4
 
// This program is free software; you can redistribute it and/or modify
5
 
// it under the terms of the GNU General Public License as published by
6
 
// the Free Software Foundation; either version 2 of the License, or
7
 
// (at your option) any later version.
8
 
// 
9
 
// This program is distributed in the hope that it will be useful,
10
 
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
// GNU General Public License for more details.
13
 
// 
14
 
// You should have received a copy of the GNU General Public License
15
 
// along with this program; if not, write to the Free Software
16
 
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 
//
18
 
// In addition, as a special exception, the copyright holders give
19
 
// permission to link the code of portions of this program with the
20
 
// OpenSSL library under certain conditions as described in each
21
 
// individual source file, and distribute linked combinations
22
 
// including the two.
23
 
//
24
 
// You must obey the GNU General Public License in all respects for
25
 
// all of the code used other than OpenSSL.  If you modify file(s)
26
 
// with this exception, you may extend this exception to your version
27
 
// of the file(s), but you are not obligated to do so.  If you do not
28
 
// wish to do so, delete this exception statement from your version.
29
 
// If you delete this exception statement from all source files in the
30
 
// program, then also delete it here.
31
 
//
32
 
// Contact:  Jari Sundell <jaris@ifi.uio.no>
33
 
//
34
 
//           Skomakerveien 33
35
 
//           3185 Skoppum, NORWAY
36
 
 
37
 
#include "config.h"
38
 
 
39
 
#include <cmath>
40
 
#include <stdexcept>
41
 
#include <rak/functional.h>
42
 
#include <rak/string_manip.h>
43
 
#include <torrent/bitfield.h>
44
 
#include <torrent/data/block.h>
45
 
#include <torrent/data/block_list.h>
46
 
#include <torrent/data/transfer_list.h>
47
 
 
48
 
#include "core/download.h"
49
 
 
50
 
#include "window_download_chunks_seen.h"
51
 
 
52
 
namespace display {
53
 
 
54
 
WindowDownloadChunksSeen::WindowDownloadChunksSeen(core::Download* d, unsigned int *focus) :
55
 
  Window(new Canvas, 0, 0, 0, extent_full, extent_full),
56
 
  m_download(d),
57
 
  m_focus(focus) {
58
 
}
59
 
 
60
 
void
61
 
WindowDownloadChunksSeen::redraw() {
62
 
  // TODO: Make this depend on tracker signal.
63
 
  m_slotSchedule(this, (cachedTime + rak::timer::from_seconds(10)).round_seconds());
64
 
  m_canvas->erase();
65
 
 
66
 
  if (m_canvas->height() < 3 || m_canvas->width() < 18)
67
 
    return;
68
 
 
69
 
  m_canvas->print(2, 0, "Chunks seen: [C/A/D %i/%i/%.2f]",
70
 
                  (int)m_download->download()->peers_complete(),
71
 
                  (int)m_download->download()->peers_accounted(),
72
 
                  std::floor(m_download->distributed_copies() * 100.0f) / 100.0f);
73
 
 
74
 
  const uint8_t* seen = m_download->download()->chunks_seen();
75
 
 
76
 
  if (seen == NULL || m_download->download()->file_list()->bitfield()->empty()) {
77
 
    m_canvas->print(2, 2, "Not available.");
78
 
    return;
79
 
  }
80
 
 
81
 
  if (!m_download->is_done()) { 
82
 
    m_canvas->print(36, 0, "X downloaded    missing    queued    downloading");
83
 
    m_canvas->print_char(50, 0, 'X' | A_BOLD);
84
 
    m_canvas->print_char(61, 0, 'X' | A_BOLD | A_UNDERLINE);
85
 
    m_canvas->print_char(71, 0, 'X' | A_REVERSE);
86
 
  }
87
 
 
88
 
  *m_focus = std::min(*m_focus, max_focus());
89
 
 
90
 
  const uint8_t* chunk = seen + *m_focus * chunks_per_row();
91
 
  const uint8_t* last = seen + m_download->download()->file_list()->size_chunks();
92
 
 
93
 
  const torrent::Bitfield* bitfield = m_download->download()->file_list()->bitfield();
94
 
  const torrent::TransferList* transfers = m_download->download()->transfer_list();
95
 
  std::vector<torrent::BlockList*> transferChunks(transfers->size(), 0);
96
 
 
97
 
  std::copy(transfers->begin(), transfers->end(), transferChunks.begin());
98
 
  std::sort(transferChunks.begin(), transferChunks.end(), rak::less2(std::mem_fun(&torrent::BlockList::index), std::mem_fun(&torrent::BlockList::index)));
99
 
 
100
 
  std::vector<torrent::BlockList*>::const_iterator itrTransfer = transferChunks.begin();
101
 
 
102
 
  while (itrTransfer != transferChunks.end() && (uint32_t)(chunk - seen) > (*itrTransfer)->index())
103
 
    itrTransfer++;
104
 
 
105
 
  for (unsigned int y = 1; y < m_canvas->height() && chunk < last; ++y) {
106
 
    m_canvas->print(0, y, "%5u ", (int)(chunk - seen));
107
 
 
108
 
    while (chunk < last) {
109
 
      chtype attr;
110
 
 
111
 
      if (bitfield->get(chunk - seen)) {
112
 
        attr = A_NORMAL;
113
 
      } else if (itrTransfer != transferChunks.end() && (uint32_t)(chunk - seen) == (*itrTransfer)->index()) {
114
 
        if (std::find_if((*itrTransfer)->begin(), (*itrTransfer)->end(), std::mem_fun_ref(&torrent::Block::is_transfering)) != (*itrTransfer)->end())
115
 
          attr = A_REVERSE;
116
 
        else
117
 
          attr = A_BOLD | A_UNDERLINE;
118
 
        itrTransfer++;
119
 
      } else {
120
 
        attr = A_BOLD;
121
 
      }
122
 
 
123
 
      m_canvas->print_char(attr | rak::value_to_hexchar<0>(std::min<uint8_t>(*chunk, 0xF)));
124
 
      chunk++;
125
 
 
126
 
      if ((chunk - seen) % 10 == 0) {
127
 
        if (m_canvas->get_x() + 12 > m_canvas->width())
128
 
          break;
129
 
 
130
 
        m_canvas->print_char(' ');
131
 
      }
132
 
    }
133
 
  }
134
 
}
135
 
 
136
 
unsigned int
137
 
WindowDownloadChunksSeen::rows() const {
138
 
  if (m_canvas->width() < 18)
139
 
    return 0;
140
 
 
141
 
  return (m_download->download()->file_list()->size_chunks() + chunks_per_row() - 1) / chunks_per_row();
142
 
}
143
 
 
144
 
}