Bibletime – a bible study tool
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

btinstallmgr.cpp 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*********
  2. *
  3. * This file is part of BibleTime's source code, http://www.bibletime.info/.
  4. *
  5. * Copyright 1999-2006 by the BibleTime developers.
  6. * The BibleTime source code is licensed under the GNU General Public License version 2.0.
  7. *
  8. **********/
  9. //BibleTime includes
  10. #include "btinstallmgr.h"
  11. #include "util/cpointers.h"
  12. #include "cbtconfig.h"
  13. //TQt includes
  14. #include <tqfile.h>
  15. #include <tqfileinfo.h>
  16. //KDE includes
  17. #include <tdeapplication.h>
  18. #include <tdeglobal.h>
  19. #include <tdelocale.h>
  20. #include <kstandarddirs.h>
  21. #include <kprocess.h>
  22. #include <tdeconfig.h>
  23. #include <tdemessagebox.h>
  24. //Sword includes
  25. #include <filemgr.h>
  26. #include <swconfig.h>
  27. #include <swbuf.h>
  28. #include <map>
  29. #include <utility>
  30. //Stl includes
  31. #include <functional>
  32. using namespace sword;
  33. namespace BookshelfManager {
  34. const TQString BTInstallMgr::Tool::LocalConfig::swordConfigFilename() {
  35. // Q_ASSERT( CPointers::backend()->sysconfig );
  36. return TQString::fromLatin1("%1/.sword/sword.conf").arg(getenv("HOME"));
  37. }
  38. TQStringList BTInstallMgr::Tool::RemoteConfig::sourceList( sword::InstallMgr* mgr ) {
  39. // tqWarning("BTInstallMgr::Tool::sourceList( sword::InstallMgr* mgr )");
  40. Q_ASSERT(mgr);
  41. Q_ASSERT(mgr->installConf);
  42. TQStringList names;
  43. //add Sword remote sources
  44. for (InstallSourceMap::iterator it = mgr->sources.begin(); it != mgr->sources.end(); it++) {
  45. names << TQString::fromLocal8Bit(it->second->caption);
  46. }
  47. // Add local directory sources
  48. SWConfig config(Tool::RemoteConfig::configFilename().latin1());
  49. sword::SectionMap::iterator sourcesSection = config.Sections.find("Sources");
  50. if (sourcesSection != config.Sections.end()) {
  51. sword::ConfigEntMap::iterator sourceBegin = sourcesSection->second.lower_bound("DIRSource");
  52. sword::ConfigEntMap::iterator sourceEnd = sourcesSection->second.upper_bound("DIRSource");
  53. while (sourceBegin != sourceEnd) {
  54. InstallSource is("DIR", sourceBegin->second.c_str());
  55. names << TQString::fromLatin1(is.caption.c_str());
  56. sourceBegin++;
  57. }
  58. }
  59. return names;
  60. }
  61. TQStringList BTInstallMgr::Tool::LocalConfig::targetList() {
  62. TQStringList names = CPointers::backend()->swordDirList();
  63. return names;
  64. }
  65. void BTInstallMgr::Tool::LocalConfig::setTargetList( const TQStringList& targets ) {
  66. //saves a new Sworc config using the provided target list
  67. TQString filename = TDEGlobal::dirs()->saveLocation("data", "bibletime/") + "sword.conf"; //default is to assume the real location isn't writable
  68. bool directAccess = false;
  69. TQFileInfo i(LocalConfig::swordConfigFilename());
  70. TQFileInfo dirInfo(i.dirPath(true));
  71. if ( i.exists() && i.isWritable() ) { //we can write to the file ourself
  72. filename = LocalConfig::swordConfigFilename();
  73. directAccess = true;
  74. }
  75. else if ( !i.exists() && dirInfo.isWritable() ) { // if the file doesn't exist but th eparent is writable for us, create it
  76. filename = LocalConfig::swordConfigFilename();
  77. directAccess = true;
  78. }
  79. bool setDataPath = false;
  80. SWConfig conf(filename.local8Bit());
  81. conf.Sections.clear();
  82. for (TQStringList::const_iterator it = targets.begin(); it != targets.end(); ++it) {
  83. TQString t = *it;
  84. if (t.contains( TQString("%1/.sword").arg(getenv("HOME")) )) {
  85. //we don't want HOME/.sword in the config
  86. continue;
  87. }
  88. else {
  89. conf["Install"].insert( std::make_pair(!setDataPath ? SWBuf("DataPath") : SWBuf("AugmentPath"), t.local8Bit().data()) );
  90. setDataPath = true;
  91. }
  92. }
  93. conf.Save();
  94. if (!directAccess) { //use tdesu to move the file to the right place
  95. TDEProcess *proc = new TDEProcess;
  96. *proc << "tdesu";
  97. *proc << TQString::fromLatin1("-c") << TQString("mv %1 %2").arg(filename).arg(LocalConfig::swordConfigFilename());
  98. proc->start(TDEProcess::Block);
  99. }
  100. }
  101. sword::InstallSource BTInstallMgr::Tool::RemoteConfig::source( sword::InstallMgr* mgr, const TQString& name ) {
  102. Q_ASSERT(mgr);
  103. InstallSourceMap::iterator source = mgr->sources.find(name.latin1());
  104. if (source != mgr->sources.end()) {
  105. return *(source->second);
  106. }
  107. else { //not found in Sword, may be a local DIR source
  108. SWConfig config(Tool::RemoteConfig::configFilename().latin1());
  109. SectionMap::iterator sourcesSection = config.Sections.find("Sources");
  110. if (sourcesSection != config.Sections.end()) {
  111. ConfigEntMap::iterator sourceBegin =
  112. sourcesSection->second.lower_bound("DIRSource");
  113. ConfigEntMap::iterator sourceEnd =
  114. sourcesSection->second.upper_bound("DIRSource");
  115. while (sourceBegin != sourceEnd) {
  116. InstallSource is("DIR", sourceBegin->second.c_str());
  117. if (!strcmp(is.caption, name.latin1()) ) { //found local dir source
  118. return is;
  119. }
  120. sourceBegin++;//next source
  121. }
  122. }
  123. }
  124. InstallSource is("EMPTY"); //default return value
  125. is.caption = "unknown caption";
  126. is.source = "unknown source";
  127. is.directory = "unknown dir";
  128. return is;
  129. }
  130. const bool BTInstallMgr::Tool::RemoteConfig::isRemoteSource( sword::InstallSource* is ) {
  131. Q_ASSERT(is);
  132. if (is)
  133. return !strcmp(is->type, "FTP");
  134. else
  135. return false;
  136. }
  137. void BTInstallMgr::Tool::RemoteConfig::addSource( sword::InstallSource* is ) {
  138. if (!is) {
  139. return;
  140. }
  141. SWConfig config(Tool::RemoteConfig::configFilename().latin1());
  142. if (!strcmp(is->type, "FTP")) {
  143. //make sure the path doesn't have a trailing slash, sword doesn't like it
  144. if (is->directory[ is->directory.length()-1 ] == '/') {
  145. is->directory--; //make one char shorter
  146. }
  147. config["Sources"].insert( std::make_pair(SWBuf("FTPSource"), is->getConfEnt()) );
  148. }
  149. else if (!strcmp(is->type, "DIR")) {
  150. config["Sources"].insert( std::make_pair(SWBuf("DIRSource"), is->getConfEnt()) );
  151. }
  152. config.Save();
  153. }
  154. void BTInstallMgr::Tool::RemoteConfig::initConfig() {
  155. SWConfig config(Tool::RemoteConfig::configFilename().latin1());
  156. config["General"]["PassiveFTP"] = "true";
  157. config.Save();
  158. }
  159. const TQString BTInstallMgr::Tool::RemoteConfig::configPath() {
  160. const char *envhomedir = getenv("HOME");
  161. TQString confPath = TQString(envhomedir ? envhomedir : ".");
  162. confPath.append("/.sword/InstallMgr");
  163. return confPath;
  164. }
  165. const TQString BTInstallMgr::Tool::RemoteConfig::configFilename() {
  166. return (configPath() + "/InstallMgr.conf");
  167. }
  168. void BTInstallMgr::Tool::RemoteConfig::removeSource( sword::InstallMgr* mgr, sword::InstallSource* is) {
  169. Q_ASSERT(mgr);
  170. Q_ASSERT(is);
  171. SWConfig config(Tool::RemoteConfig::configFilename().latin1());
  172. //this code can probably be shortened by using the stl remove_if functionality
  173. std::pair< ConfigEntMap::iterator, ConfigEntMap::iterator > range =
  174. isRemoteSource(is)
  175. ? config["Sources"].equal_range("FTPSource")
  176. : config["Sources"].equal_range("DIRSource");
  177. ConfigEntMap::iterator it = range.first;
  178. while (it != range.second) {
  179. if (it->second == is->getConfEnt()) {
  180. // tqWarning("found the source!");
  181. config["Sources"].erase(it);
  182. break;
  183. }
  184. ++it;
  185. }
  186. config.Save();
  187. }
  188. void BTInstallMgr::Tool::RemoteConfig::resetRemoteSources() {
  189. SWConfig config(Tool::RemoteConfig::configFilename().latin1());
  190. config["Sources"].erase( //remove all FTP sources
  191. config["Sources"].lower_bound("FTPSource"),
  192. config["Sources"].upper_bound("FTPSource")
  193. );
  194. config.Save();
  195. }
  196. void BTInstallMgr::Tool::RemoteConfig::resetLocalSources() {
  197. SWConfig config(Tool::RemoteConfig::configFilename().latin1());
  198. config["Sources"].erase( //remove all FTP sources
  199. config["Sources"].lower_bound("DIRSource"),
  200. config["Sources"].upper_bound("DIRSource")
  201. );
  202. config.Save();
  203. }
  204. CSwordBackend* BTInstallMgr::Tool::backend( sword::InstallSource* const is) {
  205. Q_ASSERT(is);
  206. if (!is) {
  207. return 0;
  208. }
  209. CSwordBackend* ret = 0;
  210. if (RemoteConfig::isRemoteSource(is)) {
  211. // tqWarning("## remote backend for %s", is->localShadow.c_str());
  212. ret = new CSwordBackend( TQString(is->localShadow.c_str()), false );
  213. }
  214. else {
  215. // tqWarning("## local backend for %s", TQString(is->directory.c_str()).latin1());
  216. ret = new CSwordBackend( TQString(is->directory.c_str()) );
  217. }
  218. Q_ASSERT(ret);
  219. if (ret) {
  220. ret->initModules();
  221. }
  222. return ret;
  223. }
  224. BTInstallMgr::BTInstallMgr() : InstallMgr(Tool::RemoteConfig::configPath().latin1(), this) { //use this class also as status reporter
  225. this->setFTPPassive(true);
  226. }
  227. #ifdef SWORD_INTERNET_WARNING
  228. bool BTInstallMgr::isUserDisclaimerConfirmed() const
  229. {
  230. TDEConfig* config = CBTConfig::getConfig();
  231. config->setGroup("Remote Resources");
  232. if (config->readNumEntry("Allowed", 0) == 1) {
  233. return true;
  234. }
  235. else {
  236. 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 ) {
  237. config->setGroup("Remote Resources");
  238. config->writeEntry("Allowed", 1);
  239. config->sync();
  240. return true;
  241. }
  242. else {
  243. return false;
  244. }
  245. }
  246. }
  247. #endif
  248. BTInstallMgr::~BTInstallMgr() {
  249. terminate(); //make sure to close the connection
  250. }
  251. void BTInstallMgr::statusUpdate(double dltotal, double dlnow) {
  252. if (dlnow > dltotal)
  253. dlnow = dltotal;
  254. int totalPercent = (int)((float)(dlnow + m_completedBytes) / (float)(m_totalBytes) * 100.0);
  255. if (totalPercent > 100) {
  256. totalPercent = 100;
  257. }
  258. else if (totalPercent < 0) {
  259. totalPercent = 0;
  260. }
  261. int filePercent = (int)((float)(dlnow) / (float)(dltotal+1) * 100.0);
  262. if (filePercent > 100) {
  263. filePercent = 100;
  264. }
  265. else if (filePercent < 0) {
  266. filePercent = 0;
  267. }
  268. emit completed(totalPercent, filePercent);
  269. }
  270. void BTInstallMgr::preStatus(long totalBytes, long completedBytes, const char* /*message*/) {
  271. tqWarning("pre Status: %i / %i", (int)totalBytes, (int)completedBytes);
  272. emit downloadStarted( "unknown filename" );
  273. m_completedBytes = completedBytes;
  274. m_totalBytes = (totalBytes > 0) ? totalBytes : 1; //avoid division by zero
  275. }
  276. // FTPTransport *BTInstallMgr::createFTPTransport(const char *host, StatusReporter *statusReporter) {
  277. // return new TDEIO_FTPTransport(host, statusReporter);
  278. // }
  279. }
  280. #include "btinstallmgr.moc"