Ticket #5954: dynamic_menus.2.patch

File dynamic_menus.2.patch, 6.7 KB (added by andrei@…, 17 years ago)

MENUEXEC version

  • libs/libmythui/myththemedmenu.cpp

     
    22#include <QDir>
    33#include <QKeyEvent>
    44#include <QDomDocument>
     5#include <QProcess>
    56
    67#include "myththemedmenu.h"
    78#include "mythmainwindow.h"
     
    9091                               bool allowreorder, MythThemedMenuState *state)
    9192              : MythThemedMenuState(parent, name)
    9293{
     94    Init(parent, state);
     95
     96    if (!parseMenu(menufile))
     97        m_foundtheme = false;
     98}
     99
     100/** \brief Creates a dynamic themed menu.
     101 *
     102 *  \param command      external command to execute
     103 *  \param parent       the screen stack that owns this UI type
     104 *  \param state        theme state associated with this menu
     105 */
     106MythThemedMenu::MythThemedMenu(const QString command, MythScreenStack *parent,
     107                               MythThemedMenuState *state)
     108              : MythThemedMenuState(parent, command)
     109{
     110    Init(parent, state);
     111
     112    QProcess process;
     113    process.start(command);
     114    if (!process.waitForFinished())
     115    {
     116        process.kill();
     117        VERBOSE(VB_IMPORTANT,
     118                QString("Command \"%1\" returned error\n").arg(command));
     119        ShowOkPopup(QObject::tr("Couldn't execute %1")
     120                    .arg(command));
     121
     122        m_foundtheme = false;
     123        return;
     124    }
     125    else
     126    {
     127        QString errorMsg;
     128        int errorLine = 0;
     129        int errorColumn = 0;
     130
     131        QDomDocument doc;
     132        if (!doc.setContent(process.readAllStandardOutput(), false,
     133            &errorMsg, &errorLine, &errorColumn))
     134        {
     135            VERBOSE(VB_IMPORTANT,
     136                    QString("Error parsing output of: %1\nat line: %2  column: %3 msg: %4").
     137                    arg(command).arg(errorLine).arg(errorColumn).arg(errorMsg));
     138
     139            ShowOkPopup(QObject::tr("Command %1 returned incomplete menu.")
     140                    .arg(command));
     141
     142            m_foundtheme = false;
     143            return;
     144        }
     145
     146        if (!parseMenu(doc, command))
     147            m_foundtheme = false;
     148    }
     149}
     150
     151/** \brief Initializes the menu state.
     152 *
     153 *  \param parent       the screen stack that owns this UI type
     154 *  \param state        theme state associated with this menu
     155*/
     156void MythThemedMenu::Init(MythScreenStack *parent, MythThemedMenuState *state)
     157{
    93158    m_state = state;
    94159    m_allocedstate = m_foundtheme = m_ignorekeys = m_wantpop = false;
    95160    m_exitModifier = -1;
     
    102167        m_allocedstate = true;
    103168    }
    104169
    105     Init(menufile);
    106 }
    107 
    108 /** \brief Loads the main UI theme, and a menu theme.
    109  *
    110  *  See also foundtheme(void), it will return true when called after
    111  *  this method if this method was successful.
    112  *
    113  *  \param menufile name of menu item xml file
    114  */
    115 void MythThemedMenu::Init(const QString &menufile)
    116 {
    117170    ReloadExitKey();
    118171
    119172    if (!m_state->m_loaded)
     
    133186            SLOT(setButtonActive(MythUIButtonListItem*)));
    134187    connect(m_buttonList, SIGNAL(itemClicked(MythUIButtonListItem*)),
    135188            SLOT(buttonAction(MythUIButtonListItem*)));
    136 
    137     if (!parseMenu(menufile))
    138         m_foundtheme = false;
    139189}
    140190
    141191MythThemedMenu::~MythThemedMenu(void)
     
    395443        addButton(type, text, alttext, action);
    396444}
    397445
     446/** \brief Parse the themebuttons to be added based on the contents
     447 *         of the provided XML document.
     448 */
     449bool MythThemedMenu::parseMenu(const QDomDocument &doc, const QString &menuname)
     450{
     451    QDomElement docElem = doc.documentElement();
     452
     453    m_menumode = docElem.attribute("name", "MAIN");
     454
     455    QDomNode n = docElem.firstChild();
     456    while (!n.isNull())
     457    {
     458        QDomElement e = n.toElement();
     459        if (!e.isNull())
     460        {
     461            if (e.tagName() == "button")
     462            {
     463                parseThemeButton(e);
     464            }
     465            else
     466            {
     467                VERBOSE(VB_IMPORTANT, QString("MythThemedMenu: Unknown "
     468                                              "element %1").arg(e.tagName()));
     469                return false;
     470            }
     471        }
     472        n = n.nextSibling();
     473    }
     474
     475    if (m_buttonList->GetCount() == 0)
     476    {
     477        VERBOSE(VB_IMPORTANT, QString("MythThemedMenu: No buttons "
     478                                      "for menu %1").arg(menuname));
     479        return false;
     480    }
     481
     482    if (LCD::Get())
     483    {
     484        m_titleText = "MYTH-";
     485        m_titleText += m_menumode;
     486    }
     487
     488    if (m_titleState)
     489        m_titleState->DisplayState(m_menumode);
     490
     491    m_selection = "";
     492    return true;
     493}
     494
    398495/** \brief Parse the themebuttons to be added based on the name of
    399496 *         the menu file provided.
    400497 *
     
    412509{
    413510    QString filename = findMenuFile(menuname);
    414511
    415     QDomDocument doc;
    416512    QFile f(filename);
    417513
    418514    if (!f.exists() || !f.open(QIODevice::ReadOnly))
     
    430526    int errorLine = 0;
    431527    int errorColumn = 0;
    432528
     529    QDomDocument doc;
     530
    433531    if (!doc.setContent(&f, false, &errorMsg, &errorLine, &errorColumn))
    434532    {
    435533        VERBOSE(VB_IMPORTANT,
     
    445543
    446544    f.close();
    447545
    448     QDomElement docElem = doc.documentElement();
    449 
    450     m_menumode = docElem.attribute("name", "MAIN");
    451 
    452     QDomNode n = docElem.firstChild();
    453     while (!n.isNull())
    454     {
    455         QDomElement e = n.toElement();
    456         if (!e.isNull())
    457         {
    458             if (e.tagName() == "button")
    459             {
    460                 parseThemeButton(e);
    461             }
    462             else
    463             {
    464                 VERBOSE(VB_IMPORTANT, QString("MythThemedMenu: Unknown "
    465                                               "element %1").arg(e.tagName()));
    466                 return false;
    467             }
    468         }
    469         n = n.nextSibling();
    470     }
    471 
    472     if (m_buttonList->GetCount() == 0)
    473     {
    474         VERBOSE(VB_IMPORTANT, QString("MythThemedMenu: No buttons "
    475                                       "for menu %1").arg(menuname));
    476         return false;
    477     }
    478 
    479     if (LCD::Get())
    480     {
    481         m_titleText = "MYTH-";
    482         m_titleText += m_menumode;
    483     }
    484 
    485     if (m_titleState)
    486         m_titleState->DisplayState(m_menumode);
    487 
    488     m_selection = "";
    489     return true;
     546    return parseMenu(doc, menuname);
    490547}
    491548
    492549void MythThemedMenu::updateLCD(void)
     
    629686        else
    630687            delete newmenu;
    631688    }
     689    else if (action.left(9) == "MENUEXEC ")
     690    {
     691        QString command = action.mid(9);
     692
     693        MythScreenStack *stack = GetScreenStack();
     694
     695        MythThemedMenu *newmenu = new MythThemedMenu(command, stack, m_state);
     696
     697        if (newmenu->foundTheme())
     698            stack->AddScreen(newmenu);
     699        else
     700            delete newmenu;
     701    }
    632702    else if (action.left(6) == "UPMENU")
    633703    {
    634704        m_wantpop = true;