Branch: master

a21cb206 2014-09-27 18:45:46 Timothy Pearson
Add fast mimetype accessor for use by Konqueror when checking for media device icons
This relates to Bug 699
M tdeio/tdeio/tdefileitem.cpp
M tdeio/tdeio/tdefileitem.h
diff --git a/tdeio/tdeio/tdefileitem.cpp b/tdeio/tdeio/tdefileitem.cpp
index 16f37f0..0b3b020 100644
--- a/tdeio/tdeio/tdefileitem.cpp
+++ b/tdeio/tdeio/tdefileitem.cpp
@@ -57,7 +57,12 @@
 
 class KFileItem::KFileItemPrivate {
 	public:
+		KFileItemPrivate() : commentCached(false) {}
+
+	public:
 		TQString iconName;
+		TQString comment;
+		bool commentCached;
 };
 
 KFileItem::KFileItem( const TDEIO::UDSEntry& _entry, const KURL& _url,
@@ -255,8 +260,9 @@
         break;
 
       case TDEIO::UDS_ICON_NAME:
-        if ( !d )
+        if ( !d ) {
           d = new KFileItemPrivate();
+        }
         d->iconName = (*it).m_str;
         break;
 
@@ -296,6 +302,11 @@
 
 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
 }
@@ -543,6 +554,16 @@
   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 )
@@ -568,17 +589,28 @@
 
 TQString KFileItem::mimeComment()
 {
- KMimeType::Ptr mType = determineMimeType();
+	if (d && (d->commentCached)) return d->comment;
 
- bool isLocalURL;
- KURL url = mostLocalURL(isLocalURL);
+	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()
@@ -589,7 +621,11 @@
   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
@@ -855,12 +891,18 @@
 
   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 += "  ";
@@ -1031,8 +1073,9 @@
     determineMimeType();
 
     if ( item.d ) {
-        if ( !d )
+        if ( !d ) {
             d = new KFileItemPrivate;
+        }
         d->iconName = item.d->iconName;
     } else {
         delete d;
@@ -1061,8 +1104,11 @@
   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/tdeio/tdeio/tdefileitem.h b/tdeio/tdeio/tdefileitem.h
index eace44d..b6c1b0b 100644
--- a/tdeio/tdeio/tdefileitem.h
+++ b/tdeio/tdeio/tdefileitem.h
@@ -328,6 +328,14 @@
 
   /**
    * 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
    * the mimetype first.
    * @return the mime type