PageRenderTime 50ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/djvulibre-3.5.24/gui/shared/QT/qd_port.cpp

#
C++ | 409 lines | 314 code | 34 blank | 61 comment | 61 complexity | a3c11912915357c24d182d7a3427b028 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. //C- -*- C++ -*-
  2. //C- -------------------------------------------------------------------
  3. //C- DjVuLibre-3.5
  4. //C- Copyright (c) 2002 Leon Bottou and Yann Le Cun.
  5. //C- Copyright (c) 2001 AT&T
  6. //C-
  7. //C- This software is subject to, and may be distributed under, the
  8. //C- GNU General Public License, either Version 2 of the license,
  9. //C- or (at your option) any later version. The license should have
  10. //C- accompanied the software or you may obtain a copy of the license
  11. //C- from the Free Software Foundation at http://www.fsf.org .
  12. //C-
  13. //C- This program is distributed in the hope that it will be useful,
  14. //C- but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. //C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. //C- GNU General Public License for more details.
  17. //C-
  18. //C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library from
  19. //C- Lizardtech Software. Lizardtech Software has authorized us to
  20. //C- replace the original DjVu(r) Reference Library notice by the following
  21. //C- text (see doc/lizard2002.djvu and doc/lizardtech2007.djvu):
  22. //C-
  23. //C- ------------------------------------------------------------------
  24. //C- | DjVu (r) Reference Library (v. 3.5)
  25. //C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
  26. //C- | The DjVu Reference Library is protected by U.S. Pat. No.
  27. //C- | 6,058,214 and patents pending.
  28. //C- |
  29. //C- | This software is subject to, and may be distributed under, the
  30. //C- | GNU General Public License, either Version 2 of the license,
  31. //C- | or (at your option) any later version. The license should have
  32. //C- | accompanied the software or you may obtain a copy of the license
  33. //C- | from the Free Software Foundation at http://www.fsf.org .
  34. //C- |
  35. //C- | The computer code originally released by LizardTech under this
  36. //C- | license and unmodified by other parties is deemed "the LIZARDTECH
  37. //C- | ORIGINAL CODE." Subject to any third party intellectual property
  38. //C- | claims, LizardTech grants recipient a worldwide, royalty-free,
  39. //C- | non-exclusive license to make, use, sell, or otherwise dispose of
  40. //C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the
  41. //C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU
  42. //C- | General Public License. This grant only confers the right to
  43. //C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to
  44. //C- | the extent such infringement is reasonably necessary to enable
  45. //C- | recipient to make, have made, practice, sell, or otherwise dispose
  46. //C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to
  47. //C- | any greater extent that may be necessary to utilize further
  48. //C- | modifications or combinations.
  49. //C- |
  50. //C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
  51. //C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  52. //C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
  53. //C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  54. //C- +------------------------------------------------------------------
  55. #ifdef HAVE_CONFIG_H
  56. # include "config.h"
  57. #endif
  58. #if NEED_GNUG_PRAGMAS
  59. # pragma implementation
  60. #endif
  61. #include "qd_port.h"
  62. #include "debug.h"
  63. #include <ctype.h>
  64. //****************************************************************************
  65. //******************************* QDPort::Port *******************************
  66. //****************************************************************************
  67. bool
  68. QDPort::Port::notify_error(const DjVuPort * source, const GUTF8String &msg)
  69. {
  70. GMonitorLock lock(&disabled);
  71. return disabled ? false : port->notify_error(source, msg);
  72. }
  73. bool
  74. QDPort::Port::notify_status(const DjVuPort * source, const GUTF8String &msg)
  75. {
  76. GMonitorLock lock(&disabled);
  77. return disabled ? false : port->notify_status(source, msg);
  78. }
  79. void
  80. QDPort::Port::notify_redisplay(const DjVuImage * source)
  81. {
  82. GMonitorLock lock(&disabled);
  83. if (!disabled)
  84. port->notify_redisplay(source);
  85. }
  86. void
  87. QDPort::Port::notify_relayout(const DjVuImage * source)
  88. {
  89. GMonitorLock lock(&disabled);
  90. if (!disabled)
  91. port->notify_relayout(source);
  92. }
  93. void
  94. QDPort::Port::notify_chunk_done(const DjVuPort * source, const GUTF8String &qname)
  95. {
  96. // const char * const name=qname;
  97. GMonitorLock lock(&disabled);
  98. if (!disabled)
  99. port->notify_chunk_done(source, qname);
  100. }
  101. void
  102. QDPort::Port::notify_file_flags_changed(const DjVuFile * source,
  103. long set_mask, long clr_mask)
  104. {
  105. GMonitorLock lock(&disabled);
  106. if (!disabled)
  107. port->notify_file_flags_changed(source, set_mask, clr_mask);
  108. }
  109. void
  110. QDPort::Port::notify_doc_flags_changed(const DjVuDocument * source,
  111. long set_mask, long clr_mask)
  112. {
  113. GMonitorLock lock(&disabled);
  114. if (!disabled)
  115. port->notify_doc_flags_changed(source, set_mask, clr_mask);
  116. }
  117. void
  118. QDPort::Port::notify_decode_progress(const DjVuPort * source, float done)
  119. {
  120. GMonitorLock lock(&disabled);
  121. if (!disabled)
  122. port->notify_decode_progress(source, done);
  123. }
  124. //****************************************************************************
  125. //********************************* QDPort ***********************************
  126. //****************************************************************************
  127. QDPort::~QDPort(void)
  128. {
  129. port->disabled=1;
  130. port=0;
  131. }
  132. QDPort::QDPort(bool _watch_errors, bool _watch_status,
  133. QObject * parent, const char * name) :
  134. QObject(parent, name),
  135. watch_errors(_watch_errors), watch_status(_watch_status)
  136. {
  137. sig_error_on=sig_status_on=sig_redisplay_on=
  138. sig_relayout_on=sig_chunk_done_on=
  139. sig_file_flags_changed_on=sig_doc_flags_changed_on=
  140. sig_decode_progress_on=0;
  141. port=new Port(this);
  142. messenger.setLookAhead(1);
  143. connect(&messenger, SIGNAL(sigGeneralMsg(const GUTF8String &)),
  144. this, SLOT(slotGeneralMsg(const GUTF8String &)));
  145. }
  146. void
  147. QDPort::connectNotify(const char *s)
  148. {
  149. const char * ptr=strchr(s, '(');
  150. int len=ptr-s;
  151. if (!strncmp(s, SIGNAL(sigNotifyError(const GP<DjVuPort> &, const GUTF8String &)), len))
  152. sig_error_on=1;
  153. if (!strncmp(s, SIGNAL(sigNotifyStatus(const GP<DjVuPort> &, const QString &)), len))
  154. sig_status_on=1;
  155. if (!strncmp(s, SIGNAL(sigNotifyRedisplay(const GP<DjVuImage> &)), len))
  156. sig_redisplay_on=1;
  157. if (!strncmp(s, SIGNAL(sigNotifyRelayout(const GP<DjVuImage> &)), len))
  158. sig_relayout_on=1;
  159. if (!strncmp(s, SIGNAL(sigNotifyChunkDone(const GP<DjVuPort> &, const GUTF8String &)), len))
  160. sig_chunk_done_on=1;
  161. if (!strncmp(s, SIGNAL(sigNotifyFileFlagsChanged(const GP<DjVuFile> &, long, long)), len))
  162. sig_file_flags_changed_on=1;
  163. if (!strncmp(s, SIGNAL(sigNotifyDocFlagsChanged(const GP<DjVuDocument> &, long, long)), len))
  164. sig_doc_flags_changed_on=1;
  165. if (!strncmp(s, SIGNAL(sigNotifyDecodeProgress(const GP<DjVuPort> &, float)), len))
  166. sig_decode_progress_on=1;
  167. }
  168. void
  169. QDPort::disconnectNotify(const char *s)
  170. {
  171. const char * ptr=strchr(s, '(');
  172. int len=ptr-s;
  173. if (!strncmp(s, SIGNAL(sigNotifyError(const GP<DjVuPort> &, const GUTF8String &)), len))
  174. sig_error_on=0;
  175. if (!strncmp(s, SIGNAL(sigNotifyStatus(const GP<DjVuPort> &, const QString &)), len))
  176. sig_status_on=0;
  177. if (!strncmp(s, SIGNAL(sigNotifyRedisplay(const GP<DjVuImage> &)), len))
  178. sig_redisplay_on=0;
  179. if (!strncmp(s, SIGNAL(sigNotifyRelayout(const GP<DjVuImage> &)), len))
  180. sig_relayout_on=0;
  181. if (!strncmp(s, SIGNAL(sigNotifyChunkDone(const GP<DjVuPort> &, const GUTF8String &)), len))
  182. sig_chunk_done_on=0;
  183. if (!strncmp(s, SIGNAL(sigNotifyFileFlagsChanged(const GP<DjVuFile> &, long, long)), len))
  184. sig_file_flags_changed_on=0;
  185. if (!strncmp(s, SIGNAL(sigNotifyDocFlagsChanged(const GP<DjVuDocument> &, long, long)), len))
  186. sig_doc_flags_changed_on=0;
  187. if (!strncmp(s, SIGNAL(sigNotifyDecodeProgress(const GP<DjVuPort> &, float)), len))
  188. sig_decode_progress_on=0;
  189. }
  190. void
  191. QDPort::slotGeneralMsg(const GUTF8String &qmsg)
  192. {
  193. DEBUG_MSG("QDPort::slotGeneralMsg()\n");
  194. DEBUG_MAKE_INDENT(3);
  195. const char * msg=qmsg;
  196. const char * ptr;
  197. for(ptr=msg;*ptr && !isspace(*ptr);ptr++);
  198. GUTF8String call_name=GUTF8String(msg, ptr-msg);
  199. DEBUG_MSG("QDPort::slotGeneralMsg(): call_name=" << call_name << "\n");
  200. while(*ptr && isspace(*ptr)) ptr++;
  201. msg=ptr;
  202. while(*ptr && !isspace(*ptr)) ptr++;
  203. DEBUG_MSG("QDPort::slotGeneralMsg(): source=" << atol(GUTF8String(msg, ptr-msg)) << "\n");
  204. DjVuPort * source=(DjVuPort *) atol(GUTF8String(msg, ptr-msg));
  205. GP<DjVuPort> src;
  206. {
  207. GCriticalSectionLock lock(&src_lock);
  208. for(GPosition pos=src_list;pos;++pos)
  209. if (src_list[pos]==source)
  210. {
  211. src=src_list[pos];
  212. src_list.del(pos);
  213. break;
  214. }
  215. }
  216. if (src)
  217. {
  218. if (call_name=="notify_error")
  219. {
  220. while(*ptr && isspace(*ptr)) ptr++;
  221. emit sigNotifyError(src, ptr);
  222. } else if (call_name=="notify_status")
  223. {
  224. while(*ptr && isspace(*ptr)) ptr++;
  225. emit sigNotifyStatus(src, ptr);
  226. } else if (call_name=="notify_redisplay")
  227. {
  228. emit sigNotifyRedisplay((DjVuImage *) source);
  229. } else if (call_name=="notify_relayout")
  230. {
  231. emit sigNotifyRelayout((DjVuImage *) source);
  232. } else if (call_name=="notify_chunk_done")
  233. {
  234. while(*ptr && isspace(*ptr)) ptr++;
  235. emit sigNotifyChunkDone(src, ptr);
  236. } else if (call_name=="notify_file_flags_changed")
  237. {
  238. while(*ptr && isspace(*ptr)) ptr++;
  239. long set_mask, clr_mask;
  240. sscanf(ptr, "%ld %ld", &set_mask, &clr_mask);
  241. GP<DjVuFile> fsrc = (DjVuFile*)(DjVuPort*)src;
  242. emit sigNotifyFileFlagsChanged(fsrc, set_mask, clr_mask);
  243. } else if (call_name=="notify_doc_flags_changed")
  244. {
  245. while(*ptr && isspace(*ptr)) ptr++;
  246. long set_mask, clr_mask;
  247. sscanf(ptr, "%ld %ld", &set_mask, &clr_mask);
  248. emit sigNotifyDocFlagsChanged((DjVuDocument *) source, set_mask, clr_mask);
  249. } else if (call_name=="notify_decode_progress")
  250. {
  251. while(*ptr && isspace(*ptr)) ptr++;
  252. emit sigNotifyDecodeProgress(src, atof(ptr));
  253. } else G_THROW("Internal error: Unknown call name read from the pipe.");
  254. };
  255. }
  256. bool
  257. QDPort::notify_error(const DjVuPort * source, const GUTF8String &qmsg)
  258. {
  259. if (sig_error_on && watch_errors)
  260. {
  261. {
  262. GCriticalSectionLock lock(&src_lock);
  263. src_list.append((DjVuPort *) source);
  264. }
  265. char src_str[128];
  266. sprintf(src_str, "%ld", (long) source);
  267. GUTF8String mesg=GUTF8String("notify_error ")+src_str+" "+qmsg;
  268. messenger.generalMsg(mesg);
  269. return true;
  270. }
  271. return false;
  272. }
  273. bool
  274. QDPort::notify_status(const DjVuPort * source, const GUTF8String &msg)
  275. {
  276. if (sig_status_on && watch_status)
  277. {
  278. {
  279. GCriticalSectionLock lock(&src_lock);
  280. src_list.append((DjVuPort *) source);
  281. }
  282. char src_str[128];
  283. sprintf(src_str, "%ld", (long) source);
  284. GUTF8String mesg=GUTF8String("notify_status ")+src_str+" "+msg;
  285. messenger.generalMsg(mesg);
  286. return true;
  287. }
  288. return false;
  289. }
  290. void
  291. QDPort::notify_redisplay(const DjVuImage * source)
  292. {
  293. if (sig_redisplay_on)
  294. {
  295. {
  296. GCriticalSectionLock lock(&src_lock);
  297. src_list.append((DjVuPort *) source);
  298. }
  299. GUTF8String src_str;
  300. src_str.format("notify_redisplay %ld",(long)source);
  301. messenger.generalMsg(src_str);
  302. }
  303. }
  304. void
  305. QDPort::notify_relayout(const DjVuImage * source)
  306. {
  307. if (sig_relayout_on)
  308. {
  309. {
  310. GCriticalSectionLock lock(&src_lock);
  311. src_list.append((DjVuPort *) source);
  312. }
  313. GUTF8String src_str;
  314. src_str.format("notify_relayout %ld",(long)source);
  315. messenger.generalMsg(src_str);
  316. }
  317. }
  318. void
  319. QDPort::notify_chunk_done(const DjVuPort * source, const GUTF8String &name)
  320. {
  321. if (sig_chunk_done_on)
  322. {
  323. {
  324. GCriticalSectionLock lock(&src_lock);
  325. src_list.append((DjVuPort *) source);
  326. }
  327. GUTF8String src_str;
  328. src_str.format("notify_chunk_done %ld %s",(long)source,(const char *)name);
  329. messenger.generalMsg(src_str);
  330. }
  331. }
  332. void
  333. QDPort::notify_file_flags_changed(const DjVuFile * source,
  334. long set_mask, long clr_mask)
  335. {
  336. if (sig_file_flags_changed_on)
  337. {
  338. {
  339. GCriticalSectionLock lock(&src_lock);
  340. src_list.append((DjVuPort *) source);
  341. }
  342. GUTF8String src_str;
  343. src_str.format("notify_file_flags_changed %ld %ld %ld", (long) source, set_mask, clr_mask);
  344. messenger.generalMsg(src_str);
  345. }
  346. }
  347. void
  348. QDPort::notify_doc_flags_changed(const DjVuDocument * source,
  349. long set_mask, long clr_mask)
  350. {
  351. if (sig_doc_flags_changed_on)
  352. {
  353. {
  354. GCriticalSectionLock lock(&src_lock);
  355. src_list.append((DjVuPort *) source);
  356. }
  357. GUTF8String src_str;
  358. src_str.format("notify_doc_flags_changed %ld %ld %ld", (long) source, set_mask, clr_mask);
  359. messenger.generalMsg(src_str);
  360. }
  361. }
  362. void
  363. QDPort::notify_decode_progress(const DjVuPort * source, float done)
  364. {
  365. if (sig_decode_progress_on)
  366. {
  367. {
  368. GCriticalSectionLock lock(&src_lock);
  369. src_list.append((DjVuPort *) source);
  370. }
  371. GUTF8String src_str;
  372. src_str.format("notify_decode_progress %ld %g",(long)source,done);
  373. messenger.generalMsg(src_str);
  374. }
  375. }