kfile-plugins Library API Documentation

kfile_vcf.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 <kdebug.h>
00021 #include <config.h>
00022 #include "kfile_vcf.h"
00023 
00024 #include <kprocess.h>
00025 #include <klocale.h>
00026 #include <kgenericfactory.h>
00027 #include <kstringvalidator.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<KVcfPlugin> VcfFactory;
00042 
00043 K_EXPORT_COMPONENT_FACTORY(kfile_vcf, VcfFactory( "kfile_vcf" ))
00044 
00045 KVcfPlugin::KVcfPlugin(QObject *parent, const char *name,
00046                        const QStringList &args)
00047 
00048     : KFilePlugin(parent, name, args)
00049 {
00050     KFileMimeTypeInfo* info = addMimeTypeInfo( "text/x-vcard" );
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, "Name", i18n("Name"), QVariant::String);
00059     item = addItemInfo(group, "Email", i18n("Email"), QVariant::String);
00060     item = addItemInfo(group, "Telephone", i18n("Telephone"), QVariant::String);
00061 }
00062 
00063 
00064 bool KVcfPlugin::readInfo( KFileMetaInfo& info, uint /*what*/ )
00065 {
00066 
00067     QFile file(info.path());
00068 
00069     if (!file.open(IO_ReadOnly))
00070     {
00071         kdDebug(7034) << "Couldn't open " << QFile::encodeName(info.path()) << endl;
00072         return false;
00073     }
00074 
00075     char id_name[] = "FN:";
00076     char id_email[] = "EMAIL;INTERNET:";
00077 
00078     // we need a buffer for lines
00079     char linebuf[1000];
00080 
00081     // we need a buffer for other stuff
00082     char buf_name[1000] = "";
00083     char buf_email[1000] = "";
00084     buf_name[999] = '\0';
00085     buf_email[999] = '\0';
00086     char * myptr;
00087 
00088     // FIXME: This is intensely inefficient!!!
00089 
00090     bool done=false;
00091     while (!done) {
00092 
00093         // read a line
00094         int r = file.readLine(linebuf, sizeof( linebuf ));
00095 
00096         if ( r < 0 ) {
00097             done = true;
00098             break;
00099         }
00100 
00101         // have we got something useful?
00102         if (memcmp(linebuf, id_name, 3) == 0) {
00103             // we have a name
00104             myptr = linebuf + 3;
00105             strlcpy(buf_name, myptr, sizeof( buf_name ));
00106         } else if (memcmp(linebuf, id_email, 15) == 0) {
00107             // we have an email
00108             myptr = linebuf + 15;
00109             strlcpy(buf_email, myptr, sizeof( buf_email ));
00110         }
00111 
00112         // are we done yet?
00113         if ((strlen(buf_name) > 0 && strlen(buf_email) > 0) || file.atEnd())
00114             done = true;
00115 
00116     }
00117 
00118 
00119     KFileMetaInfoGroup group = appendGroup(info, "Technical");
00120 
00121     if (strlen(buf_name) > 0)
00122         appendItem(group, "Name", QString::fromUtf8(buf_name));
00123 
00124     if (strlen(buf_email) > 0)
00125         appendItem(group, "Email", buf_email);
00126 
00127     return true;
00128 }
00129 
00130 #include "kfile_vcf.moc"
KDE Logo
This file is part of the documentation for kfile-plugins Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Apr 4 04:43:56 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003