24 #include <QtCore/QDir>
25 #include <QtCore/QFile>
26 #include <QtCore/QFileInfo>
30 #include <kconfiggroup.h>
31 #include <kcomponentdata.h>
32 #include <kstandarddirs.h>
44 QMap<QString, CSVTemplate*>::iterator it = m_templates.begin();
45 QMap<QString, CSVTemplate*>::iterator endIt = m_templates.end();
46 for (; it != endIt; ++it)
57 if (name.isEmpty())
return 0;
59 QString filename = m_templateFiles[name];
60 if (filename.isEmpty())
62 filename = findTemplateFile(name);
63 m_templateFiles[name] = filename;
66 KConfigBase* templateConfig = loadTemplateConfig(filename);
67 if (templateConfig == 0)
return 0;
76 if (name.isEmpty())
return 0;
78 QMap<QString, CSVTemplate*>::ConstIterator it = m_templates.constFind(name);
80 if (it != m_templates.constEnd()) temp = it.value();
87 m_templates[name] = temp;
98 if (m_templateNames.isEmpty())
100 addTemplateNames(QDir::currentPath());
102 QStringList templateDirs =
103 KGlobal::mainComponent().dirs()->findDirs(
"data", QLatin1String(
"kaddressbook/csv-templates"));
105 QStringList::const_iterator it = templateDirs.constBegin();
106 QStringList::const_iterator endIt = templateDirs.constEnd();
107 for (; it != endIt; ++it)
109 addTemplateNames(*it);
113 return m_templateNames;
118 QString CSVTemplateFactory::findTemplateFile(
const QString& name)
const
120 if (name.isEmpty())
return QString();
122 QString filename = name + QLatin1String(
".desktop");
125 QFileInfo fileInfo(filename);
126 if (fileInfo.exists() && fileInfo.isReadable())
return fileInfo.absoluteFilePath();
128 return KStandardDirs::locate(
"data", QLatin1String(
"kaddressbook/csv-templates/") + filename);
133 KConfigBase* CSVTemplateFactory::loadTemplateConfig(
const QString& filename)
const
135 if (filename.isEmpty())
return 0;
137 KConfig* config =
new KConfig(filename);
139 bool isTemplate = config->hasGroup(
"csv column map") &&
140 config->hasGroup(
"General") &&
141 config->hasGroup(
"Misc");
153 void CSVTemplateFactory::addTemplateNames(
const QString& directory)
155 if (directory.isEmpty())
return;
157 QFileInfo dirInfo(directory);
158 if (!dirInfo.isDir())
return;
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);
165 QFileInfoList::const_iterator it = fileInfos.constBegin();
166 QFileInfoList::const_iterator endIt = fileInfos.constEnd();
168 for (; it != endIt; ++it)
170 QFileInfo fileInfo = *it;
172 if (!fileInfo.isFile())
continue;
173 if (!fileInfo.isReadable())
continue;
175 QString name = fileInfo.fileName();
176 name = name.left(name.length() - extension.length());
178 if (name.isEmpty())
continue;
179 if (!m_templateNames[name].isEmpty())
continue;
181 KConfigBase* templateConfig = loadTemplateConfig(fileInfo.absoluteFilePath());
182 if (templateConfig == 0)
continue;
184 KConfigGroup group = templateConfig->group(
"Misc" );
185 QString templateName = group.readEntry(
"Name");
186 if (!templateName.isEmpty()) m_templateNames[name] = templateName;
188 delete templateConfig;
CSVTemplateFactory()
Creates the factory instance.
QMap< QString, QString > templateNames()
Returns a set of available templates.
Class for handling KAddressBook's CSV templates.
CSVTemplate * createCachedTemplate(const QString &name)
Creates a template handler for a given name and caches the instance.
~CSVTemplateFactory()
Destroys the instance.
CSVTemplate * createTemplate(const QString &name)
Creates a template handler for a given name.