kioslaves

urlinfo.cc

Go to the documentation of this file.
00001 /*
00002  * This is a simple kioslave to handle mbox-files.
00003  * Copyright (C) 2004 Mart Kelder (mart.kde@hccnet.nl)
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Lesser General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2 of the License, or (at your option) any later version.
00009  *
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public
00016  * License along with this library; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00018  */
00019 #include "urlinfo.h"
00020 
00021 #include <kdebug.h>
00022 #include <kurl.h>
00023 
00024 #include <qfileinfo.h>
00025 #include <qstring.h>
00026 
00027 UrlInfo::UrlInfo( const KURL& url, const UrlType type )
00028     : m_type( invalid ),
00029     m_filename( new QString ),
00030     m_id( new QString )
00031 {
00032     calculateInfo( url, type );
00033 }
00034 
00035 UrlInfo::~UrlInfo()
00036 {
00037     delete m_filename;
00038     delete m_id;
00039 }
00040 
00041 QString UrlInfo::mimetype() const
00042 {
00043     switch( m_type )
00044     {
00045     case message:
00046         return "message/rfc822";
00047     case directory:
00048         return "inode/directory";
00049     case invalid:
00050     default:
00051         return "invalid";
00052     }
00053 }
00054 
00055 QString UrlInfo::filename() const
00056 {
00057     return *m_filename;
00058 }
00059 
00060 QString UrlInfo::id() const
00061 {
00062     return *m_id;
00063 }
00064 
00065 QString UrlInfo::url() const
00066 {
00067     return *m_filename + "/" + *m_id;
00068 }
00069 
00070 
00071 void UrlInfo::calculateInfo( const KURL& url, const UrlType type )
00072 {
00073     bool found = false;
00074 
00075     if( !found && type & UrlInfo::message )
00076         found = isMessage( url );
00077     if( !found && type & UrlInfo::directory )
00078         found = isDirectory( url );
00079     if( !found )
00080     {
00081         m_type = invalid;
00082         *m_filename = "";
00083         *m_id = "";
00084     }
00085 }
00086 
00087 bool UrlInfo::isDirectory( const KURL& url )
00088 {
00089     //Check is url is in the form mbox://{filename}
00090     QString filename = url.path();
00091     QFileInfo info;
00092 
00093     //Remove ending /
00094     while( filename.length() > 1 && filename.right( 1 ) == "/" )
00095         filename.remove( filename.length()-2, 1 );
00096 
00097     //Is this a directory?
00098     info.setFile( filename );
00099     if( !info.isFile() )
00100         return false;
00101 
00102     //Setting paramaters
00103     *m_filename = filename;
00104     *m_id = QString::null;
00105     m_type = directory;
00106     kdDebug() << "urlInfo::isDirectory( " << url << " )" << endl;
00107     return true;
00108 }
00109 
00110 bool UrlInfo::isMessage( const KURL& url )
00111 {
00112     QString path = url.path();
00113     QFileInfo info;
00114     int cutindex = path.findRev( '/' );
00115     
00116     //Does it contain at least one /?
00117     if( cutindex < 0 )
00118         return false;
00119 
00120     //Does the mbox-file exists?
00121     info.setFile( path.left( cutindex ) );
00122     if( !info.isFile() )
00123         return false;
00124     
00125     //Settings parameters
00126     kdDebug() << "urlInfo::isMessage( " << url << " )" << endl;
00127     m_type = message;
00128     *m_id = path.right( path.length() - cutindex - 1 );
00129     *m_filename = path.left( cutindex );
00130     
00131     return true;
00132 }
00133