~ubuntu-branches/ubuntu/oneiric/kdepim/oneiric-updates

« back to all changes in this revision

Viewing changes to kalarm/resourceselector.cpp

  • Committer: Package Import Robot
  • Author(s): Philip Muškovac
  • Date: 2011-06-28 19:33:24 UTC
  • mfrom: (0.2.13) (0.1.13 sid)
  • Revision ID: package-import@ubuntu.com-20110628193324-8yvjs8sdv9rdoo6c
Tags: 4:4.7.0-0ubuntu1
* New upstream release
  - update install files
  - add missing kdepim-doc package to control file
  - Fix Vcs lines
  - kontact breaks/replaces korganizer << 4:4.6.80
  - tighten the dependency of kdepim-dev on libkdepim4 to fix lintian error

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 *  resourceselector.cpp  -  calendar resource selection widget
3
3
 *  Program:  kalarm
4
 
 *  Copyright © 2006-2010 by David Jarvie <djarvie@kde.org>
 
4
 *  Copyright © 2006-2011 by David Jarvie <djarvie@kde.org>
5
5
 *  Based on KOrganizer's ResourceView class and KAddressBook's ResourceSelection class,
6
6
 *  Copyright (C) 2003,2004 Cornelius Schumacher <schumacher@kde.org>
7
7
 *  Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
22
22
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23
23
 */
24
24
 
 
25
#include "resourceselector.moc"
 
26
 
25
27
#include "kalarm.h"
26
 
#include "resourceselector.moc"
27
 
 
28
28
#include "alarmcalendar.h"
 
29
#include "autoqpointer.h"
 
30
#ifndef USE_AKONADI
29
31
#include "alarmresources.h"
30
 
#include "autoqpointer.h"
31
32
#include "eventlistmodel.h"
 
33
#include "resourcemodelview.h"
 
34
#endif
32
35
#include "packedlayout.h"
33
36
#include "preferences.h"
34
37
#include "resourceconfigdialog.h"
35
 
#include "resourcemodelview.h"
36
38
 
 
39
#ifdef USE_AKONADI
 
40
#include <akonadi/agentmanager.h>
 
41
#include <akonadi/agentinstancecreatejob.h>
 
42
#include <akonadi/agenttype.h>
 
43
#include <akonadi/collectionpropertiesdialog.h>
 
44
#include <akonadi/entitydisplayattribute.h>
 
45
#else
37
46
#include <kcal/resourcecalendar.h>
 
47
#endif
38
48
 
39
49
#include <kdialog.h>
40
50
#include <klocale.h>
59
69
#include <QResizeEvent>
60
70
#include <QApplication>
61
71
 
 
72
#ifdef USE_AKONADI
 
73
using namespace KCalCore;
 
74
#else
62
75
using namespace KCal;
63
 
 
64
 
 
 
76
#endif
 
77
#ifdef USE_AKONADI
 
78
using namespace Akonadi;
 
79
#endif
 
80
 
 
81
 
 
82
#ifdef USE_AKONADI
 
83
ResourceSelector::ResourceSelector(QWidget* parent)
 
84
#else
65
85
ResourceSelector::ResourceSelector(AlarmResources* calendar, QWidget* parent)
66
 
        : QFrame(parent),
67
 
          mCalendar(calendar),
68
 
          mContextMenu(0)
 
86
#endif
 
87
    : QFrame(parent),
 
88
#ifndef USE_AKONADI
 
89
      mCalendar(calendar),
 
90
#endif
 
91
      mContextMenu(0)
69
92
{
70
 
        QBoxLayout* topLayout = new QVBoxLayout(this);
71
 
        topLayout->setMargin(KDialog::spacingHint());   // use spacingHint for the margin
72
 
 
73
 
        QLabel* label = new QLabel(i18nc("@title:group", "Calendars"), this);
74
 
        topLayout->addWidget(label, 0, Qt::AlignHCenter);
75
 
 
76
 
        mAlarmType = new KComboBox(this);
77
 
        mAlarmType->addItem(i18nc("@item:inlistbox", "Active Alarms"));
78
 
        mAlarmType->addItem(i18nc("@item:inlistbox", "Archived Alarms"));
79
 
        mAlarmType->addItem(i18nc("@item:inlistbox", "Alarm Templates"));
80
 
        mAlarmType->setFixedHeight(mAlarmType->sizeHint().height());
81
 
        mAlarmType->setWhatsThis(i18nc("@info:whatsthis", "Choose which type of data to show alarm calendars for"));
82
 
        topLayout->addWidget(mAlarmType);
83
 
        // No spacing between combo box and listview.
84
 
 
85
 
        ResourceModel* model = ResourceModel::instance();
86
 
        ResourceFilterModel* filterModel = new ResourceFilterModel(model, this);
87
 
        mListView = new ResourceView(this);
88
 
        mListView->setModel(filterModel);
89
 
        connect(mListView->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&,const QItemSelection&)), SLOT(selectionChanged()));
90
 
        mListView->setContextMenuPolicy(Qt::CustomContextMenu);
91
 
        connect(mListView, SIGNAL(customContextMenuRequested(const QPoint&)), SLOT(contextMenuRequested(const QPoint&)));
92
 
        mListView->setWhatsThis(i18nc("@info:whatsthis",
93
 
                                      "List of available calendars of the selected type. The checked state shows whether a calendar "
94
 
                                     "is enabled (checked) or disabled (unchecked). The default calendar is shown in bold."));
95
 
        topLayout->addWidget(mListView, 1);
96
 
        topLayout->addSpacing(KDialog::spacingHint());
97
 
 
98
 
        PackedLayout* blayout = new PackedLayout(Qt::AlignHCenter);
99
 
        blayout->setMargin(0);
100
 
        blayout->setSpacing(KDialog::spacingHint());
101
 
        topLayout->addLayout(blayout);
102
 
 
103
 
        mAddButton    = new QPushButton(i18nc("@action:button", "Add..."), this);
104
 
        mEditButton   = new QPushButton(i18nc("@action:button", "Edit..."), this);
105
 
        mDeleteButton = new QPushButton(i18nc("@action:button", "Remove"), this);
106
 
        blayout->addWidget(mAddButton);
107
 
        blayout->addWidget(mEditButton);
108
 
        blayout->addWidget(mDeleteButton);
109
 
        mEditButton->setWhatsThis(i18nc("@info:whatsthis", "Edit the highlighted calendar"));
110
 
        mDeleteButton->setWhatsThis(i18nc("@info:whatsthis", "<para>Remove the highlighted calendar from the list.</para>"
111
 
                                         "<para>The calendar itself is left intact, and may subsequently be reinstated in the list if desired.</para>"));
112
 
        mEditButton->setDisabled(true);
113
 
        mDeleteButton->setDisabled(true);
114
 
        connect(mAddButton, SIGNAL(clicked()), SLOT(addResource()));
115
 
        connect(mEditButton, SIGNAL(clicked()), SLOT(editResource()));
116
 
        connect(mDeleteButton, SIGNAL(clicked()), SLOT(removeResource()));
117
 
 
118
 
        connect(mCalendar, SIGNAL(resourceStatusChanged(AlarmResource*, AlarmResources::Change)), SLOT(slotStatusChanged(AlarmResource*, AlarmResources::Change)));
119
 
 
120
 
        connect(mAlarmType, SIGNAL(activated(int)), SLOT(alarmTypeSelected()));
121
 
        QTimer::singleShot(0, this, SLOT(alarmTypeSelected()));
122
 
 
123
 
        Preferences::connect(SIGNAL(archivedKeepDaysChanged(int)), this, SLOT(archiveDaysChanged(int)));
 
93
    QBoxLayout* topLayout = new QVBoxLayout(this);
 
94
    topLayout->setMargin(KDialog::spacingHint());   // use spacingHint for the margin
 
95
 
 
96
    QLabel* label = new QLabel(i18nc("@title:group", "Calendars"), this);
 
97
    topLayout->addWidget(label, 0, Qt::AlignHCenter);
 
98
 
 
99
    mAlarmType = new KComboBox(this);
 
100
    mAlarmType->addItem(i18nc("@item:inlistbox", "Active Alarms"));
 
101
    mAlarmType->addItem(i18nc("@item:inlistbox", "Archived Alarms"));
 
102
    mAlarmType->addItem(i18nc("@item:inlistbox", "Alarm Templates"));
 
103
    mAlarmType->setFixedHeight(mAlarmType->sizeHint().height());
 
104
    mAlarmType->setWhatsThis(i18nc("@info:whatsthis", "Choose which type of data to show alarm calendars for"));
 
105
    topLayout->addWidget(mAlarmType);
 
106
    // No spacing between combo box and listview.
 
107
 
 
108
#ifdef USE_AKONADI
 
109
    CollectionFilterCheckListModel* model = new CollectionFilterCheckListModel(this);
 
110
    mListView = new CollectionView(model, this);
 
111
#else
 
112
    ResourceModel* model = ResourceModel::instance();
 
113
    ResourceFilterModel* filterModel = new ResourceFilterModel(model, this);
 
114
    mListView = new ResourceView(this);
 
115
    mListView->setModel(filterModel);
 
116
#endif
 
117
    connect(mListView->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&,const QItemSelection&)), SLOT(selectionChanged()));
 
118
    mListView->setContextMenuPolicy(Qt::CustomContextMenu);
 
119
    connect(mListView, SIGNAL(customContextMenuRequested(const QPoint&)), SLOT(contextMenuRequested(const QPoint&)));
 
120
    mListView->setWhatsThis(i18nc("@info:whatsthis",
 
121
                                  "List of available calendars of the selected type. The checked state shows whether a calendar "
 
122
                                 "is enabled (checked) or disabled (unchecked). The default calendar is shown in bold."));
 
123
    topLayout->addWidget(mListView, 1);
 
124
    topLayout->addSpacing(KDialog::spacingHint());
 
125
 
 
126
    PackedLayout* blayout = new PackedLayout(Qt::AlignHCenter);
 
127
    blayout->setMargin(0);
 
128
    blayout->setSpacing(KDialog::spacingHint());
 
129
    topLayout->addLayout(blayout);
 
130
 
 
131
    mAddButton    = new QPushButton(i18nc("@action:button", "Add..."), this);
 
132
    mEditButton   = new QPushButton(i18nc("@action:button", "Edit..."), this);
 
133
    mDeleteButton = new QPushButton(i18nc("@action:button", "Remove"), this);
 
134
    blayout->addWidget(mAddButton);
 
135
    blayout->addWidget(mEditButton);
 
136
    blayout->addWidget(mDeleteButton);
 
137
    mEditButton->setWhatsThis(i18nc("@info:whatsthis", "Edit the highlighted calendar"));
 
138
    mDeleteButton->setWhatsThis(i18nc("@info:whatsthis", "<para>Remove the highlighted calendar from the list.</para>"
 
139
                                     "<para>The calendar itself is left intact, and may subsequently be reinstated in the list if desired.</para>"));
 
140
    mEditButton->setDisabled(true);
 
141
    mDeleteButton->setDisabled(true);
 
142
    connect(mAddButton, SIGNAL(clicked()), SLOT(addResource()));
 
143
    connect(mEditButton, SIGNAL(clicked()), SLOT(editResource()));
 
144
    connect(mDeleteButton, SIGNAL(clicked()), SLOT(removeResource()));
 
145
 
 
146
#ifdef USE_AKONADI
 
147
    connect(AkonadiModel::instance(), SIGNAL(collectionStatusChanged(const Akonadi::Collection&, AkonadiModel::Change, const QVariant&)),
 
148
                                      SLOT(slotStatusChanged(const Akonadi::Collection&, AkonadiModel::Change, const QVariant&)));
 
149
    connect(AkonadiModel::instance(), SIGNAL(collectionAdded(Akonadi::AgentInstanceCreateJob*, bool)),
 
150
                                      SLOT(resourceAdded(Akonadi::AgentInstanceCreateJob*, bool)));
 
151
    connect(AkonadiModel::instance(), SIGNAL(collectionAdded(const Akonadi::Collection&)),
 
152
                                      SLOT(slotCollectionAdded(const Akonadi::Collection&)));
 
153
#else
 
154
    connect(mCalendar, SIGNAL(resourceStatusChanged(AlarmResource*, AlarmResources::Change)), SLOT(slotStatusChanged(AlarmResource*, AlarmResources::Change)));
 
155
#endif
 
156
 
 
157
    connect(mAlarmType, SIGNAL(activated(int)), SLOT(alarmTypeSelected()));
 
158
    QTimer::singleShot(0, this, SLOT(alarmTypeSelected()));
 
159
 
 
160
    Preferences::connect(SIGNAL(archivedKeepDaysChanged(int)), this, SLOT(archiveDaysChanged(int)));
124
161
}
125
162
 
126
163
/******************************************************************************
129
166
* add appropriate whatsThis texts to the list and to the Add button.
130
167
*/
131
168
void ResourceSelector::alarmTypeSelected()
132
 
133
 
        QString addTip;
134
 
        switch (mAlarmType->currentIndex())
135
 
        {
136
 
                case 0:
137
 
                        mCurrentAlarmType = AlarmResource::ACTIVE;
138
 
                        addTip = i18nc("@info:tooltip", "Add a new active alarm calendar");
139
 
                        break;
140
 
                case 1:
141
 
                        mCurrentAlarmType = AlarmResource::ARCHIVED;
142
 
                        addTip = i18nc("@info:tooltip", "Add a new archived alarm calendar");
143
 
                        break;
144
 
                case 2:
145
 
                        mCurrentAlarmType = AlarmResource::TEMPLATE;
146
 
                        addTip = i18nc("@info:tooltip", "Add a new alarm template calendar");
147
 
                        break;
148
 
        }
149
 
        // WORKAROUND: Switch scroll bars off to avoid crash (see explanation
150
 
        // in reinstateAlarmTypeScrollBars() description).
151
 
        mListView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
152
 
        mListView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
153
 
        static_cast<ResourceFilterModel*>(mListView->model())->setFilter(mCurrentAlarmType);
154
 
        mAddButton->setWhatsThis(addTip);
155
 
        mAddButton->setToolTip(addTip);
156
 
        // WORKAROUND: Switch scroll bars back on after allowing geometry to update ...
157
 
        QTimer::singleShot(0, this, SLOT(reinstateAlarmTypeScrollBars()));
 
169
{
 
170
    QString addTip;
 
171
    switch (mAlarmType->currentIndex())
 
172
    {
 
173
        case 0:
 
174
            mCurrentAlarmType = KAlarm::CalEvent::ACTIVE;
 
175
            addTip = i18nc("@info:tooltip", "Add a new active alarm calendar");
 
176
            break;
 
177
        case 1:
 
178
            mCurrentAlarmType = KAlarm::CalEvent::ARCHIVED;
 
179
            addTip = i18nc("@info:tooltip", "Add a new archived alarm calendar");
 
180
            break;
 
181
        case 2:
 
182
            mCurrentAlarmType = KAlarm::CalEvent::TEMPLATE;
 
183
            addTip = i18nc("@info:tooltip", "Add a new alarm template calendar");
 
184
            break;
 
185
    }
 
186
    // WORKAROUND: Switch scroll bars off to avoid crash (see explanation
 
187
    // in reinstateAlarmTypeScrollBars() description).
 
188
    mListView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
 
189
    mListView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
 
190
#ifdef USE_AKONADI
 
191
    mListView->collectionModel()->setEventTypeFilter(mCurrentAlarmType);
 
192
#else
 
193
    static_cast<ResourceFilterModel*>(mListView->model())->setFilter(mCurrentAlarmType);
 
194
#endif
 
195
    mAddButton->setWhatsThis(addTip);
 
196
    mAddButton->setToolTip(addTip);
 
197
    // WORKAROUND: Switch scroll bars back on after allowing geometry to update ...
 
198
    QTimer::singleShot(0, this, SLOT(reinstateAlarmTypeScrollBars()));
158
199
}
159
200
 
160
201
/******************************************************************************
172
213
*/
173
214
void ResourceSelector::reinstateAlarmTypeScrollBars()
174
215
{
175
 
        mListView->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
176
 
        mListView->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
 
216
    mListView->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
 
217
    mListView->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
177
218
}
178
219
 
179
220
/******************************************************************************
181
222
*/
182
223
void ResourceSelector::addResource()
183
224
{
184
 
        AlarmResourceManager* manager = mCalendar->resourceManager();
185
 
        QStringList descs = manager->resourceTypeDescriptions();
186
 
        bool ok = false;
187
 
        QString desc = KInputDialog::getItem(i18nc("@title:window", "Calendar Configuration"),
188
 
                                             i18nc("@info", "Select storage type of new calendar:"), descs, 0, false, &ok, this);
189
 
        if (!ok  ||  descs.isEmpty())
190
 
                return;
191
 
        QString type = manager->resourceTypeNames()[descs.indexOf(desc)];
192
 
        AlarmResource* resource = dynamic_cast<AlarmResource*>(manager->createResource(type));
193
 
        if (!resource)
194
 
        {
195
 
                KMessageBox::error(this, i18nc("@info", "Unable to create calendar of type <resource>%1</resource>.", type));
196
 
                return;
197
 
        }
198
 
        resource->setResourceName(i18nc("@info/plain", "%1 calendar", type));
199
 
        resource->setAlarmType(mCurrentAlarmType);
200
 
        resource->setActive(false);   // prevent setReadOnly() declaring it as unwritable before we've tried to load it
201
 
 
202
 
        // Use AutoQPointer to guard against crash on application exit while
203
 
        // the dialogue is still open. It prevents double deletion (both on
204
 
        // deletion of ResourceSelector, and on return from this function).
205
 
        AutoQPointer<ResourceConfigDialog> dlg = new ResourceConfigDialog(this, resource);
206
 
        if (dlg->exec() == QDialog::Accepted)
207
 
        {
208
 
                resource->setEnabled(true);
209
 
                resource->setTimeSpec(Preferences::timeZone());
210
 
                manager->add(resource);
211
 
                manager->writeConfig();
212
 
                mCalendar->resourceAdded(resource);   // load the resource and connect in-process change signals
213
 
        }
214
 
        else
215
 
        {
216
 
                delete resource;
217
 
                resource = 0;
218
 
        }
219
 
}
 
225
#ifdef USE_AKONADI
 
226
    AgentInstanceCreateJob* job = AkonadiModel::instance()->addCollection(mCurrentAlarmType, this);
 
227
    if (job)
 
228
        mAddJobs += job;
 
229
#else
 
230
    AlarmResourceManager* manager = mCalendar->resourceManager();
 
231
    QStringList descs = manager->resourceTypeDescriptions();
 
232
    bool ok = false;
 
233
    QString desc = KInputDialog::getItem(i18nc("@title:window", "Calendar Configuration"),
 
234
                                         i18nc("@info", "Select storage type of new calendar:"), descs, 0, false, &ok, this);
 
235
    if (!ok  ||  descs.isEmpty())
 
236
        return;
 
237
    QString type = manager->resourceTypeNames()[descs.indexOf(desc)];
 
238
    AlarmResource* resource = dynamic_cast<AlarmResource*>(manager->createResource(type));
 
239
    if (!resource)
 
240
    {
 
241
        KMessageBox::error(this, i18nc("@info", "Unable to create calendar of type <resource>%1</resource>.", type));
 
242
        return;
 
243
    }
 
244
    resource->setResourceName(i18nc("@info/plain", "%1 calendar", type));
 
245
    resource->setAlarmType(mCurrentAlarmType);
 
246
    resource->setActive(false);   // prevent setReadOnly() declaring it as unwritable before we've tried to load it
 
247
 
 
248
    // Use AutoQPointer to guard against crash on application exit while
 
249
    // the dialogue is still open. It prevents double deletion (both on
 
250
    // deletion of ResourceSelector, and on return from this function).
 
251
    AutoQPointer<ResourceConfigDialog> dlg = new ResourceConfigDialog(this, resource);
 
252
    if (dlg->exec() == QDialog::Accepted)
 
253
    {
 
254
        resource->setEnabled(true);
 
255
        resource->setTimeSpec(Preferences::timeZone());
 
256
        manager->add(resource);
 
257
        manager->writeConfig();
 
258
        mCalendar->resourceAdded(resource);   // load the resource and connect in-process change signals
 
259
    }
 
260
    else
 
261
    {
 
262
        delete resource;
 
263
        resource = 0;
 
264
    }
 
265
#endif
 
266
}
 
267
 
 
268
#ifdef USE_AKONADI
 
269
/******************************************************************************
 
270
* Called when the job started by AkonadiModel::addCollection() has completed.
 
271
*/
 
272
void ResourceSelector::resourceAdded(AgentInstanceCreateJob* job, bool success)
 
273
{
 
274
    int i = mAddJobs.indexOf(job);
 
275
    if (i >= 0)
 
276
    {
 
277
        // The agent has been created by addResource().
 
278
        if (success)
 
279
        {
 
280
            AgentInstance agent = job->instance();
 
281
            if (agent.isValid())
 
282
            {
 
283
                // Note that we're expecting the agent's Collection to be added
 
284
                mAddAgents += agent;
 
285
            }
 
286
        }
 
287
        mAddJobs.removeAt(i);
 
288
    }
 
289
}
 
290
 
 
291
/******************************************************************************
 
292
* Called when a collection is added to the AkonadiModel.
 
293
*/
 
294
void ResourceSelector::slotCollectionAdded(const Collection& collection)
 
295
{
 
296
    if (collection.isValid())
 
297
    {
 
298
        AgentInstance agent = AgentManager::self()->instance(collection.resource());
 
299
        if (agent.isValid())
 
300
        {
 
301
            int i = mAddAgents.indexOf(agent);
 
302
            if (i >= 0)
 
303
            {
 
304
                // The collection belongs to an agent created by addResource()
 
305
                KAlarm::CalEvent::Types types = KAlarm::CalEvent::types(collection.contentMimeTypes());
 
306
                CollectionControlModel::setEnabled(collection, types, true);
 
307
                if (!(types & mCurrentAlarmType))
 
308
                {
 
309
                    // The user has selected alarm types for the resource
 
310
                    // which don't include the currently displayed type.
 
311
                    // Show a collection list which includes a selected type.
 
312
                    int index = -1;
 
313
                    if (types & KAlarm::CalEvent::ACTIVE)
 
314
                        index = 0;
 
315
                    else if (types & KAlarm::CalEvent::ARCHIVED)
 
316
                        index = 1;
 
317
                    else if (types & KAlarm::CalEvent::TEMPLATE)
 
318
                        index = 2;
 
319
                    if (index >= 0)
 
320
                    {
 
321
                        mAlarmType->setCurrentIndex(index);
 
322
                        alarmTypeSelected();
 
323
                    }
 
324
                }
 
325
                mAddAgents.removeAt(i);
 
326
            }
 
327
        }
 
328
    }
 
329
}
 
330
#endif
220
331
 
221
332
/******************************************************************************
222
333
* Edit the currently selected resource.
223
334
*/
224
335
void ResourceSelector::editResource()
225
336
{
226
 
        AlarmResource* resource = currentResource();
227
 
        if (!resource)
228
 
                return;
229
 
        bool readOnly = resource->readOnly();
230
 
        // Use AutoQPointer to guard against crash on application exit while
231
 
        // the dialogue is still open. It prevents double deletion (both on
232
 
        // deletion of ResourceSelector, and on return from this function).
233
 
        AutoQPointer<ResourceConfigDialog> dlg = new ResourceConfigDialog(this, resource);
234
 
        if (dlg->exec() == QDialog::Accepted)
235
 
        {
236
 
                // Act on any changed settings.
237
 
                // Read-only is handled automatically by AlarmResource::setReadOnly().
238
 
                if (!readOnly  &&  resource->readOnly()  &&  resource->standardResource())
239
 
                {
240
 
                        // A standard resource is being made read-only.
241
 
                        if (resource->alarmType() == AlarmResource::ACTIVE)
242
 
                        {
243
 
                                KMessageBox::sorry(this, i18nc("@info", "You cannot make your default active alarm calendar read-only."));
244
 
                                resource->setReadOnly(false);
245
 
                        }
246
 
                        else if (resource->alarmType() == AlarmResource::ARCHIVED  &&  Preferences::archivedKeepDays())
247
 
                        {
248
 
                                // Only allow the archived alarms standard resource to be made read-only
249
 
                                // if we're not saving archived alarms.
250
 
                                KMessageBox::sorry(this, i18nc("@info", "You cannot make your default archived alarm calendar "
251
 
                                                              "read-only while expired alarms are configured to be kept."));
252
 
                                resource->setReadOnly(false);
253
 
                        }
254
 
                        else if (KMessageBox::warningContinueCancel(this, i18nc("@info", "Do you really want to make your default calendar read-only?"))
255
 
                                   == KMessageBox::Cancel)
256
 
                        {
257
 
                                resource->setReadOnly(false);
258
 
                        }
259
 
                }
260
 
        }
 
337
#ifdef USE_AKONADI
 
338
    Collection collection = currentResource();
 
339
    if (collection.isValid())
 
340
    {
 
341
        AgentInstance instance = AgentManager::self()->instance(collection.resource());
 
342
        if (instance.isValid())
 
343
            instance.configure(this);
 
344
    }
 
345
#else
 
346
    AlarmResource* resource = currentResource();
 
347
    if (!resource)
 
348
        return;
 
349
    bool readOnly = resource->readOnly();
 
350
    // Use AutoQPointer to guard against crash on application exit while
 
351
    // the dialogue is still open. It prevents double deletion (both on
 
352
    // deletion of ResourceSelector, and on return from this function).
 
353
    AutoQPointer<ResourceConfigDialog> dlg = new ResourceConfigDialog(this, resource);
 
354
    if (dlg->exec() == QDialog::Accepted)
 
355
    {
 
356
        // Act on any changed settings.
 
357
        // Read-only is handled automatically by AlarmResource::setReadOnly().
 
358
        if (!readOnly  &&  resource->readOnly()  &&  resource->standardResource())
 
359
        {
 
360
            // A standard resource is being made read-only.
 
361
            if (resource->alarmType() == KAlarm::CalEvent::ACTIVE)
 
362
            {
 
363
                KMessageBox::sorry(this, i18nc("@info", "You cannot make your default active alarm calendar read-only."));
 
364
                resource->setReadOnly(false);
 
365
            }
 
366
            else if (resource->alarmType() == KAlarm::CalEvent::ARCHIVED  &&  Preferences::archivedKeepDays())
 
367
            {
 
368
                // Only allow the archived alarms standard resource to be made read-only
 
369
                // if we're not saving archived alarms.
 
370
                KMessageBox::sorry(this, i18nc("@info", "You cannot make your default archived alarm calendar "
 
371
                                              "read-only while expired alarms are configured to be kept."));
 
372
                resource->setReadOnly(false);
 
373
            }
 
374
            else if (KMessageBox::warningContinueCancel(this, i18nc("@info", "Do you really want to make your default calendar read-only?"))
 
375
                       == KMessageBox::Cancel)
 
376
            {
 
377
                resource->setReadOnly(false);
 
378
            }
 
379
        }
 
380
    }
 
381
#endif
261
382
}
262
383
 
263
384
/******************************************************************************
265
386
*/
266
387
void ResourceSelector::removeResource()
267
388
{
268
 
        AlarmResource* resource = currentResource();
269
 
        if (!resource)
270
 
                return;
271
 
        bool std = resource->standardResource();
272
 
        if (std)
273
 
        {
274
 
                // It's the standard resource for its type.
275
 
                if (resource->alarmType() == AlarmResource::ACTIVE)
276
 
                {
277
 
                        KMessageBox::sorry(this, i18nc("@info", "You cannot remove your default active alarm calendar."));
278
 
                        return;
279
 
                }
280
 
                if (resource->alarmType() == AlarmResource::ARCHIVED  &&  Preferences::archivedKeepDays())
281
 
                {
282
 
                        // Only allow the archived alarms standard resource to be removed if
283
 
                        // we're not saving archived alarms.
284
 
                        KMessageBox::sorry(this, i18nc("@info", "You cannot remove your default archived alarm calendar "
285
 
                                                      "while expired alarms are configured to be kept."));
286
 
                        return;
287
 
                }
288
 
        }
289
 
        QString text = std ? i18nc("@info", "Do you really want to remove your default calendar (<resource>%1</resource>) from the list?", resource->resourceName())
290
 
                           : i18nc("@info", "Do you really want to remove the calendar <resource>%1</resource> from the list?", resource->resourceName());
291
 
        if (KMessageBox::warningContinueCancel(this, text, "", KStandardGuiItem::remove()) == KMessageBox::Cancel)
292
 
                return;
 
389
#ifdef USE_AKONADI
 
390
    Collection collection = currentResource();
 
391
    if (!collection.isValid())
 
392
        return;
 
393
    QString name = collection.name();
 
394
    // Check if it's the standard or only resource for at least one type.
 
395
    KAlarm::CalEvent::Types standardTypes = CollectionControlModel::standardTypes(collection, true);
 
396
    bool std = standardTypes & KAlarm::CalEvent::ALL;
 
397
    KAlarm::CalEvent::Type stdType = (standardTypes & KAlarm::CalEvent::ACTIVE)   ? KAlarm::CalEvent::ACTIVE
 
398
                                   : (standardTypes & KAlarm::CalEvent::ARCHIVED) ? KAlarm::CalEvent::ARCHIVED
 
399
                                   : KAlarm::CalEvent::EMPTY;
 
400
#else
 
401
    AlarmResource* resource = currentResource();
 
402
    if (!resource)
 
403
        return;
 
404
    QString name = resource->resourceName();
 
405
    bool std = resource->standardResource();
 
406
    // Check if it's the standard resource for its type.
 
407
    KAlarm::CalEvent::Type stdType = std ? resource->alarmType() : KAlarm::CalEvent::EMPTY;
 
408
#endif
 
409
    if (stdType == KAlarm::CalEvent::ACTIVE)
 
410
    {
 
411
        KMessageBox::sorry(this, i18nc("@info", "You cannot remove your default active alarm calendar."));
 
412
        return;
 
413
    }
 
414
    if (stdType == KAlarm::CalEvent::ARCHIVED  &&  Preferences::archivedKeepDays())
 
415
    {
 
416
        // Only allow the archived alarms standard resource to be removed if
 
417
        // we're not saving archived alarms.
 
418
        KMessageBox::sorry(this, i18nc("@info", "You cannot remove your default archived alarm calendar "
 
419
                                      "while expired alarms are configured to be kept."));
 
420
        return;
 
421
    }
 
422
#ifdef __GNUC__
 
423
#warning Akonadi: warn if calendar also contains other alarm types than the current list type
 
424
#warning Akonadi: Prompt should show default calendar alarm type(s) if other than current list type
 
425
#endif
 
426
    QString text = std ? i18nc("@info", "Do you really want to remove your default calendar (<resource>%1</resource>) from the list?", name)
 
427
                       : i18nc("@info", "Do you really want to remove the calendar <resource>%1</resource> from the list?", name);
 
428
    if (KMessageBox::warningContinueCancel(this, text, "", KStandardGuiItem::remove()) == KMessageBox::Cancel)
 
429
        return;
293
430
 
294
 
        // Remove resource from alarm and resource lists before deleting it, to avoid
295
 
        // crashes when display updates occur immediately after it is deleted.
296
 
        if (resource->alarmType() == AlarmResource::TEMPLATE)
297
 
                EventListModel::templates()->removeResource(resource);
298
 
        else
299
 
                EventListModel::alarms()->removeResource(resource);
300
 
        ResourceModel::instance()->removeResource(resource);
301
 
        AlarmResourceManager* manager = mCalendar->resourceManager();
302
 
        manager->remove(resource);
303
 
        manager->writeConfig();
 
431
#ifdef USE_AKONADI
 
432
    AkonadiModel::instance()->removeCollection(collection);
 
433
#else
 
434
    // Remove resource from alarm and resource lists before deleting it, to avoid
 
435
    // crashes when display updates occur immediately after it is deleted.
 
436
    if (resource->alarmType() == KAlarm::CalEvent::TEMPLATE)
 
437
        EventListModel::templates()->removeResource(resource);
 
438
    else
 
439
        EventListModel::alarms()->removeResource(resource);
 
440
    ResourceModel::instance()->removeResource(resource);
 
441
    AlarmResourceManager* manager = mCalendar->resourceManager();
 
442
    manager->remove(resource);
 
443
    manager->writeConfig();
 
444
#endif
304
445
}
305
446
 
306
447
/******************************************************************************
309
450
*/
310
451
void ResourceSelector::selectionChanged()
311
452
{
312
 
        bool state = mListView->selectionModel()->selectedRows().count();
313
 
        mDeleteButton->setEnabled(state);
314
 
        mEditButton->setEnabled(state);
 
453
    bool state = mListView->selectionModel()->selectedRows().count();
 
454
    mDeleteButton->setEnabled(state);
 
455
    mEditButton->setEnabled(state);
315
456
}
316
457
 
317
458
/******************************************************************************
319
460
*/
320
461
void ResourceSelector::initActions(KActionCollection* actions)
321
462
{
322
 
        mActionReload      = new KAction(KIcon("view-refresh"), i18nc("@action Reload calendar", "Re&load"), this);
323
 
        actions->addAction(QLatin1String("resReload"), mActionReload);
324
 
        connect(mActionReload, SIGNAL(triggered(bool)), SLOT(reloadResource()));
325
 
        mActionSave        = new KAction(KIcon("document-save"), i18nc("@action", "&Save"), this);
326
 
        actions->addAction(QLatin1String("resSave"), mActionSave);
327
 
        connect(mActionSave, SIGNAL(triggered(bool)), SLOT(saveResource()));
328
 
        mActionShowDetails = new KAction(KIcon("help-about"), i18nc("@action", "Show &Details"), this);
329
 
        actions->addAction(QLatin1String("resDetails"), mActionShowDetails);
330
 
        connect(mActionShowDetails, SIGNAL(triggered(bool)), SLOT(showInfo()));
331
 
        mActionSetColour   = new KAction(KIcon("color-picker"), i18nc("@action", "Set &Color..."), this);
332
 
        actions->addAction(QLatin1String("resSetColour"), mActionSetColour);
333
 
        connect(mActionSetColour, SIGNAL(triggered(bool)), SLOT(setColour()));
334
 
        mActionClearColour   = new KAction(i18nc("@action", "Clear C&olor"), this);
335
 
        actions->addAction(QLatin1String("resClearColour"), mActionClearColour);
336
 
        connect(mActionClearColour, SIGNAL(triggered(bool)), SLOT(clearColour()));
337
 
        mActionEdit        = new KAction(KIcon("document-properties"), i18nc("@action", "&Edit..."), this);
338
 
        actions->addAction(QLatin1String("resEdit"), mActionEdit);
339
 
        connect(mActionEdit, SIGNAL(triggered(bool)), SLOT(editResource()));
340
 
        mActionRemove      = new KAction(KIcon("edit-delete"), i18nc("@action", "&Remove"), this);
341
 
        actions->addAction(QLatin1String("resRemove"), mActionRemove);
342
 
        connect(mActionRemove, SIGNAL(triggered(bool)), SLOT(removeResource()));
343
 
        mActionSetDefault  = new KToggleAction(this);
344
 
        actions->addAction(QLatin1String("resDefault"), mActionSetDefault);
345
 
        connect(mActionSetDefault, SIGNAL(triggered(bool)), SLOT(setStandard()));
346
 
        QAction* action    = new KAction(KIcon("document-new"), i18nc("@action", "&Add..."), this);
347
 
        actions->addAction(QLatin1String("resAdd"), action);
348
 
        connect(action, SIGNAL(triggered(bool)), SLOT(addResource()));
349
 
        mActionImport      = new KAction(i18nc("@action", "Im&port..."), this);
350
 
        actions->addAction(QLatin1String("resImport"), mActionImport);
351
 
        connect(mActionImport, SIGNAL(triggered(bool)), SLOT(importCalendar()));
352
 
        mActionExport      = new KAction(i18nc("@action", "E&xport..."), this);
353
 
        actions->addAction(QLatin1String("resExport"), mActionExport);
354
 
        connect(mActionExport, SIGNAL(triggered(bool)), SLOT(exportCalendar()));
 
463
    mActionReload      = new KAction(KIcon("view-refresh"), i18nc("@action Reload calendar", "Re&load"), this);
 
464
    actions->addAction(QLatin1String("resReload"), mActionReload);
 
465
    connect(mActionReload, SIGNAL(triggered(bool)), SLOT(reloadResource()));
 
466
    mActionSave        = new KAction(KIcon("document-save"), i18nc("@action", "&Save"), this);
 
467
    actions->addAction(QLatin1String("resSave"), mActionSave);
 
468
    connect(mActionSave, SIGNAL(triggered(bool)), SLOT(saveResource()));
 
469
    mActionShowDetails = new KAction(KIcon("help-about"), i18nc("@action", "Show &Details"), this);
 
470
    actions->addAction(QLatin1String("resDetails"), mActionShowDetails);
 
471
    connect(mActionShowDetails, SIGNAL(triggered(bool)), SLOT(showInfo()));
 
472
    mActionSetColour   = new KAction(KIcon("color-picker"), i18nc("@action", "Set &Color..."), this);
 
473
    actions->addAction(QLatin1String("resSetColour"), mActionSetColour);
 
474
    connect(mActionSetColour, SIGNAL(triggered(bool)), SLOT(setColour()));
 
475
    mActionClearColour   = new KAction(i18nc("@action", "Clear C&olor"), this);
 
476
    actions->addAction(QLatin1String("resClearColour"), mActionClearColour);
 
477
    connect(mActionClearColour, SIGNAL(triggered(bool)), SLOT(clearColour()));
 
478
    mActionEdit        = new KAction(KIcon("document-properties"), i18nc("@action", "&Edit..."), this);
 
479
    actions->addAction(QLatin1String("resEdit"), mActionEdit);
 
480
    connect(mActionEdit, SIGNAL(triggered(bool)), SLOT(editResource()));
 
481
    mActionRemove      = new KAction(KIcon("edit-delete"), i18nc("@action", "&Remove"), this);
 
482
    actions->addAction(QLatin1String("resRemove"), mActionRemove);
 
483
    connect(mActionRemove, SIGNAL(triggered(bool)), SLOT(removeResource()));
 
484
    mActionSetDefault  = new KToggleAction(this);
 
485
    actions->addAction(QLatin1String("resDefault"), mActionSetDefault);
 
486
    connect(mActionSetDefault, SIGNAL(triggered(bool)), SLOT(setStandard()));
 
487
    QAction* action    = new KAction(KIcon("document-new"), i18nc("@action", "&Add..."), this);
 
488
    actions->addAction(QLatin1String("resAdd"), action);
 
489
    connect(action, SIGNAL(triggered(bool)), SLOT(addResource()));
 
490
    mActionImport      = new KAction(i18nc("@action", "Im&port..."), this);
 
491
    actions->addAction(QLatin1String("resImport"), mActionImport);
 
492
    connect(mActionImport, SIGNAL(triggered(bool)), SLOT(importCalendar()));
 
493
    mActionExport      = new KAction(i18nc("@action", "E&xport..."), this);
 
494
    actions->addAction(QLatin1String("resExport"), mActionExport);
 
495
    connect(mActionExport, SIGNAL(triggered(bool)), SLOT(exportCalendar()));
355
496
}
356
497
 
357
498
void ResourceSelector::setContextMenu(KMenu* menu)
358
499
{
359
 
        mContextMenu = menu;
 
500
    mContextMenu = menu;
360
501
}
361
502
 
362
503
/******************************************************************************
363
 
* Display the context menu for the selected resource.
 
504
* Display the context menu for the selected calendar.
364
505
*/
365
506
void ResourceSelector::contextMenuRequested(const QPoint& viewportPos)
366
507
{
367
 
        if (!mContextMenu)
368
 
                return;
369
 
        bool active   = false;
370
 
        bool writable = false;
371
 
        int type = -1;
372
 
        AlarmResource* resource = 0;
373
 
        if (mListView->selectionModel()->hasSelection())
374
 
        {
375
 
                QModelIndex index = mListView->indexAt(viewportPos);
376
 
                if (index.isValid())
377
 
                        resource = static_cast<ResourceFilterModel*>(mListView->model())->resource(index);
378
 
                else
379
 
                        mListView->clearSelection();
380
 
        }
381
 
        if (resource)
382
 
        {
383
 
                active   = resource->isEnabled();
384
 
                type     = resource->alarmType();
385
 
                writable = resource->writable();
386
 
        }
387
 
        else
388
 
        {
389
 
                switch (mAlarmType->currentIndex())
390
 
                {
391
 
                        case 0:  type = AlarmResource::ACTIVE; break;
392
 
                        case 1:  type = AlarmResource::ARCHIVED; break;
393
 
                        case 2:  type = AlarmResource::TEMPLATE; break;
394
 
                }
395
 
        }
396
 
        mActionReload->setEnabled(active);
397
 
        mActionSave->setEnabled(active && writable);
398
 
        mActionShowDetails->setEnabled(resource);
399
 
        mActionSetColour->setEnabled(resource);
400
 
        mActionClearColour->setEnabled(resource);
401
 
        mActionClearColour->setVisible(resource && resource->colour().isValid());
402
 
        mActionEdit->setEnabled(resource);
403
 
        mActionRemove->setEnabled(resource);
404
 
        mActionImport->setEnabled(active && writable);
405
 
        mActionExport->setEnabled(active);
406
 
        QString text;
407
 
        switch (type)
408
 
        {
409
 
                case AlarmResource::ACTIVE:   text = i18nc("@action", "Use as &Default for Active Alarms");  break;
410
 
                case AlarmResource::ARCHIVED: text = i18nc("@action", "Use as &Default for Archived Alarms");  break;
411
 
                case AlarmResource::TEMPLATE: text = i18nc("@action", "Use as &Default for Alarm Templates");  break;
412
 
                default:  break;
413
 
        }
414
 
        mActionSetDefault->setText(text);
415
 
        bool standard = (resource  &&  resource == mCalendar->getStandardResource(static_cast<AlarmResource::Type>(type))  &&  resource->standardResource());
416
 
        mActionSetDefault->setChecked(active && writable && standard);
417
 
        bool allowChange = (type == AlarmResource::ARCHIVED  &&  !Preferences::archivedKeepDays());
418
 
        mActionSetDefault->setEnabled(active && writable && (!standard || allowChange));
419
 
        mContextMenu->popup(mListView->viewport()->mapToGlobal(viewportPos));
 
508
    if (!mContextMenu)
 
509
        return;
 
510
    bool active   = false;
 
511
    bool writable = false;
 
512
#ifdef USE_AKONADI
 
513
    Collection collection;
 
514
#else
 
515
    AlarmResource* resource = 0;
 
516
#endif
 
517
    if (mListView->selectionModel()->hasSelection())
 
518
    {
 
519
        QModelIndex index = mListView->indexAt(viewportPos);
 
520
        if (index.isValid())
 
521
#ifdef USE_AKONADI
 
522
            collection = mListView->collectionModel()->collection(index);
 
523
#else
 
524
            resource = static_cast<ResourceFilterModel*>(mListView->model())->resource(index);
 
525
#endif
 
526
        else
 
527
            mListView->clearSelection();
 
528
    }
 
529
    KAlarm::CalEvent::Type type = currentResourceType();
 
530
#ifdef USE_AKONADI
 
531
    bool haveCalendar = collection.isValid();
 
532
#else
 
533
    bool haveCalendar = resource;
 
534
#endif
 
535
    if (haveCalendar)
 
536
    {
 
537
#ifdef USE_AKONADI
 
538
        active   = CollectionControlModel::isEnabled(collection, type);
 
539
        writable = CollectionControlModel::isWritable(collection, type);
 
540
        if (!(AkonadiModel::instance()->types(collection) & type))
 
541
            type = KAlarm::CalEvent::EMPTY;
 
542
#else
 
543
        active   = resource->isEnabled();
 
544
        type     = resource->alarmType();
 
545
        writable = resource->writable();
 
546
#endif
 
547
    }
 
548
    mActionReload->setEnabled(active);
 
549
    mActionSave->setEnabled(active && writable);
 
550
    mActionShowDetails->setEnabled(haveCalendar);
 
551
    mActionSetColour->setEnabled(haveCalendar);
 
552
    mActionClearColour->setEnabled(haveCalendar);
 
553
#ifdef USE_AKONADI
 
554
    mActionClearColour->setVisible(AkonadiModel::instance()->backgroundColor(collection).isValid());
 
555
#else
 
556
    mActionClearColour->setVisible(resource && resource->colour().isValid());
 
557
#endif
 
558
    mActionEdit->setEnabled(haveCalendar);
 
559
    mActionRemove->setEnabled(haveCalendar);
 
560
    mActionImport->setEnabled(active && writable);
 
561
    mActionExport->setEnabled(active);
 
562
    QString text;
 
563
    switch (type)
 
564
    {
 
565
        case KAlarm::CalEvent::ACTIVE:   text = i18nc("@action", "Use as &Default for Active Alarms");  break;
 
566
        case KAlarm::CalEvent::ARCHIVED: text = i18nc("@action", "Use as &Default for Archived Alarms");  break;
 
567
        case KAlarm::CalEvent::TEMPLATE: text = i18nc("@action", "Use as &Default for Alarm Templates");  break;
 
568
        default:  break;
 
569
    }
 
570
    mActionSetDefault->setText(text);
 
571
#ifdef USE_AKONADI
 
572
    bool standard = CollectionControlModel::isStandard(collection, type);
 
573
#else
 
574
    bool standard = (resource  &&  resource == mCalendar->getStandardResource(static_cast<KAlarm::CalEvent::Type>(type))  &&  resource->standardResource());
 
575
#endif
 
576
    mActionSetDefault->setChecked(active && writable && standard);
 
577
    mActionSetDefault->setEnabled(active && writable);
 
578
    mContextMenu->popup(mListView->viewport()->mapToGlobal(viewportPos));
420
579
}
421
580
 
422
581
/******************************************************************************
424
583
*/
425
584
void ResourceSelector::reloadResource()
426
585
{
427
 
        AlarmResource* resource = currentResource();
428
 
        if (resource)
429
 
                AlarmCalendar::resources()->loadResource(resource, this);
 
586
#ifdef USE_AKONADI
 
587
    Collection collection = currentResource();
 
588
    if (collection.isValid())
 
589
        AkonadiModel::instance()->reloadCollection(collection);
 
590
#else
 
591
    AlarmResource* resource = currentResource();
 
592
    if (resource)
 
593
        AlarmCalendar::resources()->loadResource(resource, this);
 
594
#endif
430
595
}
431
596
 
432
597
/******************************************************************************
434
599
*/
435
600
void ResourceSelector::saveResource()
436
601
{
437
 
        AlarmResource* resource = currentResource();
438
 
        if (resource)
439
 
                resource->save();
 
602
#ifdef USE_AKONADI
 
603
#ifdef __GNUC__
 
604
#warning saveResource() not implemented
 
605
#endif
 
606
#else
 
607
    AlarmResource* resource = currentResource();
 
608
    if (resource)
 
609
        resource->save();
 
610
#endif
440
611
}
441
612
 
442
613
/******************************************************************************
446
617
*/
447
618
void ResourceSelector::archiveDaysChanged(int days)
448
619
{
449
 
        if (days)
450
 
        {
451
 
                AlarmResources* resources = AlarmResources::instance();
452
 
                AlarmResource* std = resources->getStandardResource(AlarmResource::ARCHIVED);
453
 
                if (std  &&  !std->standardResource())
454
 
                        resources->setStandardResource(std);
455
 
        }
 
620
    if (days)
 
621
    {
 
622
#ifdef USE_AKONADI
 
623
        if (!CollectionControlModel::getStandard(KAlarm::CalEvent::ARCHIVED).isValid())
 
624
        {
 
625
            Collection::List cols = CollectionControlModel::enabledCollections(KAlarm::CalEvent::ARCHIVED, true);
 
626
            if (cols.count() == 1)
 
627
            CollectionControlModel::setStandard(cols[1], KAlarm::CalEvent::ARCHIVED);
 
628
        }
 
629
#else
 
630
        AlarmResources* resources = AlarmResources::instance();
 
631
        AlarmResource* std = resources->getStandardResource(KAlarm::CalEvent::ARCHIVED);
 
632
        if (std  &&  !std->standardResource())
 
633
            resources->setStandardResource(std);
 
634
#endif
 
635
    }
456
636
}
457
637
 
458
638
/******************************************************************************
461
641
*/
462
642
void ResourceSelector::setStandard()
463
643
{
464
 
        AlarmResource* resource = currentResource();
465
 
        if (resource)
466
 
        {
467
 
                if (mActionSetDefault->isChecked())
468
 
                {
469
 
                        resource->setEnabled(true);
470
 
                        mCalendar->setStandardResource(resource);
471
 
                }
472
 
                else
473
 
                        resource->setStandardResource(false);
474
 
        }
 
644
#ifdef USE_AKONADI
 
645
    Collection collection = currentResource();
 
646
    if (collection.isValid())
 
647
    {
 
648
        KAlarm::CalEvent::Type alarmType = currentResourceType();
 
649
        bool standard = mActionSetDefault->isChecked();
 
650
        if (standard)
 
651
            CollectionControlModel::setEnabled(collection, alarmType, true);
 
652
        CollectionControlModel::setStandard(collection, alarmType, standard);
 
653
    }
 
654
#else
 
655
    AlarmResource* resource = currentResource();
 
656
    if (resource)
 
657
    {
 
658
        if (mActionSetDefault->isChecked())
 
659
        {
 
660
            resource->setEnabled(true);
 
661
            mCalendar->setStandardResource(resource);
 
662
        }
 
663
        else
 
664
            resource->setStandardResource(false);
 
665
    }
 
666
#endif
475
667
}
476
668
 
477
669
/******************************************************************************
478
 
* Called when a resource status has changed.
 
670
* Called when a calendar status has changed.
479
671
*/
 
672
#ifdef USE_AKONADI
 
673
void ResourceSelector::slotStatusChanged(const Collection& collection, AkonadiModel::Change change, const QVariant& value)
 
674
#else
480
675
void ResourceSelector::slotStatusChanged(AlarmResource* resource, AlarmResources::Change change)
 
676
#endif
481
677
{
482
 
        if (change == AlarmResources::WrongType  &&  resource->isWrongAlarmType())
483
 
        {
484
 
                QString text;
485
 
                switch (resource->alarmType())
486
 
                {
487
 
                        case AlarmResource::ACTIVE:
488
 
                                text = i18nc("@info/plain", "It is not an active alarm calendar.");
489
 
                                break;
490
 
                        case AlarmResource::ARCHIVED:
491
 
                                text = i18nc("@info/plain", "It is not an archived alarm calendar.");
492
 
                                break;
493
 
                        case AlarmResource::TEMPLATE:
494
 
                                text = i18nc("@info/plain", "It is not an alarm template calendar.");
495
 
                                break;
496
 
                        default:
497
 
                                return;
498
 
                }
499
 
                KMessageBox::sorry(this, i18nc("@info", "<para>Calendar <resource>%1</resource> has been disabled:</para><para>%2</para>", resource->resourceName(), text));
500
 
        }
 
678
#ifndef USE_AKONADI
 
679
    if (change == AlarmResources::WrongType  &&  resource->isWrongAlarmType())
 
680
    {
 
681
        QString text;
 
682
        switch (resource->alarmType())
 
683
        {
 
684
            case KAlarm::CalEvent::ACTIVE:
 
685
                text = i18nc("@info/plain", "It is not an active alarm calendar.");
 
686
                break;
 
687
            case KAlarm::CalEvent::ARCHIVED:
 
688
                text = i18nc("@info/plain", "It is not an archived alarm calendar.");
 
689
                break;
 
690
            case KAlarm::CalEvent::TEMPLATE:
 
691
                text = i18nc("@info/plain", "It is not an alarm template calendar.");
 
692
                break;
 
693
            default:
 
694
                return;
 
695
        }
 
696
        KMessageBox::sorry(this, i18nc("@info", "<para>Calendar <resource>%1</resource> has been disabled:</para><para>%2</para>", resource->resourceName(), text));
 
697
    }
 
698
#endif
501
699
}
502
700
 
503
701
/******************************************************************************
506
704
*/
507
705
void ResourceSelector::importCalendar()
508
706
{
509
 
        AlarmCalendar::importAlarms(this, currentResource());
 
707
#ifdef USE_AKONADI
 
708
    Collection collection = currentResource();
 
709
    AlarmCalendar::importAlarms(this, (collection.isValid() ? &collection : 0));
 
710
#else
 
711
    AlarmCalendar::importAlarms(this, currentResource());
 
712
#endif
510
713
}
511
714
 
512
715
/******************************************************************************
515
718
*/
516
719
void ResourceSelector::exportCalendar()
517
720
{
518
 
        AlarmResource* resource = currentResource();
519
 
        if (resource)
520
 
                AlarmCalendar::exportAlarms(AlarmCalendar::resources()->events(resource), this);
 
721
#ifdef USE_AKONADI
 
722
    Collection calendar = currentResource();
 
723
    if (calendar.isValid())
 
724
#else
 
725
    AlarmResource* calendar = currentResource();
 
726
    if (calendar)
 
727
#endif
 
728
        AlarmCalendar::exportAlarms(AlarmCalendar::resources()->events(calendar), this);
521
729
}
522
730
 
523
731
/******************************************************************************
525
733
*/
526
734
void ResourceSelector::setColour()
527
735
{
528
 
        AlarmResource* resource = currentResource();
529
 
        if (resource)
530
 
        {
531
 
                QColor colour = resource->colour();
532
 
                if (!colour.isValid())
533
 
                        colour = QApplication::palette().color(QPalette::Base);
534
 
                if (KColorDialog::getColor(colour, QColor(), this) == KColorDialog::Accepted)
535
 
                        resource->setColour(colour);
536
 
        }
 
736
#ifdef USE_AKONADI
 
737
    Collection collection = currentResource();
 
738
    if (collection.isValid())
 
739
    {
 
740
        QColor colour = AkonadiModel::instance()->backgroundColor(collection);
 
741
        if (!colour.isValid())
 
742
            colour = QApplication::palette().color(QPalette::Base);
 
743
        if (KColorDialog::getColor(colour, QColor(), this) == KColorDialog::Accepted)
 
744
            AkonadiModel::instance()->setBackgroundColor(collection, colour);
 
745
    }
 
746
#else
 
747
    AlarmResource* resource = currentResource();
 
748
    if (resource)
 
749
    {
 
750
        QColor colour = resource->colour();
 
751
        if (!colour.isValid())
 
752
            colour = QApplication::palette().color(QPalette::Base);
 
753
        if (KColorDialog::getColor(colour, QColor(), this) == KColorDialog::Accepted)
 
754
            resource->setColour(colour);
 
755
    }
 
756
#endif
537
757
}
538
758
 
539
759
/******************************************************************************
542
762
*/
543
763
void ResourceSelector::clearColour()
544
764
{
545
 
        AlarmResource* resource = currentResource();
546
 
        if (resource)
547
 
                resource->setColour(QColor());
 
765
#ifdef USE_AKONADI
 
766
    Collection collection = currentResource();
 
767
    if (collection.isValid())
 
768
        AkonadiModel::instance()->setBackgroundColor(collection, QColor());
 
769
#else
 
770
    AlarmResource* resource = currentResource();
 
771
    if (resource)
 
772
        resource->setColour(QColor());
 
773
#endif
548
774
}
549
775
 
550
776
/******************************************************************************
552
778
*/
553
779
void ResourceSelector::showInfo()
554
780
{
555
 
        AlarmResource* resource = currentResource();
556
 
        if (resource)
557
 
                KMessageBox::information(this, resource->infoText());
 
781
#ifdef USE_AKONADI
 
782
    Collection collection = currentResource();
 
783
    if (collection.isValid())
 
784
    {
 
785
        QString id = collection.name();
 
786
        QString name;
 
787
        if (collection.hasAttribute<EntityDisplayAttribute>())
 
788
            name = collection.attribute<EntityDisplayAttribute>()->displayName();
 
789
        KAlarm::CalEvent::Type alarmType = currentResourceType();
 
790
        QString calType = AgentManager::self()->instance(collection.resource()).type().name();
 
791
        QString storage = AkonadiModel::instance()->storageType(collection);
 
792
        QString location = collection.remoteId();
 
793
        KUrl url(location);
 
794
        if (url.isLocalFile())
 
795
            location = url.path();
 
796
        QString perms = CollectionControlModel::isWritable(collection, alarmType, true)
 
797
                  ? i18nc("@info/plain", "Read-write")
 
798
                  : i18nc("@info/plain", "Read-only");
 
799
bool wrongAlarmType = false;  //(applies only to resourcelocaldir)
 
800
        QString enabled = CollectionControlModel::isEnabled(collection, alarmType)
 
801
                    ? i18nc("@info/plain", "Enabled")
 
802
                : wrongAlarmType ? i18nc("@info/plain", "Disabled (wrong alarm type)")
 
803
                : i18nc("@info/plain", "Disabled");
 
804
        QString std = CollectionControlModel::isStandard(collection, alarmType)
 
805
                ? i18nc("@info/plain Parameter in 'Default calendar: Yes/No'", "Yes")
 
806
                : i18nc("@info/plain Parameter in 'Default calendar: Yes/No'", "No");
 
807
        QString text = (name.isEmpty() || name == id)
 
808
                     ? i18nc("@info",
 
809
                             "<title>%1</title>"
 
810
                             "Contents: %2<nl/>"
 
811
                             "%3: <filename>%4</filename><nl/>"
 
812
                             "Permissions: %5<nl/>"
 
813
                             "Status: %6<nl/>"
 
814
                             "Default calendar: %7</para>",
 
815
                             id, calType, storage, location, perms, enabled, std)
 
816
                     : i18nc("@info",
 
817
                             "<title>%1</title>"
 
818
                             "<para>ID: %2<nl/>"
 
819
                             "Contents: %3<nl/>"
 
820
                             "%4: <filename>%5</filename><nl/>"
 
821
                             "Permissions: %6<nl/>"
 
822
                             "Status: %7<nl/>"
 
823
                             "Default calendar: %8</para>",
 
824
                             name, id, calType, storage, location, perms, enabled, std);
 
825
        // Display the collection information. Because the user requested
 
826
        // the information, don't raise a KNotify event.
 
827
        KMessageBox::information(this, text, QString(), QString(), 0);
 
828
    }
 
829
#else
 
830
    AlarmResource* resource = currentResource();
 
831
    if (resource)
 
832
    {
 
833
        // Display the collection information. Because the user requested
 
834
        // the information, don't raise a KNotify event.
 
835
        KMessageBox::information(this, resource->infoText(), QString(), QString(), 0);
 
836
    }
 
837
#endif
558
838
}
559
839
 
560
840
/******************************************************************************
561
841
* Return the currently selected resource in the list.
562
842
*/
 
843
#ifdef USE_AKONADI
 
844
Collection ResourceSelector::currentResource() const
 
845
{
 
846
    return mListView->collection(mListView->selectionModel()->currentIndex());
 
847
}
 
848
#else
563
849
AlarmResource* ResourceSelector::currentResource() const
564
850
{
565
 
        return mListView->resource(mListView->selectionModel()->currentIndex());
 
851
    return mListView->resource(mListView->selectionModel()->currentIndex());
 
852
}
 
853
#endif
 
854
 
 
855
/******************************************************************************
 
856
* Return the currently selected resource type.
 
857
*/
 
858
KAlarm::CalEvent::Type ResourceSelector::currentResourceType() const
 
859
{
 
860
    switch (mAlarmType->currentIndex())
 
861
    {
 
862
        case 0:  return KAlarm::CalEvent::ACTIVE;
 
863
        case 1:  return KAlarm::CalEvent::ARCHIVED;
 
864
        case 2:  return KAlarm::CalEvent::TEMPLATE;
 
865
        default:  return KAlarm::CalEvent::EMPTY;
 
866
    }
566
867
}
567
868
 
568
869
void ResourceSelector::resizeEvent(QResizeEvent* re)
569
870
{
570
 
        emit resized(re->oldSize(), re->size());
 
871
    emit resized(re->oldSize(), re->size());
571
872
}
 
873
 
 
874
// vim: et sw=4: