org_document.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /**
  2. * @file org_document.c
  3. */
  4. /*
  5. * This program is free software: you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation, either vers* ion 3 of
  8. * the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see
  17. * <http://www.gnu.org/licenses/>.
  18. */
  19. #include "debug.h"
  20. #include "config.h"
  21. #include "gl_array_list.h"
  22. #include "gl_list.h"
  23. #include "merge_ctxt.h"
  24. #include "doc_elt.h"
  25. #include "org_heading.h"
  26. #include "org_text.h"
  27. #include "org_document.h"
  28. struct org_document
  29. {
  30. doc_elt elt;
  31. gl_list_t subheadings;
  32. gl_list_t subtext;
  33. };
  34. typedef struct org_document org_document;
  35. /* Forward Declarations */
  36. static doc_elt_ops_print org_print_op;
  37. static doc_elt_ops_isrelated org_isrelated_op;
  38. static doc_elt_ops_compare org_compare_op;
  39. static doc_elt_ops_merge org_merge_op;
  40. static doc_elt_ops_isupdated org_isupdated_op;
  41. /* Declaration of org_element operations table */
  42. doc_elt_ops org_document_ops = {
  43. /* printing */
  44. .print = &org_print_op,
  45. /* comparing */
  46. .isrelated = &org_isrelated_op,
  47. .compare = &org_compare_op,
  48. /* merging */
  49. .merge = &org_merge_op,
  50. .isupdated = &org_isupdated_op
  51. };
  52. /* Constructor, Destructor */
  53. org_document *
  54. org_document_create_empty (doc_elt_ops *ops)
  55. {
  56. org_document *d = malloc (sizeof (org_document));
  57. doc_elt_set_ops (&(d->elt), ops);
  58. doc_elt_set_type (&(d->elt), ORG_DOCUMENT);
  59. d->subheadings = gl_list_nx_create_empty (GL_ARRAY_LIST, NULL,
  60. NULL, NULL, true);
  61. d->subtext = gl_list_nx_create_empty (GL_ARRAY_LIST, NULL,
  62. NULL, NULL, true);
  63. return d;
  64. }
  65. void
  66. org_document_free (org_document *self)
  67. {
  68. free (self);
  69. }
  70. /* Adding sub elements */
  71. void
  72. org_document_add_text_last (org_document *document, doc_src src, org_text *text)
  73. {
  74. /* wrap the element in a doc_ref, and add it to the list */
  75. doc_ref *ref = doc_ref_create_empty ();
  76. doc_ref_set_src (ref, src);
  77. doc_ref_set_elt (ref, (doc_elt *) text);
  78. /* tell the doc ref that the parent is immovable */
  79. doc_ref_set_parent (ref, NULL);
  80. gl_list_nx_add_last (document->subtext, ref);
  81. return;
  82. }
  83. void
  84. org_document_add_heading_last (org_document *document, doc_src src, org_heading *heading)
  85. {
  86. /* wrap the element in a doc_ref, and add it to the list */
  87. doc_ref *ref = doc_ref_create_empty ();
  88. doc_ref_set_src (ref, src);
  89. doc_ref_set_elt (ref, (doc_elt *) heading);
  90. /* tell the doc ref that the parent is immovable */
  91. doc_ref_set_parent (ref, NULL);
  92. org_heading_set_doc_ref (heading, ref);
  93. gl_list_nx_add_last (document->subheadings, ref);
  94. return;
  95. }
  96. void
  97. org_document_print (org_document *doc, print_ctxt *ctxt, doc_stream *out)
  98. {
  99. debug_msg (DOC_ELT, 3, "Print merging org_document\n");
  100. debug_msg (DOC_ELT, 5, "Printing text\n");
  101. doc_reflist_print (doc->subtext, ctxt, out);
  102. debug_msg (DOC_ELT, 3, "Printing headings\n");
  103. doc_reflist_print (doc->subheadings, ctxt, out);
  104. return;
  105. }
  106. void
  107. org_document_merge (org_document *anc, org_document *desc, merge_ctxt *ctxt)
  108. {
  109. debug_msg (DOC_ELT, 3, "Merging org_document\n");
  110. debug_msg (DOC_ELT, 5, "Merging text\n");
  111. doc_reflist_merge (NULL, anc->subtext, desc->subtext, ctxt);
  112. debug_msg (DOC_ELT, 3, "Merging headings\n");
  113. doc_reflist_merge (NULL, anc->subheadings, desc->subheadings, ctxt);
  114. return;
  115. }
  116. /* doc_elt interface */
  117. static void
  118. org_print_op (doc_ref *ref, print_ctxt *ctxt, doc_stream *out)
  119. {
  120. /**
  121. * @todo make sure the doc merge print
  122. */
  123. org_document *doc = (org_document *) doc_ref_get_elt (ref);
  124. org_document_print (doc, ctxt, out);
  125. return;
  126. }
  127. static bool
  128. org_isrelated_op (doc_ref *local, doc_ref *remote, merge_ctxt *ctxt)
  129. {
  130. /* all org_documents are relatable */
  131. return true;
  132. }
  133. static int
  134. org_compare_op (doc_elt *a, doc_src a_src, doc_elt *b, doc_src b_src)
  135. {
  136. /* all org_documents are identical */
  137. return true;
  138. }
  139. static void
  140. org_merge_op (doc_ref *a, doc_ref *b, merge_ctxt *ctxt)
  141. {
  142. /**
  143. * @todo actually merge things
  144. */
  145. return;
  146. }
  147. static bool
  148. org_isupdated_op (doc_ref *a)
  149. {
  150. /**
  151. * @todo implement an actual function
  152. */
  153. /* check if the children are updated */
  154. /* check if this element is updated */
  155. return true;
  156. }
  157. /* will call thes function on its children after it searches for
  158. itself */
  159. bool
  160. org_document_check_for_loop (org_document *this)
  161. {
  162. /* first check to see if the target exist anywhere below */
  163. gl_list_iterator_t i;
  164. i = gl_list_iterator (this->subheadings);
  165. doc_ref *ref = NULL;
  166. bool found = false;
  167. org_heading *heading;
  168. debug_msg (DOC,3, "checking for loops");
  169. while (gl_list_iterator_next (&i, (const void **) &ref, NULL))
  170. {
  171. heading = (org_heading *)doc_ref_get_elt (ref);
  172. org_heading_check_for_loop (heading);
  173. }
  174. gl_list_iterator_free (&i);
  175. return found;
  176. }