Commit 74ca4265 authored by David Jarvie's avatar David Jarvie
Browse files

Properly evaluate read-only / old format status of collections

parent 24873141
......@@ -3,6 +3,7 @@
#warning Add a directory resource containing 2 alarm types, right click on it -> crash
#warning Set default template calendar to read-only -> crash
#warning Set default archived calendar to read-only, then read-write -> now "other format"
#warning Create new email template, get valgrind uninitialised errors from serialize()
#endif
/*
* akonadimodel.cpp - KAlarm calendar file access using Akonadi
......@@ -979,11 +980,17 @@ QString AkonadiModel::tooltip(const Collection& collection, KAlarm::CalEvent::Ty
QString AkonadiModel::readOnlyTooltip(const Collection& collection)
{
KAlarm::Calendar::Compat compat;
return AkonadiModel::isWritable(collection, compat)
? QString()
: (compat == KAlarm::Calendar::Current) ? i18nc("@info/plain", "Read-only")
: (compat == KAlarm::Calendar::Incompatible) ? i18nc("@info/plain", "Read-only (other format)")
: i18nc("@info/plain", "Read-only (old format)");
switch (AkonadiModel::isWritable(collection, compat))
{
case 1:
return QString();
case 0:
return i18nc("@info/plain", "Read-only (old format)");
default:
if (compat == KAlarm::Calendar::Current)
return i18nc("@info/plain", "Read-only");
return i18nc("@info/plain", "Read-only (other format)");
}
}
/******************************************************************************
......@@ -1814,30 +1821,37 @@ bool AkonadiModel::isCompatible(const Collection& collection)
/******************************************************************************
* Return whether a collection is fully writable.
*/
bool AkonadiModel::isWritable(const Akonadi::Collection& collection)
int AkonadiModel::isWritable(const Akonadi::Collection& collection)
{
KAlarm::Calendar::Compat format;
return isWritable(collection, format);
}
bool AkonadiModel::isWritable(const Akonadi::Collection& collection, KAlarm::Calendar::Compat& format)
int AkonadiModel::isWritable(const Akonadi::Collection& collection, KAlarm::Calendar::Compat& format)
{
format = KAlarm::Calendar::Current;
format = KAlarm::Calendar::Incompatible;
if (!collection.isValid())
return false;
return -1;
Collection col = collection;
instance()->refresh(col); // update with latest data
if ((col.rights() & writableRights) != writableRights)
return false;
if (!col.hasAttribute<CompatibilityAttribute>())
{
format = KAlarm::Calendar::Incompatible;
return false;
format = KAlarm::Calendar::Current;
return -1;
}
if (!col.hasAttribute<CompatibilityAttribute>())
return -1;
format = col.attribute<CompatibilityAttribute>()->compatibility();
if (format != KAlarm::Calendar::Current)
return false;
return true;
switch (format)
{
case KAlarm::Calendar::Current:
return 1;
case KAlarm::Calendar::Converted:
case KAlarm::Calendar::Convertible:
return 0;
default:
return -1;
}
}
KAlarm::CalEvent::Types AkonadiModel::types(const Collection& collection)
......
......@@ -161,21 +161,27 @@ class AkonadiModel : public Akonadi::EntityTreeModel
/** Return whether a collection is fully writable, i.e. with
* create/delete/change rights and compatible with the current KAlarm
* calendar format.
*
* @return 1 = fully writable,
* 0 = writable except that backend calendar is in an old KAlarm format,
* -1 = read-only or incompatible format.
*/
static bool isWritable(const Akonadi::Collection&);
static int isWritable(const Akonadi::Collection&);
/** Return whether a collection is fully writable, i.e. with
* create/delete/change rights and compatible with the current KAlarm
* calendar format.
*
* @param format If the reply is false, and the calendar is not read-only
* but its backend calendar storage format is not the
* current KAlarm format, @p format is set to the calendar
* format used by the backend. If the calendar is
* non-writable for any other reason, @p format is set
* to KAlarm::Calendar::Current.
* @param format Updated to contain the backend calendar storage format.
* If read-only, = KAlarm::Calendar::Current;
* if unknown format, = KAlarm::Calendar::Incompatible;
* otherwise = the backend calendar storage format.
* @return 1 = fully writable,
* 0 = writable except that backend calendar is in an old KAlarm format,
* -1 = read-only (if @p compat == KAlarm::Calendar::Current), or
* incompatible format otherwise.
*/
static bool isWritable(const Akonadi::Collection&, KAlarm::Calendar::Compat& format);
static int isWritable(const Akonadi::Collection&, KAlarm::Calendar::Compat& format);
static KAlarm::CalEvent::Types types(const Akonadi::Collection&);
......
......@@ -818,22 +818,28 @@ QString CollectionControlModel::typeListForDisplay(KAlarm::CalEvent::Types alarm
* Return whether a collection is both enabled and fully writable for a given
* alarm type.
* Optionally, the enabled status can be ignored.
* Reply: 1 = fully enabled and writable,
* 0 = enabled and writable except that backend calendar is in an old KAlarm format,
* -1 = not enabled, read-only, or incompatible format.
*/
bool CollectionControlModel::isWritableEnabled(const Akonadi::Collection& collection, KAlarm::CalEvent::Type type)
int CollectionControlModel::isWritableEnabled(const Akonadi::Collection& collection, KAlarm::CalEvent::Type type)
{
KAlarm::Calendar::Compat format;
return isWritableEnabled(collection, type, format);
}
bool CollectionControlModel::isWritableEnabled(const Akonadi::Collection& collection, KAlarm::CalEvent::Type type, KAlarm::Calendar::Compat& format)
int CollectionControlModel::isWritableEnabled(const Akonadi::Collection& collection, KAlarm::CalEvent::Type type, KAlarm::Calendar::Compat& format)
{
if (!AkonadiModel::isWritable(collection, format))
return false;
int writable = AkonadiModel::isWritable(collection, format);
if (writable == -1)
return -1;
// Check the collection's enabled status
if (!instance()->collections().contains(collection)
|| !collection.hasAttribute<CollectionAttribute>())
return false;
return collection.attribute<CollectionAttribute>()->isEnabled(type);
return -1;
if (!collection.attribute<CollectionAttribute>()->isEnabled(type))
return -1;
return writable;
}
/******************************************************************************
......
......@@ -178,8 +178,13 @@ class CollectionControlModel : public Akonadi::FavoriteCollectionsModel
* given alarm type, i.e. with create/delete/change rights and compatible
* with the current KAlarm calendar format.
* Optionally, the enabled status can be ignored.
*
* @return 1 = fully enabled and writable,
* 0 = enabled and writable except that backend calendar is in an
* old KAlarm format,
* -1 = read-only, disabled or incompatible format.
*/
static bool isWritableEnabled(const Akonadi::Collection&, KAlarm::CalEvent::Type);
static int isWritableEnabled(const Akonadi::Collection&, KAlarm::CalEvent::Type);
/** Return whether a collection is both enabled and fully writable for a
* given alarm type, i.e. with create/delete/change rights and compatible
......@@ -192,8 +197,13 @@ class CollectionControlModel : public Akonadi::FavoriteCollectionsModel
* format used by the backend. If the calendar is
* non-writable for any other reason, @p format is set
* to KAlarm::Calendar::Current.
* @return 1 = fully enabled and writable,
* 0 = enabled and writable except that backend calendar is in an
* old KAlarm format,
* -1 = read-only (if @p format == KAlarm::Calendar::Current), or
* disabled or incompatible format.
*/
static bool isWritableEnabled(const Akonadi::Collection&, KAlarm::CalEvent::Type, KAlarm::Calendar::Compat& format);
static int isWritableEnabled(const Akonadi::Collection&, KAlarm::CalEvent::Type, KAlarm::Calendar::Compat& format);
/** Return the standard collection for a specified mime type.
* @param useDefault false to return the defined standard collection, if any;
......
......@@ -1113,7 +1113,7 @@ bool EditAlarmDlg::validate()
}
bool cancelled = false;
KAlarm::CalEvent::Type type = mTemplate ? KAlarm::CalEvent::TEMPLATE : KAlarm::CalEvent::ACTIVE;
if (!CollectionControlModel::isWritableEnabled(mCollection, type))
if (CollectionControlModel::isWritableEnabled(mCollection, type) <= 0)
mCollection = CollectionControlModel::destination(type, this, false, &cancelled);
if (!mCollection.isValid())
{
......
......@@ -1159,7 +1159,7 @@ void MessageWin::redisplayAlarms()
MessageWin* win = new MessageWin(&event, alarm, flags);
#ifdef USE_AKONADI
win->mCollection = collection;
bool rw = CollectionControlModel::isWritableEnabled(collection, event.category());
bool rw = CollectionControlModel::isWritableEnabled(collection, event.category()) > 0;
#else
win->mResource = resource;
bool rw = resource && resource->writable();
......
......@@ -581,8 +581,9 @@ void ResourceSelector::contextMenuRequested(const QPoint& viewportPos)
// Note: the CollectionControlModel functions call AkonadiModel::refresh(collection)
active = CollectionControlModel::isEnabled(collection, type);
KAlarm::Calendar::Compat compatibility;
writable = CollectionControlModel::isWritableEnabled(collection, type, compatibility);
if (!writable
int rw = CollectionControlModel::isWritableEnabled(collection, type, compatibility);
writable = (rw > 0);
if (!rw
&& (compatibility & ~KAlarm::Calendar::Converted)
&& !(compatibility & ~(KAlarm::Calendar::Convertible | KAlarm::Calendar::Converted)))
updatable = true; // the calendar format is convertible to the current KAlarm format
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment