tdf#130857 qt weld: Support mail merge "Server Auth" dialog
[LibreOffice.git] / dbaccess / source / ui / misc / RtfReader.cxx
blob8895d494f817e954fb62c18b7b91ebf9881f8e64
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/stream.hxx>
22 #include <com/sun/star/sdbcx/XDataDescriptorFactory.hpp>
23 #include <com/sun/star/awt/FontDescriptor.hpp>
24 #include <com/sun/star/awt/FontWeight.hpp>
25 #include <com/sun/star/awt/FontStrikeout.hpp>
26 #include <com/sun/star/awt/FontSlant.hpp>
27 #include <com/sun/star/awt/FontUnderline.hpp>
28 #include <core_resource.hxx>
29 #include <svtools/rtftoken.h>
30 #include <toolkit/helper/vclunohelper.hxx>
31 #include <strings.hrc>
32 #include <connectivity/dbtools.hxx>
33 #include <comphelper/string.hxx>
34 #include <tools/color.hxx>
35 #include <WExtendPages.hxx>
36 #include <vcl/svapp.hxx>
37 #include <vcl/settings.hxx>
39 using namespace dbaui;
40 using namespace ::com::sun::star::uno;
41 using namespace ::com::sun::star::beans;
42 using namespace ::com::sun::star::container;
43 using namespace ::com::sun::star::sdbc;
44 using namespace ::com::sun::star::sdbcx;
45 using namespace ::com::sun::star::awt;
47 // ORTFReader
48 ORTFReader::ORTFReader( SvStream& rIn,
49 const SharedConnection& _rxConnection,
50 const Reference< css::util::XNumberFormatter >& _rxNumberF,
51 const css::uno::Reference< css::uno::XComponentContext >& _rxContext)
52 :SvRTFParser(rIn)
53 ,ODatabaseExport( _rxConnection, _rxNumberF, _rxContext, rIn )
55 m_bAppendFirstLine = false;
58 ORTFReader::ORTFReader(SvStream& rIn,
59 sal_Int32 nRows,
60 TPositions&& _rColumnPositions,
61 const Reference< css::util::XNumberFormatter >& _rxNumberF,
62 const css::uno::Reference< css::uno::XComponentContext >& _rxContext,
63 const TColumnVector* pList,
64 const OTypeInfoMap* _pInfoMap,
65 bool _bAutoIncrementEnabled)
66 :SvRTFParser(rIn)
67 ,ODatabaseExport( nRows, std::move(_rColumnPositions), _rxNumberF, _rxContext, pList, _pInfoMap, _bAutoIncrementEnabled, rIn )
69 m_bAppendFirstLine = false;
72 ORTFReader::~ORTFReader()
76 SvParserState ORTFReader::CallParser()
78 rInput.Seek(STREAM_SEEK_TO_BEGIN);
79 rInput.ResetError();
80 SvParserState eParseState = SvRTFParser::CallParser();
81 SetColumnTypes(m_pColumnList,m_pInfoMap);
82 return m_bFoundTable ? eParseState : SvParserState::Error;
85 #if defined _MSC_VER
86 #pragma warning(disable: 4702) // unreachable code, bug in MSVC2015
87 #endif
89 void ORTFReader::NextToken( int nToken )
91 if(m_bError || !m_nRows) // if there is an error or no more rows to check, return immediately
92 return;
94 if(m_xConnection.is()) // names, which CTOR was called and hence, if a table should be created
96 switch(nToken)
98 case RTF_COLORTBL:
101 int nTmpToken2 = GetNextToken();
104 Color aColor;
107 switch(nTmpToken2)
109 case RTF_RED: aColor.SetRed(static_cast<sal_uInt8>(nTokenValue)); break;
110 case RTF_BLUE: aColor.SetBlue(static_cast<sal_uInt8>(nTokenValue)); break;
111 case RTF_GREEN: aColor.SetGreen(static_cast<sal_uInt8>(nTokenValue)); break;
112 default: break;
114 nTmpToken2 = GetNextToken();
116 while(aToken[0] != ';' && eState != SvParserState::Error && eState != SvParserState::Accepted);
117 m_vecColor.push_back(aColor.GetRGBColor());
118 nTmpToken2 = GetNextToken();
120 while(nTmpToken2 == RTF_RED && eState != SvParserState::Error && eState != SvParserState::Accepted);
121 SkipToken();
123 break;
125 case RTF_TROWD:
127 if ( !m_xTable.is() ) // use first line as header
129 sal_uInt64 const nTell = rInput.Tell(); // perhaps alters position of the stream
131 m_bError = !CreateTable(nToken);
132 if ( m_bAppendFirstLine )
134 rInput.Seek(nTell);
135 rInput.ResetError();
139 break;
140 case RTF_INTBL:
141 if(m_bInTbl)
143 eraseTokens();
146 m_bInTbl = true; // Now we are in a table description
147 break;
148 case RTF_TEXTTOKEN:
149 case RTF_SINGLECHAR:
150 if(m_bInTbl) // important, as otherwise we also get the names of the fonts
151 m_sTextToken += aToken;
152 break;
153 case RTF_CELL:
157 insertValueIntoColumn();
159 catch(SQLException& e)
160 // handling update failure
162 showErrorDialog(e);
164 m_nColumnPos++;
165 eraseTokens();
167 break;
168 case RTF_ROW:
169 // it can happen that the last cell is not concluded with \cell
172 insertValueIntoColumn();
173 m_nRowCount++;
174 if(m_bIsAutoIncrement) // if bSetAutoIncrement then I have to set the autoincrement
175 m_pUpdateHelper->updateInt(1,m_nRowCount);
176 m_pUpdateHelper->insertRow();
178 catch(SQLException& e)
179 // handling update failure
181 showErrorDialog(e);
183 m_nColumnPos = 0;
184 break;
187 else // branch only valid for type checking
189 switch(nToken)
191 case RTF_TROWD:
192 // The head of the column is not included
193 if(m_bHead)
197 while(GetNextToken() != RTF_ROW && eState != SvParserState::Error && eState != SvParserState::Accepted);
198 m_bHead = false;
200 break;
201 case RTF_INTBL:
202 m_bInTbl = true;
203 break;
204 case RTF_TEXTTOKEN:
205 case RTF_SINGLECHAR:
206 if(m_bInTbl)
207 m_sTextToken += aToken;
208 break;
209 case RTF_CELL:
210 adjustFormat();
211 m_nColumnPos++;
212 break;
213 case RTF_ROW:
214 adjustFormat();
215 m_nColumnPos = 0;
216 m_nRows--;
217 break;
222 bool ORTFReader::CreateTable(int nToken)
224 OUString aTableName(DBA_RES(STR_TBL_TITLE));
225 aTableName = aTableName.getToken(0,' ');
226 aTableName = ::dbtools::createUniqueName(m_xTables, aTableName);
228 OUString aColumnName;
230 FontDescriptor aFont = VCLUnoHelper::CreateFontDescriptor(Application::GetSettings().GetStyleSettings().GetAppFont());
233 switch (nToken)
235 case RTF_UNKNOWNCONTROL:
236 case RTF_UNKNOWNDATA:
237 m_bInTbl = false;
238 aColumnName.clear();
239 break;
240 case RTF_INTBL:
241 if(m_bInTbl)
242 aColumnName.clear();
244 m_bInTbl = true;
245 break;
246 case RTF_TEXTTOKEN:
247 case RTF_SINGLECHAR:
248 if(m_bInTbl)
249 aColumnName += aToken;
250 break;
251 case RTF_CELL:
253 aColumnName = comphelper::string::strip(aColumnName, ' ');
254 if (aColumnName.isEmpty() || m_bAppendFirstLine )
255 aColumnName = DBA_RES(STR_COLUMN_NAME);
257 CreateDefaultColumn(aColumnName);
258 aColumnName.clear();
260 break;
261 case RTF_CF:
262 break;
263 case RTF_B:
264 aFont.Weight = css::awt::FontWeight::BOLD;
265 break;
266 case RTF_I:
267 aFont.Slant = css::awt::FontSlant_ITALIC;
268 break;
269 case RTF_UL:
270 aFont.Underline = css::awt::FontUnderline::SINGLE;
271 break;
272 case RTF_STRIKE:
273 aFont.Strikeout = css::awt::FontStrikeout::SINGLE;
274 break;
276 nToken = GetNextToken();
278 while(nToken != RTF_TROWD && eState != SvParserState::Error && eState != SvParserState::Accepted);
280 bool bOk = !m_vDestVector.empty();
281 if(bOk)
283 if ( !aColumnName.isEmpty() )
285 if ( m_bAppendFirstLine )
286 aColumnName = DBA_RES(STR_COLUMN_NAME);
287 CreateDefaultColumn(aColumnName);
290 m_bInTbl = false;
291 m_bFoundTable = true;
293 if ( isCheckEnabled() )
294 return true;
295 Any aTextColor;
296 if(!m_vecColor.empty())
297 aTextColor <<= m_vecColor[0];
299 bOk = !executeWizard(aTableName,aTextColor,aFont) && m_xTable.is();
301 return bOk;
304 TypeSelectionPageFactory ORTFReader::getTypeSelectionPageFactory()
306 return &OWizRTFExtend::Create;
309 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */