• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdepim
  • Sitemap
  • Contact Us
 

console/kabcclient

csvtemplatefactory.cpp

Go to the documentation of this file.
00001 //
00002 //  Copyright (C) 2005 - 2006 Kevin Krammer <kevin.krammer@gmx.at>
00003 //
00004 //  This program is free software; you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation; either version 2 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program; if not, write to the Free Software
00016 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00017 //
00018 
00019 // local includes
00020 #include "csvtemplatefactory.h"
00021 #include "csvtemplate.h"
00022 
00023 // Qt includes
00024 #include <QtCore/QDir>
00025 #include <QtCore/QFile>
00026 #include <QtCore/QFileInfo>
00027 
00028 // KDE includes
00029 #include <kconfig.h>
00030 #include <kconfiggroup.h>
00031 #include <kcomponentdata.h>
00032 #include <kstandarddirs.h>
00033 
00035 
00036 CSVTemplateFactory::CSVTemplateFactory()
00037 {
00038 }
00039 
00041 
00042 CSVTemplateFactory::~CSVTemplateFactory()
00043 {
00044     QMap<QString, CSVTemplate*>::iterator it    = m_templates.begin();
00045     QMap<QString, CSVTemplate*>::iterator endIt = m_templates.end();
00046     for (; it != endIt; ++it)
00047     {
00048         CSVTemplate* temp = it.value();
00049         delete temp;
00050     }
00051 }
00052 
00054 
00055 CSVTemplate* CSVTemplateFactory::createTemplate(const QString& name)
00056 {
00057     if (name.isEmpty()) return 0;
00058 
00059     QString filename = m_templateFiles[name];
00060     if (filename.isEmpty())
00061     {
00062         filename = findTemplateFile(name);
00063         m_templateFiles[name] = filename;
00064     }
00065 
00066     KConfigBase* templateConfig = loadTemplateConfig(filename);
00067     if (templateConfig == 0) return 0;
00068 
00069     return new CSVTemplate(templateConfig);
00070 }
00071 
00073 
00074 CSVTemplate* CSVTemplateFactory::createCachedTemplate(const QString& name)
00075 {
00076     if (name.isEmpty()) return 0;
00077 
00078     QMap<QString, CSVTemplate*>::iterator it = m_templates.find(name);
00079     CSVTemplate* temp = 0;
00080     if (it != m_templates.end()) temp = it.value();
00081 
00082     if (temp == 0)
00083     {
00084         temp = createTemplate(name);
00085         if (temp != 0)
00086         {
00087             m_templates[name] = temp;
00088         }
00089     }
00090 
00091     return temp;
00092 }
00093 
00095 
00096 QMap<QString, QString> CSVTemplateFactory::templateNames()
00097 {
00098     if (m_templateNames.isEmpty())
00099     {
00100         addTemplateNames(QDir::currentPath());
00101 
00102         QStringList templateDirs =
00103             KGlobal::mainComponent().dirs()->findDirs("data", "kaddressbook/csv-templates");
00104 
00105         QStringList::const_iterator it    = templateDirs.begin();
00106         QStringList::const_iterator endIt = templateDirs.end();
00107         for (; it != endIt; ++it)
00108         {
00109             addTemplateNames(*it);
00110         }
00111     }
00112 
00113     return m_templateNames;
00114 }
00115 
00117 
00118 QString CSVTemplateFactory::findTemplateFile(const QString& name) const
00119 {
00120     if (name.isEmpty()) return QString();
00121 
00122     QString filename = name + ".desktop";
00123 
00124     // check current working directory first
00125     QFileInfo fileInfo(filename);
00126     if (fileInfo.exists() && fileInfo.isReadable()) return fileInfo.absoluteFilePath();
00127 
00128     return KStandardDirs::locate("data", "kaddressbook/csv-templates/" + filename);
00129 }
00130 
00132 
00133 KConfigBase* CSVTemplateFactory::loadTemplateConfig(const QString& filename) const
00134 {
00135     if (filename.isEmpty()) return 0;
00136 
00137     KConfig* config = new KConfig(filename);
00138 
00139     bool isTemplate = config->hasGroup("csv column map") &&
00140                       config->hasGroup("General") &&
00141                       config->hasGroup("Misc");
00142     if (!isTemplate)
00143     {
00144         delete config;
00145         config = 0;
00146     }
00147 
00148     return config;
00149 }
00150 
00152 
00153 void CSVTemplateFactory::addTemplateNames(const QString& directory)
00154 {
00155     if (directory.isEmpty()) return;
00156 
00157     QFileInfo dirInfo(directory);
00158     if (!dirInfo.isDir()) return;
00159 
00160     const QString extension = ".desktop";
00161     const QStringList filters(QString::fromUtf8("*") + extension);
00162     QDir dir(dirInfo.absoluteFilePath());
00163     const QFileInfoList fileInfos = dir.entryInfoList(filters);
00164 
00165     QFileInfoList::const_iterator it    = fileInfos.begin();
00166     QFileInfoList::const_iterator endIt = fileInfos.end();
00167 
00168     for (; it != endIt; ++it)
00169     {
00170         QFileInfo fileInfo = *it;
00171 
00172         if (!fileInfo.isFile()) continue;
00173         if (!fileInfo.isReadable()) continue;
00174 
00175         QString name = fileInfo.fileName();
00176         name = name.left(name.length() - extension.length());
00177 
00178         if (name.isEmpty()) continue;
00179         if (!m_templateNames[name].isEmpty()) continue;
00180 
00181         KConfigBase* templateConfig = loadTemplateConfig(fileInfo.absoluteFilePath());
00182         if (templateConfig == 0) continue;
00183 
00184         KConfigGroup group = templateConfig->group( "Misc" );
00185         QString templateName = group.readEntry("Name");
00186         if (!templateName.isEmpty()) m_templateNames[name] = templateName;
00187 
00188         delete templateConfig;
00189     }
00190 }
00191 
00192 // End of file

console/kabcclient

Skip menu "console/kabcclient"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

kdepim

Skip menu "kdepim"
  • akonadi
  •   clients
  •   kabc
  •   kcal
  •   kcm
  • akregator
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt
  • kdgantt1
  • kjots
  • kleopatra
  • kmail
  • kmobiletools
  • knode
  • knotes
  • kontact
  • kontactinterfaces
  • korganizer
  •   korgac
  • kpilot
  • ktimetracker
  •   doc
  • libkdepim
  • libkholidays
  • libkleo
  • libkpgp
  • maildir
Generated for kdepim by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal