kexi

kexitemplateloader.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2007 Jaroslaw Staniek <js@iidea.pl>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library 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 GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "kexitemplateloader.h"
21
22#include <kstandarddirs.h>
23#include <tdeglobal.h>
24#include <tdelocale.h>
25#include <tdeconfig.h>
26#include <kdebug.h>
27#include <kiconloader.h>
28#include <tdeapplication.h>
29
30#include <tqdir.h>
31
32//static
33KexiTemplateInfo::List KexiTemplateLoader::loadListInfo()
34{
35 KexiTemplateInfo::List list;
36 const TQString subdir = TQString(kapp->instanceName()) + "/templates";
37 TQString lang( TDEGlobal::locale()->language() );
38 TQStringList dirs( kapp->dirs()->findDirs("data", subdir) );
39 while (true) {
40 foreach( TQStringList::ConstIterator, it, dirs) {
41 TQDir dir((*it)+lang);
42 if (!dir.exists())
43 continue;
44 if (!dir.isReadable()) {
45 kdWarning() << "KexiTemplateLoader::loadListInfo() \"" << dir.absPath() << "\" not readable!" << endl;
46 continue;
47 }
48 const TQStringList templateDirs( dir.entryList(TQDir::Dirs, TQDir::Name) );
49 const TQString absDirPath( dir.absPath() + '/' );
50 foreach(TQStringList::ConstIterator, templateIt, templateDirs) {
51 if ((*templateIt)=="." || (*templateIt==".."))
52 continue;
53 KexiTemplateInfo info = KexiTemplateLoader::loadInfo( absDirPath + *templateIt );
54 if (!info.name.isEmpty())
55 list.append( info );
56 }
57 }
58 if (lang != "en" && list.isEmpty()) //not found for current locale, try "en"
59 lang = "en";
60 else
61 break;
62 }
63 return list;
64}
65
66//static
67KexiTemplateInfo KexiTemplateLoader::loadInfo(const TQString& directory)
68{
69 TQDir dir(directory);
70 if (!dir.isReadable()) {
71 kdWarning() << "KexiTemplateLoader::loadInfo() \""
72 << directory << "\" not readable!" << endl;
73 return KexiTemplateInfo();
74 }
75 if (!TQFileInfo(directory+"/info.txt").isReadable())
76 return KexiTemplateInfo();
77 TDEConfig infoTxt(directory+"/info.txt", true/*readonly*/, false/*local*/);
78 KexiTemplateInfo info;
79 info.name = infoTxt.readEntry("Name");
80 if (info.name.isEmpty()) {
81 kdWarning() << "KexiTemplateLoader::loadInfo() \"" << (directory+"/info.txt") << "\" contains no \"name\" field" << endl;
82 return KexiTemplateInfo();
83 }
84 const TQStringList templateFiles( dir.entryList("*.kexi", TQDir::Files|TQDir::Readable, TQDir::Name) );
85 if (templateFiles.isEmpty()) {
86 kdWarning() << "KexiTemplateLoader::loadInfo() no readable .kexi template file found in \"" << directory << "\"" << endl;
87 return KexiTemplateInfo();
88 }
89 info.filename = directory+"/"+templateFiles.first();
90 info.description = infoTxt.readEntry("Description");
91 const TQString iconFileName( infoTxt.readEntry("Icon") );
92 if (!iconFileName.isEmpty())
93 info.icon = TQPixmap(directory+'/'+iconFileName);
94 if (info.icon.isNull())
95 info.icon = DesktopIcon("kexiproject_sqlite"); //default
96 TQStringList autoopenObjectsString = infoTxt.readListEntry("AutoOpenObjects");
97 foreach( TQStringList::ConstIterator, it, autoopenObjectsString) {
98 KexiProjectData::ObjectInfo autoopenObject;
99 TQStringList autoopenObjectNameSplitted( TQStringList::split(':', *it) );
100 if (autoopenObjectNameSplitted.count()>1) {
101 autoopenObject["type"] = autoopenObjectNameSplitted[0];
102 autoopenObject["name"] = autoopenObjectNameSplitted[1];
103 }
104 else {
105 autoopenObject["type"] = "table";
106 autoopenObject["name"] = autoopenObjectNameSplitted[0];
107 }
108 autoopenObject["action"] = "open";
109 info.autoopenObjects.append( autoopenObject );
110 }
111 return info;
112}
A structure providing information about single kexi database template file.