umbrello API Documentation

urlutil.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2003 Julian Rockey <linux@jrockey.com>
00003    Copyright (C) 2003 Alexander Dymo <cloudtemple@mksat.net>
00004    Copyright (C) 2003 Mario Scalas <mario.scalas@libero.it>
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., 59 Temple Place - Suite 330,
00019    Boston, MA 02111-1307, USA.
00020 */
00021 #include "urlutil.h"
00022 
00023 #include <qstringlist.h>
00024 
00025 #include <qdir.h>
00026 #include <qfileinfo.h>
00027 #include <kdebug.h>
00028 
00029 #include <unistd.h>
00030 #include <limits.h>
00031 #include <stdlib.h>
00032 
00033 #include <kdeversion.h>
00034 #if (KDE_VERSION_MINOR==0) && (KDE_VERSION_MAJOR==3)
00035 #include <kdevkurl.h>
00036 #endif
00037 
00039 // Namespace URLUtil
00041 
00042 QString URLUtil::filename(const QString & name) {
00043   int slashPos = name.findRev("/");
00044   return slashPos<0 ? name : name.mid(slashPos+1);
00045 }
00046 
00048 
00049 QString URLUtil::directory(const QString & name) {
00050   int slashPos = name.findRev("/");
00051   return slashPos<0 ? QString("") : name.left(slashPos);
00052 }
00053 
00055 
00056 QString URLUtil::relativePath(const KURL & parent, const KURL & child, uint slashPolicy) {
00057   bool slashPrefix = slashPolicy & SLASH_PREFIX;
00058   bool slashSuffix = slashPolicy & SLASH_SUFFIX;
00059   if (parent == child)
00060     return slashPrefix ? QString("/") : QString("");
00061 
00062   if (!parent.isParentOf(child)) return QString();
00063   int a=slashPrefix ? -1 : 1;
00064   int b=slashSuffix ? 1 : -1;
00065   return child.path(b).mid(parent.path(a).length());
00066 }
00067 
00069 
00070 QString URLUtil::relativePath(const QString & parent, const QString & child, uint slashPolicy) {
00071   return relativePath(KURL(parent), KURL(child), slashPolicy);
00072 }
00073 
00075 
00076 QString URLUtil::upDir(const QString & path, bool slashSuffix) {
00077   int slashPos = path.findRev("/");
00078   if (slashPos<1) return QString::null;
00079   return path.mid(0,slashPos+ (slashSuffix ? 1 : 0) );
00080 }
00081 
00083 
00084 KURL URLUtil::mergeURL(const KURL & source, const KURL & dest, const KURL & child) {
00085 
00086   // if already a child of source, then fine
00087   if (source.isParentOf(child) || source == child) return child;
00088 
00089   // if not a child of dest, return blank URL (error)
00090   if (!dest.isParentOf(child) && dest != child) return KURL();
00091 
00092   // if child is same as dest, return source
00093   if (dest == child) return source;
00094 
00095   // calculate
00096   QString childUrlStr = child.url(-1);
00097   QString destStemStr = dest.url(1);
00098   QString sourceStemStr = source.url(1);
00099   return KURL(sourceStemStr.append( childUrlStr.mid( destStemStr.length() ) ) );
00100 
00101 }
00102 
00104 
00105 QString URLUtil::getExtension(const QString & path) {
00106   int dotPos = path.findRev('.');
00107   if (dotPos<0) return QString("");
00108   return path.mid(dotPos+1);
00109 }
00110 
00112 
00113 QString URLUtil::extractPathNameRelative(const KURL &baseDirUrl, const KURL &url )
00114 {
00115   QString absBase = extractPathNameAbsolute( baseDirUrl ),
00116     absRef = extractPathNameAbsolute( url );
00117   int i = absRef.find( absBase, 0, true );
00118 
00119   if (i == -1)
00120     return QString();
00121 
00122   if (absRef == absBase)
00123     return QString( "." );
00124   else
00125     return absRef.replace( 0, absBase.length(), QString() );
00126 }
00127 
00129 
00130 QString URLUtil::extractPathNameRelative(const QString &basePath, const KURL &url )
00131 {
00132 #if (KDE_VERSION_MINOR!=0) || (KDE_VERSION_MAJOR!=3)
00133   KURL baseDirUrl = KURL::fromPathOrURL( basePath );
00134 #else
00135   KURL baseDirUrl = KdevKURL::fromPathOrURL( basePath );
00136 #endif
00137   return extractPathNameRelative( baseDirUrl, url );
00138 }
00139 
00141 
00142 QString URLUtil::extractPathNameRelative(const QString &basePath, const QString &absFilePath )
00143 {
00144 #if (KDE_VERSION_MINOR!=0) || (KDE_VERSION_MAJOR!=3)
00145   KURL baseDirUrl = KURL::fromPathOrURL( basePath ),
00146        fileUrl = KURL::fromPathOrURL( absFilePath );
00147 #else
00148   KURL baseDirUrl = KdevKURL::fromPathOrURL( basePath ),
00149        fileUrl = KdevKURL::fromPathOrURL( absFilePath );
00150 #endif
00151   return extractPathNameRelative( baseDirUrl, fileUrl );
00152 }
00153 
00155 
00156 QString URLUtil::extractPathNameAbsolute( const KURL &url )
00157 {
00158   if (isDirectory( url ))
00159     return url.path( +1 ); // with trailing "/" if none is present
00160   else
00161   {
00162     // Ok, this is an over-tight pre-condition on "url" since I hope nobody will never
00163     // stress this function with absurd cases ... but who knows?
00164   /*
00165     QString path = url.path();
00166     QFileInfo fi( path );  // Argh: QFileInfo is back ;))
00167     return ( fi.exists()? path : QString() );
00168   */
00169     return url.path();
00170   }
00171 }
00172 
00174 
00175 bool URLUtil::isDirectory( const KURL &url )
00176 {
00177   return isDirectory( url.path() );
00178 }
00179 
00181 
00182 bool URLUtil::isDirectory( const QString &absFilePath )
00183 {
00184   return QDir( absFilePath ).exists();
00185 }
00186 
00188 
00189 void URLUtil::dump( const KURL::List &urls, const QString &aMessage )
00190 {
00191   if (!aMessage.isNull())
00192   {
00193     kdDebug(9000) << aMessage << endl;
00194   }
00195   kdDebug(9000) << " List has " << urls.count() << " elements." << endl;
00196 
00197   for (size_t i = 0; i<urls.count(); ++i)
00198   {
00199     KURL url = urls[ i ];
00200 //    kdDebug(9000) << " * Element = "  << url.path() << endl;
00201   }
00202 }
00203 
00205 
00206 QStringList URLUtil::toRelativePaths( const QString &baseDir, const KURL::List &urls)
00207 {
00208   QStringList paths;
00209 
00210   for (size_t i=0; i<urls.count(); ++i)
00211   {
00212     paths << extractPathNameRelative( baseDir, urls[i] );
00213   }
00214 
00215   return paths;
00216 }
00217 
00219 
00220 QString URLUtil::relativePathToFile( const QString & dirUrl, const QString & fileUrl )
00221 {
00222   if (dirUrl.isEmpty() || (dirUrl == "/"))
00223     return fileUrl;
00224 
00225   QStringList dir = QStringList::split("/", dirUrl, false);
00226   QStringList file = QStringList::split("/", fileUrl, false);
00227 
00228   QString resFileName = file.last();
00229   file.remove(file.last());
00230 
00231   uint i = 0;
00232   while ( (i < dir.count()) && (i < (file.count())) && (dir[i] == file[i]) )
00233     i++;
00234 
00235   QString result_up;
00236   QString result_down;
00237   QString currDir;
00238   QString currFile;
00239   do
00240   {
00241     i >= dir.count() ? currDir = "" : currDir = dir[i];
00242     i >= file.count() ? currFile = "" : currFile = file[i];
00243     qWarning("i = %d, currDir = %s, currFile = %s", i, currDir.latin1(), currFile.latin1());
00244     if (currDir.isEmpty() && currFile.isEmpty())
00245       break;
00246     else if (currDir.isEmpty())
00247       result_down += file[i] + '/';
00248     else if (currFile.isEmpty())
00249       result_up += "../";
00250     else
00251     {
00252       result_down += file[i] + '/';
00253       result_up += "../";
00254     }
00255     i++;
00256   }
00257   while ( (!currDir.isEmpty()) || (!currFile.isEmpty()) );
00258 
00259   return result_up + result_down + resFileName;
00260 }
00261 
00263 
00264 // code from qt-3.1.2 version of QDir::canonicalPath()
00265 QString URLUtil::canonicalPath( const QString & path )
00266 {
00267     QString r;
00268     char cur[PATH_MAX+1];
00269     if ( ::getcwd( cur, PATH_MAX ) )
00270     {
00271         char tmp[PATH_MAX+1];
00272         if( ::realpath( QFile::encodeName( path ), tmp ) )
00273         {
00274             r = QFile::decodeName( tmp );
00275         }
00276         //always make sure we go back to the current dir
00277         ::chdir( cur );
00278     }
00279     return r;
00280 }
00281 
00283 
00284 //written by "Dawit A." <adawit@kde.org>
00285 //borrowed from his patch to KShell
00286 QString URLUtil::envExpand ( const QString& str )
00287 {
00288     uint len = str.length();
00289 
00290     if (len > 1 && str[0] == '$')
00291     {
00292       int pos = str.find ('/');
00293 
00294       if (pos < 0)
00295         pos = len;
00296 
00297       char* ret = getenv( QConstString(str.unicode()+1, pos-1).string().local8Bit().data() );
00298 
00299       if (ret)
00300       {
00301         QString expandedStr ( QFile::decodeName( ret ) );
00302         if (pos < (int)len)
00303           expandedStr += str.mid(pos);
00304         return expandedStr;
00305       }
00306     }
00307 
00308     return str;
00309 }
00310 
KDE Logo
This file is part of the documentation for umbrello Version 3.1.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Jun 26 08:08:02 2007 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003