00001 #include "libmyth/mythcontext.h"
00002 #include "libmyth/mythdbcon.h"
00003 #include <qsqldatabase.h>
00004 #include <qheader.h>
00005 #include <qcursor.h>
00006 #include <qlayout.h>
00007 #include <iostream>
00008 #include "playgroup.h"
00009 #include "programinfo.h"
00010
00011
00012 class PlayGroupDBStorage : public SimpleDBStorage
00013 {
00014 protected:
00015 PlayGroupDBStorage(Setting *_setting,
00016 const PlayGroup &_parent,
00017 QString _name) :
00018 SimpleDBStorage(_setting, "playgroup", _name), parent(_parent)
00019 {
00020 _setting->setName(_name);
00021 }
00022
00023 virtual QString whereClause(MSqlBindings& bindings);
00024
00025 const PlayGroup &parent;
00026 };
00027
00028 QString PlayGroupDBStorage::whereClause(MSqlBindings& bindings)
00029 {
00030 QString nameTag(":WHERENAME");
00031 QString query("name = " + nameTag);
00032
00033 bindings.insert(nameTag, parent.getName().utf8());
00034
00035 return query;
00036 }
00037
00038 class TitleMatch : public LineEditSetting, public PlayGroupDBStorage
00039 {
00040 public:
00041 TitleMatch(const PlayGroup& _parent):
00042 LineEditSetting(this), PlayGroupDBStorage(this, _parent, "titlematch")
00043 {
00044 setLabel(QObject::tr("Title match (regex)"));
00045 setHelpText(QObject::tr("Automatically set new recording rules to "
00046 "use this group if the title matches this "
00047 "regular expression. For example, "
00048 "\"(News|CNN)\" would match any title in "
00049 "which \"News\" or \"CNN\" appears."));
00050 };
00051 };
00052
00053 class SkipAhead : public SpinBoxSetting, public PlayGroupDBStorage
00054 {
00055 public:
00056 SkipAhead(const PlayGroup& _parent):
00057 SpinBoxSetting(this, 0, 600, 5, true,
00058 "(" + QObject::tr("default") + ")"),
00059 PlayGroupDBStorage(this, _parent, "skipahead") {
00060 setLabel(QObject::tr("Skip ahead (seconds)"));
00061 setHelpText(QObject::tr("How many seconds to skip forward on a fast "
00062 "forward."));
00063 };
00064 };
00065
00066 class SkipBack : public SpinBoxSetting, public PlayGroupDBStorage
00067 {
00068 public:
00069 SkipBack(const PlayGroup& _parent):
00070 SpinBoxSetting(this, 0, 600, 5, true,
00071 "(" + QObject::tr("default") + ")"),
00072 PlayGroupDBStorage(this, _parent, "skipback")
00073 {
00074 setLabel(QObject::tr("Skip back (seconds)"));
00075 setHelpText(QObject::tr("How many seconds to skip backward on a "
00076 "rewind."));
00077 };
00078 };
00079
00080 class JumpMinutes : public SpinBoxSetting, public PlayGroupDBStorage
00081 {
00082 public:
00083 JumpMinutes(const PlayGroup& _parent):
00084 SpinBoxSetting(this, 0, 30, 10, true,
00085 "(" + QObject::tr("default") + ")"),
00086 PlayGroupDBStorage(this, _parent, "jump")
00087 {
00088 setLabel(QObject::tr("Jump amount (in minutes)"));
00089 setHelpText(QObject::tr("How many minutes to jump forward or backward "
00090 "when the jump keys are pressed."));
00091 };
00092 };
00093
00094 class TimeStretch : public SpinBoxSetting, public PlayGroupDBStorage
00095 {
00096 public:
00097 TimeStretch(const PlayGroup& _parent):
00098 SpinBoxSetting(this, 45, 200, 5, false,
00099 "(" + QObject::tr("default") + ")"),
00100 PlayGroupDBStorage(this, _parent, "timestretch")
00101 {
00102 setValue(45);
00103 setLabel(QObject::tr("Time stretch (speed x 100)"));
00104 setHelpText(QObject::tr("Initial playback speed with adjusted audio. "
00105 "Use 100 for normal speed, 50 for half speed "
00106 "and 200 for double speed."));
00107 };
00108
00109 virtual void load(void) {
00110 PlayGroupDBStorage::load();
00111 if (intValue() < 50 || intValue() > 200)
00112 setValue(45);
00113 };
00114
00115 virtual void save(void) {
00116 if (intValue() < 50 || intValue() > 200)
00117 {
00118
00119
00120
00121 IntegerSetting::setValue(0);
00122 }
00123 PlayGroupDBStorage::save();
00124 }
00125 };
00126
00127 PlayGroup::PlayGroup(QString _name)
00128 : name(_name)
00129 {
00130 ConfigurationGroup* cgroup = new VerticalConfigurationGroup(false);
00131 cgroup->setLabel(getName() + " " + QObject::tr("Group", "Play Group"));
00132
00133 cgroup->addChild(new TitleMatch(*this));
00134 cgroup->addChild(new SkipAhead(*this));
00135 cgroup->addChild(new SkipBack(*this));
00136 cgroup->addChild(new JumpMinutes(*this));
00137 cgroup->addChild(new TimeStretch(*this));
00138
00139 addChild(cgroup);
00140 };
00141
00142 int PlayGroup::GetCount(void)
00143 {
00144 int names = 0;
00145
00146 MSqlQuery query(MSqlQuery::InitCon());
00147 query.prepare("SELECT COUNT(name) FROM playgroup "
00148 "WHERE name <> 'Default' ORDER BY name;");
00149 if (!query.exec())
00150 MythContext::DBError("PlayGroupEditor::load", query);
00151 else if (query.next())
00152 names = query.value(0).toInt();
00153
00154 return names;
00155 }
00156
00157 QStringList PlayGroup::GetNames(void)
00158 {
00159 QStringList names;
00160
00161 MSqlQuery query(MSqlQuery::InitCon());
00162 query.prepare("SELECT name FROM playgroup "
00163 "WHERE name <> 'Default' ORDER BY name;");
00164 if (!query.exec())
00165 MythContext::DBError("PlayGroupEditor::load", query);
00166 else
00167 {
00168 while (query.next())
00169 names << QString::fromUtf8(query.value(0).toString());
00170 }
00171
00172 return names;
00173 }
00174
00175 QString PlayGroup::GetInitialName(const ProgramInfo *pi)
00176 {
00177 QString res = "Default";
00178
00179 MSqlQuery query(MSqlQuery::InitCon());
00180 query.prepare("SELECT name FROM playgroup "
00181 "WHERE name = :TITLE OR "
00182 " name = :CATEGORY OR "
00183 " (titlematch <> '' AND "
00184 " :TITLE REGEXP titlematch) ");
00185 query.bindValue(":TITLE", pi->title.utf8());
00186 query.bindValue(":CATEGORY", pi->category.utf8());
00187 query.exec();
00188
00189 if (!query.exec())
00190 MythContext::DBError("GetInitialName", query);
00191 else if (query.next())
00192 res = QString::fromUtf8(query.value(0).toString());
00193
00194 return res;
00195 }
00196
00197 int PlayGroup::GetSetting(const QString &name, const QString &field,
00198 int defval)
00199 {
00200 int res = defval;
00201
00202 MSqlQuery query(MSqlQuery::InitCon());
00203 query.prepare(QString("SELECT name, %1 FROM playgroup "
00204 "WHERE (name = :NAME OR name = 'Default') "
00205 " AND %2 <> 0 "
00206 "ORDER BY name = 'Default';")
00207 .arg(field).arg(field));
00208 query.bindValue(":NAME", name.utf8());
00209 if (!query.exec())
00210 MythContext::DBError("PlayGroup::GetSetting", query);
00211 else if (query.next())
00212 res = query.value(1).toInt();
00213
00214 return res;
00215 }
00216
00217 PlayGroupEditor::PlayGroupEditor(void) :
00218 listbox(new ListBoxSetting(this)), lastValue("Default")
00219 {
00220 listbox->setLabel(tr("Playback Groups"));
00221 addChild(listbox);
00222 }
00223
00224 void PlayGroupEditor::open(QString name)
00225 {
00226 lastValue = name;
00227 bool created = false;
00228
00229 if (name == "__CREATE_NEW_GROUP__")
00230 {
00231 name = "";
00232 bool ok = MythPopupBox::showGetTextPopup(gContext->GetMainWindow(),
00233 tr("Create New Playback Group"),
00234 tr("Enter group name or press SELECT to enter text via the "
00235 "On Screen Keyboard"), name);
00236 if (!ok)
00237 return;
00238
00239 MSqlQuery query(MSqlQuery::InitCon());
00240 query.prepare("INSERT INTO playgroup (name) VALUES (:NAME);");
00241 query.bindValue(":NAME", name.utf8());
00242 if (!query.exec())
00243 MythContext::DBError("PlayGroupEditor::open", query);
00244 else
00245 created = true;
00246 }
00247
00248 PlayGroup group(name);
00249 if (group.exec() == QDialog::Accepted || !created)
00250 lastValue = name;
00251 else
00252 {
00253 MSqlQuery query(MSqlQuery::InitCon());
00254 query.prepare("DELETE FROM playgroup WHERE name = :NAME;");
00255 query.bindValue(":NAME", name.utf8());
00256 if (!query.exec())
00257 MythContext::DBError("PlayGroupEditor::open", query);
00258 }
00259 };
00260
00261 void PlayGroupEditor::doDelete(void)
00262 {
00263 QString name = listbox->getValue();
00264 if (name == "__CREATE_NEW_GROUP__" || name == "Default")
00265 return;
00266
00267 QString message = tr("Delete playback group:") +
00268 QString("\n'%1'?").arg(name);
00269
00270 DialogCode value = MythPopupBox::Show2ButtonPopup(
00271 gContext->GetMainWindow(),
00272 "", message,
00273 tr("Yes, delete group"),
00274 tr("No, Don't delete group"), kDialogCodeButton1);
00275
00276 if (kDialogCodeButton0 == value)
00277 {
00278 MSqlQuery query(MSqlQuery::InitCon());
00279 query.prepare("DELETE FROM playgroup WHERE name = :NAME;");
00280 query.bindValue(":NAME", name.utf8());
00281 if (!query.exec())
00282 MythContext::DBError("PlayGroupEditor::doDelete", query);
00283
00284 int lastIndex = listbox->getValueIndex(name);
00285 lastValue = "";
00286 load();
00287 listbox->setValue(lastIndex);
00288 }
00289
00290 listbox->setFocus();
00291 }
00292
00293 void PlayGroupEditor::load(void)
00294 {
00295 listbox->clearSelections();
00296
00297 listbox->addSelection(tr("Default"), "Default");
00298
00299 QStringList names = PlayGroup::GetNames();
00300 while (!names.isEmpty())
00301 {
00302 listbox->addSelection(names.front());
00303 names.pop_front();
00304 }
00305
00306 listbox->addSelection(tr("(Create new group)"), "__CREATE_NEW_GROUP__");
00307
00308 listbox->setValue(lastValue);
00309 }
00310
00311 DialogCode PlayGroupEditor::exec(void)
00312 {
00313 while (ConfigurationDialog::exec() == kDialogCodeAccepted)
00314 open(listbox->getValue());
00315
00316 return kDialogCodeRejected;
00317 }
00318
00319 MythDialog* PlayGroupEditor::dialogWidget(MythMainWindow* parent,
00320 const char* widgetName)
00321 {
00322 dialog = ConfigurationDialog::dialogWidget(parent, widgetName);
00323 connect(dialog, SIGNAL(menuButtonPressed()), this, SLOT(doDelete()));
00324 connect(dialog, SIGNAL(deleteButtonPressed()), this, SLOT(doDelete()));
00325 return dialog;
00326 }