Ignore:
Timestamp:
Nov 16, 2009, 1:06:34 AM (16 years ago)
Author:
Dmitry A. Kuminov
Message:

gui/kernel: More QPMMime work.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/gui/kernel/qmime_pm.cpp

    r321 r323  
    6060#include "qdir.h"
    6161
     62#define QMIME_DEBUG
     63
    6264QT_BEGIN_NAMESPACE
     65
     66class QPMMimeList
     67{
     68public:
     69    QPMMimeList();
     70    ~QPMMimeList();
     71    void addMime(QPMMime *mime);
     72    void removeMime(QPMMime *mime);
     73    QList<QPMMime*> mimes();
     74
     75private:
     76    void init();
     77    bool initialized;
     78    QList<QPMMime*> list;
     79};
     80
     81Q_GLOBAL_STATIC(QPMMimeList, theMimeList);
     82
    6383
    6484/*!
     
    99119*/
    100120
    101 // @todo lots of stuff...
    102 
     121/*!
     122Constructs a new conversion object, adding it to the globally accessed
     123list of available converters.
     124*/
    103125QPMMime::QPMMime()
    104126{
    105 }
    106 
     127    theMimeList()->addMime(this);
     128}
     129
     130/*!
     131Destroys a conversion object, removing it from the global
     132list of available converters.
     133*/
    107134QPMMime::~QPMMime()
    108135{
     136    theMimeList()->removeMime(this);
     137}
     138
     139/*!
     140    Registers the MIME type \a mime, and returns an ID number
     141    identifying the format on OS/2.
     142*/
     143ULONG QPMMime::registerMimeType(const QString &mime)
     144{
     145    QString atom = QLatin1String("mime:") + mime;
     146
     147    ULONG cf = WinAddAtom(WinQuerySystemAtomTable(), atom.toLocal8Bit());
     148    if (!cf) {
     149#ifndef QT_NO_DEBUG
     150        qWarning("QPMMime: WinAddAtom failed with %lX",
     151                 WinGetLastError(NULLHANDLE));
     152#endif
     153        return 0;
     154    }
     155
     156    return cf;
     157}
     158
     159/*!
     160    \fn bool QPMMime::canConvertFromMime(ULONG format, const QMimeData *mimeData) const
     161
     162    Returns true if the converter can convert from the \a mimeData to
     163    the specified \a format.
     164
     165    All subclasses must reimplement this pure virtual function.
     166*/
     167
     168/*!
     169    \fn bool QPMMime::convertFromMime(ULONG format, const QMimeData *mimeData,
     170                                      ULONG &flags, ULONG &data) const
     171
     172    Convert the \a mimeData to the specified \a format.
     173    The converted data should then be placed in the \a data variable with the
     174    necessary flags returned in the \a flags variable.
     175
     176    Return true if the conversion was successful.
     177
     178    All subclasses must reimplement this pure virtual function.
     179*/
     180
     181/*!
     182    \fn QVector<ULONG> QPMMime::formatsForMime(const QString &mimeType, const QMimeData *mimeData) const
     183
     184    Returns a QVector of ULONG values representing the different OS/2 PM clipboard
     185    formats that can be provided for the \a mimeType from the \a mimeData.
     186
     187    All subclasses must reimplement this pure virtual function.
     188*/
     189
     190/*!
     191    \fn bool QPMMime::canConvertToMime(const QString &mimeType, ULONG format) const
     192
     193    Returns true if the converter can convert to the \a mimeType from
     194    the specified \a format.
     195
     196    All subclasses must reimplement this pure virtual function.
     197*/
     198
     199/*!
     200    \fn QVariant QPMMime::convertToMime(const QString &mimeType, QVariant::Type preferredType,
     201                                        ULONG format, ULONG flags, ULONG data) const
     202
     203    Returns a QVariant containing the converted data for \a mimeType from the
     204    \a data in the specified \a format and \a flags. If possible the QVariant
     205    should be of the \a preferredType to avoid needless conversions.
     206
     207    All subclasses must reimplement this pure virtual function.
     208*/
     209
     210/*!
     211    \fn QString QPMMime::mimeForFormat(ULONG format) const
     212
     213    Returns the mime type that will be created form the specified \a format, or an
     214    empty string if this converter does not support \a format.
     215
     216    All subclasses must reimplement this pure virtual function.
     217*/
     218
     219// static
     220QPMMime *QPMMime::converterToMime(const QString &mimeType, ULONG format)
     221{
     222    QList<QPMMime*> mimes = theMimeList()->mimes();
     223    for (int i = mimes.size()-1; i >= 0; --i) {
     224        if (mimes.at(i)->canConvertToMime(mimeType, format))
     225            return mimes.at(i);
     226    }
     227    return 0;
     228}
     229
     230// static
     231QStringList QPMMime::allMimesForFormats(const QVector<ULONG> &formats)
     232{
     233    QList<QPMMime*> mimes = theMimeList()->mimes();
     234    QStringList fmts;
     235
     236    foreach(ULONG cf, formats) {
     237#ifdef QMIME_DEBUG
     238        HATOMTBL tbl = WinQuerySystemAtomTable();
     239        ULONG len = WinQueryAtomLength(tbl, cf);
     240        QByteArray atom(len, 0);
     241        WinQueryAtomName(tbl, cf, atom.data(), atom.size() + 1 /* '\0' */);
     242        qDebug("QPMMime::allMimesForFormats() CF %lu (%s)", cf, atom.constData());
     243#endif
     244        for (int i = mimes.size()-1; i >= 0; --i) {
     245            QString format = mimes.at(i)->mimeForFormat(cf);
     246            if (!format.isEmpty() && !fmts.contains(format)) {
     247                fmts += format;
     248            }
     249        }
     250    }
     251
     252    return fmts;
     253}
     254
     255// static
     256QPMMime *QPMMime::converterFromMime(ULONG format, const QMimeData *mimeData)
     257{
     258    QList<QPMMime*> mimes = theMimeList()->mimes();
     259    for (int i = mimes.size()-1; i >= 0; --i) {
     260        if (mimes.at(i)->canConvertFromMime(format, mimeData))
     261            return mimes.at(i);
     262    }
     263    return 0;
     264}
     265
     266// static
     267QVector<ULONG> QPMMime::allFormatsForMime(const QMimeData *mimeData)
     268{
     269    QList<QPMMime*> mimes = theMimeList()->mimes();
     270    QVector<ULONG> cfs(4);
     271    QStringList formats = QInternalMimeData::formatsHelper(mimeData);
     272    for (int f = 0; f < formats.size(); ++f) {
     273        for (int i = mimes.size()-1; i >= 0; --i)
     274            cfs += mimes.at(i)->formatsForMime(formats.at(f), mimeData);
     275    }
     276    return cfs;
     277}
     278
     279////////////////////////////////////////////////////////////////////////////////
     280
     281////////////////////////////////////////////////////////////////////////////////
     282
     283QPMMimeList::QPMMimeList()
     284    : initialized(false)
     285{
     286}
     287
     288QPMMimeList::~QPMMimeList()
     289{
     290    while (list.size())
     291        delete list.first();
     292}
     293
     294
     295void QPMMimeList::init()
     296{
     297    if (!initialized) {
     298        initialized = true;
     299        // @todo new QPMMimeXXX;
     300    }
     301}
     302
     303void QPMMimeList::addMime(QPMMime *mime)
     304{
     305    init();
     306    list.append(mime);
     307}
     308
     309void QPMMimeList::removeMime(QPMMime *mime)
     310{
     311    init();
     312    list.removeAll(mime);
     313}
     314
     315QList<QPMMime*> QPMMimeList::mimes()
     316{
     317    init();
     318    return list;
    109319}
    110320
Note: See TracChangeset for help on using the changeset viewer.