summaryrefslogtreecommitdiff
path: root/src/VodcatcherPlugin.cc
blob: 1474e77bcfd6a35a7f2f01ef716a5a1381041139 (plain)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * vdr-vodcatcher - A plugin for the Linux Video Disk Recorder
 * Copyright (c) 2007 - 2009 Tobias Grimm <vdr@e-tobi.net>
 *
 * 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; either version 2 of the License, or (at your option)
 * any later version.
 *
 * 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, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */

#include "VodcatcherPlugin.h"
#include <iostream>
#include <getopt.h>
#include "Version.h"
#include "IFeedUpdater.h"
#include "IErrorLogger.h"
#include "IDownloadCache.h"
#include "IServiceLocator.h"
#include "Thread.h"

using namespace std;

VodcatcherPlugin::VodcatcherPlugin() :
    cacheDirName(DEFAULT_CACHE_DIR), _maxCacheAge(30), _playBackQuality(High)
{
}

void VodcatcherPlugin::SetServiceLocator(RefPtr<IServiceLocator> serviceLocator)
{
    this->serviceLocator = serviceLocator;
}

const char* VodcatcherPlugin::Version(void)
{
    return VERSION;
}

const char* VodcatcherPlugin::Description(void)
{
    return tr("Browse and play video podcasts");
}

const char* VodcatcherPlugin::MainMenuEntry(void)
{
    return tr("Vodcatcher");
}

bool VodcatcherPlugin::Initialize(void)
{
    const char* configDir = ConfigDirectory();

    if (configDir)
    {
        sourcesFileName = string(configDir) + "/vodcatchersources.conf";
    }

    if (CacheDirIsAccessible())
    {
        return true;
    }
    else
    {
        serviceLocator->GetErrorLogger()->LogError("Unable to access cache directory `" + GetCacheDirName() + "`");
        return false;
    }
}

const char* VodcatcherPlugin::CommandLineHelp(void)
{
    return "  -c, --cache=CACHE_DIR    specify cache dir, defaults to "
        "/var/cache/vdr-plugin-vodcatcher";
}

bool VodcatcherPlugin::ProcessArgs(int argc, char* argv[])
{
    static struct option longOptions[] =
        {
            { "cache", required_argument, NULL, 'c' },
            { NULL } };

    optind = 0;
    opterr = 0;

    int optionChar;
    int optionIndex = 0;
    while ((optionChar = getopt_long(argc, argv, "c:", longOptions, &optionIndex)) != -1)
    {
        switch (optionChar)
        {
            case 'c':
            {
                cacheDirName = optarg;
                break;
            }
            default:
            {
                cerr << argv[0] << ": " << "invalid option " << argv[optind-1] << endl;
                return false;
            }
        }
    }
    return true;
}

const string VodcatcherPlugin::GetCacheDirName() const
{
    return cacheDirName;
}

const string VodcatcherPlugin::GetSourcesFileName() const
{
    return sourcesFileName;
}

int VodcatcherPlugin::GetMaxCacheAge() const
{
    return _maxCacheAge;
}

void VodcatcherPlugin::SetMaxCacheAge(const int age)
{
    _maxCacheAge = age;
}

void VodcatcherPlugin::SetPlayBackQuality(const StreamType quality)
{
    _playBackQuality = quality;
}

StreamType VodcatcherPlugin::GetPlayBackQuality() const
{
    return _playBackQuality;
}

void VodcatcherPlugin::SetMediaPlayerType(const MediaPlayerType mediaPlayerType)
{
    _mediaPlayerType = mediaPlayerType;
}

MediaPlayerType VodcatcherPlugin::GetMediaPlayerType() const
{
	return _mediaPlayerType;
}

cOsdObject* VodcatcherPlugin::MainMenuAction()
{
    return serviceLocator->CreateFeedMenu();
}

bool VodcatcherPlugin::Start()
{
    serviceLocator->GetDownloadThread()->Start();
    serviceLocator->GetFeedUpdater()->Update();
    return true;
}

void VodcatcherPlugin::Stop()
{
    serviceLocator->GetDownloadThread()->Stop();
}

void VodcatcherPlugin::Housekeeping()
{
    serviceLocator->GetFeedUpdater()->Update();
    serviceLocator->GetDownloadCache()->CleanUp(24 * GetMaxCacheAge());
}

bool VodcatcherPlugin::CacheDirIsAccessible()
{
    if (0 == access((GetCacheDirName()+"/.").c_str(), W_OK))
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool VodcatcherPlugin::SetupParse(const char* Name, const char* Value)
{
    if (string(Name) == "MaxCacheAge")
    {
        _maxCacheAge = atoi(Value);
        return true;
    }

    if (string(Name) == "MediaPlayerType")
    {
        _mediaPlayerType = (MediaPlayerType) atoi(Value);
        return true;
    }

    return false;
}

cMenuSetupPage* VodcatcherPlugin::SetupMenu()
{
    return serviceLocator->CreateSetupMenu();
}