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  *this = rCopy;
127 }
128 
130 {
131 }
132 
134 {
135  if( this != &rCopy )
136  {
137  maName = rCopy.maName;
138  for( int n = 0; n < MAXLEVEL; ++n )
139  {
140  SwNumFormatGlobal* pFormat = rCopy.aFormats[ n ].get();
141  if( pFormat )
142  aFormats[ n ].reset(new SwNumFormatGlobal( *pFormat ));
143  else
144  aFormats[ n ].reset();
145  }
146  }
147  return *this;
148 }
149 
150 std::unique_ptr<SwNumRule> SwNumRulesWithName::MakeNumRule(SwWrtShell& rSh) const
151 {
152  // #i89178#
153  std::unique_ptr<SwNumRule> pChg(new SwNumRule(maName, numfunc::GetDefaultPositionAndSpaceMode()));
154  pChg->SetAutoRule( false );
155  for (sal_uInt16 n = 0; n < MAXLEVEL; ++n)
156  {
157  SwNumFormatGlobal* pFormat = aFormats[ n ].get();
158  if (!pFormat)
159  continue;
160  pChg->Set(n, pFormat->MakeNumFormat(rSh));
161  }
162  return pChg;
163 }
164 
166  size_t const nIndex, SwNumFormat const*& rpNumFormat, OUString const*& rpName) const
167 {
168  rpNumFormat = (aFormats[nIndex]) ? &aFormats[nIndex]->aFormat : nullptr;
169  rpName = (aFormats[nIndex]) ? &aFormats[nIndex]->sCharFormatName : nullptr;
170 }
171 
173  size_t const nIndex, SwNumFormat const& rNumFormat, OUString const& rName)
174 {
175  aFormats[nIndex].reset( new SwNumFormatGlobal(rNumFormat) );
176  aFormats[nIndex]->sCharFormatName = rName;
177  aFormats[nIndex]->nCharPoolId = USHRT_MAX;
178  aFormats[nIndex]->m_Items.clear();
179 }
180 
182  : aFormat( rFormat ), nCharPoolId( USHRT_MAX )
183 {
184  // relative gaps?????
185 
186  SwCharFormat* pFormat = rFormat.GetCharFormat();
187  if( pFormat )
188  {
189  sCharFormatName = pFormat->GetName();
190  nCharPoolId = pFormat->GetPoolFormatId();
191  if( pFormat->GetAttrSet().Count() )
192  {
193  SfxItemIter aIter( pFormat->GetAttrSet() );
194  const SfxPoolItem *pCurr = aIter.GetCurItem();
195  do
196  {
197  m_Items.push_back(std::unique_ptr<SfxPoolItem>(pCurr->Clone()));
198  pCurr = aIter.NextItem();
199  } while (pCurr);
200  }
201 
202  aFormat.SetCharFormat( nullptr );
203  }
204 }
205 
207  :
208  aFormat( rFormat.aFormat ),
209  sCharFormatName( rFormat.sCharFormatName ),
210  nCharPoolId( rFormat.nCharPoolId )
211 {
212  for (size_t n = rFormat.m_Items.size(); n; )
213  {
214  m_Items.push_back(std::unique_ptr<SfxPoolItem>(rFormat.m_Items[ --n ]->Clone()));
215  }
216 }
217 
219 {
220 }
221 
223 {
224  SwCharFormat* pFormat = nullptr;
225  if( !sCharFormatName.isEmpty() )
226  {
227  // at first, look for the name
228  sal_uInt16 nArrLen = rSh.GetCharFormatCount();
229  for( sal_uInt16 i = 1; i < nArrLen; ++i )
230  {
231  pFormat = &rSh.GetCharFormat( i );
232  if (pFormat->GetName()==sCharFormatName)
233  // exists, so leave attributes as they are!
234  break;
235  pFormat = nullptr;
236  }
237 
238  if( !pFormat )
239  {
240  if( IsPoolUserFormat( nCharPoolId ) )
241  {
242  pFormat = rSh.MakeCharFormat( sCharFormatName );
243  pFormat->SetAuto(false);
244  }
245  else
246  pFormat = rSh.GetCharFormatFromPool( nCharPoolId );
247 
248  if( !pFormat->HasWriterListeners() ) // set attributes
249  {
250  for (size_t n = m_Items.size(); n; )
251  {
252  pFormat->SetFormatAttr( *m_Items[ --n ] );
253  }
254  }
255  }
256  }
257  const_cast<SwNumFormat&>(aFormat).SetCharFormat(pFormat);
258  SwNumFormat aNew = aFormat;
259  if (pFormat)
260  const_cast<SwNumFormat&>(aFormat).SetCharFormat(nullptr);
261  return aNew;
262 }
263 
264 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void SetNumFormat(size_t, SwNumFormat const &, OUString const &)
Definition: uinums.cxx:172
SwCharFormat * MakeCharFormat(const OUString &rName)
Definition: edfmt.cxx:118
void GetNumFormat(size_t, SwNumFormat const *&, OUString const *&) const
Definition: uinums.cxx:165
void CreateEmptyNumRule(sal_uInt16 nIdx)
Definition: uinums.cxx:93
const SwNumFormat * GetNumFormat(sal_uInt16 i) const
Definition: number.cxx:87
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: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:100
const OUString & GetName() const
Definition: format.hxx:111
const sal_uInt8 MAXLEVEL
Definition: swtypes.hxx:95
SwNumFormatGlobal(const SwNumFormat &rFormat)
Definition: uinums.cxx:181
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:73
SwNumRulesWithName & operator=(const SwNumRulesWithName &)
Definition: uinums.cxx:133
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:150
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
int i
sal_uInt16 Count() const
SwNumFormat MakeNumFormat(SwWrtShell &rSh) const
Definition: uinums.cxx:222
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:271
SvxNumberFormat::SvxNumPositionAndSpaceMode GetDefaultPositionAndSpaceMode()
Definition: number.cxx:1379
#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