source: trunk/libtransmission/completion.h @ 8060

Last change on this file since 8060 was 8060, checked in by livings124, 13 years ago

when showing the stats for selected files, progress on a transfer with no selected files should be 0%, not -100%

  • Property svn:keywords set to Date Rev Author Id
File size: 4.4 KB
Line 
1/*
2 * This file Copyright (C) 2009 Charles Kerr <charles@transmissionbt.com>
3 *
4 * This file is licensed by the GPL version 2.  Works owned by the
5 * Transmission project are granted a special exemption to clause 2(b)
6 * so that the bulk of its code can remain under the MIT license.
7 * This exemption does not extend to derived works not owned by
8 * the Transmission project.
9 *
10 * $Id: completion.h 8060 2009-03-14 19:55:24Z livings124 $
11 */
12
13#ifndef TR_COMPLETION_H
14#define TR_COMPLETION_H
15
16#ifndef __TRANSMISSION__
17 #error only libtransmission should #include this header.
18#endif
19
20#include <assert.h>
21
22#include "transmission.h"
23#include "utils.h" /* tr_bitfield */
24
25typedef struct tr_completion
26{
27    tr_bool  sizeWhenDoneIsDirty;
28    tr_bool  haveValidIsDirty;
29
30    tr_torrent *    tor;
31
32    /* do we have this block? */
33    tr_bitfield    blockBitfield;
34
35    /* do we have this piece? */
36    tr_bitfield    pieceBitfield;
37
38    /* a block is complete if and only if we have it */
39    uint16_t *  completeBlocks;
40
41    /* number of bytes we'll have when done downloading. [0..info.totalSize]
42       DON'T access this directly; it's a lazy field.
43       use tr_cpSizeWhenDone() instead! */
44    uint64_t    sizeWhenDoneLazy;
45
46    /* number of bytes we'll have when done downloading. [0..info.totalSize]
47       DON'T access this directly; it's a lazy field.
48       use tr_cpHaveValid() instead! */
49    uint64_t    haveValidLazy;
50
51    /* number of bytes we want or have now. [0..sizeWhenDone] */
52    uint64_t    sizeNow;
53}
54tr_completion;
55
56/**
57*** Life Cycle
58**/
59
60tr_completion * tr_cpConstruct( tr_completion *, tr_torrent * );
61
62tr_completion * tr_cpDestruct( tr_completion * );
63
64static TR_INLINE tr_completion* tr_cpNew( tr_torrent * tor )
65{
66    return tr_cpConstruct( tr_new0( tr_completion, 1 ), tor );
67}
68
69static TR_INLINE void tr_cpFree( tr_completion * cp )
70{
71    tr_free( tr_cpDestruct( cp ) );
72}
73
74/**
75*** General
76**/
77
78tr_completeness            tr_cpGetStatus( const tr_completion * );
79
80uint64_t                   tr_cpHaveValid( const tr_completion * );
81
82uint64_t                   tr_cpSizeWhenDone( const tr_completion * );
83
84void                       tr_cpInvalidateDND( tr_completion * );
85
86void                       tr_cpGetAmountDone( const   tr_completion * completion,
87                                               float                 * tab,
88                                               int                     tabCount );
89
90static TR_INLINE uint64_t tr_cpHaveTotal( const tr_completion * cp )
91{
92    return cp->sizeNow;
93}
94
95static TR_INLINE uint64_t tr_cpLeftUntilComplete( const tr_completion * cp )
96{
97    return tr_torrentInfo(cp->tor)->totalSize - cp->sizeNow;
98}
99
100static TR_INLINE uint64_t tr_cpLeftUntilDone( const tr_completion * cp )
101{
102    return tr_cpSizeWhenDone( cp ) - cp->sizeNow;
103}
104
105static TR_INLINE float tr_cpPercentComplete( const tr_completion * cp )
106{
107    return tr_getRatio( cp->sizeNow, tr_torrentInfo(cp->tor)->totalSize );
108}
109
110static TR_INLINE float tr_cpPercentDone( const tr_completion * cp )
111{
112    const double ratio = tr_getRatio( cp->sizeNow, tr_cpSizeWhenDone( cp ) );
113    return (ratio == TR_RATIO_NA ||  ratio == TR_RATIO_INF) ? 0.0f : ratio;
114}
115
116/**
117*** Pieces
118**/
119
120int tr_cpMissingBlocksInPiece( const tr_completion  * cp,
121                               tr_piece_index_t       piece );
122
123tr_bool  tr_cpPieceIsComplete( const tr_completion * cp,
124                               tr_piece_index_t      piece );
125
126void   tr_cpPieceAdd( tr_completion    * completion,
127                      tr_piece_index_t   piece );
128
129void   tr_cpPieceRem( tr_completion     * completion,
130                      tr_piece_index_t   piece );
131
132tr_bool tr_cpFileIsComplete( const tr_completion * cp, tr_file_index_t );
133
134/**
135*** Blocks
136**/
137
138static TR_INLINE tr_bool tr_cpBlockIsComplete( const tr_completion * cp, tr_block_index_t block ) {
139    return tr_bitfieldHas( &cp->blockBitfield, block );
140}
141
142void      tr_cpBlockAdd( tr_completion * completion,
143                         tr_block_index_t block );
144
145tr_bool   tr_cpBlockBitfieldSet( tr_completion      * completion,
146                                 struct tr_bitfield * blocks );
147
148/***
149****
150***/
151
152static TR_INLINE const struct tr_bitfield * tr_cpPieceBitfield( const tr_completion * cp ) {
153    return &cp->pieceBitfield;
154}
155
156static TR_INLINE const struct tr_bitfield * tr_cpBlockBitfield( const tr_completion * cp ) {
157    assert( cp );
158    assert( cp->blockBitfield.bits );
159    assert( cp->blockBitfield.bitCount );
160    return &cp->blockBitfield;
161}
162
163#endif
Note: See TracBrowser for help on using the repository browser.