Now the systrayicon change it's color when a download is in progress. I simply change...
[kdenetwork.git] / kget / transferlist.cpp
blob1d9ed902ded4fe8d82070407ae82cdaed5362e95
1 /***************************************************************************
2 * transferlist.cpp
3 * -------------------
5 * Revision : $Id$
6 * begin : Tue Jan 29 2002
7 * copyright : (C) 2002 by Patrick Charbonnier
8 * : Based On Caitoo v.0.7.3 (c) 1998 - 2000, Matej Koss
9 * email : pch@freeshell.org
11 ****************************************************************************/
13 /***************************************************************************
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 ***************************************************************************/
27 #include <kapplication.h>
28 #include <kstandarddirs.h>
29 #include <kglobal.h>
30 #include <kiconloader.h>
31 #include <klocale.h>
32 #include <kconfig.h>
33 #include <kmessagebox.h>
34 #include <ksimpleconfig.h>
35 #include <kio/netaccess.h>
37 #include "transfer.h"
38 #include "transferlist.h"
40 #define NUM_COLS 9
42 static int defaultColumnWidth[] = {
43 26, // PIXMAP
44 160, // LOCAL FILENAME
45 40, // RESUME
46 60, // COUNT
47 30, // PROGRESS
48 65, // TOTAL
49 70, // SPEED
50 70, // REMAINING TIME
51 450 // URL
55 TransferList::TransferList(QWidget * parent, const char *name)
56 : KListView(parent, name)
58 // enable selection of more than one item
59 setSelectionMode( QListView::Extended );
61 // // disable sorting and clicking on headers
62 // setSorting( -1 );
64 setAllColumnsShowFocus(true);
66 lv_pixmap = addColumn(i18n("S"));
67 lv_filename = addColumn(i18n("Local File Name"));
68 lv_resume = addColumn(i18n("Resumed"));
69 lv_count = addColumn(i18n("Count"));
70 lv_progress = addColumn(i18n("%"));
71 lv_total = addColumn(i18n("Total"));
72 lv_speed = addColumn(i18n("Speed"));
73 lv_remaining = addColumn(i18n("Rem. Time"));
74 lv_url = addColumn(i18n("Address (URL)"));
76 jobid=1;
78 // initial layout
79 KConfig *config = KGlobal::config();
80 config->setGroup("ListView");
81 if ( config->readListEntry("ColumnWidths").isEmpty() )
83 for (int i = 0; i < NUM_COLS; i++)
84 setColumnWidth(i, defaultColumnWidth[i]);
86 else
87 restoreLayout( KGlobal::config(), "ListView" );
89 QString connectPath = "pics/connect%2.png";
90 QString tryPath = "pics/try%2.png";
92 // Load animations
93 QPixmap* curPix;
94 if (animConn.count() == 0) {
95 animConn.setAutoDelete(true);
96 animTry.setAutoDelete(true);
97 for (int i = 0; i < 8; i++) {
98 curPix = new QPixmap();
99 curPix->load(locate("appdata", connectPath.arg(i)));
100 animConn.append(curPix);
102 curPix = new QPixmap();
103 curPix->load(locate("appdata", tryPath.arg(i)));
104 animTry.append(curPix);
108 pixQueued = UserIcon("md_queued");
109 pixScheduled = UserIcon("md_scheduled");
110 pixDelayed = UserIcon("md_delayed.png");
111 pixFinished = UserIcon("md_finished");
112 pixRetrying = UserIcon("retrying");
114 phasesNum = animConn.count();
116 connect(this, SIGNAL(doubleClicked(QListViewItem *)), SLOT(slotTransferSelected(QListViewItem *)));
117 connect(this, SIGNAL(rightButtonPressed(QListViewItem *, const QPoint &, int)), SLOT(slotPopupMenu(QListViewItem *)));
121 TransferList::~TransferList()
123 saveLayout( KGlobal::config(), "ListView" );
127 Transfer *TransferList::addTransfer(const KURL & _source, const KURL & _dest,
128 bool canShow)
130 Transfer *last = static_cast<Transfer*>( lastItem() );
131 Transfer *new_item = new Transfer(this, last, _source, _dest, jobid);
132 jobid++;
133 if ( canShow )
134 new_item->maybeShow();
136 return new_item;
140 void TransferList::slotTransferSelected(QListViewItem * item)
142 emit transferSelected((Transfer *) item);
146 void TransferList::slotPopupMenu(QListViewItem * item)
148 if (!item)
149 return;
150 emit popupMenu((Transfer *) item);
154 void TransferList::setSelected(QListViewItem * item, bool selected)
156 bool tmpb = selected;
158 if (tmpb && item->isSelected()) {
159 tmpb = false;
162 QListView::setSelected(item, tmpb);
166 void TransferList::moveToBegin(Transfer * item)
168 // ASSERT(item);
170 Transfer *oldfirst=static_cast<Transfer*>(firstChild());
171 item->moveItem(oldfirst); //move item after oldfirst
172 oldfirst->moveItem(item); //move oldfirst after item
176 void TransferList::moveToEnd(Transfer * item)
178 // ASSERT(item);
180 Transfer *oldlast=static_cast<Transfer*>(lastItem());
181 item->moveItem(oldlast);
185 bool TransferList::updateStatus(int counter)
187 bool isTransfer = false;
189 TransferIterator it(this);
191 for (; it.current(); ++it) {
192 isTransfer |= it.current()->updateStatus(counter);
195 return isTransfer;
199 bool TransferList::areTransfersQueuedOrScheduled()
201 TransferIterator it(this);
203 if (childCount() > 0) {
204 for (; it.current(); ++it) {
205 if ((it.current()->getMode() == Transfer::MD_QUEUED)|| \
206 (it.current()->getMode() == Transfer::MD_SCHEDULED))
207 return true;
210 return false;
214 Transfer * TransferList::find(const KURL& _src)
216 TransferIterator it(this);
218 for (; it.current(); ++it) {
219 if (it.current()->getSrc() == _src) {
220 return it.current();
224 return 0L;
228 void TransferList::readTransfers(const KURL& file)
230 QString tmpFile;
232 if (KIO::NetAccess::download(file, tmpFile, (QWidget*)parent())) {
233 KSimpleConfig config(tmpFile);
235 config.setGroup("Common");
236 int num = config.readNumEntry("Count", 0);
238 Transfer *item;
239 KURL src, dest;
241 for ( int i = 0; i < num; i++ )
243 QString str;
245 str.sprintf("Item%d", i);
246 config.setGroup(str);
248 src = KURL::fromPathOrURL( config.readPathEntry("Source") );
249 dest = KURL::fromPathOrURL( config.readPathEntry("Dest") );
250 item = addTransfer( src, dest, false ); // don't show!
252 if (!item->read(&config, i))
253 delete item;
254 else
256 // configuration read, now we know the status to determine
257 // whether to show or not
258 item->maybeShow();
264 void TransferList::writeTransfers(const QString& file)
266 sDebug << ">>>>Entering with file =" << file << endl;
268 KSimpleConfig config(file);
269 int num = childCount();
271 config.setGroup("Common");
272 config.writeEntry("Count", num);
274 TransferIterator it(this);
276 for (int id = 0; it.current(); ++it, ++id)
277 it.current()->write(&config, id);
278 config.sync();
280 sDebug << "<<<<Leaving" << endl;
283 #include "transferlist.moc"