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 constexpr OUStringLiteral CHAPTER_FILENAME = u"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 : m_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(!m_pNumRules[nIndex]);
93  m_pNumRules[nIndex].reset(new SwNumRulesWithName);
94 }
95 
96 void SwChapterNumRules::ApplyNumRules(const SwNumRulesWithName &rCopy, sal_uInt16 nIdx)
97 {
98  assert(nIdx < nMaxRules);
99  if( !m_pNumRules[nIdx] )
100  m_pNumRules[nIdx].reset(new SwNumRulesWithName( rCopy ));
101  else
102  *m_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  m_aFormats[ n ].reset(new SwNumFormatGlobal( *pFormat ));
115  else
116  m_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.m_aFormats[ n ].get();
137  if( pFormat )
138  m_aFormats[ n ].reset(new SwNumFormatGlobal( *pFormat ));
139  else
140  m_aFormats[ n ].reset();
141  }
142  }
143  return *this;
144 }
145 
147 {
148  // #i89178#
149  rNumRule.Reset(maName);
150  rNumRule.SetAutoRule( false );
151  for (sal_uInt16 n = 0; n < MAXLEVEL; ++n)
152  {
153  SwNumFormatGlobal* pFormat = m_aFormats[ n ].get();
154  if (!pFormat)
155  continue;
156  rNumRule.Set(n, pFormat->MakeNumFormat(rSh));
157  }
158 }
159 
161  size_t const nIndex, SwNumFormat const*& rpNumFormat, OUString const*& rpName) const
162 {
163  rpNumFormat = (m_aFormats[nIndex]) ? &m_aFormats[nIndex]->m_aFormat : nullptr;
164  rpName = (m_aFormats[nIndex]) ? &m_aFormats[nIndex]->m_sCharFormatName : nullptr;
165 }
166 
168  size_t const nIndex, SwNumFormat const& rNumFormat, OUString const& rName)
169 {
170  m_aFormats[nIndex].reset( new SwNumFormatGlobal(rNumFormat) );
171  m_aFormats[nIndex]->m_sCharFormatName = rName;
172  m_aFormats[nIndex]->m_nCharPoolId = USHRT_MAX;
173  m_aFormats[nIndex]->m_Items.clear();
174 }
175 
177  : m_aFormat( rFormat ), m_nCharPoolId( USHRT_MAX )
178 {
179  // relative gaps?????
180 
181  SwCharFormat* pFormat = rFormat.GetCharFormat();
182  if( !pFormat )
183  return;
184 
185  m_sCharFormatName = pFormat->GetName();
186  m_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  m_aFormat.SetCharFormat( nullptr );
199 }
200 
202  :
203  m_aFormat( rFormat.m_aFormat ),
204  m_sCharFormatName( rFormat.m_sCharFormatName ),
205  m_nCharPoolId( rFormat.m_nCharPoolId )
206 {
207  for (size_t n = rFormat.m_Items.size(); n; )
208  {
209  m_Items.push_back(std::unique_ptr<SfxPoolItem>(rFormat.m_Items[ --n ]->Clone()));
210  }
211 }
212 
214 {
215 }
216 
218 {
219  SwCharFormat* pFormat = nullptr;
220  if( !m_sCharFormatName.isEmpty() )
221  {
222  // at first, look for the name
223  sal_uInt16 nArrLen = rSh.GetCharFormatCount();
224  for( sal_uInt16 i = 1; i < nArrLen; ++i )
225  {
226  pFormat = &rSh.GetCharFormat( i );
227  if (pFormat->GetName()==m_sCharFormatName)
228  // exists, so leave attributes as they are!
229  break;
230  pFormat = nullptr;
231  }
232 
233  if( !pFormat )
234  {
235  if( IsPoolUserFormat( m_nCharPoolId ) )
236  {
237  pFormat = rSh.MakeCharFormat( m_sCharFormatName );
238  pFormat->SetAuto(false);
239  }
240  else
241  pFormat = rSh.GetCharFormatFromPool( m_nCharPoolId );
242 
243  if( !pFormat->HasWriterListeners() ) // set attributes
244  {
245  for (size_t n = m_Items.size(); n; )
246  {
247  pFormat->SetFormatAttr( *m_Items[ --n ] );
248  }
249  }
250  }
251  }
252  const_cast<SwNumFormat&>(m_aFormat).SetCharFormat(pFormat);
253  SwNumFormat aNew = m_aFormat;
254  if (pFormat)
255  const_cast<SwNumFormat&>(m_aFormat).SetCharFormat(nullptr);
256  return aNew;
257 }
258 
259 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void SetNumFormat(size_t, SwNumFormat const &, OUString const &)
Definition: uinums.cxx:167
URL aURL
bool SearchFile(OUString &rIniFile, Paths ePath=Paths::UserConfig)
SwCharFormat * MakeCharFormat(const OUString &rName)
Definition: edfmt.cxx:115
constexpr OUStringLiteral CHAPTER_FILENAME
Definition: uinums.cxx:35
void SetAutoRule(bool bFlag)
Definition: numrule.hxx:231
void GetNumFormat(size_t, SwNumFormat const *&, OUString const *&) const
Definition: uinums.cxx:160
void CreateEmptyNumRule(sal_uInt16 nIdx)
Definition: uinums.cxx:89
const SwNumFormat * GetNumFormat(sal_uInt16 i) const
Definition: number.cxx:96
void ImportStoredChapterNumberingRules(SwChapterNumRules &rRules, SvStream &rStream, OUString const &rFileName)
SwCharFormat * GetCharFormatFromPool(sal_uInt16 nId)
Definition: editsh.hxx:353
sal_Int64 n
constexpr sal_uInt8 MAXLEVEL
Definition: swtypes.hxx:93
bool IsPoolUserFormat(sal_uInt16 nId)
Definition: poolfmt.hxx:86
void ExportStoredChapterNumberingRules(SwChapterNumRules &rRules, SvStream &rStream, OUString const &rFileName)
bool HasWriterListeners() const
Definition: calbck.hxx:203
Used by the UI to modify the document model.
Definition: wrtsh.hxx:93
bool setFinalSlash()
void ApplyNumRules(const SwNumRulesWithName &rCopy, sal_uInt16 nIdx)
Definition: uinums.cxx:96
const OUString & GetName() const
Definition: format.hxx:115
SwNumFormatGlobal(const SwNumFormat &rFormat)
Definition: uinums.cxx:176
sal_uInt16 GetCharFormatCount() const
CHAR.
Definition: edfmt.cxx:30
sal_uInt16 GetPoolFormatId() const
Get and set Pool style IDs.
Definition: format.hxx:147
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:129
OUString maName
Definition: uinums.hxx:38
const OUString & GetUserConfigPath() const
void Reset(const OUString &rName)
Definition: number.cxx:565
int i
SwNumRulesWithName()=default
void SetAuto(bool bNew)
Definition: format.hxx:163
sal_uInt16 Count() const
float u
Represents the style of a text portion.
Definition: charfmt.hxx:26
SwNumFormat MakeNumFormat(SwWrtShell &rSh) const
Definition: uinums.cxx:217
virtual bool SetFormatAttr(const SfxPoolItem &rAttr)
Definition: format.cxx:450
OUString GetMainURL(DecodeMechanism eMechanism, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
SvStream * GetInStream()
void Set(sal_uInt16 i, const SwNumFormat *)
Definition: number.cxx:617
void SetCharFormat(SwCharFormat *)
Definition: number.cxx:280
#define ERRCODE_NONE
OUString maName
void ResetNumRule(SwWrtShell &rSh, SwNumRule &) const
Definition: uinums.cxx:146
bool Append(std::u16string_view rTheSegment, EncodeMechanism eMechanism=EncodeMechanism::WasEncoded, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8)
const SwAttrSet & GetAttrSet() const
For querying the attribute array.
Definition: format.hxx:120
std::unique_ptr< SwNumRulesWithName > m_pNumRules[MAX_NUM_RULES]
Definition: uinums.hxx:85
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
std::unique_ptr< SwNumFormatGlobal > m_aFormats[MAXLEVEL]
Definition: uinums.hxx:59