Index: resources/tomboynotes/CMakeLists.txt =================================================================== --- /dev/null +++ resources/tomboynotes/CMakeLists.txt @@ -0,0 +1,62 @@ +########### next target ############### + +set(tomboynotesresource_SRCS + configdialog.cpp + tomboynotesresource.cpp + tomboycollectionsdownloadjob.cpp + tomboyitemdownloadjob.cpp + tomboyitemuploadjob.cpp + tomboyitemsdownloadjob.cpp + tomboyjobbase.cpp + tomboyserverauthenticatejob.cpp +) + +ecm_qt_declare_logging_category(tomboynotesresource_SRCS + HEADER debug.h + IDENTIFIER log_tomboynotesresource + CATEGORY_NAME log_tomboynotesresource +) + +ki18n_wrap_ui(tomboynotesresource_SRCS configdialog.ui) + +kconfig_add_kcfg_files(tomboynotesresource_SRCS + ${CMAKE_CURRENT_SOURCE_DIR}/settings.kcfgc +) + +kcfg_generate_dbus_interface( + ${CMAKE_CURRENT_SOURCE_DIR}/tomboynotesresource.kcfg + org.kde.Akonadi.TomboyNotes.Settings +) + +qt5_add_dbus_adaptor(tomboynotesresource_SRCS + ${CMAKE_CURRENT_BINARY_DIR}/org.kde.Akonadi.TomboyNotes.Settings.xml + ${CMAKE_CURRENT_BINARY_DIR}/settings.h + Settings +) + + +add_executable(akonadi_tomboynotes_resource ${tomboynotesresource_SRCS}) +target_link_libraries(akonadi_tomboynotes_resource + o2 + Qt5::DBus + Qt5::Gui + Qt5::Network + Qt5::WebEngineWidgets + KF5::AkonadiAgentBase + KF5::AkonadiNotes + KF5::ConfigCore + KF5::ConfigWidgets + KF5::I18n + KF5::KIOCore + KF5::KIOFileWidgets + KF5::KIONTLM + KF5::KIOWidgets + KF5::Mime + KF5::WindowSystem +) + +install(TARGETS akonadi_tomboynotes_resource ${KDE_INSTALL_TARGETS_DEFAULT_ARGS}) + +install(FILES tomboynotesresource.desktop + DESTINATION "${KDE_INSTALL_DATAROOTDIR}/share/akonadi/agents" +) Index: resources/tomboynotes/Messages.sh =================================================================== --- /dev/null +++ resources/tomboynotes/Messages.sh @@ -0,0 +1,3 @@ +#! /usr/bin/env bash +$EXTRACTRC `find . -name \*.ui` `find . -name \*.kcfg` >> rc.cpp || exit 11 +$XGETTEXT *.cpp -o $podir/akonadi_tomboynotes_resource.pot Index: resources/tomboynotes/README =================================================================== --- /dev/null +++ resources/tomboynotes/README @@ -0,0 +1,14 @@ +==== What's this ? ==== +This is an Akonadi resource for Tomboy-compatible note servers like Grauphel. + +==== Usage ==== +For this resource you need only the URL of your service, +e.g. https://fqdn/index.php/apps/grauphel +It is the same URL that you have used in Tomboy. +For the registration process it will open a web page where you can log in +with your username and password of your account of the note server. + +==== Tested with ==== +Today it's tested only with Grauphel. So it may be incompatible with +other server implemantations (e.g. Rainy). But you are free to test it +and to report your experiences. Index: resources/tomboynotes/acknowledgements.md =================================================================== --- /dev/null +++ resources/tomboynotes/acknowledgements.md @@ -0,0 +1,7 @@ +# Daniel Vrátil +Unofficial supervisor of this project + +# Other developers of kdepim +The source code of the existing resources and KJots helped me also a lot +to understand what I had to do for the implementation. I have reused some +little parts of the source codes. Index: resources/tomboynotes/configdialog.h =================================================================== --- /dev/null +++ resources/tomboynotes/configdialog.h @@ -0,0 +1,50 @@ +/* + Copyright (c) 2016 Stefan Stäglich + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +#ifndef CONFIGDIALOG_H +#define CONFIGDIALOG_H + +#include + +class KConfigDialogManager; + +class Settings; + +namespace Ui { +class ConfigDialog; +} + +class ConfigDialog : public QDialog +{ + Q_OBJECT + +public: + explicit ConfigDialog(Settings* settings, QWidget *parent = Q_NULLPTR); + ~ConfigDialog(); + + void saveSettings(); + +private: + Ui::ConfigDialog *ui; + + KConfigDialogManager *mManager; + Settings *mSettings; +}; + +#endif // CONFIGDIALOG_H Index: resources/tomboynotes/configdialog.cpp =================================================================== --- /dev/null +++ resources/tomboynotes/configdialog.cpp @@ -0,0 +1,79 @@ +/* + Copyright (c) 2016 Stefan Stäglich + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +#include +#include "configdialog.h" +#include "settings.h" +#include "ui_configdialog.h" + +ConfigDialog::ConfigDialog(Settings* settings, QWidget *parent) : + QDialog(parent), + ui(new Ui::ConfigDialog), + mSettings(settings) +{ + // Create window + setWindowTitle(i18n("Select a tomboy server")); + QWidget *mainWidget = new QWidget(this); + QVBoxLayout *mainLayout = new QVBoxLayout; + setLayout(mainLayout); + mainLayout->addWidget(mainWidget); + ui->setupUi(mainWidget); + + // KSettings stuff. Works not in the initialization list! + mManager = new KConfigDialogManager(this, settings); + mManager->updateWidgets(); + + // Set the button actions + connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &ConfigDialog::accept); + connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &ConfigDialog::reject); + + // Load config dialog window size settings + KConfigGroup group(KSharedConfig::openConfig(), "ConfigDialog"); + const QSize size = group.readEntry("Size", QSize(600, 400)); + if (size.isValid()) { + resize(size); + } + + ui->kcfg_ServerURL->setReadOnly(!mSettings->requestToken().isEmpty()); +} + +ConfigDialog::~ConfigDialog() +{ + // Save config dialog window size + KConfigGroup group(KSharedConfig::openConfig(), "ConfigDialog"); + group.writeEntry("Size", size()); + group.sync(); + + delete ui; +} + +void ConfigDialog::saveSettings() +{ + if ( ui->kcfg_ServerURL->text() != mSettings->serverURL() ) { + mSettings->setRequestToken(QString()); + mSettings->setRequestTokenSecret(QString()); + } + + if(ui->kcfg_collectionName->text().isEmpty()) { + ui->kcfg_collectionName->setText("Tomboy Notes " + Settings::serverURL()); + } + + mManager->updateSettings(); + mSettings->save(); +} Index: resources/tomboynotes/configdialog.ui =================================================================== --- /dev/null +++ resources/tomboynotes/configdialog.ui @@ -0,0 +1,123 @@ + + + Till Adam <adam@kde.org> + ConfigDialog + + + + 0 + 0 + 400 + 290 + + + + Tomboy Server Settings + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + 0 + + + + Tomboy + + + + + + Display name: + + + + + + + + + + Type in the server URL: + + + + + + + This value is not changeable after first setup + + + + + + + Open in read-only mode + + + + + + + Ignore SSL errors + + + + + + + Qt::Vertical + + + + 20 + 141 + + + + + + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + + + + + + true + + + + + + + + KLineEdit + QLineEdit +
klineedit.h
+
+
+ + +
Index: resources/tomboynotes/o1tomboy.h =================================================================== --- /dev/null +++ resources/tomboynotes/o1tomboy.h @@ -0,0 +1,38 @@ +/* + Copyright (c) 2016 Stefan Stäglich + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +#ifndef O1TOMBOY_H +#define O1TOMBOY_H + +#include "o1.h" + +class O1Tomboy : public O1 { + Q_OBJECT +public: + explicit O1Tomboy(QObject *parent = Q_NULLPTR); + + void setBaseURL(const QString &value); + QString getRequestToken() const; + + QString getRequestTokenSecret() const; + + void restoreAuthData; +}; + +#endif // O1TOMBOY_H Index: resources/tomboynotes/o1tomboy.cpp =================================================================== --- /dev/null +++ resources/tomboynotes/o1tomboy.cpp @@ -0,0 +1,49 @@ +/* + Copyright (c) 2016 Stefan Stäglich + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +O1Tomboy::O1Tomboy(QObject *parent) : O1(parent) +{ + +} + +void O1Tomboy::setBaseURL(const QString &value) +{ + setRequestTokenUrl(QUrl(value + QStringLiteral("/oauth/request_token"))); + setAuthorizeUrl(QUrl(value + QStringLiteral("/oauth/authorize"))); + setAccessTokenUrl(QUrl(value + QStringLiteral("/oauth/access_token"))); + setClientId("anyone"); + setClientSecret("anyone"); +} + +QString O1Tomboy::getRequestToken() const +{ + return requestToken_; +} + +QString O1Tomboy::getRequestTokenSecret() const +{ + return requestTokenSecret_; +} + +void O1Tomboy::restoreAuthData(const QString &token, const QString &secret) +{ + requestToken_ = token; + requestTokenSecret_ = secret; + setLinked(true); +} Index: resources/tomboynotes/settings.kcfgc =================================================================== --- /dev/null +++ resources/tomboynotes/settings.kcfgc @@ -0,0 +1,8 @@ +File=tomboynotesresource.kcfg +ClassName=Settings +Mutators=true +ItemAccessors=true +SetUserTexts=true +Singleton=true +#IncludeFiles= +GlobalEnums=true Index: resources/tomboynotes/tomboycollectionsdownloadjob.h =================================================================== --- /dev/null +++ resources/tomboynotes/tomboycollectionsdownloadjob.h @@ -0,0 +1,47 @@ +/* + Copyright (c) 2016 Stefan Stäglich + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +#ifndef TOMBOYCOLLECTIONSDOWNLOADJOB_H +#define TOMBOYCOLLECTIONSDOWNLOADJOB_H + +#include +#include "tomboyjobbase.h" + +class TomboyCollectionsDownloadJob : public TomboyJobBase +{ + Q_OBJECT +public: + // ctor + explicit TomboyCollectionsDownloadJob(const QString &collectionName, KIO::AccessManager *manager, QObject *parent = Q_NULLPTR); + // returns the parsed results wrapped in Akonadi::Collection::List, see bellow + Akonadi::Collection::List collections() const; + + // automatically called by KJob + void start() Q_DECL_OVERRIDE; + +private Q_SLOTS: + // This will be called once the request is finished. + void onRequestFinished(); + +private: + Akonadi::Collection::List mResultCollections; + QString mCollectionName; +}; + +#endif // TOMBOYCOLLECTIONSDOWNLOADJOB_H Index: resources/tomboynotes/tomboycollectionsdownloadjob.cpp =================================================================== --- /dev/null +++ resources/tomboynotes/tomboycollectionsdownloadjob.cpp @@ -0,0 +1,80 @@ +/* + Copyright (c) 2016 Stefan Stäglich + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +#include +#include +#include +#include +#include +#include "debug.h" +#include "tomboycollectionsdownloadjob.h" + +TomboyCollectionsDownloadJob::TomboyCollectionsDownloadJob(const QString &collectionName, KIO::AccessManager *manager, QObject *parent) + : TomboyJobBase(manager, parent), + mCollectionName(collectionName) +{ +} + +Akonadi::Collection::List TomboyCollectionsDownloadJob::collections() const +{ + return mResultCollections; +} + +void TomboyCollectionsDownloadJob::start() +{ + // Get user informations + QNetworkRequest request(mContentURL); + mReply = mRequestor->get(request, QList()); + + connect(mReply, &QNetworkReply::finished, this, &TomboyCollectionsDownloadJob::onRequestFinished); + qCDebug(log_tomboynotesresource) << "TomboyCollectionsDownloadJob: Start network request"; +} + +void TomboyCollectionsDownloadJob::onRequestFinished() +{ + qCDebug(log_tomboynotesresource) << "TomboyCollectionsDownloadJob: Network request finished"; + checkReplyError(); + if (error() != TomboyJobError::NoError) + { + setErrorText(mReply->errorString()); + emitResult(); + return; + } + + // Parse received data as JSON + const QJsonDocument document = QJsonDocument::fromJson(mReply->readAll(), Q_NULLPTR); + + const QJsonObject jo = document.object(); + qCDebug(log_tomboynotesresource) << "TomboyCollectionsDownloadJob: " << jo; + const QJsonValue collectionRevision = jo[QLatin1String("latest-sync-revision")]; + qCDebug(log_tomboynotesresource) << "TomboyCollectionsDownloadJob: " << collectionRevision; + + Akonadi::Collection c; + c.setParentCollection( Akonadi::Collection::root()); + c.setRemoteId(mContentURL); + c.setName(mCollectionName); + c.setRemoteRevision(QString::number(collectionRevision.toInt())); + qCDebug(log_tomboynotesresource) << "TomboyCollectionsDownloadJob: Sync revision " << collectionRevision.toString(); + + c.setContentMimeTypes({ Akonadi::NoteUtils::noteMimeType() }); + + mResultCollections << c; + + emitResult(); +} Index: resources/tomboynotes/tomboyitemdownloadjob.h =================================================================== --- /dev/null +++ resources/tomboynotes/tomboyitemdownloadjob.h @@ -0,0 +1,45 @@ +/* + Copyright (c) 2016 Stefan Stäglich + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +#ifndef TOMBOYITEMDOWNLOADJOB_H +#define TOMBOYITEMDOWNLOADJOB_H + +#include +#include "tomboyjobbase.h" + +class TomboyItemDownloadJob : public TomboyJobBase +{ + Q_OBJECT +public: + explicit TomboyItemDownloadJob(const Akonadi::Item &item, KIO::AccessManager *manager, QObject *parent = Q_NULLPTR); + // returns the parsed results wrapped in Akonadi::Item, see bellow + Akonadi::Item item() const; + + // automatically called by KJob + void start() Q_DECL_OVERRIDE; + +private Q_SLOTS: + // This will be called once the request is finished. + void onRequestFinished(); + +private: + Akonadi::Item mResultItem; +}; + +#endif // TOMBOYITEMDOWNLOADJOB_H Index: resources/tomboynotes/tomboyitemdownloadjob.cpp =================================================================== --- /dev/null +++ resources/tomboynotes/tomboyitemdownloadjob.cpp @@ -0,0 +1,89 @@ +/* + Copyright (c) 2016 Stefan Stäglich + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +#include +#include +#include +#include +#include "debug.h" +#include "tomboyitemdownloadjob.h" + +TomboyItemDownloadJob::TomboyItemDownloadJob(const Akonadi::Item &item, KIO::AccessManager *manager, QObject *parent) + : TomboyJobBase(manager, parent), + mResultItem(item) +{ +} + +Akonadi::Item TomboyItemDownloadJob::item() const +{ + return mResultItem; +} + +void TomboyItemDownloadJob::start() +{ + // Get the speicific note + mContentURL.chop(1); + QNetworkRequest request(mContentURL + QLatin1String("/") + mResultItem.remoteId()); + mReply = mRequestor->get(request, QList()); + + connect(mReply, &QNetworkReply::finished, this, &TomboyItemDownloadJob::onRequestFinished); + qCDebug(log_tomboynotesresource) << "TomboyItemDownloadJob: Start network request"; +} + +void TomboyItemDownloadJob::onRequestFinished() +{ + checkReplyError(); + if (error() != TomboyJobError::NoError) { + setErrorText(mReply->errorString()); + emitResult(); + return; + } + qCDebug(log_tomboynotesresource) << "TomboyItemDownloadJob: Network request finished. No error occured"; + + // Parse received data as JSON + const QJsonDocument document = QJsonDocument::fromJson(mReply->readAll(), Q_NULLPTR); + + const QJsonObject jsonNote = document.object(); + + qCDebug(log_tomboynotesresource) << "TomboyItemDownloadJob: JSON note: " << jsonNote; + + mResultItem.setRemoteRevision(QString::number(jsonNote[QLatin1String("last-sync-revision")].toInt())); + qCDebug(log_tomboynotesresource) << "TomboyItemDownloadJob: Sync revision " << mResultItem.remoteRevision(); + + // Set timestamp + const QString timeStampJson = jsonNote[QLatin1String("last-change-date")].toString(); + const QDateTime modificationTime = QDateTime::fromString(timeStampJson, Qt::ISODate); + mResultItem.setModificationTime(modificationTime); + + // Set note title + auto akonadiNote = KMime::Message::Ptr::create(); + akonadiNote->subject(true)->fromUnicodeString(jsonNote[QLatin1String("title")].toString().toUtf8(), "utf-8"); + + // Set note content + akonadiNote->contentType()->setMimeType("text/html"); + akonadiNote->contentType()->setCharset("utf-8"); + akonadiNote->contentTransferEncoding(true)->setEncoding(KMime::Headers::CEquPr); + akonadiNote->mainBodyPart()->fromUnicodeString(jsonNote[QLatin1String("note-content")].toString().toUtf8()); + + // Add title and content to Akonadi::Item + akonadiNote->assemble(); + mResultItem.setPayload(akonadiNote); + + emitResult(); +} Index: resources/tomboynotes/tomboyitemsdownloadjob.h =================================================================== --- /dev/null +++ resources/tomboynotes/tomboyitemsdownloadjob.h @@ -0,0 +1,47 @@ +/* + Copyright (c) 2016 Stefan Stäglich + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +#ifndef TOMBOYITEMSDOWNLOADJOB_H +#define TOMBOYITEMSDOWNLOADJOB_H + +#include +#include "tomboyjobbase.h" + +class TomboyItemsDownloadJob : public TomboyJobBase +{ + Q_OBJECT +public: + // ctor + explicit TomboyItemsDownloadJob(const Akonadi::Collection::Id &id, KIO::AccessManager *manager, QObject *parent = Q_NULLPTR); + // returns the parsed results wrapped in Akonadi::Item::List, see bellow + Akonadi::Item::List items() const; + + // automatically called by KJob + void start() Q_DECL_OVERRIDE; + +private Q_SLOTS: + // This will be called once the request is finished. + void onRequestFinished(); + +private: + Akonadi::Collection::Id mCollectionId; + Akonadi::Item::List mResultItems; +}; + +#endif // TOMBOYITEMSDOWNLOADJOB_H Index: resources/tomboynotes/tomboyitemsdownloadjob.cpp =================================================================== --- /dev/null +++ resources/tomboynotes/tomboyitemsdownloadjob.cpp @@ -0,0 +1,74 @@ +/* + Copyright (c) 2016 Stefan Stäglich + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +#include +#include +#include +#include +#include "debug.h" +#include "tomboyitemsdownloadjob.h" + + +TomboyItemsDownloadJob::TomboyItemsDownloadJob(const Akonadi::Collection::Id &id, KIO::AccessManager *manager, QObject *parent) + : TomboyJobBase(manager, parent), + mCollectionId(id) +{ +} + +Akonadi::Item::List TomboyItemsDownloadJob::items() const +{ + return mResultItems; +} + +void TomboyItemsDownloadJob::start() +{ + // Get all notes + QNetworkRequest request(mContentURL); + mReply = mRequestor->get(request, QList()); + + connect(mReply, &QNetworkReply::finished, this, &TomboyItemsDownloadJob::onRequestFinished); + qCDebug(log_tomboynotesresource) << "TomboyItemsDownloadJob: Start network request"; +} + +void TomboyItemsDownloadJob::onRequestFinished() +{ + qCDebug(log_tomboynotesresource) << "TomboyItemsDownloadJob: Network request finished"; + checkReplyError(); + if (error() != TomboyJobError::NoError) { + setErrorText(mReply->errorString()); + emitResult(); + return; + } + + // Parse received data as JSON + const QJsonDocument document = QJsonDocument::fromJson(mReply->readAll(), Q_NULLPTR); + + const QJsonObject jo = document.object(); + const QJsonArray notes = jo[QLatin1String("notes")].toArray(); + + Q_FOREACH (const auto ¬e, notes) { + Akonadi::Item item(Akonadi::NoteUtils::noteMimeType()); + item.setRemoteId(note.toObject()[QLatin1String("guid")].toString()); + mResultItems << item; + qCDebug(log_tomboynotesresource) << "TomboyItemsDownloadJob: Retriving note with id" << item.remoteId(); + } + + setError(TomboyJobError::NoError); + emitResult(); +} Index: resources/tomboynotes/tomboyitemuploadjob.h =================================================================== --- /dev/null +++ resources/tomboynotes/tomboyitemuploadjob.h @@ -0,0 +1,64 @@ +/* + Copyright (c) 2016 Stefan Stäglich + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +#ifndef TOMBOYITEMUPLOADJOB_H +#define TOMBOYITEMUPLOADJOB_H + +#include +#include +#include "tomboyjobbase.h" + +enum class JobType +{ + AddItem, + ModifyItem, + DeleteItem +}; + +class TomboyItemUploadJob : public TomboyJobBase +{ + Q_OBJECT +public: + TomboyItemUploadJob(const Akonadi::Item &item, JobType jobType, KIO::AccessManager *manager, QObject *parent = Q_NULLPTR); + + // Returns mSourceItem for post-processing purposes + Akonadi::Item item() const; + + JobType jobType() const; + + // automatically called by KJob + void start() Q_DECL_OVERRIDE; + +private Q_SLOTS: + // This will be called once the request is finished. + void onRequestFinished(); + +private: + // Workaround for https://bugreports.qt-project.org/browse/QTBUG-26161 Qt bug + QString getCurrentISOTime() const; + + Akonadi::Item mSourceItem; + KMime::Message::Ptr mNoteContent; + + JobType mJobType; + + int mRemoteRevision; +}; + +#endif // TOMBOYITEMUPLOADJOB_H Index: resources/tomboynotes/tomboyitemuploadjob.cpp =================================================================== --- /dev/null +++ resources/tomboynotes/tomboyitemuploadjob.cpp @@ -0,0 +1,143 @@ +/* + Copyright (c) 2016 Stefan Stäglich + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +#include +#include +#include +#include +#include +#include "debug.h" +#include "tomboyitemuploadjob.h" + +TomboyItemUploadJob::TomboyItemUploadJob(const Akonadi::Item &item, JobType jobType, KIO::AccessManager *manager, QObject *parent) + : TomboyJobBase(manager, parent), + mSourceItem(item), + mJobType(jobType) +{ + mSourceItem = Akonadi::Item(item); + if (item.hasPayload()) { + mNoteContent = item.payload(); + } + + mRemoteRevision = item.parentCollection().remoteRevision().toInt(); + + // Create random remote id if adding new item + if (jobType == JobType::AddItem) { + mSourceItem.setRemoteId(QUuid::createUuid().toString()); + } +} + +Akonadi::Item TomboyItemUploadJob::item() const +{ + return mSourceItem; +} + +JobType TomboyItemUploadJob::jobType() const +{ + return mJobType; +} + +void TomboyItemUploadJob::start() +{ + // Convert note to JSON + QJsonObject jsonNote; + jsonNote["guid"] = mSourceItem.remoteId(); + switch (mJobType) { + case JobType::DeleteItem: + jsonNote[QLatin1String("command")] = QStringLiteral("delete"); + break; + case JobType::AddItem: + jsonNote["create-date"] = getCurrentISOTime(); + // Missing break is intended + case JobType::ModifyItem: + jsonNote[QLatin1String("title")] = mNoteContent->headerByType("subject")->asUnicodeString(); + jsonNote[QLatin1String("note-content")] = mNoteContent->mainBodyPart()->decodedText(); + jsonNote[QLatin1String("note-content-version")] = QStringLiteral("1"); + jsonNote[QLatin1String("last-change-date")] = getCurrentISOTime(); + } + + // Create the full JSON object + QJsonArray noteChanges; + noteChanges.append(jsonNote); + QJsonObject postJson; + postJson[QLatin1String("note-changes")] = noteChanges; + postJson[QLatin1String("latest-sync-revision")] = QString::number(++mRemoteRevision); + QJsonDocument postData; + postData.setObject(postJson); + + // Network request + QNetworkRequest request(mContentURL); + request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json; boundary=7d44e178b0439"); + request.setHeader(QNetworkRequest::ContentLengthHeader, postData.toJson().length()); + mReply = mRequestor->put(request, QList(), postData.toJson()); + connect(mReply, &QNetworkReply::finished, this, &TomboyItemUploadJob::onRequestFinished); + qCDebug(log_tomboynotesresource) << "TomboyItemUploadJob: Start network request"; +} + +void TomboyItemUploadJob::onRequestFinished() +{ + checkReplyError(); + if (error() != TomboyJobError::NoError) { + setErrorText(mReply->errorString()); + emitResult(); + return; + } + qCDebug(log_tomboynotesresource) << "TomboyItemUploadJob: Network request finished. No error occured"; + + // Parse received data as JSON + const QJsonDocument document = QJsonDocument::fromJson(mReply->readAll(), Q_NULLPTR); + + const QJsonObject jo = document.object(); + const QJsonArray notes = jo[QLatin1String("notes")].toArray(); + + // Check if server status is as expected + bool found = false; + Q_FOREACH (const auto note, notes) { + found = (note.toObject()[QLatin1String("guid")].toString() == mSourceItem.remoteId()); + if (found) { + break; + } + } + if (mJobType == JobType::DeleteItem && found) { + setError(TomboyJobError::PermanentError); + setErrorText(i18n("Sync error. Server status not as expected!")); + emitResult(); + return; + } + if (mJobType != JobType::DeleteItem && !found) { + setError(TomboyJobError::PermanentError); + setErrorText(i18n("Sync error. Server status not as expected!")); + emitResult(); + return; + } + + setError(TomboyJobError::NoError); + emitResult(); +} + +QString TomboyItemUploadJob::getCurrentISOTime() const +{ + QDateTime local = QDateTime::currentDateTime(); + QDateTime utc = local.toUTC(); + utc.setTimeSpec(Qt::LocalTime); + int utcOffset = utc.secsTo(local); + local.setUtcOffset(utcOffset); + + return local.toString(Qt::ISODate); +} Index: resources/tomboynotes/tomboyjobbase.h =================================================================== --- /dev/null +++ resources/tomboynotes/tomboyjobbase.h @@ -0,0 +1,57 @@ +/* + Copyright (c) 2016 Stefan Stäglich + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +#ifndef TOMBOYJOBBASE_H +#define TOMBOYJOBBASE_H + +#include +#include +#include +#include "o1tomboy.h" +#include "o1requestor.h" + +enum TomboyJobError +{ + NoError, + TemporaryError, + PermanentError +}; + +class TomboyJobBase : public KCompositeJob +{ + Q_OBJECT +public: + explicit TomboyJobBase(KIO::AccessManager *manager, QObject *parent = Q_NULLPTR); + + void setServerURL(const QString &apiurl, const QString &contenturl); + void setAuthentication(const QString &token, const QString &secret); + +protected: + KIO::Integration::AccessManager *mManager; + O1Requestor *mRequestor; + O1Tomboy *mO1; + QNetworkReply *mReply; + + QString mApiURL; + QString mContentURL; + + void checkReplyError(); +}; + +#endif // TOMBOYJOBBASE_H Index: resources/tomboynotes/tomboyjobbase.cpp =================================================================== --- /dev/null +++ resources/tomboynotes/tomboyjobbase.cpp @@ -0,0 +1,59 @@ +/* + Copyright (c) 2016 Stefan Stäglich + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +#include "tomboyjobbase.h" + +TomboyJobBase::TomboyJobBase(KIO::AccessManager *manager, QObject *parent) + : KCompositeJob(parent), + mO1(new O1Tomboy(this)), + mManager(manager), + mReply(Q_NULLPTR) + +{ + mRequestor = new O1Requestor(mManager, mO1, this); +} + +void TomboyJobBase::setServerURL(const QString &apiurl, const QString &contenturl) +{ + mO1->setBaseURL(apiurl); + mApiURL = apiurl + QStringLiteral("/api/1.0"); + mContentURL = contenturl; +} + +void TomboyJobBase::setAuthentication(const QString &token, const QString &secret) +{ + mO1->restoreAuthData(token, secret); +} + +void TomboyJobBase::checkReplyError() +{ + switch (mReply->error()) { + case QNetworkReply::NoError : + setError(TomboyJobError::NoError); + break; + case QNetworkReply::RemoteHostClosedError: + case QNetworkReply::HostNotFoundError: + case QNetworkReply::TimeoutError: + setError(TomboyJobError::TemporaryError); + break; + default: + setError(TomboyJobError::PermanentError); + break; + } +} Index: resources/tomboynotes/tomboynotesresource.h =================================================================== --- /dev/null +++ resources/tomboynotes/tomboynotesresource.h @@ -0,0 +1,81 @@ +/* + Copyright (c) 2016 Stefan Stäglich + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +#ifndef TOMBOYNOTESRESOURCE_H +#define TOMBOYNOTESRESOURCE_H + + +#include +#include +#include +#include "o1tomboy.h" +#include "o1requestor.h" + +class TomboyNotesResource : public Akonadi::ResourceBase, + public Akonadi::AgentBase::Observer +{ + Q_OBJECT + +public: + TomboyNotesResource(const QString &id); + ~TomboyNotesResource(); + +public Q_SLOTS: + void configure(WId windowId) Q_DECL_OVERRIDE; + +protected Q_SLOTS: + // Standard akonadi slots + void retrieveCollections() Q_DECL_OVERRIDE; + void retrieveItems(const Akonadi::Collection &col) Q_DECL_OVERRIDE; + bool retrieveItem(const Akonadi::Item &item, const QSet &parts) Q_DECL_OVERRIDE; + + // Slots for Job result handling + void onAuthorizationFinished(KJob *kjob); + void onCollectionsRetrieved(KJob *kjob); + void onItemChangeCommitted(KJob *kjob); + void onItemRetrieved(KJob *kjob); + void onItemsRetrieved(KJob *kjob); + +private Q_SLOTS: + // SSL error handling + void onSslError(QNetworkReply *reply, const QList &errors); + +protected: + void aboutToQuit() Q_DECL_OVERRIDE; + + // Standard akonadi + void itemAdded(const Akonadi::Item &item, const Akonadi::Collection &collection) Q_DECL_OVERRIDE; + void itemChanged(const Akonadi::Item &item, const QSet &parts) Q_DECL_OVERRIDE; + void itemRemoved(const Akonadi::Item &item) Q_DECL_OVERRIDE; + +private: + bool configurationNotValid(); + + void retryAfterFailure(const QString &errorMessage); + // Status handling + void showError(const QString &errorText); + QTimer *mStatusMessageTimer; + + // Only one UploadJob should run per time + bool mUploadJobProcessRunning; + + KIO::AccessManager *mManager; +}; + +#endif Index: resources/tomboynotes/tomboynotesresource.cpp =================================================================== --- /dev/null +++ resources/tomboynotes/tomboynotesresource.cpp @@ -0,0 +1,331 @@ +/* + Copyright (c) 2016 Stefan Stäglich + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +#include +#include +#include +#include +#include +#include +#include +#include "debug.h" +#include "configdialog.h" +#include "settings.h" +#include "settingsadaptor.h" +#include "tomboyserverauthenticatejob.h" +#include "tomboycollectionsdownloadjob.h" +#include "tomboyitemdownloadjob.h" +#include "tomboyitemsdownloadjob.h" +#include "tomboyitemuploadjob.h" +#include "tomboynotesresource.h" + +using namespace Akonadi; + +TomboyNotesResource::TomboyNotesResource(const QString &id) + : ResourceBase(id) +{ + new SettingsAdaptor(Settings::self()); + QDBusConnection::sessionBus().registerObject(QStringLiteral("/Settings"), + Settings::self(), + QDBusConnection::ExportAdaptors); + + // Akonadi:Item should always provide the payload + changeRecorder()->itemFetchScope().fetchFullPayload(true); + + // Status message stuff + mStatusMessageTimer = new QTimer(this); + mStatusMessageTimer->setSingleShot(true); + connect(mStatusMessageTimer, &QTimer::timeout, [=]() { Q_EMIT status(Akonadi::AgentBase::Idle, QString()); }); + connect(this, &AgentBase::error, this, &TomboyNotesResource::showError); + + mUploadJobProcessRunning = false; + + mManager = new KIO::AccessManager(this); + connect(mManager, &KIO::AccessManager::sslErrors, this, &TomboyNotesResource::onSslError); + + qCDebug(log_tomboynotesresource) << "Resource started"; +} + +TomboyNotesResource::~TomboyNotesResource() +{ +} + +void TomboyNotesResource::retrieveCollections() +{ + qCDebug(log_tomboynotesresource) << "Retriving collections started"; + + if (configurationNotValid()) { + cancelTask(i18n("Resource configuration is not valid")); + return; + } + + auto job = new TomboyCollectionsDownloadJob(Settings::collectionName(), mManager, this); + job->setAuthentication(Settings::requestToken(), Settings::requestTokenSecret()); + job->setServerURL(Settings::serverURL(), Settings::userURL()); + // connect to its result() signal + connect(job, &KJob::result, this, &TomboyNotesResource::onCollectionsRetrieved); + job->start(); + qCDebug(log_tomboynotesresource) << "Retriving collections job started"; +} + + +void TomboyNotesResource::retrieveItems(const Akonadi::Collection &collection) +{ + if (configurationNotValid()) { + cancelTask(i18n("Resource configuration is not valid")); + return; + } + + // create the job + auto job = new TomboyItemsDownloadJob(collection.id(), mManager, this); + job->setAuthentication(Settings::requestToken(), Settings::requestTokenSecret()); + job->setServerURL(Settings::serverURL(), Settings::contentURL()); + // connect to its result() signal + connect(job, &KJob::result, this, &TomboyNotesResource::onItemsRetrieved); + job->start(); + qCDebug(log_tomboynotesresource) << "Retriving items job started"; +} + +bool TomboyNotesResource::retrieveItem(const Akonadi::Item &item, const QSet &parts) +{ + Q_UNUSED( parts ); + + if (configurationNotValid()) { + cancelTask(i18n("Resource configuration is not valid")); + return false; + } + + // this method is called when Akonadi wants more data for a given item. + auto job = new TomboyItemDownloadJob(item, mManager, this); + job->setAuthentication(Settings::requestToken(), Settings::requestTokenSecret()); + job->setServerURL(Settings::serverURL(), Settings::contentURL()); + // connect to its result() signal + connect(job, &KJob::result, this, &TomboyNotesResource::onItemRetrieved); + job->start(); + qCDebug(log_tomboynotesresource) << "Retriving item data job started"; + + return true; +} + +void TomboyNotesResource::onAuthorizationFinished(KJob *kjob) +{ + // Saves the received client authentication data in the settings + qCDebug(log_tomboynotesresource) << "Authorization job finished"; + auto job = qobject_cast(kjob); + if (job->error() == TomboyJobError::NoError) { + Settings::setRequestToken(job->getRequestToken()); + Settings::setRequestTokenSecret(job->getRequestTokenSecret()); + Settings::setContentURL(job->getContentUrl()); + Settings::setUserURL(job->getUserURL()); + Settings::self()->save(); + synchronizeCollectionTree(); + synchronize(); + } + else + { + showError(job->errorText()); + } +} + +void TomboyNotesResource::onCollectionsRetrieved(KJob *kjob) +{ + auto job = qobject_cast(kjob); + if (job->error() != TomboyJobError::NoError) { + cancelTask(); + showError(job->errorText()); + return; + } + + collectionsRetrieved(job->collections()); +} + +void TomboyNotesResource::onItemChangeCommitted(KJob *kjob) +{ + auto job = qobject_cast(kjob); + mUploadJobProcessRunning = false; + switch (job->error()) { + case TomboyJobError::PermanentError: + cancelTask(); + showError(job->errorText()); + return; + case TomboyJobError::TemporaryError: + retryAfterFailure(job->errorString()); + return; + case TomboyJobError::NoError: + changeCommitted(job->item()); + // The data should be actualized for the next UploadJob + synchronize(); + return; + } +} + +void TomboyNotesResource::onItemRetrieved(KJob *kjob) +{ + auto job = qobject_cast(kjob); + + if (job->error() != TomboyJobError::NoError) { + cancelTask(); + showError(job->errorText()); + return; + } + + itemRetrieved(job->item()); + qCDebug(log_tomboynotesresource) << "Retriving item data job with remoteId " << job->item().remoteId() << " finished"; +} + +void TomboyNotesResource::onItemsRetrieved(KJob *kjob) +{ + auto job = qobject_cast(kjob); + if (job->error() != TomboyJobError::NoError) { + cancelTask(); + showError(job->errorText()); + return; + } + + itemsRetrieved(job->items()); + qCDebug(log_tomboynotesresource) << "Retriving items job finished"; +} + +void TomboyNotesResource::onSslError(QNetworkReply *reply, const QList &errors) +{ + if (Settings::ignoreSslErrors()) { + reply->ignoreSslErrors(); + } +} + +void TomboyNotesResource::aboutToQuit() +{ + // TODO: any cleanup you need to do while there is still an active + // event loop. The resource will terminate after this method returns +} + +void TomboyNotesResource::configure(WId windowId) +{ + qCDebug(log_tomboynotesresource) << "Resource configuration started"; + + ConfigDialog dialog(Settings::self()); + + if (windowId) { + KWindowSystem::setMainWindow(&dialog, windowId); + } + + // Run the configuration dialog an save settings if accepted + if (dialog.exec() != QDialog::Accepted) { + return; + } + + dialog.saveSettings(); + setAgentName(Settings::collectionName()); + + if (configurationNotValid()) { + auto job = new TomboyServerAuthenticateJob(mManager, this); + job->setServerURL(Settings::serverURL(), QString()); + connect(job, &KJob::result, this, &TomboyNotesResource::onAuthorizationFinished); + job->start(); + qCDebug(log_tomboynotesresource) << "Authorization job started"; + } + else + { + synchronize(); + } +} + +void TomboyNotesResource::itemAdded(const Akonadi::Item &item, const Akonadi::Collection &collection) +{ + Q_UNUSED( collection ); + if (Settings::readOnly() || configurationNotValid()) { + cancelTask(i18n("Resource is read-only")); + return; + } + + if (mUploadJobProcessRunning) { + retryAfterFailure(QString()); + return; + } + + auto job = new TomboyItemUploadJob(item, JobType::AddItem, mManager, this); + job->setAuthentication(Settings::requestToken(), Settings::requestTokenSecret()); + job->setServerURL(Settings::serverURL(), Settings::contentURL()); + connect(job, &KJob::result, this, &TomboyNotesResource::onItemChangeCommitted); + mUploadJobProcessRunning = true; + job->start(); +} + +void TomboyNotesResource::itemChanged(const Akonadi::Item &item, const QSet &parts) +{ + Q_UNUSED( parts ); + if (Settings::readOnly() || configurationNotValid()) { + cancelTask(i18n("Resource is read-only")); + return; + } + + if (mUploadJobProcessRunning) { + retryAfterFailure(QString()); + return; + } + + auto job = new TomboyItemUploadJob(item, JobType::ModifyItem, mManager, this); + job->setAuthentication(Settings::requestToken(), Settings::requestTokenSecret()); + job->setServerURL(Settings::serverURL(), Settings::contentURL()); + connect(job, &KJob::result, this, &TomboyNotesResource::onItemChangeCommitted); + mUploadJobProcessRunning = true; + job->start(); +} + +void TomboyNotesResource::itemRemoved(const Akonadi::Item &item) +{ + if (Settings::readOnly() || configurationNotValid()) { + cancelTask(i18n("Resource is read-only")); + return; + } + + if (mUploadJobProcessRunning) { + retryAfterFailure(QString()); + return; + } + + auto job = new TomboyItemUploadJob(item, JobType::DeleteItem, mManager, this); + job->setAuthentication(Settings::requestToken(), Settings::requestTokenSecret()); + job->setServerURL(Settings::serverURL(), Settings::contentURL()); + connect(job, &KJob::result, this, &TomboyNotesResource::onItemChangeCommitted); + mUploadJobProcessRunning = true; + job->start(); +} + +bool TomboyNotesResource::configurationNotValid() +{ + return Settings::requestToken().isEmpty() || Settings::requestToken().isEmpty() || Settings::contentURL().isEmpty(); +} + +void TomboyNotesResource::retryAfterFailure(const QString &errorMessage) +{ + Q_EMIT status(Broken, errorMessage); + deferTask(); + setTemporaryOffline(Settings::self()->refreshInterval() <= 0 ? 300 : Settings::self()->refreshInterval() * 60); +} + +void TomboyNotesResource::showError(const QString &errorText) +{ + Q_EMIT status(Akonadi::AgentBase::Idle, errorText); + mStatusMessageTimer->start(1000 * 10); +} + +AKONADI_RESOURCE_MAIN(TomboyNotesResource) + +#include "moc_tomboynotesresource.cpp" Index: resources/tomboynotes/tomboynotesresource.desktop =================================================================== --- /dev/null +++ resources/tomboynotes/tomboynotesresource.desktop @@ -0,0 +1,10 @@ +[Desktop Entry] +Name=Akonadi TomboyNotes Resource +Comment=Resource for tomboy compatible notes server +Type=AkonadiResource +Exec=akonadi_tomboynotes_resource +Icon=view-pim-notes + +X-Akonadi-MimeTypes=text/x-vnd.akonadi.note +X-Akonadi-Capabilities=Resource,Notes +X-Akonadi-Identifier=akonadi_tomboynotes_resource Index: resources/tomboynotes/tomboynotesresource.kcfg =================================================================== --- /dev/null +++ resources/tomboynotes/tomboynotesresource.kcfg @@ -0,0 +1,50 @@ + + + + + + + + + + + http://localhost/owncloud/index.php/apps/grauphel + + + + + + + + + + + + + + + + + + + + + + + + false + + + + false + + + + 5 + + + Index: resources/tomboynotes/tomboyserverauthenticatejob.h =================================================================== --- /dev/null +++ resources/tomboynotes/tomboyserverauthenticatejob.h @@ -0,0 +1,58 @@ +/* + Copyright (c) 2016 Stefan Stäglich + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +#ifndef TOMBOYSERVERAUTHENTICATEJOB_H +#define TOMBOYSERVERAUTHENTICATEJOB_H + +#include +#include +#include "tomboyjobbase.h" + + +class TomboyServerAuthenticateJob : public TomboyJobBase +{ + Q_OBJECT +public: + TomboyServerAuthenticateJob(KIO::AccessManager *manager, QObject *parent = Q_NULLPTR); + + ~TomboyServerAuthenticateJob(); + + QString getRequestToken() const; + QString getRequestTokenSecret() const; + QString getContentUrl() const; + QString getUserURL() const; + + void start() Q_DECL_OVERRIDE; + +private Q_SLOTS: + void onLinkingFailed(); + void onLinkingSucceeded(); + void onOpenBrowser(const QUrl &url); + + void onApiRequestFinished(); + void onUserRequestFinished(); + +private: + QString mUserURL; + + QWebEngineView *mWebView; + +}; + +#endif // TOMBOYSERVERAUTHENTICATEJOB_H Index: resources/tomboynotes/tomboyserverauthenticatejob.cpp =================================================================== --- /dev/null +++ resources/tomboynotes/tomboyserverauthenticatejob.cpp @@ -0,0 +1,128 @@ +/* + Copyright (c) 2016 Stefan Stäglich + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +#include +#include +#include +#include +#include "debug.h" +#include "tomboyserverauthenticatejob.h" + +TomboyServerAuthenticateJob::TomboyServerAuthenticateJob(KIO::AccessManager *manager, QObject *parent) + : TomboyJobBase(manager, parent), + mWebView(new QWebEngineView(Q_NULLPTR)) +{ + // Connect the o2 authenfication signals + connect(mO1, &O1::linkingFailed, this, &TomboyServerAuthenticateJob::onLinkingFailed); + connect(mO1, &O1::linkingSucceeded, this, &TomboyServerAuthenticateJob::onLinkingSucceeded); + connect(mO1, &O1::openBrowser, this, &TomboyServerAuthenticateJob::onOpenBrowser); + connect(mO1, &O1::closeBrowser, mWebView, &QWebEngineView::close); +} + +TomboyServerAuthenticateJob::~TomboyServerAuthenticateJob() +{ + delete mWebView; +} + +void TomboyServerAuthenticateJob::start() +{ + mO1->link(); +} + +QString TomboyServerAuthenticateJob::getRequestToken() const +{ + return mO1->getRequestToken(); +} + +QString TomboyServerAuthenticateJob::getRequestTokenSecret() const +{ + return mO1->getRequestTokenSecret(); +} + +QString TomboyServerAuthenticateJob::getContentUrl() const +{ + return mContentURL; +} + +QString TomboyServerAuthenticateJob::getUserURL() const +{ + return mUserURL; +} + +void TomboyServerAuthenticateJob::onLinkingFailed() +{ + setError(TomboyJobError::PermanentError); + setErrorText(i18n("Authorization has been failed! It could be an SSL error!")); + emitResult(); +} + +void TomboyServerAuthenticateJob::onLinkingSucceeded() +{ + QNetworkRequest request(mApiURL); + mReply = mRequestor->get(request, QList()); + + connect(mReply, &QNetworkReply::finished, this, &TomboyServerAuthenticateJob::onApiRequestFinished); + qCDebug(log_tomboynotesresource) << "TomboyServerAuthenticateJob: Start network request"; +} + +void TomboyServerAuthenticateJob::onOpenBrowser(const QUrl &url) +{ + mWebView->setUrl(url); + mWebView->show(); +} + +void TomboyServerAuthenticateJob::onApiRequestFinished() +{ + checkReplyError(); + if (error() != TomboyJobError::NoError) { + setErrorText(mReply->errorString()); + emitResult(); + return; + } + + // Parse received data as JSON and get user-href + const QJsonDocument document = QJsonDocument::fromJson(mReply->readAll(), Q_NULLPTR); + const QJsonObject jo = document.object(); + mUserURL = jo[QLatin1String("user-ref")].toObject()[QLatin1String("api-ref")].toString(); + + QNetworkRequest request(mUserURL); + mReply = mRequestor->get(request, QList()); + + connect(mReply, &QNetworkReply::finished, this, &TomboyServerAuthenticateJob::onUserRequestFinished); + qCDebug(log_tomboynotesresource) << "TomboyServerAuthenticateJob: Start network request"; +} + +void TomboyServerAuthenticateJob::onUserRequestFinished() +{ + checkReplyError(); + if (error() != TomboyJobError::NoError) + { + setErrorText(mReply->errorString()); + emitResult(); + return; + } + + // Parse received data as JSON and get contentURL + QJsonDocument document = QJsonDocument::fromJson(mReply->readAll(), Q_NULLPTR); + QJsonObject jo = document.object(); + mContentURL = jo[QLatin1String("notes-ref")].toObject()[QLatin1String("api-ref")].toString(); + + setError(TomboyJobError::NoError); + emitResult(); +}