• Skip to content
  • Skip to link menu
KDE 4.0 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • 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     time_t res = KDateTime::fromString(str, KDateTime::ISODate).toTime_t();
00065     return res != -1 ? res : 0;
00066 }
00067 
00068 time_t parseRFCDate(const QString& str)
00069 {
00070     time_t res = KDateTime::fromString(str, KDateTime::RFCDate).toTime_t();
00071     return res != -1 ? res : 0;
00072 }
00073 
00074 time_t parseDate(const QString& str, DateFormat hint)
00075 {
00076     if (str.isEmpty())
00077         return 0;
00078     
00079     if (hint == RFCDate)
00080     {
00081         time_t t = parseRFCDate(str);
00082         return t != 0 ? t : parseISODate(str);
00083     }
00084     else
00085     {
00086         time_t t = parseISODate(str);
00087         return t != 0 ? t : parseRFCDate(str);
00088     }
00089 }
00090 
00091 QString dateTimeToString(time_t date)
00092 {
00093     if (date == 0)
00094         return QString();
00095     
00096     QDateTime dt;
00097     dt.setTime_t(date);
00098     return dt.toString();
00099 }
00100 
00101 QString calcMD5Sum(const QString& str)
00102 {
00103     md5Machine.reset();
00104     md5Machine.update(str.toUtf8());
00105     return QString(md5Machine.hexDigest().data());
00106 }
00107 
00108 QString resolveEntities(const QString& str)
00109 {
00110     return KCharsets::resolveEntities(str);
00111 }
00112 
00113 QString escapeSpecialCharacters(const QString& strp)
00114 {
00115     QString str(strp);
00116     str.replace("&", "&amp;");
00117     str.replace("\"", "&quot;");
00118     str.replace("<", "&lt;");
00119     str.replace(">", "&gt;");
00120     str.replace("\'", "&apos;");
00121     return str.trimmed();
00122 }
00123 
00124 QString convertNewlines(const QString& strp)
00125 {
00126     QString str(strp);
00127     str.replace("\n", "<br/>");
00128     return str;
00129 }
00130         
00131 QString plainTextToHtml(const QString& plainText)
00132 {
00133     QString str(plainText);
00134     str.replace("&", "&amp;");
00135     str.replace("\"", "&quot;");
00136     str.replace("<", "&lt;");
00137     //str.replace(">", "&gt;");
00138     str.replace("\n", "<br/>");
00139     return str.trimmed();
00140 }
00141 
00142 QString htmlToPlainText(const QString& html)
00143 {
00144     QString str(html);
00145     //TODO: preserve some formatting, such as line breaks
00146     str.replace(QRegExp("<[^>]*>"), ""); // remove tags
00147     str = resolveEntities(str);
00148     return str.trimmed();
00149 }
00150 
00151 static QRegExp tagRegExp;
00152 static bool tagRegExpSet = false;
00153 
00154 bool stringContainsMarkup(const QString& str)
00155 {   
00156     //check for entities
00157     if (str.contains(QRegExp("&[a-zA-Z0-9#]+;")))
00158         return true;
00159     
00160     int ltc = str.count('<');
00161     if (ltc == 0 || ltc != str.count('>'))
00162         return false;
00163        
00164     if (!tagRegExpSet)
00165     {
00166         tagRegExp = QRegExp("<\\w+.*/?>");
00167         tagRegExpSet = true;
00168     }
00169     return str.contains(tagRegExp);
00170 }
00171 
00172 bool isHtml(const QString& str)
00173 {
00174     //check for entities
00175     if (str.contains(QRegExp("&[a-zA-Z0-9#]+;")))
00176         return true;
00177     
00178     int ltc = str.count('<');
00179     if (ltc == 0 || ltc != str.count('>'))
00180         return false;
00181 
00182     if (!tagRegExpSet)
00183     {
00184         tagRegExp = QRegExp("<\\w+.*/?>");
00185         tagRegExpSet = true;
00186     }
00187     if (str.contains(tagRegExp))
00188         return true;
00189         
00190     return false;
00191 }
00192 
00193 QString normalize(const QString& str)
00194 {
00195     return isHtml(str) ? str.trimmed() : plainTextToHtml(str);
00196 }
00197 
00198 QString normalize(const QString& strp, bool isCDATA, bool containsMarkup)
00199 {
00200     if (containsMarkup)
00201         return strp.trimmed();
00202     else
00203     {
00204         if (isCDATA)
00205         {
00206             QString str = resolveEntities(strp);
00207             str = escapeSpecialCharacters(str);
00208             str = convertNewlines(str);
00209             str = str.trimmed();
00210             return str;
00211         }
00212         else
00213         {
00214             QString str = escapeSpecialCharacters(strp);
00215             str = str.trimmed();
00216             return str;
00217         }
00218     }
00219 }
00220 
00221 PersonPtr personFromString(const QString& strp)
00222 {
00223     QString str = strp.trimmed();
00224     if (str.isEmpty())
00225         return PersonPtr(new PersonImpl());
00226 
00227     str = resolveEntities(str);
00228     QString name;
00229     QString uri;
00230     QString email;
00231 
00232     // look for something looking like a mail address ( "foo@bar.com", 
00233     // "<foo@bar.com>") and extract it
00234     
00235     QRegExp remail("<?([^@\\s<]+@[^>\\s]+)>?"); // FIXME: user "proper" regexp,
00236        // search kmail source for it
00237     
00238     int pos = remail.indexIn(str);
00239     if (pos != -1)
00240     {
00241         QString all = remail.cap(0);
00242         email = remail.cap(1);
00243         str.replace(all, ""); // remove mail address
00244     }
00245 
00246     // replace "mailto", "(", ")" (to be extended)    
00247     email.remove("mailto:");
00248     email.remove(QRegExp("[\\(\\)]"));
00249 
00250     // simplify the rest and use it as name
00251     
00252     name = str.simplified();
00253     
00254     // after removing the email, str might have 
00255     // the format "(Foo M. Bar)". We cut off 
00256     // parentheses if there are any. However, if
00257     // str is of the format "Foo M. Bar (President)",
00258     // we should not cut anything.
00259 
00260     QRegExp rename("^\\(([^\\)]*)\\)");
00261 
00262     if (rename.exactMatch(name))
00263     {
00264         name = rename.cap(1);
00265     }
00266     
00267     name = name.isEmpty() ? QString() : name;
00268     email = email.isEmpty() ? QString() : email;
00269     uri = uri.isEmpty() ? QString() : uri;
00270 
00271     if (name.isEmpty() && email.isEmpty() && uri.isEmpty())
00272         return PersonPtr(new PersonImpl());
00273    
00274     return PersonPtr(new PersonImpl(name, uri, email));
00275 }
00276 
00277 ElementType::ElementType(const QString& localnamep,
00278             const QString& nsp) : ns(nsp), localname(localnamep)
00279 {
00280 }
00281    
00282 bool ElementType::operator==(const ElementType& other) const
00283 { 
00284     return localname == other.localname && ns == other.ns;
00285 }
00286 
00287 } // namespace Syndication
00288 
00289 

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"
  • kabc
  • kblog
  • kcal
  • kimap
  • kioslave
  •   imap4
  •   mbox
  • kldap
  • kmime
  • kpimidentities
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.5
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