Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[nemo-qml-plugin-calendar] Add error state to import model. Contribut…
…es to JB#52653
  • Loading branch information
pvuorela committed Jan 12, 2021
1 parent 2bea9c1 commit ff2d1a7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/calendarimportmodel.cpp
Expand Up @@ -46,7 +46,8 @@
#include <memorycalendar.h>

CalendarImportModel::CalendarImportModel(QObject *parent)
: QAbstractListModel(parent)
: QAbstractListModel(parent),
mError(false)
{
}

Expand Down Expand Up @@ -90,6 +91,11 @@ void CalendarImportModel::setIcsString(const QString &icsData)
reload();
}

bool CalendarImportModel::error() const
{
return mError;
}

QObject *CalendarImportModel::getEvent(int index)
{
if (index < 0 || index >= mEventList.count())
Expand Down Expand Up @@ -196,12 +202,14 @@ static bool incidenceLessThan(const KCalCore::Incidence::Ptr e1,
void CalendarImportModel::reload()
{
if (!mFileName.isEmpty() || !mIcsRawData.isEmpty()) {
importToMemory(mFileName, mIcsRawData);
bool success = importToMemory(mFileName, mIcsRawData);
setError(!success);
} else if (!mEventList.isEmpty()) {
beginResetModel();
mEventList.clear();
endResetModel();
emit countChanged();
setError(false);
}
}

Expand All @@ -210,12 +218,14 @@ bool CalendarImportModel::importToMemory(const QString &fileName, const QByteArr
if (!mEventList.isEmpty())
mEventList.clear();

bool success = false;

beginResetModel();
KCalCore::MemoryCalendar::Ptr cal(new KCalCore::MemoryCalendar(KDateTime::Spec::LocalZone()));
if (!fileName.isEmpty()) {
CalendarUtils::importFromFile(fileName, cal);
success = CalendarUtils::importFromFile(fileName, cal);
} else {
CalendarUtils::importFromIcsRawData(icsData, cal);
success = CalendarUtils::importFromIcsRawData(icsData, cal);
}
KCalCore::Incidence::List incidenceList = cal->incidences();
for (int i = 0; i < incidenceList.size(); i++) {
Expand All @@ -228,6 +238,14 @@ bool CalendarImportModel::importToMemory(const QString &fileName, const QByteArr

endResetModel();
emit countChanged();
return true;
return success;
}

void CalendarImportModel::setError(bool error)
{
if (error != mError) {
mError = error;
emit errorChanged();
}
}

6 changes: 6 additions & 0 deletions src/calendarimportmodel.h
Expand Up @@ -44,6 +44,7 @@ class CalendarImportModel : public QAbstractListModel
Q_PROPERTY(int count READ count NOTIFY countChanged)
Q_PROPERTY(QString fileName READ fileName WRITE setFileName NOTIFY fileNameChanged)
Q_PROPERTY(QString icsString READ icsString WRITE setIcsString NOTIFY icsStringChanged)
Q_PROPERTY(bool error READ error NOTIFY errorChanged)

public:
enum {
Expand All @@ -67,6 +68,8 @@ class CalendarImportModel : public QAbstractListModel
QString icsString() const;
void setIcsString(const QString &icsData);

bool error() const;

virtual int rowCount(const QModelIndex &index) const;
virtual QVariant data(const QModelIndex &index, int role) const;

Expand All @@ -77,6 +80,7 @@ public slots:
void countChanged();
void fileNameChanged();
void icsStringChanged();
bool errorChanged();

public slots:
bool importToNotebook(const QString &notebookUid = QString());
Expand All @@ -87,10 +91,12 @@ public slots:
private:
void reload();
bool importToMemory(const QString &fileName, const QByteArray &icsData);
void setError(bool error);

QString mFileName;
QByteArray mIcsRawData;
KCalCore::Event::List mEventList;
bool mError;
};

#endif // CALENDARIMPORT_H

0 comments on commit ff2d1a7

Please sign in to comment.