[svn-upgrade] Integrating new upstream version, teleschorsch (0.0.5)
[debian/teleschorsch.git] / main.cpp
index 8d0632fa2555574150f21ba5b66f71919a4d9df8..eca80870198b4a3413e1b452c7ae8054a784d714 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -101,6 +101,55 @@ QString readChannelVec(const ConfigInfo& configInfo, ChannelVec& channelVec) {
 }
 
 
+QString substitudeVar(const QString& var, QDate date) {
+       if (var == "d") return date.toString("dd");
+       if (var == "m") return date.toString("MM");
+       if (var == "y") return date.toString("yy");
+       if (var == "Y") return date.toString("yyyy");
+       if (var == "dow_DE") {
+               int dow = date.dayOfWeek() - 1;
+               static const char dow_de[][20] = {"Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"};
+               if (dow >= 0 && dow < 7) return dow_de[dow];
+       }
+       return "{no match}"; // maybe do some error handling here...
+}
+
+
+QString substitudeStaticUrl(const QString& staticUrl, QDate date) {
+       QString result;
+       int pos = 0;
+       int lastPos = 0;
+       while (pos != -1) {
+               pos = staticUrl.indexOf("${", lastPos);
+               if (pos == -1) result += staticUrl.mid(lastPos);
+               else result += staticUrl.mid(lastPos, pos-lastPos);
+               lastPos = pos;
+               
+               // Match?
+               if (pos != -1) {
+                       pos = staticUrl.indexOf("}", lastPos);
+                       if (pos == -1) return ""; // ${ not closed with }. Some day we should report the error somehow...
+                       QString var = staticUrl.mid(lastPos+2, pos-lastPos-2);
+                       result += substitudeVar(var, date);
+                       lastPos = pos+1;
+               }
+       }
+       return result;
+}
+
+
+void appendPlayerOffsetOption(const QString& player, QTime offset, QStringList& arguments) {
+       QTime zero = QTime(0, 0, 0, 0);
+       int offsetSec = zero.secsTo(offset);
+       if (player.indexOf("vlc") != -1) arguments.append("--start-time");
+       if (player.indexOf("gmplayer") != -1) {
+               arguments << "--cache" << "512"; // --cache 512 does not belong to this function but for now...
+               arguments << "-ss"; 
+       }
+       arguments << QString::number(offsetSec);
+}
+
+
 
 // MainDialog
 // ==========
@@ -145,14 +194,14 @@ bool MainDialog::startAction() {
        int row = lwChannels->currentRow();
        if (row > 0) {
                Channel channel = channelVec[row];
-               qDebug() << "User would like to see " << channel.staticUrl;
-
-               QString program = "/usr/bin/vlc";
+               QDate date = calDate->selectedDate();
                QStringList arguments;
+               arguments.append(substitudeStaticUrl(channel.staticUrl, date));
+               appendPlayerOffsetOption(channel.player, teOffset->time(), arguments);
                // arguments << "-style" << "motif";
-               // @args=("$player $url " . ${playeroptions}{$player} . " $offsetseconds");
-               QProcess player(0);
-               player.start(program, arguments);
+               QProcess player(this);
+               qDebug() << channel.player << arguments;
+               player.start(channel.player, arguments);
                player.waitForFinished();
                return true;
        }