kioslaves

mbox.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 "mbox.h"
00020 
00021 #include "readmbox.h"
00022 #include "stat.h"
00023 #include "urlinfo.h"
00024 
00025 #include <qstring.h>
00026 #include <qcstring.h>
00027 
00028 #include <kdebug.h>
00029 #include <klocale.h>
00030 #include <kinstance.h>
00031 #include <kglobal.h>
00032 #include <kurl.h>
00033 #include <kio/global.h>
00034 
00035 #include <stdlib.h>
00036 
00037 #include "kdepimmacros.h"
00038 
00039 #include "mbox.h"
00040 
00041 extern "C" { KDE_EXPORT int kdemain(int argc, char* argv[]); }
00042 
00043 int kdemain( int argc, char * argv[] )
00044 {
00045     KLocale::setMainCatalogue("kdelibs");
00046     KInstance instance("kio_mbox");
00047     (void) KGlobal::locale();
00048 
00049     if (argc != 4) {
00050         fprintf(stderr, "Usage: kio_mbox protocol "
00051                         "domain-socket1 domain-socket2\n");
00052         exit(-1);
00053     }
00054 
00055     MBoxProtocol slave(argv[2], argv[3]);
00056     slave.dispatchLoop();
00057     
00058     return 0;
00059 }
00060 
00061 MBoxProtocol::MBoxProtocol( const QCString& arg1, const QCString& arg2 )
00062     : KIO::SlaveBase( "mbox2", arg1, arg2 ),
00063     m_errorState( true )
00064 {
00065     
00066 }
00067 
00068 MBoxProtocol::~MBoxProtocol()
00069 {
00070 }
00071 
00072 void MBoxProtocol::get( const KURL& url )
00073 {
00074     m_errorState = false;
00075     
00076     UrlInfo info( url, UrlInfo::message );
00077     QString line;
00078     QByteArray ba_line;
00079 
00080     if( info.type() == UrlInfo::invalid && !m_errorState )
00081     {
00082         error( KIO::ERR_DOES_NOT_EXIST, info.url() );
00083         return;
00084     }
00085     
00086     ReadMBox mbox( &info, this );
00087 
00088     while( !mbox.atEnd() && !m_errorState)
00089     {
00090         line = mbox.currentLine();
00091         line += '\n';
00092         ba_line = QCString( line.utf8() );
00093         ba_line.truncate( ba_line.size() - 1 ); //Removing training '\0'
00094         data( ba_line );
00095         mbox.nextLine();
00096     };
00097     
00098     if( !m_errorState )
00099     {
00100         data( QByteArray() );
00101         finished();
00102     }
00103 }
00104 
00105 void MBoxProtocol::listDir( const KURL& url )
00106 {
00107     m_errorState = false;
00108     
00109     KIO::UDSEntry entry;
00110     UrlInfo info( url, UrlInfo::directory );
00111     ReadMBox mbox( &info, this, hasMetaData( "onlynew" ), hasMetaData( "savetime" ) );
00112 
00113     if( m_errorState )
00114         return;
00115     
00116     if( info.type() != UrlInfo::directory )
00117     {
00118         error( KIO::ERR_DOES_NOT_EXIST, info.url() );
00119         return;
00120     }
00121     
00122     while( !mbox.atEnd() && !m_errorState )
00123     {
00124         entry = Stat::stat( mbox, info );
00125         if( mbox.inListing() )
00126             listEntry( entry, false );
00127     }
00128 
00129     listEntry( KIO::UDSEntry(), true );
00130     finished();
00131 }
00132 
00133 void MBoxProtocol::stat( const KURL& url )
00134 {
00135     UrlInfo info( url );
00136     if( info.type() == UrlInfo::invalid )
00137     {
00138         error( KIO::ERR_DOES_NOT_EXIST, url.path() );
00139         return;
00140     } else
00141     {
00142         statEntry( Stat::stat( info ) );
00143     }
00144     finished();
00145 }
00146 
00147 void MBoxProtocol::mimetype( const KURL& url )
00148 {   
00149     m_errorState = false;
00150     
00151     UrlInfo info( url );
00152 
00153     if( m_errorState )
00154         return;
00155     
00156     if( info.type() == UrlInfo::invalid )
00157         error( KIO::ERR_DOES_NOT_EXIST, i18n( "Invalid URL" ) );
00158     else
00159         mimeType( info.mimetype() );
00160     finished();
00161 }
00162 
00163 void MBoxProtocol::emitError( int errno, const QString& arg )
00164 {
00165     m_errorState = true;
00166     error( errno, arg );
00167 }
00168