Commit 7d3c535c authored by camilo higuita's avatar camilo higuita

remove notes too

parent aebca940
...@@ -267,7 +267,7 @@ bool DB::remove(const QString &tableName, const QVariantMap &removeData) ...@@ -267,7 +267,7 @@ bool DB::remove(const QString &tableName, const QVariantMap &removeData)
QStringList set; QStringList set;
for (auto key : removeData.keys()) for (const auto &key : removeData.keys())
set.append(key+" = ?"); set.append(key+" = ?");
QString sqlQueryString = "DELETE FROM " + tableName + " WHERE " + QString(set.join(" AND ")); QString sqlQueryString = "DELETE FROM " + tableName + " WHERE " + QString(set.join(" AND "));
......
...@@ -32,9 +32,67 @@ Notes::Notes(QObject *parent) : MauiList(parent), ...@@ -32,9 +32,67 @@ Notes::Notes(QObject *parent) : MauiList(parent),
void Notes::sortList() void Notes::sortList()
{ {
emit this->preListChanged(); emit this->preListChanged();
// this->notes = this->db->getDBData(QString("select * from notes ORDER BY %1 %2").arg( const auto key = static_cast<FMH::MODEL_KEY>(this->sort);
// FMH::MODEL_NAME[static_cast<FMH::MODEL_KEY>(this->sort)], qDebug()<< "SORTING LIST BY"<< this->sort;
// this->order == ORDER::ASC ? "asc" : "desc")); std::sort(this->notes.begin(), this->notes.end(), [&](const FMH::MODEL &e1, const FMH::MODEL &e2) -> bool
{
switch(key)
{
case FMH::MODEL_KEY::FAVORITE:
{
return e1[key] == "true";
}
case FMH::MODEL_KEY::ADDDATE:
case FMH::MODEL_KEY::MODIFIED:
{
const auto date1 = QDateTime::fromString(e1[key], Qt::TextDate);
const auto date2 = QDateTime::fromString(e2[key], Qt::TextDate);
if(this->order == Notes::ORDER::ASC)
{
if(date1.secsTo(QDateTime::currentDateTime()) > date2.secsTo(QDateTime::currentDateTime()))
return true;
}
if(this->order == Notes::ORDER::DESC)
{
if(date1.secsTo(QDateTime::currentDateTime()) < date2.secsTo(QDateTime::currentDateTime()))
return true;
}
break;
}
case FMH::MODEL_KEY::TITLE:
case FMH::MODEL_KEY::COLOR:
{
const auto str1 = QString(e1[key]).toLower();
const auto str2 = QString(e2[key]).toLower();
if(this->order == Notes::ORDER::ASC)
{
if(str1 < str2)
return true;
}
if(this->order == Notes::ORDER::DESC)
{
if(str1 > str2)
return true;
}
break;
}
default:
if(e1[key] < e2[key])
return true;
}
return false;
});
emit this->postListChanged(); emit this->postListChanged();
} }
...@@ -113,18 +171,14 @@ bool Notes::update(const QVariantMap &data, const int &index) ...@@ -113,18 +171,14 @@ bool Notes::update(const QVariantMap &data, const int &index)
bool Notes::remove(const int &index) bool Notes::remove(const int &index)
{ {
emit this->preItemRemoved(index); if(index < 0 || index >= this->notes.size())
auto id = this->notes.at(index)[FMH::MODEL_KEY::ID]; return false;
QVariantMap note = {{FMH::MODEL_NAME[FMH::MODEL_KEY::ID], id}};
// if(this->db->remove(OWL::TABLEMAP[OWL::TABLE::NOTES], note))
// {
// this->notes.removeAt(index);
// emit this->postItemRemoved();
// return true;
// }
return false; emit this->preItemRemoved(index);
this->syncer->removeNote(this->notes.at(index)[FMH::MODEL_KEY::ID]);
this->notes.removeAt(index);
emit this->postItemRemoved();
return true;
} }
void Notes::setAccount(const QVariantMap &account) void Notes::setAccount(const QVariantMap &account)
......
...@@ -129,7 +129,7 @@ signals: ...@@ -129,7 +129,7 @@ signals:
void notesReady(FMH::MODEL_LIST notes); void notesReady(FMH::MODEL_LIST notes);
void noteInserted(FMH::MODEL note); void noteInserted(FMH::MODEL note);
void noteUpdated(FMH::MODEL note); void noteUpdated(FMH::MODEL note);
void noteRemoved(FMH::MODEL note); void noteRemoved();
/** /**
* @brief responseReady * @brief responseReady
......
...@@ -168,6 +168,23 @@ void NextNote::updateNote(const QString &id, const FMH::MODEL &note) ...@@ -168,6 +168,23 @@ void NextNote::updateNote(const QString &id, const FMH::MODEL &note)
void NextNote::removeNote(const QString &id) void NextNote::removeNote(const QString &id)
{ {
if(id.isEmpty())
{
qWarning()<< "The id is empty. Can not proceed. NextNote::remove";
return;
}
const auto url = QString(NextNote::API+"%1%2").replace("PROVIDER", this->m_provider).arg("notes/", id);
const auto request = formRequest(url, this->m_user, this->m_password);
auto restclient = new QNetworkAccessManager; //constructor
QNetworkReply *reply = restclient->deleteResource(request);
connect(reply, &QNetworkReply::finished, [=]()
{
qDebug() << "Note remove finished?" << reply->errorString();
emit this->noteRemoved();
restclient->deleteLater();
});
} }
const QString NextNote::formatUrl(const QString &user, const QString &password, const QString &provider) const QString NextNote::formatUrl(const QString &user, const QString &password, const QString &provider)
......
...@@ -134,6 +134,18 @@ void Syncer::updateNote(const QString &id, const FMH::MODEL &note) ...@@ -134,6 +134,18 @@ void Syncer::updateNote(const QString &id, const FMH::MODEL &note)
emit this->noteUpdated(note, {STATE::TYPE::LOCAL, STATE::STATUS::OK, "Note updated on the DB locally"}); emit this->noteUpdated(note, {STATE::TYPE::LOCAL, STATE::STATUS::OK, "Note updated on the DB locally"});
} }
void Syncer::removeNote(const QString &id)
{
if(!this->removeLocal(id))
{
qWarning()<< "The note could not be inserted locally, "
"therefore it was not attempted to insert it to the remote provider server, "
"even if it existed.";
return;
}
this->removeRemote(id);
}
void Syncer::getNotes() void Syncer::getNotes()
{ {
const auto notes = this->db->getDBData("select n.*, ns.server, ns.user, ns.stamp from notes n inner join notes_sync ns on n.id = ns.id "); const auto notes = this->db->getDBData("select n.*, ns.server, ns.user, ns.stamp from notes n inner join notes_sync ns on n.id = ns.id ");
...@@ -212,3 +224,15 @@ void Syncer::updateRemote(const QString &id, const FMH::MODEL &note) ...@@ -212,3 +224,15 @@ void Syncer::updateRemote(const QString &id, const FMH::MODEL &note)
if(this->provider->isValid()) if(this->provider->isValid())
this->provider->updateNote(id, note); this->provider->updateNote(id, note);
} }
bool Syncer::removeLocal(const QString &id)
{
this->db->remove(OWL::TABLEMAP[OWL::TABLE::NOTES_SYNC], {{FMH::MODEL_NAME[FMH::MODEL_KEY::ID], id}});
return this->db->remove(OWL::TABLEMAP[OWL::TABLE::NOTES], {{FMH::MODEL_NAME[FMH::MODEL_KEY::ID], id}});
}
void Syncer::removeRemote(const QString &id)
{
if(this->provider->isValid())
this->provider->removeNote(id);
}
...@@ -178,6 +178,9 @@ protected: ...@@ -178,6 +178,9 @@ protected:
bool updateLocal(const QString &id, const FMH::MODEL &note); bool updateLocal(const QString &id, const FMH::MODEL &note);
void updateRemote(const QString &id, const FMH::MODEL &note); void updateRemote(const QString &id, const FMH::MODEL &note);
bool removeLocal(const QString &id);
void removeRemote(const QString &id);
signals: signals:
void noteInserted(FMH::MODEL note, STATE state); void noteInserted(FMH::MODEL note, STATE state);
void noteUpdated(FMH::MODEL note, STATE state); void noteUpdated(FMH::MODEL note, STATE state);
......
import QtQuick 2.9 import QtQuick 2.9
import QtQuick.Controls 2.3 import QtQuick.Controls 2.3
import org.kde.mauikit 1.0 as Maui import org.kde.mauikit 1.0 as Maui
import org.kde.kirigami 2.7 as Kirigami
Menu Menu
{ {
...@@ -55,7 +56,8 @@ Menu ...@@ -55,7 +56,8 @@ Menu
MenuItem MenuItem
{ {
text: qsTr("Delete") text: qsTr("Remove")
Kirigami.Theme.textColor: Kirigami.Theme.negativeTextColor
onTriggered: onTriggered:
{ {
deleteClicked() deleteClicked()
......
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