summaryrefslogtreecommitdiff
path: root/include/queueloader.h
blob: 481017d8f1f1f88b9c2e43631c1c90a805a86f39 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#ifndef PODBOAT_QUEUELOADER_H_
#define PODBOAT_QUEUELOADER_H_

#include <functional>
#include <vector>

#include "3rd-party/optional.hpp"

#include "configcontainer.h"
#include "download.h"

namespace podboat {

/// Synchronizes Podboat's array of downloads with the queue file on the
/// filesystem.
class QueueLoader {
public:
	/// Create a loader that will work with the queue file at \a filepath.
	/// `Download` objects will be given \a cb_require_view_update as an
	/// argument.
	QueueLoader(const std::string& filepath, const newsboat::ConfigContainer& cfg,
		std::function<void()> cb_require_view_update);

	/// Synchronize the queue file with \a downloads. Downloads with `DELETED`
	/// status are removed. If \a also_remove_finished is `true`, `FINISHED`
	/// downloads are removed too.
	void reload(std::vector<Download>& downloads,
		bool also_remove_finished = false) const;

private:
	std::string get_filename(const std::string& str) const;

	const std::string queuefile;
	const newsboat::ConfigContainer& cfg;
	std::function<void()> cb_require_view_update;

	/// A helper type for methods that process the queue file.
	struct CategorizedDownloads {
		/// Downloads that should be kept in the queue file.
		std::vector<Download> to_keep;

		/// Downloads that should be deleted from the queue file.
		std::vector<Download> to_delete;
	};

	/// Splits downloads into "to keep" and "to delete" categories depending on
	/// their status.
	///
	/// If `also_remove_finished` is `true`, downloads with `FINISHED` status
	/// are put into "to delete" category.
	///
	/// Returns:
	/// - nullopt if one of the downloads is currently being downloaded;
	/// - otherwise, a struct with categorized downloads.
	static nonstd::optional<CategorizedDownloads> categorize_downloads(
		const std::vector<Download>& downloads, bool also_remove_finished);

	/// Adds downloads from the queue file to the "to keep" category.
	void update_from_queue_file(CategorizedDownloads& downloads) const;

	/// Writes "to keep" downloads to the queue file.
	void write_queue_file(const CategorizedDownloads& downloads) const;

	/// If `delete-played-files` is enabled, deletes downloaded files
	/// corresponding to downloads in the "to delete" category.
	void delete_played_files(const CategorizedDownloads& downloads) const;
};

} // namespace podboat

#endif /* PODBOAT_QUEUELOADER_H_ */