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 #ifndef CHART2_UNDOGUARD_HXX
28 #define CHART2_UNDOGUARD_HXX
29 
30 #include "ChartModelClone.hxx"
31 
32 #include <com/sun/star/document/XUndoManager.hpp>
33 #include <com/sun/star/frame/XModel.hpp>
34 
35 #include <rtl/ustring.hxx>
36 
37 #include <boost/shared_ptr.hpp>
38 
39 namespace chart
40 {
41 
42 /** A guard which which does nothing, unless you explicitly call commitAction. In particular, in its destructor, it
43     does neither auto-commit nor auto-rollback the model changes.
44  */
45 class UndoGuard
46 {
47 public:
48     explicit UndoGuard(
49         const ::rtl::OUString& i_undoMessage,
50         const ::com::sun::star::uno::Reference< ::com::sun::star::document::XUndoManager > & i_undoManager,
51         const ModelFacet i_facet = E_MODEL
52     );
53     ~UndoGuard();
54 
55     void    commit();
56     void    rollback();
57 
58 protected:
59     bool    isActionPosted() const { return m_bActionPosted; }
60 
61 private:
62     void    discardSnapshot();
63 
64 private:
65     const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XModel >           m_xChartModel;
66     const ::com::sun::star::uno::Reference< ::com::sun::star::document::XUndoManager >  m_xUndoManager;
67 
68     ::boost::shared_ptr< ChartModelClone >  m_pDocumentSnapshot;
69     rtl::OUString                           m_aUndoString;
70     bool                                    m_bActionPosted;
71 };
72 
73 /** A guard which, in its destructor, restores the model state it found in the constructor. If
74     <member>commitAction</member> is called inbetween, the restouration is not performed.
75  */
76 class UndoLiveUpdateGuard : public UndoGuard
77 {
78 public:
79     explicit UndoLiveUpdateGuard(
80         const ::rtl::OUString& i_undoMessage,
81         const ::com::sun::star::uno::Reference< ::com::sun::star::document::XUndoManager > & i_undoManager
82     );
83     ~UndoLiveUpdateGuard();
84 };
85 
86 /** Same as UndoLiveUpdateGuard but with additional storage of the chart's data.
87     Only use this if the data has internal data.
88  */
89 class UndoLiveUpdateGuardWithData :
90         public UndoGuard
91 {
92 public:
93     explicit UndoLiveUpdateGuardWithData(
94         const ::rtl::OUString& i_undoMessage,
95         const ::com::sun::star::uno::Reference< ::com::sun::star::document::XUndoManager > & i_undoManager
96     );
97     ~UndoLiveUpdateGuardWithData();
98 };
99 
100 class UndoGuardWithSelection : public UndoGuard
101 {
102 public:
103     explicit UndoGuardWithSelection(
104         const ::rtl::OUString& i_undoMessage,
105         const ::com::sun::star::uno::Reference< ::com::sun::star::document::XUndoManager > & i_undoManager
106     );
107     virtual ~UndoGuardWithSelection();
108 };
109 
110 class UndoContext
111 {
112 public:
113     UndoContext(
114         const ::com::sun::star::uno::Reference< ::com::sun::star::document::XUndoManager > & i_undoManager,
115         const ::rtl::OUString& i_undoTitle
116     );
117     ~UndoContext();
118 
119 private:
120     const ::com::sun::star::uno::Reference< ::com::sun::star::document::XUndoManager >  m_xUndoManager;
121 };
122 
123 class HiddenUndoContext
124 {
125 public:
126     HiddenUndoContext(
127         const ::com::sun::star::uno::Reference< ::com::sun::star::document::XUndoManager > & i_undoManager
128     );
129     ~HiddenUndoContext();
130 
131 private:
132     ::com::sun::star::uno::Reference< ::com::sun::star::document::XUndoManager >    m_xUndoManager;
133 };
134 
135 }
136 // CHART2_UNDOGUARD_HXX
137 #endif
138