File: transferlistmodel.cpp

package info (click to toggle)
dc-qt 0.2.0.alpha-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,936 kB
  • ctags: 5,361
  • sloc: cpp: 28,936; makefile: 19
file content (158 lines) | stat: -rw-r--r-- 4,836 bytes parent folder | download | duplicates (4)
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
//
// C++ Implementation: transferlistmodel
//
// Description: 
//
//
// Author: Rikard Björklind <olof@linux.nu>, (C) 2005
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include <QtCore>
#include "transferlistmodel.h"
#include <iostream>
#include "util.h"
#include "log.h"
using namespace std;

QVariant TransferListModel::data ( const QModelIndex & index, int role ) const
{
	if(!index.isValid()) return QVariant();
	if(index.row() < 0 || index.row() >= transfers.size()) return QVariant();
	if(index.column() < 0 || index.column() >= NUM_COLUMNS) return QVariant();
	if(role == Qt::DisplayRole) {
		int i = index.row();
		switch(index.column()) {
			/*
			case 0:                return transfers[i].pos;
			case 1:                return transfers[i].startpos;
			case 2:                return transfers[i].actual;
			case 3:                return transfers[i].size;
			case 4:                return transfers[i].averageSpeed;
			case 5:                return transfers[i].secondsLeft;
			case 6:                return transfers[i].bytesLeft;
			case 7:                return transfers[i].filename;
			case 8:                return transfers[i].localfilename;
			*/
			case COL_USER:         return transfers[i].userid;
			case COL_FILE_NAME:    return transfers[i].filename;
			case COL_SIZE_LEFT:    return Util::bytesToStr(transfers[i].bytesLeft);
			case COL_SIZE_TOTAL:   return Util::bytesToStr(transfers[i].size);
			case COL_AVG_SPEED:    return Util::bytesToStr(transfers[i].averageSpeed);
			case COL_STATUS:          return transfers[i].status;
			case COL_PROGRESS_BAR: {
				QList<QVariant> pdata;
				// Data position in the QList must match its index in the eProgressData enum.
				// This silly-looking loop populates the QList in the correct order.
				for (int ii=0; ii<PDATA_MAX_INDEX; ii++) {
					switch (ii) {
					case PDATA_IS_DOWNLOAD:
						pdata.append( transfers[i].type == FileTransfer::DOWNLOAD ); break;
					case PDATA_START_POS:
						pdata.append( static_cast<qlonglong>(transfers[i].startpos) ); break;
					case PDATA_POS:
						pdata.append( static_cast<qlonglong>(transfers[i].pos) ); break;
					case PDATA_ACTUAL:
						pdata.append( static_cast<qlonglong>(transfers[i].actual) ); break;
					case PDATA_SIZE:
						pdata.append( static_cast<qlonglong>(transfers[i].size) ); break;
					case PDATA_SECONDS_LEFT:
						pdata.append( transfers[i].secondsLeft); break;
					default:
						break;
					}
				}
				return pdata;
			}
			default:
				break;
		}
	}
	return QVariant();
}

QVariant TransferListModel::headerData ( int section, Qt::Orientation orientation, int role ) const
{
    if (role != Qt::DisplayRole)
        return QVariant();

    if(orientation==Qt::Horizontal) {
		switch(section) {
		case COL_USER:         return tr("User");
		case COL_FILE_NAME:    return tr("File");
		case COL_PROGRESS_BAR: return tr("Progress");
		case COL_SIZE_LEFT:    return tr("Size left");
		case COL_SIZE_TOTAL:   return tr("Total size");
		case COL_AVG_SPEED:    return tr("Avg. Speed");
		case COL_STATUS:          return tr("Status");
		default:
			break;
		}
		/*
		if(section==0) return "Position";
		if(section==1) return "StartPos";
		if(section==2) return "Actual";
		if(section==3) return "Size";
		if(section==4) return "AvgSpeed";
		if(section==5) return "TimeLeft";
		if(section==6) return "BytesLeft";
		if(section==7) return "Filename";
		if(section==8) return "Localname";
		*/
    }
    return QVariant();
}

void TransferListModel::onTransferStart( const FileTransfer& f )
{
	logger->debug(f.toString());
	if( !transfers.contains(f) ) transfers.append(f);
	emit layoutChanged();
}

void TransferListModel::onTransferTick( const QList<FileTransfer>& fl)
{
	//logger->debug("TransferListModel::onTransferTick");
	int minIndex=2000000000;
	int maxIndex=0;
	bool newItems = false;
	
	for(int i=0;i < fl.size();i++) {
		if( !transfers.contains( fl[i] ) ) {
			transfers.append(fl[i]);
			newItems=true;
		}
		else {
			int ind = transfers.indexOf(fl[i]);
			transfers[ind] = fl[i];
			if(ind < minIndex) minIndex=ind;
			if(ind > maxIndex) maxIndex=ind;
		}
    }
	if(newItems) emit layoutChanged();
	if(maxIndex>=minIndex) emit dataChanged(createIndex(minIndex,0),createIndex(maxIndex,NUM_COLUMNS-1));
}

void TransferListModel::onTransferComplete( const FileTransfer& f)
{
	logger->info(QString("Transfer complete: ") + f.toString());
	transfers.removeAll( f );
	emit layoutChanged();
}

void TransferListModel::onTransferFailed( const FileTransfer& f, const QString& errorMsg)
{
	logger->error("Transfer failed: " + errorMsg);
	logger->info(f.toString());
	//transfers.removeAll( f );
	
	// Mark as failed, update status message field.
	int i = transfers.indexOf( f );
	if( i != -1 ) {
		transfers[i].status = errorMsg;
	}
	
	emit layoutChanged();
}