From f52fdb1b4a8dcf4139751c24696d90b0fb542a19 Mon Sep 17 00:00:00 2001 From: Timothy Pearson Date: Sat, 27 Sep 2014 18:45:46 -0500 Subject: [PATCH] Add fast mimetype accessor for use by Konqueror when checking for media device icons This relates to Bug 699 (cherry picked from commit a21cb20628b557c569f3066aa4d4ad4bedc9959c) --- kio/kio/kfileitem.cpp | 80 ++++++++++++++++++++++++++++++++++--------- kio/kio/kfileitem.h | 8 +++++ 2 files changed, 71 insertions(+), 17 deletions(-) diff --git a/kio/kio/kfileitem.cpp b/kio/kio/kfileitem.cpp index 010832573..5504ca079 100644 --- a/kio/kio/kfileitem.cpp +++ b/kio/kio/kfileitem.cpp @@ -47,8 +47,13 @@ #include class KFileItem::KFileItemPrivate { + public: + KFileItemPrivate() : commentCached(false) {} + public: TQString iconName; + TQString comment; + bool commentCached; }; KFileItem::KFileItem( const KIO::UDSEntry& _entry, const KURL& _url, @@ -239,8 +244,9 @@ void KFileItem::readUDSEntry( bool _urlIsDirectory ) break; case KIO::UDS_ICON_NAME: - if ( !d ) + if ( !d ) { d = new KFileItemPrivate(); + } d->iconName = (*it).m_str; break; @@ -279,6 +285,11 @@ void KFileItem::refresh() void KFileItem::refreshMimeType() { + if ( d ) { + d->iconName = TQString::null; + d->comment = TQString::null; + d->commentCached = false; + } m_pMimeType = 0L; init( false ); // Will determine the mimetype } @@ -510,6 +521,16 @@ TQString KFileItem::mimetype() const return that->determineMimeType()->name(); } +TQString KFileItem::mimetypeFast() const +{ + if (isMimeTypeKnown()) { + return mimetype(); + } + else { + return m_pMimeType->name(); + } +} + KMimeType::Ptr KFileItem::determineMimeType() { if ( !m_pMimeType || !m_bMimeTypeKnown ) @@ -535,17 +556,28 @@ bool KFileItem::isMimeTypeKnown() const TQString KFileItem::mimeComment() { - KMimeType::Ptr mType = determineMimeType(); - - bool isLocalURL; - KURL url = mostLocalURL(isLocalURL); + if (d && (d->commentCached)) return d->comment; + + KMimeType::Ptr mType = determineMimeType(); + + bool isLocalURL; + KURL url = mostLocalURL(isLocalURL); + + TQString comment = mType->comment( url, isLocalURL ); + //kdDebug() << "finding comment for " << url.url() << " : " << m_pMimeType->name() << endl; + if ( !d ) { + d = new KFileItemPrivate(); + } + if (!comment.isEmpty()) { + d->comment = comment; + d->commentCached = true; + } + else { + d->comment = mType->name(); + d->commentCached = true; + } - TQString comment = mType->comment( url, isLocalURL ); - //kdDebug() << "finding comment for " << url.url() << " : " << m_pMimeType->name() << endl; - if (!comment.isEmpty()) - return comment; - else - return mType->name(); + return d->comment; } TQString KFileItem::iconName() @@ -556,7 +588,11 @@ TQString KFileItem::iconName() KURL url = mostLocalURL(isLocalURL); //kdDebug() << "finding icon for " << url.url() << " : " << m_pMimeType->name() << endl; - return determineMimeType()->icon(url, isLocalURL); + if ( !d ) { + d = new KFileItemPrivate(); + } + d->iconName = determineMimeType()->icon(url, isLocalURL); + return d->iconName; } int KFileItem::overlays() const @@ -745,12 +781,18 @@ TQString KFileItem::getStatusBarInfo() if ( m_bLink ) { - TQString comment = determineMimeType()->comment( m_url, m_bIsLocalURL ); + if ( !d ) { + d = new KFileItemPrivate(); + } + if (!d->commentCached) { + d->comment = determineMimeType()->comment( m_url, m_bIsLocalURL ); + d->commentCached = true; + } TQString tmp; - if ( comment.isEmpty() ) + if ( d->comment.isEmpty() ) tmp = i18n ( "Symbolic Link" ); else - tmp = i18n("%1 (Link)").arg(comment); + tmp = i18n("%1 (Link)").arg(d->comment); text += "->"; text += linkDest(); text += " "; @@ -921,8 +963,9 @@ void KFileItem::assign( const KFileItem & item ) determineMimeType(); if ( item.d ) { - if ( !d ) + if ( !d ) { d = new KFileItemPrivate; + } d->iconName = item.d->iconName; } else { delete d; @@ -951,8 +994,11 @@ void KFileItem::setUDSEntry( const KIO::UDSEntry& _entry, const KURL& _url, m_guessedMimeType = TQString::null; m_metaInfo = KFileMetaInfo(); - if ( d ) + if ( d ) { d->iconName = TQString::null; + d->comment = TQString::null; + d->commentCached = false; + } readUDSEntry( _urlIsDirectory ); init( _determineMimeTypeOnDemand ); diff --git a/kio/kio/kfileitem.h b/kio/kio/kfileitem.h index cf6344669..2fdcd77ee 100644 --- a/kio/kio/kfileitem.h +++ b/kio/kio/kfileitem.h @@ -312,6 +312,14 @@ public: */ TQString mimetype() const; + /** + * Returns the mimetype of the file item. + * If @p _determineMimeTypeOnDemand was used in the constructor, this will return + * the mimetype as fast as possible at the expense of accuracy. + * @return the fast mime type of the file + */ + TQString mimetypeFast() const; + /** * Returns the mimetype of the file item. * If _determineMimeTypeOnDemand was used in the constructor, this will determine