1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #ifndef CHART2_UNDOGUARD_HXX
24 #define CHART2_UNDOGUARD_HXX
25 
26 #include "ChartModelClone.hxx"
27 
28 #include <com/sun/star/document/XUndoManager.hpp>
29 #include <com/sun/star/frame/XModel.hpp>
30 
31 #include <rtl/ustring.hxx>
32 
33 #include <boost/shared_ptr.hpp>
34 
35 namespace chart
36 {
37 
38 /** A guard which which does nothing, unless you explicitly call commitAction. In particular, in its destructor, it
39     does neither auto-commit nor auto-rollback the model changes.
40  */
41 class UndoGuard
42 {
43 public:
44     explicit UndoGuard(
45         const ::rtl::OUString& i_undoMessage,
46         const ::com::sun::star::uno::Reference< ::com::sun::star::document::XUndoManager > & i_undoManager,
47         const ModelFacet i_facet = E_MODEL
48     );
49     ~UndoGuard();
50 
51     void    commit();
52     void    rollback();
53 
54 protected:
isActionPosted() const55     bool    isActionPosted() const { return m_bActionPosted; }
56 
57 private:
58     void    discardSnapshot();
59 
60 private:
61     const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XModel >           m_xChartModel;
62     const ::com::sun::star::uno::Reference< ::com::sun::star::document::XUndoManager >  m_xUndoManager;
63 
64     ::boost::shared_ptr< ChartModelClone >  m_pDocumentSnapshot;
65     rtl::OUString                           m_aUndoString;
66     bool                                    m_bActionPosted;
67 };
68 
69 /** A guard which, in its destructor, restores the model state it found in the constructor. If
70     <member>commitAction</member> is called inbetween, the restouration is not performed.
71  */
72 class UndoLiveUpdateGuard : public UndoGuard
73 {
74 public:
75     explicit UndoLiveUpdateGuard(
76         const ::rtl::OUString& i_undoMessage,
77         const ::com::sun::star::uno::Reference< ::com::sun::star::document::XUndoManager > & i_undoManager
78     );
79     ~UndoLiveUpdateGuard();
80 };
81 
82 /** Same as UndoLiveUpdateGuard but with additional storage of the chart's data.
83     Only use this if the data has internal data.
84  */
85 class UndoLiveUpdateGuardWithData :
86         public UndoGuard
87 {
88 public:
89     explicit UndoLiveUpdateGuardWithData(
90         const ::rtl::OUString& i_undoMessage,
91         const ::com::sun::star::uno::Reference< ::com::sun::star::document::XUndoManager > & i_undoManager
92     );
93     ~UndoLiveUpdateGuardWithData();
94 };
95 
96 class UndoGuardWithSelection : public UndoGuard
97 {
98 public:
99     explicit UndoGuardWithSelection(
100         const ::rtl::OUString& i_undoMessage,
101         const ::com::sun::star::uno::Reference< ::com::sun::star::document::XUndoManager > & i_undoManager
102     );
103     virtual ~UndoGuardWithSelection();
104 };
105 
106 class UndoContext
107 {
108 public:
109     UndoContext(
110         const ::com::sun::star::uno::Reference< ::com::sun::star::document::XUndoManager > & i_undoManager,
111         const ::rtl::OUString& i_undoTitle
112     );
113     ~UndoContext();
114 
115 private:
116     const ::com::sun::star::uno::Reference< ::com::sun::star::document::XUndoManager >  m_xUndoManager;
117 };
118 
119 class HiddenUndoContext
120 {
121 public:
122     HiddenUndoContext(
123         const ::com::sun::star::uno::Reference< ::com::sun::star::document::XUndoManager > & i_undoManager
124     );
125     ~HiddenUndoContext();
126 
127 private:
128     ::com::sun::star::uno::Reference< ::com::sun::star::document::XUndoManager >    m_xUndoManager;
129 };
130 
131 }
132 // CHART2_UNDOGUARD_HXX
133 #endif
134