1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
23 #include <unotools/pathoptions.hxx>
24 #include <tools/stream.hxx>
25 #include <sfx2/docfile.hxx>
26 #include <svl/itemiter.hxx>
28 #include <swtypes.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()
50 void SwChapterNumRules::Save()
53 SvtPathOptions aPathOpt
;
54 aURL
.SetSmartURL( aPathOpt
.GetUserConfigPath() );
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
);
63 sw::ExportStoredChapterNumberingRules(*this, *pStream
,CHAPTER_FILENAME
);
65 pStream
->FlushBuffer();
71 SwChapterNumRules::~SwChapterNumRules()
75 void SwChapterNumRules::Init()
77 for(auto & rpNumRule
: m_pNumRules
)
80 OUString
sNm(CHAPTER_FILENAME
);
82 if( aOpt
.SearchFile( sNm
))
84 SfxMedium
aStrm( sNm
, StreamMode::STD_READ
);
85 sw::ImportStoredChapterNumberingRules(*this, *aStrm
.GetInStream(),
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
));
103 *m_pNumRules
[nIdx
] = rCopy
;
104 Save(); // store it immediately
107 SwNumRulesWithName::SwNumRulesWithName( const SwNumRule
&rCopy
,
109 : maName(std::move(aName
))
111 for( sal_uInt16 n
= 0; n
< MAXLEVEL
; ++n
)
113 const SwNumFormat
* pFormat
= rCopy
.GetNumFormat( n
);
115 m_aFormats
[ n
].reset(new SwNumFormatGlobal( *pFormat
));
117 m_aFormats
[ n
].reset();
121 SwNumRulesWithName::SwNumRulesWithName( const SwNumRulesWithName
& rCopy
)
126 SwNumRulesWithName::~SwNumRulesWithName()
130 SwNumRulesWithName
& SwNumRulesWithName::operator=(const SwNumRulesWithName
&rCopy
)
134 maName
= rCopy
.maName
;
135 for( int n
= 0; n
< MAXLEVEL
; ++n
)
137 SwNumFormatGlobal
* pFormat
= rCopy
.m_aFormats
[ n
].get();
139 m_aFormats
[ n
].reset(new SwNumFormatGlobal( *pFormat
));
141 m_aFormats
[ n
].reset();
147 void SwNumRulesWithName::ResetNumRule(SwWrtShell
& rSh
, SwNumRule
& rNumRule
) const
150 rNumRule
.Reset(maName
);
151 rNumRule
.SetAutoRule( false );
152 for (sal_uInt16 n
= 0; n
< MAXLEVEL
; ++n
)
154 SwNumFormatGlobal
* pFormat
= m_aFormats
[ n
].get();
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();
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();
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!
236 if( IsPoolUserFormat( m_nCharPoolId
) )
238 pFormat
= rSh
.MakeCharFormat( m_sCharFormatName
);
239 pFormat
->SetAuto(false);
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
;
256 const_cast<SwNumFormat
&>(m_aFormat
).SetCharFormat(nullptr);
260 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */