summaryrefslogtreecommitdiffstats
path: root/konversation/src/common.cpp
blob: dc5a4ebc37edbd40dff3b31f8a49d5f1c6727d40 (plain)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
*/

/*
  Copyright (C) 2004 Peter Simonsson <psn@linux.se>
  Copyright (C) 2006-2008 Eike Hein <hein@kde.org>
*/

#include "common.h"
#include "konversationapplication.h"

#include <tqcstring.h>
#include <tqstring.h>
#include <tqregexp.h>
#include <tqpixmap.h>
#include <tqbitmap.h>
#include <tqpainter.h>
#include <tdelocale.h>


namespace Konversation
{

    #include "guess_ja.cpp"
    #include "unicode.cpp"

    static TQRegExp colorRegExp("((\003([0-9]|0[0-9]|1[0-5])(,([0-9]|0[0-9]|1[0-5])|)|\017)|\x02|\x09|\x13|\x16|\x1f)");
    static TQRegExp urlPattern("((www\\.(?!\\.)|(fish|irc|(f|sf|ht)tp(|s))://)(\\.?[\\d\\w/,\\':~\\?=;#@\\-\\+\\%\\*\\{\\}\\!\\(\\)]|&)+)|"
        "([-.\\d\\w]+@[-.\\d\\w]{2,}\\.[\\w]{2,})");
    static TQRegExp tdlPattern("(.*)\\.(\\w+),$");

    TQString removeIrcMarkup(const TQString& text)
    {
        TQString escaped = text;
        // Escape text decoration
        escaped.remove(colorRegExp);

        // Remove Mirc's 0x03 characters too, they show up as rectangles
        escaped.remove(TQChar(0x03));

        return escaped;
    }

    TQString tagURLs(const TQString& text, const TQString& fromNick, bool useCustomColor)
    {
        // TQTime timer;
        // timer.start();

        TQString filteredLine = text;
        TQString linkColor = Preferences::color(Preferences::Hyperlink).name();
        TQString link;
        TQString insertText;
        int pos = 0;
        int urlLen = 0;
        TQString href;

        if(useCustomColor)
        {
            link = "<font color=\""+linkColor+"\"><a href=\"#%1\">%2</a></font>";
        }
        else
        {
            link = "<a href=\"#%1\">%2</a>";
        }

        if(filteredLine.contains("#"))
        {
            TQRegExp chanExp("(^|\\s|^\"|\\s\"|,|'|\\(|\\:|!|@|%|\\+)(#[^,\\s;\\)\\:\\/\\(\\<\\>]*[^.,\\s;\\)\\:\\/\\(\"\''\\<\\>])");
            while((pos = chanExp.search(filteredLine, pos)) >= 0)
            {
                href = chanExp.cap(2);
                urlLen = href.length();
                pos += chanExp.cap(1).length();

                // HACK:Use space as a placeholder for \ as TQt tries to be clever and does a replace to / in urls in TQTextEdit
                insertText = link.arg(TQString(href).replace('\\', " "), href);
                filteredLine.replace(pos, urlLen, insertText);
                pos += insertText.length();
            }
        }

        pos = 0;
        urlLen = 0;

        urlPattern.setCaseSensitive(false);
        TQString protocol;

        if(useCustomColor)
        {
            link = "<font color=\"" + linkColor + "\"><u><a href=\"%1%2\">%3</a></u></font>";
        }
        else
        {
            link = "<u><a href=\"%1%2\">%3</a></u>";
        }

        while((pos = urlPattern.search(filteredLine, pos)) >= 0)
        {
            TQString append;

            // check if the matched text is already replaced as a channel
            if ( filteredLine.findRev( "<a", pos ) > filteredLine.findRev( "</a>", pos ) )
            {
                ++pos;
                continue;
            }

            protocol="";
            urlLen = urlPattern.matchedLength();
            href = filteredLine.mid( pos, urlLen );

            // Don't consider trailing comma part of link.
            if (href.right(1) == ",")
            {
                href.truncate(href.length()-1);
                append = ',';
            }

            // Don't consider trailing semicolon part of link.
            if (href.right(1) == ";")
            {
                href.truncate(href.length()-1);
                append = ';';
            }

            // Don't consider trailing closing parenthesis part of link when
            // there's an opening parenthesis preceding the beginning of the
            // URL or there is no opening parenthesis in the URL at all.
            if (href.right(1) == ")" && (filteredLine.mid(pos-1,1) == "(" || !href.contains("(")))
            {
                href.truncate(href.length()-1);
                append.prepend(")");
            }

            // TQt doesn't support (?<=pattern) so we do it here
            if((pos > 0) && filteredLine[pos-1].isLetterOrNumber())
            {
                pos++;
                continue;
            }

            if (urlPattern.cap(1).startsWith("www.", false))
                protocol = "http://";
            else if (urlPattern.cap(1).isEmpty())
                protocol = "mailto:";

            // Use \x0b as a placeholder for & so we can readd them after changing all & in the normal text to &amp;
            // HACK Replace % with \x03 in the url to keep TQt from doing stupid things
            insertText = link.arg(protocol, TQString(href).replace('&', "\x0b").replace('%', "\x03"), href) + append;
            filteredLine.replace(pos, urlLen, insertText);
            pos += insertText.length();
            KonversationApplication::instance()->storeUrl(fromNick, href);
        }

        // Change & to &amp; to prevent html entities to do strange things to the text
        filteredLine.replace('&', "&amp;");
        filteredLine.replace("\x0b", "&");

        // kdDebug() << "Took (msecs) : " << timer.elapsed() << " for " << filteredLine << endl;

        return filteredLine;
    }

    //TODO: there's room for optimization as pahlibar said. (strm)

    // the below two functions were taken from kopeteonlinestatus.cpp.
    TQBitmap overlayMasks( const TQBitmap *under, const TQBitmap *over )
    {
        if ( !under && !over ) return TQBitmap();
        if ( !under ) return *over;
        if ( !over ) return *under;

        TQBitmap result = *under;
        bitBlt( &result, 0, 0, over, 0, 0, over->width(), over->height(), TQt::OrROP );
        return result;
    }

    TQPixmap overlayPixmaps( const TQPixmap &under, const TQPixmap &over )
    {
        if ( over.isNull() ) return under;

        TQImage imResult; imResult=under;
        TQImage imOver; imOver=over;
        TQPixmap result;

        bitBlt(&imResult,0,0,&imOver,0,0,imOver.width(),imOver.height(),0);
        result=imResult;
        return result;
    }

}