1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6  */
7 
8 #ifndef INCLUDED_ORCUS_SPREADSHEET_DOCUMENT_HPP
9 #define INCLUDED_ORCUS_SPREADSHEET_DOCUMENT_HPP
10 
11 #include "orcus/env.hpp"
12 #include "orcus/interface.hpp"
13 #include "orcus/spreadsheet/types.hpp"
14 
15 #include <ostream>
16 #include <memory>
17 
18 namespace ixion {
19 
20 class formula_name_resolver;
21 class model_context;
22 struct abs_address_t;
23 
24 }
25 
26 namespace orcus {
27 
28 class pstring;
29 class string_pool;
30 struct date_time_t;
31 
32 namespace spreadsheet {
33 
34 class import_shared_strings;
35 class styles;
36 class pivot_collection;
37 class sheet;
38 
39 struct document_config;
40 struct table_t;
41 struct document_impl;
42 
43 /**
44  * Internal document representation used only for testing the filters.  It
45  * uses ixion's model_context implementation to store raw cell values.
46  */
47 class ORCUS_SPM_DLLPUBLIC document : public orcus::iface::document_dumper
48 {
49     friend class sheet;
50 
51 public:
52     document(const document&) = delete;
53     document& operator= (const document&) = delete;
54 
55     document(const range_size_t& sheet_size);
56     ~document();
57 
58     import_shared_strings* get_shared_strings();
59     const import_shared_strings* get_shared_strings() const;
60 
61     styles& get_styles();
62     const styles& get_styles() const;
63 
64     pivot_collection& get_pivot_collection();
65     const pivot_collection& get_pivot_collection() const;
66 
67     sheet* append_sheet(const pstring& sheet_name);
68     sheet* get_sheet(const pstring& sheet_name);
69     const sheet* get_sheet(const pstring& sheet_name) const;
70     sheet* get_sheet(sheet_t sheet_pos);
71     const sheet* get_sheet(sheet_t sheet_pos) const;
72 
73     /**
74      * Clear document content, to make it empty.
75      */
76     void clear();
77 
78     /**
79      * Calculate those formula cells that have been newly inserted and have
80      * not yet been calculated.
81      */
82     void recalc_formula_cells();
83 
84     virtual void dump(dump_format_t format, const std::string& output) const override;
85 
86     /**
87      * Dump document content to specified output directory.
88      */
89     void dump_flat(const std::string& outdir) const;
90 
91     /**
92      * File name should not contain an extension.  The final name will be
93      * [filename] + _ + [sheet name] + .html.
94      *
95      * @param filename base file name
96      */
97     void dump_html(const ::std::string& outdir) const;
98 
99     void dump_json(const ::std::string& outdir) const;
100 
101     void dump_csv(const std::string& outdir) const;
102 
103     /**
104      * Dump document content to stdout in the special format used for content
105      * verification during unit test.
106      */
107     virtual void dump_check(std::ostream& os) const override;
108 
109     sheet_t get_sheet_index(const pstring& name) const;
110     pstring get_sheet_name(sheet_t sheet_pos) const;
111 
112     range_size_t get_sheet_size() const;
113     void set_sheet_size(const range_size_t& sheet_size);
114     size_t get_sheet_count() const;
115 
116     void set_origin_date(int year, int month, int day);
117     date_time_t get_origin_date() const;
118 
119     void set_formula_grammar(formula_grammar_t grammar);
120     formula_grammar_t get_formula_grammar() const;
121 
122     const ixion::formula_name_resolver* get_formula_name_resolver(formula_ref_context_t cxt) const;
123 
124     ixion::model_context& get_model_context();
125     const ixion::model_context& get_model_context() const;
126 
127     const document_config& get_config() const;
128     void set_config(const document_config& cfg);
129 
130     string_pool& get_string_pool();
131 
132     /**
133      * Insert a new table object into the document.  The document will take
134      * ownership of the inserted object after the call.  The object will get
135      * inserted only when there is no pre-existing table object of the same
136      * name.  The object not being inserted will be deleted.
137      *
138      * @param p table object to insert.
139      */
140     void insert_table(table_t* p);
141 
142     const table_t* get_table(const pstring& name) const;
143 
144     void finalize();
145 
146 private:
147     void insert_dirty_cell(const ixion::abs_address_t& pos);
148 
149 private:
150     std::unique_ptr<document_impl> mp_impl;
151 };
152 
153 }}
154 
155 #endif
156 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
157