Ignore:
Timestamp:
Nov 20, 2009, 7:39:03 PM (16 years ago)
Author:
Dmitry A. Kuminov
Message:

gui/kernel: mime: Implemented mime<-clipboard interface for "text/plain" (getting from the system clipboard).

File:
1 edited

Legend:

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

    r332 r334  
    246246
    247247/*!
    248     \fn QStringList QPMMime::mimesForFormats(const QList<ULONG> &formats) const
     248    \fn QList<MimeCFPair> QPMMime::mimesForFormats(const QList<ULONG> &formats) const
    249249
    250250    Returns a list of mime types that will be created form the specified \a list
    251251    of \a formats, in order of precedence (the most suitable mime type comes
    252252    first), or an empty list if neither of the \a formats is supported by this
    253     converter.
     253    converter. Note that each pair in the returned list consists of the mime
     254    type name and the corresponding format identifier.
    254255
    255256    All subclasses must reimplement this pure virtual function.
     
    276277    QList<QPMMime*> mimes = theMimeList()->mimes();
    277278    for (int i = mimes.size()-1; i >= 0; --i) {
    278         QStringList fmts = mimes[i]->mimesForFormats(formats);
     279        QList<MimeCFPair> fmts = mimes[i]->mimesForFormats(formats);
    279280        int priority = 0;
    280         foreach (QString fmt, fmts) {
     281        foreach (MimeCFPair fmt, fmts) {
    281282            ++priority;
    282283            QList<Match>::iterator it = matches.begin();
    283284            for (; it != matches.end(); ++it) {
    284285                Match &match = *it;
    285                 if (match.mime == fmt) {
     286                if (match.mime == fmt.first) {
    286287                    // replace if priority is higher, ignore otherwise
    287288                    if (priority < match.priority) {
    288289                        match.converter = mimes[i];
     290                        match.format = fmt.second;
    289291                        match.priority = priority;
    290292                    }
     
    293295            }
    294296            if (it == matches.end()) {
    295                 matches += Match(mimes[i], fmt, priority);
     297                matches += Match(mimes[i], fmt.first, fmt.second, priority);
    296298            }
    297299        }
     
    344346                             ULONG &flags, ULONG *data) const;
    345347
    346     QStringList mimesForFormats(const QList<ULONG> &formats) const;
     348    QList<MimeCFPair> mimesForFormats(const QList<ULONG> &formats) const;
    347349    QVariant convertFromFormat(ULONG format, ULONG flags, ULONG data,
    348350                               const QString &mimeType,
     
    388390        int maxsize = str.size()+str.size()/40+3;
    389391        r.fill('\0', maxsize);
    390         char* o = r.data();
    391         const char* d = str.data();
     392        char *o = r.data();
     393        const char *d = str.data();
    392394        const int s = str.size();
    393395        bool cr = false;
     
    454456}
    455457
    456 QStringList QPMMimeText::mimesForFormats(const QList<ULONG> &formats) const
    457 {
    458     QStringList mimes;
     458QList<QPMMime::MimeCFPair> QPMMimeText::mimesForFormats(const QList<ULONG> &formats) const
     459{
     460    QList<MimeCFPair> mimes;
    459461    foreach(ULONG cf, formats) {
    460         if (cf == CF_TEXT || cf == CF_TextUnicode){
    461             mimes << QLatin1String("text/plain");
    462             break;
    463         }
     462        // prefer unicode over local8Bit
     463        if (cf == CF_TextUnicode)
     464            mimes.prepend(qMakePair(QString(QLatin1String("text/plain")), cf));
     465        if (cf == CF_TEXT)
     466            mimes.append(qMakePair(QString(QLatin1String("text/plain")), cf));
    464467    }
    465468    return mimes;
     
    470473                                        QVariant::Type preferredType) const
    471474{
    472     return QVariant();
     475    QVariant ret;
     476
     477    // @todo why is it startsWith? the rest of the mime specification (encoding,
     478    // etc) isn't taken into account... Anyway, copied the logic from Windows.
     479    if (!mimeType.startsWith("text/plain"))
     480        return ret;
     481    if (!(flags & CFI_POINTER) || !data)
     482        return ret;
     483
     484    QString str;
     485
     486    if (format == CF_TEXT) {
     487        const char *d = (const char *)data;
     488        QByteArray r("");
     489        if (*d) {
     490            const int s = qstrlen(d);
     491            r.fill('\0', s);
     492            char *o = r.data();
     493            int j = 0;
     494            for (int i = 0; i < s; i++) {
     495                char c = d[i];
     496                if (c != '\r')
     497                    o[j++] = c;
     498            }
     499        }
     500        str = QString::fromLocal8Bit(r);
     501    } else if (format == CF_TextUnicode) {
     502        str = QString::fromUtf16((const unsigned short *)data);
     503        str.replace(QLatin1String("\r\n"), QLatin1String("\n"));
     504    }
     505
     506    if (preferredType == QVariant::String)
     507        ret = str;
     508    else
     509        ret = str.toUtf8();
     510
     511    return ret;
    473512}
    474513
Note: See TracChangeset for help on using the changeset viewer.