summaryrefslogtreecommitdiff
path: root/src/queueloader.cpp
blob: c4ad10e84320c0f9dfc1a6525c6f9fe3e95a3360 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <queueloader.h>
#include <cstdlib>
#include <logger.h>
#include <fstream>
#include <cstring>
#include <config.h>
#include <libgen.h>

using namespace newsbeuter;

namespace podbeuter {

queueloader::queueloader(const std::string& file, pb_controller * c) : queuefile(file), ctrl(c) {
}

void queueloader::reload(std::vector<download>& downloads) {
	std::vector<download> dltemp;
	std::fstream f;

	if (downloads.size() > 0) {
		for (std::vector<download>::iterator it=downloads.begin();it!=downloads.end();++it) {
			if (it->status() == DL_DOWNLOADING) { // we are not allowed to reload if a download is in progress!
				GetLogger().log(LOG_INFO, "queueloader::reload: aborting reload due to DL_DOWNLOADING status");
				return;
			}
			if (it->status() == DL_QUEUED || it->status() == DL_CANCELLED || it->status() == DL_FAILED) {
				GetLogger().log(LOG_DEBUG, "queueloader::reload: storing %s to new vector", it->url());
				dltemp.push_back(*it);
			}
		}
	}

	f.open(queuefile.c_str(), std::fstream::in);
	if (f.is_open()) {
		std::string line;
		do {
			getline(f, line);
			if (!f.eof() && line.length() > 0) {
				GetLogger().log(LOG_DEBUG, "queueloader::reload: loaded `%s' from queue file", line.c_str());
				bool url_found = false;
				if (dltemp.size() > 0) {
					for (std::vector<download>::iterator it=dltemp.begin();it!=dltemp.end();++it) {
						if (line == it->url()) {
							GetLogger().log(LOG_INFO, "queueloader::reload: found `%s' in old vector", line.c_str());
							url_found = true;
							break;
						}
					}
				}
				if (downloads.size() > 0) {
					for (std::vector<download>::iterator it=downloads.begin();it!=downloads.end();++it) {
						if (line == it->url()) {
							GetLogger().log(LOG_INFO, "queueloader::reload: found `%s' in new vector", line.c_str());
							url_found = true;
							break;
						}
					}
				}
				if (!url_found) {
					GetLogger().log(LOG_INFO, "queueloader::reload: found `%s' nowhere -> storing to new vector", line.c_str());
					download d(ctrl);
					std::string fn = get_filename(line);
					d.set_filename(fn);
					if (access(fn.c_str(), F_OK)==0) {
						GetLogger().log(LOG_INFO, "queueloader::reload: found `%s' on file system -> mark as already downloaded", fn.c_str());
						d.set_status(DL_ALREADY_DOWNLOADED); // TODO: scrap DL_ALREADY_DOWNLOADED state
					}
					d.set_url(line);
					dltemp.push_back(d);
				}
			}
		} while (!f.eof());
		f.close();
	}

	f.open(queuefile.c_str(), std::fstream::out);
	if (f.is_open()) {
		for (std::vector<download>::iterator it=dltemp.begin();it!=dltemp.end();++it) {
			f << it->url() << std::endl;
		}
		f.close();
	}

	downloads = dltemp;
}

std::string queueloader::get_filename(const std::string& str) {
	std::string fn = ctrl->get_dlpath();

	if (fn[fn.length()-1] != NEWSBEUTER_PATH_SEP[0])
		fn.append(NEWSBEUTER_PATH_SEP);
	char buf[1024];
	snprintf(buf, sizeof(buf), "%s", str.c_str());
	char * base = basename(buf);
	if (!base || strlen(base) == 0) {
		char lbuf[128];
		time_t t = time(NULL);
		strftime(lbuf, sizeof(lbuf), "%Y-%b-%d-%H%M%S.unknown", localtime(&t));
		fn.append(lbuf);
	} else {
		fn.append(base);
	}
	return fn;
}

}