summaryrefslogtreecommitdiffstats
path: root/src/kchmconfig.cpp
blob: 8f3305701c2d6d57840e7003817b123aa242ab65 (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
/***************************************************************************
 *   Copyright (C) 2004-2007 by Georgy Yunaev, gyunaev@ulduzsoft.com       *
 *   Please do not use email address above for bug reports; see            *
 *   the README file                                                       *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/

#include "kde-qt.h"
#include "kchmconfig.h"
#include "kchmsettings.h"
#include "kchmmainwindow.h"


KCHMConfig appConfig;

const char * APP_PATHINUSERDIR = ".kchmviewer";


KCHMConfig::KCHMConfig()
{
	TQDir dir;
	m_datapath = TQDir::homeDirPath() + "/" + APP_PATHINUSERDIR;
	 
	dir.setPath (m_datapath);
	
	if ( !dir.exists() && !dir.mkdir(m_datapath) )
		tqWarning ("Could not create directory %s", m_datapath.ascii());

	m_LoadLatestFileOnStartup = false;
	m_onNewChmClick = ACTION_ASK_USER;
	m_onExternalLinkClick = ACTION_ASK_USER;
	m_HistorySize = 10;
	m_HistoryStoreExtra = true;
	m_useSearchEngine = SEARCH_USE_MINE;
	
	m_TQtBrowserPath = "viewurl-netscape.sh '%s'";
	m_kdeUseTQTextBrowser = false;
	m_kdeEnableJS = false;
	m_kdeEnableJava = false;
	m_kdeEnablePlugins = true;
	m_kdeEnableRefresh = false;
	
	m_advUseInternalEditor = true;
	m_advExternalEditorPath = "kate '%s'";
	
	m_lastOpenedDir = "";
}


KCHMConfig::~KCHMConfig()
{
}

bool KCHMConfig::load()
{
	TQFile file (m_datapath + "/config");
	if ( !file.open (IO_ReadOnly) )
		return false; // no error message - not actually a problem
	
	TQString line;
	char readbuf[4096];
	bool getting_history = false;
	m_History.clear();
	
	while ( file.readLine( readbuf, sizeof(readbuf) - 1 ) > 0 )
	{
		line = TQString::fromUtf8( readbuf ).stripWhiteSpace();
		
		// skip empty lines and comments
		if ( line.isEmpty() || line[0] == '#' )
			continue;
		
		TQRegExp rxsection ("^\\[(\\w+)\\]$"), rxkeypair ("^(\\w+)\\s*=\\s*(.*)$");
		
		if ( rxsection.search ( line ) != -1 )
		{
			if ( rxsection.cap (1) == "settings" )
				getting_history = false;
			else if ( rxsection.cap (1) == "history" )
				getting_history = true;
			else
				tqWarning ("Unknown configuration section: %s", TQString(rxsection.cap(1)).ascii());
			
			continue;
		}
		else if ( !getting_history && rxkeypair.search ( line ) != -1 )
		{
			TQString key (rxkeypair.cap (1)), value (rxkeypair.cap(2));
			
			if ( key == "LoadLatestFileOnStartup" )
				m_LoadLatestFileOnStartup = value.toInt() ? true : false;
			else if ( key == "onNewChmClick" )
				m_onNewChmClick = (choose_action_t) value.toInt();
			else if ( key == "onExternalLinkClick" )
				m_onExternalLinkClick = (choose_action_t) value.toInt();
			else if ( key == "HistorySize" )
				m_HistorySize = value.toInt();
			else if ( key == "HistoryStoreExtra" )
				m_HistoryStoreExtra = value.toInt() ? true : false;
			else if ( key == "TQtBrowserPath" )
				m_TQtBrowserPath = value;
			else if ( key == "kdeUseTQTextBrowser" )
				m_kdeUseTQTextBrowser = value.toInt() ? true : false;
			else if ( key == "kdeEnableJS" )
				m_kdeEnableJS = value.toInt() ? true : false;
			else if ( key == "kdeEnableJava" )
				m_kdeEnableJava = value.toInt() ? true : false;
			else if ( key == "kdeEnablePlugins" )
				m_kdeEnablePlugins = value.toInt() ? true : false;
			else if ( key == "kdeEnableRefresh" )
				m_kdeEnableRefresh = value.toInt() ? true : false;
			else if ( key == "LastOpenedDir" )
				m_lastOpenedDir = value;
			else if ( key == "advUseInternalEditor" )
				m_advUseInternalEditor = value.toInt() ? true : false;
			else if ( key == "advExternalEditorPath" )
				m_advExternalEditorPath = value;
			else if ( key == "useSearchEngine" )
				m_useSearchEngine = (use_search_engine) value.toInt();
			else
				tqWarning ("Unknown key=value pair: %s", line.ascii());
		}
		else if ( getting_history )
		{
			if ( m_History.size() < m_HistorySize )
				addFileToHistory( line );
		}
		else
			tqWarning ("Unknown line in configuration: %s", line.ascii());
	}

	return true;
}

bool KCHMConfig::save( )
{
	TQFile file (m_datapath + "/config");
	if ( !file.open (IO_WriteOnly) )
	{
		tqWarning ("Could not write settings into file %s: %s", TQString(file.name()).ascii(), TQString(file.errorString()).ascii());
		return false;
	}
	
	TQTextStream stream( &file );
	stream.setEncoding( TQTextStream::UnicodeUTF8 );
	stream << "[settings]\n";
	stream << "LoadLatestFileOnStartup=" << m_LoadLatestFileOnStartup << "\n";

	stream << "onNewChmClick=" << m_onNewChmClick << "\n";
	stream << "onExternalLinkClick=" << m_onExternalLinkClick << "\n";
	stream << "HistorySize=" << m_HistorySize << "\n";
	stream << "HistoryStoreExtra=" << m_HistoryStoreExtra << "\n";

	stream << "TQtBrowserPath=" << m_TQtBrowserPath << "\n";
	stream << "kdeUseTQTextBrowser=" << m_kdeUseTQTextBrowser << "\n";
	stream << "kdeEnableJS=" << m_kdeEnableJS << "\n";
	stream << "kdeEnableJava=" << m_kdeEnableJava << "\n";
	stream << "kdeEnablePlugins=" << m_kdeEnablePlugins << "\n";
	stream << "kdeEnableRefresh=" << m_kdeEnableRefresh << "\n";
	stream << "advUseInternalEditor=" << m_advUseInternalEditor << "\n";
	stream << "advExternalEditorPath=" << m_advExternalEditorPath << "\n";
	stream << "useSearchEngine=" << m_useSearchEngine << "\n";
	
	stream << "LastOpenedDir=" << m_lastOpenedDir << "\n";	
	
	stream << "\n[history]\n";
	
	// Do not write all the history, but only the needed amount
	for ( unsigned int i = 0; i < m_History.size(); i++ )
		stream << m_History[m_History.size() - 1 - i] << "\n";
	
	//	m_History
	return true;
}

void KCHMConfig::addFileToHistory( const TQString & file )
{
	TQStringList::Iterator itr = m_History.find( file );
	
	// check whether file already exists in history - more it forward
	if ( itr != m_History.end() )
	{
		m_History.erase( itr );
		m_History.push_back(file);
		return;
	}

	if ( m_History.size() < m_HistorySize )
	{
		m_History.push_back( file );
		return;
	}
	
	// Remove a file from the front
	TQString filetoremove = m_History[0];
	m_History.erase( m_History.begin() );
	m_History.push_back( file );
	
	// And remove the appropriate history file
	mainWindow->currentSettings()->removeSettings ( filetoremove );
}