00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <mythtv/mythcontext.h>
00011 #include <mythtv/uitypes.h>
00012
00013 #include <algorithm>
00014
00015 #include "globals.h"
00016 #include "editmetadata.h"
00017 #include "metadata.h"
00018 #include "dbaccess.h"
00019 #include "metadatalistmanager.h"
00020 #include "videoutils.h"
00021 #include "parentalcontrols.h"
00022
00023
00024 EditMetadataDialog::EditMetadataDialog(Metadata *source_metadata,
00025 const MetadataListManager &cache,
00026 MythMainWindow *parent_,
00027 const QString &window_name,
00028 const QString &theme_filename,
00029 const char *name_)
00030 : MythThemedDialog(parent_, window_name, theme_filename, name_),
00031 m_orig_metadata(source_metadata), m_meta_cache(cache)
00032 {
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 working_metadata = new Metadata(*m_orig_metadata);
00045
00046 category_select = NULL;
00047 level_select = NULL;
00048 child_check = NULL;
00049 child_select = NULL;
00050 browse_check = NULL;
00051 coverart_button = NULL;
00052 coverart_text = NULL;
00053 done_button = NULL;
00054 title_editor = NULL;
00055 player_editor = NULL;
00056
00057 wireUpTheme();
00058 fillWidgets();
00059 assignFirstFocus();
00060 }
00061
00062 namespace
00063 {
00064 template <typename T>
00065 struct title_sort
00066 {
00067 bool operator()(const T &lhs, const T &rhs)
00068 {
00069 return QString::localeAwareCompare(lhs.second, rhs.second) < 0;
00070 }
00071 };
00072 }
00073
00074 void EditMetadataDialog::fillWidgets()
00075 {
00076 if (title_editor) title_editor->setText(working_metadata->Title());
00077
00078 if (category_select)
00079 {
00080 category_select->addItem(0, VIDEO_CATEGORY_UNKNOWN);
00081 const VideoCategory::entry_list &vcl =
00082 VideoCategory::getCategory().getList();
00083 for (VideoCategory::entry_list::const_iterator p = vcl.begin();
00084 p != vcl.end(); ++p)
00085 {
00086 category_select->addItem(p->first, p->second);
00087 }
00088 category_select->setToItem(working_metadata->getCategoryID());
00089 }
00090
00091 if (level_select)
00092 {
00093 for (ParentalLevel i = ParentalLevel::plLowest;
00094 i <= ParentalLevel::plHigh && i.good(); ++i)
00095 {
00096 level_select->addItem(i.GetLevel(),
00097 QString(tr("Level %1")).arg(i.GetLevel()));
00098 }
00099 level_select->setToItem(working_metadata->ShowLevel());
00100 }
00101
00102 if (child_select)
00103 {
00104
00105
00106
00107
00108
00109 bool trip_catch = false;
00110 QString caught_name = "";
00111 int possible_starting_point = 0;
00112 child_select->addItem(0, tr("None"));
00113
00114
00115
00116 typedef std::vector<std::pair<unsigned int, QString> > title_list;
00117 const MetadataListManager::metadata_list &mdl = m_meta_cache.getList();
00118 title_list tc;
00119 tc.reserve(mdl.size());
00120 for (MetadataListManager::metadata_list::const_iterator p = mdl.begin();
00121 p != mdl.end(); ++p)
00122 {
00123 tc.push_back(std::make_pair((*p)->ID(), (*p)->Title()));
00124 }
00125 std::sort(tc.begin(), tc.end(), title_sort<title_list::value_type>());
00126
00127 for (title_list::const_iterator p = tc.begin(); p != tc.end(); ++p)
00128 {
00129 if (trip_catch)
00130 {
00131
00132
00133
00134
00135
00136
00137 QString target_name = p->second;
00138 int length_compare = 0;
00139 if (target_name.length() < caught_name.length())
00140 {
00141 length_compare = target_name.length();
00142 }
00143 else
00144 {
00145 length_compare = caught_name.length();
00146 }
00147
00148 QString caught_name_three_quarters =
00149 caught_name.left((int)(length_compare * 0.75));
00150 QString target_name_three_quarters =
00151 target_name.left((int)(length_compare * 0.75));
00152
00153 if (caught_name_three_quarters == target_name_three_quarters &&
00154 working_metadata->ChildID() == -1)
00155 {
00156 possible_starting_point = p->first;
00157 working_metadata->setChildID(possible_starting_point);
00158 }
00159 trip_catch = false;
00160 }
00161
00162 if (p->first != working_metadata->ID())
00163 {
00164 child_select->addItem(p->first, p->second);
00165 }
00166 else
00167 {
00168
00169
00170
00171
00172
00173 trip_catch = true;
00174 caught_name = p->second;
00175 }
00176 }
00177
00178 if (working_metadata->ChildID() > 0)
00179 {
00180 child_select->setToItem(working_metadata->ChildID());
00181 cachedChildSelection = working_metadata->ChildID();
00182 }
00183 else
00184 {
00185 child_select->setToItem(possible_starting_point);
00186 cachedChildSelection = possible_starting_point;
00187 }
00188 }
00189
00190 if (child_select && child_check)
00191 {
00192 child_check->setState(cachedChildSelection > 0);
00193 child_select->allowFocus(cachedChildSelection > 0);
00194 }
00195
00196 if (browse_check) browse_check->setState(working_metadata->Browse());
00197 checkedSetText(coverart_text, working_metadata->CoverFile());
00198 if (player_editor) player_editor->setText(working_metadata->PlayCommand());
00199 }
00200
00201 void EditMetadataDialog::toggleChild(bool yes_or_no)
00202 {
00203 if (child_select)
00204 {
00205 if (yes_or_no)
00206 {
00207 child_select->setToItem(cachedChildSelection);
00208 working_metadata->setChildID(cachedChildSelection);
00209 }
00210 else
00211 {
00212 child_select->setToItem(0);
00213 working_metadata->setChildID(0);
00214 }
00215 child_select->allowFocus(yes_or_no);
00216 }
00217 }
00218
00219 void EditMetadataDialog::keyPressEvent(QKeyEvent *e)
00220 {
00221 bool handled = false;
00222 bool something_pushed = false;
00223
00224 QStringList actions;
00225 gContext->GetMainWindow()->TranslateKeyPress("Video", e, actions);
00226
00227 for (unsigned int i = 0; i < actions.size() && !handled; i++)
00228 {
00229 QString action = actions[i];
00230 handled = true;
00231
00232 if (action == "UP")
00233 nextPrevWidgetFocus(false);
00234 else if (action == "DOWN")
00235 nextPrevWidgetFocus(true);
00236 else if (action == "LEFT")
00237 {
00238 something_pushed = false;
00239 if (category_select)
00240 {
00241 if (getCurrentFocusWidget() == category_select)
00242 {
00243 category_select->push(false);
00244 something_pushed = true;
00245 }
00246 }
00247 if (level_select)
00248 {
00249 if (getCurrentFocusWidget() == level_select)
00250 {
00251 level_select->push(false);
00252 something_pushed = true;
00253 }
00254 }
00255 if (child_select)
00256 {
00257 if (getCurrentFocusWidget() == child_select)
00258 {
00259 child_select->push(false);
00260 something_pushed = true;
00261 }
00262 }
00263 if (!something_pushed)
00264 {
00265 activateCurrent();
00266 }
00267 }
00268 else if (action == "RIGHT")
00269 {
00270 something_pushed = false;
00271 if (category_select)
00272 {
00273 if (getCurrentFocusWidget() == category_select)
00274 {
00275 category_select->push(true);
00276 something_pushed = true;
00277 }
00278 }
00279 if (level_select)
00280 {
00281 if (getCurrentFocusWidget() == level_select)
00282 {
00283 level_select->push(true);
00284 something_pushed = true;
00285 }
00286 }
00287 if (child_select)
00288 {
00289 if (getCurrentFocusWidget() == child_select)
00290 {
00291 child_select->push(true);
00292 something_pushed = true;
00293 }
00294 }
00295 if (!something_pushed)
00296 {
00297 activateCurrent();
00298 }
00299 }
00300 else if (action == "SELECT")
00301 {
00302 something_pushed = false;
00303 if (category_select)
00304 {
00305 if (getCurrentFocusWidget() == category_select)
00306 {
00307 QString category = QString("");
00308 if (MythPopupBox::showGetTextPopup(
00309 gContext->GetMainWindow(),
00310 "Enter category",
00311 QObject::tr("New category"),
00312 category))
00313 {
00314
00315 int id = VideoCategory::getCategory().add(category);
00316 working_metadata->setCategoryID(id);
00317 category_select->addItem(id, category);
00318 category_select->setToItem(id);
00319 }
00320 something_pushed = true;
00321 }
00322 }
00323
00324 if (!something_pushed)
00325 {
00326 activateCurrent();
00327 }
00328 }
00329 else if (action == "0")
00330 {
00331 if (done_button)
00332 done_button->push();
00333 }
00334 else
00335 handled = false;
00336 }
00337
00338 if (!handled)
00339 MythThemedDialog::keyPressEvent(e);
00340 }
00341
00342 void EditMetadataDialog::saveAndExit()
00343 {
00344
00345
00346
00347
00348 *m_orig_metadata = *working_metadata;
00349 m_orig_metadata->updateDatabase();
00350
00351
00352
00353
00354
00355 reject();
00356 }
00357
00358 void EditMetadataDialog::setTitle(QString new_title)
00359 {
00360 working_metadata->setTitle(new_title);
00361 }
00362
00363 void EditMetadataDialog::setCategory(int new_category)
00364 {
00365 working_metadata->setCategoryID(new_category);
00366 }
00367
00368 void EditMetadataDialog::setPlayer(QString new_command)
00369 {
00370 working_metadata->setPlayCommand(new_command);
00371 }
00372
00373 void EditMetadataDialog::setLevel(int new_level)
00374 {
00375 ParentalLevel nl(new_level);
00376 working_metadata->setShowLevel(nl.GetLevel());
00377 }
00378
00379 void EditMetadataDialog::setChild(int new_child)
00380 {
00381 working_metadata->setChildID(new_child);
00382 if (child_check)
00383 {
00384 child_check->setState(new_child > 0);
00385 cachedChildSelection = new_child;
00386 }
00387 }
00388
00389 void EditMetadataDialog::toggleBrowse(bool yes_or_no)
00390 {
00391 working_metadata->setBrowse(yes_or_no);
00392 }
00393
00394 void EditMetadataDialog::findCoverArt()
00395 {
00396 QString new_coverart_file;
00397 if (!isDefaultCoverFile(working_metadata->CoverFile()))
00398 {
00399 new_coverart_file = working_metadata->CoverFile();
00400 }
00401
00402 QString fileprefix = gContext->GetSetting("VideoArtworkDir");
00403
00404
00405 if (fileprefix.length() == 0)
00406 {
00407 fileprefix = MythContext::GetConfDir() + "/MythVideo";
00408 }
00409
00410 MythImageFileDialog *nca =
00411 new MythImageFileDialog(&new_coverart_file,
00412 fileprefix,
00413 gContext->GetMainWindow(),
00414 "file_chooser",
00415 "video-",
00416 "image file chooser",
00417 true);
00418 nca->exec();
00419 if (new_coverart_file.length() > 0)
00420 {
00421 working_metadata->setCoverFile(new_coverart_file);
00422 checkedSetText(coverart_text, new_coverart_file);
00423 }
00424
00425 nca->deleteLater();
00426 }
00427
00428 void EditMetadataDialog::wireUpTheme()
00429 {
00430 title_editor = getUIRemoteEditType("title");
00431 if (title_editor)
00432 {
00433 title_editor->createEdit(this);
00434 connect(title_editor, SIGNAL(textChanged(QString)),
00435 this, SLOT(setTitle(QString)));
00436 }
00437
00438 category_select = getUISelectorType("category_select");
00439 if (category_select)
00440 {
00441 connect(category_select, SIGNAL(pushed(int)),
00442 this, SLOT(setCategory(int)));
00443 }
00444
00445 player_editor = getUIRemoteEditType("player");
00446 if (player_editor)
00447 {
00448 player_editor->createEdit(this);
00449 connect(player_editor, SIGNAL(textChanged(QString)),
00450 this, SLOT(setPlayer(QString)));
00451 }
00452
00453 level_select = getUISelectorType("level_select");
00454 if (level_select)
00455 {
00456 connect(level_select, SIGNAL(pushed(int)),
00457 this, SLOT(setLevel(int)));
00458 }
00459
00460 child_check = getUICheckBoxType("child_check");
00461 if (child_check)
00462 {
00463 connect(child_check, SIGNAL(pushed(bool)),
00464 this, SLOT(toggleChild(bool)));
00465 }
00466
00467 child_select = getUISelectorType("child_select");
00468 if (child_select)
00469 {
00470 connect(child_select, SIGNAL(pushed(int)),
00471 this, SLOT(setChild(int)));
00472 }
00473
00474 browse_check = getUICheckBoxType("browse_check");
00475 if (browse_check)
00476 {
00477 connect(browse_check, SIGNAL(pushed(bool)),
00478 this, SLOT(toggleBrowse(bool)));
00479 }
00480
00481 coverart_button = getUIPushButtonType("coverart_button");
00482 if (coverart_button)
00483 {
00484 connect(coverart_button, SIGNAL(pushed()),
00485 this, SLOT(findCoverArt()));
00486 }
00487 coverart_text = getUITextType("coverart_text");
00488
00489 done_button = getUITextButtonType("done_button");
00490 if (done_button)
00491 {
00492 done_button->setText(tr("Done"));
00493 connect(done_button, SIGNAL(pushed()), this, SLOT(saveAndExit()));
00494 }
00495
00496 buildFocusList();
00497 }
00498
00499
00500 EditMetadataDialog::~EditMetadataDialog()
00501 {
00502 if (title_editor)
00503 {
00504 title_editor->deleteLater();
00505 title_editor = NULL;
00506 }
00507
00508 if (player_editor)
00509 {
00510 player_editor->deleteLater();
00511 player_editor = NULL;
00512 }
00513
00514 if (working_metadata)
00515 {
00516 delete working_metadata;
00517 working_metadata = NULL;
00518 }
00519 }