2014-08-11 19:47:16 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) by Olivier Goffart <ogoffart@woboq.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; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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 "discoveryphase.h"
|
2014-08-11 20:41:42 +04:00
|
|
|
#include <csync_private.h>
|
2014-08-12 13:43:42 +04:00
|
|
|
#include <qdebug.h>
|
2014-08-11 20:41:42 +04:00
|
|
|
|
2014-10-17 14:02:26 +04:00
|
|
|
#include <QUrl>
|
|
|
|
|
2014-08-18 16:45:48 +04:00
|
|
|
namespace Mirall {
|
|
|
|
|
2014-10-11 18:23:32 +04:00
|
|
|
bool DiscoveryJob::isInSelectiveSyncBlackList(const QString& path) const
|
2014-08-11 20:41:42 +04:00
|
|
|
{
|
2014-08-15 14:29:10 +04:00
|
|
|
if (_selectiveSyncBlackList.isEmpty()) {
|
|
|
|
// If there is no black list, everything is allowed
|
|
|
|
return false;
|
2014-08-11 20:41:42 +04:00
|
|
|
}
|
|
|
|
|
2014-08-15 14:29:10 +04:00
|
|
|
// If one of the item in the black list is a prefix of the path, it means this path need not to
|
2014-08-11 20:41:42 +04:00
|
|
|
// be synced.
|
|
|
|
//
|
|
|
|
// We know the list is sorted (for it is done in DiscoveryJob::start)
|
2014-08-15 14:29:10 +04:00
|
|
|
// So we can do a binary search. If the path is a prefix if another item or right after in the lexical order.
|
2014-08-11 20:41:42 +04:00
|
|
|
|
2014-08-12 13:43:42 +04:00
|
|
|
QString pathSlash = path + QLatin1Char('/');
|
2014-08-11 20:41:42 +04:00
|
|
|
|
2014-08-15 14:29:10 +04:00
|
|
|
auto it = std::lower_bound(_selectiveSyncBlackList.begin(), _selectiveSyncBlackList.end(), pathSlash);
|
2014-08-11 20:41:42 +04:00
|
|
|
|
2014-10-09 17:11:04 +04:00
|
|
|
if (it != _selectiveSyncBlackList.end() && *it == pathSlash) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-15 14:29:10 +04:00
|
|
|
if (it == _selectiveSyncBlackList.begin()) {
|
2014-08-11 20:41:42 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
--it;
|
2014-10-22 15:29:59 +04:00
|
|
|
Q_ASSERT(it->endsWith(QLatin1Char('/'))); // Folder::setSelectiveSyncBlackList makes sure of that
|
2014-10-09 17:11:04 +04:00
|
|
|
if (pathSlash.startsWith(*it)) {
|
2014-08-11 20:41:42 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-11 18:23:32 +04:00
|
|
|
int DiscoveryJob::isInSelectiveSyncBlackListCallBack(void *data, const char *path)
|
2014-08-11 20:41:42 +04:00
|
|
|
{
|
2014-10-11 18:23:32 +04:00
|
|
|
return static_cast<DiscoveryJob*>(data)->isInSelectiveSyncBlackList(QString::fromUtf8(path));
|
2014-08-11 20:41:42 +04:00
|
|
|
}
|
|
|
|
|
2014-08-18 16:44:43 +04:00
|
|
|
void DiscoveryJob::update_job_update_callback (bool local,
|
|
|
|
const char *dirUrl,
|
|
|
|
void *userdata)
|
|
|
|
{
|
|
|
|
DiscoveryJob *updateJob = static_cast<DiscoveryJob*>(userdata);
|
|
|
|
if (updateJob) {
|
|
|
|
// Don't wanna overload the UI
|
|
|
|
if (!updateJob->lastUpdateProgressCallbackCall.isValid()) {
|
|
|
|
updateJob->lastUpdateProgressCallbackCall.restart(); // first call
|
|
|
|
} else if (updateJob->lastUpdateProgressCallbackCall.elapsed() < 200) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
updateJob->lastUpdateProgressCallbackCall.restart();
|
|
|
|
}
|
|
|
|
|
2014-10-17 14:02:26 +04:00
|
|
|
QString path(QUrl::fromPercentEncoding(QByteArray(dirUrl)).section('/', -1));
|
2014-08-18 16:44:43 +04:00
|
|
|
emit updateJob->folderDiscovered(local, path);
|
|
|
|
}
|
|
|
|
}
|
2014-08-11 19:47:16 +04:00
|
|
|
|
|
|
|
void DiscoveryJob::start() {
|
2014-08-15 14:29:10 +04:00
|
|
|
_selectiveSyncBlackList.sort();
|
2014-10-11 18:23:32 +04:00
|
|
|
_csync_ctx->checkSelectiveSyncBlackListHook = isInSelectiveSyncBlackListCallBack;
|
|
|
|
_csync_ctx->checkSelectiveSyncBlackListData = this;
|
2014-08-18 16:44:43 +04:00
|
|
|
|
|
|
|
_csync_ctx->callbacks.update_callback = update_job_update_callback;
|
|
|
|
_csync_ctx->callbacks.update_callback_userdata = this;
|
|
|
|
|
|
|
|
|
2014-08-11 19:47:16 +04:00
|
|
|
csync_set_log_callback(_log_callback);
|
|
|
|
csync_set_log_level(_log_level);
|
|
|
|
csync_set_log_userdata(_log_userdata);
|
2014-08-18 16:44:43 +04:00
|
|
|
lastUpdateProgressCallbackCall.invalidate();
|
2014-08-21 13:26:34 +04:00
|
|
|
int ret = csync_update(_csync_ctx);
|
|
|
|
|
2014-10-11 18:23:32 +04:00
|
|
|
_csync_ctx->checkSelectiveSyncBlackListHook = 0;
|
|
|
|
_csync_ctx->checkSelectiveSyncBlackListData = 0;
|
2014-08-21 13:26:34 +04:00
|
|
|
|
|
|
|
_csync_ctx->callbacks.update_callback = 0;
|
|
|
|
_csync_ctx->callbacks.update_callback_userdata = 0;
|
|
|
|
|
|
|
|
emit finished(ret);
|
2014-08-11 19:47:16 +04:00
|
|
|
deleteLater();
|
|
|
|
}
|
2014-08-18 16:45:48 +04:00
|
|
|
|
|
|
|
}
|