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