void RepoService::openLocalFile(const QString& repo_id,
                                const QString& path_in_repo,
                                QWidget *dialog_parent)
{
    LocalRepo r;

    seafApplet->rpcClient()->getLocalRepo(repo_id, &r);

    if (r.isValid()) {
        QString path = QDir(r.worktree).filePath(path_in_repo);

        openFile(path);
    } else {
        ServerRepo repo = getRepo(repo_id);
        if (!repo.isValid()) {
            return;
        }

        const QString path = "/" + path_in_repo;
        const Account account = seafApplet->accountManager()->currentAccount();
        DataManager data_mgr(account);

        // endless loop for setPasswordDialog
        while(1) {
            FileDownloadTask *task = data_mgr.createDownloadTask(repo_id, path);
            FileBrowserProgressDialog dialog(task, dialog_parent);
            task->start();
            if (dialog.exec()) {
                QString full_path = data_mgr.getLocalCachedFile(repo_id, path, task->fileId());
                if (!full_path.isEmpty())
                    openFile(full_path);
                break;
            }
            // if the user canceled the task, don't bother it
            if (task->error() == FileNetworkTask::TaskCanceled)
                break;
            // if the repository is encrypted and password is incorrect
            if (repo.encrypted && task->httpErrorCode() == 400) {
                SetRepoPasswordDialog password_dialog(repo, dialog_parent);
                if (password_dialog.exec())
                    continue;
                // the user cancel the dialog here? skip
                break;
            }
            QString msg =
                QObject::tr("Unable to download item \"%1\"").arg(path_in_repo);
            seafApplet->warningBox(msg);
            break;
        };
    }
}
void FileDownloadHelper::downloadFile(const QString &id)
{
    DataManager data_mgr(account_);

    QString cached_file = data_mgr.getLocalCachedFile(repo_.id, path_, id);
    if (!cached_file.isEmpty()) {
        openFile(cached_file, false);
        return;
    }

    // endless loop for setPasswordDialog
    while(true) {
        FileDownloadTask *task = data_mgr.createDownloadTask(repo_.id, path_);
        FileBrowserProgressDialog dialog(task, parent_);
        if (dialog.exec()) {
            QString full_path =
                data_mgr.getLocalCachedFile(repo_.id, path_, task->fileId());
            if (!full_path.isEmpty())
                openFile(full_path, true);
            break;
        }
        // if the user canceled the task, don't bother it
        if (task->error() == FileNetworkTask::TaskCanceled)
            break;
        // if the repo_sitory is encrypted and password is incorrect
        if (repo_.encrypted && task->httpErrorCode() == 400) {
            SetRepoPasswordDialog password_dialog(repo_, parent_);
            if (password_dialog.exec())
                continue;
            // the user canceled the dialog? skip
            break;
        }
        QString msg =
            QObject::tr("Unable to download item \"%1\"").arg(path_);
        seafApplet->warningBox(msg);
        break;
    }
}