/[debian]/teleschorsch/branches/upstream/current/main.cpp
ViewVC logotype

Contents of /teleschorsch/branches/upstream/current/main.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 576 - (show annotations)
Thu Mar 1 23:40:12 2007 UTC (14 years, 5 months ago) by gregoa
File size: 7099 byte(s)
[svn-upgrade] Integrating new upstream version, teleschorsch (0.0.5)
1 /*
2 # Copyright and License:
3 #
4 # Copyright (C) 2007
5 # gregor herrmann <gregor+debian@comodo.priv.at>,
6 # Philipp Spitzer <philipp@spitzer.priv.at>
7 #
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
19 */
20
21 #include <QtGlobal>
22 #include <QtDebug>
23 #include <QApplication>
24 #include <QVector>
25 #include <QFile>
26 #include <QProcess>
27 #include <QMessageBox>
28 #include "main.h"
29 #include "options.h"
30
31
32 // Functions
33 // =========
34
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";
40 }
41 configInfo.systemConfigFile = "/etc/teleschorschrc";
42
43 // User config file
44 if (!configInfo.userConfigFile.isEmpty()) {
45 QFile configFileUser(configInfo.userConfigFile);
46 if (configFileUser.exists()) {configInfo.usedConfigFile = configInfo.userConfigFile;}
47 }
48
49 // System config file
50 if (configInfo.usedConfigFile.isEmpty()) {
51 QFile configFileSystem(configInfo.systemConfigFile);
52 if (configFileSystem.exists()) configInfo.usedConfigFile = configInfo.systemConfigFile;
53 }
54 }
55
56
57 bool addConfig(const QString& fileName, ChannelVec& cv, QString& error) {
58 QFile file(fileName);
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
65
66 // Is the line a [section]?
67 bool section = line.startsWith("[") && line.endsWith("]") && line.size() >= 3;
68 if (section) {
69 if (sectionOccured) cv.push_back(channel);
70 sectionOccured = true;
71 channel = Channel();
72 channel.name = line.mid(1, line.size()-2);
73 }
74
75 // Read properties
76 int eqPos = line.indexOf("=");
77 bool property = !section && eqPos >= 1;
78 if (property) {
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;}
82
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;}
87 }
88
89 if (!section && !property) {error = QObject::tr("Line %1 is not valid.").arg(line); return false;}
90 }
91 if (sectionOccured) cv.push_back(channel);
92 return true;
93 }
94
95
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);
98 QString error;
99 if (!addConfig(configInfo.usedConfigFile, channelVec, error));
100 return error;
101 }
102
103
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];
113 }
114 return "{no match}"; // maybe do some error handling here...
115 }
116
117
118 QString substitudeStaticUrl(const QString& staticUrl, QDate date) {
119 QString result;
120 int pos = 0;
121 int lastPos = 0;
122 while (pos != -1) {
123 pos = staticUrl.indexOf("${", lastPos);
124 if (pos == -1) result += staticUrl.mid(lastPos);
125 else result += staticUrl.mid(lastPos, pos-lastPos);
126 lastPos = pos;
127
128 // Match?
129 if (pos != -1) {
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);
134 lastPos = pos+1;
135 }
136 }
137 return result;
138 }
139
140
141 void appendPlayerOffsetOption(const QString& player, QTime offset, QStringList& arguments) {
142 QTime zero = QTime(0, 0, 0, 0);
143 int offsetSec = zero.secsTo(offset);
144 if (player.indexOf("vlc") != -1) arguments.append("--start-time");
145 if (player.indexOf("gmplayer") != -1) {
146 arguments << "--cache" << "512"; // --cache 512 does not belong to this function but for now...
147 arguments << "-ss";
148 }
149 arguments << QString::number(offsetSec);
150 }
151
152
153
154 // MainDialog
155 // ==========
156
157 MainDialog::MainDialog(QWidget *parent): QDialog(parent) {
158 // User interface
159 setupUi(this);
160 QObject::connect(btnOptions, SIGNAL(clicked()), this, SLOT(editOptions()));
161 QObject::connect(btnStart, SIGNAL(clicked()), this, SLOT(startAction()));
162
163 // Init configInfo
164 initConfigInfo(configInfo);
165
166 // Read config
167 QString error = readChannelVec(configInfo, channelVec);
168 if (!error.isEmpty()) QMessageBox::warning(this, tr("Problem when reading the configuration file"), error);
169
170 // Fill in channels
171 updateLwChannels();
172 }
173
174
175 void MainDialog::editOptions() {
176 OptionsDialog *od = new OptionsDialog();
177 if (od->exec(configInfo.userConfigFile)) {
178 channelVec.clear();
179 QString error = readChannelVec(configInfo, channelVec);
180 if (!error.isEmpty()) QMessageBox::warning(this, tr("Problem when reading the configuration file"), error);
181 updateLwChannels();
182 }
183 delete od;
184 }
185
186
187 void MainDialog::updateLwChannels() {
188 lwChannels->clear();
189 for (int i = 0; i != channelVec.size(); ++i) lwChannels->addItem(channelVec[i].fullName);
190 }
191
192
193 bool MainDialog::startAction() {
194 int row = lwChannels->currentRow();
195 if (row > 0) {
196 Channel channel = channelVec[row];
197 QDate date = calDate->selectedDate();
198 QStringList arguments;
199 arguments.append(substitudeStaticUrl(channel.staticUrl, date));
200 appendPlayerOffsetOption(channel.player, teOffset->time(), arguments);
201 // arguments << "-style" << "motif";
202 QProcess player(this);
203 qDebug() << channel.player << arguments;
204 player.start(channel.player, arguments);
205 player.waitForFinished();
206 return true;
207 }
208 return false;
209 }
210
211
212
213 // Main function
214 // =============
215
216 int main(int argc, char *argv[]) {
217 QApplication app(argc, argv);
218 MainDialog *mainDialog = new MainDialog();
219 mainDialog->show();
220 return app.exec();
221 }

  ViewVC Help
Powered by ViewVC 1.1.26