Line data Source code
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 :
21 : #include "UndoGuard.hxx"
22 : #include "ChartModelClone.hxx"
23 : #include "UndoActions.hxx"
24 :
25 : #include <com/sun/star/container/XChild.hpp>
26 :
27 : #include <tools/diagnose_ex.h>
28 :
29 : using namespace ::com::sun::star;
30 :
31 : using ::com::sun::star::uno::Reference;
32 : using ::com::sun::star::uno::Sequence;
33 : using ::rtl::OUString;
34 :
35 : namespace chart
36 : {
37 :
38 : //-----------------------------------------------------------------------------
39 :
40 0 : UndoGuard::UndoGuard( const OUString& i_undoString, const uno::Reference< document::XUndoManager > & i_undoManager,
41 : const ModelFacet i_facet )
42 0 : :m_xChartModel( i_undoManager->getParent(), uno::UNO_QUERY_THROW )
43 : ,m_xUndoManager( i_undoManager )
44 : ,m_pDocumentSnapshot()
45 : ,m_aUndoString( i_undoString )
46 0 : ,m_bActionPosted( false )
47 : {
48 0 : m_pDocumentSnapshot.reset( new ChartModelClone( m_xChartModel, i_facet ) );
49 0 : }
50 :
51 : //-----------------------------------------------------------------------------
52 :
53 0 : UndoGuard::~UndoGuard()
54 : {
55 0 : if ( !!m_pDocumentSnapshot )
56 0 : discardSnapshot();
57 0 : }
58 :
59 : //-----------------------------------------------------------------------------
60 :
61 0 : void UndoGuard::commit()
62 : {
63 0 : if ( !m_bActionPosted && !!m_pDocumentSnapshot )
64 : {
65 : try
66 : {
67 0 : const Reference< document::XUndoAction > xAction( new impl::UndoElement( m_aUndoString, m_xChartModel, m_pDocumentSnapshot ) );
68 0 : m_pDocumentSnapshot.reset(); // don't dispose, it's data went over to the UndoElement
69 0 : m_xUndoManager->addUndoAction( xAction );
70 : }
71 0 : catch( const uno::Exception& )
72 : {
73 : DBG_UNHANDLED_EXCEPTION();
74 : }
75 : }
76 0 : m_bActionPosted = true;
77 0 : }
78 :
79 : //-----------------------------------------------------------------------------
80 :
81 0 : void UndoGuard::rollback()
82 : {
83 0 : ENSURE_OR_RETURN_VOID( !!m_pDocumentSnapshot, "no snapshot!" );
84 0 : m_pDocumentSnapshot->applyToModel( m_xChartModel );
85 0 : discardSnapshot();
86 : }
87 :
88 : //-----------------------------------------------------------------------------
89 0 : void UndoGuard::discardSnapshot()
90 : {
91 0 : ENSURE_OR_RETURN_VOID( !!m_pDocumentSnapshot, "no snapshot!" );
92 0 : m_pDocumentSnapshot->dispose();
93 0 : m_pDocumentSnapshot.reset();
94 : }
95 :
96 : //-----------------------------------------------------------------------------
97 :
98 0 : UndoLiveUpdateGuard::UndoLiveUpdateGuard( const OUString& i_undoString, const uno::Reference< document::XUndoManager >& i_undoManager )
99 0 : :UndoGuard( i_undoString, i_undoManager, E_MODEL )
100 : {
101 0 : }
102 :
103 0 : UndoLiveUpdateGuard::~UndoLiveUpdateGuard()
104 : {
105 0 : if ( !isActionPosted() )
106 0 : rollback();
107 0 : }
108 :
109 : //-----------------------------------------------------------------------------
110 :
111 0 : UndoLiveUpdateGuardWithData::UndoLiveUpdateGuardWithData(
112 : const OUString& i_undoString, const uno::Reference< document::XUndoManager >& i_undoManager )
113 0 : :UndoGuard( i_undoString, i_undoManager, E_MODEL_WITH_DATA )
114 : {
115 0 : }
116 :
117 0 : UndoLiveUpdateGuardWithData::~UndoLiveUpdateGuardWithData()
118 : {
119 0 : if ( !isActionPosted() )
120 0 : rollback();
121 0 : }
122 :
123 : //-----------------------------------------------------------------------------
124 :
125 0 : UndoGuardWithSelection::UndoGuardWithSelection(
126 : const OUString& i_undoString, const uno::Reference< document::XUndoManager >& i_undoManager )
127 0 : :UndoGuard( i_undoString, i_undoManager, E_MODEL_WITH_SELECTION )
128 : {
129 0 : }
130 :
131 : //-----------------------------------------------------------------------------
132 :
133 0 : UndoGuardWithSelection::~UndoGuardWithSelection()
134 : {
135 0 : if ( !isActionPosted() )
136 0 : rollback();
137 0 : }
138 :
139 0 : HiddenUndoContext::HiddenUndoContext( const Reference< document::XUndoManager > & i_undoManager )
140 0 : :m_xUndoManager( i_undoManager )
141 : {
142 0 : ENSURE_OR_THROW( m_xUndoManager.is(), "invalid undo manager!" );
143 : try
144 : {
145 0 : m_xUndoManager->enterHiddenUndoContext();
146 : }
147 0 : catch( const uno::Exception& )
148 : {
149 : DBG_UNHANDLED_EXCEPTION();
150 0 : m_xUndoManager.clear();
151 : // prevents the leaveUndoContext in the dtor
152 : }
153 0 : }
154 :
155 : //-----------------------------------------------------------------------------
156 :
157 0 : HiddenUndoContext::~HiddenUndoContext()
158 : {
159 : try
160 : {
161 0 : if ( m_xUndoManager.is() )
162 0 : m_xUndoManager->leaveUndoContext();
163 : }
164 0 : catch( const uno::Exception& )
165 : {
166 : DBG_UNHANDLED_EXCEPTION();
167 : }
168 0 : }
169 :
170 : } // namespace chart
171 :
172 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|