~ubuntu-branches/ubuntu/raring/ktp-text-ui/raring-proposed

« back to all changes in this revision

Viewing changes to lib/chat-widget.cpp

  • Committer: Package Import Robot
  • Author(s): Rohan Garg
  • Date: 2012-08-26 12:46:37 UTC
  • mfrom: (1.2.2)
  • Revision ID: package-import@ubuntu.com-20120826124637-lhv0slddscxlwzbw
Tags: 0.5.0-0ubuntu1
* New upstream release
  - Drop 03_no_redundant_glib_calls.diff 
  - Update 02_ktpchat_soversion.diff
  - Disable tests
  - Update install file

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
#include <KDebug>
38
38
#include <KColorScheme>
39
39
#include <KLineEdit>
 
40
#include <KMimeType>
 
41
#include <KTemporaryFile>
40
42
 
41
43
#include <TelepathyQt/Account>
42
44
#include <TelepathyQt/Message>
44
46
#include <TelepathyQt/AvatarData>
45
47
#include <TelepathyQt/Connection>
46
48
#include <TelepathyQt/Presence>
 
49
#include <TelepathyQt/PendingChannelRequest>
 
50
#include <TelepathyQt/OutgoingFileTransferChannel>
47
51
 
48
52
#include <KTp/presence.h>
49
53
#include "message-processor.h"
71
75
    LogManager *logManager;
72
76
    QTimer *pausedStateTimer;
73
77
 
 
78
    QList< Tp::OutgoingFileTransferChannelPtr > tmpFileTransfers;
 
79
 
74
80
    KComponentData telepathyComponentData();
75
81
};
76
82
 
128
134
 
129
135
    d->ui.chatArea->load((d->isGroupChat?AdiumThemeView::GroupChat:AdiumThemeView::SingleUserChat));
130
136
 
 
137
    d->ui.sendMessageBox->setAcceptDrops(false);
 
138
    d->ui.chatArea->setAcceptDrops(false);
 
139
    setAcceptDrops(true);
 
140
 
131
141
    AdiumThemeHeaderInfo info;
132
142
 
133
143
    info.setGroupChat(d->isGroupChat);
242
252
 
243
253
KIcon ChatWidget::icon() const
244
254
{
245
 
    if (d->channel->connection()->status() == Tp::ConnectionStatusConnected) {
 
255
    if (d->account->currentPresence() != Tp::Presence::offline()) {
246
256
        //normal chat - self and one other person.
247
257
        if (!d->isGroupChat) {
248
258
            //find the other contact which isn't self.
277
287
 
278
288
void ChatWidget::setChatEnabled(bool enable)
279
289
{
 
290
    d->ui.contactsView->setEnabled(enable);
280
291
    d->ui.sendMessageBox->setEnabled(enable);
281
 
 
282
 
    // show a message informing the user
283
 
    AdiumThemeStatusInfo statusMessage;
284
 
 
285
 
    if (!enable) {
286
 
        statusMessage.setMessage(i18n("Connection closed"));
287
 
    } else {
288
 
        statusMessage.setMessage(i18nc("Connected to IM service", "Connected"));
289
 
    }
290
 
    statusMessage.setService(d->channel->connection()->protocolName());
291
 
    statusMessage.setTime(QDateTime::currentDateTime());
292
 
    d->ui.chatArea->addStatusMessage(statusMessage);
293
 
 
294
292
    Q_EMIT iconChanged(icon());
295
293
}
296
294
 
297
295
void ChatWidget::setTextChannel(const Tp::TextChannelPtr &newTextChannelPtr)
298
296
{
 
297
    if (!d->channel.isNull()) {
 
298
        onChannelConnectionChanged(newTextChannelPtr->connection()->status());
 
299
    }
299
300
    d->channel = newTextChannelPtr;     // set the new channel
300
301
    d->contactModel->setTextChannel(newTextChannelPtr);
301
302
 
329
330
    QWidget::keyPressEvent(e);
330
331
}
331
332
 
 
333
void ChatWidget::temporaryFileTransferStateChanged(Tp::FileTransferState state, Tp::FileTransferStateChangeReason reason)
 
334
{
 
335
    Q_UNUSED(reason);
 
336
 
 
337
    if ((state == Tp::FileTransferStateCompleted) || (state == Tp::FileTransferStateCancelled)) {
 
338
        Tp::OutgoingFileTransferChannel *channel = qobject_cast<Tp::OutgoingFileTransferChannel*>(sender());
 
339
        Q_ASSERT(channel);
 
340
 
 
341
        QString localFile = QUrl(channel->uri()).toLocalFile();
 
342
        if (QFile::exists(localFile)) {
 
343
            QFile::remove(localFile);
 
344
            kDebug() << "File" << localFile << "removed";
 
345
        }
 
346
 
 
347
        d->tmpFileTransfers.removeAll(Tp::OutgoingFileTransferChannelPtr(channel));
 
348
    }
 
349
}
 
350
 
 
351
 
 
352
void ChatWidget::temporaryFileTransferChannelCreated(Tp::PendingOperation *operation)
 
353
{
 
354
    Tp::PendingChannelRequest *request = qobject_cast<Tp::PendingChannelRequest*>(operation);
 
355
    Q_ASSERT(request);
 
356
 
 
357
    Tp::OutgoingFileTransferChannelPtr transferChannel;
 
358
    transferChannel = Tp::OutgoingFileTransferChannelPtr::qObjectCast<Tp::Channel>(request->channelRequest()->channel());
 
359
    Q_ASSERT(!transferChannel.isNull());
 
360
 
 
361
    /* Make sure the pointer lives until the transfer is over
 
362
     * so that the signal connection below lasts until the end */
 
363
    d->tmpFileTransfers << transferChannel;
 
364
 
 
365
    connect(transferChannel.data(), SIGNAL(stateChanged(Tp::FileTransferState,Tp::FileTransferStateChangeReason)),
 
366
            this, SLOT(temporaryFileTransferStateChanged(Tp::FileTransferState,Tp::FileTransferStateChangeReason)));
 
367
}
 
368
 
 
369
 
 
370
void ChatWidget::dropEvent(QDropEvent *e)
 
371
{
 
372
    const QMimeData *data = e->mimeData();
 
373
 
 
374
    if (data->hasUrls()) {
 
375
        Q_FOREACH(const QUrl &url, data->urls()) {
 
376
            if (url.isLocalFile()) {
 
377
                Tp::FileTransferChannelCreationProperties properties(
 
378
                        url.toLocalFile(),
 
379
                        KMimeType::findByFileContent(url.toLocalFile())->name());
 
380
                d->account->createFileTransfer(d->channel->targetContact(),
 
381
                        properties, QDateTime::currentDateTime(),
 
382
                        QLatin1String("org.freedesktop.Telepathy.Client.KTp.FileTransfer"));
 
383
            } else {
 
384
                d->ui.sendMessageBox->append(url.toString());
 
385
            }
 
386
        }
 
387
        e->acceptProposedAction();
 
388
    } else if (data->hasText()) {
 
389
        d->ui.sendMessageBox->append(data->text());
 
390
        e->acceptProposedAction();
 
391
    } else if (data->hasHtml()) {
 
392
        d->ui.sendMessageBox->insertHtml(data->html());
 
393
        e->acceptProposedAction();
 
394
    } else if (data->hasImage()) {
 
395
        QImage image = qvariant_cast<QImage>(data->imageData());
 
396
 
 
397
        KTemporaryFile tmpFile;
 
398
        tmpFile.setPrefix(d->account->displayName() + QLatin1String("-"));
 
399
        tmpFile.setSuffix(QLatin1String(".png"));
 
400
        tmpFile.setAutoRemove(false);
 
401
        if (!tmpFile.open()) {
 
402
            return;
 
403
        }
 
404
        tmpFile.close();
 
405
 
 
406
        if (!image.save(tmpFile.fileName(), "PNG")) {
 
407
            return;
 
408
        }
 
409
 
 
410
        Tp::FileTransferChannelCreationProperties properties(
 
411
                    tmpFile.fileName(),
 
412
                    KMimeType::findByFileContent(tmpFile.fileName())->name());
 
413
        Tp::PendingChannelRequest *request;
 
414
        request = d->account->createFileTransfer(d->channel->targetContact(),
 
415
                        properties, QDateTime::currentDateTime(),
 
416
                        QLatin1String("org.freedesktop.Telepathy.Client.KTp.FileTransfer"));
 
417
        connect(request, SIGNAL(finished(Tp::PendingOperation*)),
 
418
                this, SLOT(temporaryFileTransferChannelCreated(Tp::PendingOperation*)));
 
419
 
 
420
        kDebug() << "Starting transfer of" << tmpFile.fileName();
 
421
        e->acceptProposedAction();
 
422
    }
 
423
 
 
424
    QWidget::dropEvent(e);
 
425
}
 
426
 
 
427
void ChatWidget::dragEnterEvent(QDragEnterEvent *e)
 
428
{
 
429
    if (e->mimeData()->hasHtml() || e->mimeData()->hasImage() ||
 
430
        e->mimeData()->hasText() || e->mimeData()->hasUrls()) {
 
431
            e->accept();
 
432
    }
 
433
 
 
434
    QWidget::dragEnterEvent(e);
 
435
}
 
436
 
332
437
QString ChatWidget::title() const
333
438
{
334
439
    return d->title;
394
499
            SLOT(onChatStatusChanged(Tp::ContactPtr,Tp::ChannelChatState)));
395
500
    connect(d->channel.data(), SIGNAL(invalidated(Tp::DBusProxy*,QString,QString)),
396
501
            this, SLOT(onChannelInvalidated()));
 
502
    connect(d->channel.data()->connection().data(), SIGNAL(statusChanged(Tp::ConnectionStatus)),
 
503
            this, SLOT(onChannelConnectionChanged(Tp::ConnectionStatus)));
397
504
 
398
505
    if (d->channel->hasChatStateInterface()) {
399
506
        connect(d->ui.sendMessageBox, SIGNAL(textChanged()), SLOT(onInputBoxChanged()));
613
720
    if (message.sender() == d->channel->groupSelfContact()) {
614
721
        return;
615
722
    }
616
 
    
 
723
 
617
724
    if (message.isDeliveryReport()) {
618
725
        return;
619
726
    }
620
 
    
 
727
 
621
728
    // kde_telepathy_contact_highlight (contains your name)
622
729
    // kde_telepathy_info_event
623
730
 
937
1044
        d->channel->requestChatState(Tp::ChannelChatStatePaused);
938
1045
}
939
1046
 
 
1047
void ChatWidget::onChannelConnectionChanged(Tp::ConnectionStatus status)
 
1048
{
 
1049
    if (status == Tp::ConnectionStatusConnected) {
 
1050
        onContactPresenceChange(d->channel->groupSelfContact(), KTp::Presence(d->channel->groupSelfContact()->presence()));
 
1051
    } else if (status == Tp::ConnectionStatusDisconnected) {
 
1052
        // show a message informing the user
 
1053
        AdiumThemeStatusInfo statusMessage;
 
1054
        statusMessage.setMessage(i18n("You are now offline"));
 
1055
        statusMessage.setService(d->channel->connection()->protocolName());
 
1056
        statusMessage.setTime(QDateTime::currentDateTime());
 
1057
        d->ui.chatArea->addStatusMessage(statusMessage);
 
1058
    }
 
1059
}
 
1060
 
940
1061
#include "chat-widget.moc"