2023-04-03 08:10:32 +05:30
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
|
|
|
* Prism Launcher - Minecraft Launcher
|
|
|
|
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
|
|
|
|
* Copyright (c) 2023 Rachel Powers <508861+Ryex@users.noreply.github.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 3.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following copyright and
|
|
|
|
* permission notice:
|
|
|
|
*
|
|
|
|
* Copyright 2013-2021 MultiMC Contributors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2022-06-12 21:37:11 +05:30
|
|
|
#pragma once
|
|
|
|
|
2023-03-30 23:52:55 +05:30
|
|
|
#include <QHash>
|
2022-06-12 21:37:11 +05:30
|
|
|
#include <QQueue>
|
|
|
|
#include <QSet>
|
2023-05-07 07:40:58 +05:30
|
|
|
#include <QUuid>
|
2023-03-30 23:52:55 +05:30
|
|
|
#include <memory>
|
2022-06-12 21:37:11 +05:30
|
|
|
|
|
|
|
#include "tasks/Task.h"
|
|
|
|
|
|
|
|
class ConcurrentTask : public Task {
|
|
|
|
Q_OBJECT
|
2023-05-07 07:40:58 +05:30
|
|
|
public:
|
2023-01-25 01:22:09 +05:30
|
|
|
using Ptr = shared_qobject_ptr<ConcurrentTask>;
|
|
|
|
|
2022-06-12 21:37:11 +05:30
|
|
|
explicit ConcurrentTask(QObject* parent = nullptr, QString task_name = "", int max_concurrent = 6);
|
2022-07-25 01:46:14 +05:30
|
|
|
~ConcurrentTask() override;
|
|
|
|
|
|
|
|
bool canAbort() const override { return true; }
|
2022-06-12 21:37:11 +05:30
|
|
|
|
2023-03-31 12:20:29 +05:30
|
|
|
inline auto isMultiStep() const -> bool override { return totalSize() > 1; };
|
|
|
|
auto getStepProgress() const -> TaskStepProgressList override;
|
2022-06-12 21:37:11 +05:30
|
|
|
|
|
|
|
void addTask(Task::Ptr task);
|
|
|
|
|
2023-05-07 07:40:58 +05:30
|
|
|
public slots:
|
2022-06-12 21:37:11 +05:30
|
|
|
bool abort() override;
|
|
|
|
|
2022-12-15 22:15:50 +05:30
|
|
|
/** Resets the internal state of the task.
|
|
|
|
* This allows the same task to be re-used.
|
|
|
|
*/
|
|
|
|
void clear();
|
|
|
|
|
2023-05-07 07:40:58 +05:30
|
|
|
protected slots:
|
2022-06-12 21:37:11 +05:30
|
|
|
void executeTask() override;
|
|
|
|
|
|
|
|
virtual void startNext();
|
|
|
|
|
|
|
|
void subTaskSucceeded(Task::Ptr);
|
2023-05-07 07:40:58 +05:30
|
|
|
void subTaskFailed(Task::Ptr, const QString& msg);
|
|
|
|
void subTaskStatus(Task::Ptr task, const QString& msg);
|
|
|
|
void subTaskDetails(Task::Ptr task, const QString& msg);
|
2023-03-30 23:52:55 +05:30
|
|
|
void subTaskProgress(Task::Ptr task, qint64 current, qint64 total);
|
2023-04-08 08:14:57 +05:30
|
|
|
void subTaskStepProgress(Task::Ptr task, TaskStepProgress const& task_step_progress);
|
2022-06-12 21:37:11 +05:30
|
|
|
|
2023-05-07 07:40:58 +05:30
|
|
|
protected:
|
2022-12-17 21:25:03 +05:30
|
|
|
// NOTE: This is not thread-safe.
|
|
|
|
[[nodiscard]] unsigned int totalSize() const { return m_queue.size() + m_doing.size() + m_done.size(); }
|
|
|
|
|
2023-04-08 01:31:45 +05:30
|
|
|
enum class Operation { ADDED, REMOVED, CHANGED };
|
|
|
|
void updateStepProgress(TaskStepProgress const& changed_progress, Operation);
|
2022-06-12 21:37:11 +05:30
|
|
|
|
|
|
|
virtual void updateState();
|
|
|
|
|
2023-05-07 07:40:58 +05:30
|
|
|
protected:
|
2022-06-12 21:37:11 +05:30
|
|
|
QString m_name;
|
|
|
|
QString m_step_status;
|
|
|
|
|
|
|
|
QQueue<Task::Ptr> m_queue;
|
|
|
|
|
2023-05-07 07:40:58 +05:30
|
|
|
QHash<Task*, Task::Ptr> m_doing;
|
2022-06-12 21:37:11 +05:30
|
|
|
QHash<Task*, Task::Ptr> m_done;
|
|
|
|
QHash<Task*, Task::Ptr> m_failed;
|
2023-03-30 23:52:55 +05:30
|
|
|
QHash<Task*, Task::Ptr> m_succeeded;
|
|
|
|
|
|
|
|
QHash<QUuid, std::shared_ptr<TaskStepProgress>> m_task_progress;
|
2022-06-12 21:37:11 +05:30
|
|
|
|
|
|
|
int m_total_max_size;
|
|
|
|
|
|
|
|
qint64 m_stepProgress = 0;
|
|
|
|
qint64 m_stepTotalProgress = 100;
|
|
|
|
|
|
|
|
bool m_aborted = false;
|
|
|
|
};
|