kexi

kexitemplateloader.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2007 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library 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 GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "kexitemplateloader.h"
00021 
00022 #include <kstandarddirs.h>
00023 #include <kglobal.h>
00024 #include <klocale.h>
00025 #include <kconfig.h>
00026 #include <kdebug.h>
00027 #include <kiconloader.h>
00028 #include <kapplication.h>
00029 
00030 #include <qdir.h>
00031 
00032 //static
00033 KexiTemplateInfo::List KexiTemplateLoader::loadListInfo()
00034 {
00035     KexiTemplateInfo::List list;
00036     const QString subdir = QString(kapp->instanceName()) + "/templates";
00037     QString lang( KGlobal::locale()->language() );
00038     QStringList dirs( kapp->dirs()->findDirs("data", subdir) );
00039     while (true) {
00040         foreach( QStringList::ConstIterator, it, dirs) {
00041             QDir dir((*it)+lang);
00042             if (!dir.exists())
00043                 continue;
00044             if (!dir.isReadable()) {
00045                 kdWarning() << "KexiTemplateLoader::loadListInfo() \"" << dir.absPath() << "\" not readable!" << endl;
00046                 continue;
00047             }
00048             const QStringList templateDirs( dir.entryList(QDir::Dirs, QDir::Name) );
00049             const QString absDirPath( dir.absPath() + '/' );
00050             foreach(QStringList::ConstIterator, templateIt, templateDirs) {
00051                 if ((*templateIt)=="." || (*templateIt==".."))
00052                     continue;
00053                 KexiTemplateInfo info = KexiTemplateLoader::loadInfo( absDirPath + *templateIt );
00054                 if (!info.name.isEmpty())
00055                     list.append( info );    
00056             }
00057         }
00058         if (lang != "en" && list.isEmpty()) //not found for current locale, try "en"
00059             lang = "en";
00060         else
00061             break;
00062     }
00063     return list;
00064 }
00065 
00066 //static
00067 KexiTemplateInfo KexiTemplateLoader::loadInfo(const QString& directory)
00068 {
00069     QDir dir(directory);
00070     if (!dir.isReadable()) {
00071         kdWarning() << "KexiTemplateLoader::loadInfo() \"" 
00072             << directory << "\" not readable!" << endl;
00073         return KexiTemplateInfo();
00074     }
00075     if (!QFileInfo(directory+"/info.txt").isReadable())
00076         return KexiTemplateInfo();
00077     KConfig infoTxt(directory+"/info.txt", true/*readonly*/, false/*local*/);
00078     KexiTemplateInfo info;
00079     info.name = infoTxt.readEntry("Name");
00080     if (info.name.isEmpty()) {
00081         kdWarning() << "KexiTemplateLoader::loadInfo() \"" << (directory+"/info.txt") << "\" contains no \"name\" field" << endl;
00082         return KexiTemplateInfo();
00083     }
00084     const QStringList templateFiles( dir.entryList("*.kexi", QDir::Files|QDir::Readable, QDir::Name) );
00085     if (templateFiles.isEmpty()) {
00086         kdWarning() << "KexiTemplateLoader::loadInfo() no readable .kexi template file found in \"" << directory << "\"" << endl;
00087         return KexiTemplateInfo();
00088     }
00089     info.filename = directory+"/"+templateFiles.first();
00090     info.description = infoTxt.readEntry("Description");
00091     const QString iconFileName( infoTxt.readEntry("Icon") );
00092     if (!iconFileName.isEmpty())
00093         info.icon = QPixmap(directory+'/'+iconFileName);
00094     if (info.icon.isNull())
00095         info.icon = DesktopIcon("kexiproject_sqlite"); //default
00096     QStringList autoopenObjectsString = infoTxt.readListEntry("AutoOpenObjects");
00097     foreach( QStringList::ConstIterator, it, autoopenObjectsString) {
00098         KexiProjectData::ObjectInfo autoopenObject;
00099         QStringList autoopenObjectNameSplitted( QStringList::split(':', *it) );
00100         if (autoopenObjectNameSplitted.count()>1) {
00101             autoopenObject["type"] = autoopenObjectNameSplitted[0];
00102             autoopenObject["name"] = autoopenObjectNameSplitted[1];
00103         }
00104         else {
00105             autoopenObject["type"] = "table";
00106             autoopenObject["name"] = autoopenObjectNameSplitted[0];
00107         }
00108         autoopenObject["action"] = "open";
00109         info.autoopenObjects.append( autoopenObject );
00110     }
00111     return info;
00112 }
KDE Home | KDE Accessibility Home | Description of Access Keys