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