File indexing completed on 2024-04-28 05:37:54
0001 /*************************************************************************** 0002 * Copyright (C) 2020 by Renaud Guezennec * 0003 * http://www.rolisteam.org/contact * 0004 * * 0005 * This software is free software; you can redistribute it and/or modify * 0006 * it under the terms of the GNU General Public License as published by * 0007 * the Free Software Foundation; either version 2 of the License, or * 0008 * (at your option) any later version. * 0009 * * 0010 * This program is distributed in the hope that it will be useful, * 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0013 * GNU General Public License for more details. * 0014 * * 0015 * You should have received a copy of the GNU General Public License * 0016 * along with this program; if not, write to the * 0017 * Free Software Foundation, Inc., * 0018 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 0019 ***************************************************************************/ 0020 #include "instantmessaging/textwritercontroller.h" 0021 0022 #include "utils/networkdownloader.h" 0023 #include <QRegularExpression> 0024 0025 static const int MaxHistorySize= 100; 0026 0027 namespace InstantMessaging 0028 { 0029 TextWriterController::TextWriterController(QObject* parent) : QObject(parent) {} 0030 0031 QString TextWriterController::text() const 0032 { 0033 return m_text; 0034 } 0035 0036 bool TextWriterController::diceCommand() const 0037 { 0038 return m_diceCmd; 0039 } 0040 0041 QUrl TextWriterController::imageLink() const 0042 { 0043 return m_imageLink; 0044 } 0045 0046 void TextWriterController::computeText() 0047 { 0048 QUrl url; 0049 static auto reg1= QRegularExpression("((?:https?)://\\S+)"); 0050 static auto reg2= QRegularExpression("((?:www)\\S+)"); 0051 0052 0053 auto text = m_text; 0054 0055 auto matcher= reg1.match(text); 0056 auto matcher2= reg2.match(text); 0057 QString replacePattern; 0058 0059 QRegularExpression usedRE; 0060 0061 if(matcher.hasMatch()) 0062 { 0063 url= QUrl::fromUserInput(matcher.captured(0)); 0064 usedRE= reg1; 0065 replacePattern= "<a href=\"\\1\">\\1</a>"; 0066 } 0067 else if(matcher2.hasMatch()) 0068 { 0069 url= QUrl::fromUserInput(matcher2.captured(0)); 0070 usedRE= reg2; 0071 replacePattern= "<a href=\"http://\\1\">\\1</a>"; 0072 } 0073 0074 if(url.isValid()) 0075 { 0076 setUrl(url); 0077 auto n= new NetworkDownloader(url); 0078 0079 connect(n, &NetworkDownloader::finished, this, 0080 [text, url, usedRE, replacePattern, n, this](const QByteArray& data, bool isImage) 0081 { 0082 Q_UNUSED(data) 0083 auto realText= text; 0084 auto pattern= replacePattern; 0085 if(isImage) 0086 { 0087 m_imageLink= url; 0088 pattern= ""; 0089 } 0090 realText= realText.replace(usedRE, pattern); 0091 0092 setText(realText); 0093 emit textComputed(); 0094 send(); 0095 n->deleteLater(); 0096 }); 0097 0098 n->download(); 0099 } 0100 else 0101 { 0102 emit textComputed(); 0103 send(); 0104 } 0105 } 0106 0107 void TextWriterController::setText(const QString& text) 0108 { 0109 if(m_text == text) 0110 return; 0111 m_text= text; 0112 emit textChanged(m_text); 0113 setDiceCommand(text.startsWith("!")); 0114 } 0115 0116 void TextWriterController::setDiceCommand(bool b) 0117 { 0118 if(m_diceCmd == b) 0119 return; 0120 m_diceCmd= b; 0121 emit diceCommandChanged(m_diceCmd); 0122 } 0123 0124 void TextWriterController::up() 0125 { 0126 if(m_historicPostion - 1 < 0 || m_history.isEmpty()) 0127 return; 0128 0129 setText(m_history[--m_historicPostion]); 0130 } 0131 0132 void TextWriterController::down() 0133 { 0134 if(m_historicPostion + 1 >= m_history.size()) 0135 return; 0136 0137 setText(m_history[++m_historicPostion]); 0138 } 0139 0140 void TextWriterController::send() 0141 { 0142 m_history.append(m_text); 0143 while(m_history.size() > MaxHistorySize) 0144 { 0145 m_history.removeFirst(); 0146 } 0147 m_historicPostion= m_history.size(); 0148 setText(QString()); 0149 setUrl(QUrl()); 0150 } 0151 0152 void TextWriterController::setUrl(const QUrl& url) 0153 { 0154 if(m_imageLink == url) 0155 return; 0156 m_imageLink= url; 0157 emit urlChanged(); 0158 } 0159 0160 } // namespace InstantMessaging