LibreOffice Module sw (master)  1
uinums.cxx
Go to the documentation of this file.
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 #include <memory>
21 #include <uinums.hxx>
22 
23 #include <unotools/pathoptions.hxx>
24 #include <tools/stream.hxx>
25 #include <sfx2/docfile.hxx>
26 #include <svl/itemiter.hxx>
27 
28 #include <swtypes.hxx>
29 #include <wrtsh.hxx>
30 #include <poolfmt.hxx>
31 #include <charfmt.hxx>
32 
33 using namespace ::com::sun::star;
34 
35 #define CHAPTER_FILENAME "chapter.cfg"
36 
37 /*
38  Description: Saving a rule
39  Parameter: rCopy -- the rule to save
40  nIdx -- position, where the rule is to be saved.
41  An old rule at that position will be overwritten.
42 */
43 
45 {
46  Init();
47 }
48 
50 {
52  SvtPathOptions aPathOpt;
53  aURL.SetSmartURL( aPathOpt.GetUserConfigPath() );
54  aURL.setFinalSlash();
56 
57  SfxMedium aMedium( aURL.GetMainURL(INetURLObject::DecodeMechanism::NONE), StreamMode::WRITE );
58  SvStream* pStream = aMedium.GetOutStream();
59  bool bRet = (pStream && pStream->GetError() == ERRCODE_NONE);
60  if (bRet)
61  {
63 
64  pStream->Flush();
65 
66  aMedium.Commit();
67  }
68 }
69 
71 {
72 }
73 
75 {
76  for(auto & rpNumRule : pNumRules)
77  rpNumRule.reset();
78 
79  OUString sNm(CHAPTER_FILENAME);
80  SvtPathOptions aOpt;
81  if( aOpt.SearchFile( sNm ))
82  {
83  SfxMedium aStrm( sNm, StreamMode::STD_READ );
86  }
87 }
88 
89 void SwChapterNumRules::CreateEmptyNumRule(sal_uInt16 const nIndex)
90 {
91  assert(nIndex < nMaxRules);
92  assert(!pNumRules[nIndex]);
93  pNumRules[nIndex].reset(new SwNumRulesWithName);
94 }
95 
96 void SwChapterNumRules::ApplyNumRules(const SwNumRulesWithName &rCopy, sal_uInt16 nIdx)
97 {
98  assert(nIdx < nMaxRules);
99  if( !pNumRules[nIdx] )
100  pNumRules[nIdx].reset(new SwNumRulesWithName( rCopy ));
101  else
102  *pNumRules[nIdx] = rCopy;
103  Save(); // store it immediately
104 }
105 
107  const OUString &rName )
108  : maName(rName)
109 {
110  for( sal_uInt16 n = 0; n < MAXLEVEL; ++n )
111  {
112  const SwNumFormat* pFormat = rCopy.GetNumFormat( n );
113  if( pFormat )
114  aFormats[ n ].reset(new SwNumFormatGlobal( *pFormat ));
115  else
116  aFormats[ n ].reset();
117  }
118 }
119 
121 {
122  *this = rCopy;
123 }
124 
126 {
127 }
128 
130 {
131  if( this != &rCopy )
132  {
133  maName = rCopy.maName;
134  for( int n = 0; n < MAXLEVEL; ++n )
135  {
136  SwNumFormatGlobal* pFormat = rCopy.aFormats[ n ].get();
137  if( pFormat )
138  aFormats[ n ].reset(new SwNumFormatGlobal( *pFormat ));
139  else
140  aFormats[ n ].reset();
141  }
142  }
143  return *this;
144 }
145 
146 std::unique_ptr<SwNumRule> SwNumRulesWithName::MakeNumRule(SwWrtShell& rSh) const
147 {
148  // #i89178#
149  std::unique_ptr<SwNumRule> pChg(new SwNumRule(maName, numfunc::GetDefaultPositionAndSpaceMode()));
150  pChg->SetAutoRule( false );
151  for (sal_uInt16 n = 0; n < MAXLEVEL; ++n)
152  {
153  SwNumFormatGlobal* pFormat = aFormats[ n ].get();
154  if (!pFormat)
155  continue;
156  pChg->Set(n, pFormat->MakeNumFormat(rSh));
157  }
158  return pChg;
159 }
160 
162  size_t const nIndex, SwNumFormat const*& rpNumFormat, OUString const*& rpName) const
163 {
164  rpNumFormat = (aFormats[nIndex]) ? &aFormats[nIndex]->aFormat : nullptr;
165  rpName = (aFormats[nIndex]) ? &aFormats[nIndex]->sCharFormatName : nullptr;
166 }
167 
169  size_t const nIndex, SwNumFormat const& rNumFormat, OUString const& rName)
170 {
171  aFormats[nIndex].reset( new SwNumFormatGlobal(rNumFormat) );
172  aFormats[nIndex]->sCharFormatName = rName;
173  aFormats[nIndex]->nCharPoolId = USHRT_MAX;
174  aFormats[nIndex]->m_Items.clear();
175 }
176 
178  : aFormat( rFormat ), nCharPoolId( USHRT_MAX )
179 {
180  // relative gaps?????
181 
182  SwCharFormat* pFormat = rFormat.GetCharFormat();
183  if( pFormat )
184  {
185  sCharFormatName = pFormat->GetName();
186  nCharPoolId = pFormat->GetPoolFormatId();
187  if( pFormat->GetAttrSet().Count() )
188  {
189  SfxItemIter aIter( pFormat->GetAttrSet() );
190  const SfxPoolItem *pCurr = aIter.GetCurItem();
191  do
192  {
193  m_Items.push_back(std::unique_ptr<SfxPoolItem>(pCurr->Clone()));
194  pCurr = aIter.NextItem();
195  } while (pCurr);
196  }
197 
198  aFormat.SetCharFormat( nullptr );
199  }
200 }
201 
203  :
204  aFormat( rFormat.aFormat ),
205  sCharFormatName( rFormat.sCharFormatName ),
206  nCharPoolId( rFormat.nCharPoolId )
207 {
208  for (size_t n = rFormat.m_Items.size(); n; )
209  {
210  m_Items.push_back(std::unique_ptr<SfxPoolItem>(rFormat.m_Items[ --n ]->Clone()));
211  }
212 }
213 
215 {
216 }
217 
219 {
220  SwCharFormat* pFormat = nullptr;
221  if( !sCharFormatName.isEmpty() )
222  {
223  // at first, look for the name
224  sal_uInt16 nArrLen = rSh.GetCharFormatCount();
225  for( sal_uInt16 i = 1; i < nArrLen; ++i )
226  {
227  pFormat = &rSh.GetCharFormat( i );
228  if (pFormat->GetName()==sCharFormatName)
229  // exists, so leave attributes as they are!
230  break;
231  pFormat = nullptr;
232  }
233 
234  if( !pFormat )
235  {
236  if( IsPoolUserFormat( nCharPoolId ) )
237  {
238  pFormat = rSh.MakeCharFormat( sCharFormatName );
239  pFormat->SetAuto(false);
240  }
241  else
242  pFormat = rSh.GetCharFormatFromPool( nCharPoolId );
243 
244  if( !pFormat->HasWriterListeners() ) // set attributes
245  {
246  for (size_t n = m_Items.size(); n; )
247  {
248  pFormat->SetFormatAttr( *m_Items[ --n ] );
249  }
250  }
251  }
252  }
253  const_cast<SwNumFormat&>(aFormat).SetCharFormat(pFormat);
254  SwNumFormat aNew = aFormat;
255  if (pFormat)
256  const_cast<SwNumFormat&>(aFormat).SetCharFormat(nullptr);
257  return aNew;
258 }
259 
260 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void SetNumFormat(size_t, SwNumFormat const &, OUString const &)
Definition: uinums.cxx:168
URL aURL
SwCharFormat * MakeCharFormat(const OUString &rName)
Definition: edfmt.cxx:116
void GetNumFormat(size_t, SwNumFormat const *&, OUString const *&) const
Definition: uinums.cxx:161
void CreateEmptyNumRule(sal_uInt16 nIdx)
Definition: uinums.cxx:89
const SwNumFormat * GetNumFormat(sal_uInt16 i) const
Definition: number.cxx:88
void ImportStoredChapterNumberingRules(SwChapterNumRules &rRules, SvStream &rStream, OUString const &rFileName)
SwCharFormat * GetCharFormatFromPool(sal_uInt16 nId)
Definition: editsh.hxx:356
sal_Int64 n
bool IsPoolUserFormat(sal_uInt16 nId)
Definition: poolfmt.hxx:87
void ExportStoredChapterNumberingRules(SwChapterNumRules &rRules, SvStream &rStream, OUString const &rFileName)
bool HasWriterListeners() const
Definition: calbck.hxx:208
Used by the UI to modify the document model.
Definition: wrtsh.hxx:90
bool setFinalSlash()
void ApplyNumRules(const SwNumRulesWithName &rCopy, sal_uInt16 nIdx)
Definition: uinums.cxx:96
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
const OUString & GetName() const
Definition: format.hxx:111
const sal_uInt8 MAXLEVEL
Definition: swtypes.hxx:95
SwNumFormatGlobal(const SwNumFormat &rFormat)
Definition: uinums.cxx:177
sal_uInt16 GetCharFormatCount() const
CHAR.
Definition: edfmt.cxx:30
sal_uInt16 GetPoolFormatId() const
Get and set Pool style IDs.
Definition: format.hxx:143
std::vector< std::unique_ptr< SfxPoolItem > > m_Items
Definition: uinums.hxx:47
SwCharFormat * GetCharFormat() const
Definition: numrule.hxx:73
SwNumRulesWithName & operator=(const SwNumRulesWithName &)
Definition: uinums.cxx:129
OUString maName
Definition: uinums.hxx:38
std::unique_ptr< SwNumRulesWithName > pNumRules[MAX_NUM_RULES]
Definition: uinums.hxx:85
const OUString & GetUserConfigPath() const
int i
std::unique_ptr< SwNumRule > MakeNumRule(SwWrtShell &rSh) const
Definition: uinums.cxx:146
bool Append(OUString const &rTheSegment, EncodeMechanism eMechanism=EncodeMechanism::WasEncoded, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8)
SwNumRulesWithName()=default
void SetAuto(bool bNew)
Definition: format.hxx:159
sal_uInt16 Count() const
SwNumFormat MakeNumFormat(SwWrtShell &rSh) const
Definition: uinums.cxx:218
virtual bool SetFormatAttr(const SfxPoolItem &rAttr)
Definition: format.cxx:458
OUString GetMainURL(DecodeMechanism eMechanism, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
SvStream * GetInStream()
void SetCharFormat(SwCharFormat *)
Definition: number.cxx:272
SvxNumberFormat::SvxNumPositionAndSpaceMode GetDefaultPositionAndSpaceMode()
Definition: number.cxx:1441
#define ERRCODE_NONE
const o3tl::enumarray< SvxAdjust, unsigned short > aSvxToUnoAdjust USHRT_MAX
Definition: unosett.cxx:253
OUString maName
bool SearchFile(OUString &rIniFile, Paths ePath=PATH_USERCONFIG)
const SwAttrSet & GetAttrSet() const
For querying the attribute array.
Definition: format.hxx:116
std::unique_ptr< SwNumFormatGlobal > aFormats[MAXLEVEL]
Definition: uinums.hxx:59
#define CHAPTER_FILENAME
Definition: uinums.cxx:35
bool SetSmartURL(OUString const &rTheAbsURIRef, EncodeMechanism eMechanism=EncodeMechanism::WasEncoded, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8, FSysStyle eStyle=FSysStyle::Detect)
SwCharFormat & GetCharFormat(sal_uInt16 nFormat) const
Definition: edfmt.cxx:35