diff options
Diffstat (limited to 'kioslaves/mbox')
-rw-r--r-- | kioslaves/mbox/AUTHORS | 1 | ||||
-rw-r--r-- | kioslaves/mbox/Makefile.am | 30 | ||||
-rw-r--r-- | kioslaves/mbox/README | 7 | ||||
-rw-r--r-- | kioslaves/mbox/mbox.cc | 168 | ||||
-rw-r--r-- | kioslaves/mbox/mbox.h | 81 | ||||
-rw-r--r-- | kioslaves/mbox/mbox.protocol | 14 | ||||
-rw-r--r-- | kioslaves/mbox/mboxfile.cc | 45 | ||||
-rw-r--r-- | kioslaves/mbox/mboxfile.h | 68 | ||||
-rw-r--r-- | kioslaves/mbox/readmbox.cc | 204 | ||||
-rw-r--r-- | kioslaves/mbox/readmbox.h | 132 | ||||
-rw-r--r-- | kioslaves/mbox/stat.cc | 115 | ||||
-rw-r--r-- | kioslaves/mbox/stat.h | 82 | ||||
-rw-r--r-- | kioslaves/mbox/urlinfo.cc | 133 | ||||
-rw-r--r-- | kioslaves/mbox/urlinfo.h | 82 |
14 files changed, 1162 insertions, 0 deletions
diff --git a/kioslaves/mbox/AUTHORS b/kioslaves/mbox/AUTHORS new file mode 100644 index 000000000..333010f6c --- /dev/null +++ b/kioslaves/mbox/AUTHORS @@ -0,0 +1 @@ +Mart Kelder <mart.kde@hccnet.nl> diff --git a/kioslaves/mbox/Makefile.am b/kioslaves/mbox/Makefile.am new file mode 100644 index 000000000..b4840ee72 --- /dev/null +++ b/kioslaves/mbox/Makefile.am @@ -0,0 +1,30 @@ +INCLUDES= $(all_includes) + +####### Files + +METASOURCES = AUTO + +kde_module_LTLIBRARIES = kio_mbox.la + +kio_mbox_la_SOURCES = \ + mbox.cc \ + mboxfile.cc \ + readmbox.cc \ + stat.cc \ + urlinfo.cc +kio_mbox_la_LIBADD = $(LIB_KIO) +kio_mbox_la_LDFLAGS = $(all_libraries) $(KDE_RPATH) -module $(KDE_PLUGIN) + +noinst_HEADERS = \ + mbox.h \ + mboxfile.h \ + readmbox.h \ + stat.h \ + urlinfo.h + +kdelnk_DATA = mbox.protocol +kdelnkdir = $(kde_servicesdir) + + +include $(top_srcdir)/admin/Doxyfile.am + diff --git a/kioslaves/mbox/README b/kioslaves/mbox/README new file mode 100644 index 000000000..f0441661c --- /dev/null +++ b/kioslaves/mbox/README @@ -0,0 +1,7 @@ +This is a simple kioslave for accessing mbox-files with kio. + +At the moment, it doesn't support locking or writing, it is just reading and listing messages. + +Usage: + mbox:/path/to/mbox/file For listing the files in a mbox-file + mbox:/path/to/mbox/file/id For getting an id out of a mbox-file. diff --git a/kioslaves/mbox/mbox.cc b/kioslaves/mbox/mbox.cc new file mode 100644 index 000000000..8ad8d09ab --- /dev/null +++ b/kioslaves/mbox/mbox.cc @@ -0,0 +1,168 @@ +/* + * This is a simple kioslave to handle mbox-files. + * Copyright (C) 2004 Mart Kelder (mart.kde@hccnet.nl) + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include "mbox.h" + +#include "readmbox.h" +#include "stat.h" +#include "urlinfo.h" + +#include <qstring.h> +#include <qcstring.h> + +#include <kdebug.h> +#include <klocale.h> +#include <kinstance.h> +#include <kglobal.h> +#include <kurl.h> +#include <kio/global.h> + +#include <stdlib.h> + +#include "kdepimmacros.h" + +#include "mbox.h" + +extern "C" { KDE_EXPORT int kdemain(int argc, char* argv[]); } + +int kdemain( int argc, char * argv[] ) +{ + KLocale::setMainCatalogue("kdelibs"); + KInstance instance("kio_mbox"); + (void) KGlobal::locale(); + + if (argc != 4) { + fprintf(stderr, "Usage: kio_mbox protocol " + "domain-socket1 domain-socket2\n"); + exit(-1); + } + + MBoxProtocol slave(argv[2], argv[3]); + slave.dispatchLoop(); + + return 0; +} + +MBoxProtocol::MBoxProtocol( const QCString& arg1, const QCString& arg2 ) + : KIO::SlaveBase( "mbox2", arg1, arg2 ), + m_errorState( true ) +{ + +} + +MBoxProtocol::~MBoxProtocol() +{ +} + +void MBoxProtocol::get( const KURL& url ) +{ + m_errorState = false; + + UrlInfo info( url, UrlInfo::message ); + QString line; + QByteArray ba_line; + + if( info.type() == UrlInfo::invalid && !m_errorState ) + { + error( KIO::ERR_DOES_NOT_EXIST, info.url() ); + return; + } + + ReadMBox mbox( &info, this ); + + while( !mbox.atEnd() && !m_errorState) + { + line = mbox.currentLine(); + line += '\n'; + ba_line = QCString( line.utf8() ); + ba_line.truncate( ba_line.size() - 1 ); //Removing training '\0' + data( ba_line ); + mbox.nextLine(); + }; + + if( !m_errorState ) + { + data( QByteArray() ); + finished(); + } +} + +void MBoxProtocol::listDir( const KURL& url ) +{ + m_errorState = false; + + KIO::UDSEntry entry; + UrlInfo info( url, UrlInfo::directory ); + ReadMBox mbox( &info, this, hasMetaData( "onlynew" ), hasMetaData( "savetime" ) ); + + if( m_errorState ) + return; + + if( info.type() != UrlInfo::directory ) + { + error( KIO::ERR_DOES_NOT_EXIST, info.url() ); + return; + } + + while( !mbox.atEnd() && !m_errorState ) + { + entry = Stat::stat( mbox, info ); + if( mbox.inListing() ) + listEntry( entry, false ); + } + + listEntry( KIO::UDSEntry(), true ); + finished(); +} + +void MBoxProtocol::stat( const KURL& url ) +{ + UrlInfo info( url ); + if( info.type() == UrlInfo::invalid ) + { + error( KIO::ERR_DOES_NOT_EXIST, url.path() ); + return; + } else + { + statEntry( Stat::stat( info ) ); + } + finished(); +} + +void MBoxProtocol::mimetype( const KURL& url ) +{ + m_errorState = false; + + UrlInfo info( url ); + + if( m_errorState ) + return; + + if( info.type() == UrlInfo::invalid ) + error( KIO::ERR_DOES_NOT_EXIST, i18n( "Invalid URL" ) ); + else + mimeType( info.mimetype() ); + finished(); +} + +void MBoxProtocol::emitError( int errno, const QString& arg ) +{ + m_errorState = true; + error( errno, arg ); +} + diff --git a/kioslaves/mbox/mbox.h b/kioslaves/mbox/mbox.h new file mode 100644 index 000000000..4d3768bd4 --- /dev/null +++ b/kioslaves/mbox/mbox.h @@ -0,0 +1,81 @@ +/* + * This is a simple kioslave to handle mbox-files. + * Copyright (C) 2004 Mart Kelder (mart.kde@hccnet.nl) + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#ifndef MBOX_H +#define MBOX_H + +#include <kio/slavebase.h> + +class QCString; +class KURL; + +/** + * This class is the main class and implements all function + * which can be called through the user. + */ +class MBoxProtocol : public KIO::SlaveBase +{ +public: + /** + * Constructor, for the parameters, See KIO::SlaveBase + */ + MBoxProtocol( const QCString&, const QCString& ); + /** + * Empty destructor + */ + virtual ~MBoxProtocol(); + + /** + * This functions is used when an user or a program wants to + * get a file from a mbox-file + * @param url The url which points to the virtual file to get + */ + virtual void get( const KURL& url ); + + /** + * This functions gives a listing back. + * @param url The url to the mbox-file. + */ + virtual void listDir( const KURL& url ); + + /** + * This functions gives general properties about a mbox-file, + * or mbox-email back. + */ + virtual void stat( const KURL& url ); + + /** + * This functions determinate the mimetype of a given mbox-file or mbox-email. + * @param url The url to get the mimetype from + */ + virtual void mimetype( const KURL& url ); + + /** + * Through this functions, other class which have an instance to this + * class (classes which are part of kio_mbox) can emit an error with + * this function + * @param errno The error number to be thrown + * @param arg The argument of the error message of the error number. + */ + void emitError( int errno, const QString& arg ); +private: + bool m_errorState; +}; + +#endif + diff --git a/kioslaves/mbox/mbox.protocol b/kioslaves/mbox/mbox.protocol new file mode 100644 index 000000000..8b6b412a8 --- /dev/null +++ b/kioslaves/mbox/mbox.protocol @@ -0,0 +1,14 @@ +[Protocol] +exec=kio_mbox +protocol=mbox +input=none +output=filesystem +listing=Name,Type,Size +reading=true +writing=false +makedir=false +deleting=false +Icon=folder_inbox +maxInstances=2 +DocPath= +Class=:local diff --git a/kioslaves/mbox/mboxfile.cc b/kioslaves/mbox/mboxfile.cc new file mode 100644 index 000000000..271b71019 --- /dev/null +++ b/kioslaves/mbox/mboxfile.cc @@ -0,0 +1,45 @@ +/* + * This is a simple kioslave to handle mbox-files. + * Copyright (C) 2004 Mart Kelder (mart.kde@hccnet.nl) + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include "mboxfile.h" + +#include <assert.h> + +MBoxFile::MBoxFile( const UrlInfo* info, MBoxProtocol* parent ) + : m_info( info ), + m_mbox( parent ) +{ + assert( m_info ); +} + +MBoxFile::~MBoxFile() +{ +} + +bool MBoxFile::lock() +{ + //Not implemented + return true; +} + +void MBoxFile::unlock() +{ + //Not implemented +} + + diff --git a/kioslaves/mbox/mboxfile.h b/kioslaves/mbox/mboxfile.h new file mode 100644 index 000000000..b8a98973e --- /dev/null +++ b/kioslaves/mbox/mboxfile.h @@ -0,0 +1,68 @@ +/* + * This is a simple kioslave to handle mbox-files. + * Copyright (C) 2004 Mart Kelder (mart.kde@hccnet.nl) + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#ifndef MBOXFILE_H +#define MBOXFILE_H +class MBoxProtocol; +class UrlInfo; + +/** + * This class can be used to lock files when implemented. + * It is a base class for all classes that needs locking and/ir + * an UrlInfo*. + */ +class MBoxFile +{ +public: + /** + * Constructor + * @param info The urlinfo which must be used + * @param parent The MBoxProtocol parent instance, used to throw errors. + */ + MBoxFile( const UrlInfo* info, MBoxProtocol* parent ); + + /** + * Empty destructor + */ + ~MBoxFile(); + +protected: + /** + * When implemented, this function handles the locking of the file. + * @return true if the locking was done succesfully. + */ + bool lock(); + + /** + * When implemented, this function unlocks the file. + */ + void unlock(); + +protected: + /** + * This can be used to get information about the file. + * The file specified here is the file that must be used. + */ + const UrlInfo* const m_info; + + /** + * A instance of the parent protocol, meant to throw errors if neccesairy. + */ + MBoxProtocol* const m_mbox; +}; +#endif diff --git a/kioslaves/mbox/readmbox.cc b/kioslaves/mbox/readmbox.cc new file mode 100644 index 000000000..c7513c6eb --- /dev/null +++ b/kioslaves/mbox/readmbox.cc @@ -0,0 +1,204 @@ +/* + * This is a simple kioslave to handle mbox-files. + * Copyright (C) 2004 Mart Kelder (mart.kde@hccnet.nl) + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include <config.h> + +#include "readmbox.h" + +#include "mbox.h" +#include "urlinfo.h" + +#include <kdebug.h> +#include <kio/global.h> + +#include <qfile.h> +#include <qfileinfo.h> +#include <qstring.h> +#include <qtextstream.h> + +#ifdef HAVE_SYS_TYPES_H +#include <sys/types.h> +#endif + +#include <utime.h> + +ReadMBox::ReadMBox( const UrlInfo* info, MBoxProtocol* parent, bool onlynew, bool savetime ) + : MBoxFile( info, parent ), + m_file( 0 ), + m_stream( 0 ), + m_current_line( new QString( QString::null ) ), + m_current_id( new QString( QString::null ) ), + m_atend( true ), + m_prev_time( 0 ), + m_only_new( onlynew ), + m_savetime( savetime ), + m_status( false ), + m_prev_status( false ), + m_header( true ) + +{ + if( m_info->type() == UrlInfo::invalid ) + m_mbox->emitError( KIO::ERR_DOES_NOT_EXIST, info->url() ); + + if( !open( savetime ) ) + m_mbox->emitError( KIO::ERR_CANNOT_OPEN_FOR_READING, info->url() ); + + if( m_info->type() == UrlInfo::message ) + if( !searchMessage( m_info->id() ) ) + m_mbox->emitError( KIO::ERR_DOES_NOT_EXIST, info->url() ); +} + +ReadMBox::~ReadMBox() +{ + delete m_current_line; + close(); +} + +QString ReadMBox::currentLine() const +{ + return *m_current_line; +} + +QString ReadMBox::currentID() const +{ + return *m_current_id; +} + +bool ReadMBox::nextLine() +{ + if( !m_stream ) + return true; + + *m_current_line = m_stream->readLine(); + m_atend = m_current_line->isNull(); + if( m_atend ) // Cursor was at EOF + { + *m_current_id = QString::null; + m_prev_status = m_status; + return true; + } + + //New message + if( m_current_line->left( 5 ) == "From " ) + { + *m_current_id = *m_current_line; + m_prev_status = m_status; + m_status = true; + m_header = true; + return true; + } else if( m_only_new ) + { + if( m_header && m_current_line->left( 7 ) == "Status:" && + ! m_current_line->contains( "U" ) && ! m_current_line->contains( "N" ) ) + { + m_status = false; + } + } + + if( m_current_line->stripWhiteSpace().isEmpty() ) + m_header = false; + + return false; +} + +bool ReadMBox::searchMessage( const QString& id ) +{ + if( !m_stream ) + return false; + + while( !m_atend && *m_current_id != id ) + nextLine(); + + return *m_current_id == id; +} + +unsigned int ReadMBox::skipMessage() +{ + unsigned int result = m_current_line->length(); + + if( !m_stream ) + return 0; + + while( !nextLine() ) + result += m_current_line->length(); + + return result; +} + +void ReadMBox::rewind() +{ + if( !m_stream ) + return; //Rewinding not possible + + m_stream->device()->reset(); + m_atend = m_stream->atEnd(); +} + +bool ReadMBox::atEnd() const +{ + if( !m_stream ) + return true; + + return m_atend || ( m_info->type() == UrlInfo::message && *m_current_id != m_info->id() ); +} + +bool ReadMBox::inListing() const +{ + return !m_only_new || m_prev_status; +} + +bool ReadMBox::open( bool savetime ) +{ + if( savetime ) + { + QFileInfo info( m_info->filename() ); + + m_prev_time = new utimbuf; + m_prev_time->actime = info.lastRead().toTime_t(); + m_prev_time->modtime = info.lastModified().toTime_t(); + } + + if( m_file ) + return false; //File already open + + m_file = new QFile( m_info->filename() ); + if( !m_file->open( IO_ReadOnly ) ) + { + delete m_file; + m_file = 0; + return false; + } + m_stream = new QTextStream( m_file ); + skipMessage(); + + return true; +} + +void ReadMBox::close() +{ + if( !m_stream ) + return; + + delete m_stream; m_stream = 0; + m_file->close(); + delete m_file; m_file = 0; + + if( m_prev_time ) + utime( QFile::encodeName( m_info->filename() ), m_prev_time ); +} + diff --git a/kioslaves/mbox/readmbox.h b/kioslaves/mbox/readmbox.h new file mode 100644 index 000000000..01b813df7 --- /dev/null +++ b/kioslaves/mbox/readmbox.h @@ -0,0 +1,132 @@ +/* + * This is a simple kioslave to handle mbox-files. + * Copyright (C) 2004 Mart Kelder (mart.kde@hccnet.nl) + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#ifndef READMBOX_H +#define READMBOX_H + +#include "mboxfile.h" + +class UrlInfo; +class MBox; + +class QFile; +class QString; +class QTextStream; + +struct utimbuf; + +/** + * This class handels reading from a mbox-file. + */ +class ReadMBox : public MBoxFile +{ +public: + /** + * Constructor + * + * @param info The information of the file to read + * @param parent The instance of the parent MBoxProtocol. + * @param onlynew Only read new messages from the MBox file. + * @param savetime If true, the atime of the mbox-file is preserved (note that this touch the ctime). + */ + ReadMBox( const UrlInfo* info, MBoxProtocol* parent, bool onlynew = false, bool savetime = false ); + + /** + * Destructor + */ + ~ReadMBox(); + + /** + * This functions return the current line + * @return The line last read, or QString::null if there wasn't such last line + */ + QString currentLine() const; + + /** + * This function returns the current id. The id is the first line of an email, + * and is used in filenaming. The id normally starts with "From ". + * @return The current ID, or QString::null if no id was found yet. + */ + QString currentID() const; + + /** + * This function reads the next line. The next line can be read by the currentLine() + * function call. + * + * @return true if succesfull, otherwise false. + */ + bool nextLine(); + + /** + * This function search the file for a certain id. + * If not found, the position is EOF. + * @param id The id of the message to be found. + * @return true if the message was found, false otherwise. + */ + bool searchMessage( const QString& id ); + + /** + * Skips all lines which belongs to the current message. The cursor is on the first line + * of a new message message at the end of this function, or at EOF if the cursor was already + * on the last message. + * @return The number of bytes read while skipping the message. + */ + unsigned int skipMessage(); + + /** + * Sets the cursor back to the beginning of the file + */ + void rewind(); + + /** + * Returns true if the cursor is at EOF. + * @return true if and only if the cursor is at EOF. + */ + bool atEnd() const; + + /** + * Return true if the message is a new message, or all messages are listed + * @return true if it must be listed + */ + bool inListing() const; +private: + /** + * Opens a file + * @return true Returns true if opening was succesful. + */ + bool open( bool savetime ); + + /** + * Closes a file. + */ + void close(); + +private: + QFile* m_file; + QTextStream* m_stream; + QString* m_current_line; + QString* m_current_id; + bool m_atend; + + struct utimbuf* m_prev_time; + + bool m_only_new, m_savetime; + + bool m_status, m_prev_status, m_header; +}; +#endif diff --git a/kioslaves/mbox/stat.cc b/kioslaves/mbox/stat.cc new file mode 100644 index 000000000..e3418cbbd --- /dev/null +++ b/kioslaves/mbox/stat.cc @@ -0,0 +1,115 @@ +/* + * This is a simple kioslave to handle mbox-files. + * Copyright (C) 2004 Mart Kelder (mart.kde@hccnet.nl) + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include "stat.h" + +#include "readmbox.h" +#include "urlinfo.h" + +#include <kdebug.h> +#include <kio/global.h> + +#include <sys/stat.h> + +KIO::UDSEntry Stat::stat( const UrlInfo& info ) +{ + if( info.type() == UrlInfo::message ) + return Stat::statMessage( info ); + else if( info.type() == UrlInfo::directory ) + return Stat::statDirectory( info ); + else + return KIO::UDSEntry(); +} + +KIO::UDSEntry Stat::stat( ReadMBox& mbox, const UrlInfo& info ) +{ + kdDebug() << "Stat::stat()" << endl; + KIO::UDSEntry entry; + QString url; + + if( info.type() == UrlInfo::invalid ) + return entry; + else if( info.type() == UrlInfo::message ) + mbox.searchMessage( info.id() ); + + Stat::addAtom( entry, KIO::UDS_FILE_TYPE, S_IFREG ); + Stat::addAtom( entry, KIO::UDS_MIME_TYPE, "message/rfc822" ); + + url = QString( "mbox:%1/%2" ).arg( info.filename(), mbox.currentID() ); + Stat::addAtom( entry, KIO::UDS_URL, url ); + if( mbox.currentID().isEmpty() ) + Stat::addAtom( entry, KIO::UDS_NAME, "foobar" ); + else + Stat::addAtom( entry, KIO::UDS_NAME, mbox.currentID() ); + + + Stat::addAtom( entry, KIO::UDS_SIZE, mbox.skipMessage() ); + + return entry; +} + +KIO::UDSEntry Stat::statDirectory( const UrlInfo& info ) +{ + kdDebug() << "statDirectory()" << endl; + KIO::UDSEntry entry; + + //Specific things for a directory + Stat::addAtom( entry, KIO::UDS_FILE_TYPE, S_IFDIR ); + Stat::addAtom( entry, KIO::UDS_NAME, info.filename() ); + + return entry; +} + +KIO::UDSEntry Stat::statMessage( const UrlInfo& info ) +{ + kdDebug() << "statMessage( " << info.url() << " )" << endl; + KIO::UDSEntry entry; + QString url = QString( "mbox:%1" ).arg( info.url() ); + + //Specific things for a message + Stat::addAtom( entry, KIO::UDS_FILE_TYPE, S_IFREG ); + Stat::addAtom( entry, KIO::UDS_MIME_TYPE, "message/rfc822" ); + + Stat::addAtom( entry, KIO::UDS_URL, url ); + url = url.right( url.length() - url.findRev( "/" ) - 1 ); + Stat::addAtom( entry, KIO::UDS_NAME, url ); + + return entry; +} + +void Stat::addAtom( KIO::UDSEntry& entry, unsigned int uds, const QString& str ) +{ + KIO::UDSAtom atom; + atom.m_uds = uds; + atom.m_str = str; + atom.m_long = 0; + + entry.append( atom ); +} + + +void Stat::addAtom( KIO::UDSEntry& entry, unsigned int uds, long lng ) +{ + KIO::UDSAtom atom; + atom.m_uds = uds; + atom.m_str = QString::null; + atom.m_long = lng; + + entry.append( atom ); +} + diff --git a/kioslaves/mbox/stat.h b/kioslaves/mbox/stat.h new file mode 100644 index 000000000..f26f4338d --- /dev/null +++ b/kioslaves/mbox/stat.h @@ -0,0 +1,82 @@ +/* + * This is a simple kioslave to handle mbox-files. + * Copyright (C) 2004 Mart Kelder (mart.kde@hccnet.nl) + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#ifndef STAT_H +#define STAT_H + +#include <kio/global.h> + +class ReadMBox; +class UrlInfo; + +class KURL; + +class QString; + +/** + * This class is used to get the stats of a mbox-email or mbox-file. + * This class only uses static members. + */ +class Stat +{ +public: + /** + * Empty constructor + */ + Stat() {} + + /** + * Emtpy destructor + */ + ~Stat() {} + + /** + * This functions gives information with a given UrlInfo. + * @param info The file information + * @return The information of the file as destribed in UrlInfo. + */ + static KIO::UDSEntry stat( const UrlInfo& info ); + /** + * This function gives information with a given ReadMBox and UrlInfo. + * Through this, it is possible to ask the stats of the next message, + * without reopening the mbox-file. + * @param mbox The ReadMBox instance, used to search the mbox-email in. + * @param info The url information. + * @return The requesteds information. + */ + static KIO::UDSEntry stat( ReadMBox& mbox, const UrlInfo& info ); + + /** + * This function gets the stats of a given mbox-file in an UDSEntry. + * @param info The location of the mbox-file. + * @return A list of Atoms. + */ + static KIO::UDSEntry statDirectory( const UrlInfo& info ); + + /** + * This function gets the stats of a geven mbox-message in a UDSEntry. + * @param info The url of the mbox-message. + * @return Information shipped in an UDSEntry. + */ + static KIO::UDSEntry statMessage( const UrlInfo& info ); +private: + static void addAtom( KIO::UDSEntry& entry, unsigned int key, const QString& value ); + static void addAtom( KIO::UDSEntry& entry, unsigned int key, const long value ); +}; + +#endif diff --git a/kioslaves/mbox/urlinfo.cc b/kioslaves/mbox/urlinfo.cc new file mode 100644 index 000000000..710dc6f0c --- /dev/null +++ b/kioslaves/mbox/urlinfo.cc @@ -0,0 +1,133 @@ +/* + * This is a simple kioslave to handle mbox-files. + * Copyright (C) 2004 Mart Kelder (mart.kde@hccnet.nl) + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include "urlinfo.h" + +#include <kdebug.h> +#include <kurl.h> + +#include <qfileinfo.h> +#include <qstring.h> + +UrlInfo::UrlInfo( const KURL& url, const UrlType type ) + : m_type( invalid ), + m_filename( new QString ), + m_id( new QString ) +{ + calculateInfo( url, type ); +} + +UrlInfo::~UrlInfo() +{ + delete m_filename; + delete m_id; +} + +QString UrlInfo::mimetype() const +{ + switch( m_type ) + { + case message: + return "message/rfc822"; + case directory: + return "inode/directory"; + case invalid: + default: + return "invalid"; + } +} + +QString UrlInfo::filename() const +{ + return *m_filename; +} + +QString UrlInfo::id() const +{ + return *m_id; +} + +QString UrlInfo::url() const +{ + return *m_filename + "/" + *m_id; +} + + +void UrlInfo::calculateInfo( const KURL& url, const UrlType type ) +{ + bool found = false; + + if( !found && type & UrlInfo::message ) + found = isMessage( url ); + if( !found && type & UrlInfo::directory ) + found = isDirectory( url ); + if( !found ) + { + m_type = invalid; + *m_filename = ""; + *m_id = ""; + } +} + +bool UrlInfo::isDirectory( const KURL& url ) +{ + //Check is url is in the form mbox://{filename} + QString filename = url.path(); + QFileInfo info; + + //Remove ending / + while( filename.length() > 1 && filename.right( 1 ) == "/" ) + filename.remove( filename.length()-2, 1 ); + + //Is this a directory? + info.setFile( filename ); + if( !info.isFile() ) + return false; + + //Setting paramaters + *m_filename = filename; + *m_id = QString::null; + m_type = directory; + kdDebug() << "urlInfo::isDirectory( " << url << " )" << endl; + return true; +} + +bool UrlInfo::isMessage( const KURL& url ) +{ + QString path = url.path(); + QFileInfo info; + int cutindex = path.findRev( '/' ); + + //Does it contain at least one /? + if( cutindex < 0 ) + return false; + + //Does the mbox-file exists? + info.setFile( path.left( cutindex ) ); + if( !info.isFile() ) + return false; + + //Settings parameters + kdDebug() << "urlInfo::isMessage( " << url << " )" << endl; + m_type = message; + *m_id = path.right( path.length() - cutindex - 1 ); + *m_filename = path.left( cutindex ); + + return true; +} + diff --git a/kioslaves/mbox/urlinfo.h b/kioslaves/mbox/urlinfo.h new file mode 100644 index 000000000..c3177db5e --- /dev/null +++ b/kioslaves/mbox/urlinfo.h @@ -0,0 +1,82 @@ +/* + * This is a simple kioslave to handle mbox-files. + * Copyright (C) 2004 Mart Kelder (mart.kde@hccnet.nl) + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#ifndef URLINFO_H +#define URLINFO_H +class KURL; + +class QString; + +class UrlInfo +{ +public: + /** + * This enum is used to determe the url type. + */ + enum UrlType { invalid = 0, message = 1, directory = 2 }; + + /** + * Constructor + * + * @param url The url: this url is used to split the location data off. + * @param type The possible types of the url + */ + UrlInfo( const KURL &url, const UrlType type = (UrlType)( message | directory ) ); + + /** + * Destructor + */ + ~UrlInfo(); + + /** + * Returns the type of the url + * @return the type of the url + */ + UrlType type() const { return m_type; } + + /** + * @return the mimetype of the url + */ + QString mimetype() const; + + /** + * @return The location of the mbox-file + */ + QString filename() const; + /** + * @return The id given in the url. + */ + QString id() const; + + /** + * @return the while url as QString + */ + QString url() const; +private: + void calculateInfo( const KURL& url, const UrlType type ); + + bool isDirectory( const KURL& url ); + bool isMessage( const KURL& url ); + +private: + UrlType m_type; + QString *m_filename; + QString *m_id; +}; + +#endif |