1 /* This file is part of the KDE project
2 Copyright (C) 2003 Andrew Stanley-Jones <asj-kde@cban.com>
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the Artistic License.
8 #include "dccManager.h"
18 #include <kpushbutton.h>
27 dccItem::dccItem( K3ListView
*parent
, dccManager
*manager
, enum dccType type
, const QString
&file
, const QString
& who
, enum dccStatus status
, unsigned int size
)
28 : QObject(), K3ListViewItem(parent
), m_who(who
), m_file(file
), m_type(type
)
37 setText(COL_FILE
, file
);
38 setText(COL_WHO
, who
);
39 setText(COL_STAT
, enumToStatus(status
));
41 setText(COL_SIZE
, "");
43 setText(COL_SIZE
, QString("%1").arg(size
));
48 dccItem::dccItem( K3ListViewItem
*parent
, dccManager
*manager
, enum dccType type
, const QString
&file
, const QString
& who
, enum dccStatus status
, unsigned int size
)
49 : QObject(), K3ListViewItem(parent
), m_who(who
), m_file(file
), m_type(type
)
57 setText(COL_FILE
, file
);
58 setText(COL_WHO
, who
);
59 setText(COL_STAT
, enumToStatus(status
));
61 setText(COL_SIZE
, QString("%1").arg(size
));
70 QString
dccItem::enumToStatus(enum dccStatus status
)
75 str
= i18n("Receiving");
78 str
= i18n("Got Offer");
81 str
= i18n("Sent Offer");
84 str
= i18n("Resume Requested");
87 str
= i18n("Did Resume");
90 str
= i18n("Sending");
93 str
= i18nc("dcc status", "Open");
99 str
= i18n("Canceled");
105 str
= i18n("Unknown State");
111 void dccItem::setWhoPostfix(const QString
&post
) {
113 setText(COL_WHO
, QString("%1 %2").arg(m_who
).arg(post
));
116 void dccItem::changeFilename(const QString
&file
) {
117 setText(COL_FILE
, file
);
121 void dccItem::changeWho(const QString
&who
) {
122 setText(COL_WHO
, who
);
126 void dccItem::changeStatus(enum dccStatus status
)
128 m_manager
->doChanged();
129 setText(COL_STAT
, enumToStatus(status
));
131 emit
statusChanged(this);
134 void dccItem::setReceivedBytes(int bytes
)
137 time_t ctime
= time(NULL
);
143 per
= (100*bytes
)/m_size
;
147 if((per
!= (int)m_percent
) ||
148 (ctime
>= (m_lasttime
+ 2))
151 setText(COL_SIZE
, QString("%1/%2").arg(bytes
).arg(m_size
));
152 setText(COL_PER
, QString("%1%").arg(per
));
154 if(m_status
== dccResumed
)
155 m_stime
= 0; /* if we are got a resume request don't update CPS, reset it */
157 setText(COL_CPS
, QString("%1").arg(1.0*bytes
/(time(NULL
) - m_stime
), 2));
163 void dccItem::doRename()
166 if(type() == dccGet
){
167 setRenameEnabled(COL_FILE
, true);
168 startRename(COL_FILE
);
170 else if(type() == dccChat
){
171 setText(COL_WHO
, m_who
);
172 setRenameEnabled(COL_WHO
, true);
173 startRename(COL_WHO
);
178 void dccItem::okRename(int col
)
180 K3ListViewItem::okRename(col
);
181 if(type() == dccGet
){
182 QString oldfile
= m_file
;
183 changeFilename(text(COL_FILE
));
184 emit
itemRenamed(this, m_who
, oldfile
);
185 setRenameEnabled(COL_FILE
, false);
187 else if(type() == dccChat
){
188 QString oldwho
= m_who
;
189 changeWho(text(COL_WHO
));
190 emit
itemRenamed(this, oldwho
, m_file
);
191 setRenameEnabled(COL_WHO
, false);
192 setWhoPostfix(m_post
);
196 void dccItem::cancelRename(int col
)
198 K3ListViewItem::cancelRename(col
);
199 if(type() == dccChat
){
200 setWhoPostfix(m_post
);
204 dccManager::dccManager( QWidget
*parent
, const char *name
) : dccManagerbase( parent
, name
)
208 m_getit
= new K3ListViewItem( klvBox
);
209 m_sendit
= new K3ListViewItem( klvBox
);
210 m_chatit
= new K3ListViewItem( klvBox
);
212 m_getit
->setOpen(true);
213 m_sendit
->setOpen(true);
214 m_chatit
->setOpen(true);
216 m_getit
->setSelectable(false);
217 m_sendit
->setSelectable(false);
218 m_chatit
->setSelectable(false);
220 // connect(klvBox, SIGNAL(clicked(QListViewItem *)),
221 // this, SLOT(getSelChange(QListViewItem *)));
222 connect(klvBox
, SIGNAL(currentChanged(Q3ListViewItem
*)),
223 this, SLOT(getSelChange(Q3ListViewItem
*)));
225 klvBox
->setCurrentItem(m_chatit
);
226 getSelChange(klvBox
->currentItem());
229 dccManager::~dccManager()
233 dccItem
*dccManager::newSendItem(QString file
, QString who
, enum dccItem::dccStatus status
, unsigned int size
)
235 emit
changed(false, i18n("dcc activity"));
236 dccItem
*it
= new dccItem(m_sendit
, this, dccItem::dccSend
, file
, who
, status
, size
);
237 connect(it
, SIGNAL(statusChanged(Q3ListViewItem
*)),
238 this, SLOT(getSelChange(Q3ListViewItem
*)));
242 dccItem
*dccManager::newGetItem(QString file
, QString who
, enum dccItem::dccStatus status
, unsigned int size
)
244 emit
changed(false, i18n("dcc activity"));
245 dccItem
*it
= new dccItem(m_getit
, this, dccItem::dccGet
, file
, who
, status
, size
);
246 connect(it
, SIGNAL(statusChanged(Q3ListViewItem
*)),
247 this, SLOT(getSelChange(Q3ListViewItem
*)));
252 dccItem
*dccManager::newChatItem(QString who
, enum dccItem::dccStatus status
)
254 emit
changed(false, i18n("dcc activity"));
255 dccItem
*it
= new dccItem(m_chatit
, this, dccItem::dccChat
, "", who
, status
, 0);
256 connect(it
, SIGNAL(statusChanged(Q3ListViewItem
*)),
257 this, SLOT(getSelChange(Q3ListViewItem
*)));
262 void dccManager::kpbNew_clicked()
265 dccNewDialog
->show();
266 dccNewDialog
->raise();
270 dccNewDialog
= new dccNew();
271 dccNewDialog
->show();
273 connect(dccNewDialog
, SIGNAL(accepted(int, QString
, QString
)),
274 this, SLOT(dccNewAccepted(int, QString
, QString
)));
278 void dccManager::dccNewAccepted(int type
, QString nick
, QString file
){
279 kDebug(5008) << "Type: " << type
<< " nick: " << nick
<< " file: " << file
<< endl
;
280 if(type
== dccNew::Chat
){
281 QByteArray cstr
= QByteArray("/dcc chat ") + nick
.latin1() + "\n";
282 kDebug(5008) << "Output: " << cstr
<< endl
;
283 emit
outputLine(cstr
);
285 else if(type
== dccNew::Send
){
286 QByteArray cstr
= QByteArray("/dcc send ") + nick
.latin1() + " " + file
.latin1() + "\n";
287 emit
outputLine(cstr
);
293 void dccManager::kpbConnect_clicked()
295 dccItem
*it
= dynamic_cast<dccItem
*>(klvBox
->currentItem());
297 emit
dccConnectClicked(it
);
301 void dccManager::kpbResume_clicked()
303 dccItem
*it
= dynamic_cast<dccItem
*>(klvBox
->currentItem());
305 emit
dccResumeClicked(it
);
308 void dccManager::kpbRename_clicked()
310 dccItem
*it
= dynamic_cast<dccItem
*>(klvBox
->currentItem());
312 emit
dccRenameClicked(it
);
315 void dccManager::kpbAbort_clicked()
317 dccItem
*it
= dynamic_cast<dccItem
*>(klvBox
->currentItem());
319 emit
dccAbortClicked(it
);
323 void dccManager::getSelChange(Q3ListViewItem
*_i
) {
324 kpbAbort
->setEnabled(false);
325 kpbResume
->setEnabled(false);
326 kpbRename
->setEnabled(false);
327 kpbConnect
->setEnabled(false);
331 dccItem
*it
= dynamic_cast<dccItem
*>(_i
);
336 * Everything is off by default
337 * We need to turn on what we want
340 case dccItem::dccChat
:
341 kDebug(5008) << "is a chat" << endl
;
342 kpbAbort
->setEnabled(true);
343 switch(it
->status()){
344 case dccItem::dccGotOffer
:
345 kpbConnect
->setEnabled(true);
347 case dccItem::dccOpen
:
348 kpbRename
->setEnabled(true);
354 case dccItem::dccGet
:
355 kDebug(5008) << "is a get" << endl
;
356 kpbAbort
->setEnabled(true);
357 switch(it
->status()){
358 case dccItem::dccGotOffer
:
359 case dccItem::dccWaitOnResume
:
360 case dccItem::dccResumed
:
361 kpbResume
->setEnabled(true);
362 kpbConnect
->setEnabled(true);
363 kpbRename
->setEnabled(true);
369 case dccItem::dccSend
:
370 kDebug(5008) << "is a send" << endl
;
371 kpbAbort
->setEnabled(true);
379 void dccManager::sendSelChange(Q3ListViewItem
*) {
383 dccItem *it = static_cast<dccItem *>(_i);
385 kDebug(5008) << "got: " << it->who() << " file: " << it->file() << endl;
387 switch(it->status()){
388 case dccItem::dccSending:
389 case dccItem::dccRecving:
390 kpbSendStop->setText(i18n("Abort"));
392 case dccItem::dccOffer:
393 kpbSendStop->setText(i18n("Forget"));
395 case dccItem::dccDone:
396 kpbSendStop->setText(i18n("Clear"));
404 #include "dccManager.moc"