~ubuntu-branches/ubuntu/trusty/ktp-text-ui/trusty

« back to all changes in this revision

Viewing changes to filters/formatting/format-filter.cpp

  • Committer: Package Import Robot
  • Author(s): Mark Purcell, Ritesh Raj Sarraf, Diane Trout, Mark Purcell
  • Date: 2013-08-11 11:01:09 UTC
  • mfrom: (1.3.1)
  • mto: This revision was merged to the branch mainline in revision 17.
  • Revision ID: package-import@ubuntu.com-20130811110109-diw18taoxh8mkocf
[ Ritesh Raj Sarraf ]
* [e26f6a8] drop patch 03_no_redundant_glib_calls.diff
* [459ee64] Drop tests, for now, as they are failing

[ Diane Trout ]
* Update watch file as KDE-Telepathy is now in stable release.
* Bump ktp-common-internals version
* Plasma-widget-telpathy-chat was moved to ktp-desktop-applet
* Add telepathy-logger dependency
* Imported Upstream version 0.6.2.1
* Refresh patches
* Bump ktp-common-internal dependency
* Set Uploaders to Diane Trout and Michał Zając
* Update to Standards-Version 3.9.4. No changes needed.
* Add several plugins to kde-telepathy-text-ui.install
* Add description to 02_ktpchat_soversion.diff
* Merge libktpchat0 into ktp-telepathy-text-ui.
* Override lintian warnings about the libktpchat0 library.

[ Mark Purcell ] 
* Imported Upstream version 0.6.3
* Add myself to Uploaders
* Update Build-Depends: libktpcommoninternalsprivate-dev (>= 0.6.3)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *    Copyright (C) 2012  Lasath Fernando <kde@lasath.org>
 
3
 *    Copyright (C) 2012  Daniele E. Domenichelli <daniele.domenichelli@gmail.com>
 
4
 *
 
5
 *    This library is free software; you can redistribute it and/or
 
6
 *    modify it under the terms of the GNU Lesser General Public
 
7
 *    License as published by the Free Software Foundation; either
 
8
 *    version 2.1 of the License, or (at your option) any later version.
 
9
 *
 
10
 *    This library is distributed in the hope that it will be useful,
 
11
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
 *    Lesser General Public License for more details.
 
14
 *
 
15
 *    You should have received a copy of the GNU Lesser General Public
 
16
 *    License along with this library; if not, write to the Free Software
 
17
 *    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 
18
*/
 
19
 
 
20
#include "format-filter.h"
 
21
 
 
22
#include <QRegExp>
 
23
 
 
24
#include <KPluginFactory>
 
25
#include <KDebug>
 
26
 
 
27
typedef QPair<QRegExp, QString> FormatTag;
 
28
 
 
29
class FormatFilter::Private {
 
30
public:
 
31
    QList<FormatTag> tags;
 
32
    QString mainPattern;
 
33
    QString allTagsPattern;
 
34
 
 
35
    void addTag (const QString &markingCharacter, const QString &htmlTag);
 
36
    QString filterString(QString string);
 
37
};
 
38
 
 
39
FormatFilter::FormatFilter (QObject* parent, const QVariantList&) :
 
40
    KTp::AbstractMessageFilter (parent),
 
41
    d(new Private())
 
42
{
 
43
    // Matches a string
 
44
    // 1. The beginning of the string or a white character [ (^|\\s) ]
 
45
    // 2. Depending on the regexp the tag to be replaced or a pattern including
 
46
    //    all the tags in tagsMap [ %1 ]
 
47
    // 3. One non-whitespace character, or by any string that starts and ends
 
48
    //    with a non-whitespace character [ (\\S|\\S.*\\S) ]
 
49
    // 4. Same as 2. [ %1 ]
 
50
    // 5. A white character or the end of the string [ (\\s|$) ]
 
51
    d->mainPattern = QLatin1String("(^|\\s)%1(\\S|\\S.*\\S)%1(\\s|$)");
 
52
 
 
53
    QMap<QString, QString> tagsMap;
 
54
 
 
55
    tagsMap[QLatin1String("_")] = QLatin1Char('u');
 
56
    tagsMap[QLatin1String("*")] = QLatin1Char('b');
 
57
    tagsMap[QLatin1String("-")] = QLatin1Char('s');
 
58
    tagsMap[QLatin1String("/")] = QLatin1Char('i');
 
59
 
 
60
    d->allTagsPattern = QLatin1Char('(');
 
61
 
 
62
    QMapIterator<QString, QString> i(tagsMap);
 
63
    while (i.hasNext()) {
 
64
        i.next();
 
65
 
 
66
        d->allTagsPattern += QRegExp::escape(i.key());
 
67
        if (i.hasNext())
 
68
            d->allTagsPattern += QLatin1Char('|');
 
69
 
 
70
        d->addTag(i.key(), i.value());
 
71
    }
 
72
 
 
73
    d->allTagsPattern += QLatin1Char(')');
 
74
}
 
75
 
 
76
FormatFilter::~FormatFilter()
 
77
{
 
78
    delete d;
 
79
}
 
80
 
 
81
void FormatFilter::filterMessage (KTp::Message& message, const KTp::MessageContext &context)
 
82
{
 
83
    Q_UNUSED(context)
 
84
    message.setMainMessagePart(d->filterString(message.mainMessagePart()));
 
85
}
 
86
 
 
87
QString FormatFilter::Private::filterString(QString string)
 
88
{
 
89
    QRegExp rx(mainPattern.arg(allTagsPattern));
 
90
    rx.setMinimal(true);
 
91
 
 
92
    int pos = 0;
 
93
    while ((pos = string.indexOf(rx, pos)) != -1) {
 
94
        string = string.replace(rx.cap(3), filterString(rx.cap(3)));
 
95
        pos += rx.matchedLength();
 
96
    }
 
97
 
 
98
    Q_FOREACH(const FormatTag &tag, tags) {
 
99
        string = string.replace(tag.first, tag.second);
 
100
    }
 
101
 
 
102
    return string;
 
103
}
 
104
 
 
105
void FormatFilter::Private::addTag (const QString &markingCharacter, const QString &htmlTag)
 
106
{
 
107
    QString repl = QLatin1String("\\1<%1>%2\\2%2</%1>\\3");
 
108
    repl = repl.arg(htmlTag).arg(markingCharacter);
 
109
 
 
110
    QRegExp exp = QRegExp(mainPattern.arg(QRegExp::escape(markingCharacter)));
 
111
    exp.setMinimal(true);
 
112
 
 
113
    tags.append(FormatTag(exp, repl));
 
114
}
 
115
 
 
116
K_PLUGIN_FACTORY(MessageFilterFactory, registerPlugin<FormatFilter>();)
 
117
K_EXPORT_PLUGIN(MessageFilterFactory("ktptextui_message_filter_formatting"))