Ticket #3334: importicons-libmythtv-04102007.diff

File importicons-libmythtv-04102007.diff, 31.7 KB (added by Matthew Wire <devel@…>, 19 years ago)

Code cleanup

  • libs/libmythtv/channeleditor.cpp

     
    1919#include "sourceutil.h"
    2020
    2121#include "scanwizard.h"
     22#include "importicons.h"
    2223
    2324ChannelWizard::ChannelWizard(int id, int default_sourceid)
    2425    : ConfigurationWizard()
     
    110111        return 0;
    111112}
    112113
    113 void ChannelListSetting::fillSelections(void)
     114int ChannelListSetting::fillSelections(void)
    114115{
    115116    QString currentValue = getValue();
    116117    clearSelections();
    117118    addSelection(QObject::tr("(New Channel)"));
    118119    bool fAllSources = true;
     120    int nCount = 0;
    119121
    120122    QString querystr = "SELECT channel.name,channum,chanid ";
    121123
     
    184186                name += " (" + sourceid  + ")";
    185187
    186188            addSelection(name, chanid, (chanid == currentValue) ? true : false);
     189            nCount++;
    187190        }
    188191    }
     192    return nCount;
    189193}
    190194
    191195class SourceSetting : public ComboBoxSetting, public Storage
     
    266270    buttonScan->setLabel(QObject::tr("Channel Scanner"));
    267271    buttonScan->setHelpText(QObject::tr("Starts the channel scanner."));
    268272    buttonScan->setEnabled(SourceUtil::IsAnySourceScanable());
     273   
     274    buttonImportIcon = new TransButtonSetting();
     275    buttonImportIcon->setLabel(QObject::tr("Icon Import"));
     276    buttonImportIcon->setHelpText(QObject::tr("Starts the icon importer"));
    269277
    270278    buttonTransportEditor = new TransButtonSetting();
    271279    buttonTransportEditor->setLabel(QObject::tr("Transport Editor"));
     
    278286    HorizontalConfigurationGroup *h =
    279287        new HorizontalConfigurationGroup(false, false);
    280288    h->addChild(buttonScan);
     289    h->addChild(buttonImportIcon);
    281290    h->addChild(buttonTransportEditor);
    282291    addChild(h);
    283292
     
    293302            this, SLOT(menu(int)));
    294303    connect(buttonScan, SIGNAL(pressed()),
    295304            this, SLOT(scan()));
     305    connect(buttonImportIcon,  SIGNAL(pressed()),
     306            this, SLOT(channelIconImport()));
    296307    connect(buttonTransportEditor, SIGNAL(pressed()),
    297308            this, SLOT(transportEditor()));
    298309    connect(del,  SIGNAL(pressed()),
  • libs/libmythtv/libmythtv.pro

    @@ -459,3 +470,36 @@
         list->fillSelections();
         list->setFocus();
     }
    +
    +void ChannelEditor::channelIconImport(void)
    +{
    +    if (list->fillSelections() == 0)
    +    {
    +        MythPopupBox::showOkPopup(gContext->GetMainWindow(), "",
    +                                        tr("Add some for channels first!"));
    +        return;
    +    }
    +    QStringList buttons;
    +    buttons.append(tr("Import all icons.."));
    +    buttons.append(tr("Rescan for missing icons.."));
    +//    buttons.append(tr("Import icon for selected channel"));
    +    int val = MythPopupBox::showButtonPopup(gContext->GetMainWindow(),
    +                                             "", "Channel Icon Import", buttons, 0);
    +    ImportIconsWizard *iconwizard;
    +    if (val == 0)
    +        iconwizard = new ImportIconsWizard(false);
    +    else if (val == 1)
    +        iconwizard = new ImportIconsWizard(true);
    +    else if (val == 2) // Import a single channel icon
    +    {
    +        //iconwizard = new ImportIconsWizard(true);
    +        MythPopupBox::showOkPopup(gContext->GetMainWindow(), "",
    +                                        tr("Sorry, not yet implemented!"));
    +        return;
    +    }
    +    iconwizard->exec();
    +    iconwizard->deleteLater();
    +    
    +    list->fillSelections();
    +    list->setFocus();
    +}
     
    148148HEADERS += playgroup.h              progdetails.h
    149149HEADERS += channeleditor.h          channelsettings.h
    150150HEADERS += previewgenerator.h       transporteditor.h
     151HEADERS += importicons.h
    151152
    152153SOURCES += programinfo.cpp          proglist.cpp
    153154SOURCES += storagegroup.cpp
     
    170171SOURCES += progdetails.cpp
    171172SOURCES += channeleditor.cpp        channelsettings.cpp
    172173SOURCES += previewgenerator.cpp     transporteditor.cpp
     174SOURCES += importicons.cpp
    173175
    174176# DiSEqC
    175177HEADERS += diseqc.h                 diseqcsettings.h
  • libs/libmythtv/importicons.h

     
     1/* -*- Mode: c++ -*-
     2 * vim: set expandtab tabstop=4 shiftwidth=4:
     3 *
     4 * Original Project
     5 *      MythTV      http://www.mythtv.org
     6 *
     7 * Author(s):
     8 *      John Pullan, Matthew Wire 
     9 *
     10 * Description:
     11 */
     12
     13#ifndef IMPORTICONS_H
     14#define IMPORTICONS_H
     15
     16#include <qsqldatabase.h>
     17#include <qurl.h>
     18
     19#include "settings.h"
     20
     21class ImportIconsWizard : public QObject, public ConfigurationWizard
     22{
     23    Q_OBJECT
     24public:
     25    ImportIconsWizard(bool fRefresh); //!< constructs an ImportIconWizard
     26
     27private:
     28
     29    enum dialogState
     30    {
     31        STATE_NORMAL,
     32        STATE_SEARCHING
     33    };
     34   
     35    struct CSVEntry                  //! describes the TV channel name
     36    {
     37        QString strChanId;           //!< local channel id
     38        QString strName;             //!< channel name
     39        QString strXmlTvId;          //!< the xmltvid
     40        QString strCallsign;         //!< callsign
     41        QString strTransportId;      //!< transport id
     42        QString strAtscMajorChan;    //!< ATSC major number
     43        QString strAtscMinorChan;    //!< ATSC minor number
     44        QString strNetworkId;        //!< network id
     45        QString strServiceId;        //!< service id
     46        QString strIconCSV;          //!< icon name (csv form)
     47        QString strNameCSV;          //!< name (csv form)
     48    }; 
     49    //! List of CSV entries
     50    typedef QValueList<CSVEntry> ListEntries;     
     51    //! iterator over list of CSV entries
     52    typedef QValueListIterator<CSVEntry> ListEntriesIter;
     53
     54    ListEntries m_listEntries;       //!< list of TV channels to search for
     55    ListEntries m_missingEntries;    //!< list of TV channels with no unique icon
     56    ListEntriesIter m_iter;          //!< the current iterator
     57    ListEntriesIter m_missingIter;
     58
     59    struct SearchEntry               //! search entry results
     60    {
     61        QString strID;               //!< the remote channel id
     62        QString strName;             //!< the remote name
     63        QString strLogo;             //!< the actual logo
     64    };
     65    //! List of SearchEntry entries
     66    typedef QValueList<SearchEntry> ListSearchEntries;
     67    //! iterator over list of SearchEntry entries
     68    typedef QValueListIterator<SearchEntry> ListSearchEntriesIter;
     69
     70    ListSearchEntries m_listSearch;  //!< the list of SearchEntry
     71    QString m_strMatches;            //!< the string for the submit() call
     72
     73    static const QString url;        //!< the default url
     74    QString m_strChannelDir;         //!< the location of the channel icon dir
     75
     76    bool m_fRefresh;                 //!< are we doing a refresh or not
     77    int m_nMaxCount;                 //!< the maximum number of TV channels
     78    int m_nCount;                    //!< the current search point (0..m_nMaxCount)
     79    int m_missingMaxCount;           //!< the total number of missing icons
     80    int m_missingCount;              //!< the current search point (0..m_missingCount)
     81
     82    /*! \brief changes a string into csv format
     83     * \param str the string to change
     84     * \return the actual string
     85     */
     86    QString escape_csv(const QString& str);
     87
     88    /*! \brief extracts the csv values out of a string
     89     * \param str the string to work on
     90     * \return the actual QStringList
     91     */
     92    QStringList extract_csv(const QString& strLine);
     93
     94    /*! \brief use the equivalent of wget to fetch the POST command
     95     * \param url the url to send this to
     96     * \param strParam the string to send
     97     * \return the actual string
     98     */
     99    QString wget(QUrl& url,const QString& strParam);
     100 
     101    TransLineEditSetting *m_editName;    //!< name field for the icon
     102    TransListBoxSetting *m_listIcons;    //!< list of potential icons
     103    TransLineEditSetting *m_editManual;  //!< manual edit field
     104    TransButtonSetting *m_buttonManual;  //!< manual button field
     105    TransButtonSetting *m_buttonSkip;    //!< button skip
     106    TransButtonSetting *m_buttonSelect;    //!< button skip
     107
     108    /*! \brief determines if a particular icon is blocked
     109     * \param str the string to work on
     110     * \return true/false
     111     */
     112    bool isBlocked(const QString& strParam);
     113
     114    /*! \brief looks up the string to determine the caller/xmltvid
     115     * \param str the string to work on
     116     * \return true/false
     117     */
     118    bool lookup(const QString& strParam);
     119
     120    /*! \brief search the remote db for icons etc
     121     * \param str the string to work on
     122     * \return true/false
     123     */
     124    bool search(const QString& strParam);
     125
     126    /*! \brief submit the icon information back to the remote db
     127     * \param str the string to work on
     128     * \return true/false
     129     */
     130    bool submit(const QString& strParam);
     131
     132    /*! \brief retrieve the actual logo for the TV channel
     133     * \param str the string to work on
     134     * \return true/false
     135     */
     136    bool findmissing(const QString& strParam);
     137
     138    /*! \brief checks and attempts to download the logo file to the appropriate
     139     *   place
     140     * \param str the string of the downloaded url
     141     * \return true/false
     142     */
     143    bool checkAndDownload(const QString& str);
     144
     145    /*! \brief attempt the inital load of the TV channel information
     146     * \return the number of TV channels
     147     */
     148    unsigned initialLoad();
     149
     150    /*! \brief attempts to move the itaration on one/more than one
     151     * \return true if we can go again or false if we can not
     152     */
     153    bool doLoad();
     154   
     155    bool m_cancelled;
     156   
     157    ~ImportIconsWizard() { };
     158   
     159protected slots:
     160    void enableControls(dialogState state=STATE_NORMAL);         //!< enable/disable the controls
     161    void manualSearch();           //!< process the manual search
     162    void menuSelect();
     163    void menuSelection(int nIndex);//!< process the icon selection
     164    void skip();                   //!< skip this icon
     165    void cancelPressed();
     166
     167};
     168
     169#endif // IMPORTICONS_H
  • libs/libmythtv/channeleditor.h

     
    2222    void edit();
    2323    void edit(int);
    2424    void scan(void);
    25     void transportEditor();
    26     void deleteChannels();
     25    void transportEditor(void);
     26    void channelIconImport(void);
     27    void deleteChannels(void);
    2728
    2829private:
    2930    int                 id;
    3031    SourceSetting      *source;
    3132    ChannelListSetting *list;
    3233    TransButtonSetting *buttonScan;
     34    TransButtonSetting *buttonImportIcon;
    3335    TransButtonSetting *buttonTransportEditor;
    3436};
    3537
     
    6870    bool getHideMode() { return currentHideMode; };
    6971
    7072public slots:
    71     void fillSelections(void);
     73    int fillSelections(void);
    7274    void setSortMode(const QString& sort) {
    7375        if (currentSortMode != sort) {
    7476            currentSortMode = sort;
  • libs/libmythtv/importicons.cpp

     
     1#include <sys/stat.h>
     2#include <qapplication.h>
     3#include <qregexp.h>
     4#include <qbuffer.h>
     5#include <qfileinfo.h>
     6
     7#include "httpcomms.h"
     8#include "importicons.h"
     9#include "util.h"
     10
     11ImportIconsWizard::ImportIconsWizard(bool fRefresh)
     12{
     13    m_fRefresh = fRefresh;   
     14   
     15        VerticalConfigurationGroup *manSearch =
     16        new VerticalConfigurationGroup(false,false,true,true);
     17   
     18    manSearch->addChild(m_editName = new TransLineEditSetting(false));
     19    m_editName->setLabel(QObject::tr("Name"));
     20    m_editName->setHelpText(QObject::tr("Name of the icon file"));
     21
     22    manSearch->addChild(m_listIcons = new TransListBoxSetting());
     23    m_listIcons->setHelpText(QObject::tr("List of possible icon files"));
     24
     25    m_editManual = new TransLineEditSetting();
     26    m_editManual->setHelpText(QObject::tr("Enter text here for the manual search"));
     27
     28    m_buttonManual = new TransButtonSetting();
     29    m_buttonManual->setLabel(QObject::tr("&Search"));
     30    m_buttonManual->setHelpText(QObject::tr("Manually search for the text"));
     31
     32    m_buttonSkip = new TransButtonSetting();
     33    m_buttonSkip->setLabel(QObject::tr("S&kip"));
     34    m_buttonSkip->setHelpText(QObject::tr("Skip this icon"));
     35
     36    m_buttonSelect = new TransButtonSetting();
     37    m_buttonSelect->setLabel(QObject::tr("S&elect"));
     38    m_buttonSelect->setHelpText(QObject::tr("Select this icon"));
     39
     40    HorizontalConfigurationGroup *hrz1 =
     41        new HorizontalConfigurationGroup(false, false, true, true);
     42
     43    hrz1->addChild(m_editManual);
     44    hrz1->addChild(m_buttonManual);
     45    hrz1->addChild(m_buttonSkip);
     46    hrz1->addChild(m_buttonSelect);
     47    manSearch->addChild(hrz1);
     48   
     49    addChild(manSearch);
     50
     51    connect(m_editManual, SIGNAL(valueChanged( const QString&)),
     52            this, SLOT(  enableControls()));
     53    connect(m_buttonManual, SIGNAL(pressed()), this, SLOT(manualSearch()));
     54    connect(m_buttonSkip, SIGNAL(pressed()), this, SLOT(skip()));
     55    connect(m_listIcons,SIGNAL(accepted(int)),this,
     56             SLOT(menuSelection(int)));
     57    connect(m_buttonSelect,SIGNAL(pressed()),this,
     58             SLOT(menuSelect()));
     59
     60    enableControls();
     61   
     62
     63    m_strChannelDir =  MythContext::GetConfDir()+ "/channels";
     64    mkdir(MythContext::GetConfDir(),0776);
     65    mkdir(m_strChannelDir,0776);
     66    m_strChannelDir+="/";
     67    m_cancelled=false;
     68
     69    if (initialLoad() > 0)
     70    {
     71        m_missingIter=m_missingEntries.begin();
     72        m_missingCount=0;
     73        enableControls();
     74        doLoad();
     75    }
     76}
     77
     78const QString ImportIconsWizard::url="http://services.mythtv.org/channel-icon/";
     79
     80void ImportIconsWizard::enableControls(dialogState state)
     81{
     82    switch (state)
     83    {
     84        case STATE_NORMAL:
     85            if (m_editManual->getValue())
     86                m_buttonManual->setEnabled(true);
     87           else
     88           m_buttonManual->setEnabled(false);
     89           if (m_missingCount < m_missingMaxCount)
     90           {
     91               m_buttonSkip->setEnabled(true);
     92               m_editName->setEnabled(true);
     93               m_listIcons->setEnabled(true);
     94               m_editManual->setEnabled(true);
     95               m_buttonSelect->setEnabled(true);
     96//               m_editManual->setValue("");
     97           }
     98           else
     99           {
     100               m_buttonSkip->setEnabled(false);
     101               m_editName->setEnabled(false);
     102               m_listIcons->setEnabled(false);
     103               m_editManual->setEnabled(false);
     104               m_buttonManual->setEnabled(false);
     105               m_buttonSelect->setEnabled(false);
     106           }
     107           break;
     108        case STATE_SEARCHING:
     109               m_buttonSkip->setEnabled(false);
     110               m_buttonSelect->setEnabled(false);
     111               m_buttonManual->setEnabled(false);
     112               m_listIcons->setEnabled(false);
     113               m_listIcons->clearSelections();
     114               m_listIcons->addSelection("Please wait...");
     115        break;
     116    }       
     117}
     118
     119void ImportIconsWizard::manualSearch()
     120{
     121    enableControls(STATE_SEARCHING);
     122    QString str = m_editManual->getValue();
     123    search(escape_csv(str));   
     124    enableControls(STATE_NORMAL);
     125}
     126
     127void ImportIconsWizard::skip()
     128{
     129    enableControls(STATE_SEARCHING);
     130    m_missingCount++;
     131    m_missingIter++;   
     132    doLoad();
     133    enableControls(STATE_NORMAL);
     134}
     135
     136void ImportIconsWizard::menuSelect()
     137{
     138    menuSelection(m_listIcons->currentItem());   
     139}
     140
     141void ImportIconsWizard::menuSelection(int nIndex)
     142{
     143    enableControls(STATE_SEARCHING);
     144    SearchEntry entry = *(m_listSearch.at(nIndex));
     145    if (!isBlocked((*m_iter).strIconCSV) &&
     146         checkAndDownload(entry.strLogo))
     147    {
     148        CSVEntry entry2 = (*m_iter);
     149        m_strMatches += QString("%1,%2,%3,%4,%5,%6,%7,%8,%9\n").
     150                              arg(escape_csv(entry.strID)).
     151                              arg(escape_csv(entry2.strName)).
     152                              arg(escape_csv(entry2.strXmlTvId)).
     153                              arg(escape_csv(entry2.strCallsign)).
     154                              arg(escape_csv(entry2.strTransportId)).
     155                              arg(escape_csv(entry2.strAtscMajorChan)).
     156                              arg(escape_csv(entry2.strAtscMinorChan)).
     157                              arg(escape_csv(entry2.strNetworkId)).
     158                              arg(escape_csv(entry2.strServiceId));
     159        m_missingCount++;
     160        m_missingIter++;
     161        doLoad();
     162        enableControls(STATE_NORMAL);
     163    }
     164    else
     165    {
     166        MythPopupBox::showOkPopup(gContext->GetMainWindow(),
     167                            QObject::tr("Error downloading"),
     168                            QObject::tr("Failed to download the icon file"));       
     169        enableControls(STATE_NORMAL);
     170    }
     171   
     172}
     173
     174unsigned ImportIconsWizard::initialLoad()
     175{
     176    VERBOSE(VB_CHANNEL, "initialLoad");
     177    MSqlQuery query(MSqlQuery::InitCon());
     178    query.prepare("SELECT chanid,name,xmltvid,callsign,"
     179                  "dtv_multiplex.transportid, "
     180                  "atsc_major_chan,atsc_minor_chan,dtv_multiplex.networkid,"
     181                  "channel.serviceid, "
     182                  "channel.mplexid, dtv_multiplex.mplexid,"
     183                  "channel.icon, channel.visible FROM "
     184                  "channel, dtv_multiplex WHERE "
     185                  "channel.visible && "
     186                  "channel.mplexid=dtv_multiplex.mplexid ORDER BY name");
     187       
     188    m_listEntries.clear();           
     189    m_nCount=0;
     190    m_nMaxCount=0;
     191    m_missingMaxCount=0;
     192
     193    if (query.exec() && query.isActive() && query.size() > 0)
     194    {
     195        MythProgressDialog *progressDialog = new MythProgressDialog("Initialising, please wait...", query.size());
     196       
     197        sleep(2); // Ensures dialog is drawn before ping freezes execution
     198       
     199        if (!ping("services.mythtv.org", 3))
     200        {
     201            progressDialog->Close();
     202            MythPopupBox::showOkPopup(gContext->GetMainWindow(),
     203                                  tr("Bad Host"),
     204                                  tr("Failed to connect to the remote server"));
     205            return 0;
     206        }
     207
     208        while(query.next())
     209        {
     210            CSVEntry entry;
     211
     212            if (m_fRefresh)
     213            {
     214                QFileInfo file(query.value(11).toString());
     215                if (file.exists())
     216                    continue;
     217            }
     218           
     219            entry.strChanId=query.value(0).toString();
     220            entry.strName=query.value(1).toString();
     221            entry.strXmlTvId=query.value(2).toString();
     222            entry.strCallsign=query.value(3).toString();
     223            entry.strTransportId=query.value(4).toString();
     224            entry.strAtscMajorChan=query.value(5).toString();
     225            entry.strAtscMinorChan=query.value(6).toString();
     226            entry.strNetworkId=query.value(7).toString();
     227            entry.strServiceId=query.value(8).toString();
     228            entry.strIconCSV= QString("%1,%2,%3,%4,%5,%6,%7,%8,%9\n").
     229                              arg(escape_csv(entry.strChanId)).
     230                              arg(escape_csv(entry.strName)).
     231                              arg(escape_csv(entry.strXmlTvId)).
     232                              arg(escape_csv(entry.strCallsign)).
     233                              arg(escape_csv(entry.strTransportId)).
     234                              arg(escape_csv(entry.strAtscMajorChan)).
     235                              arg(escape_csv(entry.strAtscMinorChan)).
     236                              arg(escape_csv(entry.strNetworkId)).
     237                              arg(escape_csv(entry.strServiceId));
     238            entry.strNameCSV=escape_csv(entry.strName);
     239            VERBOSE(VB_IMPORTANT,QString("chanid %1").arg(entry.strIconCSV));
     240
     241            m_listEntries.append(entry);
     242            m_nMaxCount++;
     243            progressDialog->setProgress(m_nMaxCount);
     244        }
     245        progressDialog->Close();
     246    }
     247   
     248    m_listIcons->clearSelections();
     249    m_editName->setValue("");
     250    m_iter = m_listEntries.begin();
     251   
     252    MythProgressDialog *progressDialog = new MythProgressDialog("Downloading, please wait...",
     253                                m_listEntries.size()+1, true, this, SLOT(cancelPressed()));
     254    while (!m_cancelled && (m_iter != m_listEntries.end()))
     255    {
     256        progressDialog->setLabel(QString("Downloading %1 / %2 : ").arg(m_nCount+1)
     257                                 .arg(m_listEntries.size()+1) + (*m_iter).strName +
     258                                 QString("\nCould not find %1 icons.").arg(m_missingEntries.size()+1));
     259        if (!findmissing((*m_iter).strIconCSV))
     260            m_missingEntries.append((*m_iter));
     261        m_nCount++;
     262        m_iter++;
     263        m_missingMaxCount++;
     264        progressDialog->setProgress(m_nCount);
     265    }
     266    progressDialog->Close();
     267   
     268    //Comment below for debugging - cancel button will continue to dialog
     269    if (m_cancelled)
     270        m_nMaxCount=0;
     271    return m_nMaxCount;
     272}
     273
     274bool ImportIconsWizard::doLoad()
     275{
     276    VERBOSE(VB_CHANNEL, QString("%1 / %2").arg(m_missingCount).arg(m_missingMaxCount));
     277    if (m_missingCount >= m_missingMaxCount)
     278    {
     279        VERBOSE(VB_CHANNEL, "doLoad Icon search complete");
     280        if (!m_strMatches.isEmpty())
     281        {
     282            int nVal = MythPopupBox::showOkCancelPopup(
     283                            gContext->GetMainWindow(),
     284                            QObject::tr("Submit information"),
     285                            QObject::tr("You now have the opportunity to "
     286                                        "transmit your choices  back to "
     287                                        "mythtv.org so that others can "
     288                                        "benefit from your selections."),
     289                          true);       
     290             if (nVal == 1)
     291                 submit(m_strMatches);
     292        }
     293        //FIXME: Exit dialog here
     294        return false;
     295    }
     296    else
     297    {
     298        // Look for the next missing icon
     299        m_editName->setValue((*m_missingIter).strName);
     300        m_buttonSelect->setEnabled(search((*m_missingIter).strNameCSV));
     301        return true;
     302    }
     303}
     304
     305QString ImportIconsWizard::escape_csv(const QString& str)
     306{
     307    VERBOSE(VB_CHANNEL, "escape_csv");
     308    QRegExp rxDblForEscape("\"");
     309    QString str2 = str;
     310    str2.replace(rxDblForEscape,"\\\"");
     311    return "\""+str2+"\"";
     312}
     313
     314QStringList ImportIconsWizard::extract_csv(const QString& strLine)
     315{
     316    VERBOSE(VB_CHANNEL, "extract_csv");
     317    QStringList ret;
     318    //Clean up the line and split out the fields
     319    QString str = strLine;
     320
     321    unsigned int pos = 0;
     322    bool fFinish = false;
     323    while(!fFinish)
     324    {
     325        str=str.stripWhiteSpace();
     326        while(!fFinish)
     327        {
     328            QString strLeft;
     329            switch (str.at(pos).unicode())
     330            {
     331            case '\\':
     332                if (pos>=1)
     333                    str.left(pos-1)+str.mid(pos+1);
     334                else
     335                    str=str.mid(pos+1);
     336                pos+=2;
     337                if (pos > str.length())
     338                {
     339                    strLeft = str.left(pos);
     340                    if (strLeft.startsWith("\"") && strLeft.endsWith("\""))
     341                        strLeft=strLeft.mid(1,strLeft.length()-2);
     342                    ret.append(strLeft);
     343                    fFinish = true;
     344                }
     345                break;
     346            case ',':
     347                strLeft = str.left(pos);
     348                if (strLeft.startsWith("\"") && strLeft.endsWith("\""))
     349                    strLeft=strLeft.mid(1,strLeft.length()-2);
     350                ret.append(strLeft);
     351                if ((pos+1) > str.length())
     352                   fFinish = true;
     353                str=str.mid(pos+1);
     354                pos=0;
     355                break;
     356            default:
     357                pos++;
     358                if (pos > str.length())
     359                {
     360                    strLeft = str.left(pos);
     361                    if (strLeft.startsWith("\"") && strLeft.endsWith("\""))
     362                        strLeft=strLeft.mid(1,strLeft.length()-2);
     363                    ret.append(strLeft);
     364                    fFinish = true;
     365                }
     366            }
     367        }
     368    }
     369    return ret;
     370}
     371
     372
     373QString ImportIconsWizard::wget(QUrl& url,const QString& strParam )
     374{
     375    VERBOSE(VB_CHANNEL, "wget");
     376    QByteArray raw;
     377    QTextStream rawStream(raw,IO_WriteOnly);
     378    rawStream << strParam;
     379
     380    QBuffer data(raw);
     381    QHttpRequestHeader header;
     382
     383    header.setContentType(QString("application/x-www-form-urlencoded"));
     384    header.setContentLength(raw.size());
     385
     386    header.setValue("User-Agent", "MythTV Channel Icon lookup bot");
     387
     388    QString str = HttpComms::postHttp(url,&header,&data);
     389
     390    return str;
     391}
     392
     393bool ImportIconsWizard::checkAndDownload(const QString& str)
     394{
     395    VERBOSE(VB_CHANNEL, "checkAndDownload");
     396    int iIndex = str.findRev('/');
     397    QString str2;
     398    if (iIndex < 0)
     399        str2=str;
     400    else
     401        str2=str.mid(iIndex+1);
     402
     403    QString str3 = str;
     404    QFileInfo file(m_strChannelDir+str2);
     405
     406    bool fRet;
     407    if (!file.exists())
     408        fRet = HttpComms::getHttpFile(m_strChannelDir+str2,str3);   
     409    else
     410        fRet = true;
     411
     412    if (fRet)
     413    {
     414        MSqlQuery query(MSqlQuery::InitCon());
     415        QString  qstr = "UPDATE channel SET icon = :ICON "
     416                        "WHERE chanid = :CHANID";
     417
     418        query.prepare(qstr);
     419        query.bindValue(":ICON", m_strChannelDir+str2);
     420        query.bindValue(":CHANID", (*m_iter).strChanId);
     421
     422        if (!query.exec())
     423        {
     424            MythContext::DBError("Error inserting channel icon", query);
     425            return false;
     426        }
     427 
     428    }
     429    return fRet;
     430}
     431
     432bool ImportIconsWizard::isBlocked(const QString& strParam)
     433{
     434    VERBOSE(VB_CHANNEL, "isBlocked");
     435    QString strParam1 = strParam;
     436    QUrl::encode(strParam1);
     437    QUrl url(ImportIconsWizard::url+"/checkblock");
     438    QString str = wget(url,"csv="+strParam1);
     439    if (str.startsWith("Error",false))
     440    {
     441        VERBOSE(VB_IMPORTANT, QString("Error from isBlocked : %1").arg(str));
     442        return true;
     443    }
     444    else if (str.isEmpty() || str.startsWith("\r\n"))
     445        return false;
     446    else
     447    {
     448        VERBOSE(VB_IMPORTANT, QString("Working isBlocked"));
     449        int nVal = MythPopupBox::showOkCancelPopup(gContext->GetMainWindow(),
     450                            QObject::tr("Icon is blocked"),
     451                            QObject::tr("This combination of channel and icon "
     452                                        "has been blocked by the MythTV "
     453                                        "admins. The most common reason for "
     454                                        "this is that there is a better match "
     455                                        "available.\n "
     456                                        "Are you still sure that you want to "
     457                                        "use this icon?"),
     458                          true);       
     459        if (nVal == 0)
     460            return false;
     461        else
     462            return true;
     463    }
     464}
     465
     466
     467bool ImportIconsWizard::lookup(const QString& strParam)
     468{
     469    QString strParam1 = "callsign="+strParam;
     470    QUrl::encode(strParam1);
     471    QUrl url(ImportIconsWizard::url+"/lookup");
     472
     473    QString str = wget(url,strParam1);
     474    if (str.isEmpty() || str.startsWith("Error",false))
     475    {
     476        VERBOSE(VB_IMPORTANT, QString("Error from lookup : %1").arg(str));
     477        return true;
     478    }
     479    else
     480    {
     481        VERBOSE(VB_IMPORTANT, QString("Working lookup : %1").arg(str));
     482        return false;
     483    }
     484}
     485
     486bool ImportIconsWizard::search(const QString& strParam)
     487{
     488    QString strParam1 = strParam;
     489    QUrl::encode(strParam1);
     490    QUrl url(ImportIconsWizard::url+"/search");
     491
     492    QString str = wget(url,"s="+strParam1);
     493   
     494    m_listSearch.clear();
     495    m_listIcons->clearSelections();
     496
     497    if (str.isEmpty() || str.startsWith("#") || str.startsWith("Error",false))
     498    {
     499        VERBOSE(VB_IMPORTANT, QString("Error from search : %1").arg(str));
     500        return false;
     501    }
     502    else
     503    {
     504        VERBOSE(VB_IMPORTANT, QString("Working search : %1").arg(str));
     505        QStringList strSplit=QStringList::split("\n",str);
     506
     507        for (QStringList::iterator begin=strSplit.begin();
     508             begin!=strSplit.end();begin++)
     509        {
     510            if (*begin != "#" )
     511            {
     512                QStringList ret = extract_csv(*begin);
     513                VERBOSE(VB_IMPORTANT, QString(" search : %1 %2 %3").arg(ret[0]).arg(ret[1]).arg(ret[2]));
     514                SearchEntry entry;
     515                entry.strID=ret[0];
     516                entry.strName=ret[1];
     517                entry.strLogo=ret[2];
     518                m_listSearch.append(entry);
     519                m_listIcons->addSelection(entry.strName);
     520            }
     521        }
     522
     523        return true;
     524    }
     525}
     526
     527bool ImportIconsWizard::findmissing(const QString& strParam)
     528{
     529    QString strParam1 = strParam;
     530    QUrl::encode(strParam1);
     531    QUrl url(ImportIconsWizard::url+"/findmissing");
     532
     533    QString str = wget(url,"csv="+strParam1);
     534    VERBOSE(VB_IMPORTANT, QString("findmissing : strParam1 = %1. str = %2").arg(strParam1).arg(str));   
     535    if (str.isEmpty() || str.startsWith("#") || str.startsWith("Error",false))
     536    {
     537        VERBOSE(VB_IMPORTANT, QString("Error from findmissing : %1").arg(str));
     538        return false;
     539    }
     540    else
     541    {
     542        VERBOSE(VB_IMPORTANT, QString("Working findmissing : %1").arg(str));
     543        QStringList strSplit=QStringList::split("\n",str);
     544        for (QStringList::iterator begin=strSplit.begin();
     545             begin!=strSplit.end();begin++)
     546        {
     547            if (*begin != "#" )
     548            {
     549                QStringList ret = extract_csv(*begin);
     550                VERBOSE(VB_IMPORTANT, QString(" findmissing : %1 %2 %3 %4 %5").arg(ret[0]).arg(ret[1]).arg(ret[2]).arg(ret[3]).arg(ret[4]));
     551                checkAndDownload(ret[4]);
     552            }
     553        }
     554        return true;
     555    }
     556}
     557
     558bool ImportIconsWizard::submit(const QString& strParam)
     559{
     560    QString strParam1 = strParam;
     561    QUrl::encode(strParam1);
     562    QUrl url(ImportIconsWizard::url+"/submit");
     563
     564    QString str = wget(url,"csv="+strParam1);
     565    if (str.isEmpty() || str.startsWith("#") || str.startsWith("Error",false))
     566    {
     567        VERBOSE(VB_IMPORTANT, QString("Error from submit : %1").arg(str));
     568        return false;
     569    }
     570    else
     571    {
     572        VERBOSE(VB_IMPORTANT, QString("Working submit : %1").arg(str));
     573        QStringList strSplit=QStringList::split("\n",str);
     574        unsigned atsc =0, dvb =0, callsign =0, tv =0, xmltvid=0;
     575        for (QStringList::iterator begin=strSplit.begin();
     576             begin!=strSplit.end();begin++)
     577        {
     578            if (*begin != "#" )
     579            {
     580                QStringList strSplit2=QStringList::split(":",*begin);
     581                QString s=strSplit2[0].stripWhiteSpace();
     582                if (s=="a")
     583                     atsc=strSplit2[1].toUInt();
     584                else if (s=="c")
     585                     callsign=strSplit2[1].toUInt();
     586                else if (s=="d")
     587                     dvb=strSplit2[1].toUInt();
     588                else if (s=="t")
     589                     tv=strSplit2[1].toUInt();
     590                else if (s=="x")
     591                     xmltvid=strSplit2[1].toUInt();
     592            }
     593        }
     594        VERBOSE(VB_IMPORTANT, QString("working submit : atsc=%1 callsign=%2 dvb=%3 tv=%4 xmltvid=%5").arg(atsc).arg(callsign).arg(dvb).arg(tv).arg(xmltvid));
     595        return true;
     596    }
     597}
     598
     599void ImportIconsWizard::cancelPressed()
     600{
     601    m_cancelled=true;
     602}