File indexing completed on 2024-05-12 16:39:41

0001 /* This file is part of the KDE project
0002    Copyright (C) 2007-2011 Jarosław Staniek <staniek@kde.org>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "kexitemplateloader.h"
0021 #include "kexi.h"
0022 
0023 #include <KDbUtils>
0024 
0025 #include <KConfig>
0026 #include <KConfigGroup>
0027 #include <KAboutData>
0028 
0029 #include <QDir>
0030 #include <QIcon>
0031 #include <QStandardPaths>
0032 #include <QApplication>
0033 
0034 KexiTemplateCategoryInfo::KexiTemplateCategoryInfo()
0035   : enabled(true)
0036 {
0037 }
0038 
0039 KexiTemplateCategoryInfo::~KexiTemplateCategoryInfo()
0040 {
0041 }
0042 
0043 KexiTemplateInfo::KexiTemplateInfo()
0044   : enabled(true)
0045 {
0046 }
0047 
0048 KexiTemplateInfo::~KexiTemplateInfo()
0049 {
0050 }
0051 
0052 void KexiTemplateCategoryInfo::addTemplate(const KexiTemplateInfo& t)
0053 {
0054     KexiTemplateInfo _t = t;
0055     _t.category = name;
0056     m_templates.append(_t);
0057 }
0058 
0059 //static
0060 KexiTemplateInfoList KexiTemplateLoader::loadListInfo()
0061 {
0062     KexiTemplateInfoList list;
0063 //! @todo KEXI3 KAboutData::applicationData().componentName() OK?
0064     const QString subdir = KAboutData::applicationData().componentName() + "/templates";
0065     const QLocale locale;
0066     QString lang(QLocale::languageToString(locale.language()));
0067     QStringList dirs(QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, subdir));
0068     while (true) {
0069         foreach(const QString &dirname, dirs) {
0070             QDir dir(dirname + lang);
0071             if (!dir.exists())
0072                 continue;
0073             if (!dir.isReadable()) {
0074                 qWarning() << "\"" << dir.absolutePath() << "\" not readable!";
0075                 continue;
0076             }
0077             const QStringList templateDirs(dir.entryList(QDir::Dirs, QDir::Name));
0078             const QString absDirPath(dir.absolutePath() + '/');
0079             foreach(const QString &templateDir, templateDirs) {
0080                 if (templateDir == "." || templateDir == "..")
0081                     continue;
0082                 KexiTemplateInfo info = KexiTemplateLoader::loadInfo(absDirPath + templateDir);
0083                 if (!info.name.isEmpty())
0084                     list.append(info);
0085             }
0086         }
0087         if (lang != "en" && list.isEmpty()) //not found for current locale, try "en"
0088             lang = "en";
0089         else
0090             break;
0091     }
0092     return list;
0093 }
0094 
0095 //static
0096 KexiTemplateInfo KexiTemplateLoader::loadInfo(const QString& directory)
0097 {
0098     QDir dir(directory);
0099     if (!dir.isReadable()) {
0100         qWarning() << "\"" << directory << "\" not readable!";
0101         return KexiTemplateInfo();
0102     }
0103     if (!QFileInfo(directory + "/info.txt").isReadable())
0104         return KexiTemplateInfo();
0105     KConfig infoTxt(directory + "/info.txt", KConfig::SimpleConfig);
0106     KConfigGroup cg = infoTxt.group(QString());
0107 
0108     KexiTemplateInfo info;
0109     info.name = cg.readEntry("Name");
0110     if (info.name.isEmpty()) {
0111         qWarning() << "\"" << (directory + "/info.txt") << "\" contains no \"name\" field";
0112         return KexiTemplateInfo();
0113     }
0114     QStringList templateFileNameFilters;
0115     templateFileNameFilters.append("*.kexi");
0116     const QStringList templateFiles(
0117         dir.entryList(templateFileNameFilters, QDir::Files | QDir::Readable, QDir::Name));
0118     if (templateFiles.isEmpty()) {
0119         qWarning() << "no readable .kexi template file found in \"" << directory << "\"";
0120         return KexiTemplateInfo();
0121     }
0122     info.filename = directory + "/" + templateFiles.first();
0123     info.description = cg.readEntry("Description");
0124     const QString iconFileName(cg.readEntry("Icon"));
0125     if (!iconFileName.isEmpty())
0126         info.icon = QIcon::fromTheme(directory + '/' + iconFileName);
0127     if (info.icon.isNull())
0128         info.icon = Kexi::defaultFileBasedDriverIcon();
0129     QStringList autoopenObjectsString = cg.readEntry("AutoOpenObjects", QStringList());
0130     foreach(const QString &autoopenObjectString, autoopenObjectsString) {
0131         KexiProjectData::ObjectInfo autoopenObject;
0132         QStringList autoopenObjectNameSplitted(autoopenObjectString.split(':'));
0133         if (autoopenObjectNameSplitted.count() > 1) {
0134             autoopenObject.insert("type", autoopenObjectNameSplitted[0]);
0135             autoopenObject.insert("name", autoopenObjectNameSplitted[1]);
0136         } else {
0137             autoopenObject.insert("type", "table");
0138             autoopenObject.insert("name", autoopenObjectNameSplitted[0]);
0139         }
0140         autoopenObject.insert("action", "open");
0141         info.autoopenObjects.append(autoopenObject);
0142     }
0143     return info;
0144 }