Merge pull request #4758 from Chocobozzz/seafile_info

Try to make Seafile implementation more robust
This commit is contained in:
John Maguire 2015-03-02 12:17:48 +01:00
commit 66333d2940
5 changed files with 113 additions and 10 deletions

View File

@ -68,8 +68,8 @@ CloudFileService::CloudFileService(Application* app, InternetModel* parent,
library_sort_model_->setSortLocaleAware(true);
library_sort_model_->sort(0);
app->global_search()->AddProvider(new CloudFileSearchProvider(
library_backend_, service_id, icon_, this));
app->global_search()->AddProvider(
new CloudFileSearchProvider(library_backend_, service_id, icon_, this));
}
QStandardItem* CloudFileService::CreateRootItem() {
@ -160,6 +160,8 @@ void CloudFileService::MaybeAddFileToDatabase(const Song& metadata,
TagReaderClient::ReplyType* reply = app_->tag_reader_client()->ReadCloudFile(
download_url, metadata.title(), metadata.filesize(), mime_type,
authorisation);
pending_tagreader_replies_.append(reply);
NewClosure(reply, SIGNAL(Finished(bool)), this,
SLOT(ReadTagsFinished(TagReaderClient::ReplyType*, Song)), reply,
metadata);
@ -167,8 +169,17 @@ void CloudFileService::MaybeAddFileToDatabase(const Song& metadata,
void CloudFileService::ReadTagsFinished(TagReaderClient::ReplyType* reply,
const Song& metadata) {
int index_reply;
reply->deleteLater();
if ((index_reply = pending_tagreader_replies_.indexOf(reply)) == -1) {
qLog(Debug) << "Ignore the reply";
return;
}
pending_tagreader_replies_.removeAt(index_reply);
indexing_task_progress_++;
if (indexing_task_progress_ == indexing_task_max_) {
task_manager_->SetTaskFinished(indexing_task_id_);
@ -219,3 +230,12 @@ QString CloudFileService::GuessMimeTypeForFile(const QString& filename) const {
}
return QString::null;
}
void CloudFileService::AbortReadTagsReplies() {
qLog(Debug) << "Aborting the read tags replies";
pending_tagreader_replies_.clear();
task_manager_->SetTaskFinished(indexing_task_id_);
indexing_task_id_ = -1;
emit AllIndexingTasksFinished();
}

View File

@ -52,7 +52,7 @@ class CloudFileService : public InternetService {
virtual bool has_credentials() const = 0;
bool is_indexing() const { return indexing_task_id_ != -1; }
signals:
signals:
void AllIndexingTasksFinished();
public slots:
@ -67,6 +67,7 @@ class CloudFileService : public InternetService {
const QString& authorisation);
virtual bool IsSupportedMimeType(const QString& mime_type) const;
QString GuessMimeTypeForFile(const QString& filename) const;
void AbortReadTagsReplies();
protected slots:
void ShowCoverManager();
@ -86,6 +87,7 @@ class CloudFileService : public InternetService {
std::unique_ptr<AlbumCoverManager> cover_manager_;
PlaylistManager* playlist_manager_;
TaskManager* task_manager_;
QList<TagReaderClient::ReplyType*> pending_tagreader_replies_;
private:
QIcon icon_;

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,11 @@ 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),
changing_libary_(false) {
QSettings s;
s.beginGroup(kSettingsGroup);
access_token_ = s.value("access_token").toString();
@ -175,6 +180,21 @@ void SeafileService::GetLibrariesFinished(QNetworkReply* reply) {
}
void SeafileService::ChangeLibrary(const QString& new_library) {
if (new_library == library_updated_ || changing_libary_) return;
if (indexing_task_id_ != -1) {
qLog(Debug) << "Want to change the Seafile library, but Clementine waits "
"the previous indexing...";
changing_libary_ = true;
NewClosure(this, SIGNAL(UpdatingLibrariesFinishedSignal()), this,
SLOT(ChangeLibrary(QString)), new_library);
return;
}
AbortReadTagsReplies();
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()) {
@ -184,6 +204,7 @@ void SeafileService::ChangeLibrary(const QString& new_library) {
}
}
changing_libary_ = false;
UpdateLibraries();
}
@ -196,6 +217,14 @@ void SeafileService::Connect() {
}
void SeafileService::UpdateLibraries() {
// Quit if we are already updating the libraries
if (indexing_task_id_ != -1) {
return;
}
indexing_task_id_ =
app_->task_manager()->StartTask(tr("Building Seafile index..."));
connect(this, SIGNAL(GetLibrariesFinishedSignal(QMap<QString, QString>)),
this, SLOT(UpdateLibrariesInProgress(QMap<QString, QString>)));
@ -211,14 +240,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 +267,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 +276,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 +299,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 +314,7 @@ void SeafileService::FetchAndCheckFolderItemsFinished(
if (!CheckReply(&reply)) {
qLog(Warning)
<< "Something wrong with the reply... (FetchFolderItemsToList)";
FinishedTaskInProgress();
return;
}
@ -300,10 +345,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 +365,7 @@ void SeafileService::AddRecursivelyFolderItemsFinished(QNetworkReply* reply,
const QString& path) {
if (!CheckReply(&reply)) {
qLog(Warning) << "Something wrong with the reply... (FetchFolderItems)";
FinishedTaskInProgress();
return;
}
@ -343,9 +393,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 +404,8 @@ void SeafileService::AddRecursivelyFolderItemsFinished(QNetworkReply* reply,
MaybeAddFileEntry(entry.name(), library, path);
}
}
FinishedTaskInProgress();
}
QNetworkReply* SeafileService::PrepareFetchContentForFile(
@ -593,6 +644,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,17 @@ 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);
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 +144,19 @@ 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_;
bool changing_libary_;
};
#endif // INTERNET_SEAFILE_SEAFILESERVICE_H_

View File

@ -134,6 +134,8 @@ void SeafileSettingsPage::Login() {
}
void SeafileSettingsPage::Logout() {
// Forget the songs added
service_->ChangeLibrary("none");
service_->ForgetCredentials();
// We choose to keep the server