• Skip to content
  • Skip to link menu
  • KDE API Reference
  • KDE Home
  • Contact Us
 

Nepomuk-Core

  • KTp
message-url-filter.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 Lasath Fernando <kde@lasath.org>
3  Copyright (C) 2012 David Edmundson <kde@davidedmundson.co.uk>
4  Copyright (C) 2012 Rohan Garg <rohangarg@kubuntu.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public
17  License along with this library; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 
21 #include "message-filters-private.h"
22 
23 #include <KUrl>
24 #include <KTp/text-parser.h>
25 
26 MessageUrlFilter::MessageUrlFilter(QObject *parent)
27  : KTp::AbstractMessageFilter(parent)
28 {
29 }
30 
31 void MessageUrlFilter::filterMessage(KTp::Message &info, const KTp::MessageContext &context)
32 {
33  Q_UNUSED(context);
34  QString message = info.mainMessagePart();
35  //FIXME: make "Urls" into a constant
36  QVariantList urls = info.property("Urls").toList();
37 
38  // link detection
39  KTp::TextUrlData parsedUrl = KTp::TextParser::instance()->extractUrlData(message);
40 
41  int offset = 0;
42  for (int i = 0; i < parsedUrl.fixedUrls.size(); i++) {
43  KUrl url(parsedUrl.fixedUrls.at(i));
44  if (url.protocol() != QLatin1String("mailto")) {
45  QString originalText = message.mid(parsedUrl.urlRanges.at(i).first + offset, parsedUrl.urlRanges.at(i).second);
46  QString link = QString::fromLatin1("<a href=\"%1\">%2</a>").arg(QString::fromAscii(url.toEncoded()), originalText);
47  message.replace(parsedUrl.urlRanges.at(i).first + offset, parsedUrl.urlRanges.at(i).second, link);
48 
49  urls.append(url);
50 
51  //after the first replacement is made, the original position values are not valid anymore, this adjusts them
52  offset += link.length() - originalText.length();
53  }
54  }
55 
56  info.setProperty("Urls", urls);
57  info.setMainMessagePart(message);
58 }
59 
60 
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Fri Mar 22 2013 10:58:52 by doxygen 1.8.1.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

ktp-common-internals API Reference

Skip menu "ktp-common-internals API Reference"
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal