source: qutecom-2.2/qutecom/src/presentation/qt/chat/QtChatWidget.cpp @ 673:345ff9dded46

Last change on this file since 673:345ff9dded46 was 673:345ff9dded46, checked in by laurent@…, 3 years ago

bug fix : double message in chat

File size: 21.2 KB
Line 
1/*
2 * QuteCom, a voice over Internet phone
3 * Copyright (C) 2010 Mbdsys
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20#include "QtChatWidget.h"
21#include "QtChatUtils.h"
22#include "chatroom/QtChatRoomInviteDlg.h"
23#include "emoticons/QtEmoticonsWidget.h"
24
25#include <coipmanager/CoIpManager.h>
26#include <filesessionmanager/SendFileSession.h>
27
28#include <model/config/Config.h>
29#include <model/config/ConfigManager.h>
30#include <model/QuteCom.h>
31#include <model/profile/Profile.h>
32#include <model/profile/UserProfile.h>
33
34#include <control/CQuteCom.h>
35#include <control/contactlist/CContactList.h>
36#include <control/profile/CUserProfile.h>
37#include <presentation/qt/QtQuteCom.h>
38#include <presentation/qt/contactlist/QtContactList.h>
39#include <presentation/qt/contactlist/QtContactListManager.h>
40#include <presentation/qt/filetransfer/QtFileTransfer.h>
41
42#include <imwrapper/Account.h>
43#include <imwrapper/EnumIMProtocol.h>
44#include <imwrapper/IMContactSet.h>
45
46#include <util/File.h>
47#include <util/Logger.h>
48#include <util/SafeDelete.h>
49#include <cutil/global.h>
50#include <qtutil/SafeConnect.h>
51#include <qtutil/StringListConvert.h>
52
53#include <string>
54
55#include <QtCore/QTime>
56#include <QtCore/QTimer>
57
58#include <QtGui/QColorDialog>
59#include <QtGui/QFontDialog>
60#include <QtGui/QKeyEvent>
61#include <QtGui/QPainter>
62
63static const int CHAT_STOPPED_TYPING_DELAY = 1000;
64
65static const int EMOTICON_OFFSET_X = 13;
66static const int EMOTICON_OFFSET_Y = 30;
67
68QString clearHtml( const QString & str)
69{
70        QString html = str;
71       
72        QRegExp htmlDocumentToHtmlSnippet(".*<body[^>]*>\\s*<p[^>]*>(.*)</p>\\s*</body>\\s*</html>");
73        QRegExp pTag("<p[^>]*>");
74
75        // toHtml() returns an HTML document, complete with html and body tags.
76        // This regexp strip those to produce an HTML snippet, which can be
77        // concatenated to the existing history
78        html.replace(htmlDocumentToHtmlSnippet, "\\1");
79        html.replace(pTag, "<div>");
80        html.replace("</p>", "</div>");
81       
82        return html;
83}
84
85QtChatWidget::QtChatWidget(CChatHandler & cChatHandler,
86        QtQuteCom * qtQuteCom, int sessionId,
87        QWidget * parent) :
88        QWidget(parent),
89        _cChatHandler(cChatHandler),
90        _qtQuteCom(qtQuteCom),isLoaded(false) {
91
92        //Default nickname for testing purpose
93        _nickName = "QuteCom";
94        _sessionId = sessionId;
95        _imChatSession = NULL;
96
97        _bold = false;
98        _italic = false;
99        _underline = false;
100
101        _isContactConnected = true;
102
103        //setup ui
104        _ui.setupUi(this);
105        QPixmap pix = _ui.sendButton->icon().pixmap(50,50);
106        QPainter painter(&pix);
107        painter.setPen(Qt::white);
108        painter.drawText(pix.rect(),Qt::AlignCenter,tr("send"));
109        painter.end();
110        _ui.sendButton->setIcon(QIcon(pix));
111        _ui.editActionBar->setBackgroundRole(QPalette::Base);
112        _ui.editFrame->setBackgroundRole(QPalette::Base);
113        ////
114
115        //creates sub widgets
116        _emoticonsWidget = new EmoticonsWidget(this);
117        ////
118
119        //install event filters
120        _ui.chatHistory->installEventFilter(this);
121        _ui.chatEdit->installEventFilter(this);
122        ////
123       
124        //timer
125        _isTyping = false;
126        _stoppedTypingTimer = new QTimer(this);
127        _stoppedTypingTimer->setSingleShot(true);
128        SAFE_CONNECT_RECEIVER(_stoppedTypingTimer, SIGNAL(timeout()), this, SLOT(stoppedTypingSlot()));
129        ////
130
131        //signal connection
132        SAFE_CONNECT(_ui.sendButton, SIGNAL(clicked()), SLOT(sendMessage()));
133        SAFE_CONNECT(_ui.editActionBar, SIGNAL(fontLabelClicked()), SLOT(changeFont()));
134        SAFE_CONNECT(_ui.editActionBar, SIGNAL(emoticonsLabelClicked()), SLOT(chooseEmoticon()));
135        SAFE_CONNECT(_ui.editActionBar, SIGNAL(fontColorLabelClicked()), SLOT(changeFontColor()));
136        SAFE_CONNECT(_ui.editActionBar, SIGNAL(boldLabelClicked()), SLOT(boldClickedSlot()));
137        SAFE_CONNECT(_ui.editActionBar, SIGNAL(italicLabelClicked()), SLOT(italicClickedSlot()));
138        SAFE_CONNECT(_ui.editActionBar, SIGNAL(underlineLabelClicked()), SLOT(underlineClickedSlot()));
139        SAFE_CONNECT(_emoticonsWidget, SIGNAL(emoticonClicked(QtEmoticon)), SLOT(emoticonSelected(QtEmoticon)));
140        SAFE_CONNECT_RECEIVER(_emoticonsWidget, SIGNAL(closed()), _ui.chatEdit, SLOT(setFocus()));
141        SAFE_CONNECT(_ui.chatEdit, SIGNAL(textChanged()), SLOT(chatEditTextChanged()));
142        SAFE_CONNECT(_ui.chatEdit, SIGNAL(fileDragged(const QString &)), SLOT(sendFileToSession(const QString &)));
143        SAFE_CONNECT(this, SIGNAL(contactAddedEventSignal(const IMContact &)),
144                SLOT(contactAddedEventSlot(const IMContact &)));
145        SAFE_CONNECT(this, SIGNAL(contactRemovedEventSignal(const IMContact &)),
146                SLOT(contactRemovedEventSlot(const IMContact &)));
147        //SAFE_CONNECT(_ui.avatarFrameButton, SIGNAL(clicked()), SLOT(avatarFrameButtonClicked()));
148
149        QtContactList * qtContactList = _qtQuteCom->getQtContactList();
150        SAFE_CONNECT(qtContactList, SIGNAL(contactChangedEventSignal(QString)), SLOT(contactChangedSlot(QString)));
151
152        _cChatHandler.getCUserProfile().getUserProfile().profileChangedEvent +=
153                boost::bind(&QtChatWidget::profileChangedEventHandler, this);
154        SAFE_CONNECT_TYPE(this, SIGNAL(profileChangedEventHandlerSignal()), 
155                SLOT(updateUserAvatar()), Qt::QueuedConnection);
156               
157        //send translate message
158        _webView = new QWebView;
159        _webView->page()->mainFrame()->addToJavaScriptWindowObject("parent",this);
160        Config & config = ConfigManager::getInstance().getCurrentConfig();
161        QFile file(QString::fromUtf8(config.getResourcesDir().c_str())+"chat/base.html");
162        file.open(QIODevice::ReadOnly);
163        _webView->setHtml(file.readAll());     
164
165        //load theme
166        connect(_ui.chatHistory,SIGNAL(loadFinished(bool)),this,SLOT(htmlLoadFinished(bool)));
167        _ui.chatHistory->setTheme(QString::fromUtf8(config.getChatTheme().c_str()),QString::fromUtf8(config.getChatThemeVariant().c_str()));
168}
169
170QtChatWidget::~QtChatWidget() {
171        _imChatSession->close();
172       
173        _stoppedTypingTimer->stop();
174       
175        delete _webView;
176}
177
178void QtChatWidget::profileChangedEventHandler() {
179        profileChangedEventHandlerSignal();
180}
181
182void QtChatWidget::changeFont() {
183        bool ok;
184        QFont font = QFontDialog::getFont(&ok, _ui.chatEdit->currentFont(), this);
185        if (ok) {
186                _currentFont = font;
187                _ui.chatEdit->setCurrentFont(font);
188        }
189        _ui.chatEdit->setFocus();
190}
191
192void QtChatWidget::changeFontColor() {
193        bool ok;
194        QRgb color = QColorDialog::getRgba(_ui.chatEdit->textColor().rgba(), &ok, this);
195        if (ok) {
196                _currentColor = QColor(color);
197                _ui.chatEdit->setTextColor(_currentColor);
198        }
199        _ui.chatEdit->setFocus();
200}
201
202void QtChatWidget::boldClickedSlot() {
203        _bold = !_bold;
204        if (_bold) {
205                _ui.chatEdit->setFontWeight(QFont::Bold);
206        } else {
207                _ui.chatEdit->setFontWeight(QFont::Normal);
208        }
209}
210
211void QtChatWidget::italicClickedSlot() {
212        _italic = !_italic;
213        _ui.chatEdit->setFontItalic(_italic);
214}
215
216void QtChatWidget::underlineClickedSlot() {
217        _underline = !_underline;
218        _ui.chatEdit->setFontUnderline(_underline);
219}
220
221void QtChatWidget::translate(const QString & html)
222{
223        Config & config = ConfigManager::getInstance().getCurrentConfig();     
224        QString lang = QString::fromUtf8(config.getTranslationSent().c_str()); 
225        QString text = html;
226       
227        if(text.isEmpty())
228                return;
229       
230        text = text.replace("\"","\\\"");
231        text = text.replace("\'","\\\'");
232       
233        QString script = QString("translate(\"\",\"\",\"%1\",\"\",\"%2\")").arg(text).arg(lang);;
234        _webView->page ()->mainFrame ()->evaluateJavaScript(script);
235}
236
237void QtChatWidget::chooseEmoticon() {
238        QPoint p = _ui.editActionBar->pos();
239        QPoint offset(EMOTICON_OFFSET_X, EMOTICON_OFFSET_Y);
240        p += offset;
241        _emoticonsWidget->move(mapToGlobal(p));
242        _emoticonsWidget->show();
243}
244
245void QtChatWidget::setVisible(bool visible) {
246        QWidget::setVisible(visible);
247        if (visible) {
248                _ui.chatEdit->setFocus();
249        }
250}
251
252void QtChatWidget::showInviteDialog() {
253        if (canDoMultiChat()) {
254                QtChatRoomInviteDlg dlg(*_imChatSession,
255                        _cChatHandler.getCUserProfile().getCContactList(), this);
256                dlg.exec();
257        }
258}
259
260void QtChatWidget::contactAddedEventHandler(IMChatSession & sender, const IMContact & imContact) {
261        contactAddedEventSignal(imContact);
262}
263
264void QtChatWidget::contactRemovedEventHandler(IMChatSession & sender, const IMContact & imContact) {
265        contactRemovedEventSignal(imContact);
266}
267
268void QtChatWidget::contactAddedEventSlot(const IMContact & imContact) {
269
270        QtContactList * qtContactList = _qtQuteCom->getQtContactList();
271        CContactList & cContactList = qtContactList->getCContactList();
272        std::string contactId = cContactList.findContactThatOwns(imContact);
273
274        /*if (_ui.avatarFrame->getContactIDList().contains(QString::fromUtf8(contactId))) {
275                LOG_DEBUG("" + imContact.getContactId() + " deja dans la session !");
276                return;
277        }
278
279        ContactProfile profile = cContactList.getContactProfile(contactId);
280
281        std::string data = profile.getIcon().getData();
282        QPixmap pixmap;
283        pixmap.loadFromData((uchar *)data.c_str(), data.size());
284        _ui.avatarFrame->addRemoteContact(
285                QString::fromUtf8(contactId),
286                QString::fromUtf8(profile.getDisplayName().c_str()),
287                QString::fromUtf8(imContact.getContactId()),
288                pixmap
289        );*/
290
291        QString imContactId = QString::fromUtf8(imContact.getContactId().c_str());
292        addStatusMessage(tr("%1 has joined the chat").arg(imContactId));
293}
294
295void QtChatWidget::contactRemovedEventSlot(const IMContact & imContact) {
296        QString imContactId = QString::fromUtf8(imContact.getContactId().c_str());
297        addStatusMessage(tr("%1 has left the chat").arg(imContactId));
298
299        /*QtContactList * qtContactList = _qtQuteCom->getQtContactList();
300        CContactList & cContactList = qtContactList->getCContactList();
301        std::string contactId = cContactList.findContactThatOwns(imContact);
302        _ui.avatarFrame->removeRemoteContact(QString::fromUtf8(contactId));*/
303}
304
305void QtChatWidget::emoticonSelected(QtEmoticon emoticon) {
306        _ui.chatEdit->insertHtml(emoticon.getHtml());
307        _ui.chatEdit->ensureCursorVisible();
308}
309
310void QtChatWidget::avatarFrameButtonClicked() {
311        /*if (_ui.avatarFrame->isVisible()) {
312                hideAvatarFrame();
313        } else {
314                showAvatarFrame();
315        }*/
316}
317
318void QtChatWidget::hideAvatarFrame() {
319        //_ui.avatarFrame->hide();
320        //_ui.avatarFrameButton->setIcon(QIcon(":pics/chat/show_avatar_frame.png"));
321}
322
323void QtChatWidget::showAvatarFrame() {
324        //_ui.avatarFrame->show();
325        //_ui.avatarFrameButton->setIcon(QIcon(":pics/chat/hide_avatar_frame.png"));
326}
327
328void QtChatWidget::addToHistory(const QString & contactId, const QString & senderName, const QString & str, const QTime & time) {
329        if(!isLoaded)
330                return;
331
332        QTime tmp;
333        if (time.isNull()) {
334                tmp = QTime::currentTime();
335        } else {
336                tmp = time;
337        }
338        _ui.chatHistory->insertMessage(contactId, senderName, str, tmp);
339}
340
341void QtChatWidget::addStatusMessage(const QString & statusMessage) {
342        _ui.chatHistory->insertStatusMessage(statusMessage, QTime::currentTime());
343}
344
345void QtChatWidget::sendMessage() 
346{
347        if(_ui.chatEdit->toPlainText().isEmpty())
348                return;
349       
350        Config & config = ConfigManager::getInstance().getCurrentConfig();
351        QString lang = QString::fromUtf8(config.getTranslationSent().c_str());
352        QString html = clearHtml(_ui.chatEdit->toHtml());
353       
354        translate(html);
355}
356
357void QtChatWidget::setIMChatSession(IMChatSession * imChatSession) {
358        IMAccount * imAccount =
359                _cChatHandler.getCUserProfile().getUserProfile().getIMAccountManager().getIMAccount(imChatSession->getIMChat().getIMAccountId());
360
361        _imChatSession = imChatSession;
362        _emoticonsWidget->initButtons(QtChatUtils::getProtocol(imAccount->getProtocol()));
363       
364        _ui.chatHistory->setProtocol(imAccount->getProtocol());
365
366        OWSAFE_DELETE(imAccount);
367
368        updateAvatarFrame();
369        updateUserAvatar();
370        if (_imChatSession->getIMContactSet().size() == 1) {
371                // It's useless to show the avatar frame if there is only one contact,
372                // since we can see its avatar when he talks.
373                hideAvatarFrame();
374        }
375
376        _imChatSession->contactAddedEvent +=
377                boost::bind(&QtChatWidget::contactAddedEventHandler, this, _1, _2);
378        _imChatSession->contactRemovedEvent +=
379                boost::bind(&QtChatWidget::contactRemovedEventHandler, this, _1, _2);
380        _imChatSession->changeTypingState(IMChat::TypingStateNotTyping);
381}
382
383void QtChatWidget::chatEditTextChanged() {
384        // change typing state to Typing
385        if (!_isTyping) {
386                _imChatSession->changeTypingState(IMChat::TypingStateTyping);
387                _isTyping = true;
388        }
389        ////
390
391        // manage timers
392        _stoppedTypingTimer->start(CHAT_STOPPED_TYPING_DELAY);
393        ////
394}
395
396void QtChatWidget::stoppedTypingSlot() {
397        _imChatSession->changeTypingState(IMChat::TypingStateStopTyping);
398        _isTyping = false;
399}
400
401void QtChatWidget::translationFinishedSlot(const QVariant  & traduction, const QVariant  &, const QVariant  &, const QVariant  & message, const QVariant  & )
402{
403        QString tmp = traduction.toString().replace("&#39;","'");
404        IMAccount * imAccount = _cChatHandler.getCUserProfile().getUserProfile().getIMAccountManager().getIMAccount(_imChatSession->getIMChat().getIMAccountId());
405        QString msg;
406       
407        if(tmp.isEmpty())
408        {
409                msg = message.toString();
410                _imChatSession->sendMessage(QtChatUtils::encodeMessage(imAccount->getProtocol(), msg).toUtf8().constData());
411        }
412        else
413        {
414                QTextDocument doc;
415                doc.setHtml(tmp);
416                msg = "<div>"+message.toString()+"</div><div style=\"color:grey\">"+doc.toPlainText()+"</div>";
417                _imChatSession->sendMessage(QtChatUtils::encodeMessage(imAccount->getProtocol(), tmp).toUtf8().constData());
418        }
419       
420        addToHistory("self", _nickName, msg);
421       
422       
423        OWSAFE_DELETE(imAccount);
424       
425        _isTyping = true;
426        _ui.chatEdit->clear();
427        _ui.chatEdit->setFocus();
428        _ui.chatEdit->ensureCursorVisible();
429       
430        //Not typing anymore
431        _imChatSession->changeTypingState(IMChat::TypingStateStopTyping);
432        _isTyping = false;
433        _stoppedTypingTimer->stop();
434}
435
436void QtChatWidget::updateAvatarFrame() {
437        QMutexLocker locker(&_mutex);
438
439        QtContactList * qtContactList = _qtQuteCom->getQtContactList();
440        CContactList & cContactList = qtContactList->getCContactList();
441
442        IMContactSet imContactSet = _imChatSession->getIMContactSet();
443        IMContactSet::iterator it;
444        for (it = imContactSet.begin(); it != imContactSet.end(); it++) {
445
446                std::string contactId = cContactList.findContactThatOwns(*it);
447                ContactProfile profile = cContactList.getContactProfile(contactId);
448
449                std::string data = profile.getIcon().getData();
450                QPixmap pixmap;
451                if (data.size() > 0) {
452                        pixmap.loadFromData((uchar *)data.c_str(), data.size());
453                        _ui.chatHistory->setAvatarPixmap(QString::fromUtf8(contactId.c_str()), pixmap);
454                }
455        }
456}
457
458void QtChatWidget::updateUserAvatar() {
459
460        QPixmap pixmap;
461        UserProfile& userProfile = _cChatHandler.getCUserProfile().getUserProfile();
462        std::string myData = userProfile.getIcon().getData();
463        pixmap.loadFromData((uchar *)myData.c_str(), myData.size());
464
465        _ui.chatHistory->setAvatarPixmap("self", pixmap);
466        //_ui.avatarFrame->setUserPixmap(pixmap);
467}
468
469void QtChatWidget::sendFileToSession(const QString & filename) {
470        Config & config = ConfigManager::getInstance().getCurrentConfig();
471
472        if (!canDoFileTransfer()) {
473
474                QtContactList * qtContactList = _qtQuteCom->getQtContactList();
475                CContactList & cContactList = qtContactList->getCContactList();
476                ContactProfile contactProfile = cContactList.getContactProfile(std::string(_contactId.toUtf8()));
477
478                if (contactProfile.getFirstQuteComId().empty()) {
479
480                        QString myMess = tr("Your file can not be sent: your contact must be connected on the @company@ network. ");
481                        //myMess += tr("An automatic message has been sent to him");
482                        addStatusMessage(myMess);
483
484                        QString hisMess = "<i>";
485                        QString url = QString::fromUtf8(config.getCompanyWebSiteUrl().c_str());
486                        hisMess += tr("Your contact wishes to send a file with @company@. ");
487                        hisMess += tr("Go to %1 to install it").arg(url);
488                        hisMess += "</i>";
489                        _imChatSession->sendMessage(std::string(hisMess.toUtf8()));
490                }
491                return;
492        }
493
494        QtContactList * qtContactList = _qtQuteCom->getQtContactList();
495        CContactList & cContactList = qtContactList->getCContactList();
496        QtFileTransfer * qtFileTransfer = _qtQuteCom->getFileTransfer();
497        if (qtFileTransfer) {
498                qtFileTransfer->createSendFileSession(_imChatSession->getIMContactSet(), filename, cContactList);
499        }
500}
501
502void QtChatWidget::saveHistoryAsHtml() {
503        _ui.chatHistory->saveHistoryAsHtml();
504}
505
506void QtChatWidget::setContactConnected(bool connected) {
507        QtContactList * qtContactList = _qtQuteCom->getQtContactList();
508        CContactList & cContactList = qtContactList->getCContactList();
509        ContactProfile profile = cContactList.getContactProfile(std::string(_contactId.toUtf8()));
510
511        QString contactName;
512        if (!profile.getShortDisplayName().empty()) {
513                contactName = QString::fromUtf8(profile.getShortDisplayName().c_str());
514        } else {
515                contactName = QString::fromUtf8(profile.getDisplayName().c_str());
516        }
517
518        if (connected && !_isContactConnected) {
519                addStatusMessage(QString(tr("%1 is connected.")).arg(contactName));
520        } else if (!connected && _isContactConnected) {
521                addStatusMessage(QString(tr("%1 is disconnected.")).arg(contactName));
522        }
523
524        _isContactConnected = connected;
525}
526
527void QtChatWidget::contactChangedSlot(QString contactId) {
528
529        /*QtContactList * qtContactList = _qtQuteCom->getQtContactList();
530        CContactList & cContactList = qtContactList->getCContactList();
531        ContactProfile profile = cContactList.getContactProfile(contactId.toUtf8());
532        std::string data = profile.getIcon().getData();
533        QPixmap pixmap;
534        pixmap.loadFromData((uchar *)data.c_str(), data.size());
535        _ui.avatarFrame->updateContact(contactId, pixmap, QString::fromUtf8(profile.getDisplayName()));*/
536}
537
538bool QtChatWidget::canDoFileTransfer() {
539        QtContactList * qtContactList = _qtQuteCom->getQtContactList();
540        CContactList & cContactList = qtContactList->getCContactList();
541        ContactProfile contactProfile = cContactList.getContactProfile(std::string(_contactId.toUtf8()));
542
543        if (!contactProfile.getFirstQuteComId().empty() && contactProfile.isAvailable()) {
544                IMContact imContact = contactProfile.getFirstAvailableQuteComIMContact();
545                if ( (imContact.getPresenceState() != EnumPresenceState::PresenceStateOffline) &&
546                                (imContact.getPresenceState() != EnumPresenceState::PresenceStateUnknown) &&
547                                (imContact.getPresenceState() != EnumPresenceState::PresenceStateUnavailable)) {
548       
549                                return true;
550                }
551        }
552        return false;
553}
554
555/**
556 * Helper method to clone a key event, so that it can be send to another widget
557 */
558static QKeyEvent* cloneKeyEvent(QKeyEvent* event) {
559        return new QKeyEvent(event->type(), event->key(), event->modifiers(), event->text());
560}
561
562bool QtChatWidget::eventFilter(QObject* object, QEvent* event) {
563        if (object == _ui.chatHistory && event->type() == QEvent::KeyPress) {
564                return historyKeyPressEventFilter(static_cast<QKeyEvent*>(event));
565        }
566        if (object == _ui.chatEdit && event->type() == QEvent::KeyPress) {
567                return editKeyPressEventFilter(static_cast<QKeyEvent*>(event));
568        }
569        return false;
570}
571
572bool QtChatWidget::historyKeyPressEventFilter(QKeyEvent* event) {
573        // Set focus on edit widget if the user types a "printable" character
574        if (event->text().size() > 0 && event->text().at(0).isPrint() && _ui.chatEdit->isEnabled()) {
575                _ui.chatEdit->setFocus();
576                QKeyEvent* newEvent = cloneKeyEvent(event);
577                QApplication::postEvent(_ui.chatEdit, newEvent);
578                return true;
579        }
580        return false;
581}
582
583bool QtChatWidget::editKeyPressEventFilter(QKeyEvent* event) {
584        int key = event->key();
585       
586        if (key == Qt::Key_PageUp || key == Qt::Key_PageDown) {
587                QKeyEvent* newEvent = cloneKeyEvent(event);
588                QApplication::postEvent(_ui.chatHistory, newEvent);
589                return true;
590        }
591
592        // Send message with Enter key, unless a modifier is pressed
593        if ((key == Qt::Key_Enter || key == Qt::Key_Return) && 
594                (event->modifiers() == Qt::NoModifier || event->modifiers() == Qt::KeypadModifier)) 
595        {
596                sendMessage();
597                return true;
598        }
599
600#ifdef OS_MACOSX
601        static const int REAL_CTRL_MODIFIER = Qt::MetaModifier;
602#else
603        static const int REAL_CTRL_MODIFIER = Qt::ControlModifier;
604#endif
605        bool realCtrlPressed = event->modifiers() & REAL_CTRL_MODIFIER;
606
607        if (key == Qt::Key_Tab && realCtrlPressed) {
608                // We use realCtrlPressed because on MacOS, Qt::ControlModifier
609                // corresponds to the Command key and Command + Tab is used to switch
610                // between applications, (like Alt + Tab on Windows)
611                ctrlTabPressed();
612                return true;
613        }
614
615        return false;
616}
617
618void QtChatWidget::readyToLoadHistory()
619{       
620
621        HistoryMementoCollection * hmc = _cChatHandler.getCUserProfile().getUserProfile().getHistory().getSessionCollection(_imChatSession->getId());
622       
623       
624        if(hmc)
625                for (HistoryMap::iterator it = hmc->begin(); it != hmc->end(); it++) {
626                       
627                        HistoryMemento * memento = it->second;
628
629                        Time mementoTime = memento->getTime();
630                        QTime time = QTime(mementoTime.getHour(), mementoTime.getMinute());
631                       
632                       
633                        QString contactName;
634                       
635
636                        if(memento->getAlias().size())
637                                contactName = QString::fromUtf8(memento->getAlias().c_str());
638                        else
639                        {
640                                contactName = QString::fromUtf8(memento->getPeer().c_str());
641                                contactName.remove("sip:");
642                        }
643
644                       
645                        QtContactList * qtContactList = _qtQuteCom->getQtContactList();
646                        CContactList & cContactList = qtContactList->getCContactList();
647                        QString contactId = QString::fromUtf8(cContactList.findContactThatOwns(std::string(contactName.toUtf8())).c_str());
648                       
649                        _ui.chatHistory->insertMessage(
650                                                                                        contactId,
651                                                                                        contactName,
652                                                                                        QString::fromUtf8(memento->getData().c_str()),
653                                                                                        time
654                                                                                        );
655                }
656                isLoaded = true;
657}
658
659void QtChatWidget::htmlLoadFinished(bool)
660{
661        //WebKit need a little time to load resources ...
662        QTimer::singleShot(500, this, SLOT(readyToLoadHistory()));
663}
Note: See TracBrowser for help on using the repository browser.