summaryrefslogtreecommitdiffstats
path: root/tools/assistant/config.cpp
blob: dbc823ff80057d8973580ca42b3420007cc5209b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/**********************************************************************
** Copyright (C) 2000-2008 Trolltech ASA.  All rights reserved.
**
** This file is part of the Qt Assistant.
**
** This file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free
** Software Foundation and appearing in the files LICENSE.GPL2
** and LICENSE.GPL3 included in the packaging of this file.
** Alternatively you may (at your option) use any later version
** of the GNU General Public License if such license has been
** publicly approved by Trolltech ASA (or its successors, if any)
** and the KDE Free Qt Foundation.
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://trolltech.com/products/qt/licenses/licensing/opensource/.
** If you are unsure which license is appropriate for your use, please
** review the following information:
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
** or contact the sales department at sales@trolltech.com.
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with
** the Software.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted
** herein.
**
**********************************************************************/

#include "config.h"
#include "profile.h"
#include "docuparser.h"

#include <qapplication.h>
#include <qdir.h>
#include <qfile.h>
#include <qfileinfo.h>
#include <qsettings.h>
#include <qxml.h>

static Config *static_configuration = 0;

inline QString getVersionString()
{
    return QString::number( (QT_VERSION >> 16) & 0xff )
	+ "." + QString::number( (QT_VERSION >> 8) & 0xff );
}

Config::Config()
    : hideSidebar( FALSE ), profil( 0 ), maximized(FALSE)
{
    fontSiz = qApp->font().pointSize();
    if( !static_configuration ) {
	static_configuration = this;
    } else {
	tqWarning( "Multiple configurations not allowed!" );
    }
}

Config *Config::loadConfig(const QString &profileFileName)
{
    Config *config = new Config();

    if (profileFileName.isEmpty()) { // no profile
	config->profil = Profile::createDefaultProfile();
	config->load();
	config->loadDefaultProfile();
	return config;
    }

    QFile file(profileFileName);
    if (!file.exists()) {
	tqWarning( "File does not exist: " + profileFileName );
	return 0;
    }
    DocuParser *parser = DocuParser::createParser( profileFileName );
    if (!parser) {
	tqWarning( "Failed to create parser for file: " + profileFileName );
	return 0;
    }
    if (parser->parserVersion() < DocuParser::Qt320) {
	tqWarning( "File does not contain profile information" );
	return 0;
    }
    DocuParser320 *profileParser = static_cast<DocuParser320*>(parser);
    parser->parse(&file);
    config->profil = profileParser->profile();
    if (!config->profil) {
	tqWarning( "Config::loadConfig(), no profile in: " + profileFileName );
	return 0;
    }
    config->profil->setProfileType(Profile::UserProfile);
    config->profil->setDocuParser(profileParser);
    config->load();
    return config;
}

Config *Config::configuration()
{
    Q_ASSERT( static_configuration );
    return static_configuration;
}

void Config::load()
{
    const QString key = "/Qt Assistant/" + getVersionString() + "/";
    const QString profkey = key + "Profile/" + profil->props["name"] + "/";

    QSettings settings;
    settings.insertSearchPath( QSettings::Windows, "/Trolltech" );

    webBrows = settings.readEntry( key + "Webbrowser" );
    home = settings.readEntry( profkey + "Homepage" );
    pdfApp = settings.readEntry( key + "PDFApplication" );
    linkUnder = settings.readBoolEntry( key + "LinkUnderline", TRUE );
    linkCol = settings.readEntry( key + "LinkColor", "#0000FF" );
    src = settings.readListEntry( profkey + "Source" );
    sideBar = settings.readNumEntry( key + "SideBarPage" );
    if (qApp->type() != QApplication::Tty) {
	fontFam = settings.readEntry( key + "Family", qApp->font().family() );

	fontFix = settings.readEntry( key + "FixedFamily", "courier" );
	fontSiz = settings.readNumEntry( key + "Size", -1 );
	if ( fontSiz < 4 ) {
	    fontSiz = qApp->font().pointSize();
	}

	geom.setRect( settings.readNumEntry( key + "GeometryX", QApplication::desktop()->availableGeometry().x() ),
		      settings.readNumEntry( key + "GeometryY", QApplication::desktop()->availableGeometry().y() ),
		      settings.readNumEntry( key + "GeometryWidth", 800 ),
		      settings.readNumEntry( key + "GeometryHeight", 600 ) );
	maximized = settings.readBoolEntry( key + "GeometryMaximized", FALSE );
    }
    mainWinLayout = settings.readEntry( key + "MainwindowLayout" );
    rebuildDocs = settings.readBoolEntry( key + "RebuildDocDB", TRUE );

    profileNames = settings.entryList( key + "Profile" );
}

void Config::save()
{
    saveSettings();
    saveProfile( profil );
}

void Config::saveSettings()
{
    const QString key = "/Qt Assistant/" + getVersionString() + "/";
    const QString profkey = key + "Profile/" + profil->props["name"] + "/";

    QSettings settings;
    settings.insertSearchPath( QSettings::Windows, "/Trolltech" );

    settings.writeEntry( key + "Webbrowser", webBrows );
    settings.writeEntry( profkey + "Homepage", home );
    settings.writeEntry( key + "PDFApplication", pdfApp );
    settings.writeEntry( key + "LinkUnderline", linkUnder );
    settings.writeEntry( key + "LinkColor", linkCol );
    settings.writeEntry( profkey + "Source", src );
    settings.writeEntry( key + "SideBarPage", sideBarPage() );
    if (qApp->type() != QApplication::Tty) {
	settings.writeEntry( key + "GeometryX", geom.x() );
	settings.writeEntry( key + "GeometryY", geom.y() );
	settings.writeEntry( key + "GeometryWidth", geom.width() );
	settings.writeEntry( key + "GeometryHeight", geom.height() );
	settings.writeEntry( key + "GeometryMaximized", maximized );
	settings.writeEntry( key + "Family",  fontFam );
	settings.writeEntry( key + "Size",  fontSiz < 4 ? qApp->font().pointSize() : fontSiz );
	settings.writeEntry( key + "FixedFamily", fontFix );
    }
    if ( !hideSidebar )
	settings.writeEntry( key + "MainwindowLayout", mainWinLayout );
    settings.writeEntry( key + "RebuildDocDB", rebuildDocs );
}

#ifdef ASSISTANT_DEBUG
static void dumpmap( const QMap<QString,QString> &m, const QString &header )
{
    tqDebug( header );
    QMap<QString,QString>::ConstIterator it = m.begin();
    while (it != m.end()) {
	tqDebug( "  " + it.key() + ":\t\t" + *it );
	++it;
    }
}
#endif

void Config::loadDefaultProfile()
{
    QSettings settings;
    settings.insertSearchPath( QSettings::Windows, "/Trolltech" );
    const QString key = "/Qt Assistant/" + QString(QT_VERSION_STR) + "/Profile";
    const QString profKey = key + "/default/";

    if( settings.entryList( key + "/default" ).count() == 0 ) {
	return;
    }

    // Override the defaults with settings in registry.
    profil->icons.clear();
    profil->indexPages.clear();
    profil->imageDirs.clear();
    profil->docs.clear();
    profil->dcfTitles.clear();

    QStringList titles = settings.readListEntry( profKey + "Titles" );
    QStringList iconLst = settings.readListEntry( profKey + "DocIcons" );
    QStringList indexLst = settings.readListEntry( profKey + "IndexPages" );
    QStringList imgDirLst = settings.readListEntry( profKey + "ImageDirs" );
    QStringList dcfs = settings.readListEntry( profKey + "DocFiles" );

    QStringList::ConstIterator it = titles.begin();
    QValueListConstIterator<QString> iconIt = iconLst.begin();
    QValueListConstIterator<QString> indexIt = indexLst.begin();
    QValueListConstIterator<QString> imageIt = imgDirLst.begin();
    QValueListConstIterator<QString> dcfIt = dcfs.begin();
    for( ; it != titles.end();
	++it, ++iconIt, ++indexIt, ++imageIt, ++dcfIt )
    {
	profil->addDCFIcon( *it, *iconIt );
	profil->addDCFIndexPage( *it, *indexIt );
	profil->addDCFImageDir( *it, *imageIt );
	profil->addDCFTitle( *dcfIt, *it );
    }
#if ASSISTANT_DEBUG
    dumpmap( profil->icons, "Icons" );
    dumpmap( profil->indexPages, "IndexPages" );
    dumpmap( profil->imageDirs, "ImageDirs" );
    dumpmap( profil->dcfTitles, "dcfTitles" );
    tqDebug( "Docfiles: \n  " + profil->docs.join( "\n  " ) );
#endif
}

void Config::saveProfile( Profile *profile )
{
    if (profil && profil->profileType() == Profile::UserProfile)
	return;
    QSettings settings;
    settings.insertSearchPath( QSettings::Windows, "/Trolltech" );
    QString versionString = (profile->props["name"] == "default")
	? QString(QT_VERSION_STR)
	: getVersionString();
    const QString key = "/Qt Assistant/" + versionString + "/";
    const QString profKey = key + "Profile/" + profile->props["name"] + "/";

    QStringList indexes, icons, imgDirs, dcfs;
    QValueList<QString> titles = profile->dcfTitles.keys();
    QValueListConstIterator<QString> it = titles.begin();
    for ( ; it != titles.end(); ++it ) {
	indexes << profile->indexPages[*it];
	icons << profile->icons[*it];
	imgDirs << profile->imageDirs[*it];
	dcfs << profile->dcfTitles[*it];
    }

    settings.writeEntry( profKey + "Titles", titles );
    settings.writeEntry( profKey + "DocFiles", dcfs );
    settings.writeEntry( profKey + "IndexPages", indexes );
    settings.writeEntry( profKey + "DocIcons", icons );
    settings.writeEntry( profKey + "ImageDirs", imgDirs );

#if ASSISTANT_DEBUG
    tqDebug( "Titles:\n  - " + ( (QStringList*) &titles )->join( "\n  - " ) );
    tqDebug( "Docfiles:\n  - " + dcfs.join( "\n  - " ) );
    tqDebug( "IndexPages:\n  - " + indexes.join( "\n  - " ) );
    tqDebug( "DocIcons:\n  - " + icons.join( "\n  - " ) );
    tqDebug( "ImageDirs:\n  - " + imgDirs.join( "\n  - " ) );
#endif
}

QStringList Config::mimePaths()
{
    static QStringList lst;

    if( lst.count() > 0 )
	return lst;

    for (QMap<QString,QString>::ConstIterator it = profil->dcfTitles.begin();
	 it != profil->dcfTitles.end(); ++it ) {

	// Mime source for .dcf file path
	QFileInfo info( *it );
	QString dcfPath = info.dirPath(TRUE);
	if (lst.contains(dcfPath) == 0)
	    lst << dcfPath;

	// Image dir for .dcf
	QString imgDir = QDir::convertSeparators( dcfPath + QDir::separator()
						  + profil->imageDirs[it.key()] );
	if (lst.contains(imgDir) == 0)
	    lst << imgDir;
    }
    return lst;
}

QStringList Config::profiles() const
{
    return profileNames;
}

QString Config::title() const
{
    return profil->props[ "title" ];
}

QString Config::aboutApplicationMenuText() const
{
    return profil->props[ "aboutmenutext" ];
}

QString Config::aboutURL() const
{
    return profil->props[ "abouturl" ];
}

QString Config::homePage() const
{
    return home.isEmpty() ? profil->props["startpage"] : home;
}

QStringList Config::source() const
{
    return src.size() == 0 ? QStringList(profil->props["startpage"]) : src;
}

QStringList Config::docFiles() const
{
    return profil->docs;
}

QPixmap Config::docIcon( const QString &title ) const
{
    // ### To allow qdoc generated dcf files to reference the doc icons from qmake_image_col
    if (!QFile::exists(profil->icons[title]))
	return QPixmap::fromMimeSource( QFileInfo(profil->icons[title]).fileName() );
    return QPixmap::fromMimeSource( profil->icons[title] );
}

QPixmap Config::applicationIcon() const
{
    return QPixmap::fromMimeSource( profil->props["applicationicon"] );
}

QStringList Config::docTitles() const
{
    return QStringList(profil->indexPages.keys());
}

QString Config::docImageDir( const QString &docfile ) const
{
    return profil->imageDirs[docfile];
}

QString Config::indexPage( const QString &title ) const
{
    return profil->indexPages
	[title];
}

void Config::hideSideBar( bool b )
{
    hideSidebar = b;
}

bool Config::sideBarHidden() const
{
    return hideSidebar;
}

QString Config::assistantDocPath() const
{
    return profil->props["assistantdocs"].isEmpty()
	? QString( tqInstallPathDocs() ) + "/html"
	: profil->props["assistantdocs"];
}