1
#include <pb_controller.h>
3
#include <poddlthread.h>
10
#include <sys/types.h>
15
#include <configcontainer.h>
16
#include <colormanager.h>
17
#include <exceptions.h>
18
#include <queueloader.h>
21
using namespace newsbeuter;
23
static std::string lock_file = "pb-lock.pid";
25
void ctrl_c_action(int sig) {
26
GetLogger().log(LOG_DEBUG,"caugh signal %d",sig);
28
utils::remove_fs_lock(lock_file);
30
fprintf(stderr,"%s\n", _("Segmentation fault."));
37
pb_controller::pb_controller() : v(0), config_file("config"), queue_file("queue"), cfg(0), view_update_(true), max_dls(1), ql(0) {
38
std::ostringstream cfgfile;
41
if (!(cfgdir = ::getenv("HOME"))) {
42
struct passwd * spw = ::getpwuid(::getuid());
46
std::cout << _("Fatal error: couldn't determine home directory!") << std::endl;
48
snprintf(buf, sizeof(buf), _("Please set the HOME environment variable or add a valid user for UID %u!"), ::getuid());
49
std::cout << buf << std::endl;
56
config_dir.append(NEWSBEUTER_PATH_SEP);
57
config_dir.append(NEWSBEUTER_CONFIG_SUBDIR);
58
mkdir(config_dir.c_str(),0700); // create configuration directory if it doesn't exist
60
config_file = config_dir + std::string(NEWSBEUTER_PATH_SEP) + config_file;
61
queue_file = config_dir + std::string(NEWSBEUTER_PATH_SEP) + queue_file;
62
lock_file = config_dir + std::string(NEWSBEUTER_PATH_SEP) + lock_file;
65
pb_controller::~pb_controller() {
69
void pb_controller::run(int argc, char * argv[]) {
73
::signal(SIGINT, ctrl_c_action);
74
::signal(SIGSEGV, ctrl_c_action);
77
if ((c = ::getopt(argc, argv, "C:q:d:l:h")) < 0)
91
case 'd': // this is an undocumented debug commandline option!
92
GetLogger().set_logfile(optarg);
94
case 'l': // this is an undocumented debug commandline option!
96
loglevel level = static_cast<loglevel>(atoi(optarg));
97
if (level > LOG_NONE && level <= LOG_DEBUG)
98
GetLogger().set_loglevel(level);
105
snprintf(msgbuf, sizeof(msgbuf), _("%s: unknown option - %c"), argv[0], static_cast<char>(c));
106
std::cout << msgbuf << std::endl;
112
snprintf(msgbuf, sizeof(msgbuf), _("Starting %s %s..."), "podbeuter", PROGRAM_VERSION);
113
std::cout << msgbuf << std::endl;
116
if (!utils::try_fs_lock(lock_file, pid)) {
117
snprintf(msgbuf, sizeof(msgbuf), _("Error: an instance of %s is already running (PID: %u)"), "podbeuter", pid);
118
std::cout << msgbuf << std::endl;
122
std::cout << _("Loading configuration...");
125
configparser cfgparser(config_file.c_str());
126
cfg = new configcontainer();
127
cfg->register_commands(cfgparser);
128
colormanager * colorman = new colormanager();
129
colorman->register_commands(cfgparser);
132
cfgparser.register_handler("bind-key", &keys);
133
cfgparser.register_handler("unbind-key", &keys);
135
v->set_keymap(&keys);
139
} catch (const configexception& ex) {
140
std::cout << ex.what() << std::endl;
144
if (colorman->colors_loaded())
145
colorman->set_pb_colors(v);
148
max_dls = cfg->get_configvalue_as_int("max-downloads");
150
std::cout << _("done.") << std::endl;
152
ql = new queueloader(queue_file, this);
153
ql->reload(downloads_);
157
std::cout << _("Cleaning up queue...");
160
ql->reload(downloads_);
163
std::cout << _("done.") << std::endl;
165
utils::remove_fs_lock(lock_file);
168
void pb_controller::usage(const char * argv0) {
170
snprintf(buf, sizeof(buf),
171
_("%s %s\nusage %s [-C <file>] [-q <file>] [-h]\n"
172
"-C <configfile> read configuration from <configfile>\n"
173
"-q <queuefile> use <queuefile> as queue file\n"
174
"-h this help\n"), "podbeuter", PROGRAM_VERSION, argv0);
176
::exit(EXIT_FAILURE);
179
std::string pb_controller::get_dlpath() {
180
return cfg->get_configvalue("download-path");
183
unsigned int pb_controller::downloads_in_progress() {
184
unsigned int count = 0;
185
if (downloads_.size() > 0) {
186
for (std::vector<download>::iterator it=downloads_.begin();it!=downloads_.end();++it) {
187
if (it->status() == DL_DOWNLOADING)
194
unsigned int pb_controller::get_maxdownloads() {
198
void pb_controller::reload_queue() {
200
ql->reload(downloads_);
204
double pb_controller::get_total_kbps() {
206
if (downloads_.size() > 0) {
207
for (std::vector<download>::iterator it=downloads_.begin();it!=downloads_.end();++it) {
208
if (it->status() == DL_DOWNLOADING) {
209
result += it->kbps();
216
void pb_controller::start_downloads() {
217
int dl2start = get_maxdownloads() - downloads_in_progress();
218
for (std::vector<download>::iterator it=downloads_.begin();dl2start > 0 && it!=downloads_.end();++it) {
219
if (it->status() == DL_QUEUED) {
220
poddlthread * thread = new poddlthread(&(*it));
227
void pb_controller::increase_parallel_downloads() {
231
void pb_controller::decrease_parallel_downloads() {
236
void pb_controller::play_file(const std::string& str) {
238
std::string player = cfg->get_configvalue("player");
241
cmdline.append(player);
242
cmdline.append(" '");
246
GetLogger().log(LOG_DEBUG, "pb_controller::play_file: running `%s'", cmdline.c_str());
247
::system(cmdline.c_str());