kfile-plugins Library API Documentation

kfile_rfc822.cpp

00001 /* This file is part of the KDE project
00002  * Copyright (C) 2002 Shane Wright <me@shanewright.co.uk>
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public
00006  * License as published by the Free Software Foundation version 2.
00007  *
00008  * This program is distributed in the hope that it will be useful,
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011  * General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * along with this program; see the file COPYING.  If not, write to
00015  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00016  * Boston, MA 02111-1307, USA.
00017  *
00018  */
00019 
00020 #include <config.h>
00021 #include "kfile_rfc822.h"
00022 
00023 #include <kprocess.h>
00024 #include <klocale.h>
00025 #include <kgenericfactory.h>
00026 #include <kstringvalidator.h>
00027 #include <kdebug.h>
00028 
00029 #include <qdict.h>
00030 #include <qvalidator.h>
00031 #include <qcstring.h>
00032 #include <qfile.h>
00033 #include <qdatetime.h>
00034 
00035 #if !defined(__osf__)
00036 #include <inttypes.h>
00037 #else
00038 typedef unsigned short uint32_t;
00039 #endif
00040 
00041 typedef KGenericFactory<KRfc822Plugin> Rfc822Factory;
00042 
00043 K_EXPORT_COMPONENT_FACTORY(kfile_rfc822, Rfc822Factory( "kfile_rfc822" ))
00044 
00045 KRfc822Plugin::KRfc822Plugin(QObject *parent, const char *name,
00046                        const QStringList &args)
00047 
00048     : KFilePlugin(parent, name, args)
00049 {
00050     KFileMimeTypeInfo* info = addMimeTypeInfo( "message/rfc822" );
00051 
00052     KFileMimeTypeInfo::GroupInfo* group = 0L;
00053 
00054     group = addGroupInfo(info, "Technical", i18n("Technical Details"));
00055 
00056     KFileMimeTypeInfo::ItemInfo* item;
00057 
00058     item = addItemInfo(group, "From", i18n("From"), QVariant::String);
00059     item = addItemInfo(group, "To", i18n("To"), QVariant::String);
00060     item = addItemInfo(group, "Subject", i18n("Subject"), QVariant::String);
00061     item = addItemInfo(group, "Date", i18n("Date"), QVariant::String);
00062     item = addItemInfo(group, "Content-Type", i18n("Content-Type"), QVariant::String);
00063 }
00064 
00065 
00066 bool KRfc822Plugin::readInfo( KFileMetaInfo& info, uint /*what*/ )
00067 {
00068 
00069     QFile file(info.path());
00070 
00071     if (!file.open(IO_ReadOnly))
00072     {
00073         kdDebug(7034) << "Couldn't open " << QFile::encodeName(info.path()) << endl;
00074         return false;
00075     }
00076 
00077     /*
00078          Note to self: probably should use QCString for all this, but
00079          what we're doing is simple and self-contained so never mind..
00080     */
00081 
00082     char id_from[] = "From: ";
00083     char id_to[] = "To: ";
00084     char id_subject[] = "Subject: ";
00085     char id_date[] = "Date: ";
00086     char id_contenttype[] = "Content-Type: ";
00087 
00088     // we need a buffer for lines
00089     char linebuf[4096];
00090 
00091     // we need a buffer for other stuff
00092     char buf_from[1000] = "";
00093     char buf_to[1000] = "";
00094     char buf_subject[1000] = "";
00095     char buf_date[1000] = "";
00096     char buf_contenttype[1000] = "";
00097 
00098     memset(buf_from, 0, 999);
00099     memset(buf_to, 0, 999);
00100     memset(buf_subject, 0, 999);
00101     memset(buf_date, 0, 999);
00102     memset(buf_contenttype, 0, 999);
00103     char * myptr;
00104 
00105     bool done=false;
00106     while (!done) {
00107 
00108         // read a line
00109         file.readLine(linebuf, 4095);
00110 
00111         // have we got something useful?
00112         if (memcmp(linebuf, id_from, 6) == 0) {
00113             // we have a name
00114             myptr = linebuf + 6;
00115             strncpy(buf_from, myptr, 999);
00116             buf_from[998]='\0';
00117         } else if (memcmp(linebuf, id_to, 4) == 0) {
00118             // we have a name
00119             myptr = linebuf + 4;
00120             strncpy(buf_to, myptr, 999);
00121             buf_to[998]='\0';
00122         } else if (memcmp(linebuf, id_subject, 9) == 0) {
00123             // we have a name
00124             myptr = linebuf + 9;
00125             strncpy(buf_subject, myptr, 999);
00126             buf_subject[998]='\0';
00127         } else if (memcmp(linebuf, id_date, 6) == 0) {
00128             // we have a name
00129             myptr = linebuf + 6;
00130             strncpy(buf_date, myptr, 999);
00131             buf_date[998]='\0';
00132         } else if (memcmp(linebuf, id_contenttype, 14) == 0) {
00133             // we have a name
00134             myptr = linebuf + 14;
00135             strncpy(buf_contenttype, myptr, 999);
00136             buf_contenttype[998]='\0';
00137         }
00138 
00139         // are we done yet?
00140         if (
00141           ((strlen(buf_from) > 0) && (strlen(buf_to) > 0) &&
00142           (strlen(buf_subject) > 0) && (strlen(buf_date) > 0) &&
00143           (strlen(buf_contenttype) > 0)) ||
00144           (file.atEnd())
00145           )
00146             done = true;
00147 
00148     };
00149 
00150     KFileMetaInfoGroup group = appendGroup(info, "Technical");
00151 
00152     if (strlen(buf_from) > 0)           appendItem(group, "From", buf_from);
00153     if (strlen(buf_to) > 0)             appendItem(group, "To", buf_to);
00154     if (strlen(buf_subject) > 0)        appendItem(group, "Subject", buf_subject);
00155     if (strlen(buf_date) > 0)           appendItem(group, "Date", buf_date);
00156     if (strlen(buf_contenttype) > 0)    appendItem(group, "Content-Type", buf_contenttype);
00157 
00158     return true;
00159 }
00160 
00161 #include "kfile_rfc822.moc"
KDE Logo
This file is part of the documentation for kfile-plugins Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:37:47 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003