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

console/kabcclient

  • sources
  • kde-4.14
  • kdepim
  • console
  • kabcclient
  • src
csvtemplatefactory.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2005 - 2006 Kevin Krammer <kevin.krammer@gmx.at>
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 //
18 
19 // local includes
20 #include "csvtemplatefactory.h"
21 #include "csvtemplate.h"
22 
23 // Qt includes
24 #include <QtCore/QDir>
25 #include <QtCore/QFile>
26 #include <QtCore/QFileInfo>
27 
28 // KDE includes
29 #include <kconfig.h>
30 #include <kconfiggroup.h>
31 #include <kcomponentdata.h>
32 #include <kstandarddirs.h>
33 
35 
36 CSVTemplateFactory::CSVTemplateFactory()
37 {
38 }
39 
41 
42 CSVTemplateFactory::~CSVTemplateFactory()
43 {
44  QMap<QString, CSVTemplate*>::iterator it = m_templates.begin();
45  QMap<QString, CSVTemplate*>::iterator endIt = m_templates.end();
46  for (; it != endIt; ++it)
47  {
48  CSVTemplate* temp = it.value();
49  delete temp;
50  }
51 }
52 
54 
55 CSVTemplate* CSVTemplateFactory::createTemplate(const QString& name)
56 {
57  if (name.isEmpty()) return 0;
58 
59  QString filename = m_templateFiles[name];
60  if (filename.isEmpty())
61  {
62  filename = findTemplateFile(name);
63  m_templateFiles[name] = filename;
64  }
65 
66  KConfigBase* templateConfig = loadTemplateConfig(filename);
67  if (templateConfig == 0) return 0;
68 
69  return new CSVTemplate(templateConfig);
70 }
71 
73 
74 CSVTemplate* CSVTemplateFactory::createCachedTemplate(const QString& name)
75 {
76  if (name.isEmpty()) return 0;
77 
78  QMap<QString, CSVTemplate*>::ConstIterator it = m_templates.constFind(name);
79  CSVTemplate* temp = 0;
80  if (it != m_templates.constEnd()) temp = it.value();
81 
82  if (temp == 0)
83  {
84  temp = createTemplate(name);
85  if (temp != 0)
86  {
87  m_templates[name] = temp;
88  }
89  }
90 
91  return temp;
92 }
93 
95 
96 QMap<QString, QString> CSVTemplateFactory::templateNames()
97 {
98  if (m_templateNames.isEmpty())
99  {
100  addTemplateNames(QDir::currentPath());
101 
102  QStringList templateDirs =
103  KGlobal::mainComponent().dirs()->findDirs("data", QLatin1String("kaddressbook/csv-templates"));
104 
105  QStringList::const_iterator it = templateDirs.constBegin();
106  QStringList::const_iterator endIt = templateDirs.constEnd();
107  for (; it != endIt; ++it)
108  {
109  addTemplateNames(*it);
110  }
111  }
112 
113  return m_templateNames;
114 }
115 
117 
118 QString CSVTemplateFactory::findTemplateFile(const QString& name) const
119 {
120  if (name.isEmpty()) return QString();
121 
122  QString filename = name + QLatin1String(".desktop");
123 
124  // check current working directory first
125  QFileInfo fileInfo(filename);
126  if (fileInfo.exists() && fileInfo.isReadable()) return fileInfo.absoluteFilePath();
127 
128  return KStandardDirs::locate("data", QLatin1String("kaddressbook/csv-templates/") + filename);
129 }
130 
132 
133 KConfigBase* CSVTemplateFactory::loadTemplateConfig(const QString& filename) const
134 {
135  if (filename.isEmpty()) return 0;
136 
137  KConfig* config = new KConfig(filename);
138 
139  bool isTemplate = config->hasGroup("csv column map") &&
140  config->hasGroup("General") &&
141  config->hasGroup("Misc");
142  if (!isTemplate)
143  {
144  delete config;
145  config = 0;
146  }
147 
148  return config;
149 }
150 
152 
153 void CSVTemplateFactory::addTemplateNames(const QString& directory)
154 {
155  if (directory.isEmpty()) return;
156 
157  QFileInfo dirInfo(directory);
158  if (!dirInfo.isDir()) return;
159 
160  const QString extension = QLatin1String(".desktop");
161  const QStringList filters(QString::fromUtf8("*") + extension);
162  QDir dir(dirInfo.absoluteFilePath());
163  const QFileInfoList fileInfos = dir.entryInfoList(filters);
164 
165  QFileInfoList::const_iterator it = fileInfos.constBegin();
166  QFileInfoList::const_iterator endIt = fileInfos.constEnd();
167 
168  for (; it != endIt; ++it)
169  {
170  QFileInfo fileInfo = *it;
171 
172  if (!fileInfo.isFile()) continue;
173  if (!fileInfo.isReadable()) continue;
174 
175  QString name = fileInfo.fileName();
176  name = name.left(name.length() - extension.length());
177 
178  if (name.isEmpty()) continue;
179  if (!m_templateNames[name].isEmpty()) continue;
180 
181  KConfigBase* templateConfig = loadTemplateConfig(fileInfo.absoluteFilePath());
182  if (templateConfig == 0) continue;
183 
184  KConfigGroup group = templateConfig->group( "Misc" );
185  QString templateName = group.readEntry("Name");
186  if (!templateName.isEmpty()) m_templateNames[name] = templateName;
187 
188  delete templateConfig;
189  }
190 }
191 
192 // End of file
QFileInfo::isReadable
bool isReadable() const
CSVTemplateFactory::CSVTemplateFactory
CSVTemplateFactory()
Creates the factory instance.
Definition: csvtemplatefactory.cpp:36
QMap
CSVTemplateFactory::templateNames
QMap< QString, QString > templateNames()
Returns a set of available templates.
Definition: csvtemplatefactory.cpp:96
QMap::constFind
const_iterator constFind(const Key &key) const
QDir::currentPath
QString currentPath()
QList::const_iterator
QFileInfo::isFile
bool isFile() const
QString::fromUtf8
QString fromUtf8(const char *str, int size)
QFileInfo::fileName
QString fileName() const
QDir::entryInfoList
QFileInfoList entryInfoList(QFlags< QDir::Filter > filters, QFlags< QDir::SortFlag > sort) const
QFileInfo::absoluteFilePath
QString absoluteFilePath() const
QString::isEmpty
bool isEmpty() const
CSVTemplate
Class for handling KAddressBook's CSV templates.
Definition: csvtemplate.h:88
QMap::constEnd
const_iterator constEnd() const
csvtemplatefactory.h
QString
QMap::end
iterator end()
QMap::begin
iterator begin()
QStringList
QFileInfo
CSVTemplateFactory::createCachedTemplate
CSVTemplate * createCachedTemplate(const QString &name)
Creates a template handler for a given name and caches the instance.
Definition: csvtemplatefactory.cpp:74
CSVTemplateFactory::~CSVTemplateFactory
~CSVTemplateFactory()
Destroys the instance.
Definition: csvtemplatefactory.cpp:42
QDir
QLatin1String
QString::length
int length() const
QString::left
QString left(int n) const
CSVTemplateFactory::createTemplate
CSVTemplate * createTemplate(const QString &name)
Creates a template handler for a given name.
Definition: csvtemplatefactory.cpp:55
QMap::isEmpty
bool isEmpty() const
csvtemplate.h
QList::constEnd
const_iterator constEnd() const
QList::constBegin
const_iterator constBegin() const
QMap::value
const T value(const Key &key) const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:31:23 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

console/kabcclient

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

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal