00001 #include <qlayout.h>
00002 #include <qpushbutton.h>
00003 #include <qbuttongroup.h>
00004 #include <qlabel.h>
00005 #include <qcursor.h>
00006 #include <qlistview.h>
00007 #include <qdatetime.h>
00008 #include <qapplication.h>
00009 #include <qimage.h>
00010 #include <qpainter.h>
00011 #include <qheader.h>
00012 #include <qsqldatabase.h>
00013 #include <qhbox.h>
00014
00015 #include <unistd.h>
00016
00017 #include <iostream>
00018 using namespace std;
00019
00020 #include "customedit.h"
00021
00022 #include "mythcontext.h"
00023 #include "dialogbox.h"
00024 #include "programinfo.h"
00025 #include "proglist.h"
00026 #include "scheduledrecording.h"
00027 #include "recordingtypes.h"
00028 #include "mythdbcon.h"
00029
00030 CustomEdit::CustomEdit(MythMainWindow *parent, const char *name,
00031 ProgramInfo *pginfo)
00032 : MythDialog(parent, name)
00033 {
00034 ProgramInfo *p = new ProgramInfo();
00035
00036 if (pginfo)
00037 {
00038 delete p;
00039 p = pginfo;
00040 }
00041
00042 QString baseTitle = p->title;
00043 baseTitle.remove(QRegExp(" \\(.*\\)$"));
00044
00045 QString quoteTitle = baseTitle;
00046 quoteTitle.replace("\'","\'\'");
00047
00048 prevItem = 0;
00049 maxex = 0;
00050 seSuffix = QString(" (%1)").arg(tr("stored search"));
00051 exSuffix = QString(" (%1)").arg(tr("stored example"));
00052 addString = tr("Add");
00053
00054 QVBoxLayout *vbox = new QVBoxLayout(this, (int)(20 * wmult));
00055
00056 QVBoxLayout *vkbox = new QVBoxLayout(vbox, (int)(1 * wmult));
00057 QHBoxLayout *hbox = new QHBoxLayout(vkbox, (int)(1 * wmult));
00058
00059
00060 hbox = new QHBoxLayout(vbox, (int)(10 * wmult));
00061
00062 QString message = tr("Edit Rule") + ": ";
00063 QLabel *label = new QLabel(message, this);
00064 label->setBackgroundOrigin(WindowOrigin);
00065 label->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
00066 hbox->addWidget(label);
00067
00068 m_rule = new MythComboBox( false, this, "rule");
00069 m_rule->setBackgroundOrigin(WindowOrigin);
00070
00071 m_rule->insertItem(tr("<New rule>"));
00072 m_recid << "0";
00073 m_recsub << "";
00074 m_recdesc << "";
00075
00076 MSqlQuery result(MSqlQuery::InitCon());
00077 result.prepare("SELECT recordid, title, subtitle, description "
00078 "FROM record WHERE search = :SEARCH ORDER BY title;");
00079 result.bindValue(":SEARCH", kPowerSearch);
00080
00081 int titlematch = -1;
00082 if (result.exec() && result.isActive())
00083 {
00084 while (result.next())
00085 {
00086 QString trimTitle = QString::fromUtf8(result.value(1).toString());
00087 trimTitle.remove(QRegExp(" \\(.*\\)$"));
00088
00089 m_rule->insertItem(trimTitle);
00090 m_recid << result.value(0).toString();
00091 m_recsub << QString::fromUtf8(result.value(2).toString());
00092 m_recdesc << QString::fromUtf8(result.value(3).toString());
00093
00094 if (trimTitle == baseTitle ||
00095 result.value(0).toInt() == p->recordid)
00096 titlematch = m_rule->count() - 1;
00097 }
00098 }
00099 else
00100 MythContext::DBError("Get power search rules query", result);
00101
00102 hbox->addWidget(m_rule);
00103
00104
00105 hbox = new QHBoxLayout(vbox, (int)(10 * wmult));
00106
00107 message = tr("Rule Name") + ": ";
00108 label = new QLabel(message, this);
00109 label->setBackgroundOrigin(WindowOrigin);
00110 label->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
00111 hbox->addWidget(label);
00112
00113 m_title = new MythRemoteLineEdit( this, "title" );
00114 m_title->setBackgroundOrigin(WindowOrigin);
00115 hbox->addWidget(m_title);
00116
00117 m_clause = new MythComboBox( false, this, "clause");
00118 m_clause->setBackgroundOrigin(WindowOrigin);
00119
00120 m_clause->insertItem(tr("Match an exact title"));
00121 m_cfrom << "";
00122 if (p->title > "")
00123 m_csql << QString("program.title = '%1' ")
00124 .arg(quoteTitle);
00125 else
00126 m_csql << "program.title = 'Nova' ";
00127
00128 if (p->seriesid > "")
00129 {
00130 m_clause->insertItem(tr("Match this series"));
00131 m_cfrom << "";
00132 m_csql << QString("program.seriesid = '%1' ").arg(p->seriesid);
00133 }
00134 m_clause->insertItem(tr("Match words in the title"));
00135 m_cfrom << "";
00136 if (p->title > "")
00137 m_csql << QString("program.title LIKE '\%%1\%' ").arg(quoteTitle);
00138 else
00139 m_csql << "program.title LIKE 'CSI: %' ";
00140
00141 m_clause->insertItem(tr("Match words in the subtitle"));
00142 m_cfrom << "";
00143 if (p->subtitle > "")
00144 {
00145 QString subt = p->subtitle;
00146 subt.replace("\'","\'\'");
00147 m_csql << QString("program.subtitle LIKE '\%%1\%' ").arg(subt);
00148 }
00149 else
00150 m_csql << "program.subtitle LIKE '%Las Vegas%' ";
00151
00152 if (p->programid > "")
00153 {
00154 m_clause->insertItem(tr("Match this episode"));
00155 m_cfrom << "";
00156 m_csql << QString("program.programid = '%1' ").arg(p->programid);
00157 }
00158 else if (p->subtitle > "")
00159 {
00160 m_clause->insertItem(tr("Match this episode"));
00161 m_cfrom << "";
00162 m_csql << QString("program.subtitle = '%1' \n"
00163 "AND program.description = '%2' ")
00164 .arg(p->subtitle.replace("\'","\'\'"))
00165 .arg(p->description.replace("\'","\'\'"));
00166 }
00167 else
00168 {
00169 m_clause->insertItem(tr("Match an exact episode"));
00170 m_cfrom << "";
00171 m_csql << QString("program.title = 'Seinfeld' \n"
00172 "AND program.subtitle = 'The Soup' ");
00173 }
00174 m_clause->insertItem(tr("Match in any descriptive field"));
00175 m_cfrom << "";
00176 m_csql << QString("(program.title LIKE '%Japan%' \n"
00177 " OR program.subtitle LIKE '%Japan%' \n"
00178 " OR program.description LIKE '%Japan%') ");
00179
00180 m_clause->insertItem(tr("New episodes only"));
00181 m_cfrom << "";
00182 m_csql << "program.previouslyshown = 0 ";
00183
00184 m_clause->insertItem(tr("Exclude unidentified episodes"));
00185 m_cfrom << "";
00186 m_csql << "program.generic = 0 ";
00187
00188 m_clause->insertItem(tr("First showing of each episode"));
00189 m_cfrom << "";
00190 m_csql << "program.first > 0 ";
00191
00192 m_clause->insertItem(tr("Last showing of each episode"));
00193 m_cfrom << "";
00194 m_csql << "program.last > 0 ";
00195
00196 m_clause->insertItem(tr("Anytime on a specific day of the week"));
00197 m_cfrom << "";
00198 m_csql << QString("DAYNAME(program.starttime) = '%1' ")
00199 .arg(p->startts.toString("dddd"));
00200
00201 m_clause->insertItem(tr("Only on weekdays (Monday through Friday)"));
00202 m_cfrom << "";
00203 m_csql << "WEEKDAY(program.starttime) < 5 ";
00204
00205 m_clause->insertItem(tr("Only on weekends"));
00206 m_cfrom << "";
00207 m_csql << "WEEKDAY(program.starttime) >= 5 ";
00208
00209 m_clause->insertItem(tr("Only in primetime"));
00210 m_cfrom << "";
00211 m_csql << QString("HOUR(program.starttime) >= 19 \n"
00212 "AND HOUR(program.starttime) < 23 ");
00213
00214 m_clause->insertItem(tr("Not in primetime"));
00215 m_cfrom << "";
00216 m_csql << QString("(HOUR(program.starttime) < 19 \n"
00217 " OR HOUR(program.starttime) >= 23) ");
00218
00219 m_clause->insertItem(tr("Only on a specific station"));
00220 m_cfrom << "";
00221 if (p->chansign > "")
00222 m_csql << QString("channel.callsign = '%1' ").arg(p->chansign);
00223 else
00224 m_csql << "channel.callsign = 'ESPN' ";
00225
00226 m_clause->insertItem(tr("Exclude one station"));
00227 m_cfrom << "";
00228 m_csql << "channel.callsign != 'GOLF' ";
00229
00230 m_clause->insertItem(tr("Match related callsigns"));
00231 m_cfrom << "";
00232 m_csql << "channel.callsign LIKE 'HBO%' ";
00233
00234 m_clause->insertItem(tr("Only on channels marked as favorites"));
00235 m_cfrom << ", favorites";
00236 m_csql << "program.chanid = favorites.chanid ";
00237
00238 m_clause->insertItem(tr("Only channels from a specific video source"));
00239 m_cfrom << "";
00240 m_csql << "channel.sourceid = 2 ";
00241
00242 m_clause->insertItem(tr("Only channels marked as commercial free"));
00243 m_cfrom << "";
00244 m_csql << "channel.commmethod = -2 ";
00245
00246 m_clause->insertItem(tr("Only shows marked as HDTV"));
00247 m_cfrom << "";
00248 m_csql << "program.hdtv > 0 ";
00249
00250 m_clause->insertItem(tr("Only shows marked as widescreen"));
00251 m_cfrom << "";
00252 m_csql << "FIND_IN_SET('WIDESCREEN', program.videoprop) > 0 ";
00253
00254 m_clause->insertItem(tr("Exclude H.264 encoded streams (EIT only)"));
00255 m_cfrom << "";
00256 m_csql << "FIND_IN_SET('AVC', program.videoprop) = 0 ";
00257
00258 m_clause->insertItem(tr("Limit by category"));
00259 m_cfrom << "";
00260 if (p->category > "")
00261 m_csql << QString("program.category = '%1' ").arg(p->category);
00262 else
00263 m_csql << "program.category = 'Reality' ";
00264
00265 m_clause->insertItem(tr("All matches for a genre (Data Direct)"));
00266 m_cfrom << "LEFT JOIN programgenres ON "
00267 "program.chanid = programgenres.chanid AND "
00268 "program.starttime = programgenres.starttime ";
00269 if (p->category > "")
00270 m_csql << QString("programgenres.genre = '%1' ").arg(p->category);
00271 else
00272 m_csql << "programgenres.genre = 'Reality' ";
00273
00274 m_clause->insertItem(tr("Limit by MPAA or VCHIP rating (Data Direct)"));
00275 m_cfrom << "LEFT JOIN programrating ON "
00276 "program.chanid = programrating.chanid AND "
00277 "program.starttime = programrating.starttime ";
00278 m_csql << "(programrating.rating = 'G' OR programrating.rating "
00279 "LIKE 'TV-Y%') ";
00280
00281 m_clause->insertItem(QString(tr("Category type") +
00282 " ('movie', 'series', 'sports' " + tr("or") + " 'tvshow')"));
00283 m_cfrom << "";
00284 m_csql << "program.category_type = 'sports' ";
00285
00286 m_clause->insertItem(tr("Limit movies by the year of release"));
00287 m_cfrom << "";
00288 m_csql << "program.category_type = 'movie' AND program.airdate >= 2000 ";
00289
00290 m_clause->insertItem(tr("Minimum star rating (0.0 to 1.0 for movies only)"));
00291 m_cfrom << "";
00292 m_csql << "program.stars >= 0.75 ";
00293
00294 m_clause->insertItem(tr("Person named in the credits (Data Direct)"));
00295 m_cfrom << ", people, credits";
00296 m_csql << QString("people.name = 'Tom Hanks' \n"
00297 "AND credits.person = people.person \n"
00298 "AND program.chanid = credits.chanid \n"
00299 "AND program.starttime = credits.starttime ");
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310 m_clause->insertItem(tr("Multiple sports teams (complete example)"));
00311 m_cfrom << "";
00312 m_csql << QString("program.title = 'NBA Basketball' \n"
00313 "AND program.subtitle REGEXP '(Miami|Cavaliers|Lakers)' \n"
00314 "AND program.first > 0 \n");
00315
00316 m_clause->insertItem(tr("Sci-fi B-movies (complete example)"));
00317 m_cfrom << "";
00318 m_csql << QString("program.category_type='movie' \n"
00319 "AND program.category='Science fiction' \n"
00320 "AND program.stars <= 0.5 AND program.airdate < 1970 ");
00321
00322 m_clause->insertItem(tr("SportsCenter Overnight (complete example - use FindDaily)"));
00323 m_cfrom << "";
00324 m_csql << QString("program.title = 'SportsCenter' \n"
00325 "AND HOUR(program.starttime) >= 2 \n"
00326 "AND HOUR(program.starttime) <= 6 ");
00327
00328 m_clause->insertItem(tr("Movie of the Week (complete example - use FindWeekly)"));
00329 m_cfrom << "";
00330 m_csql << QString("program.category_type='movie' \n"
00331 "AND program.stars >= 1.0 AND program.airdate >= 1965 \n"
00332 "AND DAYNAME(program.starttime) = 'Friday' \n"
00333 "AND HOUR(program.starttime) >= 12 ");
00334
00335 m_clause->insertItem(tr("First Episodes (complete example for Data Direct)"));
00336 m_cfrom << "";
00337 m_csql << QString("program.first > 0 \n"
00338 "AND program.programid LIKE 'EP%0001' \n"
00339 "AND program.originalairdate = DATE(program.starttime) ");
00340
00341 maxex = m_clause->count();
00342
00343 result.prepare("SELECT rulename,fromclause,whereclause,search "
00344 "FROM customexample;");
00345
00346 if (result.exec() && result.isActive())
00347 {
00348 while (result.next())
00349 {
00350 QString str = QString::fromUtf8(result.value(0).toString());
00351
00352 if (result.value(3).toInt() > 0)
00353 str += seSuffix;
00354 else
00355 str += exSuffix;
00356
00357 m_clause->insertItem(str);
00358 m_cfrom << QString::fromUtf8(result.value(1).toString());
00359 m_csql << QString::fromUtf8(result.value(2).toString());
00360 }
00361 }
00362 vbox->addWidget(m_clause);
00363
00364
00365 m_addButton = new MythPushButton( this, "add" );
00366 m_addButton->setBackgroundOrigin(WindowOrigin);
00367 m_addButton->setText(addString);
00368 m_addButton->setEnabled(true);
00369
00370 vbox->addWidget(m_addButton);
00371
00372
00373 hbox = new QHBoxLayout(vbox, (int)(10 * wmult));
00374
00375 message = tr("Additional Tables") + ": ";
00376 label = new QLabel(message, this);
00377 label->setBackgroundOrigin(WindowOrigin);
00378 label->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
00379 hbox->addWidget(label);
00380
00381 m_subtitle = new MythRemoteLineEdit(this, "subtitle" );
00382 m_subtitle->setBackgroundOrigin(WindowOrigin);
00383 hbox->addWidget(m_subtitle);
00384
00385
00386 m_description = new MythRemoteLineEdit(5, this, "description" );
00387 m_description->setBackgroundOrigin(WindowOrigin);
00388 vbox->addWidget(m_description);
00389
00390
00391 hbox = new QHBoxLayout(vbox, (int)(10 * wmult));
00392
00393 m_testButton = new MythPushButton( this, "test" );
00394 m_testButton->setBackgroundOrigin(WindowOrigin);
00395 m_testButton->setText( tr( "Test" ) );
00396 m_testButton->setEnabled(false);
00397
00398 hbox->addWidget(m_testButton);
00399
00400
00401 m_recordButton = new MythPushButton( this, "record" );
00402 m_recordButton->setBackgroundOrigin(WindowOrigin);
00403 m_recordButton->setText( tr( "Record" ) );
00404 m_recordButton->setEnabled(false);
00405
00406 hbox->addWidget(m_recordButton);
00407
00408
00409 m_storeButton = new MythPushButton( this, "store" );
00410 m_storeButton->setBackgroundOrigin(WindowOrigin);
00411 m_storeButton->setText( tr( "Store" ) );
00412 m_storeButton->setEnabled(false);
00413
00414 hbox->addWidget(m_storeButton);
00415
00416
00417 m_cancelButton = new MythPushButton( this, "cancel" );
00418 m_cancelButton->setBackgroundOrigin(WindowOrigin);
00419 m_cancelButton->setText( tr( "Cancel" ) );
00420 m_cancelButton->setEnabled(true);
00421
00422 hbox->addWidget(m_cancelButton);
00423
00424 connect(this, SIGNAL(dismissWindow()), this, SLOT(accept()));
00425
00426 connect(m_rule, SIGNAL(activated(int)), this, SLOT(ruleChanged(void)));
00427 connect(m_rule, SIGNAL(highlighted(int)), this, SLOT(ruleChanged(void)));
00428 connect(m_title, SIGNAL(textChanged(void)), this, SLOT(textChanged(void)));
00429 connect(m_addButton, SIGNAL(clicked()), this, SLOT(addClicked()));
00430 connect(m_clause, SIGNAL(activated(int)), this, SLOT(clauseChanged(void)));
00431 connect(m_clause, SIGNAL(highlighted(int)), this, SLOT(clauseChanged(void)));
00432 connect(m_description, SIGNAL(textChanged(void)), this,
00433 SLOT(textChanged(void)));
00434 connect(m_testButton, SIGNAL(clicked()), this, SLOT(testClicked()));
00435 connect(m_recordButton, SIGNAL(clicked()), this, SLOT(recordClicked()));
00436 connect(m_storeButton, SIGNAL(clicked()), this, SLOT(storeClicked()));
00437 connect(m_cancelButton, SIGNAL(clicked()), this, SLOT(cancelClicked()));
00438
00439 gContext->addListener(this);
00440 gContext->addCurrentLocation("CustomEdit");
00441
00442 if (titlematch >= 0)
00443 {
00444 m_rule->setCurrentItem(titlematch);
00445 ruleChanged();
00446 }
00447 else if (p->title > "")
00448 {
00449 m_title->setText(baseTitle);
00450 m_subtitle->setText("");
00451 m_description->setText("program.title = '" + quoteTitle + "' ");
00452 textChanged();
00453 }
00454
00455 if (m_title->text().isEmpty())
00456 m_rule->setFocus();
00457 else
00458 m_clause->setFocus();
00459
00460 clauseChanged();
00461 }
00462
00463 CustomEdit::~CustomEdit(void)
00464 {
00465 gContext->removeListener(this);
00466 gContext->removeCurrentLocation();
00467 }
00468
00469 void CustomEdit::ruleChanged(void)
00470 {
00471 int curItem = m_rule->currentItem();
00472 if (curItem == prevItem)
00473 return;
00474
00475 prevItem = curItem;
00476
00477 if (curItem > 0)
00478 m_title->setText(m_rule->currentText());
00479 else
00480 m_title->setText("");
00481
00482 m_subtitle->setText(m_recsub[curItem]);
00483 m_description->setText(m_recdesc[curItem]);
00484 textChanged();
00485 }
00486
00487 void CustomEdit::textChanged(void)
00488 {
00489 bool hastitle = !m_title->text().isEmpty();
00490 bool hasdesc = !m_description->text().isEmpty();
00491
00492 m_testButton->setEnabled(hasdesc);
00493 m_recordButton->setEnabled(hastitle && hasdesc);
00494 m_storeButton->setEnabled(m_clause->currentItem() >= maxex ||
00495 (hastitle && hasdesc));
00496 }
00497
00498 void CustomEdit::clauseChanged(void)
00499 {
00500 QString msg = m_csql[m_clause->currentItem()];
00501 msg.replace("\n", " ");
00502 msg.replace(QRegExp(" [ ]*"), " ");
00503 msg = QString("%1: \"%2\"").arg(addString).arg(msg);
00504 if (msg.length() > 50)
00505 {
00506 msg.truncate(48);
00507 msg += "...\"";
00508 }
00509 m_addButton->setText(msg);
00510
00511 bool hastitle = !m_title->text().isEmpty();
00512 bool hasdesc = !m_description->text().isEmpty();
00513
00514 m_storeButton->setEnabled(m_clause->currentItem() >= maxex ||
00515 (hastitle && hasdesc));
00516 }
00517
00518 void CustomEdit::addClicked(void)
00519 {
00520 QString clause = "";
00521
00522 if (m_description->text().contains(QRegExp("\\S")))
00523 clause = "AND ";
00524
00525 clause += m_csql[m_clause->currentItem()];
00526 m_description->append(clause);
00527 m_subtitle->append(m_cfrom[m_clause->currentItem()]);
00528 }
00529
00530 void CustomEdit::testClicked(void)
00531 {
00532 if (!checkSyntax())
00533 {
00534 m_testButton->setFocus();
00535 return;
00536 }
00537
00538 ProgLister *pl = new ProgLister(plSQLSearch, m_description->text(),
00539 m_subtitle->text(),
00540 gContext->GetMainWindow(), "proglist");
00541 pl->exec();
00542 delete pl;
00543
00544 m_testButton->setFocus();
00545 }
00546
00547 void CustomEdit::recordClicked(void)
00548 {
00549 if (!checkSyntax())
00550 {
00551 m_recordButton->setFocus();
00552 return;
00553 }
00554
00555 ScheduledRecording *record = new ScheduledRecording();
00556
00557 int cur_recid = m_recid[m_rule->currentItem()].toInt();
00558
00559 if (cur_recid > 0)
00560 record->modifyPowerSearchByID(cur_recid, m_title->text(),
00561 m_subtitle->text(),
00562 m_description->text());
00563 else
00564 record->loadBySearch(kPowerSearch, m_title->text(),
00565 m_subtitle->text(), m_description->text());
00566 record->exec();
00567
00568 if (record->getRecordID())
00569 accept();
00570 else
00571 m_recordButton->setFocus();
00572
00573 record->deleteLater();
00574 }
00575
00576 void CustomEdit::storeClicked(void)
00577 {
00578 bool nameExists = false;
00579 QString oldwhere = "";
00580
00581 MSqlQuery query(MSqlQuery::InitCon());
00582 query.prepare("SELECT rulename,whereclause FROM customexample "
00583 "WHERE rulename = :RULE;");
00584 query.bindValue(":RULE", m_title->text());
00585
00586 if (query.exec() && query.isActive() && query.next())
00587 {
00588 nameExists = true;
00589 oldwhere = QString::fromUtf8(query.value(1).toString());
00590 }
00591 QString msg = QString("%1: %2\n\n").arg(QObject::tr("Current Example"))
00592 .arg(m_title->text());
00593
00594 if (m_subtitle->text() != "")
00595 msg += m_subtitle->text() + "\n\n";
00596
00597 msg += m_description->text();
00598
00599 DialogBox *storediag = new DialogBox(gContext->GetMainWindow(), msg);
00600 int button = 0, sebtn = -1, exbtn = -1, deletebtn = -1, cancelbtn = -1;
00601
00602 QString action = QObject::tr("Store");
00603 if (nameExists)
00604 action = QObject::tr("Replace");
00605
00606 QString str = QString("%1 \"%2\"").arg(action).arg(m_title->text());
00607
00608 if (!m_title->text().isEmpty())
00609 {
00610 QString str2;
00611 str2 = QString("%1 %2").arg(str).arg(QObject::tr("as a search"));
00612 storediag->AddButton(str2);
00613 sebtn = button++;
00614
00615 str2 = QString("%1 %2").arg(str).arg(QObject::tr("as an example"));
00616 storediag->AddButton(str2);
00617 exbtn = button++;
00618 }
00619 if (m_clause->currentItem() >= maxex)
00620 {
00621 str = QString("%1 \"%2\"").arg(QObject::tr("Delete"))
00622 .arg(m_clause->currentText());
00623
00624 storediag->AddButton(str);
00625 deletebtn = button++;
00626 }
00627 storediag->AddButton(QObject::tr("Cancel"));
00628 cancelbtn = button++;
00629
00630 DialogCode code = storediag->exec();
00631 int ret = MythDialog::CalcItemIndex(code);
00632 storediag->deleteLater();
00633 storediag = NULL;
00634
00635 if (ret == sebtn || ret == exbtn)
00636 {
00637
00638 query.prepare("REPLACE INTO customexample "
00639 "(rulename,fromclause,whereclause,search) "
00640 "VALUES(:RULE,:FROMC,:WHEREC,:SEARCH);");
00641 query.bindValue(":RULE", m_title->text());
00642 query.bindValue(":FROMC", m_subtitle->text());
00643 query.bindValue(":WHEREC", m_description->text());
00644 query.bindValue(":SEARCH", ret == sebtn);
00645
00646 if (!query.exec())
00647 MythContext::DBError("Store custom example", query);
00648 else if (nameExists)
00649 {
00650
00651 unsigned i = maxex;
00652 while (i < m_csql.count())
00653 {
00654 if (m_csql[i] == oldwhere)
00655 {
00656 m_cfrom[i] = m_subtitle->text();
00657 m_csql[i] = m_description->text();
00658 break;
00659 }
00660 i++;
00661 }
00662 }
00663 else
00664 {
00665
00666 if (ret == sebtn)
00667 m_clause->insertItem(m_title->text() + seSuffix);
00668 else
00669 m_clause->insertItem(m_title->text() + exSuffix);
00670 m_cfrom << m_subtitle->text();
00671 m_csql << m_description->text();
00672 }
00673 }
00674 else if (ret == deletebtn)
00675 {
00676 query.prepare("DELETE FROM customexample "
00677 "WHERE rulename = :RULE;");
00678 query.bindValue(":RULE", m_clause->currentText().remove(seSuffix)
00679 .remove(exSuffix));
00680 if (!query.exec())
00681 MythContext::DBError("Delete custom example", query);
00682 else
00683 {
00684
00685 unsigned i = m_clause->currentItem();
00686 m_clause->removeItem(i);
00687 i++;
00688 while (i < m_csql.count())
00689 {
00690 m_cfrom[i-1] = m_cfrom[i];
00691 m_csql[i-1] = m_csql[i];
00692 i++;
00693 }
00694 m_cfrom.pop_back();
00695 m_csql.pop_back();
00696 }
00697 }
00698 clauseChanged();
00699 m_storeButton->setFocus();
00700 }
00701
00702 void CustomEdit::cancelClicked(void)
00703 {
00704 accept();
00705 }
00706
00707 bool CustomEdit::checkSyntax(void)
00708 {
00709 bool ret = false;
00710 QString msg = "";
00711
00712 QString desc = m_description->text();
00713 QString from = m_subtitle->text();
00714 if (desc.contains(QRegExp("^\\s*AND\\s", false)))
00715 {
00716 msg = "Power Search rules no longer reqiure a leading \"AND\".";
00717 }
00718 else if (desc.contains(";", false))
00719 {
00720 msg = "Power Search rules can not include semicolon ( ; ) ";
00721 msg += "statement terminators.";
00722 }
00723 else
00724 {
00725 MSqlQuery query(MSqlQuery::InitCon());
00726 query.prepare(QString("SELECT NULL FROM (program,channel) "
00727 "%1 WHERE\n%2").arg(from).arg(desc));
00728
00729 if (query.exec() && query.isActive())
00730 {
00731 ret = true;
00732 }
00733 else
00734 {
00735 msg = tr("An error was found when checking") + ":\n\n";
00736 #if QT_VERSION >= 0x030200
00737 msg += query.executedQuery();
00738 #else
00739 msg += query.lastQuery();
00740 #endif
00741 msg += "\n\n" + tr("The database error was") + ":\n";
00742 msg += query.lastError().databaseText();
00743 }
00744 }
00745
00746 if (!msg.isEmpty())
00747 {
00748 DialogBox *errdiag = new DialogBox(gContext->GetMainWindow(), msg);
00749 errdiag->AddButton(QObject::tr("OK"));
00750 errdiag->exec();
00751
00752 errdiag->deleteLater();
00753 ret = false;
00754 }
00755 return ret;
00756 }