PageRenderTime 27ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/src/abstractcontrol.cpp

https://github.com/berndhs/satview
C++ | 560 lines | 484 code | 65 blank | 11 comment | 63 complexity | eeb7bd08194350240ec0adaed5ce45f8 MD5 | raw file
Possible License(s): GPL-3.0
  1. #include "abstractcontrol.h"
  2. //
  3. // Copyright (C) 2010 - Bernd H Stramm
  4. //
  5. // This program is distributed under the terms of
  6. // the GNU General Public License version 3
  7. //
  8. // This software is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty
  10. // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. //
  12. #include <QTimer>
  13. #include <QString>
  14. #include <QByteArray>
  15. #include <QDesktopServices>
  16. #include <QMessageBox>
  17. #include "version.h"
  18. using namespace std;
  19. namespace satview {
  20. AbstractControl::AbstractControl ()
  21. :pApp(0),
  22. ready(false),
  23. mMeth(DBConnection::Con_None)
  24. {
  25. ready = false;
  26. /** @brief the runstate is context for the animation and winding features*/
  27. mRunState.timelimit = 0;
  28. mRunState.allway = false;
  29. mRunState.show = true;
  30. mRunState.backwards = false;
  31. mRunState.stopped = true;
  32. mRunState.pBuf = 0;
  33. mPicState.waiting = false;
  34. mPicState.failed = false;
  35. mPicState.pBuf = 0;
  36. mPicState.pImg = 0;
  37. connect (&showTimer, SIGNAL(timeout()), this, SLOT(DoShowMove()));
  38. StartTimers ();
  39. ready = true;
  40. }
  41. void
  42. AbstractControl::StartTimers ()
  43. {
  44. showTimeDelay = 100;
  45. noshowTimeDelay = 1;
  46. currentDelay = noshowTimeDelay;
  47. showTimer.start(currentDelay);
  48. }
  49. void
  50. AbstractControl::ShowVersion ()
  51. {
  52. satview::ShowVersionWindow ();
  53. }
  54. void
  55. AbstractControl::quit()
  56. {
  57. if (pApp) {
  58. pApp->quit();
  59. }
  60. }
  61. void
  62. AbstractControl::show()
  63. {
  64. }
  65. bool
  66. AbstractControl::DBWaiting()
  67. {
  68. return SatPicList::Instance()->DBConnect()->Waiting();
  69. }
  70. void
  71. AbstractControl::LinkToBernd()
  72. {
  73. QDesktopServices::openUrl
  74. (QUrl(QString("http://www.bernd-stramm.com/freestuff.php")));
  75. }
  76. void
  77. AbstractControl::PicArrive (QImage * pImg)
  78. {
  79. mPicState.pImg = pImg;
  80. mPicState.waiting = false;
  81. emit ReallyShowPic(pImg);
  82. }
  83. string
  84. AbstractControl::TimeAndDate (time_t t)
  85. {
  86. struct tm theTime;
  87. #ifdef _MSC_VER
  88. localtime_s(&theTime,&t);
  89. #else
  90. localtime_r (&t, &theTime);
  91. #endif
  92. const int datelen = 256;
  93. char plain[datelen+sizeof(void*)];
  94. int len = strftime (plain, datelen, "%Y-%m-%d %H:%M:%S", &theTime);
  95. plain[len] = 0;
  96. return string (plain);
  97. }
  98. void
  99. AbstractControl::DoSwitchPicname (QByteArray & bytes)
  100. {
  101. mPicname = string(bytes.data());
  102. SatPicList::Instance()->SetFilename(mPicname);
  103. SatPicList::Instance()->Ditch();
  104. SatPicList::Instance()->LoadFromDB();
  105. AbstractControl::DoWindFwd(0,true);
  106. AbstractControl::DoStepFwd();
  107. update();
  108. }
  109. void
  110. AbstractControl::ShowPic (SatPicBuf * pBuf)
  111. {
  112. if (pBuf) {
  113. if (mPicState.waiting) { // one at a time!
  114. return;
  115. }
  116. if (DBWaiting()) {
  117. return;
  118. }
  119. mPicState.SetNewBuf(pBuf);
  120. mPicState.waiting = true;
  121. QImage *pI = pBuf->Get_Image();
  122. if (pI == 0) {
  123. connect (pBuf, SIGNAL(ImageArrival(QImage *)),
  124. this, SLOT(PicArrive(QImage *)));
  125. }
  126. if (pI) {
  127. mPicState.waiting = false;
  128. mPicState.pImg = pI;
  129. emit ReallyShowPic(mPicState.pImg);
  130. } else {
  131. emit ShowIndexRec(pBuf);
  132. }
  133. }
  134. }
  135. unsigned long int
  136. AbstractControl::EndTime(long int diff)
  137. {
  138. SatPicBuf * curBuf = SatPicList::Instance()->Current();
  139. unsigned long end_t;
  140. if (curBuf) {
  141. end_t = curBuf->Ident() + diff;
  142. } else if (diff < 0) {
  143. end_t = 0;
  144. } else {
  145. end_t = static_cast<unsigned long int>(~0);
  146. }
  147. return end_t;
  148. }
  149. void
  150. AbstractControl::IndexWaitWakeup()
  151. {
  152. mRunState.stopped = true;
  153. SatPicList::Instance()->ToEnd();
  154. SatPicBuf *pBuf = SatPicList::Instance()->Current();
  155. if (pBuf) {
  156. ShowPic(pBuf);
  157. emit IndexArrived ();
  158. }
  159. }
  160. void
  161. AbstractControl::DoStepFwd ()
  162. {
  163. if (DBWaiting()) {
  164. return;
  165. }
  166. mRunState.stopped = true;
  167. SatPicList::Instance()->PostIncr();
  168. SatPicBuf *pBuf = SatPicList::Instance()->Current(false);
  169. if (pBuf) {
  170. ShowPic(pBuf);
  171. }
  172. }
  173. void
  174. AbstractControl::DoStepBack ()
  175. {
  176. if (DBWaiting()) {
  177. return;
  178. }
  179. mRunState.stopped = true;
  180. SatPicList::Instance()->PostDecr();
  181. SatPicBuf *pBuf = SatPicList::Instance()->Current(false);
  182. if (pBuf) {
  183. ShowPic(pBuf);
  184. }
  185. }
  186. void
  187. AbstractControl::DoWindBack (int secs, bool allway)
  188. {
  189. unsigned long int to = EndTime ( - secs);
  190. mRunState.timelimit = to;
  191. mRunState.allway = allway;
  192. mRunState.show = false;
  193. mRunState.stopped = false;
  194. mRunState.backwards = true;
  195. mRunState.pBuf = SatPicList::Instance()->Current(false);
  196. if (allway) {
  197. mRunState.pBuf = SatPicList::Instance()->ToStart();
  198. mRunState.stopped = true;
  199. mRunState.show = true;
  200. mPicState.SetNewBuf(mRunState.pBuf);
  201. ShowPic (mRunState.pBuf);
  202. return;
  203. }
  204. if (!DBWaiting()) {
  205. SatPicBuf *pBuf = SatPicList::Instance()->PostDecr();
  206. mRunState.pBuf = pBuf;
  207. }
  208. currentDelay = noshowTimeDelay;
  209. showTimer.setInterval(noshowTimeDelay);
  210. }
  211. void
  212. AbstractControl::DoRunBack (int secs, bool allway)
  213. {
  214. unsigned long int to = EndTime ( - secs);
  215. mRunState.timelimit = to;
  216. mRunState.allway = allway;
  217. mRunState.show = true;
  218. mRunState.stopped = false;
  219. mRunState.backwards = true;
  220. mRunState.pBuf = SatPicList::Instance()->Current(false);
  221. if (!DBWaiting()) {
  222. SatPicBuf *pBuf = SatPicList::Instance()->PostDecr();
  223. if (pBuf) {
  224. ShowPic(pBuf);
  225. }
  226. mRunState.pBuf = pBuf;
  227. }
  228. currentDelay = showTimeDelay;
  229. showTimer.setInterval(showTimeDelay);
  230. }
  231. void
  232. AbstractControl::DoWindFwd (int secs, bool allway)
  233. {
  234. unsigned long int to = EndTime (+ secs);
  235. mRunState.timelimit = to;
  236. mRunState.allway = allway;
  237. mRunState.show = false;
  238. mRunState.stopped = false;
  239. mRunState.backwards = false;
  240. mRunState.pBuf = SatPicList::Instance()->Current();
  241. if (!DBWaiting()) {
  242. SatPicBuf *pBuf = SatPicList::Instance()->PostIncr();
  243. mRunState.pBuf = pBuf;
  244. }
  245. currentDelay = noshowTimeDelay;
  246. showTimer.setInterval(noshowTimeDelay);
  247. }
  248. void
  249. AbstractControl::DoRunFwd (int secs, bool allway)
  250. {
  251. unsigned long int to = EndTime (+ secs);
  252. mRunState.timelimit = to;
  253. mRunState.allway = allway;
  254. mRunState.show = true;
  255. mRunState.stopped = false;
  256. mRunState.backwards = false;
  257. mRunState.pBuf = SatPicList::Instance()->Current();
  258. if (!DBWaiting()) {
  259. SatPicBuf *pBuf = SatPicList::Instance()->PostIncr();
  260. if (pBuf) {
  261. ShowPic(pBuf);
  262. }
  263. mRunState.pBuf = pBuf;
  264. }
  265. currentDelay = showTimeDelay;
  266. showTimer.setInterval(showTimeDelay);
  267. }
  268. void
  269. AbstractControl::FwdSome ()
  270. {
  271. if (DBWaiting()) {
  272. return; // maybe later
  273. }
  274. unsigned long int to = mRunState.timelimit;
  275. bool allway = mRunState.allway;
  276. bool oldStopped = mRunState.stopped;
  277. SatPicBuf * pBuf = mRunState.pBuf;
  278. if (pBuf && (allway || (pBuf->Ident() <= to))) {
  279. pBuf = SatPicList::Instance()->PostIncr();
  280. if (pBuf && mRunState.show) {
  281. ShowPic(pBuf);
  282. }
  283. mRunState.pBuf = pBuf;
  284. mRunState.stopped |= (pBuf == 0) || !(allway || (pBuf->Ident() <= to));
  285. } else { // fell off end
  286. mRunState.stopped = true;
  287. if (allway) {
  288. pBuf = SatPicList::Instance()->ToEnd();
  289. }
  290. }
  291. if (mRunState.stopped != oldStopped) { /* if we stopped just now */
  292. if (pBuf) {
  293. ShowPic(pBuf);
  294. }
  295. currentDelay = showTimeDelay;
  296. showTimer.setInterval(showTimeDelay);
  297. }
  298. }
  299. void
  300. AbstractControl::BackSome ()
  301. {
  302. if (DBWaiting()) {
  303. return;
  304. }
  305. unsigned long int to = mRunState.timelimit;
  306. bool allway = mRunState.allway;
  307. bool oldStopped = mRunState.stopped;
  308. SatPicBuf * pBuf = mRunState.pBuf;
  309. if (pBuf && (allway || (pBuf->Ident() >= to))) {
  310. pBuf = SatPicList::Instance()->PostDecr();
  311. if (pBuf && mRunState.show) {
  312. ShowPic(pBuf);
  313. }
  314. mRunState.pBuf = pBuf;
  315. mRunState.stopped |= (pBuf == 0) || !(allway || (pBuf->Ident() >= to));
  316. } else { // fell off end
  317. mRunState.stopped = true;
  318. if (allway || pBuf == 0) {
  319. pBuf = SatPicList::Instance()->ToStart();
  320. }
  321. }
  322. if (mRunState.stopped != oldStopped) { /* if we stopped just now */
  323. if (pBuf) {
  324. ShowPic(pBuf);
  325. }
  326. showTimer.setInterval(showTimeDelay);
  327. }
  328. }
  329. void
  330. AbstractControl::DoStopMoving ()
  331. {
  332. mRunState.stopped = true;
  333. }
  334. void
  335. AbstractControl::DoShowMove ()
  336. {
  337. if (!mRunState.stopped) {
  338. showTimer.stop(); /** don't want to get called twice */
  339. if (mRunState.backwards) {
  340. BackSome();
  341. } else {
  342. FwdSome();
  343. }
  344. showTimer.start(currentDelay);
  345. if (mRunState.show) {
  346. emit ShowMove ();
  347. }
  348. }
  349. }
  350. void
  351. AbstractControl::ConnectDB ()
  352. {
  353. }
  354. void
  355. AbstractControl::CannotLoadMsg ()
  356. {
  357. QMessageBox msgBox;
  358. QTimer::singleShot(15000, &msgBox, SLOT(accept()));
  359. string badmsg = "Cannot Load from Server '" + mServer
  360. + "' using Method '" + mConMeth + "'";
  361. msgBox.setText(badmsg.c_str());
  362. msgBox.exec();
  363. }
  364. void
  365. AbstractControl::ReloadDB ()
  366. {
  367. SatPicList::Instance()->SetServer(mServer);
  368. SatPicList::Instance()->SetFilename(mPicname);
  369. SatPicList::Instance()->SetMethod(mMeth);
  370. SatPicList::Instance()->Ditch();
  371. bool loadedok = SatPicList::Instance()->LoadFromDB();
  372. if (!loadedok && !DBWaiting()) {
  373. CannotLoadMsg ();
  374. }
  375. if (!DBWaiting()) {
  376. DoWindFwd(0,true);
  377. DoStepFwd();
  378. }
  379. update();
  380. }
  381. string
  382. AbstractControl::Server ()
  383. {
  384. return mServer;
  385. }
  386. string
  387. AbstractControl::SetServer (string sv)
  388. {
  389. QString newname(sv.c_str());
  390. mServer = sv;
  391. return mServer;
  392. }
  393. string
  394. AbstractControl::Picname ()
  395. {
  396. return mPicname;
  397. }
  398. string
  399. AbstractControl::SetPicname (string pn)
  400. {
  401. QString newname(pn.c_str());
  402. mPicname = pn;
  403. return mPicname;
  404. }
  405. string
  406. AbstractControl::Remark ()
  407. {
  408. return mRemark;
  409. }
  410. string
  411. AbstractControl::SetRemark (string rm)
  412. {
  413. QString newremark(rm.c_str());
  414. mRemark = rm;
  415. return mRemark;
  416. }
  417. string
  418. AbstractControl::IdentTag ()
  419. {
  420. return mIdFancy;
  421. }
  422. string
  423. AbstractControl::SetIdentTag (string id)
  424. {
  425. QString newtext (id.c_str());
  426. mIdFancy = id;
  427. return mIdFancy;
  428. }
  429. string
  430. AbstractControl::Date ()
  431. {
  432. return mDate;
  433. }
  434. string
  435. AbstractControl::SetDate (string dt)
  436. {
  437. QString newdate(dt.c_str());
  438. mDate = dt;
  439. return mDate;
  440. }
  441. string
  442. AbstractControl::SetMinAge (unsigned long int ma)
  443. {
  444. return SetMinAge(berndsutil::toString(ma));
  445. }
  446. string
  447. AbstractControl::SetMinAge (string ma)
  448. {
  449. mMinHours = ma;
  450. QString age(ma.c_str());
  451. return mMinHours;
  452. }
  453. string
  454. AbstractControl::MinAge ()
  455. {
  456. return mMinHours;
  457. }
  458. string
  459. AbstractControl::SetMaxAge (unsigned long int ma)
  460. {
  461. return SetMaxAge (berndsutil::toString(ma));
  462. }
  463. string
  464. AbstractControl::SetMaxAge (string ma)
  465. {
  466. mMaxHours = ma;
  467. QString age(ma.c_str());
  468. return mMaxHours;
  469. }
  470. string
  471. AbstractControl::MaxAge ()
  472. {
  473. return mMaxHours;
  474. }
  475. string
  476. AbstractControl::SetConMeth (string cm)
  477. {
  478. bool is_dir = (cm == "dir");
  479. mConMeth = cm;
  480. mMeth = DBConnection::Con_Web; // safer
  481. if (is_dir) {
  482. mMeth = DBConnection::Con_MySql;
  483. }
  484. return mConMeth;
  485. }
  486. string
  487. AbstractControl::ConMeth ()
  488. {
  489. return mConMeth;
  490. }
  491. void
  492. AbstractControl::update ()
  493. {
  494. }
  495. }