Rev 25027 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
25027 | jghali | 1 | |
2 | /* |
||
3 | For general Scribus (>=1.3.2) copyright and licensing information please refer |
||
4 | to the COPYING file provided with the program. Following this notice may exist |
||
5 | a copyright and/or license notice that predates the release of Scribus 1.3.2 |
||
6 | for which a new license (GPL+exception) is in place. |
||
7 | */ |
||
8 | /*************************************************************************** |
||
9 | * Copyright (C) 2008 by Andreas Vox * |
||
10 | * * |
||
11 | * This program is free software; you can redistribute it and/or modify * |
||
12 | * it under the terms of the GNU General Public License as published by * |
||
13 | * the Free Software Foundation; either version 2 of the License, or * |
||
14 | * (at your option) any later version. * |
||
15 | * * |
||
16 | * This program is distributed in the hope that it will be useful, * |
||
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
||
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
||
19 | * GNU General Public License for more details. * |
||
20 | * * |
||
21 | * You should have received a copy of the GNU General Public License * |
||
22 | * along with this program; if not, write to the * |
||
23 | * Free Software Foundation, Inc., * |
||
24 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * |
||
25 | ***************************************************************************/ |
||
26 | |||
27 | #ifndef TRANSACTION_H |
||
28 | #define TRANSACTION_H |
||
29 | |||
30 | #include "scribusapi.h" |
||
31 | |||
32 | #include <QSharedData> |
||
33 | #include <QExplicitlySharedDataPointer> |
||
34 | |||
35 | /** |
||
36 | Interface class for objects representing a going transaction. |
||
37 | Will be specialized by classes like Undomanager for use as |
||
38 | a result type in their beginTransaction() method. |
||
39 | */ |
||
40 | class SCRIBUS_API Transaction { |
||
41 | |||
42 | public: |
||
25210 | jghali | 43 | enum Status |
44 | { |
||
25027 | jghali | 45 | STATE_OPEN, |
46 | STATE_WILLFAIL, |
||
47 | STATE_FAILED, |
||
48 | STATE_COMMITTED |
||
49 | }; |
||
50 | |||
51 | class TransactionStateBase : public QSharedData |
||
52 | { |
||
53 | public: |
||
54 | virtual ~TransactionStateBase() = default; |
||
55 | |||
25210 | jghali | 56 | Status m_status { STATE_OPEN }; |
25027 | jghali | 57 | }; |
58 | |||
59 | Transaction(TransactionStateBase* data) : m_data(data) |
||
60 | { |
||
25210 | jghali | 61 | if (data != nullptr) |
25027 | jghali | 62 | m_data->m_status = STATE_OPEN; |
63 | } |
||
64 | |||
65 | /** |
||
66 | Automatically commits if forgotten. |
||
67 | Override as appropriate. Since the superclass destructor is called last, you |
||
68 | can cancel in the subclass destructor; the commit here then will do nothing. |
||
69 | Don't forget to set m_data to NULL if you free the m_data pointer! |
||
70 | */ |
||
71 | virtual ~Transaction() = default; |
||
72 | |||
73 | /** |
||
74 | Test if transaction has some valid data |
||
75 | */ |
||
76 | operator bool() const { return (m_data.constData() != 0); } |
||
77 | |||
78 | /** |
||
79 | Commits this transaction if in STATE_OPEN. |
||
80 | If in STATE_WILLFAIL, cancel the transaction instead; otherwise do nothing. |
||
81 | Returns true iff the transaction was committed or canceled. |
||
82 | */ |
||
83 | virtual bool commit() = 0; |
||
84 | |||
85 | /** |
||
86 | Cancels this transaction if in STATE_OPEN or STATE_WILLFAIL; otherwise do nothing |
||
87 | Returns true iff the transaction was canceled. |
||
88 | */ |
||
89 | virtual bool cancel() = 0; |
||
90 | |||
91 | /** |
||
92 | Marks this transaction as failed. |
||
93 | */ |
||
94 | virtual void markFailed(); |
||
95 | |||
96 | /** |
||
97 | Reset underlying transaction data |
||
98 | */ |
||
99 | virtual void reset(); |
||
100 | |||
101 | int getState() const; |
||
102 | |||
103 | bool isNull() const { return (m_data.constData() == 0); } |
||
104 | |||
105 | bool isStarted() const { return (m_data.constData() != 0); } |
||
106 | |||
107 | bool isOpened() const; |
||
108 | |||
109 | protected: |
||
110 | // if you subclass, do *not* add any data members but use this pointer instead, |
||
111 | // otherwise the copy initializer will strip your objects. |
||
112 | QExplicitlySharedDataPointer<TransactionStateBase> m_data; |
||
113 | }; |
||
114 | |||
115 | #endif |