[Qt] Lookup mimetypes using QMimeDatabase
authorallan.jensen@digia.com <allan.jensen@digia.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 22 Nov 2012 13:23:28 +0000 (13:23 +0000)
committerallan.jensen@digia.com <allan.jensen@digia.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 22 Nov 2012 13:23:28 +0000 (13:23 +0000)
https://bugs.webkit.org/show_bug.cgi?id=102667

Reviewed by Simon Hausmann.

The Qt backend of MIMETypeRegistry now looks up using QMimeDatabase.

* loader/archive/ArchiveFactory.cpp:
(WebCore::archiveMIMETypes):
    Also map the preferred freedesktop mimetype for MIME archives to MIME archive constructor.
* platform/MIMETypeRegistry.cpp:
(WebCore::MIMETypeRegistry::getNormalizedMIMEType):
* platform/qt/MIMETypeRegistryQt.cpp:
(WebCore::MIMETypeRegistry::getMIMETypeForExtension):
(WebCore::MIMETypeRegistry::getMIMETypeForPath):
(WebCore::MIMETypeRegistry::getNormalizedMIMEType):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@135507 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/loader/archive/ArchiveFactory.cpp
Source/WebCore/platform/MIMETypeRegistry.cpp
Source/WebCore/platform/qt/MIMETypeRegistryQt.cpp

index 872ece9..82bade2 100644 (file)
@@ -1,3 +1,22 @@
+2012-11-22  Allan Sandfeld Jensen  <allan.jensen@digia.com>
+
+        [Qt] Lookup mimetypes using QMimeDatabase
+        https://bugs.webkit.org/show_bug.cgi?id=102667
+
+        Reviewed by Simon Hausmann.
+
+        The Qt backend of MIMETypeRegistry now looks up using QMimeDatabase.
+
+        * loader/archive/ArchiveFactory.cpp:
+        (WebCore::archiveMIMETypes):
+            Also map the preferred freedesktop mimetype for MIME archives to MIME archive constructor.
+        * platform/MIMETypeRegistry.cpp:
+        (WebCore::MIMETypeRegistry::getNormalizedMIMEType):
+        * platform/qt/MIMETypeRegistryQt.cpp:
+        (WebCore::MIMETypeRegistry::getMIMETypeForExtension):
+        (WebCore::MIMETypeRegistry::getMIMETypeForPath):
+        (WebCore::MIMETypeRegistry::getNormalizedMIMEType):
+
 2012-11-22  Cosmin Truta  <ctruta@rim.com>
 
         Move URL-checking code into Frame
index 04fb5ed..4ebb52b 100644 (file)
@@ -70,6 +70,8 @@ static ArchiveMIMETypesMap& archiveMIMETypes()
     mimeTypes.set("multipart/related", archiveFactoryCreate<MHTMLArchive>);
 #if PLATFORM(GTK)
     mimeTypes.set("message/rfc822", archiveFactoryCreate<MHTMLArchive>);
+#elif PLATFORM(QT)
+    mimeTypes.set("application/x-mimearchive", archiveFactoryCreate<MHTMLArchive>);
 #endif
 #endif
 
index 440b429..c5f3140 100644 (file)
@@ -491,6 +491,7 @@ String MIMETypeRegistry::getWellKnownMIMETypeForExtension(const String& extensio
     return findMimeType(commonMediaTypes, sizeof(commonMediaTypes) / sizeof(commonMediaTypes[0]), extension);
 }
 
+#if !PLATFORM(QT)
 String MIMETypeRegistry::getMIMETypeForPath(const String& path)
 {
     size_t pos = path.reverseFind('.');
@@ -500,8 +501,9 @@ String MIMETypeRegistry::getMIMETypeForPath(const String& path)
         if (result.length())
             return result;
     }
-    return "application/octet-stream";
+    return defaultMIMEType();
 }
+#endif
 
 bool MIMETypeRegistry::isSupportedImageMIMEType(const String& mimeType)
 {
@@ -638,6 +640,13 @@ const String& defaultMIMEType()
     return defaultMIMEType;
 }
 
+#if !PLATFORM(QT) && !PLATFORM(BLACKBERRY)
+String MIMETypeRegistry::getNormalizedMIMEType(const String& mimeType)
+{
+    return mimeType;
+}
+#endif
+
 #if PLATFORM(BLACKBERRY)
 typedef HashMap<String, String> MIMETypeAssociationMap;
 
@@ -696,18 +705,16 @@ static const MIMETypeAssociationMap& mimeTypeAssociationMap()
 
     return *mimeTypeMap;
 }
-#endif
 
 String MIMETypeRegistry::getNormalizedMIMEType(const String& mimeType)
 {
-#if PLATFORM(BLACKBERRY)
     MIMETypeAssociationMap::const_iterator it = mimeTypeAssociationMap().find(mimeType);
 
     if (it != mimeTypeAssociationMap().end())
         return it->value;
-#endif
+
     return mimeType;
 }
-
+#endif
 
 } // namespace WebCore
index bc5553a..850770d 100644 (file)
 #include "config.h"
 #include "MIMETypeRegistry.h"
 
+#include <QMimeDatabase>
 #include <wtf/Assertions.h>
 #include <wtf/MainThread.h>
 
 namespace WebCore {
 
-struct ExtensionMap {
-    const char* extension;
-    const char* mimeType;
-};
+String MIMETypeRegistry::getMIMETypeForExtension(const String &ext)
+{
+    // QMimeDatabase lacks the ability to query by extension alone, so we create a fake filename to lookup.
+    const QString filename = QStringLiteral("filename.") + QString(ext.lower());
 
-static const ExtensionMap extensionMap[] = {
-    { "bmp", "image/bmp" },
-    { "css", "text/css" },
-    { "gif", "image/gif" },
-    { "html", "text/html" },
-    { "htm", "text/html" },
-    { "ico", "image/x-icon" },
-    { "jpeg", "image/jpeg" },
-    { "jpg", "image/jpeg" },
-    { "js", "application/x-javascript" },
-    { "mng", "video/x-mng" },
-    { "mp4", "video/mp4" },
-    { "m4v", "video/mp4" },
-    { "m4a", "audio/x-m4a" },
-    { "mht", "multipart/related" },
-    { "mhtml", "multipart/related" },
-    { "mp3", "audio/mp3" },
-    { "ogv", "video/ogg" },
-    { "oga", "audio/ogg" },
-    { "ogm", "audio/ogg" },
-    { "ogg", "audio/ogg" },
-    { "webm", "video/webm" },
-    { "webm", "audio/webm" },
-    { "wav", "audio/wav" },
-    { "mov", "video/quicktime" },
-    { "pbm", "image/x-portable-bitmap" },
-    { "pgm", "image/x-portable-graymap" },
-    { "pdf", "application/pdf" },
-    { "png", "image/png" },
-    { "ppm", "image/x-portable-pixmap" },
-    { "rss", "application/rss+xml" },
-    { "svg", "image/svg+xml" },
-    { "text", "text/plain" },
-    { "tif", "image/tiff" },
-    { "tiff", "image/tiff" },
-    { "txt", "text/plain" },
-    { "xbm", "image/x-xbitmap" },
-    { "xml", "text/xml" },
-    { "xpm", "image/x-xpm" },
-    { "xsl", "text/xsl" },
-    { "xhtml", "application/xhtml+xml" },
-    { "wml", "text/vnd.wap.wml" },
-    { "wmlc", "application/vnd.wap.wmlc" },
-    { 0, 0 }
-};
+    QMimeType mimeType = QMimeDatabase().mimeTypeForFile(filename, QMimeDatabase::MatchExtension);
+    if (mimeType.isValid() && !mimeType.isDefault())
+        return mimeType.name();
 
-String MIMETypeRegistry::getMIMETypeForExtension(const String &ext)
+    return String();
+}
+
+String MIMETypeRegistry::getMIMETypeForPath(const String& path)
 {
-    ASSERT(isMainThread());
+    QMimeType type = QMimeDatabase().mimeTypeForFile(path, QMimeDatabase::MatchExtension);
+    if (type.isValid())
+        return type.name();
 
-    String s = ext.lower();
+    return defaultMIMEType();
+}
 
-    const ExtensionMap *e = extensionMap;
-    while (e->extension) {
-        if (s == e->extension)
-            return e->mimeType;
-        ++e;
-    }
+String MIMETypeRegistry::getNormalizedMIMEType(const String& mimeTypeName)
+{
+    // This looks up the mime type object by preferred name or alias, and returns the preferred name.
+    QMimeType mimeType = QMimeDatabase().mimeTypeForName(mimeTypeName);
+    if (mimeType.isValid() && !mimeType.isDefault())
+        return mimeType.name();
 
-    return String();
+    return mimeTypeName;
 }
 
 bool MIMETypeRegistry::isApplicationPluginMIMEType(const String& mimeType)