summaryrefslogtreecommitdiffstats
path: root/bibletime/frontend/btinstallmgr.cpp
blob: e5696427f7006a5b2017467bb98fb8c8420ad72f (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
/*********
*
* This file is part of BibleTime's source code, http://www.bibletime.info/.
*
* Copyright 1999-2006 by the BibleTime developers.
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
*
**********/

//BibleTime includes
#include "btinstallmgr.h"
#include "util/cpointers.h"
#include "cbtconfig.h"

//TQt includes
#include <tqfile.h>
#include <tqfileinfo.h>

//KDE includes
#include <kapplication.h>
#include <kglobal.h>
#include <klocale.h>
#include <kstandarddirs.h>
#include <kprocess.h>
#include <kconfig.h>
#include <kmessagebox.h>

//Sword includes
#include <filemgr.h>
#include <swconfig.h>
#include <swbuf.h>
#include <map>
#include <utility>

//Stl includes
#include <functional>

using namespace sword;

namespace BookshelfManager {

	const TQString BTInstallMgr::Tool::LocalConfig::swordConfigFilename() {
		//  Q_ASSERT( CPointers::backend()->sysconfig );
		return TQString::tqfromLatin1("%1/.sword/sword.conf").tqarg(getenv("HOME"));
	}

	TQStringList BTInstallMgr::Tool::RemoteConfig::sourceList( sword::InstallMgr* mgr ) {
		//  qWarning("BTInstallMgr::Tool::sourceList( sword::InstallMgr* mgr )");
		Q_ASSERT(mgr);
		Q_ASSERT(mgr->installConf);

		TQStringList names;

		//add Sword remote sources
		for (InstallSourceMap::iterator it = mgr->sources.begin(); it != mgr->sources.end(); it++) {
			names << TQString::fromLocal8Bit(it->second->caption);
		}

		// Add local directory sources
		SWConfig config(Tool::RemoteConfig::configFilename().latin1());
		sword::SectionMap::iterator sourcesSection = config.Sections.find("Sources");
		if (sourcesSection != config.Sections.end()) {
			sword::ConfigEntMap::iterator sourceBegin = sourcesSection->second.lower_bound("DIRSource");
			sword::ConfigEntMap::iterator sourceEnd = sourcesSection->second.upper_bound("DIRSource");

			while (sourceBegin != sourceEnd) {
				InstallSource is("DIR", sourceBegin->second.c_str());
				names << TQString::tqfromLatin1(is.caption.c_str());

				sourceBegin++;
			}
		}

		return names;
	}

	TQStringList BTInstallMgr::Tool::LocalConfig::targetList() {
		TQStringList names = CPointers::backend()->swordDirList();
		return names;
	}

	void BTInstallMgr::Tool::LocalConfig::setTargetList( const TQStringList& targets ) {
		//saves a new Sworc config using the provided target list
		TQString filename = KGlobal::dirs()->saveLocation("data", "bibletime/") + "sword.conf"; //default is to assume the real location isn't writable
		bool directAccess = false;

		TQFileInfo i(LocalConfig::swordConfigFilename());
		TQFileInfo dirInfo(i.dirPath(true));

		if ( i.exists() && i.isWritable() ) { //we can write to the file ourself
			filename = LocalConfig::swordConfigFilename();
			directAccess = true;
		}
		else if ( !i.exists() && dirInfo.isWritable() ) { // if the file doesn't exist but th etqparent is writable for us, create it
			filename = LocalConfig::swordConfigFilename();
			directAccess = true;
		}

		bool setDataPath = false;
		SWConfig conf(filename.local8Bit());
		conf.Sections.clear();

		for (TQStringList::const_iterator it = targets.begin(); it != targets.end(); ++it) {
			TQString t = *it;
			if (t.tqcontains( TQString("%1/.sword").tqarg(getenv("HOME")) )) {
				//we don't want HOME/.sword in the config
				continue;
			}
			else {
				conf["Install"].insert( std::make_pair(!setDataPath ? SWBuf("DataPath") : SWBuf("AugmentPath"), t.local8Bit().data()) );

				setDataPath = true;
			}
		}
		conf.Save();

		if (!directAccess) { //use kdesu to move the file to the right place
			KProcess *proc = new KProcess;
			*proc << "kdesu";
			*proc << TQString::tqfromLatin1("-c") << TQString("mv %1 %2").tqarg(filename).tqarg(LocalConfig::swordConfigFilename());
			proc->start(KProcess::Block);
		}
	}

	sword::InstallSource BTInstallMgr::Tool::RemoteConfig::source( sword::InstallMgr* mgr, const TQString& name ) {
		Q_ASSERT(mgr);

		InstallSourceMap::iterator source = mgr->sources.find(name.latin1());
		if (source != mgr->sources.end()) {
			return *(source->second);
		}
		else { //not found in Sword, may be a local DIR source
			SWConfig config(Tool::RemoteConfig::configFilename().latin1());
			SectionMap::iterator sourcesSection = config.Sections.find("Sources");
			if (sourcesSection != config.Sections.end()) {
				ConfigEntMap::iterator sourceBegin =
					sourcesSection->second.lower_bound("DIRSource");
				ConfigEntMap::iterator sourceEnd =
					sourcesSection->second.upper_bound("DIRSource");

				while (sourceBegin != sourceEnd) {
					InstallSource is("DIR", sourceBegin->second.c_str());
					if (!strcmp(is.caption, name.latin1()) ) { //found local dir source
						return is;
					}

					sourceBegin++;//next source
				}
			}
		}

		InstallSource is("EMPTY");   //default return value
		is.caption = "unknown caption";
		is.source = "unknown source";
		is.directory = "unknown dir";

		return is;
	}

	const bool BTInstallMgr::Tool::RemoteConfig::isRemoteSource( sword::InstallSource* is ) {
		Q_ASSERT(is);
		if (is)
			return !strcmp(is->type, "FTP");
		else
			return false;
	}

	void BTInstallMgr::Tool::RemoteConfig::addSource( sword::InstallSource* is ) {
		if (!is) {
			return;
		}

		SWConfig config(Tool::RemoteConfig::configFilename().latin1());
		if (!strcmp(is->type, "FTP")) {
			//make sure the path doesn't have a trailing slash, sword doesn't like it
			if (is->directory[ is->directory.length()-1 ] == '/') {
				is->directory--; //make one char shorter
			}

			config["Sources"].insert( std::make_pair(SWBuf("FTPSource"), is->getConfEnt()) );
		}
		else if (!strcmp(is->type, "DIR")) {
			config["Sources"].insert( std::make_pair(SWBuf("DIRSource"), is->getConfEnt()) );
		}
		config.Save();
	}

	void BTInstallMgr::Tool::RemoteConfig::initConfig() {
		SWConfig config(Tool::RemoteConfig::configFilename().latin1());
		config["General"]["PassiveFTP"] = "true";
		config.Save();
	}

	const TQString BTInstallMgr::Tool::RemoteConfig::configPath() {
		const char *envhomedir  = getenv("HOME");
		TQString confPath = TQString(envhomedir ? envhomedir : ".");
		confPath.append("/.sword/InstallMgr");

		return confPath;
	}

	const TQString BTInstallMgr::Tool::RemoteConfig::configFilename() {
		return (configPath() + "/InstallMgr.conf");
	}

	void BTInstallMgr::Tool::RemoteConfig::removeSource( sword::InstallMgr* mgr, sword::InstallSource* is) {
		Q_ASSERT(mgr);
		Q_ASSERT(is);

		SWConfig config(Tool::RemoteConfig::configFilename().latin1());

		//this code can probably be shortened by using the stl remove_if functionality
		std::pair< ConfigEntMap::iterator, ConfigEntMap::iterator > range =
			isRemoteSource(is)
			? config["Sources"].equal_range("FTPSource")
			: config["Sources"].equal_range("DIRSource");

		ConfigEntMap::iterator it = range.first;
		while (it != range.second) {
			if (it->second == is->getConfEnt()) {
				//    qWarning("found the source!");
				config["Sources"].erase(it);
				break;
			}

			++it;
		}

		config.Save();
	}

	void BTInstallMgr::Tool::RemoteConfig::resetRemoteSources() {
		SWConfig config(Tool::RemoteConfig::configFilename().latin1());
		config["Sources"].erase( //remove all FTP sources
			config["Sources"].lower_bound("FTPSource"),
			config["Sources"].upper_bound("FTPSource")
		);
		config.Save();
	}

	void BTInstallMgr::Tool::RemoteConfig::resetLocalSources() {
		SWConfig config(Tool::RemoteConfig::configFilename().latin1());
		config["Sources"].erase( //remove all FTP sources
			config["Sources"].lower_bound("DIRSource"),
			config["Sources"].upper_bound("DIRSource")
		);
		config.Save();
	}

	CSwordBackend* BTInstallMgr::Tool::backend( sword::InstallSource* const is) {
		Q_ASSERT(is);
		if (!is) {
			return 0;
		}

		CSwordBackend* ret = 0;
		if (RemoteConfig::isRemoteSource(is)) {
			//     qWarning("## remote backend for %s", is->localShadow.c_str());
			ret = new CSwordBackend( TQString(is->localShadow.c_str()), false );
		}
		else {
			//    qWarning("## local  backend for %s", TQString(is->directory.c_str()).latin1());
			ret = new CSwordBackend( TQString(is->directory.c_str()) );
		}

		Q_ASSERT(ret);
		if (ret) {
			ret->initModules();
		}

		return ret;
	}

	BTInstallMgr::BTInstallMgr() : InstallMgr(Tool::RemoteConfig::configPath().latin1(), this) { //use this class also as status reporter
		this->setFTPPassive(true);
	}

#ifdef SWORD_INTERNET_WARNING
	bool BTInstallMgr::isUserDisclaimerConfirmed() const
	{
		KConfig* config = CBTConfig::getConfig();
		config->setGroup("Remote Resources");
		if (config->readNumEntry("Allowed", 0) == 1) {
			return true;
		}
		else {
			if (KMessageBox::questionYesNo(0, i18n("Do you wish to allow BibleTime to access the Internet?\nWARNING: Click DENY if you live in a persecuted country!"), i18n("Confirm Internet Access"), i18n("DENY"), i18n("Allow") ) == KMessageBox::No ) {
				config->setGroup("Remote Resources");
				config->writeEntry("Allowed", 1);
				config->sync();
				return true;
			}
			else {
				return false;
			}
		}
	}
#endif

	BTInstallMgr::~BTInstallMgr() {
		terminate(); //make sure to close the connection
	}

	void BTInstallMgr::statusUpdate(double dltotal, double dlnow) {

		if (dlnow > dltotal)
			dlnow = dltotal;

		int totalPercent = (int)((float)(dlnow + m_completedBytes) / (float)(m_totalBytes) * 100.0);

		if (totalPercent > 100) {
			totalPercent = 100;
		}
		else if (totalPercent < 0) {
			totalPercent = 0;
		}

		int filePercent  = (int)((float)(dlnow) / (float)(dltotal+1) * 100.0);
		if (filePercent > 100) {
			filePercent = 100;
		}
		else if (filePercent < 0) {
			filePercent = 0;
		}

		emit completed(totalPercent, filePercent);
	}

	void BTInstallMgr::pretqStatus(long totalBytes, long completedBytes, const char* /*message*/) {
		qWarning("pre tqStatus: %i / %i", (int)totalBytes, (int)completedBytes);
		emit downloadStarted( "unknown filename" );

		m_completedBytes = completedBytes;
		m_totalBytes = (totalBytes > 0) ? totalBytes : 1; //avoid division by zero
	}

//	FTPTransport *BTInstallMgr::createFTPTransport(const char *host, StatusReporter *statusReporter) {
//		return new KIO_FTPTransport(host, statusReporter);
//	}


}