1 /* AbiSource
2  *
3  * Copyright (C) 2005 INdT
4  * Author: Daniel d'Andrada T. de Carvalho <daniel.carvalho@indt.org.br>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301 USA.
20  */
21 
22 // Class definition include
23 #include "ODe_AutomaticStyles.h"
24 
25 // Internal includes
26 #include "ODe_Common.h"
27 
28 // Internal classes
29 #include "ODe_Style_Style.h"
30 #include "ODe_Style_PageLayout.h"
31 #include "ODe_Style_List.h"
32 
33 
34 /**
35  * Destructor
36  */
~ODe_AutomaticStyles()37 ODe_AutomaticStyles::~ODe_AutomaticStyles() {
38     UT_GenericVector<ODe_Style_Style*>* pStyleVector;
39     UT_GenericVector<ODe_Style_PageLayout*>* pPageLayoutVector;
40     UT_GenericVector<ODe_Style_List*>* pListStyleVector;
41 
42     pStyleVector = m_textStyles.enumerate();
43     UT_VECTOR_PURGEALL(ODe_Style_Style*, (*pStyleVector));
44 
45     pStyleVector = m_paragraphStyles.enumerate();
46     UT_VECTOR_PURGEALL(ODe_Style_Style*, (*pStyleVector));
47 
48     pStyleVector = m_sectionStyles.enumerate();
49     UT_VECTOR_PURGEALL(ODe_Style_Style*, (*pStyleVector));
50 
51     pStyleVector = m_tableStyles.enumerate();
52     UT_VECTOR_PURGEALL(ODe_Style_Style*, (*pStyleVector));
53 
54     pStyleVector = m_tableColumnStyles.enumerate();
55     UT_VECTOR_PURGEALL(ODe_Style_Style*, (*pStyleVector));
56 
57     pStyleVector = m_tableRowStyles.enumerate();
58     UT_VECTOR_PURGEALL(ODe_Style_Style*, (*pStyleVector));
59 
60     pStyleVector = m_tableCellStyles.enumerate();
61     UT_VECTOR_PURGEALL(ODe_Style_Style*, (*pStyleVector));
62 
63     pStyleVector = m_graphicStyles.enumerate();
64     UT_VECTOR_PURGEALL(ODe_Style_Style*, (*pStyleVector));
65     DELETEP(pStyleVector);
66 
67     pPageLayoutVector = m_pageLayouts.enumerate();
68     UT_VECTOR_PURGEALL(ODe_Style_PageLayout*, (*pPageLayoutVector));
69     DELETEP(pPageLayoutVector);
70 
71     pListStyleVector = m_listStyles.enumerate();
72     UT_VECTOR_PURGEALL(ODe_Style_List*, (*pListStyleVector));
73     DELETEP(pListStyleVector);
74 }
75 
76 
77 /**
78  * See ODe_AutomaticStyles::_storeStyle
79  */
storeTextStyle(ODe_Style_Style * & rpTextStyle)80 void ODe_AutomaticStyles::storeTextStyle(ODe_Style_Style*& rpTextStyle) {
81     _storeStyle(rpTextStyle, m_textStyles, "T");
82 }
83 
84 
85 /**
86  * See ODe_AutomaticStyles::_storeStyle
87  */
storeParagraphStyle(ODe_Style_Style * & rpParagraphStyle)88 void ODe_AutomaticStyles::storeParagraphStyle(ODe_Style_Style*& rpParagraphStyle) {
89     _storeStyle(rpParagraphStyle, m_paragraphStyles, "P");
90 }
91 
92 
93 /**
94  * See ODe_AutomaticStyles::_storeStyle
95  */
storeSectionStyle(ODe_Style_Style * & rpSectionStyle)96 void ODe_AutomaticStyles::storeSectionStyle(ODe_Style_Style*& rpSectionStyle) {
97     _storeStyle(rpSectionStyle, m_sectionStyles, "Sect");
98 }
99 
100 
101 /**
102  * See ODe_AutomaticStyles::_storeStyle
103  */
storeGraphicStyle(ODe_Style_Style * & rpGraphicStyle)104 void ODe_AutomaticStyles::storeGraphicStyle(ODe_Style_Style*& rpGraphicStyle) {
105     _storeStyle(rpGraphicStyle, m_graphicStyles, "graphic");
106 }
107 
108 
109 /**
110  *
111  */
addTableStyle(const UT_UTF8String & rStyleName)112 ODe_Style_Style* ODe_AutomaticStyles::addTableStyle(
113                                             const UT_UTF8String& rStyleName) {
114 
115     ODe_Style_Style* pStyle;
116 
117     pStyle = new ODe_Style_Style();
118     pStyle->setStyleName(rStyleName);
119     pStyle->setFamily("table");
120 
121     m_tableStyles.insert(rStyleName.utf8_str(), pStyle);
122 
123     return pStyle;
124 }
125 
126 
127 /**
128  *
129  */
addTableColumnStyle(const UT_UTF8String & rStyleName)130 ODe_Style_Style* ODe_AutomaticStyles::addTableColumnStyle(
131                                             const UT_UTF8String& rStyleName) {
132 
133     ODe_Style_Style* pStyle;
134 
135     pStyle = new ODe_Style_Style();
136     pStyle->setStyleName(rStyleName);
137     pStyle->setFamily("table-column");
138 
139     m_tableColumnStyles.insert(rStyleName.utf8_str(), pStyle);
140 
141     return pStyle;
142 }
143 
144 
145 /**
146  *
147  */
addTableRowStyle(const UT_UTF8String & rStyleName)148 ODe_Style_Style* ODe_AutomaticStyles::addTableRowStyle(
149                                             const UT_UTF8String& rStyleName) {
150 
151     ODe_Style_Style* pStyle;
152 
153     pStyle = new ODe_Style_Style();
154     pStyle->setStyleName(rStyleName);
155     pStyle->setFamily("table-row");
156 
157     m_tableRowStyles.insert(rStyleName.utf8_str(), pStyle);
158 
159     return pStyle;
160 }
161 
162 
163 /**
164  *
165  */
addTableCellStyle(const UT_UTF8String & rStyleName)166 ODe_Style_Style* ODe_AutomaticStyles::addTableCellStyle(
167                                             const UT_UTF8String& rStyleName) {
168     ODe_Style_Style* pStyle;
169 
170     pStyle = new ODe_Style_Style();
171     pStyle->setStyleName(rStyleName);
172     pStyle->setFamily("table-cell");
173 
174     m_tableCellStyles.insert(rStyleName.utf8_str(), pStyle);
175 
176     return pStyle;
177 }
178 
179 
180 /**
181  *
182  */
addPageLayout()183 ODe_Style_PageLayout* ODe_AutomaticStyles::addPageLayout() {
184     ODe_Style_PageLayout* pStyle;
185     UT_UTF8String styleName;
186 
187     UT_UTF8String_sprintf(styleName, "PLayout%d", m_pageLayouts.size() + 1);
188 
189     pStyle = new ODe_Style_PageLayout();
190     pStyle->setName(styleName);
191 
192     m_pageLayouts.insert(styleName.utf8_str(), pStyle);
193 
194     return pStyle;
195 }
196 
197 
198 /**
199  *
200  */
addListStyle()201 ODe_Style_List* ODe_AutomaticStyles::addListStyle() {
202     ODe_Style_List* pStyle;
203     UT_UTF8String styleName;
204 
205     UT_UTF8String_sprintf(styleName, "L%d", m_listStyles.size() + 1);
206 
207     pStyle = new ODe_Style_List();
208     pStyle->setName(styleName);
209 
210     m_listStyles.insert(styleName.utf8_str(), pStyle);
211 
212     return pStyle;
213 }
214 
215 
216 /**
217  *
218  */
addPageLayout(ODe_Style_PageLayout * & pPageLayout)219 void ODe_AutomaticStyles::addPageLayout(ODe_Style_PageLayout*& pPageLayout) {
220     m_pageLayouts.insert(pPageLayout->getName(), pPageLayout);
221 }
222 
223 
224 /**
225  * Writes <office:automatic-styles> element.
226  */
write(GsfOutput * pContentStream) const227 void ODe_AutomaticStyles::write(GsfOutput* pContentStream) const {
228 
229     UT_GenericVector<ODe_Style_Style*>* pStyleVector;
230     UT_GenericVector<ODe_Style_PageLayout*>* pPageLayoutVector;
231     UT_GenericVector<ODe_Style_List*>* pListStyleVector;
232     UT_uint32 i, count;
233     UT_UTF8String spacesOffset = "  ";
234 
235     ODe_writeUTF8String(pContentStream, " <office:automatic-styles>\n");
236 
237 #define ODE_WRITE_STYLES(styleMap) \
238     pStyleVector = styleMap.enumerate(); \
239     count = pStyleVector->getItemCount(); \
240     for (i=0; i<count; i++) { \
241         (*pStyleVector)[i]->write(pContentStream, spacesOffset); \
242     } \
243     DELETEP(pStyleVector);
244 
245 
246     ODE_WRITE_STYLES (m_textStyles);
247     ODE_WRITE_STYLES (m_paragraphStyles);
248     ODE_WRITE_STYLES (m_sectionStyles);
249     ODE_WRITE_STYLES (m_tableStyles);
250     ODE_WRITE_STYLES (m_tableColumnStyles);
251     ODE_WRITE_STYLES (m_tableRowStyles);
252     ODE_WRITE_STYLES (m_tableCellStyles);
253     ODE_WRITE_STYLES (m_graphicStyles);
254 
255 #undef ODE_WRITE_STYLES
256 
257     pPageLayoutVector = m_pageLayouts.enumerate();
258     count = pPageLayoutVector->getItemCount();
259     for (i=0; i<count; i++) {
260         (*pPageLayoutVector)[i]->write(pContentStream, spacesOffset);
261     }
262 
263     pListStyleVector = m_listStyles.enumerate();
264     count = pListStyleVector->getItemCount();
265     for (i=0; i<count; i++) {
266         (*pListStyleVector)[i]->write(pContentStream, spacesOffset);
267     }
268 
269     ODe_writeUTF8String(pContentStream, " </office:automatic-styles>\n");
270 }
271 
272 
273 /**
274  * Store the style in this automatic styles holder. As the specified
275  * style get's stored here, this class takes care of freeing its memory later, so
276  * you don't have to worry about freeing the memory of the stored style.
277  *
278  * The style also get's it's unique name on this method.
279  *
280  * After calling this method you may end up with your style pointer pointing to
281  * a different style. It happens when there is already a stored style equivalent
282  * to the one that you sent to be stored. The one that was passed is deleted.
283  */
_storeStyle(ODe_Style_Style * & rpStyle,UT_GenericStringMap<ODe_Style_Style * > & rStyles,const char * pNamingPrefix)284 void ODe_AutomaticStyles::_storeStyle(ODe_Style_Style*& rpStyle,
285                      UT_GenericStringMap<ODe_Style_Style*>& rStyles,
286                      const char* pNamingPrefix) {
287 
288     UT_GenericVector<ODe_Style_Style*>* pStyleVector;
289     ODe_Style_Style* pStyle;
290     bool isDuplicated;
291     UT_uint32 i, count;
292 
293     pStyleVector = rStyles.enumerate();
294     count = pStyleVector->getItemCount();
295 
296     for (i=0, isDuplicated=false; i<count && isDuplicated==false; i++) {
297 
298         pStyle = pStyleVector->getNthItem(i);
299         if ( pStyle->isEquivalentTo(*rpStyle) ) {
300             isDuplicated = true; // exit the loop
301             delete rpStyle; // We don't want a duplicated style.
302             rpStyle = pStyle;
303         }
304     }
305 
306 
307     if (!isDuplicated) {
308         // Let's name and store this style.
309         UT_UTF8String styleName;
310 
311         UT_UTF8String_sprintf(styleName, "%s%d", pNamingPrefix, count+1);
312 
313         rpStyle->setStyleName(styleName);
314         rStyles.insert(styleName.utf8_str(), rpStyle);
315     }
316 }
317