00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <mythtv/mythcontext.h>
00013 #include <mythtv/uitypes.h>
00014
00015 #include "fileassoc.h"
00016 #include "dbaccess.h"
00017
00018 class FileAssociation
00019 {
00020
00021
00022
00023
00024 public:
00025 FileAssociation(const QString &new_extension) :
00026 id(-1), extension(new_extension), player_command(""), ignore(false),
00027 use_default(true), changed(true), loaded_from_db(false) {}
00028 FileAssociation(int i, const QString &e, const QString &p, bool g, bool u) :
00029 id(i), extension(e), player_command(p), ignore(g), use_default(u),
00030 changed(false), loaded_from_db(true) {}
00031
00032 int getID() const { return id; }
00033 QString getExtension() const { return extension; }
00034 QString getCommand() const { return player_command; }
00035 bool getDefault() const { return use_default; }
00036 bool getIgnore() const { return ignore; }
00037
00038 void saveYourself()
00039 {
00040 if (changed)
00041 {
00042 id = FileAssociations::getFileAssociation()
00043 .add(extension, player_command, ignore, use_default);
00044 loaded_from_db = true;
00045 changed = false;
00046 }
00047 }
00048
00049 void deleteYourselfFromDB()
00050 {
00051 if (loaded_from_db)
00052 {
00053 FileAssociations::getFileAssociation().remove(id);
00054 loaded_from_db = false;
00055 id = -1;
00056 }
00057 }
00058
00059 void setDefault(bool yes_or_no)
00060 {
00061 if (use_default != yes_or_no)
00062 {
00063 setChanged();
00064 use_default = yes_or_no;
00065 }
00066 }
00067
00068 void setIgnore(bool yes_or_no)
00069 {
00070 if (ignore != yes_or_no)
00071 {
00072 setChanged();
00073 ignore = yes_or_no;
00074 }
00075 }
00076
00077 void setCommand(const QString &new_command)
00078 {
00079 if (player_command != new_command)
00080 {
00081 setChanged();
00082 player_command = new_command;
00083 }
00084 }
00085
00086 private:
00087 int id;
00088 QString extension;
00089 QString player_command;
00090 bool ignore;
00091 bool use_default;
00092 bool changed;
00093 bool loaded_from_db;
00094
00095 private:
00096 void setChanged() { changed = true; }
00097
00098 };
00099
00100
00101
00102
00103
00104 FileAssocDialog::FileAssocDialog(MythMainWindow *parent_,
00105 QString window_name,
00106 QString theme_filename,
00107 const char *name_)
00108 : MythThemedDialog(parent_, window_name, theme_filename, name_)
00109 {
00110 command_editor = NULL;
00111 file_associations.clear();
00112 file_associations.setAutoDelete(true);
00113 current_fa = NULL;
00114 new_extension_popup = NULL;
00115 new_extension_editor = NULL;
00116 wireUpTheme();
00117 assignFirstFocus();
00118
00119 loadFileAssociations();
00120 showCurrentFA();
00121 }
00122
00123 void FileAssocDialog::keyPressEvent(QKeyEvent *e)
00124 {
00125 bool handled = false;
00126 QStringList actions;
00127 gContext->GetMainWindow()->TranslateKeyPress("Video", e, actions);
00128
00129 for (unsigned int i = 0; i < actions.size() && !handled; i++)
00130 {
00131 QString action = actions[i];
00132 handled = true;
00133
00134 if (action == "UP")
00135 {
00136 nextPrevWidgetFocus(false);
00137 while (getCurrentFocusWidget()->GetContext() < -1)
00138 nextPrevWidgetFocus(false);
00139 }
00140 else if (action == "DOWN")
00141 {
00142 nextPrevWidgetFocus(true);
00143 while (getCurrentFocusWidget()->GetContext() < -1)
00144 nextPrevWidgetFocus(true);
00145 }
00146 else if (action == "LEFT")
00147 {
00148 if (extension_select)
00149 {
00150 if (extension_select == getCurrentFocusWidget())
00151 extension_select->push(false);
00152 }
00153 if (default_check)
00154 {
00155 if (default_check == getCurrentFocusWidget())
00156 activateCurrent();
00157 }
00158 if (ignore_check)
00159 {
00160 if (ignore_check == getCurrentFocusWidget())
00161 activateCurrent();
00162 }
00163 }
00164 else if (action == "RIGHT")
00165 {
00166 if (extension_select)
00167 {
00168 if (extension_select == getCurrentFocusWidget())
00169 extension_select->push(true);
00170 }
00171 if (default_check)
00172 {
00173 if (default_check == getCurrentFocusWidget())
00174 activateCurrent();
00175 }
00176 if (ignore_check)
00177 {
00178 if (ignore_check == getCurrentFocusWidget())
00179 activateCurrent();
00180 }
00181 }
00182 else if (action == "SELECT")
00183 activateCurrent();
00184 else
00185 handled = false;
00186 }
00187
00188 if (!handled)
00189 MythThemedDialog::keyPressEvent(e);
00190 }
00191
00192 void FileAssocDialog::loadFileAssociations()
00193 {
00194 const FileAssociations::association_list &fa_list =
00195 FileAssociations::getFileAssociation().getList();
00196
00197 for (FileAssociations::association_list::const_iterator p = fa_list.begin();
00198 p != fa_list.end(); ++p)
00199 {
00200 FileAssociation *new_fa =
00201 new FileAssociation(p->id, p->extension, p->playcommand,
00202 p->ignore, p->use_default);
00203 file_associations.append(new_fa);
00204 }
00205
00206 if (file_associations.count())
00207 {
00208 current_fa = file_associations.getFirst();
00209 }
00210 else
00211 {
00212 VERBOSE(VB_IMPORTANT, QString("%1: Couldn't get any filetypes from "
00213 "your database.").arg(__FILE__));
00214 }
00215 }
00216
00217 void FileAssocDialog::saveFileAssociations()
00218 {
00219 for (uint i = 0; i < file_associations.count(); i++)
00220 {
00221 file_associations.at(i)->saveYourself();
00222 }
00223 }
00224
00225 void FileAssocDialog::showCurrentFA()
00226 {
00227 if (!current_fa)
00228 {
00229 if (extension_select)
00230 {
00231 extension_select->SetContext(-2);
00232 }
00233 if (command_editor)
00234 {
00235 command_editor->hide();
00236 command_editor->SetContext(-2);
00237 }
00238 if (default_check)
00239 {
00240 default_check->SetContext(-2);
00241 }
00242 if (ignore_check)
00243 {
00244 ignore_check->SetContext(-2);
00245 }
00246 if (delete_button)
00247 {
00248 delete_button->SetContext(-2);
00249 }
00250 UIType *current_widget = getCurrentFocusWidget();
00251 if (current_widget)
00252 {
00253 current_widget->looseFocus();
00254 }
00255 if (new_button)
00256 {
00257 new_button->takeFocus();
00258 widget_with_current_focus = new_button;
00259 }
00260 else if (done_button)
00261 {
00262 done_button->takeFocus();
00263 widget_with_current_focus = done_button;
00264 }
00265 else
00266 {
00267 assignFirstFocus();
00268 }
00269 }
00270 else
00271 {
00272 if (extension_select)
00273 {
00274 extension_select->SetContext(-1);
00275 extension_select->cleanOut();
00276 for (uint i = 0; i < file_associations.count(); i++)
00277 {
00278 extension_select->addItem(file_associations.at(i)->getID(),
00279 file_associations.at(i)->getExtension());
00280 }
00281 extension_select->setToItem(current_fa->getID());
00282 }
00283
00284 if (command_editor)
00285 {
00286 command_editor->SetContext(-1);
00287 command_editor->show();
00288 command_editor->setText(current_fa->getCommand());
00289 }
00290 if (default_check)
00291 {
00292 default_check->SetContext(-1);
00293 default_check->setState(current_fa->getDefault());
00294 }
00295 if (ignore_check)
00296 {
00297 ignore_check->SetContext(-1);
00298 ignore_check->setState(current_fa->getIgnore());
00299 }
00300 if (delete_button)
00301 {
00302 delete_button->SetContext(-1);
00303 }
00304 }
00305 update();
00306 }
00307
00308 void FileAssocDialog::switchToFA(int which_one)
00309 {
00310 for (uint i = 0; i < file_associations.count(); i++)
00311 {
00312 if (file_associations.at(i)->getID() == which_one)
00313 {
00314 current_fa = file_associations.at(i);
00315 i = file_associations.count() + 1;
00316 }
00317 }
00318 showCurrentFA();
00319 }
00320
00321 void FileAssocDialog::saveAndExit()
00322 {
00323 saveFileAssociations();
00324 reject();
00325 }
00326
00327 void FileAssocDialog::toggleDefault(bool yes_or_no)
00328 {
00329 if (current_fa)
00330 {
00331 current_fa->setDefault(yes_or_no);
00332 }
00333 }
00334
00335 void FileAssocDialog::toggleIgnore(bool yes_or_no)
00336 {
00337 if (current_fa)
00338 {
00339 current_fa->setIgnore(yes_or_no);
00340 }
00341 }
00342
00343 void FileAssocDialog::setPlayerCommand(QString new_command)
00344 {
00345 if (current_fa)
00346 {
00347 current_fa->setCommand(new_command);
00348 }
00349 }
00350
00351 void FileAssocDialog::deleteCurrent()
00352 {
00353 if (current_fa)
00354 {
00355 current_fa->deleteYourselfFromDB();
00356 file_associations.remove(current_fa);
00357 current_fa = file_associations.first();
00358 }
00359 showCurrentFA();
00360 }
00361
00362 void FileAssocDialog::makeNewExtension()
00363 {
00364
00365
00366 new_extension_popup = new MythPopupBox(gContext->GetMainWindow(),
00367 "new extension popup");
00368 gContext->ThemeWidget(new_extension_popup);
00369
00370 new_extension_popup->addLabel("");
00371 new_extension_popup->addLabel(tr("Please enter the new extension:"));
00372 new_extension_popup->addLabel("");
00373
00374
00375 new_extension_editor = new MythRemoteLineEdit(new_extension_popup);
00376 new_extension_popup->addWidget(new_extension_editor);
00377
00378 new_extension_popup->addButton(tr("Create new extension"), this,
00379 SLOT(createExtension()));
00380 new_extension_popup->addButton(tr("Cancel"), this,
00381 SLOT(removeExtensionPopup()));
00382
00383 new_extension_editor->setFocus();
00384
00385 new_extension_popup->ShowPopup(this, SLOT(removeExtensionPopup()));
00386 }
00387
00388 void FileAssocDialog::createExtension()
00389 {
00390 QString new_extension = new_extension_editor->text();
00391 if (new_extension.length() > 0)
00392 {
00393 FileAssociation *new_fa = new FileAssociation(new_extension);
00394 file_associations.append(new_fa);
00395 current_fa = new_fa;
00396 }
00397 removeExtensionPopup();
00398 showCurrentFA();
00399 }
00400
00401 void FileAssocDialog::removeExtensionPopup()
00402 {
00403 new_extension_popup->close();
00404 new_extension_editor->deleteLater();
00405 new_extension_editor = NULL;
00406 new_extension_popup->deleteLater();
00407 new_extension_popup = NULL;
00408
00409
00410
00411
00412
00413
00414 UIType *current_widget = getCurrentFocusWidget();
00415 if (current_widget)
00416 {
00417 current_widget->looseFocus();
00418 }
00419 if (extension_select)
00420 {
00421 widget_with_current_focus = extension_select;
00422 extension_select->takeFocus();
00423 }
00424 else
00425 {
00426 assignFirstFocus();
00427 }
00428 update();
00429 }
00430
00431 void FileAssocDialog::wireUpTheme()
00432 {
00433 extension_select = getUISelectorType("extension_select");
00434 if (extension_select)
00435 {
00436 connect(extension_select, SIGNAL(pushed(int)), this,
00437 SLOT(switchToFA(int)));
00438 }
00439 command_editor = getUIRemoteEditType("command");
00440 if (command_editor)
00441 {
00442 command_editor->createEdit(this);
00443 connect(command_editor, SIGNAL(textChanged(QString)),
00444 this, SLOT(setPlayerCommand(QString)));
00445 }
00446
00447 default_check = getUICheckBoxType("default_check");
00448 if (default_check)
00449 {
00450 connect(default_check, SIGNAL(pushed(bool)), this,
00451 SLOT(toggleDefault(bool)));
00452 }
00453
00454 ignore_check = getUICheckBoxType("ignore_check");
00455 if (ignore_check)
00456 {
00457 connect(ignore_check, SIGNAL(pushed(bool)), this,
00458 SLOT(toggleIgnore(bool)));
00459 }
00460
00461 done_button = getUITextButtonType("done_button");
00462 if (done_button)
00463 {
00464 done_button->setText(tr("Done"));
00465 connect(done_button, SIGNAL(pushed()), this, SLOT(saveAndExit()));
00466 }
00467
00468 new_button = getUITextButtonType("new_button");
00469 if (new_button)
00470 {
00471 new_button->setText(tr("New"));
00472 connect(new_button, SIGNAL(pushed()), this, SLOT(makeNewExtension()));
00473 }
00474
00475 delete_button = getUITextButtonType("delete_button");
00476 if (delete_button)
00477 {
00478 delete_button->setText(tr("Delete"));
00479 connect(delete_button, SIGNAL(pushed()), this, SLOT(deleteCurrent()));
00480 }
00481 buildFocusList();
00482 }
00483
00484 FileAssocDialog::~FileAssocDialog()
00485 {
00486 file_associations.clear();
00487 if (command_editor)
00488 {
00489 command_editor->deleteLater();
00490 command_editor = NULL;
00491 }
00492 }