MythTV  0.26-pre
proglist_helpers.cpp
Go to the documentation of this file.
00001 // MythTV
00002 #include "mythuibuttonlist.h"
00003 #include "proglist_helpers.h"
00004 #include "mythcorecontext.h"
00005 #include "mythuitextedit.h"
00006 #include "scheduleeditor.h"
00007 #include "recordingrule.h"
00008 #include "mythuibutton.h"
00009 #include "channelutil.h"
00010 #include "proglist.h"
00011 #include "mythmiscutil.h"
00012 
00013 PhrasePopup::PhrasePopup(MythScreenStack *parentStack,
00014                          ProgLister *parent,
00015                          RecSearchType searchType,
00016                          const QStringList &list,
00017                          const QString &currentValue) :
00018     MythScreenType(parentStack, "phrasepopup"),
00019     m_parent(parent), m_searchType(searchType),  m_list(list),
00020     m_titleText(NULL), m_phraseList(NULL), m_phraseEdit(NULL),
00021     m_okButton(NULL), m_deleteButton(NULL), m_recordButton(NULL)
00022 {
00023     m_currentValue = currentValue;
00024 }
00025 
00026 bool PhrasePopup::Create()
00027 {
00028     if (!LoadWindowFromXML("schedule-ui.xml", "phrasepopup", this))
00029         return false;
00030 
00031     bool err = false;
00032     UIUtilE::Assign(this, m_titleText, "title_text", &err);
00033     UIUtilE::Assign(this, m_phraseList, "phrase_list", &err);
00034     UIUtilE::Assign(this, m_phraseEdit, "phrase_edit", &err);
00035     UIUtilE::Assign(this, m_okButton, "ok_button", &err);
00036     UIUtilE::Assign(this, m_deleteButton, "delete_button", &err);
00037     UIUtilE::Assign(this, m_recordButton, "record_button", &err);
00038 
00039     if (err)
00040     {
00041         LOG(VB_GENERAL, LOG_ERR, "Cannot load screen 'phrasepopup'");
00042         return false;
00043     }
00044 
00045     if (m_searchType == kPowerSearch)
00046     {
00047         m_titleText->SetText(tr("Select Search"));
00048         new MythUIButtonListItem(m_phraseList, tr("<New Search>"), NULL, false);
00049         m_okButton->SetText(tr("Edit"));
00050     }
00051     else
00052     {
00053         m_titleText->SetText(tr("Phrase"));
00054         new MythUIButtonListItem(m_phraseList, tr("<New Phrase>"), NULL, false);
00055     }
00056 
00057     for (int x = 0; x < m_list.size(); x++)
00058     {
00059         new MythUIButtonListItem(m_phraseList, m_list.at(x), NULL, false);
00060     }
00061 
00062     connect(m_phraseList, SIGNAL(itemClicked(MythUIButtonListItem*)),
00063             this, SLOT(phraseClicked(MythUIButtonListItem*)));
00064     connect(m_phraseList, SIGNAL(itemSelected(MythUIButtonListItem*)),
00065             this, SLOT(phraseSelected(MythUIButtonListItem*)));
00066 
00067 
00068     m_phraseList->MoveToNamedPosition(m_currentValue);
00069 
00070     m_deleteButton->SetText(tr("Delete"));
00071     m_recordButton->SetText(tr("Record"));
00072 
00073     connect(m_okButton, SIGNAL(Clicked()), this, SLOT(okClicked()));
00074     connect(m_deleteButton, SIGNAL(Clicked()), this, SLOT(deleteClicked()));
00075     connect(m_recordButton, SIGNAL(Clicked()), this, SLOT(recordClicked()));
00076 
00077     connect(m_phraseEdit, SIGNAL(valueChanged()), this, SLOT(editChanged()));
00078 
00079     BuildFocusList();
00080 
00081     SetFocusWidget(m_phraseList);
00082 
00083     return true;
00084 }
00085 
00086 void PhrasePopup::editChanged(void)
00087 {
00088     m_okButton->SetEnabled(!m_phraseEdit->GetText().trimmed().isEmpty());
00089     m_deleteButton->SetEnabled(
00090         (m_list.indexOf(m_phraseEdit->GetText().trimmed()) != -1));
00091     m_recordButton->SetEnabled(!m_phraseEdit->GetText().trimmed().isEmpty());
00092 }
00093 
00094 void PhrasePopup::phraseClicked(MythUIButtonListItem *item)
00095 {
00096     if (!item)
00097         return;
00098 
00099     int pos = m_phraseList->GetCurrentPos();
00100 
00101     if (pos == 0)
00102         SetFocusWidget(m_phraseEdit);
00103     else
00104         okClicked();
00105 }
00106 
00107 void PhrasePopup::phraseSelected(MythUIButtonListItem *item)
00108 {
00109     if (!item)
00110         return;
00111 
00112     if (m_phraseList->GetCurrentPos() == 0)
00113         m_phraseEdit->Reset();
00114     else
00115         m_phraseEdit->SetText(item->GetText());
00116 
00117     m_okButton->SetEnabled((m_phraseList->GetCurrentPos() != 0));
00118     m_deleteButton->SetEnabled((m_phraseList->GetCurrentPos() != 0));
00119     m_recordButton->SetEnabled((m_phraseList->GetCurrentPos() != 0));
00120 }
00121 
00122 void PhrasePopup::okClicked(void)
00123 {
00124     if (m_phraseEdit->GetText().trimmed().isEmpty())
00125         return;
00126 
00127     // check to see if we need to save the phrase
00128     m_parent->UpdateKeywordInDB(
00129         m_phraseEdit->GetText(), m_phraseList->GetValue());
00130 
00131 //    emit haveResult(m_phraseList->GetCurrentPos());
00132     emit haveResult(m_phraseEdit->GetText());
00133 
00134     Close();
00135 }
00136 
00137 void PhrasePopup::deleteClicked(void)
00138 {
00139     int view = m_phraseList->GetCurrentPos() - 1;
00140 
00141     if (view < 0)
00142         return;
00143 
00144     QString text = m_list[view];
00145     QString qphrase = text;
00146 
00147     MSqlQuery query(MSqlQuery::InitCon());
00148     query.prepare("DELETE FROM keyword "
00149                   "WHERE phrase = :PHRASE AND searchtype = :TYPE;");
00150     query.bindValue(":PHRASE", qphrase);
00151     query.bindValue(":TYPE", m_searchType);
00152     if (!query.exec())
00153         MythDB::DBError("PhrasePopup::deleteClicked", query);
00154 
00155     m_phraseList->RemoveItem(m_phraseList->GetItemCurrent());
00156 
00157     m_parent->m_viewList.removeAll(text);
00158     m_parent->m_viewTextList.removeAll(text);
00159 
00160     if (view < m_parent->m_curView)
00161         m_parent->m_curView--;
00162     else if (view == m_parent->m_curView)
00163         m_parent->m_curView = -1;
00164 
00165     if (m_parent->m_viewList.count() < 1)
00166         SetFocusWidget(m_phraseEdit);
00167     else
00168         SetFocusWidget(m_phraseList);
00169 }
00170 
00171 void PhrasePopup::recordClicked(void)
00172 {
00173     QString text = m_phraseEdit->GetText();
00174     bool genreflag = false;
00175 
00176     QString what = text;
00177 
00178     if (text.trimmed().isEmpty())
00179         return;
00180 
00181     if (m_searchType == kNoSearch)
00182     {
00183         LOG(VB_GENERAL, LOG_ERR, "Unknown search in ProgLister");
00184         return;
00185     }
00186 
00187     if (m_searchType == kPowerSearch)
00188     {
00189         if (text == ":::::")
00190             return;
00191 
00192         MSqlBindings bindings;
00193         genreflag = m_parent->PowerStringToSQL(text, what, bindings);
00194 
00195         if (what.isEmpty())
00196             return;
00197 
00198         MSqlEscapeAsAQuery(what, bindings);
00199     }
00200 
00201     RecordingRule *record = new RecordingRule();
00202 
00203     if (genreflag)
00204     {
00205         QString fromgenre = QString("LEFT JOIN programgenres ON "
00206                                     "program.chanid = programgenres.chanid AND "
00207                                     "program.starttime = programgenres.starttime ");
00208         record->LoadBySearch(m_searchType, text, what, fromgenre);
00209     }
00210     else
00211     {
00212         record->LoadBySearch(m_searchType, text, what);
00213     }
00214 
00215     MythScreenStack *mainStack = GetMythMainWindow()->GetMainStack();
00216     ScheduleEditor *schededit = new ScheduleEditor(mainStack, record);
00217     if (schededit->Create())
00218     {
00219         mainStack->AddScreen(schededit);
00220         okClicked();
00221     }
00222     else
00223         delete schededit;
00224 }
00225 
00227 
00228 PowerSearchPopup::PowerSearchPopup(MythScreenStack *parentStack,
00229                                    ProgLister *parent,
00230                                    RecSearchType searchType,
00231                                    const QStringList &list,
00232                                    const QString &currentValue)
00233     : MythScreenType(parentStack, "phrasepopup"),
00234       m_parent(parent), m_searchType(searchType), m_list(list),
00235       m_currentValue(currentValue),
00236       m_titleText(NULL), m_phraseList(NULL), m_phraseEdit(NULL),
00237       m_editButton(NULL), m_deleteButton(NULL), m_recordButton(NULL)
00238 {
00239 }
00240 
00241 bool PowerSearchPopup::Create()
00242 {
00243     if (!LoadWindowFromXML("schedule-ui.xml", "powersearchpopup", this))
00244         return false;
00245 
00246     bool err = false;
00247     UIUtilE::Assign(this, m_titleText, "title_text", &err);
00248     UIUtilE::Assign(this, m_phraseList, "phrase_list", &err);
00249     UIUtilE::Assign(this, m_editButton, "edit_button", &err);
00250     UIUtilE::Assign(this, m_deleteButton, "delete_button", &err);
00251     UIUtilE::Assign(this, m_recordButton, "record_button", &err);
00252 
00253     if (err)
00254     {
00255         LOG(VB_GENERAL, LOG_ERR, "Cannot load screen 'powersearchpopup'");
00256         return false;
00257     }
00258 
00259     m_titleText->SetText(tr("Select Search"));
00260     new MythUIButtonListItem(m_phraseList, tr("<New Search>"), NULL, false);
00261 
00262     for (int x = 0; x < m_list.size(); x++)
00263     {
00264         new MythUIButtonListItem(m_phraseList, m_list.at(x), NULL, false);
00265     }
00266 
00267     connect(m_phraseList, SIGNAL(itemClicked(MythUIButtonListItem*)),
00268             this, SLOT(phraseClicked(MythUIButtonListItem*)));
00269     connect(m_phraseList, SIGNAL(itemSelected(MythUIButtonListItem*)),
00270             this, SLOT(phraseSelected(MythUIButtonListItem*)));
00271 
00272 
00273     m_phraseList->MoveToNamedPosition(m_currentValue);
00274 
00275     m_editButton->SetText(tr("Edit"));
00276     m_deleteButton->SetText(tr("Delete"));
00277     m_recordButton->SetText(tr("Record"));
00278 
00279     connect(m_editButton, SIGNAL(Clicked()), this, SLOT(editClicked()));
00280     connect(m_deleteButton, SIGNAL(Clicked()), this, SLOT(deleteClicked()));
00281     connect(m_recordButton, SIGNAL(Clicked()), this, SLOT(recordClicked()));
00282 
00283     BuildFocusList();
00284 
00285     SetFocusWidget(m_phraseList);
00286 
00287     return true;
00288 }
00289 
00290 void PowerSearchPopup::phraseClicked(MythUIButtonListItem *item)
00291 {
00292     if (!item)
00293         return;
00294 
00295     int pos = m_phraseList->GetCurrentPos();
00296 
00297     if (pos == 0)
00298         editClicked();
00299     else
00300     {
00301         emit haveResult(m_phraseList->GetValue());
00302         Close();
00303     }
00304 }
00305 
00306 void PowerSearchPopup::phraseSelected(MythUIButtonListItem *item)
00307 {
00308     if (!item)
00309         return;
00310 
00311     m_editButton->SetEnabled((m_phraseList->GetCurrentPos() != 0));
00312     m_deleteButton->SetEnabled((m_phraseList->GetCurrentPos() != 0));
00313     m_recordButton->SetEnabled((m_phraseList->GetCurrentPos() != 0));
00314 }
00315 
00316 void PowerSearchPopup::editClicked(void)
00317 {
00318     MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
00319 
00320     QString currentItem = ":::::";
00321 
00322     if (m_phraseList->GetCurrentPos() != 0)
00323         currentItem = m_phraseList->GetValue();
00324 
00325     EditPowerSearchPopup *popup = new EditPowerSearchPopup(
00326         popupStack, m_parent, currentItem);
00327 
00328     if (!popup->Create())
00329     {
00330         delete popup;
00331         return;
00332     }
00333 
00334     popupStack->AddScreen(popup);
00335 
00336     Close();
00337 }
00338 
00339 void PowerSearchPopup::deleteClicked(void)
00340 {
00341     int view = m_phraseList->GetCurrentPos() - 1;
00342 
00343     if (view < 0)
00344         return;
00345 
00346     QString text = m_list[view];
00347     QString qphrase = text;
00348 
00349     MSqlQuery query(MSqlQuery::InitCon());
00350     query.prepare("DELETE FROM keyword "
00351                   "WHERE phrase = :PHRASE AND searchtype = :TYPE;");
00352     query.bindValue(":PHRASE", qphrase);
00353     query.bindValue(":TYPE", m_searchType);
00354     if (!query.exec())
00355         MythDB::DBError("PowerSearchPopup::deleteClicked", query);
00356 
00357     m_phraseList->RemoveItem(m_phraseList->GetItemCurrent());
00358 
00359     m_parent->m_viewList.removeAll(text);
00360     m_parent->m_viewTextList.removeAll(text);
00361 
00362     if (view < m_parent->m_curView)
00363         m_parent->m_curView--;
00364     else if (view == m_parent->m_curView)
00365         m_parent->m_curView = -1;
00366 
00367     if (m_parent->m_viewList.count() < 1)
00368         SetFocusWidget(m_phraseEdit);
00369     else
00370         SetFocusWidget(m_phraseList);
00371 }
00372 
00373 void PowerSearchPopup::recordClicked(void)
00374 {
00375     QString text = m_phraseList->GetValue();
00376     bool genreflag = false;
00377 
00378     QString what = text;
00379 
00380     if (text.trimmed().isEmpty())
00381         return;
00382 
00383     if (m_searchType == kNoSearch)
00384     {
00385         LOG(VB_GENERAL, LOG_ERR, "Unknown search in ProgLister");
00386         return;
00387     }
00388 
00389     if (m_searchType == kPowerSearch)
00390     {
00391         if (text == ":::::")
00392             return;
00393 
00394         MSqlBindings bindings;
00395         genreflag = m_parent->PowerStringToSQL(text, what, bindings);
00396 
00397         if (what.isEmpty())
00398             return;
00399 
00400         MSqlEscapeAsAQuery(what, bindings);
00401     }
00402 
00403     RecordingRule *record = new RecordingRule();
00404 
00405     if (genreflag)
00406     {
00407         QString fromgenre = QString(
00408             "LEFT JOIN programgenres ON "
00409             "program.chanid = programgenres.chanid AND "
00410             "program.starttime = programgenres.starttime ");
00411         record->LoadBySearch(m_searchType, text, what, fromgenre);
00412     }
00413     else
00414     {
00415         record->LoadBySearch(m_searchType, text, what);
00416     }
00417 
00418     MythScreenStack *mainStack = GetMythMainWindow()->GetMainStack();
00419     ScheduleEditor *schededit = new ScheduleEditor(mainStack, record);
00420     if (schededit->Create())
00421     {
00422         mainStack->AddScreen(schededit);
00423         emit haveResult(m_phraseList->GetValue());
00424         Close();
00425     }
00426     else
00427         delete schededit;
00428 }
00429 
00431 
00432 EditPowerSearchPopup::EditPowerSearchPopup(MythScreenStack *parentStack,
00433                                            ProgLister *parent,
00434                                            const QString &currentValue)
00435     : MythScreenType(parentStack, "phrasepopup"),
00436         m_titleEdit(NULL), m_subtitleEdit(NULL), m_descEdit(NULL),
00437         m_categoryList(NULL), m_genreList(NULL), m_channelList(NULL),
00438         m_okButton(NULL)
00439 {
00440     m_parent = parent;
00441 
00442     //sanity check currentvalue
00443     m_currentValue = currentValue;
00444     QStringList field = m_currentValue.split(':');
00445     if (field.count() != 6)
00446     {
00447         LOG(VB_GENERAL, LOG_ERR, QString("Error. PowerSearch %1 has %2 fields")
00448                 .arg(m_currentValue).arg(field.count()));
00449         m_currentValue = ":::::";
00450     }
00451 }
00452 
00453 bool EditPowerSearchPopup::Create()
00454 {
00455     if (!LoadWindowFromXML("schedule-ui.xml", "editpowersearchpopup", this))
00456         return false;
00457 
00458     bool err = false;
00459     UIUtilE::Assign(this, m_titleEdit, "title_edit", &err);
00460     UIUtilE::Assign(this, m_subtitleEdit, "subtitle_edit", &err);
00461     UIUtilE::Assign(this, m_descEdit, "desc_edit", &err);
00462     UIUtilE::Assign(this, m_categoryList, "category_list", &err);
00463     UIUtilE::Assign(this, m_genreList, "genre_list", &err);
00464     UIUtilE::Assign(this, m_channelList, "channel_list", &err);
00465     UIUtilE::Assign(this, m_okButton, "ok_button", &err);
00466 
00467     if (err)
00468     {
00469         LOG(VB_GENERAL, LOG_ERR, "Cannot load screen 'editpowersearchpopup'");
00470         return false;
00471     }
00472 
00473     QStringList field = m_currentValue.split(':');
00474 
00475     m_titleEdit->SetText(field[0]);
00476     m_subtitleEdit->SetText(field[1]);
00477     m_descEdit->SetText(field[2]);
00478 
00479     initLists();
00480 
00481     connect(m_okButton, SIGNAL(Clicked()), this, SLOT(okClicked()));
00482 
00483     BuildFocusList();
00484 
00485     SetFocusWidget(m_titleEdit);
00486 
00487     return true;
00488 }
00489 
00490 void EditPowerSearchPopup::okClicked(void)
00491 {
00492     QString text;
00493     text =  m_titleEdit->GetText().replace(':','%').replace('*','%') + ':';
00494     text += m_subtitleEdit->GetText().replace(':','%').replace('*','%') + ':';
00495     text += m_descEdit->GetText().replace(':','%').replace('*','%') + ':';
00496 
00497     if (m_categoryList->GetCurrentPos() > 0)
00498         text += m_categories[m_categoryList->GetCurrentPos()];
00499     text += ':';
00500     if (m_genreList->GetCurrentPos() > 0)
00501         text += m_genres[m_genreList->GetCurrentPos()];
00502     text += ':';
00503     if (m_channelList->GetCurrentPos() > 0)
00504         text += m_channels[m_channelList->GetCurrentPos()];
00505 
00506     if (text == ":::::")
00507         return;
00508 
00509     m_parent->UpdateKeywordInDB(text, m_currentValue);
00510     m_parent->FillViewList(text);
00511     m_parent->SetViewFromList(text);
00512 
00513     Close();
00514 }
00515 
00516 void EditPowerSearchPopup::initLists(void)
00517 {
00518     QStringList field = m_currentValue.split(':');
00519 
00520     // category type
00521     m_categories.clear();
00522     new MythUIButtonListItem(
00523         m_categoryList, tr("(Any Program Type)"), NULL, false);
00524     m_categories << "";
00525     new MythUIButtonListItem(m_categoryList, tr("Movies"), NULL, false);
00526     m_categories << "movie";
00527     new MythUIButtonListItem(m_categoryList, tr("Series"), NULL, false);
00528     m_categories << "series";
00529     new MythUIButtonListItem(m_categoryList, tr("Show"), NULL, false);
00530     m_categories << "tvshow";
00531     new MythUIButtonListItem(m_categoryList, tr("Sports"), NULL, false);
00532     m_categories << "sports";
00533     m_categoryList->SetItemCurrent(m_categories.indexOf(field[3]));
00534 
00535     // genre
00536     m_genres.clear();
00537     new MythUIButtonListItem(m_genreList, tr("(Any Genre)"), NULL, false);
00538     m_genres << "";
00539 
00540     MSqlQuery query(MSqlQuery::InitCon());
00541 
00542     query.prepare("SELECT genre FROM programgenres GROUP BY genre;");
00543 
00544     if (query.exec())
00545     {
00546         while (query.next())
00547         {
00548             QString category = query.value(0).toString();
00549             if (category.isEmpty() || category.trimmed().isEmpty())
00550                 continue;
00551             category = query.value(0).toString();
00552             new MythUIButtonListItem(m_genreList, category, NULL, false);
00553             m_genres << category;
00554             if (category == field[4])
00555                 m_genreList->SetItemCurrent(m_genreList->GetCount() - 1);
00556         }
00557     }
00558 
00559     // channel
00560     QString channelOrdering = gCoreContext->GetSetting(
00561         "ChannelOrdering", "channum");
00562 
00563     m_channels.clear();
00564     new MythUIButtonListItem(m_channelList, tr("(Any Channel)"), NULL, false);
00565     m_channels << "";
00566 
00567     DBChanList channels = ChannelUtil::GetChannels(0, true, "callsign");
00568     ChannelUtil::SortChannels(channels, channelOrdering, true);
00569 
00570     MythUIButtonListItem *item;
00571     for (uint i = 0; i < channels.size(); ++i)
00572     {
00573         QString chantext = channels[i].GetFormatted(DBChannel::kChannelShort);
00574 
00575         m_parent->m_viewList << QString::number(channels[i].chanid);
00576         m_parent->m_viewTextList << chantext;
00577 
00578         item = new MythUIButtonListItem(m_channelList, chantext, NULL, false);
00579 
00580         InfoMap chanmap;
00581         channels[i].ToMap(chanmap);
00582         item->SetTextFromMap(chanmap);
00583 
00584         m_channels << channels[i].callsign;
00585         if (channels[i].callsign == field[5])
00586             m_channelList->SetItemCurrent(m_channelList->GetCount() - 1);
00587     }
00588 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends