nextcloud-desktop/src/libsync/account.h

187 lines
5.7 KiB
C
Raw Normal View History

2013-10-21 23:42:52 +04:00
/*
* Copyright (C) by Daniel Molkentin <danimo@owncloud.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program 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 General Public License
* for more details.
*/
#ifndef SERVERCONNECTION_H
#define SERVERCONNECTION_H
#include <QByteArray>
#include <QUrl>
#include <QNetworkCookie>
2013-10-23 16:48:44 +04:00
#include <QNetworkRequest>
#include <QSslCertificate>
#include <QSslConfiguration>
2013-10-24 02:29:08 +04:00
#include <QSslError>
#include "utility.h"
2013-10-21 23:42:52 +04:00
class QSettings;
class QNetworkReply;
class QUrl;
2013-10-23 16:48:44 +04:00
class QNetworkAccessManager;
2013-10-21 23:42:52 +04:00
2014-11-10 00:34:07 +03:00
namespace OCC {
2013-10-21 23:42:52 +04:00
class AbstractCredentials;
class Account;
class QuotaInfo;
2014-11-10 00:30:29 +03:00
class AccessManager;
2013-10-21 23:42:52 +04:00
class OWNCLOUDSYNC_EXPORT AccountManager : public QObject {
2013-11-05 21:15:47 +04:00
Q_OBJECT
2013-10-21 23:42:52 +04:00
public:
static AccountManager *instance();
2013-11-05 21:15:47 +04:00
~AccountManager() {}
2013-10-21 23:42:52 +04:00
void setAccount(Account *account);
2013-10-21 23:42:52 +04:00
Account *account() { return _account; }
2013-11-05 21:15:47 +04:00
Q_SIGNALS:
void accountChanged(Account *newAccount, Account *oldAccount);
void accountAboutToChange(Account *newAccount, Account *oldAccount);
2013-10-21 23:42:52 +04:00
private:
AccountManager() : _account(0) {}
2013-10-21 23:42:52 +04:00
Account *_account;
static AccountManager *_instance;
};
2013-10-24 02:29:08 +04:00
/* Reimplement this to handle SSL errors */
class AbstractSslErrorHandler {
public:
virtual ~AbstractSslErrorHandler() {}
2013-10-24 02:29:08 +04:00
virtual bool handleErrors(QList<QSslError>, QList<QSslCertificate>*, Account*) = 0;
};
2013-10-21 23:42:52 +04:00
/**
* @brief This class represents an account on an ownCloud Server
*/
class OWNCLOUDSYNC_EXPORT Account : public QObject {
2013-10-24 02:29:08 +04:00
Q_OBJECT
2013-10-21 23:42:52 +04:00
public:
enum State { Disconnected = 0, /// no network connection
Connected, /// account is online
SignedOut, /// Disconnected + credential token has been discarded
2014-12-03 16:59:53 +03:00
InvalidCredential /// The credentials are invalid and we are asking the user for them
};
2014-03-21 20:14:04 +04:00
QString davPath() const { return _davPath; }
void setDavPath(const QString&s) { _davPath = s; }
Account(AbstractSslErrorHandler *sslErrorHandler = 0, QObject *parent = 0);
~Account();
2013-10-21 23:42:52 +04:00
/**
* Saves the account to a given settings file
*/
void save();
2013-10-21 23:42:52 +04:00
/**
* Creates an account object from from a given settings file.
*/
static Account* restore();
2013-10-21 23:42:52 +04:00
/**
* @brief Checks the Account instance is different from \param other
*
* @returns true, if credentials or url have changed, false otherwise
*/
bool changed(Account *other, bool ignoreUrlProtocol) const;
2013-10-21 23:42:52 +04:00
/** Holds the accounts credentials */
AbstractCredentials* credentials() const;
void setCredentials(AbstractCredentials *cred);
/** Server url of the account */
void setUrl(const QUrl &url);
2013-10-23 16:48:44 +04:00
QUrl url() const { return _url; }
2013-10-21 23:42:52 +04:00
/** Returns webdav entry URL, based on url() */
QUrl davUrl() const;
/** set and retrieve the migration flag: if an account of a branded
* client was migrated from a former ownCloud Account, this is true
*/
void setMigrated(bool mig);
bool wasMigrated();
2013-10-21 23:42:52 +04:00
QList<QNetworkCookie> lastAuthCookies() const;
QNetworkReply* headRequest(const QString &relPath);
QNetworkReply* headRequest(const QUrl &url);
2013-10-21 23:42:52 +04:00
QNetworkReply* getRequest(const QString &relPath);
QNetworkReply* getRequest(const QUrl &url);
2013-10-23 16:48:44 +04:00
QNetworkReply* davRequest(const QByteArray &verb, const QString &relPath, QNetworkRequest req, QIODevice *data = 0);
QNetworkReply* davRequest(const QByteArray &verb, const QUrl &url, QNetworkRequest req, QIODevice *data = 0);
2013-10-21 23:42:52 +04:00
/** The ssl configuration during the first connection */
QSslConfiguration sslConfiguration() const { return _sslConfiguration; }
void setSslConfiguration(const QSslConfiguration &config);
2013-10-23 16:48:44 +04:00
/** The certificates of the account */
2013-10-24 02:29:08 +04:00
QList<QSslCertificate> approvedCerts() const { return _approvedCerts; }
void setApprovedCerts(const QList<QSslCertificate> certs);
void addApprovedCerts(const QList<QSslCertificate> certs);
2013-10-21 23:42:52 +04:00
// pluggable handler
2013-10-24 02:29:08 +04:00
void setSslErrorHandler(AbstractSslErrorHandler *handler);
2013-10-23 16:48:44 +04:00
// static helper function
2013-10-23 16:48:44 +04:00
static QUrl concatUrlPath(const QUrl &url, const QString &concatPath);
2014-03-26 20:08:34 +04:00
/** Returns a new settings pre-set in a specific group. The Settings will be created
with the given parent. If no parents is specified, the caller must destroy the settings */
static QSettings* settingsWithGroup(const QString &group, QObject *parent = 0);
// to be called by credentials only
QVariant credentialSetting(const QString& key) const;
void setCredentialSetting(const QString& key, const QVariant &value);
int state() const;
void setState(int state);
static QString stateString(int state);
void clearCookieJar();
QNetworkAccessManager* networkAccessManager();
QuotaInfo *quotaInfo();
signals:
void stateChanged(int state);
void propagatorNetworkActivity();
2013-11-05 21:15:47 +04:00
protected Q_SLOTS:
2013-10-24 02:29:08 +04:00
void slotHandleErrors(QNetworkReply*,QList<QSslError>);
2013-10-21 23:42:52 +04:00
private:
QMap<QString, QVariant> _settingsMap;
2013-10-21 23:42:52 +04:00
QUrl _url;
2013-10-24 02:29:08 +04:00
QList<QSslCertificate> _approvedCerts;
QSslConfiguration _sslConfiguration;
QScopedPointer<AbstractSslErrorHandler> _sslErrorHandler;
QuotaInfo *_quotaInfo;
QNetworkAccessManager *_am;
AbstractCredentials* _credentials;
bool _treatSslErrorsAsFailure;
int _state;
2013-11-14 18:48:46 +04:00
static QString _configFileName;
2014-03-21 20:14:04 +04:00
QString _davPath; // default "remote.php/webdav/";
bool _wasMigrated;
2013-10-21 23:42:52 +04:00
};
}
2014-11-10 00:34:07 +03:00
Q_DECLARE_METATYPE(OCC::Account*)
2013-10-21 23:42:52 +04:00
#endif //SERVERCONNECTION_H