summaryrefslogtreecommitdiffstats
path: root/ksirc/dccManager.cpp
blob: 9ac46b7f0c0c37c63582d9a111f3a0f650797303 (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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/* This file is part of the KDE project
   Copyright (C) 2003 Andrew Stanley-Jones <[email protected]>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the Artistic License.
*/

#include "dccManager.h"
#include "dccNew.h"

#include <tqobject.h>
#include <tqsignal.h>

#include <kdebug.h>
#include <tdelocale.h>
#include <kpushbutton.h>

#define COL_FILE 1
#define COL_WHO  0
#define COL_STAT 2
#define COL_SIZE 3
#define COL_CPS  4
#define COL_PER  5

dccItem::dccItem( TDEListView *parent, dccManager *manager, enum dccType type, const TQString &file, const TQString& who, enum dccStatus status, unsigned int size )
    : TQObject(), TDEListViewItem(parent), m_who(who), m_file(file), m_type(type)
{
    m_percent = 0;
    m_status = status;
    m_size = size;
    m_stime = 0;
    m_lasttime = 0;
    m_manager = manager;

    setText(COL_FILE, file);
    setText(COL_WHO, who);
    setText(COL_STAT, enumToStatus(status));
    if(m_type == dccChat)
        setText(COL_SIZE, "");
    else
        setText(COL_SIZE, TQString("%1").arg(size));
    setText(COL_PER, "");

}

dccItem::dccItem( TDEListViewItem *parent, dccManager *manager, enum dccType type, const TQString &file, const TQString& who, enum dccStatus status, unsigned int size )
: TQObject(), TDEListViewItem(parent), m_who(who), m_file(file), m_type(type)
{
    m_percent = 0;
    m_status = status;
    m_size = size;
    m_stime = 0;
    m_manager = manager;

    setText(COL_FILE, file);
    setText(COL_WHO, who);
    setText(COL_STAT, enumToStatus(status));
    if(type != dccChat)
	setText(COL_SIZE, TQString("%1").arg(size));
    setText(COL_PER, "");

}

dccItem::~dccItem()
{
}

TQString dccItem::enumToStatus(enum dccStatus status)
{
    TQString str;
    switch(status){
    case dccRecving:
        str = i18n("Receiving");
        break;
    case dccGotOffer:
        str = i18n("Got Offer");
	break;
    case dccSentOffer:
        str = i18n("Sent Offer");
	break;
    case dccWaitOnResume:
	str = i18n("Resume Requested");
        break;
    case dccResumed:
        str = i18n("Did Resume");
        break;
    case dccSending:
        str = i18n("Sending");
        break;
    case dccOpen:
        str = i18n("dcc status", "Open");
        break;
    case dccDone:
        str = i18n("Done");
        break;
    case dccCancel:
        str = i18n("Canceled");
        break;
    case dccError:
        str = i18n("Error");
        break;
    default:
        str = i18n("Unknown State");
        break;
    }
    return str;
}

void dccItem::setWhoPostfix(const TQString &post) {
    m_post = post;
    setText(COL_WHO, TQString("%1 %2").arg(m_who).arg(post));
}

void dccItem::changeFilename(const TQString &file) {
    setText(COL_FILE, file);
    m_file = file;
}

void dccItem::changeWho(const TQString &who) {
    setText(COL_WHO, who);
    m_who = who;
}

void dccItem::changeStatus(enum dccStatus status)
{
    m_manager->doChanged();
    setText(COL_STAT, enumToStatus(status));
    m_status = status;
    emit statusChanged(this);
}

void dccItem::setReceivedBytes(int bytes)
{
    int per;
    time_t ctime = time(NULL);

    if(m_stime == 0)
        m_stime = ctime-1;

    if(m_size)
        per = (100*bytes)/m_size;
    else
        per = 100;

    if((per != (int)m_percent) ||
       (ctime >= (m_lasttime + 2))
      ){
        m_lasttime = ctime;
        setText(COL_SIZE, TQString("%1/%2").arg(bytes).arg(m_size));
        setText(COL_PER, TQString("%1%").arg(per));
	m_percent = per;
	if(m_status == dccResumed)
            m_stime = 0; /* if we are got a resume request don't update CPS, reset it */
        else
	    setText(COL_CPS, TQString("%1").arg(1.0*bytes/(time(NULL) - m_stime), 2));
    }


}

void dccItem::doRename()
{

    if(type() == dccGet){
	setRenameEnabled(COL_FILE, true);
	startRename(COL_FILE);
    }
    else if(type() == dccChat){
	setText(COL_WHO, m_who);
	setRenameEnabled(COL_WHO, true);
	startRename(COL_WHO);
    }

}

void dccItem::okRename(int col)
{
    TDEListViewItem::okRename(col);
    if(type() == dccGet){
	TQString oldfile = m_file;
	changeFilename(text(COL_FILE));
	emit itemRenamed(this, m_who, oldfile);
	setRenameEnabled(COL_FILE, false);
    }
    else if(type() == dccChat){
	TQString oldwho = m_who;
	changeWho(text(COL_WHO));
	emit itemRenamed(this, oldwho, m_file);
	setRenameEnabled(COL_WHO, false);
        setWhoPostfix(m_post);
    }
}

void dccItem::cancelRename(int col)
{
    TDEListViewItem::cancelRename(col);
    if(type() == dccChat){
	setWhoPostfix(m_post);
    }
}

dccManager::dccManager( TQWidget *parent, const char *name ) : dccManagerbase( parent, name)
{
    dccNewDialog = 0x0;

    m_getit = new TDEListViewItem(klvBox, i18n("Get"));
    m_sendit = new TDEListViewItem(klvBox, i18n("Send"));
    m_chatit = new TDEListViewItem(klvBox, i18n("Chat"));

    m_getit->setOpen(true);
    m_sendit->setOpen(true);
    m_chatit->setOpen(true);

    m_getit->setSelectable(false);
    m_sendit->setSelectable(false);
    m_chatit->setSelectable(false);

//    connect(klvBox, TQ_SIGNAL(clicked(TQListViewItem *)),
//	    this,   TQ_SLOT(getSelChange(TQListViewItem *)));
    connect(klvBox, TQ_SIGNAL(currentChanged(TQListViewItem *)),
	    this,   TQ_SLOT(getSelChange(TQListViewItem *)));

    klvBox->setCurrentItem(m_chatit);
    getSelChange(klvBox->currentItem());
}

dccManager::~dccManager()
{
}

dccItem *dccManager::newSendItem(TQString file, TQString who, enum dccItem::dccStatus status, unsigned int size)
{
    emit changed(false, i18n("dcc activity"));
    dccItem *it = new dccItem(m_sendit, this, dccItem::dccSend, file, who, status, size);
    connect(it,   TQ_SIGNAL(statusChanged(TQListViewItem *)),
            this, TQ_SLOT(getSelChange(TQListViewItem *)));
    return it;
}

dccItem *dccManager::newGetItem(TQString file, TQString who, enum dccItem::dccStatus status, unsigned int size)
{
    emit changed(false, i18n("dcc activity"));
    dccItem *it = new dccItem(m_getit, this, dccItem::dccGet, file, who, status, size);
    connect(it,   TQ_SIGNAL(statusChanged(TQListViewItem *)),
	    this, TQ_SLOT(getSelChange(TQListViewItem *)));
    return it;

}

dccItem *dccManager::newChatItem(TQString who, enum dccItem::dccStatus status)
{
    emit changed(false, i18n("dcc activity"));
    dccItem *it = new dccItem(m_chatit, this, dccItem::dccChat, "", who, status, 0);
    connect(it,   TQ_SIGNAL(statusChanged(TQListViewItem *)),
	    this, TQ_SLOT(getSelChange(TQListViewItem *)));
    return it;

}

void dccManager::kpbNew_clicked()
{
    if(dccNewDialog){
	dccNewDialog->show();
	dccNewDialog->raise();
        return;
    }

    dccNewDialog = new dccNew();
    dccNewDialog->show();

    connect(dccNewDialog, TQ_SIGNAL(accepted(int, TQString, TQString)),
	    this, TQ_SLOT(dccNewAccepted(int, TQString, TQString)));

}

void dccManager::dccNewAccepted(int type, TQString nick, TQString file){
    kdDebug(5008) << "Type: " << type << " nick: " << nick << " file: " << file << endl;
    if(type == dccNew::Chat){
	TQCString cstr = TQCString("/dcc chat ") + nick.latin1() + "\n";
	kdDebug(5008) << "Output: " << cstr << endl;
        emit outputLine(cstr);
    }
    else if(type == dccNew::Send){
	TQCString cstr = TQCString("/dcc send ") + nick.latin1() + " " + file.latin1() + "\n";
	emit outputLine(cstr);
    }
    delete dccNewDialog;
    dccNewDialog = 0x0;
}

void dccManager::kpbConnect_clicked()
{
    dccItem *it = dynamic_cast<dccItem *>(klvBox->currentItem());
    if(it){
        emit dccConnectClicked(it);
    }

}
void dccManager::kpbResume_clicked()
{
    dccItem *it = dynamic_cast<dccItem *>(klvBox->currentItem());
    if(it){
        emit dccResumeClicked(it);
    }
}
void dccManager::kpbRename_clicked()
{
    dccItem *it = dynamic_cast<dccItem *>(klvBox->currentItem());
    if(it){
        emit dccRenameClicked(it);
    }
}
void dccManager::kpbAbort_clicked()
{
    dccItem *it = dynamic_cast<dccItem *>(klvBox->currentItem());
    if(it){
        emit dccAbortClicked(it);
    }
}

void dccManager::getSelChange(TQListViewItem *_i) {
    kpbAbort->setEnabled(false);
    kpbResume->setEnabled(false);
    kpbRename->setEnabled(false);
    kpbConnect->setEnabled(false);

    if(_i == 0)
        return;
    dccItem *it = dynamic_cast<dccItem *>(_i);
    if(!it)
        return;

    /*
     * Everything is off by default
     * We need to turn on what we want
     */
    switch(it->type()){
    case dccItem::dccChat:
        kdDebug(5008) << "is a chat" << endl;
        kpbAbort->setEnabled(true);
	switch(it->status()){
	case dccItem::dccGotOffer:
	    kpbConnect->setEnabled(true);
	    break;
	case dccItem::dccOpen:
	    kpbRename->setEnabled(true);
	    break;
	default:
            break;
	}
	break;
    case dccItem::dccGet:
	kdDebug(5008) << "is a get" << endl;
	kpbAbort->setEnabled(true);
	switch(it->status()){
	case dccItem::dccGotOffer:
	case dccItem::dccWaitOnResume:
	case dccItem::dccResumed:
	    kpbResume->setEnabled(true);
	    kpbConnect->setEnabled(true);
	    kpbRename->setEnabled(true);
	    break;
	default:
	    break;
	}
	break;
    case dccItem::dccSend:
	kdDebug(5008) << "is a send" << endl;
	kpbAbort->setEnabled(true);
	break;
    default:
	break;
    }


}
void dccManager::sendSelChange(TQListViewItem *) {
    /*
    if(_i == 0)
        return;
    dccItem *it = static_cast<dccItem *>(_i);

    kdDebug(5008) << "got: " << it->who() << " file: " << it->file() << endl;

    switch(it->status()){
    case dccItem::dccSending:
    case dccItem::dccRecving:
        kpbSendStop->setText(i18n("Abort"));
        break;
    case dccItem::dccOffer:
        kpbSendStop->setText(i18n("Forget"));
        break;
    case dccItem::dccDone:
        kpbSendStop->setText(i18n("Clear"));
        break;
        }
        */

}


#include "dccManager.moc"