Ticket #303: extensions-mythtv.diff

File extensions-mythtv.diff, 9.8 KB (added by leo weppelman, 19 years ago)

extend REG_MEDIA_HANDLER to handle extensions (mythtv-part)

  • libs/libmyth/mythmedia.cpp

     
    77
    88// Qt Headers
    99#include <qfile.h>
     10#include <qdir.h>
    1011
    1112// MythTV headers
    1213#include "mythmedia.h"
     
    3839    "MEDIATYPE_MIXED",
    3940    "MEDIATYPE_AUDIO",
    4041    "MEDIATYPE_DVD",
    41     "MEDIATYPE_VCD"
     42    "MEDIATYPE_VCD",
     43    "MEDIATYPE_MMUSIC",
     44    "MEDIATYPE_MVIDEO",
     45    "MEDIATYPE_MGALLERY"
    4246};
    4347
    4448const char* MythMediaDevice::MediaErrorStrings[] =
     
    109113            {
    110114                m_Status = MEDIASTAT_MOUNTED;
    111115                onDeviceMounted();
     116                DetectMediaType();
    112117            }
    113118            else
    114119                onDeviceUnmounted();
     
    122127    }
    123128    else
    124129    {
    125         VERBOSE( VB_ALL,  "Disk iserted on a supermount device" );
     130        VERBOSE( VB_ALL,  "Disk inserted on a supermount device" );
    126131        // If it's a super mount then the OS will handle mounting /  unmounting.
    127132        // We just need to give derived classes a chance to perform their
    128133        // mount / unmount logic.
    129134        if (DoMount)
     135        {
    130136            onDeviceMounted();
     137            DetectMediaType();
     138        }
    131139        else
    132140            onDeviceUnmounted();
    133141        return true;
     
    243251    return m_Status;
    244252}
    245253
     254
     255//
     256// Try to make an educated guess about the type of data present on the Media.
     257// This fuction changes m_MediaType accordingly.
     258//
     259void MythMediaDevice::DetectMediaType(void)
     260{
     261
     262    ScanMediaType(m_MountPath);
     263    if (ExtensionArray.isEmpty ())
     264    {
     265        VERBOSE(VB_GENERAL,
     266           QString("No files with extension found in '%1'").arg (m_MountPath));
     267    } else {
     268        int  score = 0, highscore = 0, type = 0;
     269
     270        ExtensionMap *em;
     271        for (em = ExtensionMapList.first(); em; em = ExtensionMapList.next())
     272        {
     273            QStringList::Iterator it;
     274            for (it = em->Extensions.begin(); it != em->Extensions.end(); ++it)
     275            {
     276                if (ExtensionArray.contains(*it))
     277                    score += ExtensionArray[*it];
     278            }
     279            VERBOSE(VB_ALL, QString("Looking for: '%1', Score: %2")
     280                   .arg(MediaTypeStrings[ffs(em->Mediatype)])
     281                   .arg(score));
     282            if (score > highscore)
     283                type = em->Mediatype; highscore = score;
     284        }
     285        if (type != 0)
     286            m_MediaType = (MediaType)type;
     287    }
     288    ExtensionArray.clear();
     289}
     290
     291//
     292// Recursively scan directories and create an associative array with
     293// key: 'tolower(extension)' and value the number of times we've
     294// seen it.
     295//
     296void MythMediaDevice::ScanMediaType(const QString& directory)
     297{
     298    QDir d(directory);
     299
     300    d.setSorting(QDir::DirsFirst | QDir::Name | QDir::IgnoreCase);
     301    if (!d.exists())
     302            return;
     303
     304    const QFileInfoList *list = d.entryInfoList();
     305    if (!list)                       
     306        return;
     307
     308    QFileInfoListIterator it(*list);
     309    QFileInfo *fi;
     310
     311    while ((fi = it.current()) != 0)
     312    {
     313        ++it;
     314        if (fi->fileName() == "." || fi->fileName() == ".." )
     315                continue;
     316        if (!fi->isDir())
     317        {
     318            QString extension = fi->extension(false);
     319            if (!extension.isEmpty())
     320            {
     321                extension = extension.lower();
     322                if (ExtensionArray.contains(extension))
     323                    ExtensionArray[extension]++;
     324                else
     325                    ExtensionArray[extension] = 1;
     326            }
     327        }
     328        else ScanMediaType(fi->absFilePath());
     329    }
     330}
     331
     332//
     333// Allow Plugin's to register their supported MediaType and the file
     334// extensions that map to it.
     335// The extensions argument is a comma separated list of extensions like:
     336//    'mp3,ogg,flac'.
     337//
     338void MythMediaDevice::MediaRegisterExtensions(const int mediatype, const QString& extensions)
     339{
     340    ExtensionMap *eMap = new ExtensionMap;
     341    eMap->Mediatype  = mediatype;
     342    eMap->Extensions = QStringList::split(",", extensions, "");
     343
     344    if (eMap->Extensions.count() == 0)
     345    {
     346        cerr << "Empty setting for entry: " << mediatype << endl;
     347        delete eMap;
     348        return;
     349    }
     350    ExtensionMapList.append(eMap);
     351}
  • libs/libmyth/mythmediamonitor.h

     
    5757    void checkDevices(void);
    5858    void startMonitoring(void);
    5959    void stopMonitoring(void);
     60    void MonitorRegisterExtensions(int mediaType, const QString &extensions);
    6061    QValueList <MythMediaDevice*> getMedias(MediaType mediatype);
    6162   
    6263    static MediaMonitor* getMediaMonitor();
  • libs/libmyth/mythdialogs.cpp

     
    660660                                          const QString &description,
    661661                                          const QString &key,
    662662                              void (*callback)(MythMediaDevice*mediadevice),
    663                                           int mediaType)
     663                                          int mediaType,
     664                                          const QString &extensions)
    664665{
    665666    (void)key;
    666667
     
    672673                                   .arg(destination));
    673674
    674675        d->mediaHandlerMap[destination] = mhd;
     676        if (extensions != NULL)
     677        {
     678            MediaMonitor *mon = MediaMonitor::getMediaMonitor();
     679            if (mon == NULL)
     680            {
     681                cerr << "MythMainWindow::RegisterMediaHandler: " <<
     682                        "Cannot get to MediaMonitor" << endl;
     683            } else {
     684                mon->MonitorRegisterExtensions(mediaType, extensions);
     685            }
     686        }
    675687    }
    676688    else
    677689    {
  • libs/libmyth/mythmediamonitor.cpp

     
    306306    m_Thread.wait();
    307307}
    308308
     309//
     310// Register the extension list on all known devices
     311//
     312void MediaMonitor::MonitorRegisterExtensions(int mediaType,
     313                                             const QString &extensions)
     314{
     315    QValueList<MythMediaDevice*>::iterator itr = m_Devices.begin();
     316    MythMediaDevice* pDev;
     317    while (itr != m_Devices.end())
     318    {
     319        pDev = *itr;
     320        if (pDev)
     321            pDev->MediaRegisterExtensions(mediaType,extensions);
     322        itr++;
     323    }
     324
     325}
     326//
    309327// Ask for available media
    310328QValueList <MythMediaDevice*> MediaMonitor::getMedias(MediaType mediatype)
    311329{
  • libs/libmyth/mythdialogs.h

     
    6060
    6161#define REG_KEY(a, b, c, d) gContext->GetMainWindow()->RegisterKey(a, b, c, d)
    6262#define REG_JUMP(a, b, c, d) gContext->GetMainWindow()->RegisterJump(a, b, c, d)
    63 #define REG_MEDIA_HANDLER(a, b, c, d, e) gContext->GetMainWindow()->RegisterMediaHandler(a, b, c, d, e)
     63#define REG_MEDIA_HANDLER(a, b, c, d, e, f) gContext->GetMainWindow()->RegisterMediaHandler(a, b, c, d, e, f)
    6464#define REG_MEDIAPLAYER(a,b,c) gContext->GetMainWindow()->RegisterMediaPlugin(a, b, c)
    6565
    6666typedef  int (*MediaPlayCallback)(const char*, const char*, const char*, const char*, int, const char*);
     
    9898    void RegisterMediaHandler(const QString &destination,
    9999                              const QString &description, const QString &key,
    100100                              void (*callback)(MythMediaDevice* mediadevice),
    101                               int mediaType);
     101                              int mediaType,
     102                              const QString &extensions);
    102103
    103104    void RegisterMediaPlugin(const QString &name, const QString &desc,
    104105                             MediaPlayCallback fn);
  • libs/libmyth/mythmedia.h

     
    33
    44#include <qobject.h>
    55#include <qstring.h>
     6#include <qstringlist.h>
    67
    78typedef enum {
    89    MEDIASTAT_ERROR,
     
    1920    MEDIATYPE_MIXED=4,
    2021    MEDIATYPE_AUDIO=8,
    2122    MEDIATYPE_DVD=16,
    22     MEDIATYPE_VCD=32
     23    MEDIATYPE_VCD=32,
     24    MEDIATYPE_MMUSIC=64,
     25    MEDIATYPE_MVIDEO=128,
     26    MEDIATYPE_MGALLERY=256
    2327} MediaType;
    2428
    2529typedef enum {
     
    7377    bool mount() {  return performMountCmd(true); }
    7478    bool unmount() { return performMountCmd(false); }
    7579    bool isMounted(bool bVerify = false);
     80
     81    void MediaRegisterExtensions(const int mediatype,const QString& extensions);
     82
    7683   
    7784    static const char* MediaStatusStrings[];
    7885    static const char* MediaTypeStrings[];
     
    8693    virtual void onDeviceMounted() {};
    8794    virtual void onDeviceUnmounted() {};
    8895
     96    void DetectMediaType(void);
     97    void ScanMediaType(const QString& directory);
    8998    MediaStatus setStatus(MediaStatus newStat, bool CloseIt=false);
    9099
    91100    QString m_MountPath;        ///< The path to this media's mount point (i.e. /mnt/cdrom). Read only.
     
    99108    int m_DeviceHandle;         ///< A file handle for opening and closing the device.
    100109    MediaType m_MediaType;      ///< The type of media
    101110    bool m_SuperMount;          ///< Is this a supermount device?
     111    QMap<QString, int> ExtensionArray; ///< current tally of extensions on the disk
     112    typedef struct {
     113        int         Mediatype;
     114        QStringList Extensions;
     115    } ExtensionMap;
     116    QPtrList<ExtensionMap> ExtensionMapList; ///< Map extansions to a mediatype
     117
    102118};
    103119
    104120#endif