source: qutecom-2.2/wengophone/src/presentation/qt/chat/QtChatLogViewer.cpp @ 512:ba5700ffd4f8

Last change on this file since 512:ba5700ffd4f8 was 512:ba5700ffd4f8, checked in by laurent@…, 3 years ago

bug fix : if/ele

File size: 14.7 KB
Line 
1/*
2 * WengoPhone, a voice over Internet phone
3 * Copyright (C) 2004-2007  Wengo
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 "QtChatLogViewer.h"
21#include "ui_ChatLogViewer.h"
22
23#include <model/config/Config.h>
24#include <model/config/ConfigManager.h>
25#include <model/contactlist/ContactProfile.h>
26#include <model/profile/UserProfile.h>
27#include <model/profile/UserProfileHandler.h>
28
29#include <control/CWengoPhone.h>
30#include <control/profile/CUserProfile.h>
31#include <control/profile/CUserProfileHandler.h>
32
33#include <presentation/qt/QtWengoPhone.h>
34#include <presentation/qt/QtNoWengoAlert.h>
35#include <presentation/qt/QtToolBar.h>
36#include <presentation/qt/contactlist/QtContactList.h>
37#include <presentation/qt/contactlist/QtContactListManager.h>
38#include <presentation/qt/filetransfer/QtFileTransfer.h>
39#include <presentation/qt/profile/QtProfileDetails.h>
40#include <presentation/qt/webservices/sms/QtSms.h>
41
42#include <imwrapper/IMContact.h>
43#include <imwrapper/IMContactSet.h>
44
45#include <cutil/global.h>
46#include <qtutil/Object.h>
47#include <qtutil/SafeConnect.h>
48#include <qtutil/StringListConvert.h>
49#include <util/Logger.h>
50#include <util/SafeDelete.h>
51#include <util/StringList.h>
52
53#include <QtCore/QDate>
54#include <QtCore/QDir>
55#include <QtCore/QFileInfo>
56#include <QtCore/QLocale>
57#include <QtCore/QTime>
58
59#include <QtGui/QDialog>
60
61QtChatLogViewer::QtChatLogViewer(QWidget * parent, QtWengoPhone & qtWengoPhone, QString log)
62        : QMainWindow(parent),
63        _qtWengoPhone(qtWengoPhone) {
64
65        _ui = new Ui::ChatLogViewer();
66        _ui->setupUi(this);
67        setupMenuBarActions();
68        setupToolBarActions();
69
70        QtContactList * qtContactList = _qtWengoPhone.getQtContactList();
71        CContactList & cContactList = qtContactList->getCContactList();
72
73        UserProfile * userProfile = _qtWengoPhone.getCWengoPhone().getCUserProfileHandler().getUserProfileHandler().getCurrentUserProfile();
74        if (userProfile) {
75                // Setup user avatar
76                QPixmap pixmap;
77                std::string myData = userProfile->getIcon().getData();
78                pixmap.loadFromData((uchar *)myData.c_str(), myData.size());
79                _ui->historyLog->setAvatarPixmap("self", pixmap);
80       
81                // Read history
82                std::string userlogin;
83                HistoryMementoCollection hmc;
84                StringList cuuidList;
85                userProfile->getHistory().loadChatLog(log.toStdString(), &hmc, &userlogin, &cuuidList);
86                _cuuidList = StringListConvert::toQStringList(cuuidList);
87               
88                bool isWengoAccountConnected = userProfile->hasWengoAccount();
89
90                QString selfContactName = QString::fromUtf8(userlogin.c_str());
91
92                // Set remote user avatars
93                Q_FOREACH(QString uuid, _cuuidList) {
94                        ContactProfile profile = cContactList.getContactProfile(uuid.toStdString());
95
96                        std::string data = profile.getIcon().getData();
97                        if (data.size() != 0) {
98                                QPixmap pixmap;
99                                pixmap.loadFromData((uchar *)data.c_str(), data.size());
100                                _ui->historyLog->setAvatarPixmap(uuid, pixmap);
101                        }
102                }
103               
104                // parses HistoryMementoCollection to find messages and inserts them in historyLog
105                _ui->historyLog->setProtocol (EnumIMProtocol::IMProtocolAll);
106                Config & config = ConfigManager::getInstance().getCurrentConfig();
107                QString wengoSuffix = "@" + QString::fromStdString( config.getWengoRealm() );
108                QDate previousDate;
109                for (HistoryMap::iterator it = hmc.begin(); it != hmc.end(); it++) {
110               
111                        HistoryMemento * memento = it->second;
112
113                        // Read date and time
114                        Date mementoDate = memento->getDate();
115                        QDate date(mementoDate.getYear(), mementoDate.getMonth(), mementoDate.getDay());
116                        Time mementoTime = memento->getTime();
117                        QTime time = QTime(mementoTime.getHour(), mementoTime.getMinute());
118
119                        // Insert date line if necessary
120                        if (date != previousDate) {
121                                QString txt = QLocale::system().toString(date);
122                                _ui->historyLog->insertStatusMessage(txt, time);
123                                previousDate = date;
124                        }
125                       
126                        // Insert message
127                        QString contactName = QString::fromUtf8(memento->getPeer().c_str());
128                        if (isWengoAccountConnected) {
129                                contactName.remove(wengoSuffix);
130                        }
131                        contactName.remove("sip:");
132                       
133                        QString contactId;
134                        if (contactName == selfContactName) {
135                                contactId = "self";
136                        } else {
137                                contactId = _cuuidList[0];
138                                // FIXME: History format is broken: it does not store the uuid
139                                // associated with a peer. Using the first uid will work for
140                                // all one-to-one chat, but will fail for chat conference.
141                        }
142
143                        _ui->historyLog->insertMessage(
144                                contactId,
145                                contactName,
146                                QString::fromUtf8(memento->getData().c_str()),
147                                time
148                        );
149                }
150        }
151        ////
152       
153#if defined(OS_MACOSX)
154        setUnifiedTitleAndToolBarOnMac(true);
155#endif
156       
157        SAFE_CONNECT(qtWengoPhone.getQtContactList(), SIGNAL(contactChangedEventSignal(QString )), SLOT(contactChangedEventSlot(QString)));
158       
159        updateToolBarActions();
160}
161
162QtChatLogViewer::~QtChatLogViewer() {
163        OWSAFE_DELETE(_ui);
164}
165
166void QtChatLogViewer::copyQAction(QObject * actionParent, QAction * action) {
167        QAction * tmp = actionParent->findChild<QAction *>(action->objectName());
168        if (!tmp) {
169                LOG_FATAL("QAction is null, cannot copy it");
170        }
171        action->setIcon(tmp->icon());
172        action->setIconText(tmp->iconText());
173        action->setText(tmp->text());
174        action->setToolTip(tmp->toolTip());
175        action->setShortcut(tmp->shortcut());
176}
177
178void QtChatLogViewer::setupMenuBarActions() {
179        QtToolBar * qtToolBar = &_qtWengoPhone.getQtToolBar();
180        QWidget * toolBar = qtToolBar->getWidget();
181
182        // setup "Wengo" menu
183        copyQAction(toolBar, _ui->actionShowWengoAccount);
184        SAFE_CONNECT_RECEIVER(_ui->actionShowWengoAccount, SIGNAL(triggered()), qtToolBar, SLOT(showWengoAccount()));
185        copyQAction(toolBar, _ui->actionEditMyProfile);
186        SAFE_CONNECT_RECEIVER(_ui->actionEditMyProfile, SIGNAL(triggered()), qtToolBar, SLOT(editMyProfile()));
187        copyQAction(toolBar, _ui->actionWengoServices);
188        SAFE_CONNECT_RECEIVER(_ui->actionWengoServices, SIGNAL(triggered()), qtToolBar, SLOT(showWengoServices()));
189        copyQAction(toolBar, _ui->actionClose);
190        SAFE_CONNECT(_ui->actionClose, SIGNAL(triggered()), SLOT(close()));
191        ////
192
193        // setup "contact" menubar
194        copyQAction(toolBar, _ui->actionAddContact);
195        SAFE_CONNECT_RECEIVER(_ui->actionAddContact, SIGNAL(triggered()), qtToolBar, SLOT(addContact()));
196        copyQAction(toolBar, _ui->actionSearchWengoContact);
197        SAFE_CONNECT_RECEIVER(_ui->actionSearchWengoContact, SIGNAL(triggered()), qtToolBar, SLOT(searchWengoContact()));
198        ////
199
200        // setup "actions" menu
201        copyQAction(toolBar, _ui->actionCreateConferenceCall);
202        SAFE_CONNECT_RECEIVER(_ui->actionCreateConferenceCall, SIGNAL(triggered()), qtToolBar, SLOT(createConferenceCall()));
203
204#if defined (DISABLE_SMS)
205        _ui->actionSendSms->setVisible(false);
206#else
207        copyQAction(toolBar, _ui->actionSendSms);
208        SAFE_CONNECT_RECEIVER(_ui->actionSendSms, SIGNAL(triggered()), qtToolBar, SLOT(sendSms()));
209#endif
210       
211        SAFE_CONNECT(_ui->actionSaveHistoryAs, SIGNAL(triggered()), SLOT(saveChatHistory()));
212        SAFE_CONNECT(_ui->actionActionRestartChat, SIGNAL(triggered()), SLOT(restartChat()));
213        ////
214
215#if defined (DISABLE_FILETRANSFER)
216        _ui->actionSendFile->setVisible(false);
217        _ui->actionShowFileTransfer->setVisible(false);
218#else
219        // setup "tools" menu
220        copyQAction(toolBar, _ui->actionShowFileTransfer);
221        SAFE_CONNECT_RECEIVER(_ui->actionShowFileTransfer, SIGNAL(triggered()), qtToolBar, SLOT(showFileTransferWindow()));
222        ////
223#endif
224
225        // setup "help" menu
226        copyQAction(toolBar, _ui->actionShowWengoForum);
227        SAFE_CONNECT_RECEIVER(_ui->actionShowWengoForum, SIGNAL(triggered()), qtToolBar, SLOT(showWengoForum()));
228        copyQAction(toolBar, _ui->actionWengoFAQ);
229        SAFE_CONNECT_RECEIVER(_ui->actionWengoFAQ, SIGNAL(triggered()), qtToolBar, SLOT(showWengoFAQ()));
230        ////
231}
232
233void QtChatLogViewer::setupToolBarActions() {
234        SAFE_CONNECT(_ui->actionCallContact, SIGNAL(triggered()), SLOT(callContact()));
235        SAFE_CONNECT(_ui->actionSendSms, SIGNAL(triggered()), SLOT(sendSmsToContact()));
236        SAFE_CONNECT(_ui->actionSendFile, SIGNAL(triggered()), SLOT(sendFileToContact()));
237        SAFE_CONNECT(_ui->actionCreateChatConf, SIGNAL(triggered()), SLOT(createChatConference()));
238        SAFE_CONNECT(_ui->actionContactInfo, SIGNAL(triggered()), SLOT(showContactInfo()));
239}
240
241bool QtChatLogViewer::canDoFileTransfer(const ContactProfile & contactProfile) {
242        if (!contactProfile.getFirstWengoId().empty() && contactProfile.isAvailable()) {
243                IMContact imContact = contactProfile.getFirstAvailableWengoIMContact();
244                if ( (imContact.getPresenceState() != EnumPresenceState::PresenceStateOffline) &&
245                                (imContact.getPresenceState() != EnumPresenceState::PresenceStateUnknown) &&
246                                (imContact.getPresenceState() != EnumPresenceState::PresenceStateUnavailable)) {
247                                return true;
248                }
249        }
250        return false;
251}
252
253void QtChatLogViewer::updateToolBarActions() {
254
255        QtContactList * qtContactList = _qtWengoPhone.getQtContactList();
256        ContactProfile contactProfile;
257       
258        //call'n chat
259        if (_cuuidList.size() <= 0) {
260                //no contact to call
261                _ui->actionCallContact->setEnabled(false);
262                _ui->actionActionRestartChat->setEnabled(false);
263        } else  if (_cuuidList.size() > 1) {
264                //TO DO
265                _ui->actionCallContact->setEnabled(false);
266                _ui->actionActionRestartChat->setEnabled(false);
267        } else {
268                std::string cuuid = _cuuidList[0].toStdString();
269                contactProfile = qtContactList->getCContactList().getContactProfile(cuuid);
270                _ui->actionCallContact->setEnabled(contactProfile.hasCall() && contactProfile.isAvailable());
271               
272                _ui->actionActionRestartChat->setEnabled(contactProfile.hasIM());
273        }
274        ////
275       
276        // depending on sip account type ( SMS, File Transfert )
277        bool sendSMS = false;
278        bool sendFile = false;
279        UserProfile * userProfile = _qtWengoPhone.getCWengoPhone().getCUserProfileHandler().getUserProfileHandler().getCurrentUserProfile();
280        if (userProfile->hasWengoAccount()) {
281                for (QStringList::const_iterator it = _cuuidList.begin(); it != _cuuidList.end(); it++) {
282                        contactProfile = qtContactList->getCContactList().getContactProfile(it->toStdString());
283                        sendSMS |= !contactProfile.getMobilePhone().empty();
284                        sendFile |= canDoFileTransfer(contactProfile);
285                }
286        } else if (userProfile->hasSipAccount()) {
287                sendSMS = true; // a pop up
288                //sendFile = false; //useless
289        }
290        _ui->actionSendSms->setEnabled(sendSMS);
291        _ui->actionSendFile->setEnabled(sendFile);
292        ////
293
294        if (_cuuidList.size() <= 0) {
295                _ui->actionContactInfo->setEnabled(false);
296                _ui->actionCreateChatConf->setEnabled(false);
297        } else {
298                _ui->actionContactInfo->setEnabled(true);
299                _ui->actionCreateChatConf->setEnabled(false);
300        }
301
302}
303
304void QtChatLogViewer::sendSmsToContact() {
305        QtContactList * qtContactList = _qtWengoPhone.getQtContactList();
306        ContactProfile contactProfile;
307        UserProfile * userProfile = _qtWengoPhone.getCWengoPhone().getCUserProfileHandler().getUserProfileHandler().getCurrentUserProfile();
308       
309        if (userProfile->hasWengoAccount()) {
310                QtSms * sms = _qtWengoPhone.getQtSms();
311                if (sms) {
312                        sms->clear();
313                        for (QStringList::const_iterator it = _cuuidList.begin(); it != _cuuidList.end(); it++) {
314                                contactProfile = qtContactList->getCContactList().getContactProfile(it->toStdString());
315                                sms->addPhoneNumber(QString::fromStdString(contactProfile.getMobilePhone()));
316                        }
317                        sms->getWidget()->show();
318                }
319        } else {
320                QtNoWengoAlert alert(this, _qtWengoPhone);
321                alert.getQDialog()->exec();
322        }
323}
324
325void QtChatLogViewer::sendFileToContact() {
326
327        QtFileTransfer * qtFileTransfer = _qtWengoPhone.getFileTransfer();
328        if (!qtFileTransfer) {
329                return;
330        }
331
332        // gets a filename
333        QString filename = qtFileTransfer->getChosenFile();
334        if (filename.isEmpty()) {
335                return;
336        }
337        QFileInfo fileInfo(filename);
338        Config & config = ConfigManager::getInstance().getCurrentConfig();
339        config.set(Config::FILETRANSFER_LASTUPLOADEDFILE_FOLDER_KEY, fileInfo.dir().absolutePath().toStdString());
340        ////
341       
342        // sends this file to all available contacts
343        QtContactList * qtContactList = _qtWengoPhone.getQtContactList();
344        CContactList & cContactList = qtContactList->getCContactList();
345        IMContactSet contactSet;
346        ContactProfile contactProfile;
347       
348        for (QStringList::const_iterator it = _cuuidList.begin(); it != _cuuidList.end(); it++) {
349                contactProfile = qtContactList->getCContactList().getContactProfile(it->toStdString());
350               
351                if (contactProfile.hasAvailableWengoId()) {
352                        contactSet.insert(contactProfile.getFirstAvailableWengoIMContact());
353                }
354        }
355        qtFileTransfer->createSendFileSession(contactSet, filename, cContactList);
356        ////
357}
358
359void QtChatLogViewer::callContact() {
360        if ((_cuuidList.size() > 0) && (!_cuuidList[0].isEmpty())) {
361                QtContactListManager::getInstance()->startCall(_cuuidList[0]);
362                if (_qtWengoPhone.getWidget()->isMinimized()) {
363                        _qtWengoPhone.getWidget()->showNormal();
364                }
365        }
366}
367
368void QtChatLogViewer::showContactInfo() {
369
370        QtContactList * qtContactList = _qtWengoPhone.getQtContactList();
371        CContactList & cContactList = qtContactList->getCContactList();
372        ContactProfile contactProfile;
373
374        for (QStringList::const_iterator it = _cuuidList.begin(); it != _cuuidList.end(); it++) {
375                contactProfile = cContactList.getContactProfile(it->toStdString());
376
377                QtProfileDetails qtProfileDetails(
378                                *_qtWengoPhone.getCWengoPhone().getCUserProfileHandler().getCUserProfile(),
379                contactProfile, this, tr("Edit Contact"));
380                if (qtProfileDetails.show()) {
381                        _qtWengoPhone.getCWengoPhone().getCUserProfileHandler().getCUserProfile()->getCContactList().updateContact(contactProfile);
382                }
383        }
384}
385
386void QtChatLogViewer::saveChatHistory() {
387        _ui->historyLog->saveHistoryAsHtml();
388}
389
390void QtChatLogViewer::restartChat() {
391
392        // starts a "normal" chat
393        if (_cuuidList.size() > 0) {
394                _qtWengoPhone.getCWengoPhone().getCUserProfileHandler().getCUserProfile()->startIM(_cuuidList[0].toStdString());
395        }
396        ////
397       
398        //// TO DO allow to start a multi-chat
399        // launches a confchat
400        //if (_cuuidList.size() > 1) {
401        //      for (QStringList::const_iterator it = _cuuidList.begin(); it != _cuuidList.end(); it++) {
402        //             
403        //      }
404        //}
405        //////
406}
407
408void QtChatLogViewer::createChatConference() {
409
410        //// TO DO allow to start a multi-chat
411
412        //QtChatWidget * widget = getActiveTabWidget();
413        //if (widget) {
414        //      widget->showInviteDialog();
415        //}
416       
417        //if (canDoMultiChat()) {
418        //      QtChatRoomInviteDlg dlg(*_imChatSession,
419        //              _cChatHandler.getCUserProfile().getCContactList(), this);
420        //      dlg.exec();
421        //}
422}
423
424void QtChatLogViewer::contactChangedEventSlot(QString contactid) {
425
426        bool updateNeeded = false;
427
428        for (QStringList::const_iterator it = _cuuidList.begin(); it != _cuuidList.end(); it++) {
429                if ((*it) == contactid) {
430                                updateNeeded = true;
431                } 
432        }
433       
434        if (updateNeeded && isVisible()) {
435                updateToolBarActions();
436        }
437}
Note: See TracBrowser for help on using the repository browser.