~larryprice/acolyterm/release-0.1

« back to all changes in this revision

Viewing changes to src/plugin/konsole/Filter.cpp

  • Committer: Larry Price
  • Date: 2016-06-15 14:47:59 UTC
  • Revision ID: larry.price@canonical.com-20160615144759-6wopn0gxwgta3x1n
Updating QMLTermWidget and removing unnecessary konsole codebase

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
    Copyright 2007-2008 by Robert Knight <robertknight@gmail.com>
3
 
 
4
 
    This program is free software; you can redistribute it and/or modify
5
 
    it under the terms of the GNU General Public License as published by
6
 
    the Free Software Foundation; either version 2 of the License, or
7
 
    (at your option) any later version.
8
 
 
9
 
    This program is distributed in the hope that it will be useful,
10
 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
    GNU General Public License for more details.
13
 
 
14
 
    You should have received a copy of the GNU General Public License
15
 
    along with this program; if not, write to the Free Software
16
 
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17
 
    02110-1301  USA.
18
 
*/
19
 
 
20
 
// Own
21
 
#include "Filter.h"
22
 
 
23
 
// System
24
 
#include <iostream>
25
 
 
26
 
// Qt
27
 
#include <QtWidgets/QAction>
28
 
#include <QtWidgets/QApplication>
29
 
#include <QtGui/QClipboard>
30
 
#include <QtCore/QString>
31
 
#include <QtCore/QTextStream>
32
 
#include <QtCore/QSharedData>
33
 
#include <QtCore/QFile>
34
 
#include <QDesktopServices>
35
 
#include <QUrl>
36
 
 
37
 
// KDE
38
 
//#include <KLocale>
39
 
//#include <KRun>
40
 
 
41
 
// Konsole
42
 
#include "TerminalCharacterDecoder.h"
43
 
#include "konsole_wcwidth.h"
44
 
 
45
 
 
46
 
FilterChain::~FilterChain()
47
 
{
48
 
    QMutableListIterator<Filter*> iter(*this);
49
 
 
50
 
    while ( iter.hasNext() )
51
 
    {
52
 
        Filter* filter = iter.next();
53
 
        iter.remove();
54
 
        delete filter;
55
 
    }
56
 
}
57
 
 
58
 
void FilterChain::addFilter(Filter* filter)
59
 
{
60
 
    append(filter);
61
 
}
62
 
void FilterChain::removeFilter(Filter* filter)
63
 
{
64
 
    removeAll(filter);
65
 
}
66
 
bool FilterChain::containsFilter(Filter* filter)
67
 
{
68
 
    return contains(filter);
69
 
}
70
 
void FilterChain::reset()
71
 
{
72
 
    QListIterator<Filter*> iter(*this);
73
 
    while (iter.hasNext())
74
 
        iter.next()->reset();
75
 
}
76
 
void FilterChain::setBuffer(const QString* buffer , const QList<int>* linePositions)
77
 
{
78
 
    QListIterator<Filter*> iter(*this);
79
 
    while (iter.hasNext())
80
 
        iter.next()->setBuffer(buffer,linePositions);
81
 
}
82
 
void FilterChain::process()
83
 
{
84
 
    QListIterator<Filter*> iter(*this);
85
 
    while (iter.hasNext())
86
 
        iter.next()->process();
87
 
}
88
 
void FilterChain::clear()
89
 
{
90
 
    QList<Filter*>::clear();
91
 
}
92
 
Filter::HotSpot* FilterChain::hotSpotAt(int line , int column) const
93
 
{
94
 
    QListIterator<Filter*> iter(*this);
95
 
    while (iter.hasNext())
96
 
    {
97
 
        Filter* filter = iter.next();
98
 
        Filter::HotSpot* spot = filter->hotSpotAt(line,column);
99
 
        if ( spot != 0 )
100
 
        {
101
 
            return spot;
102
 
        }
103
 
    }
104
 
 
105
 
    return 0;
106
 
}
107
 
 
108
 
QList<Filter::HotSpot*> FilterChain::hotSpots() const
109
 
{
110
 
    QList<Filter::HotSpot*> list;
111
 
    QListIterator<Filter*> iter(*this);
112
 
    while (iter.hasNext())
113
 
    {
114
 
        Filter* filter = iter.next();
115
 
        list << filter->hotSpots();
116
 
    }
117
 
    return list;
118
 
}
119
 
//QList<Filter::HotSpot*> FilterChain::hotSpotsAtLine(int line) const;
120
 
 
121
 
TerminalImageFilterChain::TerminalImageFilterChain()
122
 
: _buffer(0)
123
 
, _linePositions(0)
124
 
{
125
 
}
126
 
 
127
 
TerminalImageFilterChain::~TerminalImageFilterChain()
128
 
{
129
 
    delete _buffer;
130
 
    delete _linePositions;
131
 
}
132
 
 
133
 
void TerminalImageFilterChain::setImage(const Character* const image , int lines , int columns, const QVector<LineProperty>& lineProperties)
134
 
{
135
 
    if (empty())
136
 
        return;
137
 
 
138
 
    // reset all filters and hotspots
139
 
    reset();
140
 
 
141
 
    PlainTextDecoder decoder;
142
 
    decoder.setTrailingWhitespace(false);
143
 
 
144
 
    // setup new shared buffers for the filters to process on
145
 
    QString* newBuffer = new QString();
146
 
    QList<int>* newLinePositions = new QList<int>();
147
 
    setBuffer( newBuffer , newLinePositions );
148
 
 
149
 
    // free the old buffers
150
 
    delete _buffer;
151
 
    delete _linePositions;
152
 
 
153
 
    _buffer = newBuffer;
154
 
    _linePositions = newLinePositions;
155
 
 
156
 
    QTextStream lineStream(_buffer);
157
 
    decoder.begin(&lineStream);
158
 
 
159
 
    for (int i=0 ; i < lines ; i++)
160
 
    {
161
 
        _linePositions->append(_buffer->length());
162
 
        decoder.decodeLine(image + i*columns,columns,LINE_DEFAULT);
163
 
 
164
 
        // pretend that each line ends with a newline character.
165
 
        // this prevents a link that occurs at the end of one line
166
 
        // being treated as part of a link that occurs at the start of the next line
167
 
        //
168
 
        // the downside is that links which are spread over more than one line are not
169
 
        // highlighted.
170
 
        //
171
 
        // TODO - Use the "line wrapped" attribute associated with lines in a
172
 
        // terminal image to avoid adding this imaginary character for wrapped
173
 
        // lines
174
 
        if ( !(lineProperties.value(i,LINE_DEFAULT) & LINE_WRAPPED) )
175
 
            lineStream << QChar('\n');
176
 
    }
177
 
    decoder.end();
178
 
}
179
 
 
180
 
Filter::Filter() :
181
 
_linePositions(0),
182
 
_buffer(0)
183
 
{
184
 
}
185
 
 
186
 
Filter::~Filter()
187
 
{
188
 
    QListIterator<HotSpot*> iter(_hotspotList);
189
 
    while (iter.hasNext())
190
 
    {
191
 
        delete iter.next();
192
 
    }
193
 
}
194
 
void Filter::reset()
195
 
{
196
 
    _hotspots.clear();
197
 
    _hotspotList.clear();
198
 
}
199
 
 
200
 
void Filter::setBuffer(const QString* buffer , const QList<int>* linePositions)
201
 
{
202
 
    _buffer = buffer;
203
 
    _linePositions = linePositions;
204
 
}
205
 
 
206
 
void Filter::getLineColumn(int position , int& startLine , int& startColumn)
207
 
{
208
 
    Q_ASSERT( _linePositions );
209
 
    Q_ASSERT( _buffer );
210
 
 
211
 
 
212
 
    for (int i = 0 ; i < _linePositions->count() ; i++)
213
 
    {
214
 
        int nextLine = 0;
215
 
 
216
 
        if ( i == _linePositions->count()-1 )
217
 
            nextLine = _buffer->length() + 1;
218
 
        else
219
 
            nextLine = _linePositions->value(i+1);
220
 
 
221
 
        if ( _linePositions->value(i) <= position && position < nextLine )
222
 
        {
223
 
            startLine = i;
224
 
            startColumn = string_width(buffer()->mid(_linePositions->value(i),position - _linePositions->value(i)));
225
 
            return;
226
 
        }
227
 
    }
228
 
}
229
 
 
230
 
 
231
 
/*void Filter::addLine(const QString& text)
232
 
{
233
 
    _linePositions << _buffer.length();
234
 
    _buffer.append(text);
235
 
}*/
236
 
 
237
 
const QString* Filter::buffer()
238
 
{
239
 
    return _buffer;
240
 
}
241
 
Filter::HotSpot::~HotSpot()
242
 
{
243
 
}
244
 
void Filter::addHotSpot(HotSpot* spot)
245
 
{
246
 
    _hotspotList << spot;
247
 
 
248
 
    for (int line = spot->startLine() ; line <= spot->endLine() ; line++)
249
 
    {
250
 
        _hotspots.insert(line,spot);
251
 
    }
252
 
}
253
 
QList<Filter::HotSpot*> Filter::hotSpots() const
254
 
{
255
 
    return _hotspotList;
256
 
}
257
 
QList<Filter::HotSpot*> Filter::hotSpotsAtLine(int line) const
258
 
{
259
 
    return _hotspots.values(line);
260
 
}
261
 
 
262
 
Filter::HotSpot* Filter::hotSpotAt(int line , int column) const
263
 
{
264
 
    QListIterator<HotSpot*> spotIter(_hotspots.values(line));
265
 
 
266
 
    while (spotIter.hasNext())
267
 
    {
268
 
        HotSpot* spot = spotIter.next();
269
 
 
270
 
        if ( spot->startLine() == line && spot->startColumn() > column )
271
 
            continue;
272
 
        if ( spot->endLine() == line && spot->endColumn() < column )
273
 
            continue;
274
 
 
275
 
        return spot;
276
 
    }
277
 
 
278
 
    return 0;
279
 
}
280
 
 
281
 
Filter::HotSpot::HotSpot(int startLine , int startColumn , int endLine , int endColumn)
282
 
    : _startLine(startLine)
283
 
    , _startColumn(startColumn)
284
 
    , _endLine(endLine)
285
 
    , _endColumn(endColumn)
286
 
    , _type(NotSpecified)
287
 
{
288
 
}
289
 
QString Filter::HotSpot::tooltip() const
290
 
{
291
 
    return QString();
292
 
}
293
 
QList<QAction*> Filter::HotSpot::actions()
294
 
{
295
 
    return QList<QAction*>();
296
 
}
297
 
int Filter::HotSpot::startLine() const
298
 
{
299
 
    return _startLine;
300
 
}
301
 
int Filter::HotSpot::endLine() const
302
 
{
303
 
    return _endLine;
304
 
}
305
 
int Filter::HotSpot::startColumn() const
306
 
{
307
 
    return _startColumn;
308
 
}
309
 
int Filter::HotSpot::endColumn() const
310
 
{
311
 
    return _endColumn;
312
 
}
313
 
Filter::HotSpot::Type Filter::HotSpot::type() const
314
 
{
315
 
    return _type;
316
 
}
317
 
void Filter::HotSpot::setType(Type type)
318
 
{
319
 
    _type = type;
320
 
}
321
 
 
322
 
RegExpFilter::RegExpFilter()
323
 
{
324
 
}
325
 
 
326
 
RegExpFilter::HotSpot::HotSpot(int startLine,int startColumn,int endLine,int endColumn)
327
 
    : Filter::HotSpot(startLine,startColumn,endLine,endColumn)
328
 
{
329
 
    setType(Marker);
330
 
}
331
 
 
332
 
void RegExpFilter::HotSpot::activate(QObject*)
333
 
{
334
 
}
335
 
 
336
 
void RegExpFilter::HotSpot::setCapturedTexts(const QStringList& texts)
337
 
{
338
 
    _capturedTexts = texts;
339
 
}
340
 
QStringList RegExpFilter::HotSpot::capturedTexts() const
341
 
{
342
 
    return _capturedTexts;
343
 
}
344
 
 
345
 
void RegExpFilter::setRegExp(const QRegExp& regExp)
346
 
{
347
 
    _searchText = regExp;
348
 
}
349
 
QRegExp RegExpFilter::regExp() const
350
 
{
351
 
    return _searchText;
352
 
}
353
 
/*void RegExpFilter::reset(int)
354
 
{
355
 
    _buffer = QString();
356
 
}*/
357
 
void RegExpFilter::process()
358
 
{
359
 
    int pos = 0;
360
 
    const QString* text = buffer();
361
 
 
362
 
    Q_ASSERT( text );
363
 
 
364
 
    // ignore any regular expressions which match an empty string.
365
 
    // otherwise the while loop below will run indefinitely
366
 
    static const QString emptyString("");
367
 
    if ( _searchText.exactMatch(emptyString) )
368
 
        return;
369
 
 
370
 
    while(pos >= 0)
371
 
    {
372
 
        pos = _searchText.indexIn(*text,pos);
373
 
 
374
 
        if ( pos >= 0 )
375
 
        {
376
 
            int startLine = 0;
377
 
            int endLine = 0;
378
 
            int startColumn = 0;
379
 
            int endColumn = 0;
380
 
 
381
 
            getLineColumn(pos,startLine,startColumn);
382
 
            getLineColumn(pos + _searchText.matchedLength(),endLine,endColumn);
383
 
 
384
 
            RegExpFilter::HotSpot* spot = newHotSpot(startLine,startColumn,
385
 
                                           endLine,endColumn);
386
 
            spot->setCapturedTexts(_searchText.capturedTexts());
387
 
 
388
 
            addHotSpot( spot );
389
 
            pos += _searchText.matchedLength();
390
 
 
391
 
            // if matchedLength == 0, the program will get stuck in an infinite loop
392
 
            if ( _searchText.matchedLength() == 0 )
393
 
                pos = -1;
394
 
        }
395
 
    }
396
 
}
397
 
 
398
 
RegExpFilter::HotSpot* RegExpFilter::newHotSpot(int startLine,int startColumn,
399
 
                                                int endLine,int endColumn)
400
 
{
401
 
    return new RegExpFilter::HotSpot(startLine,startColumn,
402
 
                                                  endLine,endColumn);
403
 
}
404
 
RegExpFilter::HotSpot* UrlFilter::newHotSpot(int startLine,int startColumn,int endLine,
405
 
                                                    int endColumn)
406
 
{
407
 
    return new UrlFilter::HotSpot(startLine,startColumn,
408
 
                                               endLine,endColumn);
409
 
}
410
 
UrlFilter::HotSpot::HotSpot(int startLine,int startColumn,int endLine,int endColumn)
411
 
: RegExpFilter::HotSpot(startLine,startColumn,endLine,endColumn)
412
 
, _urlObject(new FilterObject(this))
413
 
{
414
 
    setType(Link);
415
 
}
416
 
QString UrlFilter::HotSpot::tooltip() const
417
 
{
418
 
    QString url = capturedTexts().first();
419
 
 
420
 
    const UrlType kind = urlType();
421
 
 
422
 
    if ( kind == StandardUrl )
423
 
        return QString();
424
 
    else if ( kind == Email )
425
 
        return QString();
426
 
    else
427
 
        return QString();
428
 
}
429
 
UrlFilter::HotSpot::UrlType UrlFilter::HotSpot::urlType() const
430
 
{
431
 
    QString url = capturedTexts().first();
432
 
 
433
 
    if ( FullUrlRegExp.exactMatch(url) )
434
 
        return StandardUrl;
435
 
    else if ( EmailAddressRegExp.exactMatch(url) )
436
 
        return Email;
437
 
    else
438
 
        return Unknown;
439
 
}
440
 
 
441
 
void UrlFilter::HotSpot::activate(QObject* object)
442
 
{
443
 
    QString url = capturedTexts().first();
444
 
 
445
 
    const UrlType kind = urlType();
446
 
 
447
 
    const QString& actionName = object ? object->objectName() : QString();
448
 
 
449
 
    if ( actionName == "copy-action" )
450
 
    {
451
 
        QApplication::clipboard()->setText(url);
452
 
        return;
453
 
    }
454
 
 
455
 
    if ( !object || actionName == "open-action" )
456
 
    {
457
 
        if ( kind == StandardUrl )
458
 
        {
459
 
            // if the URL path does not include the protocol ( eg. "www.kde.org" ) then
460
 
            // prepend http:// ( eg. "www.kde.org" --> "http://www.kde.org" )
461
 
            if (!url.contains("://"))
462
 
            {
463
 
                url.prepend("http://");
464
 
            }
465
 
        }
466
 
        else if ( kind == Email )
467
 
        {
468
 
            url.prepend("mailto:");
469
 
        }
470
 
 
471
 
        QDesktopServices::openUrl(QUrl(url));
472
 
        //new KRun(url,QApplication::activeWindow());
473
 
    }
474
 
}
475
 
 
476
 
// Note:  Altering these regular expressions can have a major effect on the performance of the filters
477
 
// used for finding URLs in the text, especially if they are very general and could match very long
478
 
// pieces of text.
479
 
// Please be careful when altering them.
480
 
 
481
 
//regexp matches:
482
 
// full url:
483
 
// protocolname:// or www. followed by anything other than whitespaces, <, >, ' or ", and ends before whitespaces, <, >, ', ", ], !, comma and dot
484
 
const QRegExp UrlFilter::FullUrlRegExp("(www\\.(?!\\.)|[a-z][a-z0-9+.-]*://)[^\\s<>'\"]+[^!,\\.\\s<>'\"\\]]");
485
 
// email address:
486
 
// [word chars, dots or dashes]@[word chars, dots or dashes].[word chars]
487
 
const QRegExp UrlFilter::EmailAddressRegExp("\\b(\\w|\\.|-)+@(\\w|\\.|-)+\\.\\w+\\b");
488
 
 
489
 
// matches full url or email address
490
 
const QRegExp UrlFilter::CompleteUrlRegExp('('+FullUrlRegExp.pattern()+'|'+
491
 
                                            EmailAddressRegExp.pattern()+')');
492
 
 
493
 
UrlFilter::UrlFilter()
494
 
{
495
 
    setRegExp( CompleteUrlRegExp );
496
 
}
497
 
UrlFilter::HotSpot::~HotSpot()
498
 
{
499
 
    delete _urlObject;
500
 
}
501
 
void FilterObject::activated()
502
 
{
503
 
    _filter->activate(sender());
504
 
}
505
 
QList<QAction*> UrlFilter::HotSpot::actions()
506
 
{
507
 
    QList<QAction*> list;
508
 
 
509
 
    const UrlType kind = urlType();
510
 
 
511
 
    QAction* openAction = new QAction(_urlObject);
512
 
    QAction* copyAction = new QAction(_urlObject);;
513
 
 
514
 
    Q_ASSERT( kind == StandardUrl || kind == Email );
515
 
 
516
 
    if ( kind == StandardUrl )
517
 
    {
518
 
        openAction->setText(QObject::tr("Open Link"));
519
 
        copyAction->setText(QObject::tr("Copy Link Address"));
520
 
    }
521
 
    else if ( kind == Email )
522
 
    {
523
 
        openAction->setText(QObject::tr("Send Email To..."));
524
 
        copyAction->setText(QObject::tr("Copy Email Address"));
525
 
    }
526
 
 
527
 
    // object names are set here so that the hotspot performs the
528
 
    // correct action when activated() is called with the triggered
529
 
    // action passed as a parameter.
530
 
    openAction->setObjectName( QLatin1String("open-action" ));
531
 
    copyAction->setObjectName( QLatin1String("copy-action" ));
532
 
 
533
 
    QObject::connect( openAction , SIGNAL(triggered()) , _urlObject , SLOT(activated()) );
534
 
    QObject::connect( copyAction , SIGNAL(triggered()) , _urlObject , SLOT(activated()) );
535
 
 
536
 
    list << openAction;
537
 
    list << copyAction;
538
 
 
539
 
    return list;
540
 
}
541
 
 
542
 
//#include "Filter.moc"