Try to make seafile implementation more robusts and more verbose

This commit is contained in:
Chocobozzz 2015-02-20 15:15:55 +01:00
parent de0f9a4213
commit 716064a006
2 changed files with 85 additions and 8 deletions

View File

@ -26,6 +26,7 @@
#include <QTimer>
#include "core/application.h"
#include "core/taskmanager.h"
#include "core/player.h"
#include "core/waitforsignal.h"
#include "internet/seafile/seafileurlhandler.h"
@ -51,7 +52,10 @@ static const int kMaxTries = 10;
SeafileService::SeafileService(Application* app, InternetModel* parent)
: CloudFileService(app, parent, kServiceName, kSettingsGroup,
QIcon(":/providers/seafile.png"),
SettingsDialog::Page_Seafile) {
SettingsDialog::Page_Seafile),
indexing_task_id_(-1),
indexing_task_max_(0),
indexing_task_progress_(0) {
QSettings s;
s.beginGroup(kSettingsGroup);
access_token_ = s.value("access_token").toString();
@ -174,7 +178,22 @@ void SeafileService::GetLibrariesFinished(QNetworkReply* reply) {
emit GetLibrariesFinishedSignal(libraries);
}
void SeafileService::ChangeLibrary(const QString& new_library) {
void SeafileService::ChangeLibrary(const QString& new_library,
bool have_to_disconnect) {
if (have_to_disconnect) {
disconnect(this, SIGNAL(UpdatingLibrariesFinishedSignal()));
}
if (indexing_task_id_ != -1) {
qLog(Debug) << "Want to change the Seafile library, but Clementine waits "
"the previous indexing...";
NewClosure(this, SIGNAL(UpdatingLibrariesFinishedSignal()), this,
SLOT(ChangeLibrary(QString, bool)), new_library, true);
return;
}
qLog(Debug) << "Change the Seafile library";
// Every other libraries have to be destroyed from the tree
if (new_library != "all") {
for (SeafileTree::TreeItem* library : tree_.libraries()) {
@ -196,6 +215,15 @@ void SeafileService::Connect() {
}
void SeafileService::UpdateLibraries() {
if (indexing_task_id_ == -1) {
indexing_task_id_ =
app_->task_manager()->StartTask(tr("Build index of Seafile"));
}
// Quit if we are already updating the libraries
else {
return;
}
connect(this, SIGNAL(GetLibrariesFinishedSignal(QMap<QString, QString>)),
this, SLOT(UpdateLibrariesInProgress(QMap<QString, QString>)));
@ -211,14 +239,20 @@ void SeafileService::UpdateLibrariesInProgress(
s.beginGroup(kSettingsGroup);
QString library_to_update = s.value("library").toString();
// If the library doesn't change, we don't need to update
// If the library didn't change, we don't need to update
if (!library_updated_.isNull() && library_updated_ == library_to_update) {
app_->task_manager()->SetTaskFinished(indexing_task_id_);
indexing_task_id_ = -1;
UpdatingLibrariesFinishedSignal();
return;
}
library_updated_ = library_to_update;
if (library_to_update == "none") {
app_->task_manager()->SetTaskFinished(indexing_task_id_);
indexing_task_id_ = -1;
UpdatingLibrariesFinishedSignal();
return;
}
@ -232,7 +266,7 @@ void SeafileService::UpdateLibrariesInProgress(
SeafileTree::Entry(library.value(), library.key(),
SeafileTree::Entry::LIBRARY),
"/");
// If not, we can destroy the library from the tree
// If not, we can destroy the library from the tree
} else {
// If the library was not in the tree, it's not a problem because
// DeleteEntry won't do anything
@ -241,6 +275,13 @@ void SeafileService::UpdateLibrariesInProgress(
SeafileTree::Entry::LIBRARY));
}
}
// If we didn't do anything, set the task finished
if (indexing_task_max_ == 0) {
app_->task_manager()->SetTaskFinished(indexing_task_id_);
indexing_task_id_ = -1;
UpdatingLibrariesFinishedSignal();
}
}
QNetworkReply* SeafileService::PrepareFetchFolderItems(const QString& library,
@ -257,6 +298,8 @@ QNetworkReply* SeafileService::PrepareFetchFolderItems(const QString& library,
void SeafileService::FetchAndCheckFolderItems(const SeafileTree::Entry& library,
const QString& path) {
StartTaskInProgress();
QNetworkReply* reply = PrepareFetchFolderItems(library.id(), path);
NewClosure(reply, SIGNAL(finished()), this,
SLOT(FetchAndCheckFolderItemsFinished(
@ -270,6 +313,7 @@ void SeafileService::FetchAndCheckFolderItemsFinished(
if (!CheckReply(&reply)) {
qLog(Warning)
<< "Something wrong with the reply... (FetchFolderItemsToList)";
FinishedTaskInProgress();
return;
}
@ -300,10 +344,14 @@ void SeafileService::FetchAndCheckFolderItemsFinished(
}
tree_.CheckEntries(entries, library, path);
FinishedTaskInProgress();
}
void SeafileService::AddRecursivelyFolderItems(const QString& library,
const QString& path) {
StartTaskInProgress();
QNetworkReply* reply = PrepareFetchFolderItems(library, path);
NewClosure(
reply, SIGNAL(finished()), this,
@ -316,6 +364,7 @@ void SeafileService::AddRecursivelyFolderItemsFinished(QNetworkReply* reply,
const QString& path) {
if (!CheckReply(&reply)) {
qLog(Warning) << "Something wrong with the reply... (FetchFolderItems)";
FinishedTaskInProgress();
return;
}
@ -343,9 +392,8 @@ void SeafileService::AddRecursivelyFolderItemsFinished(QNetworkReply* reply,
entry_type);
// If AddEntry was not successful we stop
// It could happen when the user changes the library to update while an
// update was in progress
if (!tree_.AddEntry(library, path, entry)) {
FinishedTaskInProgress();
return;
}
@ -355,6 +403,8 @@ void SeafileService::AddRecursivelyFolderItemsFinished(QNetworkReply* reply,
MaybeAddFileEntry(entry.name(), library, path);
}
}
FinishedTaskInProgress();
}
QNetworkReply* SeafileService::PrepareFetchContentForFile(
@ -593,6 +643,24 @@ bool SeafileService::CheckReply(QNetworkReply** reply, int tries) {
return false;
}
void SeafileService::StartTaskInProgress() {
indexing_task_max_++;
task_manager_->SetTaskProgress(indexing_task_id_, indexing_task_progress_,
indexing_task_max_);
}
void SeafileService::FinishedTaskInProgress() {
indexing_task_progress_++;
if (indexing_task_progress_ == indexing_task_max_) {
task_manager_->SetTaskFinished(indexing_task_id_);
indexing_task_id_ = -1;
UpdatingLibrariesFinishedSignal();
} else {
task_manager_->SetTaskProgress(indexing_task_id_, indexing_task_progress_,
indexing_task_max_);
}
}
SeafileService::~SeafileService() {
// Save the tree !
QSettings s;

View File

@ -76,16 +76,18 @@ class SeafileService : public CloudFileService {
const QString& server);
// Get all the libraries available for the user. Will emit a signal
void GetLibraries();
void ChangeLibrary(const QString& new_library);
public slots:
void Connect();
void ForgetCredentials();
void ChangeLibrary(const QString& new_library,
bool have_to_disconnect = false);
signals:
signals:
void Connected();
// QMap, key : library's id, value : library's name
void GetLibrariesFinishedSignal(QMap<QString, QString>);
void UpdatingLibrariesFinishedSignal();
private slots:
// Will emit the signal
@ -143,10 +145,17 @@ class SeafileService : public CloudFileService {
// argument
bool CheckReply(QNetworkReply** reply, int tries = 1);
void StartTaskInProgress();
void FinishedTaskInProgress();
SeafileTree tree_;
QString access_token_;
QString server_;
QString library_updated_;
int indexing_task_id_;
int indexing_task_max_;
int indexing_task_progress_;
};
#endif // INTERNET_SEAFILE_SEAFILESERVICE_H_