1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_dbaccess.hxx"
30 #include "RtfReader.hxx"
31 #include <tools/debug.hxx>
32 #include <com/sun/star/sdbcx/XDataDescriptorFactory.hpp>
33 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
34 #include <com/sun/star/sdbcx/XAppend.hpp>
35 #include <com/sun/star/sdbc/DataType.hpp>
36 #include <com/sun/star/sdbc/ColumnValue.hpp>
37 #include <com/sun/star/awt/FontDescriptor.hpp>
38 #include <com/sun/star/awt/FontWeight.hpp>
39 #include <com/sun/star/awt/FontStrikeout.hpp>
40 #include <com/sun/star/awt/FontSlant.hpp>
41 #include <com/sun/star/awt/FontUnderline.hpp>
42 #include <com/sun/star/util/NumberFormat.hpp>
43 #include <com/sun/star/util/XNumberFormatTypes.hpp>
44 #include "dbustrings.hrc"
45 #include <svtools/rtftoken.h>
46 #include "dbu_misc.hrc"
47 #include <vcl/msgbox.hxx>
48 #include <connectivity/dbconversion.hxx>
49 #include <connectivity/dbtools.hxx>
50 #include <comphelper/extract.hxx>
51 #include <tools/color.hxx>
52 #include "WExtendPages.hxx"
53 #include "moduledbu.hxx"
54 #include "QEnumTypes.hxx"
55 #include "UITools.hxx"
56 #include <vcl/svapp.hxx>
57 #include <rtl/logfile.hxx>
58 
59 using namespace dbaui;
60 using namespace ::com::sun::star::uno;
61 using namespace ::com::sun::star::beans;
62 using namespace ::com::sun::star::container;
63 using namespace ::com::sun::star::sdbc;
64 using namespace ::com::sun::star::sdbcx;
65 using namespace ::com::sun::star::awt;
66 
67 DBG_NAME(ORTFReader)
68 // ==========================================================================
69 // ORTFReader
70 // ==========================================================================
71 ORTFReader::ORTFReader(	SvStream& rIn,
72 						const SharedConnection& _rxConnection,
73 						const Reference< ::com::sun::star::util::XNumberFormatter >& _rxNumberF,
74 						const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rM,
75 						const TColumnVector* pList,
76 						const OTypeInfoMap* _pInfoMap)
77 	:SvRTFParser(rIn)
78 	,ODatabaseExport( _rxConnection, _rxNumberF, _rM, pList, _pInfoMap, rIn )
79 {
80     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORTFReader::ORTFReader" );
81 	DBG_CTOR(ORTFReader,NULL);
82     m_bAppendFirstLine = false;
83 }
84 // ---------------------------------------------------------------------------
85 ORTFReader::ORTFReader(SvStream& rIn,
86 					   sal_Int32 nRows,
87 					   const TPositions &_rColumnPositions,
88 					   const Reference< ::com::sun::star::util::XNumberFormatter >& _rxNumberF,
89 					   const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rM,
90 					   const TColumnVector* pList,
91 					   const OTypeInfoMap* _pInfoMap,
92                        sal_Bool _bAutoIncrementEnabled)
93    :SvRTFParser(rIn)
94    ,ODatabaseExport( nRows, _rColumnPositions, _rxNumberF, _rM, pList, _pInfoMap, _bAutoIncrementEnabled, rIn )
95 {
96     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORTFReader::ORTFReader" );
97 	DBG_CTOR(ORTFReader,NULL);
98     m_bAppendFirstLine = false;
99 }
100 // ---------------------------------------------------------------------------
101 ORTFReader::~ORTFReader()
102 {
103 	DBG_DTOR(ORTFReader,NULL);
104 }
105 // ---------------------------------------------------------------------------
106 SvParserState ORTFReader::CallParser()
107 {
108     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORTFReader::CallParser" );
109 	DBG_CHKTHIS(ORTFReader,NULL);
110 	rInput.Seek(STREAM_SEEK_TO_BEGIN);
111 	rInput.ResetError();
112 	SvParserState  eParseState = SvRTFParser::CallParser();
113 	SetColumnTypes(m_pColumnList,m_pInfoMap);
114 	return m_bFoundTable ? eParseState : SVPAR_ERROR;
115 }
116 // ---------------------------------------------------------------------------
117 void ORTFReader::NextToken( int nToken )
118 {
119     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORTFReader::NextToken" );
120 	DBG_CHKTHIS(ORTFReader,NULL);
121 	if(m_bError || !m_nRows) // falls Fehler oder keine Rows mehr zur "Uberpr"ufung dann gleich zur"uck
122 		return;
123 
124 	if(m_xConnection.is())	  // gibt an welcher CTOR gerufen wurde und damit, ob eine Tabelle erstellt werden soll
125 	{
126 		switch(nToken)
127 		{
128 			case RTF_COLORTBL:
129 				{
130 
131 					int nTmpToken2 = GetNextToken();
132 					sal_Bool bNext = sal_True;
133 					do
134 					{
135 						Color aColor;
136 						do
137 						{
138 							switch(nTmpToken2)
139 							{
140 								case RTF_RED:	aColor.SetRed((sal_uInt8)nTokenValue); break;
141 								case RTF_BLUE:	aColor.SetBlue((sal_uInt8)nTokenValue); break;
142 								case RTF_GREEN:	aColor.SetGreen((sal_uInt8)nTokenValue); break;
143 								default:
144 									bNext = sal_False;
145 							}
146 							nTmpToken2 = GetNextToken();
147 						}
148 						while(aToken.GetChar(0) != ';' && eState != SVPAR_ERROR && eState != SVPAR_ACCEPTED);
149 						m_vecColor.push_back(aColor.GetRGBColor());
150 						nTmpToken2 = GetNextToken();
151 					}
152 					while(nTmpToken2 == RTF_RED && eState != SVPAR_ERROR && eState != SVPAR_ACCEPTED);
153 					SkipToken();
154 				}
155 				break;
156 
157 			case RTF_DEFLANG:
158 			case RTF_LANG: // Sprache abfragen
159 				m_nDefToken = (rtl_TextEncoding)nTokenValue;
160 				break;
161 			case RTF_TROWD:
162                 {
163                     bool bInsertRow = true;
164 				    if ( !m_xTable.is() ) // erste Zeile als Header verwenden
165                     {
166                         sal_uInt32 nTell = rInput.Tell(); // ver�ndert vielleicht die Position des Streams
167 
168 					    m_bError = !CreateTable(nToken);
169                         bInsertRow = m_bAppendFirstLine;
170                         if ( m_bAppendFirstLine )
171                         {
172                             rInput.Seek(nTell);
173                             rInput.ResetError();
174                         }
175                     }
176 				    if ( bInsertRow && !m_bError)
177 				    {
178 					    try
179 					    {
180 						    m_pUpdateHelper->moveToInsertRow(); // sonst neue Zeile anh"angen
181 					    }
182 					    catch(SQLException& e)
183 					    // UpdateFehlerbehandlung
184 					    {
185 						    showErrorDialog(e);
186 					    }
187 				    }
188                 }
189 				break;
190 			case RTF_INTBL:
191 				if(m_bInTbl)
192                 {
193                     eraseTokens();
194                 }
195 
196 				m_bInTbl = sal_True; // jetzt befinden wir uns in einer Tabellenbeschreibung
197 				break;
198 			case RTF_TEXTTOKEN:
199 			case RTF_SINGLECHAR:
200 				if(m_bInTbl) // wichtig, da wir sonst auch die Namen der Fonts bekommen
201 					m_sTextToken += aToken;
202 				break;
203 			case RTF_CELL:
204 				{
205 					try
206 					{
207 						insertValueIntoColumn();
208 					}
209 					catch(SQLException& e)
210 					// UpdateFehlerbehandlung
211 					{
212 						showErrorDialog(e);
213 					}
214 					m_nColumnPos++;
215                     eraseTokens();
216 				}
217 				break;
218 			case RTF_ROW:
219 				// es kann vorkommen, da� die letzte Celle nicht mit \cell abgeschlossen ist
220 				try
221 				{
222 					insertValueIntoColumn();
223 					m_nRowCount++;
224 					if(m_bIsAutoIncrement) // if bSetAutoIncrement then I have to set the autoincrement
225 						m_pUpdateHelper->updateInt(1,m_nRowCount);
226 					m_pUpdateHelper->insertRow();
227 				}
228 				catch(SQLException& e)
229 				//////////////////////////////////////////////////////////////////////
230 				// UpdateFehlerbehandlung
231 				{
232 					showErrorDialog(e);
233 				}
234 				m_nColumnPos = 0;
235 				break;
236 		}
237 	}
238 	else // Zweig nur f"ur Typpr"ufung g"ultig
239 	{
240 		switch(nToken)
241 		{
242 			case RTF_TROWD:
243 				// Der Spalten Kopf z"ahlt nicht mit
244 				if(m_bHead)
245 				{
246 					do
247 					{}
248 					while(GetNextToken() != RTF_ROW && eState != SVPAR_ERROR && eState != SVPAR_ACCEPTED);
249 					m_bHead = sal_False;
250 				}
251 				break;
252 			case RTF_INTBL:
253 				m_bInTbl = sal_True;
254 				break;
255 			case RTF_TEXTTOKEN:
256 			case RTF_SINGLECHAR:
257 				if(m_bInTbl)
258 					m_sTextToken += aToken;
259 				break;
260 			case RTF_CELL:
261                 adjustFormat();
262 				m_nColumnPos++;
263 				break;
264 			case RTF_ROW:
265                 adjustFormat();
266 				m_nColumnPos = 0;
267 				m_nRows--;
268 				break;
269 		}
270 	}
271 }
272 // ---------------------------------------------------------------------------
273 sal_Bool ORTFReader::CreateTable(int nToken)
274 {
275     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORTFReader::CreateTable" );
276 	DBG_CHKTHIS(ORTFReader,NULL);
277 	String aTableName(ModuleRes(STR_TBL_TITLE));
278 	aTableName = aTableName.GetToken(0,' ');
279 	aTableName = String(::dbtools::createUniqueName(m_xTables,::rtl::OUString(aTableName)));
280 
281 	int nTmpToken2 = nToken;
282 	String aColumnName;
283 
284 	FontDescriptor aFont = ::dbaui::CreateFontDescriptor(Application::GetSettings().GetStyleSettings().GetAppFont());
285 	do
286 	{
287 		switch(nTmpToken2)
288 		{
289 			case RTF_UNKNOWNCONTROL:
290 			case RTF_UNKNOWNDATA:
291 				m_bInTbl = sal_False;
292 				aColumnName.Erase();
293 				break;
294 			case RTF_INTBL:
295 				if(m_bInTbl)
296 					aColumnName.Erase();
297 
298 				m_bInTbl = sal_True;
299 				break;
300 			case RTF_TEXTTOKEN:
301 			case RTF_SINGLECHAR:
302 				if(m_bInTbl)
303 					aColumnName += aToken;
304 				break;
305 			case RTF_CELL:
306 				{
307 					aColumnName.EraseLeadingChars();
308 					aColumnName.EraseTrailingChars();
309 					if (!aColumnName.Len() || m_bAppendFirstLine )
310 						aColumnName = String(ModuleRes(STR_COLUMN_NAME));
311 
312 					CreateDefaultColumn(aColumnName);
313 					aColumnName.Erase();
314 				}
315 				break;
316 			case RTF_CF:
317 				//	if(nTokenValue < m_vecColor.size())
318 					//	m_xTable->setPropertyValue(PROPERTY_TEXTCOLOR,makeAny(m_vecColor[nTokenValue]));
319 				break;
320 			case RTF_B:
321 				aFont.Weight = ::com::sun::star::awt::FontWeight::BOLD;
322 				break;
323 			case RTF_I:
324 				aFont.Slant = ::com::sun::star::awt::FontSlant_ITALIC;
325 				break;
326 			case RTF_UL:
327 				aFont.Underline = ::com::sun::star::awt::FontUnderline::SINGLE;
328 				break;
329 			case RTF_STRIKE:
330 				aFont.Strikeout = ::com::sun::star::awt::FontStrikeout::SINGLE;
331 				break;
332 		}
333 	}
334 	while((nTmpToken2 = GetNextToken()) != RTF_TROWD && eState != SVPAR_ERROR && eState != SVPAR_ACCEPTED);
335 
336 	sal_Bool bOk = !m_vDestVector.empty();
337 	if(bOk)
338 	{
339 		if ( aColumnName.Len() )
340         {
341             if ( m_bAppendFirstLine )
342 				aColumnName = String(ModuleRes(STR_COLUMN_NAME));
343 			CreateDefaultColumn(aColumnName);
344         }
345 
346 		m_bInTbl		= sal_False;
347 		m_bFoundTable	= sal_True;
348 
349 		if ( isCheckEnabled() )
350 			return sal_True;
351 		Any aTextColor;
352 		if(!m_vecColor.empty())
353 			aTextColor <<= m_vecColor[0];
354 
355 		bOk = !executeWizard(aTableName,aTextColor,aFont) && m_xTable.is();
356 	}
357 	return bOk;
358 }
359 // -----------------------------------------------------------------------------
360 void ORTFReader::release()
361 {
362     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORTFReader::release" );
363 	DBG_CHKTHIS(ORTFReader,NULL);
364 	ReleaseRef();
365 }
366 
367 // -----------------------------------------------------------------------------
368 TypeSelectionPageFactory ORTFReader::getTypeSelectionPageFactory()
369 {
370     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ORTFReader::getTypeSelectionPageFactory" );
371 	DBG_CHKTHIS(ORTFReader,NULL);
372     return &OWizRTFExtend::Create;
373 }
374 // -----------------------------------------------------------------------------
375 
376 
377 
378