• Skip to content
  • Skip to link menu
KDE 4.6 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • KDE Home
  • Contact Us
 

Syndication Library

tools.cpp
00001 /*
00002  * This file is part of the syndication library
00003  *
00004  * Copyright (C) 2006 Frank Osterfeld <osterfeld@kde.org>
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Library General Public License
00017  * along with this library; see the file COPYING.LIB.  If not, write to
00018  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "tools.h"
00024 #include "personimpl.h"
00025 
00026 #include <kcharsets.h>
00027 #include <kcodecs.h>
00028 #include <kdatetime.h>
00029 
00030 #include <QtCore/QByteArray>
00031 #include <QtCore/QDateTime>
00032 #include <QtCore/QRegExp>
00033 #include <QtCore/QString>
00034 
00035 #include <kdebug.h>
00036 
00037 namespace Syndication {
00038 
00039 KMD5 md5Machine;
00040 
00041 unsigned int calcHash(const QString& str)
00042 {
00043     return calcHash(str.toUtf8());
00044 }
00045 
00046 unsigned int calcHash(const QByteArray& array)
00047 {
00048     if (array.isEmpty())
00049     {
00050         return 0;
00051     }
00052     else
00053     {
00054         const char* s = array.data();
00055         unsigned int hash = 5381;
00056         int c;
00057         while ( ( c = *s++ ) ) hash = ((hash << 5) + hash) + c; // hash*33 + c
00058         return hash;
00059     }
00060 }
00061 
00062 time_t parseISODate(const QString& str)
00063 {
00064     KDateTime kdt = KDateTime::fromString(str, KDateTime::ISODate);
00065     uint ret = kdt.isValid() ? kdt.toTime_t() : 0;
00066     return (time_t)ret;
00067 }
00068 
00069 time_t parseRFCDate(const QString& str)
00070 {
00071     KDateTime kdt = KDateTime::fromString(str, KDateTime::RFCDate);
00072     uint ret = kdt.isValid() ? kdt.toTime_t() : 0;
00073     return (time_t)ret;
00074 }
00075 
00076 time_t parseDate(const QString& str, DateFormat hint)
00077 {
00078     if (str.isEmpty())
00079         return 0;
00080 
00081     if (hint == RFCDate)
00082     {
00083         time_t t = parseRFCDate(str);
00084         return t != 0 ? t : parseISODate(str);
00085     }
00086     else
00087     {
00088         time_t t = parseISODate(str);
00089         return t != 0 ? t : parseRFCDate(str);
00090     }
00091 }
00092 
00093 QString dateTimeToString(time_t date)
00094 {
00095     if (date == 0)
00096         return QString();
00097 
00098     QDateTime dt;
00099     dt.setTime_t(date);
00100     return dt.toString();
00101 }
00102 
00103 QString calcMD5Sum(const QString& str)
00104 {
00105     md5Machine.reset();
00106     md5Machine.update(str.toUtf8());
00107     return QString(md5Machine.hexDigest().data());
00108 }
00109 
00110 QString resolveEntities(const QString& str)
00111 {
00112     return KCharsets::resolveEntities(str);
00113 }
00114 
00115 QString escapeSpecialCharacters(const QString& strp)
00116 {
00117     QString str(strp);
00118     str.replace('&', "&amp;");
00119     str.replace('\"', "&quot;");
00120     str.replace('<', "&lt;");
00121     str.replace('>', "&gt;");
00122     str.replace('\'', "&apos;");
00123     return str.trimmed();
00124 }
00125 
00126 QString convertNewlines(const QString& strp)
00127 {
00128     QString str(strp);
00129     str.replace('\n', "<br/>");
00130     return str;
00131 }
00132 
00133 QString plainTextToHtml(const QString& plainText)
00134 {
00135     QString str(plainText);
00136     str.replace('&', "&amp;");
00137     str.replace('\"', "&quot;");
00138     str.replace('<', "&lt;");
00139     //str.replace('>', "&gt;");
00140     str.replace('\n', "<br/>");
00141     return str.trimmed();
00142 }
00143 
00144 QString htmlToPlainText(const QString& html)
00145 {
00146     QString str(html);
00147     //TODO: preserve some formatting, such as line breaks
00148     str.remove(QRegExp("<[^>]*>")); // remove tags
00149     str = resolveEntities(str);
00150     return str.trimmed();
00151 }
00152 
00153 namespace {
00154     static QRegExp tagRegExp;
00155     static bool tagRegExpSet = false;
00156 }
00157 
00158 bool stringContainsMarkup(const QString& str)
00159 {
00160     //check for entities
00161     if (str.contains(QRegExp("&[a-zA-Z0-9#]+;")))
00162         return true;
00163 
00164     const int ltc = str.count('<');
00165     if (ltc == 0)
00166         return false;
00167 
00168     if (!tagRegExpSet)
00169     {
00170         tagRegExp = QRegExp("<\\w+.*/?>");
00171         tagRegExpSet = true;
00172     }
00173     return str.contains(tagRegExp);
00174 }
00175 
00176 bool isHtml(const QString& str)
00177 {
00178     //check for entities
00179     if (str.contains(QRegExp("&[a-zA-Z0-9#]+;")))
00180         return true;
00181 
00182     const int ltc = str.count('<');
00183     if (ltc == 0)
00184         return false;
00185 
00186     if (!tagRegExpSet)
00187     {
00188         tagRegExp = QRegExp("<\\w+.*/?>");
00189         tagRegExpSet = true;
00190     }
00191     if (str.contains(tagRegExp))
00192         return true;
00193 
00194     return false;
00195 }
00196 
00197 QString normalize(const QString& str)
00198 {
00199     return isHtml(str) ? str.trimmed() : plainTextToHtml(str);
00200 }
00201 
00202 QString normalize(const QString& strp, bool isCDATA, bool containsMarkup)
00203 {
00204     if (containsMarkup)
00205         return strp.trimmed();
00206     else
00207     {
00208         if (isCDATA)
00209         {
00210             QString str = resolveEntities(strp);
00211             str = escapeSpecialCharacters(str);
00212             str = convertNewlines(str);
00213             str = str.trimmed();
00214             return str;
00215         }
00216         else
00217         {
00218             QString str = escapeSpecialCharacters(strp);
00219             str = str.trimmed();
00220             return str;
00221         }
00222     }
00223 }
00224 
00225 PersonPtr personFromString(const QString& strp)
00226 {
00227     QString str = strp.trimmed();
00228     if (str.isEmpty())
00229         return PersonPtr(new PersonImpl());
00230 
00231     str = resolveEntities(str);
00232     QString name;
00233     QString uri;
00234     QString email;
00235 
00236     // look for something looking like a mail address ( "foo@bar.com",
00237     // "<foo@bar.com>") and extract it
00238 
00239     QRegExp remail("<?([^@\\s<]+@[^>\\s]+)>?"); // FIXME: user "proper" regexp,
00240        // search kmail source for it
00241 
00242     int pos = remail.indexIn(str);
00243     if (pos != -1)
00244     {
00245         QString all = remail.cap(0);
00246         email = remail.cap(1);
00247         str.remove(all); // remove mail address
00248     }
00249 
00250     // replace "mailto", "(", ")" (to be extended)
00251     email.remove("mailto:");
00252     email.remove(QRegExp("[\\(\\)]"));
00253 
00254     // simplify the rest and use it as name
00255 
00256     name = str.simplified();
00257 
00258     // after removing the email, str might have
00259     // the format "(Foo M. Bar)". We cut off
00260     // parentheses if there are any. However, if
00261     // str is of the format "Foo M. Bar (President)",
00262     // we should not cut anything.
00263 
00264     QRegExp rename("^\\(([^\\)]*)\\)");
00265 
00266     if (rename.exactMatch(name))
00267     {
00268         name = rename.cap(1);
00269     }
00270 
00271     name = name.isEmpty() ? QString() : name;
00272     email = email.isEmpty() ? QString() : email;
00273     uri = uri.isEmpty() ? QString() : uri;
00274 
00275     if (name.isEmpty() && email.isEmpty() && uri.isEmpty())
00276         return PersonPtr(new PersonImpl());
00277 
00278     return PersonPtr(new PersonImpl(name, uri, email));
00279 }
00280 
00281 ElementType::ElementType(const QString& localnamep,
00282             const QString& nsp) : ns(nsp), localname(localnamep)
00283 {
00284 }
00285 
00286 bool ElementType::operator==(const ElementType& other) const
00287 {
00288     return localname == other.localname && ns == other.ns;
00289 }
00290 
00291 } // namespace Syndication
00292 
00293 

Syndication Library

Skip menu "Syndication Library"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.7.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal