1 | /**********************************************************************
|
---|
2 | ** Copyright (C) 2000-2007 Trolltech ASA. All rights reserved.
|
---|
3 | **
|
---|
4 | ** This file is part of the Qt Assistant.
|
---|
5 | **
|
---|
6 | ** This file may be distributed and/or modified under the terms of the
|
---|
7 | ** GNU General Public License version 2 as published by the Free Software
|
---|
8 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
9 | ** packaging of this file.
|
---|
10 | **
|
---|
11 | ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
|
---|
12 | ** licenses may use this file in accordance with the Qt Commercial License
|
---|
13 | ** Agreement provided with the Software.
|
---|
14 | **
|
---|
15 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
---|
16 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
---|
17 | **
|
---|
18 | ** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
---|
19 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
---|
20 | ** information about Qt Commercial License Agreements.
|
---|
21 | **
|
---|
22 | ** Contact info@trolltech.com if any conditions of this licensing are
|
---|
23 | ** not clear to you.
|
---|
24 | **
|
---|
25 | **********************************************************************/
|
---|
26 |
|
---|
27 | #include "config.h"
|
---|
28 | #include "profile.h"
|
---|
29 | #include "docuparser.h"
|
---|
30 |
|
---|
31 | #include <qapplication.h>
|
---|
32 | #include <qdir.h>
|
---|
33 | #include <qfile.h>
|
---|
34 | #include <qfileinfo.h>
|
---|
35 | #include <qsettings.h>
|
---|
36 | #include <qxml.h>
|
---|
37 |
|
---|
38 | static Config *static_configuration = 0;
|
---|
39 |
|
---|
40 | inline QString getVersionString()
|
---|
41 | {
|
---|
42 | return QString::number( (QT_VERSION >> 16) & 0xff )
|
---|
43 | + "." + QString::number( (QT_VERSION >> 8) & 0xff );
|
---|
44 | }
|
---|
45 |
|
---|
46 | Config::Config()
|
---|
47 | : hideSidebar( FALSE ), profil( 0 ), maximized(FALSE)
|
---|
48 | {
|
---|
49 | fontSiz = qApp->font().pointSize();
|
---|
50 | if( !static_configuration ) {
|
---|
51 | static_configuration = this;
|
---|
52 | } else {
|
---|
53 | qWarning( "Multiple configurations not allowed!" );
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | Config *Config::loadConfig(const QString &profileFileName)
|
---|
58 | {
|
---|
59 | Config *config = new Config();
|
---|
60 |
|
---|
61 | if (profileFileName.isEmpty()) { // no profile
|
---|
62 | config->profil = Profile::createDefaultProfile();
|
---|
63 | config->load();
|
---|
64 | config->loadDefaultProfile();
|
---|
65 | return config;
|
---|
66 | }
|
---|
67 |
|
---|
68 | QFile file(profileFileName);
|
---|
69 | if (!file.exists()) {
|
---|
70 | qWarning( "File does not exist: " + profileFileName );
|
---|
71 | return 0;
|
---|
72 | }
|
---|
73 | DocuParser *parser = DocuParser::createParser( profileFileName );
|
---|
74 | if (!parser) {
|
---|
75 | qWarning( "Failed to create parser for file: " + profileFileName );
|
---|
76 | return 0;
|
---|
77 | }
|
---|
78 | if (parser->parserVersion() < DocuParser::Qt320) {
|
---|
79 | qWarning( "File does not contain profile information" );
|
---|
80 | return 0;
|
---|
81 | }
|
---|
82 | DocuParser320 *profileParser = static_cast<DocuParser320*>(parser);
|
---|
83 | parser->parse(&file);
|
---|
84 | config->profil = profileParser->profile();
|
---|
85 | if (!config->profil) {
|
---|
86 | qWarning( "Config::loadConfig(), no profile in: " + profileFileName );
|
---|
87 | return 0;
|
---|
88 | }
|
---|
89 | config->profil->setProfileType(Profile::UserProfile);
|
---|
90 | config->profil->setDocuParser(profileParser);
|
---|
91 | config->load();
|
---|
92 | return config;
|
---|
93 | }
|
---|
94 |
|
---|
95 | Config *Config::configuration()
|
---|
96 | {
|
---|
97 | Q_ASSERT( static_configuration );
|
---|
98 | return static_configuration;
|
---|
99 | }
|
---|
100 |
|
---|
101 | void Config::load()
|
---|
102 | {
|
---|
103 | const QString key = "/Qt Assistant/" + getVersionString() + "/";
|
---|
104 | const QString profkey = key + "Profile/" + profil->props["name"] + "/";
|
---|
105 |
|
---|
106 | QSettings settings;
|
---|
107 | settings.insertSearchPath( QSettings::Windows, "/Trolltech" );
|
---|
108 |
|
---|
109 | webBrows = settings.readEntry( key + "Webbrowser" );
|
---|
110 | home = settings.readEntry( profkey + "Homepage" );
|
---|
111 | pdfApp = settings.readEntry( key + "PDFApplication" );
|
---|
112 | linkUnder = settings.readBoolEntry( key + "LinkUnderline", TRUE );
|
---|
113 | linkCol = settings.readEntry( key + "LinkColor", "#0000FF" );
|
---|
114 | src = settings.readListEntry( profkey + "Source" );
|
---|
115 | sideBar = settings.readNumEntry( key + "SideBarPage" );
|
---|
116 | if (qApp->type() != QApplication::Tty) {
|
---|
117 | fontFam = settings.readEntry( key + "Family", qApp->font().family() );
|
---|
118 |
|
---|
119 | fontFix = settings.readEntry( key + "FixedFamily", "courier" );
|
---|
120 | fontSiz = settings.readNumEntry( key + "Size", -1 );
|
---|
121 | if ( fontSiz < 4 ) {
|
---|
122 | fontSiz = qApp->font().pointSize();
|
---|
123 | }
|
---|
124 |
|
---|
125 | geom.setRect( settings.readNumEntry( key + "GeometryX", QApplication::desktop()->availableGeometry().x() ),
|
---|
126 | settings.readNumEntry( key + "GeometryY", QApplication::desktop()->availableGeometry().y() ),
|
---|
127 | settings.readNumEntry( key + "GeometryWidth", 800 ),
|
---|
128 | settings.readNumEntry( key + "GeometryHeight", 600 ) );
|
---|
129 | maximized = settings.readBoolEntry( key + "GeometryMaximized", FALSE );
|
---|
130 | }
|
---|
131 | mainWinLayout = settings.readEntry( key + "MainwindowLayout" );
|
---|
132 | rebuildDocs = settings.readBoolEntry( key + "RebuildDocDB", TRUE );
|
---|
133 |
|
---|
134 | profileNames = settings.entryList( key + "Profile" );
|
---|
135 | }
|
---|
136 |
|
---|
137 | void Config::save()
|
---|
138 | {
|
---|
139 | saveSettings();
|
---|
140 | saveProfile( profil );
|
---|
141 | }
|
---|
142 |
|
---|
143 | void Config::saveSettings()
|
---|
144 | {
|
---|
145 | const QString key = "/Qt Assistant/" + getVersionString() + "/";
|
---|
146 | const QString profkey = key + "Profile/" + profil->props["name"] + "/";
|
---|
147 |
|
---|
148 | QSettings settings;
|
---|
149 | settings.insertSearchPath( QSettings::Windows, "/Trolltech" );
|
---|
150 |
|
---|
151 | settings.writeEntry( key + "Webbrowser", webBrows );
|
---|
152 | settings.writeEntry( profkey + "Homepage", home );
|
---|
153 | settings.writeEntry( key + "PDFApplication", pdfApp );
|
---|
154 | settings.writeEntry( key + "LinkUnderline", linkUnder );
|
---|
155 | settings.writeEntry( key + "LinkColor", linkCol );
|
---|
156 | settings.writeEntry( profkey + "Source", src );
|
---|
157 | settings.writeEntry( key + "SideBarPage", sideBarPage() );
|
---|
158 | if (qApp->type() != QApplication::Tty) {
|
---|
159 | settings.writeEntry( key + "GeometryX", geom.x() );
|
---|
160 | settings.writeEntry( key + "GeometryY", geom.y() );
|
---|
161 | settings.writeEntry( key + "GeometryWidth", geom.width() );
|
---|
162 | settings.writeEntry( key + "GeometryHeight", geom.height() );
|
---|
163 | settings.writeEntry( key + "GeometryMaximized", maximized );
|
---|
164 | settings.writeEntry( key + "Family", fontFam );
|
---|
165 | settings.writeEntry( key + "Size", fontSiz < 4 ? qApp->font().pointSize() : fontSiz );
|
---|
166 | settings.writeEntry( key + "FixedFamily", fontFix );
|
---|
167 | }
|
---|
168 | if ( !hideSidebar )
|
---|
169 | settings.writeEntry( key + "MainwindowLayout", mainWinLayout );
|
---|
170 | settings.writeEntry( key + "RebuildDocDB", rebuildDocs );
|
---|
171 | }
|
---|
172 |
|
---|
173 | #ifdef ASSISTANT_DEBUG
|
---|
174 | static void dumpmap( const QMap<QString,QString> &m, const QString &header )
|
---|
175 | {
|
---|
176 | qDebug( header );
|
---|
177 | QMap<QString,QString>::ConstIterator it = m.begin();
|
---|
178 | while (it != m.end()) {
|
---|
179 | qDebug( " " + it.key() + ":\t\t" + *it );
|
---|
180 | ++it;
|
---|
181 | }
|
---|
182 | }
|
---|
183 | #endif
|
---|
184 |
|
---|
185 | void Config::loadDefaultProfile()
|
---|
186 | {
|
---|
187 | QSettings settings;
|
---|
188 | settings.insertSearchPath( QSettings::Windows, "/Trolltech" );
|
---|
189 | const QString key = "/Qt Assistant/" + QString(QT_VERSION_STR) + "/Profile";
|
---|
190 | const QString profKey = key + "/default/";
|
---|
191 |
|
---|
192 | if( settings.entryList( key + "/default" ).count() == 0 ) {
|
---|
193 | return;
|
---|
194 | }
|
---|
195 |
|
---|
196 | // Override the defaults with settings in registry.
|
---|
197 | profil->icons.clear();
|
---|
198 | profil->indexPages.clear();
|
---|
199 | profil->imageDirs.clear();
|
---|
200 | profil->docs.clear();
|
---|
201 | profil->dcfTitles.clear();
|
---|
202 |
|
---|
203 | QStringList titles = settings.readListEntry( profKey + "Titles" );
|
---|
204 | QStringList iconLst = settings.readListEntry( profKey + "DocIcons" );
|
---|
205 | QStringList indexLst = settings.readListEntry( profKey + "IndexPages" );
|
---|
206 | QStringList imgDirLst = settings.readListEntry( profKey + "ImageDirs" );
|
---|
207 | QStringList dcfs = settings.readListEntry( profKey + "DocFiles" );
|
---|
208 |
|
---|
209 | QStringList::ConstIterator it = titles.begin();
|
---|
210 | QValueListConstIterator<QString> iconIt = iconLst.begin();
|
---|
211 | QValueListConstIterator<QString> indexIt = indexLst.begin();
|
---|
212 | QValueListConstIterator<QString> imageIt = imgDirLst.begin();
|
---|
213 | QValueListConstIterator<QString> dcfIt = dcfs.begin();
|
---|
214 | for( ; it != titles.end();
|
---|
215 | ++it, ++iconIt, ++indexIt, ++imageIt, ++dcfIt )
|
---|
216 | {
|
---|
217 | profil->addDCFIcon( *it, *iconIt );
|
---|
218 | profil->addDCFIndexPage( *it, *indexIt );
|
---|
219 | profil->addDCFImageDir( *it, *imageIt );
|
---|
220 | profil->addDCFTitle( *dcfIt, *it );
|
---|
221 | }
|
---|
222 | #if ASSISTANT_DEBUG
|
---|
223 | dumpmap( profil->icons, "Icons" );
|
---|
224 | dumpmap( profil->indexPages, "IndexPages" );
|
---|
225 | dumpmap( profil->imageDirs, "ImageDirs" );
|
---|
226 | dumpmap( profil->dcfTitles, "dcfTitles" );
|
---|
227 | qDebug( "Docfiles: \n " + profil->docs.join( "\n " ) );
|
---|
228 | #endif
|
---|
229 | }
|
---|
230 |
|
---|
231 | void Config::saveProfile( Profile *profile )
|
---|
232 | {
|
---|
233 | if (profil && profil->profileType() == Profile::UserProfile)
|
---|
234 | return;
|
---|
235 | QSettings settings;
|
---|
236 | settings.insertSearchPath( QSettings::Windows, "/Trolltech" );
|
---|
237 | QString versionString = (profile->props["name"] == "default")
|
---|
238 | ? QString(QT_VERSION_STR)
|
---|
239 | : getVersionString();
|
---|
240 | const QString key = "/Qt Assistant/" + versionString + "/";
|
---|
241 | const QString profKey = key + "Profile/" + profile->props["name"] + "/";
|
---|
242 |
|
---|
243 | QStringList indexes, icons, imgDirs, dcfs;
|
---|
244 | QValueList<QString> titles = profile->dcfTitles.keys();
|
---|
245 | QValueListConstIterator<QString> it = titles.begin();
|
---|
246 | for ( ; it != titles.end(); ++it ) {
|
---|
247 | indexes << profile->indexPages[*it];
|
---|
248 | icons << profile->icons[*it];
|
---|
249 | imgDirs << profile->imageDirs[*it];
|
---|
250 | dcfs << profile->dcfTitles[*it];
|
---|
251 | }
|
---|
252 |
|
---|
253 | settings.writeEntry( profKey + "Titles", titles );
|
---|
254 | settings.writeEntry( profKey + "DocFiles", dcfs );
|
---|
255 | settings.writeEntry( profKey + "IndexPages", indexes );
|
---|
256 | settings.writeEntry( profKey + "DocIcons", icons );
|
---|
257 | settings.writeEntry( profKey + "ImageDirs", imgDirs );
|
---|
258 |
|
---|
259 | #if ASSISTANT_DEBUG
|
---|
260 | qDebug( "Titles:\n - " + ( (QStringList*) &titles )->join( "\n - " ) );
|
---|
261 | qDebug( "Docfiles:\n - " + dcfs.join( "\n - " ) );
|
---|
262 | qDebug( "IndexPages:\n - " + indexes.join( "\n - " ) );
|
---|
263 | qDebug( "DocIcons:\n - " + icons.join( "\n - " ) );
|
---|
264 | qDebug( "ImageDirs:\n - " + imgDirs.join( "\n - " ) );
|
---|
265 | #endif
|
---|
266 | }
|
---|
267 |
|
---|
268 | QStringList Config::mimePaths()
|
---|
269 | {
|
---|
270 | static QStringList lst;
|
---|
271 |
|
---|
272 | if( lst.count() > 0 )
|
---|
273 | return lst;
|
---|
274 |
|
---|
275 | for (QMap<QString,QString>::ConstIterator it = profil->dcfTitles.begin();
|
---|
276 | it != profil->dcfTitles.end(); ++it ) {
|
---|
277 |
|
---|
278 | // Mime source for .dcf file path
|
---|
279 | QFileInfo info( *it );
|
---|
280 | QString dcfPath = info.dirPath(TRUE);
|
---|
281 | if (lst.contains(dcfPath) == 0)
|
---|
282 | lst << dcfPath;
|
---|
283 |
|
---|
284 | // Image dir for .dcf
|
---|
285 | QString imgDir = QDir::convertSeparators( dcfPath + QDir::separator()
|
---|
286 | + profil->imageDirs[it.key()] );
|
---|
287 | if (lst.contains(imgDir) == 0)
|
---|
288 | lst << imgDir;
|
---|
289 | }
|
---|
290 | return lst;
|
---|
291 | }
|
---|
292 |
|
---|
293 | QStringList Config::profiles() const
|
---|
294 | {
|
---|
295 | return profileNames;
|
---|
296 | }
|
---|
297 |
|
---|
298 | QString Config::title() const
|
---|
299 | {
|
---|
300 | return profil->props[ "title" ];
|
---|
301 | }
|
---|
302 |
|
---|
303 | QString Config::aboutApplicationMenuText() const
|
---|
304 | {
|
---|
305 | return profil->props[ "aboutmenutext" ];
|
---|
306 | }
|
---|
307 |
|
---|
308 | QString Config::aboutURL() const
|
---|
309 | {
|
---|
310 | return profil->props[ "abouturl" ];
|
---|
311 | }
|
---|
312 |
|
---|
313 | QString Config::homePage() const
|
---|
314 | {
|
---|
315 | return home.isEmpty() ? profil->props["startpage"] : home;
|
---|
316 | }
|
---|
317 |
|
---|
318 | QStringList Config::source() const
|
---|
319 | {
|
---|
320 | return src.size() == 0 ? QStringList(profil->props["startpage"]) : src;
|
---|
321 | }
|
---|
322 |
|
---|
323 | QStringList Config::docFiles() const
|
---|
324 | {
|
---|
325 | return profil->docs;
|
---|
326 | }
|
---|
327 |
|
---|
328 | QPixmap Config::docIcon( const QString &title ) const
|
---|
329 | {
|
---|
330 | // ### To allow qdoc generated dcf files to reference the doc icons from qmake_image_col
|
---|
331 | if (!QFile::exists(profil->icons[title]))
|
---|
332 | return QPixmap::fromMimeSource( QFileInfo(profil->icons[title]).fileName() );
|
---|
333 | return QPixmap::fromMimeSource( profil->icons[title] );
|
---|
334 | }
|
---|
335 |
|
---|
336 | QPixmap Config::applicationIcon() const
|
---|
337 | {
|
---|
338 | return QPixmap::fromMimeSource( profil->props["applicationicon"] );
|
---|
339 | }
|
---|
340 |
|
---|
341 | QStringList Config::docTitles() const
|
---|
342 | {
|
---|
343 | return QStringList(profil->indexPages.keys());
|
---|
344 | }
|
---|
345 |
|
---|
346 | QString Config::docImageDir( const QString &docfile ) const
|
---|
347 | {
|
---|
348 | return profil->imageDirs[docfile];
|
---|
349 | }
|
---|
350 |
|
---|
351 | QString Config::indexPage( const QString &title ) const
|
---|
352 | {
|
---|
353 | return profil->indexPages
|
---|
354 | [title];
|
---|
355 | }
|
---|
356 |
|
---|
357 | void Config::hideSideBar( bool b )
|
---|
358 | {
|
---|
359 | hideSidebar = b;
|
---|
360 | }
|
---|
361 |
|
---|
362 | bool Config::sideBarHidden() const
|
---|
363 | {
|
---|
364 | return hideSidebar;
|
---|
365 | }
|
---|
366 |
|
---|
367 | QString Config::assistantDocPath() const
|
---|
368 | {
|
---|
369 | return profil->props["assistantdocs"].isEmpty()
|
---|
370 | ? QString( qInstallPathDocs() ) + "/html"
|
---|
371 | : profil->props["assistantdocs"];
|
---|
372 | }
|
---|