summaryrefslogtreecommitdiffstats
path: root/ksirc/ksview.cpp
blob: 85d0fd1fe876a0fad25f0e74551a513d053dc760 (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/* This file is part of ksirc
   Copyright (c) 2001 Malte Starostik <malte@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

// $Id$

#include <tqclipboard.h>
#include <tqdatetime.h>
#include <tqregexp.h>
#include <tqdragobject.h>
#include <tqvaluestack.h>
#include <tqstylesheet.h>

#include <kapplication.h>
#include <kdebug.h>
#include <klocale.h>
#include <krun.h>
#include <kpopupmenu.h>
#include <kstringhandler.h>
#include <knotifyclient.h>
#include <kfiledialog.h>
#include <kio/job.h>
#include <kurldrag.h>

#include "ksopts.h"
#include "ksview.moc"
#include "ksparser.h"
#include "nickColourMaker.h"
#include "ksircprocess.h"

KSircView::KSircView(KSircProcess *proc, TQWidget *tqparent, const char *name)
    : KSirc::TextView(tqparent, name), m_proc(proc)
{
    m_acceptFiles = false;
    m_timestamps = false;
    viewport()->setAcceptDrops(true);
    clear();
    connect( this, TQT_SIGNAL( linkClicked( const TQMouseEvent *, const TQString & ) ),
             this, TQT_SLOT( anchorClicked( const TQMouseEvent *, const TQString & ) ) );

    TQPixmap background = ksopts->backgroundPixmap();
    if ( !background.isNull() )
        viewport()->setBackgroundPixmap( background );

    setLinkColor( ksopts->linkColor );
}

KSircView::~KSircView()
{
}

void KSircView::clear()
{
    m_lines = 0;
    m_timeStamps.clear();
    KSirc::TextView::clear();
}

TQString KSircView::makeTimeStamp()
{
    TQTime now = TQTime::currentTime();
    TQString timeStamp = TQString::tqfromLatin1( "[%1:%2:%3] " )
        .tqarg( TQString::number( now.hour() ).rightJustify( 2, '0' ) )
        .tqarg( TQString::number( now.minute() ).rightJustify( 2, '0' ) )
        .tqarg( TQString::number( now.second() ).rightJustify( 2, '0' ) );
    return timeStamp;
}

void KSircView::saveURL( const TQString &url )
{
	KURL kurl( url );

	KFileDialog *dlg = new KFileDialog( TQString(), TQString() /*filter*/, this /*tqparent*/, "filedialog" /*name*/, true /*modal*/ );

	dlg->setKeepLocation( true );

	dlg->setCaption( i18n( "Save As" ) );

	if ( !kurl.fileName().isEmpty() )
		dlg->setSelection( kurl.fileName() );

	if ( dlg->exec() ) {
		KURL destURL( dlg->selectedURL() );
		if ( destURL.isValid() ) {
			KIO::Job *job = KIO::copy( kurl, destURL );
			job->setAutoErrorHandlingEnabled( true );
		}
	}

	delete dlg;
}

TQString KSircView::addLine(const TQString &pixmap, const TQColor &color, const TQString &_text)
{
    //kdDebug(5008) << "Start Text:      " << _text << endl;

    TQString richText( "<font color=\"%1\">" );
    richText = richText.tqarg( color.name() );

    if ( !pixmap.isEmpty() )
        richText.prepend( TQString::tqfromLatin1( "<img src=\"%1\"></img>" ).tqarg( pixmap ) );

    TQString timeStamp = TQString::tqfromLatin1( "<font color=\"%1\">%2</font>" )
        .tqarg( ksopts->textColor.name() )
        .tqarg( makeTimeStamp() );
    m_timeStamps.append(timeStamp);
    if ( m_timestamps )
        richText.prepend( timeStamp );

    TQString text = TQStyleSheet::escape(_text);

    // ### a bit of a hack: turn '&lt;nick&gt; message' into
    // <span>&lt;nick&gt;<span> message' . span itself isn't supported but it
    // enforces the creation of separate item objects and hence separate
    // drawing of '<nick>' and 'message' , which is needed for BiDi users,
    // according to UV Kochavi <uv1st@yahoo.com> , to prevent output like
    // '<nick message<' , which is supposedly a bug in TQt's reordering.  The
    // same is done for [nick] and >nick< to catch queries.
    TQRegExp bidiRe( "^(&lt;\\S+&gt;)(.+)$" );
    text.tqreplace( bidiRe, TQString::tqfromLatin1( "<span>\\1</span>\\2" ) );
    TQRegExp bidiRe2( "^(\\[\\S+\\])(.+)$" );
    text.tqreplace( bidiRe2, TQString::tqfromLatin1( "<span>\\1</span>\\2" ) );
    TQRegExp bidiRe3( "^(&gt;\\S+&lt;)(.+)$" );
    text.tqreplace( bidiRe3, TQString::tqfromLatin1( "<span>\\1</span>\\2" ) );

    TQRegExp nickCol( "~n(.+)~n" );
    nickCol.setMinimal(true);
    int pos;

    while( (pos = nickCol.search(text)) >= 0 ){
        //kdDebug(5008) << "Found nick: " << nickCol.cap(1) << endl;
        TQString newText = nickCol.cap(1);
        if( nickCol.cap(1) != m_proc->getNick()){
            TQColor col = nickColourMaker::colourMaker()->findFg(nickCol.cap(1));

            newText.prepend(TQString("<font color=\"%1\">").tqarg(col.name()));
            newText.append("</font>");
        }  else {
            TQColor col = ksopts->ownNickColor.name();
            if( ! col.isValid())
                nickColourMaker::colourMaker()->findFg(nickCol.cap(1));
            newText.prepend(TQString("<font color=\"%1\">").tqarg(col.name()));
            newText.append("</font>");
            if(ksopts->ownNickBold){
                newText.prepend("<b>");
                newText.append("</b>");
            }
            if(ksopts->ownNickUl){
                newText.prepend("<u>");
                newText.append("</u>");
            }
            if(ksopts->ownNickRev){
                newText.prepend("<r>");
                newText.append("</r>");
            }
        }
        text.tqreplace(pos, nickCol.matchedLength(), newText);
    }

    //kdDebug(5008) << "After Text:      " << text << endl;

    KSParser parser;
    richText += parser.parse( text );

    richText += "</font>";


    //kdDebug(5008) << "Text:      " << _text << endl;


    richText = KStringHandler::tagURLs( richText );
    //kdDebug(5008) << "Rich text: " << richText << endl;

    KSirc::TextParagIterator parag = appendParag( richText );

    m_lines++;
    if ( ksopts->windowLength && m_lines > ksopts->windowLength )
    {
        while ( m_lines > ksopts->windowLength )
        {
            removeParag( firstParag() );
            m_timeStamps.remove( m_timeStamps.begin() );
            m_lines--;
        }
    }

    if (parser.beeped()) {
        KNotifyClient::event(winId(), TQString::tqfromLatin1("BeepReceived"),
                        i18n("Beep Received"));
    }

    TQString logText = parag.plainText();
    // append timestamp if it's not already there
    if ( ! m_timestamps )
        logText.prepend( makeTimeStamp() );

    return logText + '\n';
}

void KSircView::addRichText(const TQString &_text)
{
    //kdDebug(5008) << "Start Text:      " << _text << endl;

    TQString text = _text;

    TQRegExp re("^(<font color=\"[^\"]+\">\\[[0-9:]+\\] </font>)");
    TQString timeStamp;

    if(re.search(text) >= 0){
        timeStamp = re.cap(1);
    }
    else {
	timeStamp = TQString::tqfromLatin1( "<font color=\"%1\">%2</font>" )
	    .tqarg( ksopts->textColor.name() )
	    .tqarg( makeTimeStamp() );
	if ( m_timestamps )
	    text.prepend( timeStamp );
    }
    m_timeStamps.append(timeStamp);

    KSirc::TextParagIterator parag = appendParag( text );

    m_lines++;
    if ( ksopts->windowLength && m_lines > ksopts->windowLength )
    {
        while ( m_lines > ksopts->windowLength )
        {
            removeParag( firstParag() );
            m_timeStamps.remove( m_timeStamps.begin() );
            m_lines--;
        }
    }

}

void KSircView::enableTimeStamps(bool enable)
{
    if(enable == m_timestamps)
        return;
    setUpdatesEnabled( false );
    m_timestamps = enable;
    KSirc::TextParagIterator paragIt = firstParag();
    TQStringList::ConstIterator timeStampIt = m_timeStamps.begin();
    for (; !paragIt.atEnd(); ++paragIt, ++timeStampIt )
    {
        TQString text = paragIt.richText();
        if ( enable )
            text.prepend( *timeStampIt );
        else
            text.remove( 0, (*timeStampIt).length() );
        paragIt.setRichText( text );
    }
    setUpdatesEnabled( true );
    updateContents();
}

void KSircView::anchorClicked(const TQMouseEvent *ev, const TQString &url)
{
    if ( (ev->button() & Qt::LeftButton) && (ev->state() & ShiftButton ) )
		saveURL( url );
	else if ( (ev->button() & Qt::LeftButton) || (ev->button() & Qt::MidButton) )
    {
        openBrowser( url );
    }
    else if ( ev->button() & Qt::RightButton )
    {
        static const int openURLID = 0;
        static const int copyLinkLocationID = 1;

        // Adding a nice contextmenu
        KPopupMenu* menu = new KPopupMenu( this );
        menu->insertTitle( i18n( "URL" ) );
        menu->insertItem( i18n("Open URL"), openURLID );
        menu->insertItem( i18n("Copy Link Address"), copyLinkLocationID );
        switch( menu->exec( ( ev->globalPos() ) ) )
        {
        case openURLID :
            openBrowser( url );
            break;
        case copyLinkLocationID :
            copyLinkToClipboard( url );
            break;
        default:
            break;
        }
        delete menu;
    }
}

void KSircView::openBrowser(const TQString &url )
{
    (void) new KRun( KURL( url.startsWith("www") ? TQString::tqfromLatin1("http://") + url : url));
}

void KSircView::copyLinkToClipboard( const TQString &url )
{
    TQApplication::tqclipboard()->setText( url, TQClipboard::Clipboard );
}

TQColor KSircView::ircColor(int code)
{
    if (code >= 0 && code < 16)
        return ksopts->ircColors[code];
    return TQColor();
}

void KSircView::contentsDragEnterEvent(TQDragEnterEvent* event)
{
    event->accept((TQTextDrag::canDecode(event) ||
                   (m_acceptFiles && KURLDrag::canDecode(event))) &&
                  (!event->source() || event->source() != viewport()));
}

void KSircView::contentsDragMoveEvent(TQDragMoveEvent* event)
{
    event->accept(!event->source() || event->source() != viewport());
}

void KSircView::contentsDropEvent(TQDropEvent* event)
{
    TQStringList urls;
    TQString text;

    if (m_acceptFiles && KURLDrag::decodeLocalFiles(event, urls))
        emit urlsDropped(urls);
    else if (TQTextDrag::decode(event, text))
        emit textDropped(text);
}

// vim: ts=4 sw=4 noet