Ticket #11019: 0002-Cache-Channelicons-in-local-folder-026.patch

File 0002-Cache-Channelicons-in-local-folder-026.patch, 9.5 KB (added by Shinck@…, 12 years ago)

Patch for master / 0.26-rc

  • mythtv/libs/libmythtv/dbchannelinfo.cpp

    From 3074732c8da922ca8fa6426064a62d4a05aae8a6 Mon Sep 17 00:00:00 2001
    From: Sascha Hinck <SHinck@web.de>
    Date: Sat, 25 Aug 2012 18:21:05 +0200
    Subject: [PATCH] Cache channelicons in local folder on remote frontends
    
    If channelicons are not in local folder $(ConfDIR)/channels they will be
    fetched from backend StorageGroup ChannelIcons an cached in local folder
    on first showing in the program guide.
    ---
     mythtv/libs/libmythtv/dbchannelinfo.cpp        |   82 ++++++++++++++++++++++++
     mythtv/libs/libmythtv/dbchannelinfo.h          |    3 +
     mythtv/libs/libmythtv/osd.cpp                  |   15 +++--
     mythtv/programs/mythfilldatabase/icondata.cpp  |    2 +-
     mythtv/programs/mythfrontend/guidegrid.cpp     |   20 +++---
     mythtv/programs/mythtv-setup/channeleditor.cpp |   14 ++++-
     mythtv/programs/mythtv-setup/importicons.cpp   |    2 +-
     7 files changed, 118 insertions(+), 20 deletions(-)
    
    diff --git a/mythtv/libs/libmythtv/dbchannelinfo.cpp b/mythtv/libs/libmythtv/dbchannelinfo.cpp
    index 2e9086a..b0caa68 100644
    a b QString DBChannel::GetFormatted(const ChannelFormat &format) const 
    102102////////////////////////////////////////////////////////////////////////////
    103103////////////////////////////////////////////////////////////////////////////
    104104
     105bool DBChannel::CacheChannelIcon(void)
     106{
     107    if (icon.isEmpty())
     108        return false;
     109
     110    m_localIcon = icon;
     111
     112    // Is icon local?
     113    if (QFile(icon).exists())
     114        return true;
     115
     116    QString localDirStr = QString("%1/channels").arg(GetConfDir());
     117    QDir localDir(localDirStr);
     118
     119    if (!localDir.exists() && !localDir.mkdir(localDirStr))
     120    {
     121        LOG(VB_GENERAL, LOG_ERR,
     122            QString("Icons directory is missing and could not be created: %1")
     123                .arg(localDirStr));
     124        icon.clear();
     125        return false;
     126    }
     127
     128    // Has it been saved to the local cache?
     129    m_localIcon = QString("%1/%2").arg(localDirStr)
     130                                 .arg(QFileInfo(icon).fileName());
     131    if (QFile(m_localIcon).exists())
     132        return true;
     133
     134    // Get address of master backed
     135    QString url = gCoreContext->GetMasterHostPrefix("ChannelIcons",
     136                                                                                icon);
     137    if (url.length() < 1)
     138    {
     139        icon.clear();
     140        return false;
     141    }
     142
     143    QUrl qurl = url;
     144    if (qurl.host().isEmpty())
     145    {
     146        icon.clear();
     147        return false;
     148    }
     149
     150    RemoteFile *rf = new RemoteFile(url, false, false, 0);
     151
     152    QByteArray data;
     153    bool ret = rf->SaveAs(data);
     154
     155    delete rf;
     156
     157    if (ret && data.size())
     158    {
     159        QImage image;
     160
     161        image.loadFromData(data);
     162
     163        //if (image.loadFromData(data) && image.width() > 0
     164
     165        if (image.save(m_localIcon))
     166        {
     167            LOG(VB_GENERAL, LOG_INFO,
     168                QString("Caching channel icon %1").arg(m_localIcon));
     169            return true;
     170        }
     171        else
     172            LOG(VB_GENERAL, LOG_ERR,
     173                QString("Failed to save to %1").arg(m_localIcon));
     174    }
     175
     176    // if we get here then the icon is set in the db but couldn't be found
     177    // anywhere so maybe we should remove it from the DB?
     178    icon.clear();
     179
     180    return false;
     181}
     182
     183////////////////////////////////////////////////////////////////////////////
     184////////////////////////////////////////////////////////////////////////////
     185
     186
    105187QString ChannelInfo::GetFormatted(const ChannelFormat &format) const
    106188{
    107189    QString tmp;
  • mythtv/libs/libmythtv/dbchannelinfo.h

    diff --git a/mythtv/libs/libmythtv/dbchannelinfo.h b/mythtv/libs/libmythtv/dbchannelinfo.h
    index 3e4b6ea..991b8b2 100644
    a b class MTV_PUBLIC DBChannel 
    3333    bool operator == (uint _chanid) const
    3434        { return chanid == _chanid; }
    3535
     36    bool CacheChannelIcon(void);
     37
    3638    enum ChannelFormat { kChannelShort, kChannelLong };
    3739    QString GetFormatted(const ChannelFormat &format) const;
    3840    void ToMap(InfoMap &infoMap) const;
    class MTV_PUBLIC DBChannel 
    4244    QString callsign;
    4345    QString name;
    4446    QString icon;
     47    QString m_localIcon;
    4548    uint    chanid;
    4649    uint    major_chan;
    4750    uint    minor_chan;
  • mythtv/libs/libmythtv/osd.cpp

    diff --git a/mythtv/libs/libmythtv/osd.cpp b/mythtv/libs/libmythtv/osd.cpp
    index 5703ed3..694d18f 100644
    a b  
    44// libmyth
    55#include "mythlogging.h"
    66
     7// libmythbase
     8#include "mythdirs.h"
     9
    710// libmythui
    811#include "mythmainwindow.h"
    912#include "mythuihelper.h"
    void OSD::SetText(const QString &window, QHash<QString,QString> &map, 
    436439                iconpath = map["iconpath"];
    437440            else
    438441                iconpath = ChannelUtil::GetIcon(chanid);
     442               
     443                QString localIcon = QString("%1/channels/%2").arg(GetConfDir())
     444                                                .arg(QFileInfo(iconpath).fileName());
    439445
    440             if (!iconpath.isEmpty())
    441             {
    442                 QString iconurl =
    443                                 gCoreContext->GetMasterHostPrefix("ChannelIcons",
    444                                                                   iconpath);
    445446
    446                 icon->SetFilename(iconurl);
     447            if (QFile(localIcon).exists())
     448            {
     449                icon->SetFilename(localIcon);
    447450                icon->Load(false);
    448451            }
    449452        }
  • mythtv/programs/mythfilldatabase/icondata.cpp

    diff --git a/mythtv/programs/mythfilldatabase/icondata.cpp b/mythtv/programs/mythfilldatabase/icondata.cpp
    index 95e44d9..1e47ee3 100644
    a b bool IconData::Save(const FI &fi, const QByteArray &data) 
    316316        "UPDATE channel SET icon = :ICON "
    317317        "WHERE chanid = :CHANID");
    318318
    319     update.bindValue(":ICON",     fi.filename);
     319    update.bindValue(":ICON", QFileInfo(fi.filename).fileName());
    320320    update.bindValue(":CHANID",   fi.chanid);
    321321
    322322    if (!update.exec())
  • mythtv/programs/mythfrontend/guidegrid.cpp

    diff --git a/mythtv/programs/mythfrontend/guidegrid.cpp b/mythtv/programs/mythfrontend/guidegrid.cpp
    index 24e1956..1990c4e 100644
    a b void GuideGrid::updateChannels(void) 
    15761576            chinfo->ToMap(infomap);
    15771577            item->SetTextFromMap(infomap);
    15781578
    1579             if (!chinfo->icon.isEmpty())
     1579            if (!chinfo->icon.isEmpty() &&
     1580                chinfo->CacheChannelIcon())
    15801581            {
    1581                 QString iconurl =
    1582                                 gCoreContext->GetMasterHostPrefix("ChannelIcons",
    1583                                                                   chinfo->icon);
    1584                 item->SetImage(iconurl, "channelicon");
     1582                QString localpath = chinfo->m_localIcon;
     1583                item->SetImage(localpath, "channelicon");
    15851584            }
    15861585        }
    15871586    }
    void GuideGrid::updateInfo(void) 
    16131612        m_channelImage->Reset();
    16141613        if (!chinfo->icon.isEmpty())
    16151614        {
    1616             QString iconurl = gCoreContext->GetMasterHostPrefix("ChannelIcons",
    1617                                                                 chinfo->icon);
    1618 
    1619             m_channelImage->SetFilename(iconurl);
    1620             m_channelImage->Load();
     1615            if (chinfo->CacheChannelIcon())
     1616            {
     1617                QString localpath = chinfo->m_localIcon;
     1618                m_channelImage->SetFilename(localpath);
     1619                m_channelImage->Load();
     1620            }
    16211621        }
    16221622    }
    16231623
  • mythtv/programs/mythtv-setup/channeleditor.cpp

    diff --git a/mythtv/programs/mythtv-setup/channeleditor.cpp b/mythtv/programs/mythtv-setup/channeleditor.cpp
    index f93c03b..9ea11db 100644
    a b  
    44// MythTV
    55#include "mythprogressdialog.h"
    66#include "mythuibuttonlist.h"
     7#include "mythcorecontext.h"
    78#include "channelsettings.h"
    89#include "transporteditor.h"
    910#include "mythuicheckbox.h"
    void ChannelEditor::itemChanged(MythUIButtonListItem *item) 
    210211        QString iconpath = item->GetImageFilename();
    211212        if (!iconpath.isEmpty())
    212213        {
    213             m_preview->SetFilename(iconpath);
     214            QString iconurl = gCoreContext->GetMasterHostPrefix("ChannelIcons",
     215                                                               iconpath);
     216
     217            m_preview->SetFilename(iconurl);
    214218            m_preview->Load();
    215219        }
    216220    }
    void ChannelEditor::fillList(void) 
    336340            item->SetText(callsign, "callsign");
    337341            item->SetText(sourceid, "sourcename");
    338342            item->SetImage(icon);
    339             item->SetImage(icon, "icon");
     343            if (!icon.isEmpty())
     344            {
     345                QString iconurl =
     346                                gCoreContext->GetMasterHostPrefix("ChannelIcons",
     347                                                                        icon);
     348                item->SetImage(iconurl, "icon");
     349            }
    340350            item->DisplayState(state, "status");
    341351        }
    342352    }
  • mythtv/programs/mythtv-setup/importicons.cpp

    diff --git a/mythtv/programs/mythtv-setup/importicons.cpp b/mythtv/programs/mythtv-setup/importicons.cpp
    index 4fe041b..3cce488 100644
    a b bool ImportIconsWizard::checkAndDownload(const QString& url, const QString& loca 
    553553                        "WHERE chanid = :CHANID";
    554554
    555555        query.prepare(qstr);
    556         query.bindValue(":ICON", file.absoluteFilePath());
     556        query.bindValue(":ICON", file.fileName());
    557557        query.bindValue(":CHANID", localChanId);
    558558
    559559        if (!query.exec())