2 # Copyright and License:
5 # gregor herrmann <gregor+debian@comodo.priv.at>,
6 # Philipp Spitzer <philipp@spitzer.priv.at>
8 # This program is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License version 2 as published
10 # by the Free Software Foundation.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA, or point
18 # your browser to http://www.gnu.org/licenses/gpl.html
23 #include <QApplication>
27 #include <QMessageBox>
35 void initConfigInfo(ConfigInfo& configInfo) {
36 QStringList environment = QProcess::systemEnvironment().filter(QRegExp("^HOME=")); // ("HOME=/home/gregoa")
37 if (environment.size() == 1) {
38 configInfo.home = environment.at(0).mid(5);
39 configInfo.userConfigFile = configInfo.home + + "/.teleschorschrc";
41 configInfo.systemConfigFile = "/etc/teleschorschrc";
44 if (!configInfo.userConfigFile.isEmpty()) {
45 QFile configFileUser(configInfo.userConfigFile);
46 if (configFileUser.exists()) {configInfo.usedConfigFile = configInfo.userConfigFile;}
50 if (configInfo.usedConfigFile.isEmpty()) {
51 QFile configFileSystem(configInfo.systemConfigFile);
52 if (configFileSystem.exists()) configInfo.usedConfigFile = configInfo.systemConfigFile;
57 bool addConfig(const QString& fileName, ChannelVec& cv, QString& error) {
59 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {error = QObject::tr("Could not open file %1").arg(fileName); return false;}
60 Channel channel; // current channel
61 bool sectionOccured = false;
62 while (!file.atEnd()) {
63 QString line = QString(file.readLine()).trimmed();
64 if (line.startsWith("#") || line.startsWith(";") || line.isEmpty()) continue; // strip comments
66 // Is the line a [section]?
67 bool section = line.startsWith("[") && line.endsWith("]") && line.size() >= 3;
69 if (sectionOccured) cv.push_back(channel);
70 sectionOccured = true;
72 channel.name = line.mid(1, line.size()-2);
76 int eqPos = line.indexOf("=");
77 bool property = !section && eqPos >= 1;
79 QString propKey = line.left(eqPos);
80 QString propVal = line.right(line.size()-eqPos-1);
81 if (!sectionOccured) {error = QObject::tr("Property %1 is only allowed in a [section].").arg(propKey); return false;}
83 if (propKey == "FULLNAME") channel.fullName = propVal;
84 else if (propKey == "STATICURL") channel.staticUrl = propVal;
85 else if (propKey == "PLAYER") channel.player = propVal;
86 else {error = QObject::tr("Unknown key in ini file: %1").arg(propKey); return false;}
89 if (!section && !property) {error = QObject::tr("Line %1 is not valid.").arg(line); return false;}
91 if (sectionOccured) cv.push_back(channel);
96 QString readChannelVec(const ConfigInfo& configInfo, ChannelVec& channelVec) {
97 if (configInfo.usedConfigFile.isEmpty()) return QObject::tr("Neither %1 nor %2 found.").arg(configInfo.systemConfigFile).arg(configInfo.userConfigFile);
99 if (!addConfig(configInfo.usedConfigFile, channelVec, error));
104 QString substitudeVar(const QString& var, QDate date) {
105 if (var == "d") return date.toString("dd");
106 if (var == "m") return date.toString("MM");
107 if (var == "y") return date.toString("yy");
108 if (var == "Y") return date.toString("yyyy");
109 if (var == "dow_DE") {
110 int dow = date.dayOfWeek() - 1;
111 static const char dow_de[][20] = {"Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"};
112 if (dow >= 0 && dow < 7) return dow_de[dow];
114 return "{no match}"; // maybe do some error handling here...
118 QString substitudeStaticUrl(const QString& staticUrl, QDate date) {
123 pos = staticUrl.indexOf("${", lastPos);
124 if (pos == -1) result += staticUrl.mid(lastPos);
125 else result += staticUrl.mid(lastPos, pos-lastPos);
130 pos = staticUrl.indexOf("}", lastPos);
131 if (pos == -1) return ""; // ${ not closed with }. Some day we should report the error somehow...
132 QString var = staticUrl.mid(lastPos+2, pos-lastPos-2);
133 result += substitudeVar(var, date);
138 // Evaluate staticUrl - it might be dynamic despite its name :-)
139 if (result.left(1) == "`" && result.right(1) == "`") {
141 QString command = "/bin/sh -c \"echo " + result + "\"";
142 evalUrl.start(command);
143 if (evalUrl.waitForFinished()) {
144 QByteArray newResult = evalUrl.readAllStandardOutput();
145 if (result != newResult) {result = newResult.trimmed();}
152 void appendPlayerOffsetOption(const QString& player, QTime offset, QStringList& arguments) {
153 QTime zero = QTime(0, 0, 0, 0);
154 int offsetSec = zero.secsTo(offset);
155 if (player.indexOf("vlc") != -1) arguments.append("--start-time");
156 if (player.indexOf("gmplayer") != -1) {
157 arguments << "--cache" << "512"; // --cache 512 does not belong to this function but for now...
160 arguments << QString::number(offsetSec);
168 MainDialog::MainDialog(QWidget *parent): QDialog(parent) {
171 QObject::connect(btnOptions, SIGNAL(clicked()), this, SLOT(editOptions()));
172 QObject::connect(btnStart, SIGNAL(clicked()), this, SLOT(startAction()));
175 initConfigInfo(configInfo);
178 QString error = readChannelVec(configInfo, channelVec);
179 if (!error.isEmpty()) QMessageBox::warning(this, tr("Problem when reading the configuration file"), error);
186 void MainDialog::editOptions() {
187 OptionsDialog *od = new OptionsDialog();
188 if (od->exec(configInfo.userConfigFile)) {
190 QString error = readChannelVec(configInfo, channelVec);
191 if (!error.isEmpty()) QMessageBox::warning(this, tr("Problem when reading the configuration file"), error);
198 void MainDialog::updateLwChannels() {
200 for (int i = 0; i != channelVec.size(); ++i) lwChannels->addItem(channelVec[i].fullName);
204 bool MainDialog::startAction() {
205 int row = lwChannels->currentRow();
207 Channel channel = channelVec[row];
208 QDate date = calDate->selectedDate();
209 QStringList arguments;
210 arguments.append(substitudeStaticUrl(channel.staticUrl, date));
211 appendPlayerOffsetOption(channel.player, teOffset->time(), arguments);
212 QProcess player(this);
213 qDebug() << channel.player << arguments;
214 player.start(channel.player, arguments);
215 player.waitForFinished();
226 int main(int argc, char *argv[]) {
227 QApplication app(argc, argv);
228 MainDialog *mainDialog = new MainDialog();