1/*
2 Copyright (c) 2007 Volker Krause <vkrause@kde.org>
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 "changerecorder.h"
21#include "changerecorder_p.h"
22
23#include <kdebug.h>
24#include <QtCore/QSettings>
25
26using namespace Akonadi;
27
28ChangeRecorder::ChangeRecorder(QObject *parent)
29 : Monitor(new ChangeRecorderPrivate(0, this), parent)
30{
31}
32
33ChangeRecorder::ChangeRecorder(ChangeRecorderPrivate *privateclass, QObject *parent)
34 : Monitor(privateclass, parent)
35{
36}
37
38ChangeRecorder::~ChangeRecorder()
39{
40}
41
42void ChangeRecorder::setConfig(QSettings *settings)
43{
44 Q_D(ChangeRecorder);
45 if (settings) {
46 d->settings = settings;
47 Q_ASSERT(d->pendingNotifications.isEmpty());
48 d->loadNotifications();
49 } else if (d->settings) {
50 if (d->enableChangeRecording) {
51 d->saveNotifications();
52 }
53 d->settings = settings;
54 }
55}
56
57void ChangeRecorder::replayNext()
58{
59 Q_D(ChangeRecorder);
60
61 if (!d->enableChangeRecording) {
62 return;
63 }
64
65 if (!d->pendingNotifications.isEmpty()) {
66 const NotificationMessageV3 msg = d->pendingNotifications.head();
67 if (d->ensureDataAvailable(msg)) {
68 if (!d->emitNotification(msg)) {
69 //If no signal was emitted (e.g. because noone was connected to it), noone is going to call changeProcessed, so we help ourselves
70 d->dequeueNotification();
71 return replayNext();
72 }
73 } else if (d->translateAndCompress(d->pipeline, msg)) {
74 // The msg is now in both pipeline and pendingNotifications.
75 // When data is available, MonitorPrivate::flushPipeline will emitNotification.
76 // When changeProcessed is called, we'll finally remove it from pendingNotifications.
77 } else {
78 // In the case of a move where both source and destination are
79 // ignored, we ignore the message and process the next one.
80 d->dequeueNotification();
81 return replayNext();
82 }
83 } else {
84 // This is necessary when none of the notifications were accepted / processed
85 // above, and so there is no one to call changeProcessed() and the ChangeReplay task
86 // will be stuck forever in the ResourceScheduler.
87 emit nothingToReplay();
88 }
89}
90
91bool ChangeRecorder::isEmpty() const
92{
93 Q_D(const ChangeRecorder);
94 return d->pendingNotifications.isEmpty();
95}
96
97void ChangeRecorder::changeProcessed()
98{
99 Q_D(ChangeRecorder);
100
101 if (!d->enableChangeRecording) {
102 return;
103 }
104
105 // changerecordertest.cpp calls changeProcessed after receiving nothingToReplay,
106 // so test for emptiness. Not sure real code does this though.
107 // Q_ASSERT( !d->pendingNotifications.isEmpty() )
108 if (!d->pendingNotifications.isEmpty()) {
109 d->dequeueNotification();
110 }
111}
112
113void ChangeRecorder::setChangeRecordingEnabled(bool enable)
114{
115 Q_D(ChangeRecorder);
116 if (d->enableChangeRecording == enable) {
117 return;
118 }
119 d->enableChangeRecording = enable;
120 if (enable) {
121 d->m_needFullSave = true;
122 d->notificationsLoaded();
123 } else {
124 d->dispatchNotifications();
125 }
126}
127
128QString Akonadi::ChangeRecorder::dumpNotificationListToString() const
129{
130 Q_D(const ChangeRecorder);
131 return d->dumpNotificationListToString();
132}
133