1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_chart2.hxx"
30 
31 #include "UndoGuard.hxx"
32 #include "ChartModelClone.hxx"
33 #include "UndoActions.hxx"
34 
35 #include <com/sun/star/container/XChild.hpp>
36 
37 #include <tools/diagnose_ex.h>
38 
39 using namespace ::com::sun::star;
40 
41 using ::com::sun::star::uno::Reference;
42 using ::com::sun::star::uno::Sequence;
43 using ::rtl::OUString;
44 
45 namespace chart
46 {
47 
48 //-----------------------------------------------------------------------------
49 
50 UndoGuard::UndoGuard( const OUString& i_undoString, const uno::Reference< document::XUndoManager > & i_undoManager,
51                                const ModelFacet i_facet )
52     :m_xChartModel( i_undoManager->getParent(), uno::UNO_QUERY_THROW )
53     ,m_xUndoManager( i_undoManager )
54     ,m_pDocumentSnapshot()
55     ,m_aUndoString( i_undoString )
56     ,m_bActionPosted( false )
57 {
58     m_pDocumentSnapshot.reset( new ChartModelClone( m_xChartModel, i_facet ) );
59 }
60 
61 //-----------------------------------------------------------------------------
62 
63 UndoGuard::~UndoGuard()
64 {
65     if ( !!m_pDocumentSnapshot )
66         discardSnapshot();
67 }
68 
69 //-----------------------------------------------------------------------------
70 
71 void UndoGuard::commit()
72 {
73     if ( !m_bActionPosted && !!m_pDocumentSnapshot )
74     {
75         try
76         {
77             const Reference< document::XUndoAction > xAction( new impl::UndoElement( m_aUndoString, m_xChartModel, m_pDocumentSnapshot ) );
78             m_pDocumentSnapshot.reset();    // don't dispose, it's data went over to the UndoElement
79             m_xUndoManager->addUndoAction( xAction );
80         }
81         catch( const uno::Exception& )
82         {
83         	DBG_UNHANDLED_EXCEPTION();
84         }
85     }
86     m_bActionPosted = true;
87 }
88 
89 //-----------------------------------------------------------------------------
90 
91 void UndoGuard::rollback()
92 {
93     ENSURE_OR_RETURN_VOID( !!m_pDocumentSnapshot, "no snapshot!" );
94     m_pDocumentSnapshot->applyToModel( m_xChartModel );
95     discardSnapshot();
96 }
97 
98 //-----------------------------------------------------------------------------
99 void UndoGuard::discardSnapshot()
100 {
101     ENSURE_OR_RETURN_VOID( !!m_pDocumentSnapshot, "no snapshot!" );
102     m_pDocumentSnapshot->dispose();
103     m_pDocumentSnapshot.reset();
104 }
105 
106 //-----------------------------------------------------------------------------
107 
108 UndoLiveUpdateGuard::UndoLiveUpdateGuard( const OUString& i_undoString, const uno::Reference< document::XUndoManager >& i_undoManager )
109     :UndoGuard( i_undoString, i_undoManager, E_MODEL )
110 {
111 }
112 
113 UndoLiveUpdateGuard::~UndoLiveUpdateGuard()
114 {
115     if ( !isActionPosted() )
116         rollback();
117 }
118 
119 //-----------------------------------------------------------------------------
120 
121 UndoLiveUpdateGuardWithData::UndoLiveUpdateGuardWithData(
122         const OUString& i_undoString, const uno::Reference< document::XUndoManager >& i_undoManager )
123     :UndoGuard( i_undoString, i_undoManager, E_MODEL_WITH_DATA )
124 {
125 }
126 
127 UndoLiveUpdateGuardWithData::~UndoLiveUpdateGuardWithData()
128 {
129     if ( !isActionPosted() )
130         rollback();
131 }
132 
133 //-----------------------------------------------------------------------------
134 
135 UndoGuardWithSelection::UndoGuardWithSelection(
136         const OUString& i_undoString, const uno::Reference< document::XUndoManager >& i_undoManager )
137     :UndoGuard( i_undoString, i_undoManager, E_MODEL_WITH_SELECTION )
138 {
139 }
140 
141 //-----------------------------------------------------------------------------
142 
143 UndoGuardWithSelection::~UndoGuardWithSelection()
144 {
145     if ( !isActionPosted() )
146         rollback();
147 }
148 
149 //-----------------------------------------------------------------------------
150 
151 UndoContext::UndoContext( const Reference< document::XUndoManager > & i_undoManager, const ::rtl::OUString& i_undoTitle )
152     :m_xUndoManager( i_undoManager )
153 {
154     ENSURE_OR_THROW( m_xUndoManager.is(), "invalid undo manager!" );
155     m_xUndoManager->enterUndoContext( i_undoTitle );
156 }
157 
158 //-----------------------------------------------------------------------------
159 
160 UndoContext::~UndoContext()
161 {
162     m_xUndoManager->leaveUndoContext();
163 }
164 
165 //-----------------------------------------------------------------------------
166 
167 HiddenUndoContext::HiddenUndoContext( const Reference< document::XUndoManager > & i_undoManager )
168     :m_xUndoManager( i_undoManager )
169 {
170     ENSURE_OR_THROW( m_xUndoManager.is(), "invalid undo manager!" );
171     try
172     {
173         m_xUndoManager->enterHiddenUndoContext();
174     }
175     catch( const uno::Exception& )
176     {
177     	DBG_UNHANDLED_EXCEPTION();
178         m_xUndoManager.clear();
179             // prevents the leaveUndoContext in the dtor
180     }
181 }
182 
183 //-----------------------------------------------------------------------------
184 
185 HiddenUndoContext::~HiddenUndoContext()
186 {
187     try
188     {
189         if ( m_xUndoManager.is() )
190             m_xUndoManager->leaveUndoContext();
191     }
192     catch( const uno::Exception& )
193     {
194     	DBG_UNHANDLED_EXCEPTION();
195     }
196 }
197 
198 } //  namespace chart
199