1/*
2 Copyright (c) 2008 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 "cachepolicypage.h"
21
22#include "ui_cachepolicypage.h"
23
24#include "cachepolicy.h"
25#include "collection.h"
26#include "collectionutils_p.h"
27
28using namespace Akonadi;
29
30class CachePolicyPage::Private
31{
32public:
33 Private()
34 : mUi(new Ui::CachePolicyPage)
35 {
36 }
37
38 ~Private()
39 {
40 delete mUi;
41 }
42
43 void slotIntervalValueChanged(int);
44 void slotCacheValueChanged(int);
45 void slotRetrievalOptionsGroupBoxDisabled(bool disable);
46
47 Ui::CachePolicyPage *mUi;
48};
49
50void CachePolicyPage::Private::slotIntervalValueChanged(int interval)
51{
52 mUi->checkInterval->setSuffix(QLatin1Char(' ') + i18np("minute", "minutes", interval));
53}
54
55void CachePolicyPage::Private::slotCacheValueChanged(int interval)
56{
57 mUi->localCacheTimeout->setSuffix(QLatin1Char(' ') + i18np("minute", "minutes", interval));
58}
59
60void CachePolicyPage::Private::slotRetrievalOptionsGroupBoxDisabled(bool disable)
61{
62 mUi->retrievalOptionsGroupBox->setDisabled(disable);
63 if (!disable) {
64 mUi->label->setEnabled(mUi->retrieveOnlyHeaders->isChecked());
65 mUi->localCacheTimeout->setEnabled(mUi->retrieveOnlyHeaders->isChecked());
66 }
67}
68
69CachePolicyPage::CachePolicyPage(QWidget *parent, GuiMode mode)
70 : CollectionPropertiesPage(parent)
71 , d(new Private)
72{
73 setObjectName(QLatin1String("Akonadi::CachePolicyPage"));
74 setPageTitle(i18n("Retrieval"));
75
76 d->mUi->setupUi(this);
77 connect(d->mUi->checkInterval, SIGNAL(valueChanged(int)),
78 SLOT(slotIntervalValueChanged(int)));
79 connect(d->mUi->localCacheTimeout, SIGNAL(valueChanged(int)),
80 SLOT(slotCacheValueChanged(int)));
81 connect(d->mUi->inherit, SIGNAL(toggled(bool)),
82 SLOT(slotRetrievalOptionsGroupBoxDisabled(bool)));
83 if (mode == AdvancedMode) {
84 d->mUi->stackedWidget->setCurrentWidget(d->mUi->rawPage);
85 }
86}
87
88CachePolicyPage::~CachePolicyPage()
89{
90 delete d;
91}
92
93bool Akonadi::CachePolicyPage::canHandle(const Collection &collection) const
94{
95 return !collection.isVirtual();
96}
97
98void CachePolicyPage::load(const Collection &collection)
99{
100 const CachePolicy policy = collection.cachePolicy();
101
102 int interval = policy.intervalCheckTime();
103 if (interval == -1) {
104 interval = 0;
105 }
106
107 int cache = policy.cacheTimeout();
108 if (cache == -1) {
109 cache = 0;
110 }
111
112 d->mUi->inherit->setChecked(policy.inheritFromParent());
113 d->mUi->checkInterval->setValue(interval);
114 d->mUi->localCacheTimeout->setValue(cache);
115 d->mUi->syncOnDemand->setChecked(policy.syncOnDemand());
116 d->mUi->localParts->setItems(policy.localParts());
117
118 const bool fetchBodies = policy.localParts().contains(QLatin1String("RFC822"));
119 d->mUi->retrieveFullMessages->setChecked(fetchBodies);
120
121 //done explicitly to disable/enabled widgets
122 d->mUi->retrieveOnlyHeaders->setChecked(!fetchBodies);
123 d->mUi->label->setEnabled(!fetchBodies);
124 d->mUi->localCacheTimeout->setEnabled(!fetchBodies);
125}
126
127void CachePolicyPage::save(Collection &collection)
128{
129 int interval = d->mUi->checkInterval->value();
130 if (interval == 0) {
131 interval = -1;
132 }
133
134 int cache = d->mUi->localCacheTimeout->value();
135 if (cache == 0) {
136 cache = -1;
137 }
138
139 CachePolicy policy = collection.cachePolicy();
140 policy.setInheritFromParent(d->mUi->inherit->isChecked());
141 policy.setIntervalCheckTime(interval);
142 policy.setCacheTimeout(cache);
143 policy.setSyncOnDemand(d->mUi->syncOnDemand->isChecked());
144
145 QStringList localParts = d->mUi->localParts->items();
146
147 // Unless we are in "raw" mode, add "bodies" to the list of message
148 // parts to keep around locally, if the user selected that, or remove
149 // it otherwise. In "raw" mode we simple use the values from the list
150 // view.
151 if (d->mUi->stackedWidget->currentWidget() != d->mUi->rawPage) {
152 if (d->mUi->retrieveFullMessages->isChecked() &&
153 !localParts.contains(QLatin1String("RFC822"))) {
154 localParts.append(QLatin1String("RFC822"));
155 } else if (!d->mUi->retrieveFullMessages->isChecked() &&
156 localParts.contains(QLatin1String("RFC822"))) {
157 localParts.removeAll(QLatin1String("RFC822"));
158 }
159 }
160
161 policy.setLocalParts(localParts);
162 collection.setCachePolicy(policy);
163}
164
165#include "moc_cachepolicypage.cpp"
166