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 <sal/config.h>
21 
22 #include <string_view>
23 
24 #include <UndoBookmark.hxx>
25 
26 #include <strings.hrc>
27 #include <doc.hxx>
28 #include <swundo.hxx>
29 #include <pam.hxx>
30 
31 #include <UndoCore.hxx>
32 #include <IMark.hxx>
33 #include <rolbck.hxx>
34 
35 #include <SwRewriter.hxx>
36 
SwUndoBookmark(SwUndoId nUndoId,const::sw::mark::IMark & rBkmk)37 SwUndoBookmark::SwUndoBookmark( SwUndoId nUndoId,
38             const ::sw::mark::IMark& rBkmk )
39     : SwUndo( nUndoId, &rBkmk.GetMarkPos().GetDoc() )
40     , m_pHistoryBookmark(new SwHistoryBookmark(rBkmk, true, rBkmk.IsExpanded()))
41 {
42 }
43 
~SwUndoBookmark()44 SwUndoBookmark::~SwUndoBookmark()
45 {
46 }
47 
SetInDoc(SwDoc * pDoc)48 void SwUndoBookmark::SetInDoc( SwDoc* pDoc )
49 {
50     m_pHistoryBookmark->SetInDoc( pDoc, false );
51 }
52 
ResetInDoc(SwDoc & rDoc)53 void SwUndoBookmark::ResetInDoc( SwDoc& rDoc )
54 {
55     IDocumentMarkAccess* const pMarkAccess = rDoc.getIDocumentMarkAccess();
56     for ( IDocumentMarkAccess::const_iterator_t ppBkmk = pMarkAccess->getAllMarksBegin();
57           ppBkmk != pMarkAccess->getAllMarksEnd();
58           ++ppBkmk )
59     {
60         if ( m_pHistoryBookmark->IsEqualBookmark( **ppBkmk ) )
61         {
62             pMarkAccess->deleteMark( ppBkmk );
63             break;
64         }
65     }
66 }
67 
GetRewriter() const68 SwRewriter SwUndoBookmark::GetRewriter() const
69 {
70     SwRewriter aResult;
71 
72     aResult.AddRule(UndoArg1, m_pHistoryBookmark->GetName());
73 
74     return aResult;
75 }
76 
SwUndoInsBookmark(const::sw::mark::IMark & rBkmk)77 SwUndoInsBookmark::SwUndoInsBookmark( const ::sw::mark::IMark& rBkmk )
78     : SwUndoBookmark( SwUndoId::INSBOOKMARK, rBkmk )
79 {
80 }
81 
UndoImpl(::sw::UndoRedoContext & rContext)82 void SwUndoInsBookmark::UndoImpl(::sw::UndoRedoContext & rContext)
83 {
84     ResetInDoc( rContext.GetDoc() );
85 }
86 
RedoImpl(::sw::UndoRedoContext & rContext)87 void SwUndoInsBookmark::RedoImpl(::sw::UndoRedoContext & rContext)
88 {
89     SetInDoc( &rContext.GetDoc() );
90 }
91 
SwUndoDeleteBookmark(const::sw::mark::IMark & rBkmk)92 SwUndoDeleteBookmark::SwUndoDeleteBookmark( const ::sw::mark::IMark& rBkmk )
93     : SwUndoBookmark( SwUndoId::DELBOOKMARK, rBkmk )
94 {
95 }
96 
UndoImpl(::sw::UndoRedoContext & rContext)97 void SwUndoDeleteBookmark::UndoImpl(::sw::UndoRedoContext & rContext)
98 {
99     SetInDoc( &rContext.GetDoc() );
100 }
101 
RedoImpl(::sw::UndoRedoContext & rContext)102 void SwUndoDeleteBookmark::RedoImpl(::sw::UndoRedoContext & rContext)
103 {
104     ResetInDoc( rContext.GetDoc() );
105 }
106 
SwUndoRenameBookmark(const OUString & rOldName,const OUString & rNewName,const SwDoc & rDoc)107 SwUndoRenameBookmark::SwUndoRenameBookmark( const OUString& rOldName, const OUString& rNewName, const SwDoc& rDoc )
108     : SwUndo( SwUndoId::BOOKMARK_RENAME, &rDoc )
109     , m_sOldName( rOldName )
110     , m_sNewName( rNewName )
111 {
112 }
113 
~SwUndoRenameBookmark()114 SwUndoRenameBookmark::~SwUndoRenameBookmark()
115 {
116 }
117 
lcl_QuoteName(std::u16string_view rName)118 static OUString lcl_QuoteName(std::u16string_view rName)
119 {
120     static const OUString sStart = SwResId(STR_START_QUOTE);
121     static const OUString sEnd = SwResId(STR_END_QUOTE);
122     return sStart + rName + sEnd;
123 }
124 
GetRewriter() const125 SwRewriter SwUndoRenameBookmark::GetRewriter() const
126 {
127     SwRewriter aRewriter;
128     aRewriter.AddRule(UndoArg1, lcl_QuoteName(m_sOldName));
129     aRewriter.AddRule(UndoArg2, SwResId(STR_YIELDS));
130     aRewriter.AddRule(UndoArg3, lcl_QuoteName(m_sNewName));
131     return aRewriter;
132 }
133 
Rename(::sw::UndoRedoContext const & rContext,const OUString & sFrom,const OUString & sTo)134 void SwUndoRenameBookmark::Rename(::sw::UndoRedoContext const & rContext, const OUString& sFrom, const OUString& sTo)
135 {
136     IDocumentMarkAccess* const pMarkAccess = rContext.GetDoc().getIDocumentMarkAccess();
137     IDocumentMarkAccess::const_iterator_t ppBkmk = pMarkAccess->findMark(sFrom);
138     if (ppBkmk != pMarkAccess->getAllMarksEnd())
139     {
140         pMarkAccess->renameMark( *ppBkmk, sTo );
141     }
142 }
143 
UndoImpl(::sw::UndoRedoContext & rContext)144 void SwUndoRenameBookmark::UndoImpl(::sw::UndoRedoContext & rContext)
145 {
146     Rename(rContext, m_sNewName, m_sOldName);
147 }
148 
RedoImpl(::sw::UndoRedoContext & rContext)149 void SwUndoRenameBookmark::RedoImpl(::sw::UndoRedoContext & rContext)
150 {
151     Rename(rContext, m_sOldName, m_sNewName);
152 }
153 
SwUndoInsNoTextFieldmark(const::sw::mark::IFieldmark & rFieldmark)154 SwUndoInsNoTextFieldmark::SwUndoInsNoTextFieldmark(const ::sw::mark::IFieldmark& rFieldmark)
155     : SwUndo(SwUndoId::INSERT, &rFieldmark.GetMarkPos().GetDoc())
156     , m_pHistoryNoTextFieldmark(new SwHistoryNoTextFieldmark(rFieldmark))
157 {
158 }
159 
UndoImpl(::sw::UndoRedoContext & rContext)160 void SwUndoInsNoTextFieldmark::UndoImpl(::sw::UndoRedoContext & rContext)
161 {
162     m_pHistoryNoTextFieldmark->ResetInDoc(rContext.GetDoc());
163 }
164 
RedoImpl(::sw::UndoRedoContext & rContext)165 void SwUndoInsNoTextFieldmark::RedoImpl(::sw::UndoRedoContext & rContext)
166 {
167     m_pHistoryNoTextFieldmark->SetInDoc(&rContext.GetDoc(), false);
168 }
169 
SwUndoDelNoTextFieldmark(const::sw::mark::IFieldmark & rFieldmark)170 SwUndoDelNoTextFieldmark::SwUndoDelNoTextFieldmark(const ::sw::mark::IFieldmark& rFieldmark)
171     : SwUndo(SwUndoId::DELETE, &rFieldmark.GetMarkPos().GetDoc())
172     , m_pHistoryNoTextFieldmark(new SwHistoryNoTextFieldmark(rFieldmark))
173 {
174 }
175 
176 SwUndoDelNoTextFieldmark::~SwUndoDelNoTextFieldmark() = default;
177 
UndoImpl(::sw::UndoRedoContext & rContext)178 void SwUndoDelNoTextFieldmark::UndoImpl(::sw::UndoRedoContext & rContext)
179 {
180     m_pHistoryNoTextFieldmark->SetInDoc(&rContext.GetDoc(), false);
181 }
182 
RedoImpl(::sw::UndoRedoContext & rContext)183 void SwUndoDelNoTextFieldmark::RedoImpl(::sw::UndoRedoContext & rContext)
184 {
185     m_pHistoryNoTextFieldmark->ResetInDoc(rContext.GetDoc());
186 }
187 
SwUndoInsTextFieldmark(const::sw::mark::IFieldmark & rFieldmark)188 SwUndoInsTextFieldmark::SwUndoInsTextFieldmark(const ::sw::mark::IFieldmark& rFieldmark)
189     : SwUndo(SwUndoId::INSERT, &rFieldmark.GetMarkPos().GetDoc())
190     , m_pHistoryTextFieldmark(new SwHistoryTextFieldmark(rFieldmark))
191 {
192 }
193 
UndoImpl(::sw::UndoRedoContext & rContext)194 void SwUndoInsTextFieldmark::UndoImpl(::sw::UndoRedoContext & rContext)
195 {
196     m_pHistoryTextFieldmark->ResetInDoc(rContext.GetDoc());
197 }
198 
RedoImpl(::sw::UndoRedoContext & rContext)199 void SwUndoInsTextFieldmark::RedoImpl(::sw::UndoRedoContext & rContext)
200 {
201     m_pHistoryTextFieldmark->SetInDoc(&rContext.GetDoc(), false);
202 }
203 
SwUndoDelTextFieldmark(const::sw::mark::IFieldmark & rFieldmark)204 SwUndoDelTextFieldmark::SwUndoDelTextFieldmark(const ::sw::mark::IFieldmark& rFieldmark)
205     : SwUndo(SwUndoId::DELETE, &rFieldmark.GetMarkPos().GetDoc())
206     , m_pHistoryTextFieldmark(new SwHistoryTextFieldmark(rFieldmark))
207 {
208 }
209 
210 SwUndoDelTextFieldmark::~SwUndoDelTextFieldmark() = default;
211 
UndoImpl(::sw::UndoRedoContext & rContext)212 void SwUndoDelTextFieldmark::UndoImpl(::sw::UndoRedoContext & rContext)
213 {
214     m_pHistoryTextFieldmark->SetInDoc(&rContext.GetDoc(), false);
215 }
216 
RedoImpl(::sw::UndoRedoContext & rContext)217 void SwUndoDelTextFieldmark::RedoImpl(::sw::UndoRedoContext & rContext)
218 {
219     m_pHistoryTextFieldmark->ResetInDoc(rContext.GetDoc());
220 }
221 
222 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
223