KDECore
kurl.h
Go to the documentation of this file.
00001 // -*- c-basic-offset: 2 -*- 00002 /* This file is part of the KDE libraries 00003 * Copyright (C) 1999 Torben Weis <weis@kde.org> 00004 * Copyright (C) 2005-2006 David Faure <faure@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 #ifndef kurl_h 00023 #define kurl_h 00024 00025 #include <kdecore_export.h> 00026 00027 #include <QtCore/QVariant> 00028 #include <QtCore/QUrl> 00029 #include <QtCore/QMap> 00030 00031 class QStringList; 00032 class QMimeData; 00033 00034 class KUrlPrivate; 00035 00036 // maybe we should encapsulate QUrl instead of inheriting from it. 00037 // this would even allow us to inherit from KUri instead. 00038 // and this way hacks like setPath() would be less ugly, and we could avoid 00039 // half KDE code using setScheme() and the other half using setProtocol(), etc. 00040 // (DF) 00041 00111 class KDECORE_EXPORT KUrl : public QUrl // krazy:exclude=dpointer,qclasses (krazy can't deal with embedded classes) 00112 { 00113 public: 00114 typedef QMap<QString, QString> MetaDataMap; 00115 enum MimeDataFlags { DefaultMimeDataFlags = 0, NoTextExport = 1 }; 00116 00120 enum AdjustPathOption 00121 { 00125 RemoveTrailingSlash, 00126 00130 LeaveTrailingSlash, 00131 00135 AddTrailingSlash 00136 }; 00137 00146 class KDECORE_EXPORT List : public QList<KUrl> //krazy:exclude=dpointer (just some convenience methods) 00147 { 00148 public: 00152 List() { } 00158 List(const KUrl &url); 00164 List(const QStringList &list); 00169 List(const QList<KUrl> &list); 00175 List(const QList<QUrl> &list); 00180 QStringList toStringList() const; 00181 00191 QStringList toStringList(KUrl::AdjustPathOption trailing) const; 00192 00197 operator QVariant() const; 00198 00203 operator QList<QUrl>() const; 00204 00222 void populateMimeData( QMimeData* mimeData, 00223 const KUrl::MetaDataMap& metaData = MetaDataMap(), 00224 MimeDataFlags flags = DefaultMimeDataFlags ) const; 00225 00260 void populateMimeData(const KUrl::List& mostLocalUrls, 00261 QMimeData* mimeData, 00262 const KUrl::MetaDataMap& metaData = MetaDataMap(), 00263 MimeDataFlags flags = DefaultMimeDataFlags) const; 00264 00268 static bool canDecode( const QMimeData *mimeData ); 00269 00273 static QStringList mimeDataTypes(); 00274 00283 static KUrl::List fromMimeData( const QMimeData *mimeData, KUrl::MetaDataMap* metaData = 0 ); 00284 00289 enum DecodeOptions { 00296 PreferLocalUrls, 00303 PreferKdeUrls 00304 }; 00305 00316 static KUrl::List fromMimeData( const QMimeData *mimeData, 00317 DecodeOptions decodeOptions, // TODO KDE5: = PreferKdeUrls, and merge with above 00318 KUrl::MetaDataMap* metaData = 0 ); 00319 00320 }; 00324 KUrl(); 00325 00329 ~KUrl(); 00330 00335 KUrl( const QString& urlOrPath ); 00342 explicit KUrl( const char * urlOrPath ); 00349 explicit KUrl( const QByteArray& urlOrPath ); 00350 00355 KUrl( const KUrl& u ); 00360 KUrl( const QUrl &u ); //krazy:exclude=qclasses 00371 KUrl( const KUrl& _baseurl, const QString& _rel_url ); 00372 00377 QString protocol() const; 00378 00383 void setProtocol( const QString& proto ); 00384 00389 QString user() const; 00390 00397 void setUser( const QString& user ); 00398 00403 bool hasUser() const; 00404 00409 QString pass() const; 00410 00421 void setPass( const QString& pass ); 00422 00427 bool hasPass() const; 00428 00433 bool hasHost() const; 00434 00441 QString path( AdjustPathOption trailing = LeaveTrailingSlash ) const; 00442 00449 QString toLocalFile( AdjustPathOption trailing = LeaveTrailingSlash ) const; 00450 00452 void setPath( const QString& path ); 00453 00458 bool hasPath() const; 00459 00463 enum CleanPathOption 00464 { 00469 SimplifyDirSeparators = 0x00, 00470 00474 KeepDirSeparators = 0x01 00475 }; 00476 00477 Q_DECLARE_FLAGS(CleanPathOptions,CleanPathOption) 00478 00479 00487 void cleanPath(const CleanPathOption& options = SimplifyDirSeparators); 00488 00489 00503 void adjustPath(AdjustPathOption trailing); 00504 00510 void setEncodedPathAndQuery( const QString& _txt ); 00511 00512 #if 0 00513 00518 void setEncodedPath(const QString& _txt ); 00519 #endif 00520 00524 enum EncodedPathAndQueryOption 00525 { 00529 PermitEmptyPath=0x00, 00534 AvoidEmptyPath=0x01 00535 }; 00536 Q_DECLARE_FLAGS( EncodedPathAndQueryOptions, EncodedPathAndQueryOption) 00537 00538 00546 QString encodedPathAndQuery( AdjustPathOption trailing = LeaveTrailingSlash, const EncodedPathAndQueryOptions &options = PermitEmptyPath ) const; 00547 00554 void setQuery( const QString& query ); 00555 00564 QString query() const; 00565 00571 QString ref() const; 00572 00578 void setRef( const QString& fragment ); 00579 00586 bool hasRef() const; 00587 00595 QString htmlRef() const; 00596 00602 QString encodedHtmlRef() const; 00603 00611 void setHTMLRef( const QString& _ref ); 00612 00618 bool hasHTMLRef() const; 00619 00625 bool isLocalFile() const; 00626 00633 void setFileEncoding(const QString &encoding); 00634 00641 QString fileEncoding() const; 00642 00649 bool hasSubUrl() const; 00650 00661 void addPath( const QString& txt ); 00662 00669 enum QueryItemsOption { CaseInsensitiveKeys = 1 }; 00670 Q_DECLARE_FLAGS(QueryItemsOptions,QueryItemsOption) 00671 00672 00684 QMap< QString, QString > queryItems( const QueryItemsOptions& options = 0 ) const; 00685 // #### TODO port the above queryItems to look more like QUrl's 00686 //using QUrl::queryItems; // temporary 00687 00699 QString queryItem(const QString &item) const; 00700 00709 void addQueryItem( const QString& _item, const QString& _value ); 00710 00711 00725 void setFileName( const QString&_txt ); 00726 00730 enum DirectoryOption 00731 { 00741 ObeyTrailingSlash = 0x02, 00750 AppendTrailingSlash = 0x04, 00755 IgnoreTrailingSlash = 0x01 00756 00757 }; 00758 Q_DECLARE_FLAGS(DirectoryOptions,DirectoryOption) 00759 00760 00761 00767 QString fileName( const DirectoryOptions& options = IgnoreTrailingSlash ) const; 00768 00777 QString directory( const DirectoryOptions& options = IgnoreTrailingSlash ) const; 00778 00782 void setDirectory(const QString &dir); 00783 00797 bool cd( const QString& _dir ); 00798 00813 QString url( AdjustPathOption trailing = LeaveTrailingSlash ) const; 00814 00827 QString prettyUrl( AdjustPathOption trailing = LeaveTrailingSlash ) const; 00828 00839 QString pathOrUrl() const; 00845 QString pathOrUrl(AdjustPathOption trailing) const; // KDE5: merge with above. Rename to toUrlOrLocalFile? 00846 00852 QString toMimeDataString() const; 00853 00862 KUrl upUrl( ) const; 00863 00864 KUrl& operator=( const KUrl& _u ); 00865 00866 // Define those, since the constructors are explicit 00867 KUrl& operator=( const char * _url ) { *this = KUrl(_url); return *this; } 00868 KUrl& operator=( const QByteArray& _url ) { *this = KUrl(_url); return *this; } 00869 KUrl& operator=( const QString& _url ) { *this = KUrl(_url); return *this; } 00870 00871 bool operator==( const KUrl& _u ) const; 00872 bool operator==( const QString& _u ) const; 00873 bool operator!=( const KUrl& _u ) const { return !( *this == _u ); } 00874 bool operator!=( const QString& _u ) const { return !( *this == _u ); } 00875 00880 operator QVariant() const; 00881 00893 #ifndef KDE_NO_DEPRECATED 00894 KDE_DEPRECATED bool cmp( const KUrl &u, bool ignore_trailing = false ) const; 00895 #endif 00896 00897 00901 enum EqualsOption 00902 { 00913 CompareWithoutTrailingSlash = 0x01, 00917 CompareWithoutFragment = 0x02, 00927 AllowEmptyPath = 0x04 00928 }; 00929 Q_DECLARE_FLAGS(EqualsOptions,EqualsOption) 00930 00931 00940 bool equals( const KUrl &u, const EqualsOptions& options=0 ) const; 00941 00948 bool isParentOf( const KUrl& u ) const; 00949 // (this overload of the QUrl method allows to use the implicit KUrl constructors) 00950 // but also the equality test 00951 00967 static List split( const QString& _url ); 00968 00984 static List split( const KUrl& _url ); 00985 00993 static KUrl join( const List& _list ); 00994 01004 static KUrl fromPath( const QString& text ); 01005 01011 #ifndef KDE_NO_DEPRECATED 01012 static KDE_DEPRECATED KUrl fromPathOrUrl( const QString& text ); 01013 #endif 01014 01020 static KUrl fromMimeDataByteArray( const QByteArray& str ); 01021 01038 void populateMimeData( QMimeData* mimeData, 01039 const MetaDataMap& metaData = MetaDataMap(), 01040 MimeDataFlags flags = DefaultMimeDataFlags ) const; 01041 01052 #ifndef KDE_NO_DEPRECATED 01053 static KDE_DEPRECATED QString encode_string(const QString &str) { 01054 return QString::fromLatin1( QUrl::toPercentEncoding( str ).constData() ); //krazy:exclude=qclasses 01055 } 01056 #endif 01057 01069 #ifndef KDE_NO_DEPRECATED 01070 static KDE_DEPRECATED QString encode_string_no_slash(const QString &str) { 01071 return QString::fromLatin1( QUrl::toPercentEncoding( str, "/" ).constData() ); //krazy:exclude=qclasses 01072 } 01073 #endif 01074 01085 #ifndef KDE_NO_DEPRECATED 01086 static KDE_DEPRECATED QString decode_string(const QString &str) { 01087 return QUrl::fromPercentEncoding( str.toLatin1() ); //krazy:exclude=qclasses 01088 } 01089 #endif 01090 01091 01111 static bool isRelativeUrl(const QString &_url); 01112 01127 static QString relativeUrl(const KUrl &base_url, const KUrl &url); 01128 01138 static QString relativePath(const QString &base_dir, const QString &path, bool *isParent=0); 01139 01140 private: 01141 void _setQuery( const QString& query ); 01142 void _setEncodedUrl(const QByteArray& url); 01143 QString toString() const; // forbidden, use url(), prettyUrl(), or pathOrUrl() instead. 01144 operator QString() const; // forbidden, use url(), prettyUrl(), or pathOrUrl() instead. 01145 private: 01146 KUrlPrivate* const d; // Don't ever use this, it would break clear() (which is in QUrl) 01147 }; 01148 01149 Q_DECLARE_OPERATORS_FOR_FLAGS(KUrl::EncodedPathAndQueryOptions) 01150 Q_DECLARE_OPERATORS_FOR_FLAGS(KUrl::CleanPathOptions) 01151 Q_DECLARE_OPERATORS_FOR_FLAGS(KUrl::QueryItemsOptions) 01152 Q_DECLARE_OPERATORS_FOR_FLAGS(KUrl::EqualsOptions) 01153 Q_DECLARE_OPERATORS_FOR_FLAGS(KUrl::DirectoryOptions) 01154 01155 Q_DECLARE_METATYPE(KUrl) 01156 Q_DECLARE_METATYPE(KUrl::List) 01157 01166 #ifndef KDE_NO_DEPRECATED 01167 KDECORE_EXPORT_DEPRECATED bool urlcmp( const QString& _url1, const QString& _url2 ); // KDE5: remove, KUrl::equals is better API 01168 #endif 01169 01182 #ifndef KDE_NO_DEPRECATED 01183 KDECORE_EXPORT_DEPRECATED bool urlcmp( const QString& _url1, const QString& _url2, const KUrl::EqualsOptions& options ); // KDE5: remove, KUrl::equals is better API 01184 #endif 01185 01186 KDECORE_EXPORT uint qHash(const KUrl& kurl); 01187 01188 #endif
KDE 4.7 API Reference