Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include <tools/urlobj.hxx>
22 : #include <svl/urihelper.hxx>
23 : #include <rtl/tencinfo.h>
24 : #include <swerror.h>
25 : #include <ndtxt.hxx>
26 : #include <pam.hxx>
27 : #include <shellio.hxx>
28 : #include <docsh.hxx>
29 : #include <fmtanchr.hxx>
30 : #include <frmfmt.hxx>
31 : #include <doc.hxx>
32 : #include <docary.hxx>
33 : #include "ww8glsy.hxx"
34 : #include "ww8par.hxx"
35 :
36 :
37 0 : WW8Glossary::WW8Glossary(SvStorageStreamRef &refStrm, sal_uInt8 nVersion,
38 : SvStorage *pStg)
39 0 : : pGlossary(0), rStrm(refStrm), xStg(pStg), nStrings(0)
40 : {
41 0 : refStrm->SetNumberFormatInt(NUMBERFORMAT_INT_LITTLEENDIAN);
42 0 : WW8Fib aWwFib(*refStrm, nVersion);
43 :
44 0 : if (aWwFib.nFibBack >= 0x6A) //Word97
45 : {
46 : xTableStream = pStg->OpenSotStream(rtl::OUString::createFromAscii(
47 0 : aWwFib.fWhichTblStm ? SL::a1Table : SL::a0Table), STREAM_STD_READ);
48 :
49 0 : if (xTableStream.Is() && SVSTREAM_OK == xTableStream->GetError())
50 : {
51 0 : xTableStream->SetNumberFormatInt(NUMBERFORMAT_INT_LITTLEENDIAN);
52 : pGlossary =
53 0 : new WW8GlossaryFib(*refStrm, nVersion, *xTableStream, aWwFib);
54 : }
55 : }
56 0 : }
57 :
58 0 : bool WW8Glossary::HasBareGraphicEnd(SwDoc *pDoc,SwNodeIndex &rIdx)
59 : {
60 0 : bool bRet=false;
61 0 : for( sal_uInt16 nCnt = pDoc->GetSpzFrmFmts()->size(); nCnt; )
62 : {
63 0 : SwFrmFmt* pFrmFmt = (*pDoc->GetSpzFrmFmts())[ --nCnt ];
64 0 : if ( RES_FLYFRMFMT != pFrmFmt->Which() &&
65 0 : RES_DRAWFRMFMT != pFrmFmt->Which() )
66 0 : continue;
67 0 : const SwFmtAnchor& rAnchor = pFrmFmt->GetAnchor();
68 0 : SwPosition const*const pAPos = rAnchor.GetCntntAnchor();
69 0 : if (pAPos &&
70 0 : ((FLY_AT_PARA == rAnchor.GetAnchorId()) ||
71 0 : (FLY_AT_CHAR == rAnchor.GetAnchorId())) &&
72 0 : rIdx == pAPos->nNode.GetIndex() )
73 : {
74 0 : bRet=true;
75 0 : break;
76 : }
77 : }
78 0 : return bRet;
79 : }
80 :
81 0 : bool WW8Glossary::MakeEntries(SwDoc *pD, SwTextBlocks &rBlocks,
82 : bool bSaveRelFile, const std::vector<String>& rStrings,
83 : const std::vector<ww::bytes>& rExtra)
84 : {
85 : // this code will be called after reading all text into the
86 : // empty sections
87 0 : const String aOldURL( rBlocks.GetBaseURL() );
88 0 : bool bRet=false;
89 0 : if( bSaveRelFile )
90 : {
91 : rBlocks.SetBaseURL(
92 : URIHelper::SmartRel2Abs(
93 0 : INetURLObject(), rBlocks.GetFileName(),
94 0 : URIHelper::GetMaybeFileHdl()));
95 : }
96 : else
97 0 : rBlocks.SetBaseURL( aEmptyStr );
98 :
99 0 : SwNodeIndex aDocEnd( pD->GetNodes().GetEndOfContent() );
100 0 : SwNodeIndex aStart( *aDocEnd.GetNode().StartOfSectionNode(), 1 );
101 :
102 : // search the first NormalStartNode
103 0 : while( !( aStart.GetNode().IsStartNode() && SwNormalStartNode ==
104 0 : aStart.GetNode().GetStartNode()->GetStartNodeType()) &&
105 0 : aStart < aDocEnd )
106 0 : ++aStart;
107 :
108 0 : if( aStart < aDocEnd )
109 : {
110 : SwTxtFmtColl* pColl = pD->GetTxtCollFromPool
111 0 : (RES_POOLCOLL_STANDARD, false);
112 0 : sal_uInt16 nGlosEntry = 0;
113 0 : SwCntntNode* pCNd = 0;
114 0 : do {
115 0 : SwPaM aPam( aStart );
116 : {
117 0 : SwNodeIndex& rIdx = aPam.GetPoint()->nNode;
118 0 : ++rIdx;
119 0 : if( 0 == ( pCNd = rIdx.GetNode().GetTxtNode() ) )
120 : {
121 0 : pCNd = pD->GetNodes().MakeTxtNode( rIdx, pColl );
122 0 : rIdx = *pCNd;
123 : }
124 : }
125 0 : aPam.GetPoint()->nContent.Assign( pCNd, 0 );
126 0 : aPam.SetMark();
127 : {
128 0 : SwNodeIndex& rIdx = aPam.GetPoint()->nNode;
129 0 : rIdx = aStart.GetNode().EndOfSectionIndex() - 1;
130 0 : if(( 0 == ( pCNd = rIdx.GetNode().GetCntntNode() ) )
131 0 : || HasBareGraphicEnd(pD,rIdx))
132 : {
133 0 : ++rIdx;
134 0 : pCNd = pD->GetNodes().MakeTxtNode( rIdx, pColl );
135 0 : rIdx = *pCNd;
136 : }
137 : }
138 0 : aPam.GetPoint()->nContent.Assign( pCNd, pCNd->Len() );
139 :
140 : // now we have the right selection for one entry. Copy this to
141 : // the definied TextBlock, but only if it is not an autocorrection
142 : // entry (== -1) otherwise the group indicates the group in the
143 : // sttbfglsystyle list that this entry belongs to. Unused at the
144 : // moment
145 0 : const ww::bytes &rData = rExtra[nGlosEntry];
146 0 : sal_uInt16 n = SVBT16ToShort( &(rData[2]) );
147 0 : if(n != 0xFFFF)
148 : {
149 0 : rBlocks.ClearDoc();
150 0 : const String &rLNm = rStrings[nGlosEntry];
151 :
152 0 : String sShortcut = rLNm;
153 :
154 : // Need to check make sure the shortcut is not already being used
155 0 : sal_Int32 nStart = 0;
156 0 : sal_uInt16 nCurPos = rBlocks.GetIndex( sShortcut );
157 0 : xub_StrLen nLen = sShortcut.Len();
158 0 : while( (sal_uInt16)-1 != nCurPos )
159 : {
160 0 : sShortcut.Erase( nLen ) +=
161 0 : rtl::OUString::valueOf(++nStart); // add an Number to it
162 0 : nCurPos = rBlocks.GetIndex( sShortcut );
163 : }
164 :
165 0 : if( rBlocks.BeginPutDoc( sShortcut, sShortcut )) // Make the shortcut and the name the same
166 :
167 : {
168 0 : SwDoc* pGlDoc = rBlocks.GetDoc();
169 0 : SwNodeIndex aIdx( pGlDoc->GetNodes().GetEndOfContent(),
170 0 : -1 );
171 0 : pCNd = aIdx.GetNode().GetCntntNode();
172 0 : SwPosition aPos( aIdx, SwIndex( pCNd, pCNd->Len() ));
173 0 : pD->CopyRange( aPam, aPos, false );
174 0 : rBlocks.PutDoc();
175 0 : }
176 : }
177 0 : aStart = aStart.GetNode().EndOfSectionIndex() + 1;
178 0 : ++nGlosEntry;
179 0 : } while( aStart.GetNode().IsStartNode() &&
180 0 : SwNormalStartNode == aStart.GetNode().
181 0 : GetStartNode()->GetStartNodeType());
182 0 : bRet=true;
183 : }
184 :
185 : // this code will be called after reading all text into the empty sections
186 :
187 0 : rBlocks.SetBaseURL( aOldURL );
188 0 : return bRet;
189 : }
190 :
191 :
192 0 : bool WW8Glossary::Load( SwTextBlocks &rBlocks, bool bSaveRelFile )
193 : {
194 0 : bool bRet=false;
195 0 : if (pGlossary && pGlossary->IsGlossaryFib() && rBlocks.StartPutMuchBlockEntries())
196 : {
197 : //read the names of the autotext entries
198 0 : std::vector<String> aStrings;
199 0 : std::vector<ww::bytes> aData;
200 :
201 : rtl_TextEncoding eStructCharSet =
202 0 : WW8Fib::GetFIBCharset(pGlossary->chseTables);
203 :
204 0 : WW8ReadSTTBF(true, *xTableStream, pGlossary->fcSttbfglsy,
205 0 : pGlossary->lcbSttbfglsy, 0, eStructCharSet, aStrings, &aData );
206 :
207 0 : rStrm->Seek(0);
208 :
209 0 : if ( 0 != (nStrings = static_cast< sal_uInt16 >(aStrings.size())))
210 : {
211 0 : SfxObjectShellLock xDocSh(new SwDocShell(SFX_CREATE_MODE_INTERNAL));
212 0 : if (xDocSh->DoInitNew(0))
213 : {
214 0 : SwDoc *pD = ((SwDocShell*)(&xDocSh))->GetDoc();
215 : SwWW8ImplReader* pRdr = new SwWW8ImplReader(pGlossary->nVersion,
216 0 : xStg, &rStrm, *pD, rBlocks.GetBaseURL(), true);
217 :
218 : SwNodeIndex aIdx(
219 0 : *pD->GetNodes().GetEndOfContent().StartOfSectionNode(), 1);
220 0 : if( !aIdx.GetNode().IsTxtNode() )
221 : {
222 : OSL_ENSURE( !this, "wo ist der TextNode?" );
223 0 : pD->GetNodes().GoNext( &aIdx );
224 : }
225 0 : SwPaM aPamo( aIdx );
226 0 : aPamo.GetPoint()->nContent.Assign(aIdx.GetNode().GetCntntNode(),
227 0 : 0);
228 0 : pRdr->LoadDoc(aPamo,this);
229 :
230 0 : bRet = MakeEntries(pD, rBlocks, bSaveRelFile, aStrings, aData);
231 :
232 0 : delete pRdr;
233 : }
234 0 : xDocSh->DoClose();
235 0 : rBlocks.EndPutMuchBlockEntries();
236 0 : }
237 : }
238 0 : return bRet;
239 : }
240 :
241 :
242 0 : bool WW8GlossaryFib::IsGlossaryFib()
243 : {
244 : // fGlsy will indicate whether this has AutoText or not
245 0 : return fGlsy;
246 : }
247 :
248 0 : sal_uInt32 WW8GlossaryFib::FindGlossaryFibOffset(SvStream & /* rTableStrm */,
249 : SvStream & /* rStrm */,
250 : const WW8Fib &rFib)
251 : {
252 0 : sal_uInt32 nGlossaryFibOffset = 0;
253 0 : if ( rFib.fDot ) // its a template
254 : {
255 0 : if ( rFib.pnNext )
256 0 : nGlossaryFibOffset = ( rFib.pnNext * 512 );
257 : }
258 0 : return nGlossaryFibOffset;
259 18 : }
260 :
261 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|