summaryrefslogtreecommitdiffstats
path: root/src/kbtransferfile.cpp
blob: d8e158e95498d7ff9b10a746f289b6e13be7caf3 (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
/***************************************************************************
 *   Copyright (C) 2004 by Magnus Kulke                                    *
 *   mkulke@magnusmachine                                                  *
 *                                                                         *
 *   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; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
  
// enable > 2gb support (LFS)

#define _LARGEFILE_SOURCE
#define _LARGEFILE64_SOURCE 
 
#include <qpainter.h>
#include <qcolor.h>
  
#include "kbstatustip.h"
#include "kbsiteinfo.h"

#include "kbtransferfile.h"

KbTransferFile::KbTransferFile(QListView *taskview, QListViewItem *after, FtpSession *srcsession, FtpSession *dstsession, KbFileInfo *src, KbFileInfo *dst) : KbTransferItem(taskview, after, srcsession, dstsession, src, dst)
{
	setPixmap(0, KGlobal::iconLoader()->loadIcon("files",KIcon::Small));
	m_time_old = -1;
	m_xfered_old = 0;
	m_percentage = 0;
}

KbTransferFile::KbTransferFile(QListViewItem *root, QListViewItem *after, FtpSession *srcsession, FtpSession *dstsession, KbFileInfo *src, KbFileInfo *dst) : KbTransferItem(root, after, srcsession, dstsession, src, dst)
{
	setPixmap(0, KGlobal::iconLoader()->loadIcon("files",KIcon::Small));
	m_time_old = -1;
	m_xfered_old = 0;
}

KbTransferFile::~KbTransferFile()
{
}

int KbTransferFile::rtti() const
{
    return KbTransferItem::file;
}

void KbTransferFile::Info()
{
	qWarning("INFO: transfer file from %s to %s", mp_srcsession->name(), mp_dstsession->name());
	qWarning("INFO: mp_src->fileName() = %s", mp_src->fileName().latin1());
	qWarning("INFO: mp_dst->fileName() = %s", mp_dst->fileName().latin1());
	qWarning("INFO: mp_src->dirPath() = %s", mp_src->dirPath(true).latin1());
	qWarning("INFO: mp_dst->dirPath() = %s", mp_dst->dirPath(true).latin1());
}

void KbTransferFile::ShowProgress(KbStatusTip *statustip)
{
	int time = m_time.elapsed();
	int time_dif = time - m_time_old;
	if (time_dif == 0) time_dif = 1;
	off64_t xfer_dif = m_xfered - m_xfered_old;

	off64_t currentsize = (m_xfered + mp_dst->Size()) >> 10;
	off64_t wholesize = mp_src->Size() >> 10;
	off64_t rest = wholesize - currentsize;
	m_percentage = ((currentsize * 100 ) / (wholesize + 1));
	int speed = xfer_dif / time_dif;
	off64_t remaining = rest / (speed + 1);
	
	if ((mp_srcsession->Connected()) && (mp_dstsession->Connected()))
	{
		setText(1, "unknown kb of " + QString::number(wholesize) + "kb");	
		setText(2, "unknown kb/s");	
		setText(4, "unknown");
	}
	else
	{
		setText(1, QString::number(currentsize) + "kb of " + QString::number(wholesize) + "kb");
		setText(2, QString::number(speed) + "kb/s");
		setText(4, QString::number(remaining / 3600) + "h" + QString::number(remaining / 60) + "m" + QString::number(remaining % 60) + "s");
	}
		
	m_time_old = time;
	m_xfered_old = m_xfered;
	
	statustip->ShowStatus(mp_src->fileName()
		+ "," + QString::number(m_percentage) + "%," + QString::number(speed) + "kb/s");
	
	//QToolTip::add(systemtray, mp_src->fileName() 
	//+ "," + QString::number(percentage) + "%," + QString::number(speed) + "kb/s");
}

void KbTransferFile::paintCell( QPainter *painter, const QColorGroup &colorGroup, int column,
		int width, int alignment )
{
	if ((column == 3) 
		&& (m_xfered_old != 0) 
		&& ((!mp_srcsession->Connected()) || (!mp_dstsession->Connected())))
		PaintPercentageBar (painter, width);
	else QListViewItem::paintCell(painter, colorGroup, column, width, alignment);
}

void KbTransferFile::PaintPercentageBar(QPainter * painter, int width)
{
	int len = width * m_percentage / 100;
	painter->fillRect(0, 0, width, height(), Qt::lightGray);
	painter->fillRect(0, 0, len, height(), Qt::gray);
	painter->drawText((width / 2) - 10, height() - 5, QString::number(m_percentage) + "%"); 
}