2015-10-19 15:41:53 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) by Klaas Freitag <freitag@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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QtGui>
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
|
|
|
|
#include <QtWidgets>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "activitywidget.h"
|
|
|
|
#include "configfile.h"
|
|
|
|
#include "syncresult.h"
|
|
|
|
#include "logger.h"
|
|
|
|
#include "utility.h"
|
|
|
|
#include "theme.h"
|
|
|
|
#include "folderman.h"
|
|
|
|
#include "syncfileitem.h"
|
|
|
|
#include "folder.h"
|
|
|
|
#include "openfilemanager.h"
|
|
|
|
#include "owncloudpropagator.h"
|
2015-11-02 00:27:33 +03:00
|
|
|
#include "account.h"
|
|
|
|
#include "accountstate.h"
|
|
|
|
#include "accountmanager.h"
|
2015-10-19 15:41:53 +03:00
|
|
|
|
|
|
|
#include "ui_activitywidget.h"
|
|
|
|
|
|
|
|
#include <climits>
|
|
|
|
|
|
|
|
namespace OCC {
|
|
|
|
|
2015-11-02 00:27:33 +03:00
|
|
|
void ActivityList::setAccountName( const QString& name )
|
|
|
|
{
|
|
|
|
_accountName = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ActivityList::accountName() const
|
|
|
|
{
|
|
|
|
return _accountName;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ========================================================================
|
|
|
|
|
2015-10-19 15:41:53 +03:00
|
|
|
ActivityListModel::ActivityListModel(QWidget *parent)
|
|
|
|
:QAbstractListModel(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant ActivityListModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
2015-11-02 17:44:13 +03:00
|
|
|
Activity a;
|
|
|
|
|
2015-10-19 15:41:53 +03:00
|
|
|
if (!index.isValid())
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
if (role == Qt::EditRole)
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
switch (role) {
|
|
|
|
case Qt::ToolTipRole:
|
2015-11-02 17:44:13 +03:00
|
|
|
return QVariant();
|
2015-10-19 15:41:53 +03:00
|
|
|
case Qt::DisplayRole:
|
2015-11-02 17:44:13 +03:00
|
|
|
a = _finalList.at(index.row());
|
|
|
|
return tr("Account %1 at %2: %3").arg(a._accName).arg(a._dateTime.toString(Qt::SystemLocaleShortDate)).arg(a._subject);
|
|
|
|
break;
|
2015-10-19 15:41:53 +03:00
|
|
|
case Qt::DecorationRole:
|
2015-11-02 17:44:13 +03:00
|
|
|
// return QFileIconProvider().icon(QFileIconProvider::Folder);
|
|
|
|
return QVariant();
|
2015-10-19 15:41:53 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int ActivityListModel::rowCount(const QModelIndex&) const
|
|
|
|
{
|
2015-11-02 17:44:13 +03:00
|
|
|
return _finalList.count();
|
2015-11-02 00:27:33 +03:00
|
|
|
}
|
|
|
|
|
2015-11-02 17:44:13 +03:00
|
|
|
// current strategy: Fetch 100 items per Account
|
|
|
|
bool ActivityListModel::canFetchMore(const QModelIndex& ) const
|
2015-11-02 00:27:33 +03:00
|
|
|
{
|
2015-11-02 17:44:13 +03:00
|
|
|
if( _activityLists.count() == 0 ) return true;
|
2015-11-02 00:27:33 +03:00
|
|
|
|
2015-11-02 17:44:13 +03:00
|
|
|
QMap<AccountStatePtr, ActivityList>::const_iterator i = _activityLists.begin();
|
|
|
|
while (i != _activityLists.end()) {
|
2015-11-02 18:39:33 +03:00
|
|
|
AccountStatePtr ast = i.key();
|
|
|
|
ActivityList activities = i.value();
|
|
|
|
if( ast->isConnected() && activities.count() == 0 &&
|
|
|
|
! _currentlyFetching.contains(ast) ) {
|
2015-11-02 17:44:13 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
2015-11-02 00:27:33 +03:00
|
|
|
|
2015-11-02 17:44:13 +03:00
|
|
|
return false;
|
2015-10-19 15:41:53 +03:00
|
|
|
}
|
|
|
|
|
2015-11-02 17:44:13 +03:00
|
|
|
void ActivityListModel::startFetchJob(AccountStatePtr s)
|
2015-10-19 15:41:53 +03:00
|
|
|
{
|
2015-11-02 17:44:13 +03:00
|
|
|
JsonApiJob *job = new JsonApiJob(s->account(), QLatin1String("ocs/v1.php/cloud/activity"), this);
|
|
|
|
QObject::connect(job, SIGNAL(jsonRecieved(QVariantMap)), this, SLOT(slotActivitiesReceived(QVariantMap)));
|
|
|
|
job->setProperty("AccountStatePtr", QVariant::fromValue<AccountStatePtr>(s));
|
2015-10-19 15:41:53 +03:00
|
|
|
|
2015-11-02 19:51:12 +03:00
|
|
|
QList< QPair<QString,QString> > params;
|
|
|
|
params.append(qMakePair(QLatin1String("page"), QLatin1String("0")));
|
|
|
|
params.append(qMakePair(QLatin1String("pagesize"), QLatin1String("100")));
|
|
|
|
job->addQueryParams(params);
|
2015-10-19 15:41:53 +03:00
|
|
|
|
2015-11-02 19:51:12 +03:00
|
|
|
_currentlyFetching.insert(s);
|
|
|
|
job->start();
|
2015-11-02 17:44:13 +03:00
|
|
|
}
|
2015-10-19 15:41:53 +03:00
|
|
|
|
2015-11-02 17:44:13 +03:00
|
|
|
void ActivityListModel::slotActivitiesReceived(const QVariantMap& json)
|
|
|
|
{
|
|
|
|
auto activities = json.value("ocs").toMap().value("data").toList();
|
|
|
|
qDebug() << "*** activities" << activities;
|
|
|
|
|
|
|
|
ActivityList list;
|
|
|
|
AccountStatePtr ai = qvariant_cast<AccountStatePtr>(sender()->property("AccountStatePtr"));
|
|
|
|
_currentlyFetching.remove(ai);
|
|
|
|
list.setAccountName( ai->account()->displayName());
|
|
|
|
foreach( auto activ, activities ) {
|
|
|
|
auto json = activ.toMap();
|
|
|
|
|
|
|
|
Activity a;
|
|
|
|
a._accName = ai->account()->displayName();
|
|
|
|
a._id = json.value("id").toLongLong();
|
|
|
|
a._subject = json.value("subject").toString();
|
|
|
|
a._message = json.value("message").toString();
|
|
|
|
a._file = json.value("file").toString();
|
|
|
|
a._link = json.value("link").toUrl();
|
|
|
|
a._dateTime = json.value("date").toDateTime();
|
|
|
|
list.append(a);
|
|
|
|
}
|
2015-11-02 00:27:33 +03:00
|
|
|
|
2015-11-02 17:44:13 +03:00
|
|
|
_activityLists[ai] = list;
|
2015-11-02 00:27:33 +03:00
|
|
|
|
2015-11-02 17:44:13 +03:00
|
|
|
// if all activity lists were received, assemble the whole list
|
|
|
|
bool allAreHere = true;
|
|
|
|
foreach( ActivityList list, _activityLists.values() ) {
|
|
|
|
if( list.count() == 0 ) {
|
|
|
|
allAreHere = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( allAreHere ) {
|
|
|
|
combineActivityLists();
|
|
|
|
}
|
2015-10-19 15:41:53 +03:00
|
|
|
}
|
|
|
|
|
2015-11-02 17:44:13 +03:00
|
|
|
|
|
|
|
void ActivityListModel::combineActivityLists()
|
2015-11-02 00:27:33 +03:00
|
|
|
{
|
2015-11-02 17:44:13 +03:00
|
|
|
ActivityList resultList;
|
|
|
|
|
|
|
|
foreach( ActivityList list, _activityLists.values() ) {
|
|
|
|
resultList.append(list);
|
2015-11-02 00:27:33 +03:00
|
|
|
}
|
2015-11-02 17:44:13 +03:00
|
|
|
|
|
|
|
std::sort( resultList.begin(), resultList.end() );
|
|
|
|
|
|
|
|
beginInsertRows(QModelIndex(), 0, resultList.count()-1);
|
|
|
|
_finalList = resultList;
|
|
|
|
endInsertRows();
|
2015-11-02 00:27:33 +03:00
|
|
|
}
|
|
|
|
|
2015-11-02 17:44:13 +03:00
|
|
|
void ActivityListModel::fetchMore(const QModelIndex &)
|
2015-11-02 00:27:33 +03:00
|
|
|
{
|
2015-11-02 17:44:13 +03:00
|
|
|
QList<AccountStatePtr> accounts = AccountManager::instance()->accounts();
|
2015-11-02 00:27:33 +03:00
|
|
|
|
2015-11-02 17:44:13 +03:00
|
|
|
foreach (AccountStatePtr asp, accounts) {
|
2015-11-02 00:27:33 +03:00
|
|
|
|
2015-11-02 17:44:13 +03:00
|
|
|
// if the account is not yet managed, add an empty list.
|
|
|
|
if( !_activityLists.contains(asp) ) {
|
|
|
|
_activityLists[asp] = ActivityList();
|
|
|
|
}
|
|
|
|
ActivityList activities = _activityLists[asp];
|
|
|
|
if( activities.count() == 0 ) {
|
|
|
|
startFetchJob(asp);
|
|
|
|
}
|
2015-11-02 00:27:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-02 17:44:13 +03:00
|
|
|
/* ==================================================================== */
|
|
|
|
|
|
|
|
ActivityWidget::ActivityWidget(QWidget *parent) :
|
|
|
|
QWidget(parent),
|
|
|
|
_ui(new Ui::ActivityWidget)
|
2015-11-02 00:27:33 +03:00
|
|
|
{
|
2015-11-02 17:44:13 +03:00
|
|
|
_ui->setupUi(this);
|
2015-11-02 00:27:33 +03:00
|
|
|
|
2015-11-02 17:44:13 +03:00
|
|
|
// Adjust copyToClipboard() when making changes here!
|
|
|
|
#if defined(Q_OS_MAC)
|
|
|
|
_ui->_activityList->setMinimumWidth(400);
|
|
|
|
#endif
|
2015-11-02 00:27:33 +03:00
|
|
|
|
2015-11-02 17:44:13 +03:00
|
|
|
_model = new ActivityListModel(this);
|
|
|
|
_ui->_activityList->setModel(_model);
|
2015-11-02 00:27:33 +03:00
|
|
|
|
2015-11-02 17:44:13 +03:00
|
|
|
_copyBtn = _ui->_dialogButtonBox->addButton(tr("Copy"), QDialogButtonBox::ActionRole);
|
|
|
|
_copyBtn->setToolTip( tr("Copy the activity list to the clipboard."));
|
|
|
|
_copyBtn->setEnabled(false);
|
|
|
|
connect(_copyBtn, SIGNAL(clicked()), SLOT(copyToClipboard()));
|
2015-11-02 00:27:33 +03:00
|
|
|
}
|
|
|
|
|
2015-10-19 15:41:53 +03:00
|
|
|
ActivityWidget::~ActivityWidget()
|
|
|
|
{
|
|
|
|
delete _ui;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ActivityWidget::copyToClipboard()
|
|
|
|
{
|
|
|
|
QString text;
|
|
|
|
QTextStream ts(&text);
|
|
|
|
#if 0
|
|
|
|
int topLevelItems = _ui->_activityList->topLevelItemCount();
|
|
|
|
for (int i = 0; i < topLevelItems; i++) {
|
|
|
|
QTreeWidgetItem *child = _ui->_activityList->topLevelItem(i);
|
|
|
|
ts << left
|
|
|
|
// time stamp
|
|
|
|
<< qSetFieldWidth(10)
|
|
|
|
<< child->data(0,Qt::DisplayRole).toString()
|
|
|
|
// file name
|
|
|
|
<< qSetFieldWidth(64)
|
|
|
|
<< child->data(1,Qt::DisplayRole).toString()
|
|
|
|
// folder
|
|
|
|
<< qSetFieldWidth(30)
|
|
|
|
<< child->data(2, Qt::DisplayRole).toString()
|
|
|
|
// action
|
|
|
|
<< qSetFieldWidth(15)
|
|
|
|
<< child->data(3, Qt::DisplayRole).toString()
|
|
|
|
// size
|
|
|
|
<< qSetFieldWidth(10)
|
|
|
|
<< child->data(4, Qt::DisplayRole).toString()
|
|
|
|
<< qSetFieldWidth(0)
|
|
|
|
<< endl;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
QApplication::clipboard()->setText(text);
|
|
|
|
emit guiLog(tr("Copied to clipboard"), tr("The sync status has been copied to the clipboard."));
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Reused from protocol widget. Move over to utilities.
|
|
|
|
QString ActivityWidget::timeString(QDateTime dt, QLocale::FormatType format) const
|
|
|
|
{
|
|
|
|
const QLocale loc = QLocale::system();
|
|
|
|
QString dtFormat = loc.dateTimeFormat(format);
|
|
|
|
static const QRegExp re("(HH|H|hh|h):mm(?!:s)");
|
|
|
|
dtFormat.replace(re, "\\1:mm:ss");
|
|
|
|
return loc.toString(dt, dtFormat);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ActivityWidget::slotOpenFile( )
|
|
|
|
{
|
|
|
|
// FIXME make work at all.
|
|
|
|
#if 0
|
|
|
|
QString folderName = item->data(2, Qt::UserRole).toString();
|
|
|
|
QString fileName = item->text(1);
|
|
|
|
|
|
|
|
Folder *folder = FolderMan::instance()->folder(folderName);
|
|
|
|
if (folder) {
|
|
|
|
// folder->path() always comes back with trailing path
|
|
|
|
QString fullPath = folder->path() + fileName;
|
|
|
|
if (QFile(fullPath).exists()) {
|
|
|
|
showInFileManager(fullPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|