1 /******************************************************************************\
2 * Copyright (c) 2004-2020
3 *
4 * Author(s):
5 * Volker Fischer
6 *
7 ******************************************************************************
8 *
9 * This program is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License as published by the Free Software
11 * Foundation; either version 2 of the License, or (at your option) any later
12 * version.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 *
23 \******************************************************************************/
24
25 #include "chatdlg.h"
26
27 /* Implementation *************************************************************/
CChatDlg(QWidget * parent)28 CChatDlg::CChatDlg ( QWidget* parent ) : CBaseDlg ( parent, Qt::Window ) // use Qt::Window to get min/max window buttons
29 {
30 setupUi ( this );
31
32 // Add help text to controls -----------------------------------------------
33 // chat window
34 txvChatWindow->setWhatsThis ( "<b>" + tr ( "Chat Window" ) + ":</b> " + tr ( "The chat window shows a history of all chat messages." ) );
35
36 txvChatWindow->setAccessibleName ( tr ( "Chat history" ) );
37
38 // input message text
39 edtLocalInputText->setWhatsThis ( "<b>" + tr ( "Input Message Text" ) + ":</b> " +
40 tr ( "Enter the chat message text in the edit box and press enter to send the "
41 "message to the server which distributes the message to all connected "
42 "clients. Your message will then show up in the chat window." ) );
43
44 edtLocalInputText->setAccessibleName ( tr ( "New chat text edit box" ) );
45
46 // clear chat window and edit line
47 txvChatWindow->clear();
48 edtLocalInputText->clear();
49
50 // we do not want to show a cursor in the chat history
51 txvChatWindow->setCursorWidth ( 0 );
52
53 // set a placeholder text to make sure where to type the message in (#384)
54 edtLocalInputText->setPlaceholderText ( tr ( "Type a message here" ) );
55
56 // Menu -------------------------------------------------------------------
57 QMenuBar* pMenu = new QMenuBar ( this );
58 QMenu* pEditMenu = new QMenu ( tr ( "&Edit" ), this );
59
60 pEditMenu->addAction ( tr ( "Cl&ear Chat History" ), this, SLOT ( OnClearChatHistory() ), QKeySequence ( Qt::CTRL + Qt::Key_E ) );
61
62 pMenu->addMenu ( pEditMenu );
63 #if defined( Q_OS_IOS )
64 QAction* action = pMenu->addAction ( tr ( "&Close" ) );
65 connect ( action, SIGNAL ( triggered() ), this, SLOT ( close() ) );
66 #endif
67
68 #if defined( ANDROID ) || defined( Q_OS_ANDROID )
69 pEditMenu->addAction ( tr ( "&Close" ), this, SLOT ( close() ), QKeySequence ( Qt::CTRL + Qt::Key_W ) );
70 #endif
71
72 // Now tell the layout about the menu
73 layout()->setMenuBar ( pMenu );
74
75 // Connections -------------------------------------------------------------
76 QObject::connect ( edtLocalInputText, &QLineEdit::textChanged, this, &CChatDlg::OnLocalInputTextTextChanged );
77
78 QObject::connect ( butSend, &QPushButton::clicked, this, &CChatDlg::OnSendText );
79
80 QObject::connect ( txvChatWindow, &QTextBrowser::anchorClicked, this, &CChatDlg::OnAnchorClicked );
81 }
82
OnLocalInputTextTextChanged(const QString & strNewText)83 void CChatDlg::OnLocalInputTextTextChanged ( const QString& strNewText )
84 {
85 // check and correct length
86 if ( strNewText.length() > MAX_LEN_CHAT_TEXT )
87 {
88 // text is too long, update control with shortened text
89 edtLocalInputText->setText ( strNewText.left ( MAX_LEN_CHAT_TEXT ) );
90 }
91 }
92
OnSendText()93 void CChatDlg::OnSendText()
94 {
95 // send new text and clear line afterwards, do not send an empty message
96 if ( !edtLocalInputText->text().isEmpty() )
97 {
98 emit NewLocalInputText ( edtLocalInputText->text() );
99 edtLocalInputText->clear();
100 }
101 }
102
OnClearChatHistory()103 void CChatDlg::OnClearChatHistory()
104 {
105 // clear chat window
106 txvChatWindow->clear();
107 }
108
AddChatText(QString strChatText)109 void CChatDlg::AddChatText ( QString strChatText )
110 {
111 // notify accessibility plugin that text has changed
112 QAccessible::updateAccessibility ( new QAccessibleValueChangeEvent ( txvChatWindow, strChatText ) );
113
114 // analyze strChatText to check if hyperlink (limit ourselves to http(s)://) but do not
115 // replace the hyperlinks if any HTML code for a hyperlink was found (the user has done the HTML
116 // coding hisself and we should not mess with that)
117 if ( !strChatText.contains ( QRegExp ( "href\\s*=|src\\s*=" ) ) )
118 {
119 // searches for all occurrences of http(s) and cuts until a space (\S matches any non-white-space
120 // character and the + means that matches the previous element one or more times.)
121 strChatText.replace ( QRegExp ( "(https?://\\S+)" ), "<a href=\"\\1\">\\1</a>" );
122 }
123
124 // add new text in chat window
125 txvChatWindow->append ( strChatText );
126 }
127
OnAnchorClicked(const QUrl & Url)128 void CChatDlg::OnAnchorClicked ( const QUrl& Url )
129 {
130 // only allow http(s) URLs to be opened in an external browser
131 if ( Url.scheme() == QLatin1String ( "https" ) || Url.scheme() == QLatin1String ( "http" ) )
132 {
133 if ( QMessageBox::question ( this,
134 APP_NAME,
135 tr ( "Do you want to open the link '%1' in your browser?" ).arg ( "<b>" + Url.toString() + "</b>" ),
136 QMessageBox::Yes | QMessageBox::No ) == QMessageBox::Yes )
137 {
138 QDesktopServices::openUrl ( Url );
139 }
140 }
141 }
142