• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.10.5 API Reference
  • KDE Home
  • Contact Us
 

akonadi

  • akonadi
  • calendar
history.cpp
1 /*
2  Copyright (C) 2010-2012 Sérgio Martins <iamsergio@gmail.com>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 */
19 
20 #include "history.h"
21 #include "history_p.h"
22 
23 #include <kcalutils/stringify.h>
24 
25 using namespace KCalCore;
26 using namespace Akonadi;
27 
28 History::History( QObject *parent ) : QObject( parent ), d( new Private( this ) )
29 {
30  d->mChanger = new IncidenceChanger( /*history=*/false, this );
31  d->mChanger->setObjectName( "changer" ); // for auto-connects
32  d->mOperationTypeInProgress = TypeNone;
33  d->mEnabled = true;
34  d->mUndoAllInProgress = false;
35 }
36 
37 History::~History()
38 {
39  delete d;
40 }
41 
42 History::Private::Private( History *qq ) : q( qq )
43 {
44 
45 }
46 
47 void History::recordCreation( const Akonadi::Item &item,
48  const QString &description,
49  const uint atomicOperationId )
50 {
51  Q_ASSERT_X( item.isValid(), "History::recordCreation()",
52  "Item must be valid." );
53 
54  Q_ASSERT_X( item.hasPayload<KCalCore::Incidence::Ptr>(), "History::recordCreation()",
55  "Item must have Incidence::Ptr payload." );
56 
57  Entry::Ptr entry( new CreationEntry( item, description, this ) );
58 
59  d->stackEntry( entry, atomicOperationId );
60 }
61 
62 void History::recordModification( const Akonadi::Item &oldItem,
63  const Akonadi::Item &newItem,
64  const QString &description,
65  const uint atomicOperationId )
66 {
67  Q_ASSERT_X( oldItem.isValid(), "History::recordModification", "old item must be valid" );
68  Q_ASSERT_X( newItem.isValid(), "History::recordModification", "newItem item must be valid" );
69  Q_ASSERT_X( oldItem.hasPayload<KCalCore::Incidence::Ptr>(), "History::recordModification",
70  "old item must have Incidence::Ptr payload" );
71  Q_ASSERT_X( newItem.hasPayload<KCalCore::Incidence::Ptr>(), "History::recordModification",
72  "newItem item must have Incidence::Ptr payload" );
73 
74  Entry::Ptr entry( new ModificationEntry( newItem, oldItem.payload<KCalCore::Incidence::Ptr>(),
75  description, this ) );
76 
77  Q_ASSERT( newItem.revision() >= oldItem.revision() );
78 
79  d->stackEntry( entry, atomicOperationId );
80 }
81 
82 void History::recordDeletion( const Akonadi::Item &item,
83  const QString &description,
84  const uint atomicOperationId )
85 {
86  Q_ASSERT_X( item.isValid(), "History::recordDeletion", "Item must be valid" );
87  Item::List list;
88  list.append( item );
89  recordDeletions( list, description, atomicOperationId );
90 }
91 
92 void History::recordDeletions( const Akonadi::Item::List &items,
93  const QString &description,
94  const uint atomicOperationId )
95 {
96  Entry::Ptr entry( new DeletionEntry( items, description, this ) );
97 
98  foreach( const Akonadi::Item &item, items ) {
99  Q_ASSERT_X( item.isValid(),
100  "History::recordDeletion()", "Item must be valid." );
101  Q_ASSERT_X( item.hasPayload<Incidence::Ptr>(),
102  "History::recordDeletion()", "Item must have an Incidence::Ptr payload." );
103  }
104 
105  d->stackEntry( entry, atomicOperationId );
106 }
107 
108 QString History::nextUndoDescription() const
109 {
110  if ( !d->mUndoStack.isEmpty() )
111  return d->mUndoStack.top()->mDescription;
112  else
113  return QString();
114 }
115 
116 QString History::nextRedoDescription() const
117 {
118  if ( !d->mRedoStack.isEmpty() )
119  return d->mRedoStack.top()->mDescription;
120  else
121  return QString();
122 }
123 
124 void History::undo( QWidget *parent )
125 {
126  d->undoOrRedo( TypeUndo, parent );
127 }
128 
129 void History::redo( QWidget *parent )
130 {
131  d->undoOrRedo( TypeRedo, parent );
132 }
133 
134 void History::undoAll( QWidget *parent )
135 {
136  if ( d->mOperationTypeInProgress != TypeNone ) {
137  kWarning() << "Dont call History::undoAll() while an undo/redo/undoAll is in progress";
138  } else if ( d->mEnabled ) {
139  d->mUndoAllInProgress = true;
140  d->mCurrentParent = parent;
141  d->doIt( TypeUndo );
142  } else {
143  kWarning() << "Don't call undo/redo when History is disabled";
144  }
145 }
146 
147 bool History::clear()
148 {
149  bool result = true;
150  if ( d->mOperationTypeInProgress == TypeNone ) {
151  d->mRedoStack.clear();
152  d->mUndoStack.clear();
153  d->mLastErrorString.clear();
154  d->mQueuedEntries.clear();
155  } else {
156  result = false;
157  }
158  emit changed();
159  return result;
160 }
161 
162 QString History::lastErrorString() const
163 {
164  return d->mLastErrorString;
165 }
166 
167 void History::setEnabled( bool enabled )
168 {
169  if ( enabled != d->mEnabled ) {
170  d->mEnabled = enabled;
171  }
172 }
173 
174 bool History::undoAvailable() const
175 {
176  return !d->mUndoStack.isEmpty() && d->mOperationTypeInProgress == TypeNone;
177 }
178 
179 bool History::redoAvailable() const
180 {
181  return !d->mRedoStack.isEmpty() && d->mOperationTypeInProgress == TypeNone;
182 }
183 
184 void History::Private::updateIds( Item::Id oldId, Item::Id newId )
185 {
186  mEntryInProgress->updateIds( oldId, newId );
187 
188  foreach( const Entry::Ptr &entry, mUndoStack )
189  entry->updateIds( oldId, newId );
190 
191  foreach( const Entry::Ptr &entry, mRedoStack )
192  entry->updateIds( oldId, newId );
193 }
194 
195 void History::Private::doIt( OperationType type )
196 {
197  mOperationTypeInProgress = type;
198  emit q->changed(); // Application will disable undo/redo buttons because operation is in progress
199  Q_ASSERT( !stack().isEmpty() );
200  mEntryInProgress = stack().pop();
201 
202  connect( mEntryInProgress.data(), SIGNAL(finished(Akonadi::IncidenceChanger::ResultCode,QString)),
203  SLOT(handleFinished(Akonadi::IncidenceChanger::ResultCode,QString)),
204  Qt::UniqueConnection);
205  mEntryInProgress->doIt( type );
206 }
207 
208 void History::Private::handleFinished( IncidenceChanger::ResultCode changerResult,
209  const QString &errorString )
210 {
211  Q_ASSERT( mOperationTypeInProgress != TypeNone );
212  Q_ASSERT( !( mUndoAllInProgress && mOperationTypeInProgress == TypeRedo ) );
213 
214  const bool success = ( changerResult == IncidenceChanger::ResultCodeSuccess );
215  const History::ResultCode resultCode = success ? History::ResultCodeSuccess :
216  History::ResultCodeError;
217 
218  if ( success ) {
219  mLastErrorString.clear();
220  destinationStack().push( mEntryInProgress );
221  } else {
222  mLastErrorString = errorString;
223  stack().push( mEntryInProgress );
224  }
225 
226  mCurrentParent = 0;
227 
228  // Process recordCreation/Modification/Deletions that came in while an operation
229  // was in progress
230  if ( !mQueuedEntries.isEmpty() ) {
231  mRedoStack.clear();
232  foreach( const Entry::Ptr &entry, mQueuedEntries ) {
233  mUndoStack.push( entry );
234  }
235  mQueuedEntries.clear();
236  }
237 
238  emitDone( mOperationTypeInProgress, resultCode );
239  mOperationTypeInProgress = TypeNone;
240  emit q->changed();
241 }
242 
243 void History::Private::stackEntry( const Entry::Ptr &entry, uint atomicOperationId )
244 {
245  const bool useMultiEntry = ( atomicOperationId > 0 );
246 
247  Entry::Ptr entryToPush;
248 
249  if ( useMultiEntry ) {
250  Entry::Ptr topEntry = ( mOperationTypeInProgress == TypeNone ) ?
251  ( mUndoStack.isEmpty() ? Entry::Ptr() : mUndoStack.top() ) :
252  ( mQueuedEntries.isEmpty() ? Entry::Ptr() : mQueuedEntries.last() );
253 
254  const bool topIsMultiEntry = qobject_cast<MultiEntry*>( topEntry.data() );
255 
256  if ( topIsMultiEntry ) {
257  MultiEntry::Ptr multiEntry = topEntry.staticCast<MultiEntry>();
258  if ( multiEntry->mAtomicOperationId != atomicOperationId ) {
259  multiEntry = MultiEntry::Ptr( new MultiEntry( atomicOperationId, entry->mDescription, q ) );
260  entryToPush = multiEntry;
261  }
262  multiEntry->addEntry( entry );
263  } else {
264  MultiEntry::Ptr multiEntry = MultiEntry::Ptr( new MultiEntry( atomicOperationId,
265  entry->mDescription, q ) );
266  multiEntry->addEntry( entry );
267  entryToPush = multiEntry;
268  }
269  } else {
270  entryToPush = entry;
271  }
272 
273  if ( mOperationTypeInProgress == TypeNone ) {
274  if ( entryToPush ) {
275  mUndoStack.push( entryToPush );
276  }
277  mRedoStack.clear();
278  emit q->changed();
279  } else {
280  if ( entryToPush ) {
281  mQueuedEntries.append( entryToPush );
282  }
283  }
284 }
285 
286 void History::Private::undoOrRedo( OperationType type, QWidget *parent )
287 {
288  // Don't call undo() without the previous one finishing
289  Q_ASSERT( mOperationTypeInProgress == TypeNone );
290 
291  if ( !stack( type ).isEmpty() ) {
292  if ( mEnabled ) {
293  mCurrentParent = parent;
294  doIt( type );
295  } else {
296  kWarning() << "Don't call undo/redo when History is disabled";
297  }
298  } else {
299  kWarning() << "Don't call undo/redo when the stack is empty.";
300  }
301 }
302 
303 QStack<Entry::Ptr>& History::Private::stack( OperationType type )
304 {
305  // Entries from the undo stack go to the redo stack, and vice-versa
306  return type == TypeUndo ? mUndoStack : mRedoStack;
307 }
308 
309 QStack<Entry::Ptr>& History::Private::stack()
310 {
311  return stack( mOperationTypeInProgress );
312 }
313 
314 QStack<Entry::Ptr>& History::Private::destinationStack()
315 {
316  // Entries from the undo stack go to the redo stack, and vice-versa
317  return mOperationTypeInProgress == TypeRedo ? mUndoStack : mRedoStack;
318 }
319 
320 void History::Private::emitDone( OperationType type, History::ResultCode resultCode )
321 {
322  if ( type == TypeUndo ) {
323  emit q->undone( resultCode );
324  } else if ( type == TypeRedo ){
325  emit q->redone( resultCode );
326  } else {
327  Q_ASSERT( false );
328  }
329 }
330 
331 #include "history.moc"
332 #include "history_p.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:27:37 by doxygen 1.8.3.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

Skip menu "akonadi"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Modules
  • Related Pages

kdepimlibs-4.10.5 API Reference

Skip menu "kdepimlibs-4.10.5 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal