forked from MyDuckIsGoingMad/BroChat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qchatmessage.cpp
147 lines (113 loc) · 4.29 KB
/
qchatmessage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <QDebug>
#include <QStringList>
#include "qchatmessage.h"
QChatMessage::QChatMessage( const QString &service, const QString &nickName, const QString &message, const QString &type, QObject *parent )
: QObject( parent )
, service_( service )
, nickName_( nickName )
, message_( message )
, type_( type )
{
}
QChatMessage::~QChatMessage()
{
}
QString QChatMessage::service() const
{
return service_;
}
void QChatMessage::setService( const QString& service )
{
service_ = service;
}
QString QChatMessage::nickName() const
{
return nickName_;
}
void QChatMessage::setNickName( const QString& nickName )
{
nickName_ = nickName;
}
QString QChatMessage::message() const
{
return message_;
}
void QChatMessage::setMessage( const QString &message )
{
message_ = message;
}
QString QChatMessage::type() const
{
return type_;
}
void QChatMessage::setType( const QString &type )
{
type_ = type;
}
QString QChatMessage::insertLinks( const QString& message, bool convertImageLinks )
{
QString convertedMessage = message;
QStringList tokens = convertedMessage.split( QRegExp( "\\s" ) );
QStringList convertedTokens = tokens;
for( int i = 0; i < tokens.size(); ++i )
{
if ( isLink( tokens.at( i ) ) )
{
//TODO: Try repair links
//convertedTokens[ i ] = "<a href=\\\'" + tokens.at( i ).toLower() + "\\\'>link</a>";
if( convertImageLinks && ( tokens.at( i ).right(4).toLower() == ".svg" || tokens.at( i ).right(4).toLower() == ".png" || tokens.at( i ).right( 4 ).toLower() == ".jpg" || tokens.at( i ).right(4).toLower() == ".gif" || tokens.at( i ).right( 5 ).toLower() == ".jpeg" ) )
{
//convertedTokens[ i ] = "<a href=\\\'" + tokens.at( i ) + "\\\'><img src=\\\'" + tokens.at( i ) + "\\\'><></a>";
//convertedTokens[ i ] = "<img src=\\\'" + tokens.at( i ) + "\\\' width=100%></img>";
convertedTokens[ i ] = "<a href=\"" + tokens.at( i ) + "\"><img class=\"picture\" src=\"" + tokens.at( i ) + "\"></img></a>";
}
else
{
convertedTokens[ i ] = "<a href=\"" + tokens.at( i ) + "\"title=\"" + tokens.at( i ) + "\">link</a>";
}
}
}
int startPos = 0;
for( int i = 0; i < tokens.size(); ++i )
{
//convertedMessage.replace( tokens.at( i ), convertedTokens.at( i ) );
int pos = convertedMessage.indexOf( tokens.at( i ), startPos );
convertedMessage.remove( pos, tokens.at( i ).length() );
convertedMessage.insert( pos, convertedTokens.at( i ) );
startPos = pos + convertedTokens.at( i ).length();
}
//qDebug() << convertedMessage;
return convertedMessage;
}
bool QChatMessage::isLink( const QString& link )
{
QRegExp urlRegExp(
"((?:(?:ht|f)tps?)(?:://))"
"(((?:(?:[a-z\\u0430-\\u0451\\d](?:[a-z\\u0430-\\u0451\\d-]*[a-z\\u0430-\\u0451\\d])*)\\.)+(?:[a-z]{2,}|\\u0440\\u0444)"
"|(?:(?:\\d{1,3}\\.){3}\\d{1,3}))"
"(:\\d+)?"
"(/[-a-z\\u0430-\\u0451\\d%_~\\+\\(\\):]*(?:[\\.,][-a-z\\u0430-\\u0451\\d%_~\\+\\(\\):]+)*)*"
"(\\?(?:&|"|'|[&\"'.:;a-z\\u0430-\\u0451\\d%_~\\+=-])*)?"
"(#(?:&|"|'|[*!()/&\"'.:;a-z\\u0430-\\u0451\\d%_~\\+=-])*)?)"
);
bool retValue = urlRegExp.exactMatch( link );
return retValue;
}
QString QChatMessage::replaceEscapeCharecters( const QString &message )
{
QString convertedMessage = message;
convertedMessage.replace( '<', "<" );
convertedMessage.replace( '<', ">" );
//convertedMessage.replace( '\'', "'" );
//convertedMessage.replace( '\\', "\" );
//convertedMessage.replace( '\\', "\\\\" );
return convertedMessage;
}
QString QChatMessage::deleteTags( const QString &message )
{
QString convertedMessage = message;
convertedMessage.remove( QRegExp( "<[^>]*>" ) );
//convertedMessage.remove( QRegExp( "<(/?)a\\a((?:[^>\"\']|\"[^\"]*\"|\'[^\']*\')*)>" ) );
//qDebug() << convertedMessage;
return convertedMessage;
}