Bump version to 5.0-14
[LibreOffice.git] / dbaccess / source / ui / misc / RtfReader.cxx
bloba4ece11b42f5b4860f512909cdb5cbdfd802ffe4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "RtfReader.hxx"
21 #include <tools/debug.hxx>
22 #include <com/sun/star/sdbcx/XDataDescriptorFactory.hpp>
23 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
24 #include <com/sun/star/sdbcx/XAppend.hpp>
25 #include <com/sun/star/sdbc/DataType.hpp>
26 #include <com/sun/star/sdbc/ColumnValue.hpp>
27 #include <com/sun/star/awt/FontDescriptor.hpp>
28 #include <com/sun/star/awt/FontWeight.hpp>
29 #include <com/sun/star/awt/FontStrikeout.hpp>
30 #include <com/sun/star/awt/FontSlant.hpp>
31 #include <com/sun/star/awt/FontUnderline.hpp>
32 #include <com/sun/star/util/NumberFormat.hpp>
33 #include <com/sun/star/util/XNumberFormatTypes.hpp>
34 #include "dbustrings.hrc"
35 #include <svtools/rtftoken.h>
36 #include <toolkit/helper/vclunohelper.hxx>
37 #include "dbu_misc.hrc"
38 #include <vcl/msgbox.hxx>
39 #include <connectivity/dbconversion.hxx>
40 #include <connectivity/dbtools.hxx>
41 #include <comphelper/extract.hxx>
42 #include <comphelper/string.hxx>
43 #include <tools/color.hxx>
44 #include "WExtendPages.hxx"
45 #include "moduledbu.hxx"
46 #include "QEnumTypes.hxx"
47 #include "UITools.hxx"
48 #include <vcl/svapp.hxx>
49 #include <vcl/settings.hxx>
51 using namespace dbaui;
52 using namespace ::com::sun::star::uno;
53 using namespace ::com::sun::star::beans;
54 using namespace ::com::sun::star::container;
55 using namespace ::com::sun::star::sdbc;
56 using namespace ::com::sun::star::sdbcx;
57 using namespace ::com::sun::star::awt;
59 // ORTFReader
60 ORTFReader::ORTFReader( SvStream& rIn,
61 const SharedConnection& _rxConnection,
62 const Reference< ::com::sun::star::util::XNumberFormatter >& _rxNumberF,
63 const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& _rxContext,
64 const TColumnVector* pList,
65 const OTypeInfoMap* _pInfoMap)
66 :SvRTFParser(rIn)
67 ,ODatabaseExport( _rxConnection, _rxNumberF, _rxContext, pList, _pInfoMap, rIn )
69 m_bAppendFirstLine = false;
72 ORTFReader::ORTFReader(SvStream& rIn,
73 sal_Int32 nRows,
74 const TPositions &_rColumnPositions,
75 const Reference< ::com::sun::star::util::XNumberFormatter >& _rxNumberF,
76 const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& _rxContext,
77 const TColumnVector* pList,
78 const OTypeInfoMap* _pInfoMap,
79 bool _bAutoIncrementEnabled)
80 :SvRTFParser(rIn)
81 ,ODatabaseExport( nRows, _rColumnPositions, _rxNumberF, _rxContext, pList, _pInfoMap, _bAutoIncrementEnabled, rIn )
83 m_bAppendFirstLine = false;
86 ORTFReader::~ORTFReader()
90 SvParserState ORTFReader::CallParser()
92 rInput.Seek(STREAM_SEEK_TO_BEGIN);
93 rInput.ResetError();
94 SvParserState eParseState = SvRTFParser::CallParser();
95 SetColumnTypes(m_pColumnList,m_pInfoMap);
96 return m_bFoundTable ? eParseState : SVPAR_ERROR;
99 void ORTFReader::NextToken( int nToken )
101 if(m_bError || !m_nRows) // if there is an error or no more rows to check, return immediatelly
102 return;
104 if(m_xConnection.is()) // names, which CTOR was called and hence, if a table should be created
106 switch(nToken)
108 case RTF_COLORTBL:
111 int nTmpToken2 = GetNextToken();
114 Color aColor;
117 switch(nTmpToken2)
119 case RTF_RED: aColor.SetRed((sal_uInt8)nTokenValue); break;
120 case RTF_BLUE: aColor.SetBlue((sal_uInt8)nTokenValue); break;
121 case RTF_GREEN: aColor.SetGreen((sal_uInt8)nTokenValue); break;
122 default: break;
124 nTmpToken2 = GetNextToken();
126 while(aToken[0] != ';' && eState != SVPAR_ERROR && eState != SVPAR_ACCEPTED);
127 m_vecColor.push_back(aColor.GetRGBColor());
128 nTmpToken2 = GetNextToken();
130 while(nTmpToken2 == RTF_RED && eState != SVPAR_ERROR && eState != SVPAR_ACCEPTED);
131 SkipToken();
133 break;
135 case RTF_DEFLANG:
136 case RTF_LANG: // inquire language
137 m_nDefToken = (rtl_TextEncoding)nTokenValue;
138 break;
139 case RTF_TROWD:
141 bool bInsertRow = true;
142 if ( !m_xTable.is() ) // use first line as header
144 sal_Size nTell = rInput.Tell(); // perhaps alters position of the stream
146 m_bError = !CreateTable(nToken);
147 bInsertRow = m_bAppendFirstLine;
148 if ( m_bAppendFirstLine )
150 rInput.Seek(nTell);
151 rInput.ResetError();
154 if ( bInsertRow && !m_bError)
158 m_pUpdateHelper->moveToInsertRow(); // otherwise append new line
160 catch(SQLException& e)
161 // handling update failure
163 showErrorDialog(e);
167 break;
168 case RTF_INTBL:
169 if(m_bInTbl)
171 eraseTokens();
174 m_bInTbl = true; // Now we are in a table description
175 break;
176 case RTF_TEXTTOKEN:
177 case RTF_SINGLECHAR:
178 if(m_bInTbl) // important, as otherwise we also get the names of the fonts
179 m_sTextToken += aToken;
180 break;
181 case RTF_CELL:
185 insertValueIntoColumn();
187 catch(SQLException& e)
188 // handling update failure
190 showErrorDialog(e);
192 m_nColumnPos++;
193 eraseTokens();
195 break;
196 case RTF_ROW:
197 // it can happen that the last cell is not concluded with \cell
200 insertValueIntoColumn();
201 m_nRowCount++;
202 if(m_bIsAutoIncrement) // if bSetAutoIncrement then I have to set the autoincrement
203 m_pUpdateHelper->updateInt(1,m_nRowCount);
204 m_pUpdateHelper->insertRow();
206 catch(SQLException& e)
207 // handling update failure
209 showErrorDialog(e);
211 m_nColumnPos = 0;
212 break;
215 else // branch only valid for type checking
217 switch(nToken)
219 case RTF_TROWD:
220 // The head of the column is not included
221 if(m_bHead)
225 while(GetNextToken() != RTF_ROW && eState != SVPAR_ERROR && eState != SVPAR_ACCEPTED);
226 m_bHead = false;
228 break;
229 case RTF_INTBL:
230 m_bInTbl = true;
231 break;
232 case RTF_TEXTTOKEN:
233 case RTF_SINGLECHAR:
234 if(m_bInTbl)
235 m_sTextToken += aToken;
236 break;
237 case RTF_CELL:
238 adjustFormat();
239 m_nColumnPos++;
240 break;
241 case RTF_ROW:
242 adjustFormat();
243 m_nColumnPos = 0;
244 m_nRows--;
245 break;
250 bool ORTFReader::CreateTable(int nToken)
252 OUString aTableName(ModuleRes(STR_TBL_TITLE));
253 aTableName = aTableName.getToken(0,' ');
254 aTableName = ::dbtools::createUniqueName(m_xTables, aTableName);
256 OUString aColumnName;
258 FontDescriptor aFont = VCLUnoHelper::CreateFontDescriptor(Application::GetSettings().GetStyleSettings().GetAppFont());
261 switch (nToken)
263 case RTF_UNKNOWNCONTROL:
264 case RTF_UNKNOWNDATA:
265 m_bInTbl = false;
266 aColumnName.clear();
267 break;
268 case RTF_INTBL:
269 if(m_bInTbl)
270 aColumnName.clear();
272 m_bInTbl = true;
273 break;
274 case RTF_TEXTTOKEN:
275 case RTF_SINGLECHAR:
276 if(m_bInTbl)
277 aColumnName += aToken;
278 break;
279 case RTF_CELL:
281 aColumnName = comphelper::string::strip(aColumnName, ' ');
282 if (aColumnName.isEmpty() || m_bAppendFirstLine )
283 aColumnName = ModuleRes(STR_COLUMN_NAME);
285 CreateDefaultColumn(aColumnName);
286 aColumnName.clear();
288 break;
289 case RTF_CF:
290 break;
291 case RTF_B:
292 aFont.Weight = ::com::sun::star::awt::FontWeight::BOLD;
293 break;
294 case RTF_I:
295 aFont.Slant = ::com::sun::star::awt::FontSlant_ITALIC;
296 break;
297 case RTF_UL:
298 aFont.Underline = ::com::sun::star::awt::FontUnderline::SINGLE;
299 break;
300 case RTF_STRIKE:
301 aFont.Strikeout = ::com::sun::star::awt::FontStrikeout::SINGLE;
302 break;
304 nToken = GetNextToken();
306 while(nToken != RTF_TROWD && eState != SVPAR_ERROR && eState != SVPAR_ACCEPTED);
308 bool bOk = !m_vDestVector.empty();
309 if(bOk)
311 if ( !aColumnName.isEmpty() )
313 if ( m_bAppendFirstLine )
314 aColumnName = ModuleRes(STR_COLUMN_NAME);
315 CreateDefaultColumn(aColumnName);
318 m_bInTbl = false;
319 m_bFoundTable = true;
321 if ( isCheckEnabled() )
322 return true;
323 Any aTextColor;
324 if(!m_vecColor.empty())
325 aTextColor <<= m_vecColor[0];
327 bOk = !executeWizard(aTableName,aTextColor,aFont) && m_xTable.is();
329 return bOk;
332 void ORTFReader::release()
334 ReleaseRef();
337 TypeSelectionPageFactory ORTFReader::getTypeSelectionPageFactory()
339 return &OWizRTFExtend::Create;
342 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */