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