source: qutecom-2.2/wengophone/src/presentation/qt/chat/QtChatHistoryWidget.cpp @ 587:a5603d5e5c5c

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

bug fix : log chat windows

File size: 9.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 "QtChatHistoryWidget.h"
21
22#include <presentation/qt/chat/QtChatTheme.h>
23#include <presentation/qt/chat/QtChatUtils.h>
24
25#include <model/config/Config.h>
26#include <model/config/ConfigManager.h>
27#include <model/profile/AvatarList.h>
28
29#include <control/chat/CChatHandler.h>
30
31#include <imwrapper/Account.h>
32
33#include <util/Logger.h>
34#include <util/WebBrowser.h>
35
36#include <QtCore/QFile>
37#include <QtCore/QFileInfo>
38#include <QtCore/QTime>
39
40#include <QtGui/QFileDialog>
41#include <QtGui/QScrollBar>
42
43// If previous message from last sender is more than MAX_SECONDS_BETWEEN_CONSECUTIVE_MESSAGES
44// old, don't group new message with it.
45static int MAX_SECONDS_BETWEEN_CONSECUTIVE_MESSAGES = 3 * 60;
46
47QtChatHistoryWidget::QtChatHistoryWidget(QWidget * parent) 
48: QWebView(parent) 
49{
50        _translate = true;
51        //connect (this, SIGNAL(anchorClicked(const QUrl &)), this, SLOT(urlClicked(const QUrl &)));
52        setContextMenuPolicy(Qt::NoContextMenu);
53        Config & config = ConfigManager::getInstance().getCurrentConfig();
54
55        _theme = new QtChatTheme();
56       
57        QWebFrame *     frame = page()->mainFrame (); 
58        frame->setScrollBarPolicy(Qt::Horizontal,Qt::ScrollBarAlwaysOff);
59
60        QString path = QString::fromStdString(config.getResourcesDir()) + "pics/avatars/default-avatar.png";
61#if defined(OS_WINDOWS)
62        path = path.replace("\\","/");
63        _avatarMap["defaut"] = path;
64#else
65        _avatarMap["defaut"] = "file://"+path;
66#endif
67
68        page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
69        connect(this,SIGNAL(linkClicked(const QUrl &)),this,SLOT(urlClicked(const QUrl &)));
70       
71        //load(QUrl("/Users/laurent/Desktop/qutecom-2.2/debug/debug/QuteCom.app/Contents/Resources/chat/base.html"));
72}
73
74QtChatHistoryWidget::~QtChatHistoryWidget() {
75        delete _theme;
76}
77
78void QtChatHistoryWidget::setTheme(const QString& themeDir,const QString & variant) {
79        _theme->load(themeDir);
80        Config & config = ConfigManager::getInstance().getCurrentConfig();
81       
82        QString Resources_path;
83       
84#if defined(OS_WINDOWS)
85        Resources_path = _theme->getCurrentThemePath();
86#else
87        Resources_path = "file://"+_theme->getCurrentThemePath();
88#endif
89
90        Resources_path += "Contents/Resources/";
91       
92        page()->mainFrame()->addToJavaScriptWindowObject("parent",this);
93        QFile file_base(QString::fromStdString(config.getResourcesDir())+"chat/base.html");
94        file_base.open(QIODevice::ReadOnly);
95       
96        QString base = file_base.readAll();
97       
98        if (variant.isEmpty())
99                base.replace("%cssPath%","");
100        else 
101                base.replace("%cssPath%","Variants/"+variant+".css");
102
103        setHtml(base,QUrl(Resources_path));
104}
105
106void QtChatHistoryWidget::saveHistoryAsHtml() {
107        Config & config = ConfigManager::getInstance().getCurrentConfig();
108        QString contentToSave =_data;
109
110        QString filePath = QFileDialog::getSaveFileName(this, tr("Save As"), 
111                QString::fromStdString(config.getLastChatHistorySaveDir()), "HTML (*.htm *.html)");
112
113        if(filePath.length() > 0) {
114
115                if(!filePath.endsWith(QString(".htm"), Qt::CaseInsensitive) && 
116                        !filePath.endsWith(QString(".html"), Qt::CaseInsensitive)) {
117
118                        filePath.append(QString(".html"));
119                }
120
121                QFile fileToSave(filePath);
122                config.set(Config::LAST_CHAT_HISTORY_SAVE_DIR_KEY, QFileInfo(fileToSave).absolutePath().toStdString());
123                fileToSave.open(QIODevice::WriteOnly);
124                fileToSave.write(contentToSave.toStdString().c_str(), (long long)contentToSave.length());
125                fileToSave.close();
126        }
127}
128
129void QtChatHistoryWidget::urlClicked(const QUrl & link) {
130        //setSource(QUrl(QString::null));
131        QString url = link.toString();
132        if(url.contains(_theme->getCurrentThemePath()))
133                url = url.remove(_theme->getCurrentThemePath());
134
135        WebBrowser::openUrl(url.toStdString());
136}
137
138void QtChatHistoryWidget::insertMessage(const QString & contactId, const QString & senderName, const QString & message, const QTime & time) {
139        if (contactId == "self" || !_translate)
140                insertMessage2(contactId,senderName,message,time);
141        else
142        {
143                QString text = message;
144                text = text.replace("\"","\\\"");
145                text = text.replace("\'","\\\'");
146               
147                Config & config = ConfigManager::getInstance().getCurrentConfig();
148                QString lang = QString::fromStdString(config.getTranslationReceived());
149                QString script = QString("translate(\"%1\",\"%2\",\"%3\",\"%4\",\"%5\")").arg(contactId).arg(senderName).arg(text).arg(time.toString()).arg(lang);
150                page ()->currentFrame ()->evaluateJavaScript(script);
151        }
152}
153
154void QtChatHistoryWidget::insertMessage2(const QString & contactId, const QString & senderName, const QString & message, const QTime & time) {
155        // Prepare message
156        QString htmlMessage = QtChatUtils::decodeMessage(_protocol, message);
157       
158
159        // Get html
160        QtChatTheme::Position position;
161        int timeDelta = _lastMessageTime.secsTo(time);
162        if (_lastSenderName == senderName && timeDelta < MAX_SECONDS_BETWEEN_CONSECUTIVE_MESSAGES) 
163        {
164                position = QtChatTheme::Next;
165        } 
166        else 
167        {
168                if (!_lastSenderName.isEmpty()) 
169                {
170                        QString footer =  _theme->getFooter();
171                        footer = footer.replace("\n","");
172                        footer = footer.replace("\r","");
173                        footer = footer.replace("\t","");
174                        footer = footer.replace("\"","\\\"");
175                        footer = footer.replace("\'","\\\'");
176                        page ()->currentFrame ()->evaluateJavaScript("insertMessage(\"\")");
177                        page ()->currentFrame ()->evaluateJavaScript("insertMessage(\""+footer+"\")");
178                }
179                position = QtChatTheme::First;
180                _lastSenderName = senderName;
181        }
182        _lastMessageTime = time;
183
184        QtChatTheme::Direction direction;
185
186        if (contactId == "self") { // FIXME: do not duplicate "self"
187                direction = QtChatTheme::Outgoing;
188        } else {
189                direction = QtChatTheme::Incoming;
190               
191        }
192
193        QString html = _theme->getContent(direction, position);
194        QtChatTheme::setKeywordValue(html, "senderId", contactId);
195        QtChatTheme::setKeywordValue(html, "sender", senderName);
196        QtChatTheme::setKeywordValue(html, "message", "<div id='magic_id'>"+htmlMessage+"</div>");
197
198        QRegExp reg("\\%(time|shortTime)(\\%|\\{.*\\}\\%)");
199        html = html.replace(reg,time.toString("HH:mm"));
200
201        QRegExp reg2("\\%timeOpened(\\%|\\{.*\\}\\%)");
202        html = html.replace(reg2,QTime::currentTime().toString("HH:mm"));
203
204        QtChatTheme::setKeywordValue(html, "service", "");
205
206        QString avatar_path = _avatarMap["defaut"];
207        if(_avatarMap.contains(contactId))
208                avatar_path = _avatarMap[contactId];
209        QtChatTheme::setKeywordValue(html, "userIconPath", avatar_path);
210
211        //updateHtml();
212
213        html = html.replace("\n","");
214        html = html.replace("\r","");
215        html = html.replace("\t","");
216        html = html.replace("\"","\\\"");
217        html = html.replace("\'","\\\'");
218       
219        if(position == QtChatTheme::First)
220                page ()->currentFrame ()->evaluateJavaScript("insertMessage(\"\")");
221       
222        page ()->currentFrame ()->evaluateJavaScript("insertMessage(\""+html+"\")");
223       
224        //delay for QWebView to update itself
225        QTimer::singleShot(200,this,SLOT(scrollToBottom()));
226}
227
228void QtChatHistoryWidget::translationFinishedSlot(const QVariant  & traduction, const QVariant  & contactId, const QVariant  & senderName, const QVariant  & message, const QVariant  & time)
229{
230        QString tmp = traduction.toString().replace("&#39;","'");
231        if(tmp.isEmpty())
232        {
233                insertMessage2(contactId.toString(),senderName.toString(),message.toString(),time.toTime());
234        }
235        else
236        {
237               
238                QTextDocument doc;
239                doc.setHtml(message.toString());
240               
241                QString str = "<div>"+tmp+"</div><div style=\"color:grey\">"+doc.toPlainText()+"</div>";
242                insertMessage2(contactId.toString(),senderName.toString(),str,time.toTime());
243        }
244}
245
246void QtChatHistoryWidget::insertStatusMessage(const QString & message, const QTime& time) {
247        // Make sure we close any opened message group
248        if (!_lastSenderName.isEmpty()) {
249                QString footer = _theme->getFooter();
250                page ()->currentFrame ()->evaluateJavaScript("insertMessage(\"\")");
251                page ()->currentFrame ()->evaluateJavaScript("insertMessage(\""+footer+"\")");
252                _lastSenderName = "";
253        }
254        QString html = _theme->getStatusMessage();
255        QtChatTheme::setKeywordValue(html, "message", message);
256        QtChatTheme::setKeywordValue(html, "time", time.toString("HH:mm"));
257       
258        html = html.replace("\n","");
259        html = html.replace("\r","");
260        html = html.replace("\t","");
261        html = html.replace("\"","\\\"");
262        html = html.replace("\'","\\\'");
263        page ()->currentFrame ()->evaluateJavaScript("insertMessage(\"\")");
264        page ()->currentFrame ()->evaluateJavaScript("insertMessage(\""+html+"\")");
265       
266        //_data += html;
267       
268}
269
270void QtChatHistoryWidget::setAvatarPixmap(const QString& name, const QPixmap& pixmap) 
271{
272        QDir dir(QString::fromStdString(Config::getConfigDir()));
273       
274        if(!dir.exists("cache"))
275                dir.mkdir("cache");
276        dir.cd("cache");
277       
278        //QString path = dir.absolutePath()+QDir::separator()+name+".png";
279        QString path = dir.absolutePath()+"/"+name+".png";
280
281        pixmap.save(path,"PNG");
282       
283#if defined(OS_WINDOWS)
284        _avatarMap[name] = path;
285#else
286        _avatarMap[name] = "file://"+path;
287#endif
288}
289
290void QtChatHistoryWidget::scrollToBottom() {
291       
292        QWebFrame *     frame = page()->mainFrame ();
293        frame->setScrollBarValue(Qt::Vertical,frame->scrollBarMaximum(Qt::Vertical));
294}
295
296void QtChatHistoryWidget::clear() 
297{
298        _data  = "";
299        _lastSenderName = "";
300}
301
302
303void QtChatHistoryWidget::updateHtml()
304{
305#if defined(OS_WINDOWS)
306        setHtml(_data,QUrl(_theme->getCurrentThemePath()));
307#else
308        setHtml(_data,QUrl("file://"+_theme->getCurrentThemePath()));
309#endif
310}
Note: See TracBrowser for help on using the repository browser.