2014-09-03 18:12:21 +04:00
|
|
|
#include "sharedialog.h"
|
|
|
|
#include "ui_sharedialog.h"
|
|
|
|
#include "networkjobs.h"
|
|
|
|
#include "account.h"
|
|
|
|
#include "json.h"
|
|
|
|
#include "folderman.h"
|
|
|
|
#include <QBuffer>
|
|
|
|
#include <QMovie>
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
int SHARETYPE_PUBLIC = 3;
|
|
|
|
}
|
|
|
|
|
2014-12-19 18:56:17 +03:00
|
|
|
namespace OCC {
|
2014-09-03 18:12:21 +04:00
|
|
|
|
|
|
|
ShareDialog::ShareDialog(QWidget *parent) :
|
|
|
|
QDialog(parent),
|
|
|
|
_ui(new Ui::ShareDialog)
|
|
|
|
{
|
|
|
|
setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
_ui->setupUi(this);
|
|
|
|
_ui->pushButton_copy->setIcon(QIcon::fromTheme("edit-copy"));
|
|
|
|
layout()->setSizeConstraint(QLayout::SetFixedSize);
|
|
|
|
QMovie *movie = new QMovie("/home/azelphur/ownCloud-share-tools/loading-icon.gif");
|
|
|
|
movie->start();
|
|
|
|
_ui->labelShareSpinner->setMovie(movie);
|
|
|
|
_ui->labelShareSpinner->hide();
|
|
|
|
|
|
|
|
_ui->labelPasswordSpinner->setMovie(movie);
|
|
|
|
_ui->labelPasswordSpinner->hide();
|
|
|
|
|
|
|
|
_ui->labelCalendarSpinner->setMovie(movie);
|
|
|
|
_ui->labelCalendarSpinner->hide();
|
|
|
|
connect(_ui->checkBox_shareLink, SIGNAL(clicked()), this, SLOT(slotCheckBoxShareLinkClicked()));
|
|
|
|
connect(_ui->checkBox_password, SIGNAL(clicked()), this, SLOT(slotCheckBoxPasswordClicked()));
|
|
|
|
connect(_ui->lineEdit_password, SIGNAL(returnPressed()), this, SLOT(slotPasswordReturnPressed()));
|
|
|
|
connect(_ui->checkBox_expire, SIGNAL(clicked()), this, SLOT(slotCheckBoxExpireClicked()));
|
2014-09-09 03:50:31 +04:00
|
|
|
connect(_ui->calendar, SIGNAL(clicked(QDate)), SLOT(slotCalendarClicked(QDate)));
|
2014-09-03 18:12:21 +04:00
|
|
|
|
|
|
|
_ui->labelShareSpinner->hide();
|
|
|
|
_ui->lineEdit_shareLink->hide();
|
|
|
|
_ui->pushButton_copy->hide();
|
|
|
|
_ui->lineEdit_password->hide();
|
|
|
|
_ui->checkBox_password->hide();
|
|
|
|
_ui->checkBox_expire->hide();
|
|
|
|
_ui->calendar->hide();
|
2015-01-11 15:22:48 +03:00
|
|
|
_ui->lineEdit_shareGroup->setPlaceholderText(tr("Share with group..."));
|
|
|
|
_ui->lineEdit_shareUser->setPlaceholderText(tr("Share with user..."));
|
|
|
|
_ui->lineEdit_password->setPlaceholderText(tr("Choose a password for the public link"));
|
2014-09-03 18:12:21 +04:00
|
|
|
}
|
|
|
|
|
2014-09-09 03:50:31 +04:00
|
|
|
void ShareDialog::setExpireDate(QString date)
|
|
|
|
{
|
|
|
|
_ui->labelCalendarSpinner->show();
|
2014-12-24 16:44:35 +03:00
|
|
|
QUrl url = Account::concatUrlPath(AccountManager::instance()->account()->url(), QString("ocs/v1.php/apps/files_sharing/api/v1/shares/").append(QString::number(_public_share_id)));
|
2014-09-09 03:50:31 +04:00
|
|
|
QUrl postData;
|
|
|
|
QList<QPair<QString, QString> > getParams;
|
|
|
|
QList<QPair<QString, QString> > postParams;
|
|
|
|
getParams.append(qMakePair(QString::fromLatin1("format"), QString::fromLatin1("json")));
|
|
|
|
postParams.append(qMakePair(QString::fromLatin1("expireDate"), date));
|
|
|
|
url.setQueryItems(getParams);
|
|
|
|
postData.setQueryItems(postParams);
|
|
|
|
OcsShareJob *job = new OcsShareJob("PUT", url, postData, AccountManager::instance()->account(), this);
|
|
|
|
connect(job, SIGNAL(jobFinished(QString)), this, SLOT(slotExpireSet(QString)));
|
|
|
|
job->start();
|
|
|
|
}
|
|
|
|
|
2015-01-11 13:19:12 +03:00
|
|
|
void ShareDialog::slotExpireSet(const QString &reply)
|
2014-09-09 03:50:31 +04:00
|
|
|
{
|
|
|
|
_ui->labelCalendarSpinner->hide();
|
|
|
|
}
|
|
|
|
|
2015-01-11 13:19:12 +03:00
|
|
|
void ShareDialog::slotCalendarClicked(const QDate &date)
|
2014-09-09 03:50:31 +04:00
|
|
|
{
|
|
|
|
ShareDialog::setExpireDate(date.toString("dd-MM-yyyy"));
|
|
|
|
}
|
|
|
|
|
2014-09-03 18:12:21 +04:00
|
|
|
QString ShareDialog::getPath()
|
|
|
|
{
|
|
|
|
return _path;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShareDialog::setPath(const QString &path)
|
|
|
|
{
|
|
|
|
_path = path;
|
|
|
|
ShareDialog::getShares();
|
|
|
|
}
|
|
|
|
|
|
|
|
ShareDialog::~ShareDialog()
|
|
|
|
{
|
|
|
|
delete _ui;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShareDialog::slotPasswordReturnPressed()
|
2014-09-09 03:50:31 +04:00
|
|
|
{
|
|
|
|
ShareDialog::setPassword(_ui->lineEdit_password->text());
|
2015-01-11 15:22:48 +03:00
|
|
|
_ui->lineEdit_password->setPlaceholderText(tr("Password Protected"));
|
2015-01-11 15:27:32 +03:00
|
|
|
_ui->lineEdit_password->setText(QString());
|
2014-09-09 03:50:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ShareDialog::setPassword(QString password)
|
2014-09-03 18:12:21 +04:00
|
|
|
{
|
|
|
|
_ui->labelPasswordSpinner->show();
|
2014-12-24 16:44:35 +03:00
|
|
|
QUrl url = Account::concatUrlPath(AccountManager::instance()->account()->url(), QString("ocs/v1.php/apps/files_sharing/api/v1/shares/").append(QString::number(_public_share_id)));
|
2014-09-03 18:12:21 +04:00
|
|
|
QUrl postData;
|
|
|
|
QList<QPair<QString, QString> > getParams;
|
|
|
|
QList<QPair<QString, QString> > postParams;
|
|
|
|
getParams.append(qMakePair(QString::fromLatin1("format"), QString::fromLatin1("json")));
|
2014-09-09 03:50:31 +04:00
|
|
|
postParams.append(qMakePair(QString::fromLatin1("password"), password));
|
2014-09-03 18:12:21 +04:00
|
|
|
url.setQueryItems(getParams);
|
|
|
|
postData.setQueryItems(postParams);
|
|
|
|
OcsShareJob *job = new OcsShareJob("PUT", url, postData, AccountManager::instance()->account(), this);
|
2014-09-09 03:50:31 +04:00
|
|
|
connect(job, SIGNAL(jobFinished(QString)), this, SLOT(slotPasswordSet(QString)));
|
2014-09-03 18:12:21 +04:00
|
|
|
job->start();
|
|
|
|
}
|
|
|
|
|
2015-01-11 13:19:12 +03:00
|
|
|
void ShareDialog::slotPasswordSet(const QString &reply)
|
2014-09-09 03:50:31 +04:00
|
|
|
{
|
|
|
|
_ui->labelPasswordSpinner->hide();
|
|
|
|
}
|
|
|
|
|
2014-09-03 18:12:21 +04:00
|
|
|
void ShareDialog::getShares()
|
|
|
|
{
|
2015-01-11 15:22:48 +03:00
|
|
|
this->setWindowTitle(tr("Sharing %1").arg(_path));
|
2014-09-03 18:12:21 +04:00
|
|
|
QUrl url = Account::concatUrlPath(AccountManager::instance()->account()->url(), QLatin1String("ocs/v1.php/apps/files_sharing/api/v1/shares"));
|
|
|
|
QList<QPair<QString, QString> > params;
|
|
|
|
params.append(qMakePair(QString::fromLatin1("format"), QString::fromLatin1("json")));
|
|
|
|
params.append(qMakePair(QString::fromLatin1("path"), _path));
|
|
|
|
url.setQueryItems(params);
|
|
|
|
OcsShareJob *job = new OcsShareJob("GET", url, QUrl(), AccountManager::instance()->account(), this);
|
|
|
|
connect(job, SIGNAL(jobFinished(QString)), this, SLOT(slotSharesFetched(QString)));
|
|
|
|
job->start();
|
|
|
|
}
|
|
|
|
|
2015-01-11 13:19:12 +03:00
|
|
|
void ShareDialog::slotSharesFetched(const QString &reply)
|
2014-09-03 18:12:21 +04:00
|
|
|
{
|
|
|
|
bool success = false;
|
|
|
|
QVariantMap json = QtJson::parse(reply, success).toMap();
|
|
|
|
ShareDialog::_shares = json.value("ocs").toMap().values("data")[0].toList();
|
|
|
|
for(int i = 0; i < ShareDialog::_shares.count(); i++)
|
|
|
|
{
|
|
|
|
QVariantMap data = ShareDialog::_shares[i].toMap();
|
|
|
|
|
|
|
|
if (data.value("share_type").toInt() == SHARETYPE_PUBLIC)
|
|
|
|
{
|
2014-12-24 16:44:35 +03:00
|
|
|
_public_share_id = data.value("id").toULongLong();
|
2014-09-03 18:12:21 +04:00
|
|
|
|
|
|
|
_ui->lineEdit_shareLink->show();
|
|
|
|
_ui->pushButton_copy->show();
|
|
|
|
_ui->checkBox_password->show();
|
|
|
|
_ui->checkBox_expire->show();
|
|
|
|
_ui->pushButton_copy->show();
|
|
|
|
_ui->checkBox_shareLink->setChecked(true);
|
|
|
|
|
|
|
|
if (data.value("share_with").isValid())
|
|
|
|
{
|
|
|
|
_ui->checkBox_password->setChecked(true);
|
|
|
|
_ui->lineEdit_password->setText("********");
|
|
|
|
_ui->lineEdit_password->show();
|
|
|
|
}
|
|
|
|
|
2014-09-09 03:50:31 +04:00
|
|
|
if (data.value("expire_date").isValid())
|
|
|
|
{
|
|
|
|
_ui->calendar->setSelectedDate(QDate::fromString(data.value("expire_date").toString(), "dd-MM-yyyy"));
|
|
|
|
_ui->calendar->show();
|
|
|
|
_ui->checkBox_expire->setChecked(true);
|
|
|
|
}
|
|
|
|
|
2014-09-03 18:12:21 +04:00
|
|
|
char url[512];
|
|
|
|
sprintf(url, "https://home.azelphur.com/owncloud/public.php?service=files&t=%s", data.value("token").toString().toStdString().c_str());
|
|
|
|
_ui->lineEdit_shareLink->setText(url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-11 13:19:12 +03:00
|
|
|
void ShareDialog::slotDeleteShareFetched(const QString &reply)
|
2014-09-03 18:12:21 +04:00
|
|
|
{
|
|
|
|
_ui->labelShareSpinner->hide();
|
|
|
|
_ui->lineEdit_shareLink->hide();
|
|
|
|
_ui->pushButton_copy->hide();
|
|
|
|
_ui->lineEdit_password->hide();
|
|
|
|
_ui->checkBox_password->hide();
|
|
|
|
_ui->checkBox_expire->hide();
|
|
|
|
_ui->calendar->hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShareDialog::slotCheckBoxShareLinkClicked()
|
|
|
|
{
|
|
|
|
if (_ui->checkBox_shareLink->checkState() == Qt::Checked)
|
|
|
|
{
|
|
|
|
_ui->labelShareSpinner->show();
|
|
|
|
QUrl url = Account::concatUrlPath(AccountManager::instance()->account()->url(), QLatin1String("ocs/v1.php/apps/files_sharing/api/v1/shares"));
|
|
|
|
QUrl postData;
|
|
|
|
QList<QPair<QString, QString> > getParams;
|
|
|
|
QList<QPair<QString, QString> > postParams;
|
|
|
|
getParams.append(qMakePair(QString::fromLatin1("format"), QString::fromLatin1("json")));
|
|
|
|
postParams.append(qMakePair(QString::fromLatin1("path"), _path));
|
|
|
|
postParams.append(qMakePair(QString::fromLatin1("shareType"), QString::number(SHARETYPE_PUBLIC)));
|
|
|
|
url.setQueryItems(getParams);
|
|
|
|
postData.setQueryItems(postParams);
|
|
|
|
OcsShareJob *job = new OcsShareJob("POST", url, postData, AccountManager::instance()->account(), this);
|
|
|
|
connect(job, SIGNAL(jobFinished(QString)), this, SLOT(slotCreateShareFetched(QString)));
|
|
|
|
job->start();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_ui->labelShareSpinner->show();
|
2014-12-24 16:44:35 +03:00
|
|
|
QUrl url = Account::concatUrlPath(AccountManager::instance()->account()->url(), QString("ocs/v1.php/apps/files_sharing/api/v1/shares/").append(QString::number(_public_share_id)));
|
2014-09-03 18:12:21 +04:00
|
|
|
QList<QPair<QString, QString> > getParams;
|
|
|
|
getParams.append(qMakePair(QString::fromLatin1("format"), QString::fromLatin1("json")));
|
|
|
|
url.setQueryItems(getParams);
|
|
|
|
OcsShareJob *job = new OcsShareJob("DELETE", url, QUrl(), AccountManager::instance()->account(), this);
|
|
|
|
connect(job, SIGNAL(jobFinished(QString)), this, SLOT(slotDeleteShareFetched(QString)));
|
|
|
|
job->start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-11 13:19:12 +03:00
|
|
|
void ShareDialog::slotCreateShareFetched(const QString &reply)
|
2014-09-03 18:12:21 +04:00
|
|
|
{
|
2015-01-11 15:41:15 +03:00
|
|
|
qDebug() << Q_FUNC_INFO << reply;
|
2014-09-03 18:12:21 +04:00
|
|
|
_ui->labelShareSpinner->hide();
|
|
|
|
bool success;
|
|
|
|
QVariantMap json = QtJson::parse(reply, success).toMap();
|
2014-12-24 16:44:35 +03:00
|
|
|
_public_share_id = json.value("ocs").toMap().values("data")[0].toMap().value("id").toULongLong();
|
2014-09-03 18:12:21 +04:00
|
|
|
QString url = json.value("ocs").toMap().values("data")[0].toMap().value("url").toString();
|
|
|
|
_ui->lineEdit_shareLink->setText(url);
|
|
|
|
_ui->lineEdit_shareLink->show();
|
|
|
|
_ui->pushButton_copy->show();
|
|
|
|
_ui->checkBox_password->show();
|
|
|
|
_ui->checkBox_expire->show();
|
|
|
|
_ui->pushButton_copy->show();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShareDialog::slotCheckBoxPasswordClicked()
|
|
|
|
{
|
|
|
|
if (_ui->checkBox_password->checkState() == Qt::Checked)
|
|
|
|
{
|
|
|
|
_ui->lineEdit_password->show();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-01-11 15:24:40 +03:00
|
|
|
ShareDialog::setPassword(QString());
|
2014-09-09 03:50:31 +04:00
|
|
|
_ui->labelPasswordSpinner->show();
|
2014-09-03 18:12:21 +04:00
|
|
|
_ui->lineEdit_password->hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShareDialog::slotCheckBoxExpireClicked()
|
|
|
|
{
|
|
|
|
if (_ui->checkBox_expire->checkState() == Qt::Checked)
|
|
|
|
{
|
2014-09-09 03:50:31 +04:00
|
|
|
QDate date = QDate::currentDate().addDays(1);
|
|
|
|
ShareDialog::setExpireDate(date.toString("dd-MM-yyyy"));
|
|
|
|
_ui->calendar->setSelectedDate(date);
|
2014-09-03 18:12:21 +04:00
|
|
|
_ui->calendar->show();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-01-11 15:24:40 +03:00
|
|
|
ShareDialog::setExpireDate(QString());
|
2014-09-03 18:12:21 +04:00
|
|
|
_ui->calendar->hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OcsShareJob::OcsShareJob(const QByteArray &verb, const QUrl &url, const QUrl &postData, Account* account, QObject* parent)
|
|
|
|
: AbstractNetworkJob(account, "", parent),
|
|
|
|
_verb(verb),
|
|
|
|
_url(url),
|
|
|
|
_postData(postData)
|
|
|
|
|
|
|
|
{
|
|
|
|
setIgnoreCredentialFailure(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OcsShareJob::start()
|
|
|
|
{
|
|
|
|
QNetworkRequest req;
|
|
|
|
req.setRawHeader("OCS-APIREQUEST", "true");
|
|
|
|
req.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
|
|
|
|
QBuffer *buffer = new QBuffer;
|
2014-12-19 18:56:17 +03:00
|
|
|
|
2015-01-11 15:56:23 +03:00
|
|
|
QStringList tmp;
|
|
|
|
Q_FOREACH(auto tmp2, _postData.queryItems()) {
|
|
|
|
tmp.append(tmp2.first + "=" + tmp2.second);
|
2014-12-19 18:56:17 +03:00
|
|
|
}
|
2015-01-11 15:56:23 +03:00
|
|
|
buffer->setData(tmp.join("&").toAscii());
|
2014-12-19 18:56:17 +03:00
|
|
|
|
2014-09-03 18:12:21 +04:00
|
|
|
setReply(davRequest(_verb, _url, req, buffer));
|
|
|
|
setupConnections(reply());
|
|
|
|
buffer->setParent(reply());
|
|
|
|
AbstractNetworkJob::start();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OcsShareJob::finished()
|
|
|
|
{
|
|
|
|
emit jobFinished(reply()->readAll());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|