Skip to content

Commit

Permalink
[mkcal] Add a new storage observer signal on DB update
Browse files Browse the repository at this point in the history
The existing storageModified() signal is emitted whenever
the ".changed" file is touched. Restrict its emission
only if some change has been done to the database content
from an external process. Currently emit it also if a
change is done in process to any notebook.

Add a new signal storageUpdated() that is emitted on any
modification of the database component parts (addition,
modification of deletion) to be in part with the associated
mCalendar.
  • Loading branch information
dcaliste committed Mar 14, 2022
1 parent d81500e commit f3e123a
Show file tree
Hide file tree
Showing 10 changed files with 288 additions and 28 deletions.
6 changes: 3 additions & 3 deletions rpm/mkcal-qt5.spec
@@ -1,7 +1,7 @@
Name: mkcal-qt5

Summary: Extended KDE kcal calendar library port for Maemo
Version: 0.5.45
Summary: SQlite storage backend for KCalendarCore
Version: 0.5.58
Release: 1
License: LGPLv2+
URL: https://github.com/sailfishos/mkcal
Expand All @@ -20,7 +20,7 @@ BuildRequires: pkgconfig(timed-qt5) >= 2.88
BuildRequires: pkgconfig(QmfClient)

%description
Extended KDE kcal calendar library port for Maemo
Extends KDE calendar core library and provides an SQlite backend.


%package devel
Expand Down
20 changes: 20 additions & 0 deletions src/extendedstorage.cpp
Expand Up @@ -317,6 +317,17 @@ void ExtendedStorageObserver::storageFinished(ExtendedStorage *storage,
Q_UNUSED(info);
}

void ExtendedStorageObserver::storageUpdated(ExtendedStorage *storage,
const KCalendarCore::Incidence::List &added,
const KCalendarCore::Incidence::List &modified,
const KCalendarCore::Incidence::List &deleted)
{
Q_UNUSED(storage);
Q_UNUSED(added);
Q_UNUSED(modified);
Q_UNUSED(deleted);
}

void ExtendedStorage::registerObserver(ExtendedStorageObserver *observer)
{
if (!d->mObservers.contains(observer)) {
Expand Down Expand Up @@ -363,6 +374,15 @@ void ExtendedStorage::setFinished(bool error, const QString &info)
}
}

void ExtendedStorage::setUpdated(const KCalendarCore::Incidence::List &added,
const KCalendarCore::Incidence::List &modified,
const KCalendarCore::Incidence::List &deleted)
{
foreach (ExtendedStorageObserver *observer, d->mObservers) {
observer->storageUpdated(this, added, modified, deleted);
}
}

bool ExtendedStorage::addNotebook(const Notebook::Ptr &nb)
{
if (nb->uid().length() < 7) {
Expand Down
3 changes: 3 additions & 0 deletions src/extendedstorage.h
Expand Up @@ -674,6 +674,9 @@ class MKCAL_EXPORT ExtendedStorage
void setModified(const QString &info);
void setProgress(const QString &info);
void setFinished(bool error, const QString &info);
void setUpdated(const KCalendarCore::Incidence::List &added,
const KCalendarCore::Incidence::List &modified,
const KCalendarCore::Incidence::List &deleted);

// These alarm methods are used to communicate with an external
// daemon, like timed, to bind Incidence::Alarm with the system notification.
Expand Down
27 changes: 26 additions & 1 deletion src/extendedstorageobserver.h
Expand Up @@ -32,6 +32,7 @@
#define MKCAL_STORAGEOBSERVER_H

#include <QString>
#include <KCalendarCore/Incidence>


namespace mKCal {
Expand All @@ -51,7 +52,11 @@ class MKCAL_EXPORT ExtendedStorageObserver //krazy:exclude=dpointer
virtual ~ExtendedStorageObserver() {};

/**
Notify the Observer that a Storage has been modified.
Notify the Observer that a Storage has been modified by an external
process. There is no information about what has been changed.
See also storageUpdated() for a notification of modifications done
in-process.
@param storage is a pointer to the ExtendedStorage object that
is being observed.
Expand Down Expand Up @@ -79,6 +84,26 @@ class MKCAL_EXPORT ExtendedStorageObserver //krazy:exclude=dpointer
@param info textual information
*/
virtual void storageFinished(ExtendedStorage *storage, bool error, const QString &info);

/**
Notify the Observer that a Storage has been updated to reflect the
content of the associated calendar. This notification is delivered
because of local changes done in-process by a call to
ExtendedStorage::save() for instance.
See also storageModified() for a notification for modifications
done to the database by an external process.
@param storage is a pointer to the ExtendedStorage object that
is being observed.
@param added is a list of newly added incidences in the storage
@param modified is a list of updated incidences in the storage
@param deleted is a list of deleted incidences from the storage
*/
virtual void storageUpdated(ExtendedStorage *storage,
const KCalendarCore::Incidence::List &added,
const KCalendarCore::Incidence::List &modified,
const KCalendarCore::Incidence::List &deleted);
};

};
Expand Down
66 changes: 66 additions & 0 deletions src/sqliteformat.cpp
Expand Up @@ -54,6 +54,8 @@ class mKCal::SqliteFormat::Private
}
~Private()
{
sqlite3_finalize(mSelectMetadata);
sqlite3_finalize(mUpdateMetadata);
sqlite3_finalize(mSelectCalProps);
sqlite3_finalize(mInsertCalProps);
sqlite3_finalize(mSelectIncProperties);
Expand Down Expand Up @@ -84,6 +86,9 @@ class mKCal::SqliteFormat::Private
sqlite3 *mDatabase;

// Cache for various queries.
sqlite3_stmt *mSelectMetadata = nullptr;
sqlite3_stmt *mUpdateMetadata = nullptr;

sqlite3_stmt *mSelectCalProps = nullptr;
sqlite3_stmt *mInsertCalProps = nullptr;

Expand Down Expand Up @@ -116,6 +121,7 @@ class mKCal::SqliteFormat::Private

sqlite3_stmt *mMarkDeletedIncidences = nullptr;

bool updateMetadata(int transactionId);
bool selectCustomproperties(Incidence::Ptr &incidence, int rowid);
int selectRowId(Incidence::Ptr incidence);
bool selectRecursives(Incidence::Ptr &incidence, int rowid);
Expand Down Expand Up @@ -153,6 +159,66 @@ SqliteFormat::~SqliteFormat()
delete d;
}

bool SqliteFormat::selectMetadata(int *id)
{
int rv = 0;

if (!id)
return false;
if (!d->mSelectMetadata) {
const char *query = SELECT_METADATA;
int qsize = sizeof(SELECT_METADATA);
SL3_prepare_v2(d->mDatabase, query, qsize, &d->mSelectMetadata, NULL);
}
SL3_step(d->mSelectMetadata);
*id = (rv == SQLITE_ROW) ? sqlite3_column_int(d->mSelectMetadata, 0) : -1;
SL3_reset(d->mSelectMetadata);

return true;

error:
qCWarning(lcMkcal) << "Sqlite error:" << sqlite3_errmsg(d->mDatabase);
return false;
}

bool SqliteFormat::incrementTransactionId(int *id)
{
int savedId;

if (id)
*id = -1;
if (!selectMetadata(&savedId))
return false;
savedId += 1;

if (!d->updateMetadata(savedId))
return false;
if (id)
*id = savedId;
return true;
}

bool SqliteFormat::Private::updateMetadata(int transactionId)
{
int rv = 0;
int index = 1;

if (!mUpdateMetadata) {
const char *qupdate = UPDATE_METADATA;
int qsize = sizeof(UPDATE_METADATA);
SL3_prepare_v2(mDatabase, qupdate, qsize, &mUpdateMetadata, NULL);
}
SL3_reset(mUpdateMetadata);
SL3_bind_int64(mUpdateMetadata, index, transactionId);
SL3_step(mUpdateMetadata);

return true;

error:
qCWarning(lcMkcal) << "Sqlite error:" << sqlite3_errmsg(mDatabase);
return false;
}

bool SqliteFormat::modifyCalendars(const Notebook::Ptr &notebook,
DBOperation dbop, sqlite3_stmt *stmt)
{
Expand Down
3 changes: 3 additions & 0 deletions src/sqliteformat.h
Expand Up @@ -121,6 +121,9 @@ class MKCAL_EXPORT SqliteFormat
*/
KCalendarCore::Person::List selectContacts();

bool selectMetadata(int *id);
bool incrementTransactionId(int *id);

private:
//@cond PRIVATE
Q_DISABLE_COPY(SqliteFormat)
Expand Down

0 comments on commit f3e123a

Please sign in to comment.