Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sw / source / uibase / config / uinums.cxx
blob2659a1bb3b0528f788b21afa0aa40269ae46b8d1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <memory>
21 #include <uinums.hxx>
23 #include <unotools/pathoptions.hxx>
24 #include <tools/stream.hxx>
25 #include <sfx2/docfile.hxx>
26 #include <svl/itemiter.hxx>
28 #include <swtypes.hxx>
29 #include <utility>
30 #include <wrtsh.hxx>
31 #include <poolfmt.hxx>
32 #include <charfmt.hxx>
34 using namespace ::com::sun::star;
36 constexpr OUStringLiteral CHAPTER_FILENAME = u"chapter.cfg";
39 Description: Saving a rule
40 Parameter: rCopy -- the rule to save
41 nIdx -- position, where the rule is to be saved.
42 An old rule at that position will be overwritten.
45 SwChapterNumRules::SwChapterNumRules()
47 Init();
50 void SwChapterNumRules::Save()
52 INetURLObject aURL;
53 SvtPathOptions aPathOpt;
54 aURL.SetSmartURL( aPathOpt.GetUserConfigPath() );
55 aURL.setFinalSlash();
56 aURL.Append(CHAPTER_FILENAME);
58 SfxMedium aMedium( aURL.GetMainURL(INetURLObject::DecodeMechanism::NONE), StreamMode::WRITE );
59 SvStream* pStream = aMedium.GetOutStream();
60 bool bRet = (pStream && pStream->GetError() == ERRCODE_NONE);
61 if (bRet)
63 sw::ExportStoredChapterNumberingRules(*this, *pStream,CHAPTER_FILENAME);
65 pStream->FlushBuffer();
67 aMedium.Commit();
71 SwChapterNumRules::~SwChapterNumRules()
75 void SwChapterNumRules::Init()
77 for(auto & rpNumRule : m_pNumRules)
78 rpNumRule.reset();
80 OUString sNm(CHAPTER_FILENAME);
81 SvtPathOptions aOpt;
82 if( aOpt.SearchFile( sNm ))
84 SfxMedium aStrm( sNm, StreamMode::STD_READ );
85 sw::ImportStoredChapterNumberingRules(*this, *aStrm.GetInStream(),
86 CHAPTER_FILENAME);
90 void SwChapterNumRules::CreateEmptyNumRule(sal_uInt16 const nIndex)
92 assert(nIndex < nMaxRules);
93 assert(!m_pNumRules[nIndex]);
94 m_pNumRules[nIndex].reset(new SwNumRulesWithName);
97 void SwChapterNumRules::ApplyNumRules(const SwNumRulesWithName &rCopy, sal_uInt16 nIdx)
99 assert(nIdx < nMaxRules);
100 if( !m_pNumRules[nIdx] )
101 m_pNumRules[nIdx].reset(new SwNumRulesWithName( rCopy ));
102 else
103 *m_pNumRules[nIdx] = rCopy;
104 Save(); // store it immediately
107 SwNumRulesWithName::SwNumRulesWithName( const SwNumRule &rCopy,
108 OUString aName )
109 : maName(std::move(aName))
111 for( sal_uInt16 n = 0; n < MAXLEVEL; ++n )
113 const SwNumFormat* pFormat = rCopy.GetNumFormat( n );
114 if( pFormat )
115 m_aFormats[ n ].reset(new SwNumFormatGlobal( *pFormat ));
116 else
117 m_aFormats[ n ].reset();
121 SwNumRulesWithName::SwNumRulesWithName( const SwNumRulesWithName& rCopy )
123 *this = rCopy;
126 SwNumRulesWithName::~SwNumRulesWithName()
130 SwNumRulesWithName& SwNumRulesWithName::operator=(const SwNumRulesWithName &rCopy)
132 if( this != &rCopy )
134 maName = rCopy.maName;
135 for( int n = 0; n < MAXLEVEL; ++n )
137 SwNumFormatGlobal* pFormat = rCopy.m_aFormats[ n ].get();
138 if( pFormat )
139 m_aFormats[ n ].reset(new SwNumFormatGlobal( *pFormat ));
140 else
141 m_aFormats[ n ].reset();
144 return *this;
147 void SwNumRulesWithName::ResetNumRule(SwWrtShell& rSh, SwNumRule& rNumRule) const
149 // #i89178#
150 rNumRule.Reset(maName);
151 rNumRule.SetAutoRule( false );
152 for (sal_uInt16 n = 0; n < MAXLEVEL; ++n)
154 SwNumFormatGlobal* pFormat = m_aFormats[ n ].get();
155 if (!pFormat)
156 continue;
157 rNumRule.Set(n, pFormat->MakeNumFormat(rSh));
161 void SwNumRulesWithName::GetNumFormat(
162 size_t const nIndex, SwNumFormat const*& rpNumFormat, OUString const*& rpName) const
164 rpNumFormat = (m_aFormats[nIndex]) ? &m_aFormats[nIndex]->m_aFormat : nullptr;
165 rpName = (m_aFormats[nIndex]) ? &m_aFormats[nIndex]->m_sCharFormatName : nullptr;
168 void SwNumRulesWithName::SetNumFormat(
169 size_t const nIndex, SwNumFormat const& rNumFormat, OUString const& rName)
171 m_aFormats[nIndex].reset( new SwNumFormatGlobal(rNumFormat) );
172 m_aFormats[nIndex]->m_sCharFormatName = rName;
173 m_aFormats[nIndex]->m_nCharPoolId = USHRT_MAX;
174 m_aFormats[nIndex]->m_Items.clear();
177 SwNumRulesWithName::SwNumFormatGlobal::SwNumFormatGlobal( const SwNumFormat& rFormat )
178 : m_aFormat( rFormat ), m_nCharPoolId( USHRT_MAX )
180 // relative gaps?????
182 SwCharFormat* pFormat = rFormat.GetCharFormat();
183 if( !pFormat )
184 return;
186 m_sCharFormatName = pFormat->GetName();
187 m_nCharPoolId = pFormat->GetPoolFormatId();
188 if( pFormat->GetAttrSet().Count() )
190 SfxItemIter aIter( pFormat->GetAttrSet() );
191 const SfxPoolItem *pCurr = aIter.GetCurItem();
194 m_Items.push_back(std::unique_ptr<SfxPoolItem>(pCurr->Clone()));
195 pCurr = aIter.NextItem();
196 } while (pCurr);
199 m_aFormat.SetCharFormat( nullptr );
202 SwNumRulesWithName::SwNumFormatGlobal::SwNumFormatGlobal( const SwNumFormatGlobal& rFormat )
204 m_aFormat( rFormat.m_aFormat ),
205 m_sCharFormatName( rFormat.m_sCharFormatName ),
206 m_nCharPoolId( rFormat.m_nCharPoolId )
208 for (size_t n = rFormat.m_Items.size(); n; )
210 m_Items.push_back(std::unique_ptr<SfxPoolItem>(rFormat.m_Items[ --n ]->Clone()));
214 SwNumRulesWithName::SwNumFormatGlobal::~SwNumFormatGlobal()
218 SwNumFormat SwNumRulesWithName::SwNumFormatGlobal::MakeNumFormat(SwWrtShell& rSh) const
220 SwCharFormat* pFormat = nullptr;
221 if( !m_sCharFormatName.isEmpty() )
223 // at first, look for the name
224 sal_uInt16 nArrLen = rSh.GetCharFormatCount();
225 for( sal_uInt16 i = 1; i < nArrLen; ++i )
227 pFormat = &rSh.GetCharFormat( i );
228 if (pFormat->GetName()==m_sCharFormatName)
229 // exists, so leave attributes as they are!
230 break;
231 pFormat = nullptr;
234 if( !pFormat )
236 if( IsPoolUserFormat( m_nCharPoolId ) )
238 pFormat = rSh.MakeCharFormat( m_sCharFormatName );
239 pFormat->SetAuto(false);
241 else
242 pFormat = rSh.GetCharFormatFromPool( m_nCharPoolId );
244 if( !pFormat->HasWriterListeners() ) // set attributes
246 for (size_t n = m_Items.size(); n; )
248 pFormat->SetFormatAttr( *m_Items[ --n ] );
253 const_cast<SwNumFormat&>(m_aFormat).SetCharFormat(pFormat);
254 SwNumFormat aNew = m_aFormat;
255 if (pFormat)
256 const_cast<SwNumFormat&>(m_aFormat).SetCharFormat(nullptr);
257 return aNew;
260 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */