From 7dc3cc31af71522b8fc55b7f97bcba7f5ef9eac5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?V=C3=A1clav=20Slav=C3=ADk?= Date: Wed, 19 Jan 2000 23:03:53 +0000 Subject: [PATCH] mimetype.cpp/.h split into unix,mac,msw git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@5529 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/mac/mimetype.h | 75 +++ include/wx/msw/mimetype.h | 94 +++ include/wx/unix/mimetype.h | 126 ++++ src/common/mimecmn.cpp | 341 ++++++++++ src/mac/carbon/mimetype.cpp | 174 ++++++ src/mac/mimetype.cpp | 174 ++++++ src/msw/mimetype.cpp | 451 ++++++++++++++ src/{common => unix}/mimetype.cpp | 991 +----------------------------- 8 files changed, 1442 insertions(+), 984 deletions(-) create mode 100644 include/wx/mac/mimetype.h create mode 100644 include/wx/msw/mimetype.h create mode 100644 include/wx/unix/mimetype.h create mode 100644 src/common/mimecmn.cpp create mode 100644 src/mac/carbon/mimetype.cpp create mode 100644 src/mac/mimetype.cpp create mode 100644 src/msw/mimetype.cpp rename src/{common => unix}/mimetype.cpp (61%) diff --git a/include/wx/mac/mimetype.h b/include/wx/mac/mimetype.h new file mode 100644 index 0000000000..9f88165669 --- /dev/null +++ b/include/wx/mac/mimetype.h @@ -0,0 +1,75 @@ +///////////////////////////////////////////////////////////////////////////// +// Name: wx/mac/mimetype.h +// Purpose: classes and functions to manage MIME types +// Author: Vadim Zeitlin +// Modified by: +// Created: 23.09.98 +// RCS-ID: $Id$ +// Copyright: (c) 1998 Vadim Zeitlin +// Licence: wxWindows license (part of wxExtra library) +///////////////////////////////////////////////////////////////////////////// + +#ifndef _MIMETYPE_IMPL_H +#define _MIMETYPE_IMPL_H + + +#include "wx/defs.h" +#include "wx/mimetype.h" + + + +class wxMimeTypesManagerImpl +{ +public : + wxMimeTypesManagerImpl() { } + + // implement containing class functions + wxFileType *GetFileTypeFromExtension(const wxString& ext); + wxFileType *GetFileTypeFromMimeType(const wxString& mimeType); + + size_t EnumAllFileTypes(wxArrayString& mimetypes); + + // this are NOPs under MacOS + bool ReadMailcap(const wxString& filename, bool fallback = TRUE) { return TRUE; } + bool ReadMimeTypes(const wxString& filename) { return TRUE; } + + void AddFallback(const wxFileTypeInfo& ft) { m_fallbacks.Add(ft); } + +private: + wxArrayFileTypeInfo m_fallbacks; +}; + +class wxFileTypeImpl +{ +public: + // initialize us with our file type name + void SetFileType(const wxString& strFileType) + { m_strFileType = strFileType; } + void SetExt(const wxString& ext) + { m_ext = ext; } + + // implement accessor functions + bool GetExtensions(wxArrayString& extensions); + bool GetMimeType(wxString *mimeType) const; + bool GetIcon(wxIcon *icon) const; + bool GetDescription(wxString *desc) const; + bool GetOpenCommand(wxString *openCmd, + const wxFileType::MessageParameters&) const + { return GetCommand(openCmd, "open"); } + bool GetPrintCommand(wxString *printCmd, + const wxFileType::MessageParameters&) const + { return GetCommand(printCmd, "print"); } + +private: + // helper function + bool GetCommand(wxString *command, const char *verb) const; + + wxString m_strFileType, m_ext; +}; + + + +#endif + //_MIMETYPE_H + +/* vi: set cin tw=80 ts=4 sw=4: */ diff --git a/include/wx/msw/mimetype.h b/include/wx/msw/mimetype.h new file mode 100644 index 0000000000..5b069f9d5e --- /dev/null +++ b/include/wx/msw/mimetype.h @@ -0,0 +1,94 @@ +///////////////////////////////////////////////////////////////////////////// +// Name: wx/mimetype.h +// Purpose: classes and functions to manage MIME types +// Author: Vadim Zeitlin +// Modified by: +// Created: 23.09.98 +// RCS-ID: $Id$ +// Copyright: (c) 1998 Vadim Zeitlin +// Licence: wxWindows license (part of wxExtra library) +///////////////////////////////////////////////////////////////////////////// + +#ifndef _MIMETYPE_IMPL_H +#define _MIMETYPE_IMPL_H + +#ifdef __GNUG__ +#pragma interface "mimetype.h" +#endif + +#include "wx/defs.h" + +#include "wx/mimetype.h" + + +class WXDLLEXPORT wxFileTypeImpl +{ +public: + // ctor + wxFileTypeImpl() { m_info = NULL; } + + // one of these Init() function must be called (ctor can't take any + // arguments because it's common) + + // initialize us with our file type name and extension - in this case + // we will read all other data from the registry + void Init(const wxString& strFileType, const wxString& ext) + { m_strFileType = strFileType; m_ext = ext; } + + // initialize us with a wxFileTypeInfo object - it contains all the + // data + void Init(const wxFileTypeInfo& info) + { m_info = &info; } + + // implement accessor functions + bool GetExtensions(wxArrayString& extensions); + bool GetMimeType(wxString *mimeType) const; + bool GetIcon(wxIcon *icon) const; + bool GetDescription(wxString *desc) const; + bool GetOpenCommand(wxString *openCmd, + const wxFileType::MessageParameters& params) const; + bool GetPrintCommand(wxString *printCmd, + const wxFileType::MessageParameters& params) const; + +private: + // helper function: reads the command corresponding to the specified verb + // from the registry (returns an empty string if not found) + wxString GetCommand(const wxChar *verb) const; + + // we use either m_info or read the data from the registry if m_info == NULL + const wxFileTypeInfo *m_info; + wxString m_strFileType, // may be empty + m_ext; +}; + + + +class WXDLLEXPORT wxMimeTypesManagerImpl +{ +public: + // nothing to do here, we don't load any data but just go and fetch it from + // the registry when asked for + wxMimeTypesManagerImpl() { } + + // implement containing class functions + wxFileType *GetFileTypeFromExtension(const wxString& ext); + wxFileType *GetFileTypeFromMimeType(const wxString& mimeType); + + size_t EnumAllFileTypes(wxArrayString& mimetypes); + + // this are NOPs under Windows + bool ReadMailcap(const wxString& filename, bool fallback = TRUE) + { return TRUE; } + bool ReadMimeTypes(const wxString& filename) + { return TRUE; } + + void AddFallback(const wxFileTypeInfo& ft) { m_fallbacks.Add(ft); } + +private: + wxArrayFileTypeInfo m_fallbacks; +}; + + +#endif + //_MIMETYPE_IMPL_H + diff --git a/include/wx/unix/mimetype.h b/include/wx/unix/mimetype.h new file mode 100644 index 0000000000..2677c1ffe7 --- /dev/null +++ b/include/wx/unix/mimetype.h @@ -0,0 +1,126 @@ +///////////////////////////////////////////////////////////////////////////// +// Name: unix/mimetype.h +// Purpose: classes and functions to manage MIME types +// Author: Vadim Zeitlin +// Modified by: +// Created: 23.09.98 +// RCS-ID: $Id$ +// Copyright: (c) 1998 Vadim Zeitlin +// Licence: wxWindows license (part of wxExtra library) +///////////////////////////////////////////////////////////////////////////// + +#ifndef _MIMETYPE_IMPL_H +#define _MIMETYPE_IMPL_H + + +#ifdef __GNUG__ +#pragma interface "mimetype.h" +#endif + +#include "wx/mimetype.h" + +#if (wxUSE_FILE && wxUSE_TEXTFILE) + +class MailCapEntry; +class wxMimeTypeIconHandler; + +WX_DEFINE_ARRAY(wxMimeTypeIconHandler *, ArrayIconHandlers); +WX_DEFINE_ARRAY(MailCapEntry *, ArrayTypeEntries); + +// this is the real wxMimeTypesManager for Unix +class WXDLLEXPORT wxMimeTypesManagerImpl +{ +friend class WXDLLEXPORT wxFileTypeImpl; // give it access to m_aXXX variables + +public: + // ctor loads all info into memory for quicker access later on + // TODO it would be nice to load them all, but parse on demand only... + wxMimeTypesManagerImpl(); + + // implement containing class functions + wxFileType *GetFileTypeFromExtension(const wxString& ext); + wxFileType *GetFileTypeFromMimeType(const wxString& mimeType); + + size_t EnumAllFileTypes(wxArrayString& mimetypes); + + bool ReadMailcap(const wxString& filename, bool fallback = FALSE); + bool ReadMimeTypes(const wxString& filename); + + void AddFallback(const wxFileTypeInfo& filetype); + + // add information about the given mimetype + void AddMimeTypeInfo(const wxString& mimetype, + const wxString& extensions, + const wxString& description); + void AddMailcapInfo(const wxString& strType, + const wxString& strOpenCmd, + const wxString& strPrintCmd, + const wxString& strTest, + const wxString& strDesc); + + // accessors + // get the string containing space separated extensions for the given + // file type + wxString GetExtension(size_t index) { return m_aExtensions[index]; } + + // get the array of icon handlers + static ArrayIconHandlers& GetIconHandlers(); + +private: + wxArrayString m_aTypes, // MIME types + m_aDescriptions, // descriptions (just some text) + m_aExtensions; // space separated list of extensions + ArrayTypeEntries m_aEntries; // commands and tests for this file type + + // head of the linked list of the icon handlers + static ArrayIconHandlers ms_iconHandlers; +}; + +class WXDLLEXPORT wxFileTypeImpl +{ +public: + // initialization functions + void Init(wxMimeTypesManagerImpl *manager, size_t index) + { m_manager = manager; m_index = index; } + + // accessors + bool GetExtensions(wxArrayString& extensions); + bool GetMimeType(wxString *mimeType) const + { *mimeType = m_manager->m_aTypes[m_index]; return TRUE; } + bool GetIcon(wxIcon *icon) const; + bool GetDescription(wxString *desc) const + { *desc = m_manager->m_aDescriptions[m_index]; return TRUE; } + + bool GetOpenCommand(wxString *openCmd, + const wxFileType::MessageParameters& params) const + { + return GetExpandedCommand(openCmd, params, TRUE); + } + + bool GetPrintCommand(wxString *printCmd, + const wxFileType::MessageParameters& params) const + { + return GetExpandedCommand(printCmd, params, FALSE); + } + +private: + // get the entry which passes the test (may return NULL) + MailCapEntry *GetEntry(const wxFileType::MessageParameters& params) const; + + // choose the correct entry to use and expand the command + bool GetExpandedCommand(wxString *expandedCmd, + const wxFileType::MessageParameters& params, + bool open) const; + + wxMimeTypesManagerImpl *m_manager; + size_t m_index; // in the wxMimeTypesManagerImpl arrays +}; + + + +#endif + // wxUSE_FILE + +#endif + //_MIMETYPE_IMPL_H + diff --git a/src/common/mimecmn.cpp b/src/common/mimecmn.cpp new file mode 100644 index 0000000000..9dedb8ec86 --- /dev/null +++ b/src/common/mimecmn.cpp @@ -0,0 +1,341 @@ +///////////////////////////////////////////////////////////////////////////// +// Name: common/mimecmn.cpp +// Purpose: classes and functions to manage MIME types +// Author: Vadim Zeitlin +// Modified by: +// Created: 23.09.98 +// RCS-ID: $Id$ +// Copyright: (c) 1998 Vadim Zeitlin +// Licence: wxWindows license (part of wxExtra library) +///////////////////////////////////////////////////////////////////////////// + +#ifdef __GNUG__ +#pragma implementation "mimetypebase.h" +#endif + +// for compilers that support precompilation, includes "wx.h". +#include "wx/wxprec.h" + +#ifdef __BORLANDC__ + #pragma hdrstop +#endif + +#ifndef WX_PRECOMP + #include "wx/defs.h" +#endif + +#if (wxUSE_FILE && wxUSE_TEXTFILE) || defined(__WXMSW__) + +#ifndef WX_PRECOMP + #include "wx/string.h" + #if wxUSE_GUI + #include "wx/icon.h" + #endif +#endif //WX_PRECOMP + +// Doesn't compile in WIN16 mode +#ifndef __WIN16__ + +#include "wx/log.h" +#include "wx/file.h" +#include "wx/intl.h" +#include "wx/dynarray.h" +#include "wx/confbase.h" + +#ifdef __WXMSW__ + #include "wx/msw/registry.h" + #include "windows.h" +#elif defined(__UNIX__) || defined(__WXPM__) + #include "wx/ffile.h" + #include "wx/textfile.h" + #include "wx/dir.h" + #include "wx/utils.h" + #include "wx/tokenzr.h" +#endif // OS + +#include "wx/mimetype.h" + +// other standard headers +#include + +// in case we're compiling in non-GUI mode +class WXDLLEXPORT wxIcon; + + +// implementation classes: + +#if defined(__WXMSW__) +#include "wx/msw/mimetype.h" +#elif defined (__WXMAC__) +#include "wx/mac/mimetype.h" +#else +#include "wx/unix/mimetype.h" +#endif + +// ============================================================================ +// common classes +// ============================================================================ + +// ---------------------------------------------------------------------------- +// wxFileTypeInfo +// ---------------------------------------------------------------------------- + +wxFileTypeInfo::wxFileTypeInfo(const char *mimeType, + const char *openCmd, + const char *printCmd, + const char *desc, + ...) + : m_mimeType(mimeType), + m_openCmd(openCmd), + m_printCmd(printCmd), + m_desc(desc) +{ + va_list argptr; + va_start(argptr, desc); + + for ( ;; ) + { + const char *ext = va_arg(argptr, const char *); + if ( !ext ) + { + // NULL terminates the list + break; + } + + m_exts.Add(ext); + } + + va_end(argptr); +} + +#include "wx/arrimpl.cpp" +WX_DEFINE_OBJARRAY(wxArrayFileTypeInfo); + + +// ============================================================================ +// implementation of the wrapper classes +// ============================================================================ + +// ---------------------------------------------------------------------------- +// wxFileType +// ---------------------------------------------------------------------------- + +wxString wxFileType::ExpandCommand(const wxString& command, + const wxFileType::MessageParameters& params) +{ + bool hasFilename = FALSE; + + wxString str; + for ( const wxChar *pc = command.c_str(); *pc != wxT('\0'); pc++ ) { + if ( *pc == wxT('%') ) { + switch ( *++pc ) { + case wxT('s'): + // '%s' expands into file name (quoted because it might + // contain spaces) - except if there are already quotes + // there because otherwise some programs may get confused + // by double double quotes +#if 0 + if ( *(pc - 2) == wxT('"') ) + str << params.GetFileName(); + else + str << wxT('"') << params.GetFileName() << wxT('"'); +#endif + str << params.GetFileName(); + hasFilename = TRUE; + break; + + case wxT('t'): + // '%t' expands into MIME type (quote it too just to be + // consistent) + str << wxT('\'') << params.GetMimeType() << wxT('\''); + break; + + case wxT('{'): + { + const wxChar *pEnd = wxStrchr(pc, wxT('}')); + if ( pEnd == NULL ) { + wxString mimetype; + wxLogWarning(_("Unmatched '{' in an entry for " + "mime type %s."), + params.GetMimeType().c_str()); + str << wxT("%{"); + } + else { + wxString param(pc + 1, pEnd - pc - 1); + str << wxT('\'') << params.GetParamValue(param) << wxT('\''); + pc = pEnd; + } + } + break; + + case wxT('n'): + case wxT('F'): + // TODO %n is the number of parts, %F is an array containing + // the names of temp files these parts were written to + // and their mime types. + break; + + default: + wxLogDebug(wxT("Unknown field %%%c in command '%s'."), + *pc, command.c_str()); + str << *pc; + } + } + else { + str << *pc; + } + } + + // metamail(1) man page states that if the mailcap entry doesn't have '%s' + // the program will accept the data on stdin: so give it to it! + if ( !hasFilename && !str.IsEmpty() ) { + str << wxT(" < '") << params.GetFileName() << wxT('\''); + } + + return str; +} + +wxFileType::wxFileType() +{ + m_impl = new wxFileTypeImpl; +} + +wxFileType::~wxFileType() +{ + delete m_impl; +} + +bool wxFileType::GetExtensions(wxArrayString& extensions) +{ + return m_impl->GetExtensions(extensions); +} + +bool wxFileType::GetMimeType(wxString *mimeType) const +{ + return m_impl->GetMimeType(mimeType); +} + +bool wxFileType::GetIcon(wxIcon *icon) const +{ + return m_impl->GetIcon(icon); +} + +bool wxFileType::GetDescription(wxString *desc) const +{ + return m_impl->GetDescription(desc); +} + +bool +wxFileType::GetOpenCommand(wxString *openCmd, + const wxFileType::MessageParameters& params) const +{ + return m_impl->GetOpenCommand(openCmd, params); +} + +bool +wxFileType::GetPrintCommand(wxString *printCmd, + const wxFileType::MessageParameters& params) const +{ + return m_impl->GetPrintCommand(printCmd, params); +} + +// ---------------------------------------------------------------------------- +// wxMimeTypesManager +// ---------------------------------------------------------------------------- + +void wxMimeTypesManager::EnsureImpl() +{ + if (m_impl == NULL) + m_impl = new wxMimeTypesManagerImpl; +} + +bool wxMimeTypesManager::IsOfType(const wxString& mimeType, + const wxString& wildcard) +{ + wxASSERT_MSG( mimeType.Find(wxT('*')) == wxNOT_FOUND, + wxT("first MIME type can't contain wildcards") ); + + // all comparaisons are case insensitive (2nd arg of IsSameAs() is FALSE) + if ( wildcard.BeforeFirst(wxT('/')).IsSameAs(mimeType.BeforeFirst(wxT('/')), FALSE) ) + { + wxString strSubtype = wildcard.AfterFirst(wxT('/')); + + if ( strSubtype == wxT("*") || + strSubtype.IsSameAs(mimeType.AfterFirst(wxT('/')), FALSE) ) + { + // matches (either exactly or it's a wildcard) + return TRUE; + } + } + + return FALSE; +} + +wxMimeTypesManager::wxMimeTypesManager() +{ + m_impl = NULL; +} + +wxMimeTypesManager::~wxMimeTypesManager() +{ + if (m_impl != NULL) + delete m_impl; +} + +wxFileType * +wxMimeTypesManager::GetFileTypeFromExtension(const wxString& ext) +{ + EnsureImpl(); + return m_impl->GetFileTypeFromExtension(ext); +} + +wxFileType * +wxMimeTypesManager::GetFileTypeFromMimeType(const wxString& mimeType) +{ + EnsureImpl(); + return m_impl->GetFileTypeFromMimeType(mimeType); +} + +bool wxMimeTypesManager::ReadMailcap(const wxString& filename, bool fallback) +{ + EnsureImpl(); + return m_impl->ReadMailcap(filename, fallback); +} + +bool wxMimeTypesManager::ReadMimeTypes(const wxString& filename) +{ + EnsureImpl(); + return m_impl->ReadMimeTypes(filename); +} + +void wxMimeTypesManager::AddFallbacks(const wxFileTypeInfo *filetypes) +{ + EnsureImpl(); + for ( const wxFileTypeInfo *ft = filetypes; ft->IsValid(); ft++ ) { + m_impl->AddFallback(*ft); + } +} + +size_t wxMimeTypesManager::EnumAllFileTypes(wxArrayString& mimetypes) +{ + EnsureImpl(); + return m_impl->EnumAllFileTypes(mimetypes); +} + + +// ---------------------------------------------------------------------------- +// global data +// ---------------------------------------------------------------------------- + +// private object +static wxMimeTypesManager gs_mimeTypesManager; + +// and public pointer +wxMimeTypesManager * wxTheMimeTypesManager = &gs_mimeTypesManager; + + +#endif + // wxUSE_FILE && wxUSE_TEXTFILE + +#endif + // __WIN16__ diff --git a/src/mac/carbon/mimetype.cpp b/src/mac/carbon/mimetype.cpp new file mode 100644 index 0000000000..c4944d12f8 --- /dev/null +++ b/src/mac/carbon/mimetype.cpp @@ -0,0 +1,174 @@ +///////////////////////////////////////////////////////////////////////////// +// Name: mac/mimetype.cpp +// Purpose: classes and functions to manage MIME types +// Author: Vadim Zeitlin +// Modified by: +// Created: 23.09.98 +// RCS-ID: $Id$ +// Copyright: (c) 1998 Vadim Zeitlin +// Licence: wxWindows license (part of wxExtra library) +///////////////////////////////////////////////////////////////////////////// + +#ifdef __GNUG__ +#pragma implementation "mimetype.h" +#endif + +// for compilers that support precompilation, includes "wx.h". +#include "wx/wxprec.h" + +#ifdef __BORLANDC__ + #pragma hdrstop +#endif + +#ifndef WX_PRECOMP + #include "wx/defs.h" +#endif + +#ifndef WX_PRECOMP + #include "wx/string.h" + #if wxUSE_GUI + #include "wx/icon.h" + #endif +#endif //WX_PRECOMP + + +#include "wx/log.h" +#include "wx/file.h" +#include "wx/intl.h" +#include "wx/dynarray.h" +#include "wx/confbase.h" + +#include "wx/mac/mimetype.h" + +// other standard headers +#include + +// in case we're compiling in non-GUI mode +class WXDLLEXPORT wxIcon; + + + + + + +bool wxFileTypeImpl::GetCommand(wxString *command, const char *verb) const +{ + return FALSE; +} + +// @@ this function is half implemented +bool wxFileTypeImpl::GetExtensions(wxArrayString& extensions) +{ + return FALSE; +} + +bool wxFileTypeImpl::GetMimeType(wxString *mimeType) const +{ + if ( m_strFileType.Length() > 0 ) + { + *mimeType = m_strFileType ; + return TRUE ; + } + else + return FALSE; +} + +bool wxFileTypeImpl::GetIcon(wxIcon *icon) const +{ + // no such file type or no value or incorrect icon entry + return FALSE; +} + +bool wxFileTypeImpl::GetDescription(wxString *desc) const +{ + return FALSE; +} + +// extension -> file type +wxFileType * +wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString& e) +{ + wxString ext = e ; + ext = ext.Lower() ; + if ( ext == "txt" ) + { + wxFileType *fileType = new wxFileType; + fileType->m_impl->SetFileType("text/text"); + fileType->m_impl->SetExt(ext); + return fileType; + } + else if ( ext == "htm" || ext == "html" ) + { + wxFileType *fileType = new wxFileType; + fileType->m_impl->SetFileType("text/html"); + fileType->m_impl->SetExt(ext); + return fileType; + } + else if ( ext == "gif" ) + { + wxFileType *fileType = new wxFileType; + fileType->m_impl->SetFileType("image/gif"); + fileType->m_impl->SetExt(ext); + return fileType; + } + else if ( ext == "png" ) + { + wxFileType *fileType = new wxFileType; + fileType->m_impl->SetFileType("image/png"); + fileType->m_impl->SetExt(ext); + return fileType; + } + else if ( ext == "jpg" || ext == "jpeg" ) + { + wxFileType *fileType = new wxFileType; + fileType->m_impl->SetFileType("image/jpeg"); + fileType->m_impl->SetExt(ext); + return fileType; + } + else if ( ext == "bmp" ) + { + wxFileType *fileType = new wxFileType; + fileType->m_impl->SetFileType("image/bmp"); + fileType->m_impl->SetExt(ext); + return fileType; + } + else if ( ext == "tif" || ext == "tiff" ) + { + wxFileType *fileType = new wxFileType; + fileType->m_impl->SetFileType("image/tiff"); + fileType->m_impl->SetExt(ext); + return fileType; + } + else if ( ext == "xpm" ) + { + wxFileType *fileType = new wxFileType; + fileType->m_impl->SetFileType("image/xpm"); + fileType->m_impl->SetExt(ext); + return fileType; + } + else if ( ext == "xbm" ) + { + wxFileType *fileType = new wxFileType; + fileType->m_impl->SetFileType("image/xbm"); + fileType->m_impl->SetExt(ext); + return fileType; + } + + // unknown extension + return NULL; +} + +// MIME type -> extension -> file type +wxFileType * +wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString& mimeType) +{ + return NULL; +} + +size_t wxMimeTypesManagerImpl::EnumAllFileTypes(wxArrayString& mimetypes) +{ + wxFAIL_MSG( _T("TODO") ); // VZ: don't know anything about this for Mac + + return 0; +} + diff --git a/src/mac/mimetype.cpp b/src/mac/mimetype.cpp new file mode 100644 index 0000000000..c4944d12f8 --- /dev/null +++ b/src/mac/mimetype.cpp @@ -0,0 +1,174 @@ +///////////////////////////////////////////////////////////////////////////// +// Name: mac/mimetype.cpp +// Purpose: classes and functions to manage MIME types +// Author: Vadim Zeitlin +// Modified by: +// Created: 23.09.98 +// RCS-ID: $Id$ +// Copyright: (c) 1998 Vadim Zeitlin +// Licence: wxWindows license (part of wxExtra library) +///////////////////////////////////////////////////////////////////////////// + +#ifdef __GNUG__ +#pragma implementation "mimetype.h" +#endif + +// for compilers that support precompilation, includes "wx.h". +#include "wx/wxprec.h" + +#ifdef __BORLANDC__ + #pragma hdrstop +#endif + +#ifndef WX_PRECOMP + #include "wx/defs.h" +#endif + +#ifndef WX_PRECOMP + #include "wx/string.h" + #if wxUSE_GUI + #include "wx/icon.h" + #endif +#endif //WX_PRECOMP + + +#include "wx/log.h" +#include "wx/file.h" +#include "wx/intl.h" +#include "wx/dynarray.h" +#include "wx/confbase.h" + +#include "wx/mac/mimetype.h" + +// other standard headers +#include + +// in case we're compiling in non-GUI mode +class WXDLLEXPORT wxIcon; + + + + + + +bool wxFileTypeImpl::GetCommand(wxString *command, const char *verb) const +{ + return FALSE; +} + +// @@ this function is half implemented +bool wxFileTypeImpl::GetExtensions(wxArrayString& extensions) +{ + return FALSE; +} + +bool wxFileTypeImpl::GetMimeType(wxString *mimeType) const +{ + if ( m_strFileType.Length() > 0 ) + { + *mimeType = m_strFileType ; + return TRUE ; + } + else + return FALSE; +} + +bool wxFileTypeImpl::GetIcon(wxIcon *icon) const +{ + // no such file type or no value or incorrect icon entry + return FALSE; +} + +bool wxFileTypeImpl::GetDescription(wxString *desc) const +{ + return FALSE; +} + +// extension -> file type +wxFileType * +wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString& e) +{ + wxString ext = e ; + ext = ext.Lower() ; + if ( ext == "txt" ) + { + wxFileType *fileType = new wxFileType; + fileType->m_impl->SetFileType("text/text"); + fileType->m_impl->SetExt(ext); + return fileType; + } + else if ( ext == "htm" || ext == "html" ) + { + wxFileType *fileType = new wxFileType; + fileType->m_impl->SetFileType("text/html"); + fileType->m_impl->SetExt(ext); + return fileType; + } + else if ( ext == "gif" ) + { + wxFileType *fileType = new wxFileType; + fileType->m_impl->SetFileType("image/gif"); + fileType->m_impl->SetExt(ext); + return fileType; + } + else if ( ext == "png" ) + { + wxFileType *fileType = new wxFileType; + fileType->m_impl->SetFileType("image/png"); + fileType->m_impl->SetExt(ext); + return fileType; + } + else if ( ext == "jpg" || ext == "jpeg" ) + { + wxFileType *fileType = new wxFileType; + fileType->m_impl->SetFileType("image/jpeg"); + fileType->m_impl->SetExt(ext); + return fileType; + } + else if ( ext == "bmp" ) + { + wxFileType *fileType = new wxFileType; + fileType->m_impl->SetFileType("image/bmp"); + fileType->m_impl->SetExt(ext); + return fileType; + } + else if ( ext == "tif" || ext == "tiff" ) + { + wxFileType *fileType = new wxFileType; + fileType->m_impl->SetFileType("image/tiff"); + fileType->m_impl->SetExt(ext); + return fileType; + } + else if ( ext == "xpm" ) + { + wxFileType *fileType = new wxFileType; + fileType->m_impl->SetFileType("image/xpm"); + fileType->m_impl->SetExt(ext); + return fileType; + } + else if ( ext == "xbm" ) + { + wxFileType *fileType = new wxFileType; + fileType->m_impl->SetFileType("image/xbm"); + fileType->m_impl->SetExt(ext); + return fileType; + } + + // unknown extension + return NULL; +} + +// MIME type -> extension -> file type +wxFileType * +wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString& mimeType) +{ + return NULL; +} + +size_t wxMimeTypesManagerImpl::EnumAllFileTypes(wxArrayString& mimetypes) +{ + wxFAIL_MSG( _T("TODO") ); // VZ: don't know anything about this for Mac + + return 0; +} + diff --git a/src/msw/mimetype.cpp b/src/msw/mimetype.cpp new file mode 100644 index 0000000000..7d09acec8b --- /dev/null +++ b/src/msw/mimetype.cpp @@ -0,0 +1,451 @@ +///////////////////////////////////////////////////////////////////////////// +// Name: common/mimetype.cpp +// Purpose: classes and functions to manage MIME types +// Author: Vadim Zeitlin +// Modified by: +// Created: 23.09.98 +// RCS-ID: $Id$ +// Copyright: (c) 1998 Vadim Zeitlin +// Licence: wxWindows license (part of wxExtra library) +///////////////////////////////////////////////////////////////////////////// + +#ifdef __GNUG__ +#pragma implementation "mimetype.h" +#endif + +// for compilers that support precompilation, includes "wx.h". +#include "wx/wxprec.h" + +#ifdef __BORLANDC__ + #pragma hdrstop +#endif + +#ifndef WX_PRECOMP + #include "wx/defs.h" +#endif + +#if (wxUSE_FILE && wxUSE_TEXTFILE) || defined(__WXMSW__) + +#ifndef WX_PRECOMP + #include "wx/string.h" + #if wxUSE_GUI + #include "wx/icon.h" + #endif +#endif //WX_PRECOMP + +// Doesn't compile in WIN16 mode +#ifndef __WIN16__ + +#include "wx/log.h" +#include "wx/file.h" +#include "wx/intl.h" +#include "wx/dynarray.h" +#include "wx/confbase.h" + +#ifdef __WXMSW__ + #include "wx/msw/registry.h" + #include "windows.h" +#elif defined(__UNIX__) || defined(__WXPM__) + #include "wx/ffile.h" + #include "wx/textfile.h" + #include "wx/dir.h" + #include "wx/utils.h" + #include "wx/tokenzr.h" +#endif // OS + +#include "wx/msw/mimetype.h" + +// other standard headers +#include + +// in case we're compiling in non-GUI mode +class WXDLLEXPORT wxIcon; + + +// These classes use Windows registry to retrieve the required information. +// +// Keys used (not all of them are documented, so it might actually stop working +// in futur versions of Windows...): +// 1. "HKCR\MIME\Database\Content Type" contains subkeys for all known MIME +// types, each key has a string value "Extension" which gives (dot preceded) +// extension for the files of this MIME type. +// +// 2. "HKCR\.ext" contains +// a) unnamed value containing the "filetype" +// b) value "Content Type" containing the MIME type +// +// 3. "HKCR\filetype" contains +// a) unnamed value containing the description +// b) subkey "DefaultIcon" with single unnamed value giving the icon index in +// an icon file +// c) shell\open\command and shell\open\print subkeys containing the commands +// to open/print the file (the positional parameters are introduced by %1, +// %2, ... in these strings, we change them to %s ourselves) + +// although I don't know of any official documentation which mentions this +// location, uses it, so it isn't likely to change +static const wxChar *MIME_DATABASE_KEY = wxT("MIME\\Database\\Content Type\\"); + + + + +wxString wxFileTypeImpl::GetCommand(const wxChar *verb) const +{ + // suppress possible error messages + wxLogNull nolog; + wxString strKey; + + if ( wxRegKey(wxRegKey::HKCR, m_ext + _T("\\shell")).Exists() ) + strKey = m_ext; + if ( wxRegKey(wxRegKey::HKCR, m_strFileType + _T("\\shell")).Exists() ) + strKey = m_strFileType; + + if ( !strKey ) + { + // no info + return wxEmptyString; + } + + strKey << wxT("\\shell\\") << verb; + wxRegKey key(wxRegKey::HKCR, strKey + _T("\\command")); + wxString command; + if ( key.Open() ) { + // it's the default value of the key + if ( key.QueryValue(wxT(""), command) ) { + // transform it from '%1' to '%s' style format string (now also + // test for %L - apparently MS started using it as well for the + // same purpose) + + // NB: we don't make any attempt to verify that the string is valid, + // i.e. doesn't contain %2, or second %1 or .... But we do make + // sure that we return a string with _exactly_ one '%s'! + bool foundFilename = FALSE; + size_t len = command.Len(); + for ( size_t n = 0; (n < len) && !foundFilename; n++ ) { + if ( command[n] == wxT('%') && + (n + 1 < len) && + (command[n + 1] == wxT('1') || + command[n + 1] == wxT('L')) ) { + // replace it with '%s' + command[n + 1] = wxT('s'); + + foundFilename = TRUE; + } + } + + // look whether we must issue some DDE requests to the application + // (and not just launch it) + strKey += _T("\\DDEExec"); + wxRegKey keyDDE(wxRegKey::HKCR, strKey); + if ( keyDDE.Open() ) { + wxString ddeCommand, ddeServer, ddeTopic; + keyDDE.QueryValue(_T(""), ddeCommand); + ddeCommand.Replace(_T("%1"), _T("%s")); + + wxRegKey(wxRegKey::HKCR, strKey + _T("\\Application")). + QueryValue(_T(""), ddeServer); + wxRegKey(wxRegKey::HKCR, strKey + _T("\\Topic")). + QueryValue(_T(""), ddeTopic); + + // HACK: we use a special feature of wxExecute which exists + // just because we need it here: it will establish DDE + // conversation with the program it just launched + command.Prepend(_T("WX_DDE#")); + command << _T('#') << ddeServer + << _T('#') << ddeTopic + << _T('#') << ddeCommand; + } + else if ( !foundFilename ) { + // we didn't find any '%1' - the application doesn't know which + // file to open (note that we only do it if there is no DDEExec + // subkey) + // + // HACK: append the filename at the end, hope that it will do + command << wxT(" %s"); + } + } + } + //else: no such file type or no value, will return empty string + + return command; +} + +bool +wxFileTypeImpl::GetOpenCommand(wxString *openCmd, + const wxFileType::MessageParameters& params) + const +{ + wxString cmd; + if ( m_info ) { + cmd = m_info->GetOpenCommand(); + } + else { + cmd = GetCommand(wxT("open")); + } + + *openCmd = wxFileType::ExpandCommand(cmd, params); + + return !openCmd->IsEmpty(); +} + +bool +wxFileTypeImpl::GetPrintCommand(wxString *printCmd, + const wxFileType::MessageParameters& params) + const +{ + wxString cmd; + if ( m_info ) { + cmd = m_info->GetPrintCommand(); + } + else { + cmd = GetCommand(wxT("print")); + } + + *printCmd = wxFileType::ExpandCommand(cmd, params); + + return !printCmd->IsEmpty(); +} + +// TODO this function is half implemented +bool wxFileTypeImpl::GetExtensions(wxArrayString& extensions) +{ + if ( m_info ) { + extensions = m_info->GetExtensions(); + + return TRUE; + } + else if ( m_ext.IsEmpty() ) { + // the only way to get the list of extensions from the file type is to + // scan through all extensions in the registry - too slow... + return FALSE; + } + else { + extensions.Empty(); + extensions.Add(m_ext); + + // it's a lie too, we don't return _all_ extensions... + return TRUE; + } +} + +bool wxFileTypeImpl::GetMimeType(wxString *mimeType) const +{ + if ( m_info ) { + // we already have it + *mimeType = m_info->GetMimeType(); + + return TRUE; + } + + // suppress possible error messages + wxLogNull nolog; + wxRegKey key(wxRegKey::HKCR, wxT(".") + m_ext); + if ( key.Open() && key.QueryValue(wxT("Content Type"), *mimeType) ) { + return TRUE; + } + else { + return FALSE; + } +} + +bool wxFileTypeImpl::GetIcon(wxIcon *icon) const +{ +#if wxUSE_GUI + if ( m_info ) { + // we don't have icons in the fallback resources + return FALSE; + } + + wxString strIconKey; + strIconKey << m_strFileType << wxT("\\DefaultIcon"); + + // suppress possible error messages + wxLogNull nolog; + wxRegKey key(wxRegKey::HKCR, strIconKey); + + if ( key.Open() ) { + wxString strIcon; + // it's the default value of the key + if ( key.QueryValue(wxT(""), strIcon) ) { + // the format is the following: , + // NB: icon index may be negative as well as positive and the full + // path may contain the environment variables inside '%' + wxString strFullPath = strIcon.BeforeLast(wxT(',')), + strIndex = strIcon.AfterLast(wxT(',')); + + // index may be omitted, in which case BeforeLast(',') is empty and + // AfterLast(',') is the whole string + if ( strFullPath.IsEmpty() ) { + strFullPath = strIndex; + strIndex = wxT("0"); + } + + wxString strExpPath = wxExpandEnvVars(strFullPath); + int nIndex = wxAtoi(strIndex); + + HICON hIcon = ExtractIcon(GetModuleHandle(NULL), strExpPath, nIndex); + switch ( (int)hIcon ) { + case 0: // means no icons were found + case 1: // means no such file or it wasn't a DLL/EXE/OCX/ICO/... + wxLogDebug(wxT("incorrect registry entry '%s': no such icon."), + key.GetName().c_str()); + break; + + default: + icon->SetHICON((WXHICON)hIcon); + return TRUE; + } + } + } + + // no such file type or no value or incorrect icon entry +#endif // wxUSE_GUI + + return FALSE; +} + +bool wxFileTypeImpl::GetDescription(wxString *desc) const +{ + if ( m_info ) { + // we already have it + *desc = m_info->GetDescription(); + + return TRUE; + } + + // suppress possible error messages + wxLogNull nolog; + wxRegKey key(wxRegKey::HKCR, m_strFileType); + + if ( key.Open() ) { + // it's the default value of the key + if ( key.QueryValue(wxT(""), *desc) ) { + return TRUE; + } + } + + return FALSE; +} + +// extension -> file type +wxFileType * +wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString& ext) +{ + // add the leading point if necessary + wxString str; + if ( ext[0u] != wxT('.') ) { + str = wxT('.'); + } + str << ext; + + // suppress possible error messages + wxLogNull nolog; + + bool knownExtension = FALSE; + + wxString strFileType; + wxRegKey key(wxRegKey::HKCR, str); + if ( key.Open() ) { + // it's the default value of the key + if ( key.QueryValue(wxT(""), strFileType) ) { + // create the new wxFileType object + wxFileType *fileType = new wxFileType; + fileType->m_impl->Init(strFileType, ext); + + return fileType; + } + else { + // this extension doesn't have a filetype, but it's known to the + // system and may be has some other useful keys (open command or + // content-type), so still return a file type object for it + knownExtension = TRUE; + } + } + + // check the fallbacks + // TODO linear search is potentially slow, perhaps we should use a sorted + // array? + size_t count = m_fallbacks.GetCount(); + for ( size_t n = 0; n < count; n++ ) { + if ( m_fallbacks[n].GetExtensions().Index(ext) != wxNOT_FOUND ) { + wxFileType *fileType = new wxFileType; + fileType->m_impl->Init(m_fallbacks[n]); + + return fileType; + } + } + + if ( knownExtension ) + { + wxFileType *fileType = new wxFileType; + fileType->m_impl->Init(wxEmptyString, ext); + + return fileType; + } + else + { + // unknown extension + return NULL; + } +} + +// MIME type -> extension -> file type +wxFileType * +wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString& mimeType) +{ + wxString strKey = MIME_DATABASE_KEY; + strKey << mimeType; + + // suppress possible error messages + wxLogNull nolog; + + wxString ext; + wxRegKey key(wxRegKey::HKCR, strKey); + if ( key.Open() ) { + if ( key.QueryValue(wxT("Extension"), ext) ) { + return GetFileTypeFromExtension(ext); + } + } + + // check the fallbacks + // TODO linear search is potentially slow, perhaps we should use a sorted + // array? + size_t count = m_fallbacks.GetCount(); + for ( size_t n = 0; n < count; n++ ) { + if ( wxMimeTypesManager::IsOfType(mimeType, + m_fallbacks[n].GetMimeType()) ) { + wxFileType *fileType = new wxFileType; + fileType->m_impl->Init(m_fallbacks[n]); + + return fileType; + } + } + + // unknown MIME type + return NULL; +} + +size_t wxMimeTypesManagerImpl::EnumAllFileTypes(wxArrayString& mimetypes) +{ + // enumerate all keys under MIME_DATABASE_KEY + wxRegKey key(wxRegKey::HKCR, MIME_DATABASE_KEY); + + wxString type; + long cookie; + bool cont = key.GetFirstKey(type, cookie); + while ( cont ) + { + mimetypes.Add(type); + + cont = key.GetNextKey(type, cookie); + } + + return mimetypes.GetCount(); +} + + +#endif + // wxUSE_FILE && wxUSE_TEXTFILE + +#endif + // __WIN16__ diff --git a/src/common/mimetype.cpp b/src/unix/mimetype.cpp similarity index 61% rename from src/common/mimetype.cpp rename to src/unix/mimetype.cpp index 57eacdb803..ece8e422eb 100644 --- a/src/common/mimetype.cpp +++ b/src/unix/mimetype.cpp @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: common/mimetype.cpp +// Name: unix/mimetype.cpp // Purpose: classes and functions to manage MIME types // Author: Vadim Zeitlin // Modified by: @@ -33,8 +33,6 @@ #endif #endif //WX_PRECOMP -// Doesn't compile in WIN16 mode -#ifndef __WIN16__ #include "wx/log.h" #include "wx/file.h" @@ -42,18 +40,13 @@ #include "wx/dynarray.h" #include "wx/confbase.h" -#ifdef __WXMSW__ - #include "wx/msw/registry.h" - #include "windows.h" -#elif defined(__UNIX__) || defined(__WXPM__) - #include "wx/ffile.h" - #include "wx/textfile.h" - #include "wx/dir.h" - #include "wx/utils.h" - #include "wx/tokenzr.h" -#endif // OS +#include "wx/ffile.h" +#include "wx/textfile.h" +#include "wx/dir.h" +#include "wx/utils.h" +#include "wx/tokenzr.h" -#include "wx/mimetype.h" +#include "wx/unix/mimetype.h" // other standard headers #include @@ -65,158 +58,6 @@ class WXDLLEXPORT wxIcon; // private classes // ---------------------------------------------------------------------------- -// implementation classes, platform dependent -#ifdef __WXMSW__ - -// These classes use Windows registry to retrieve the required information. -// -// Keys used (not all of them are documented, so it might actually stop working -// in futur versions of Windows...): -// 1. "HKCR\MIME\Database\Content Type" contains subkeys for all known MIME -// types, each key has a string value "Extension" which gives (dot preceded) -// extension for the files of this MIME type. -// -// 2. "HKCR\.ext" contains -// a) unnamed value containing the "filetype" -// b) value "Content Type" containing the MIME type -// -// 3. "HKCR\filetype" contains -// a) unnamed value containing the description -// b) subkey "DefaultIcon" with single unnamed value giving the icon index in -// an icon file -// c) shell\open\command and shell\open\print subkeys containing the commands -// to open/print the file (the positional parameters are introduced by %1, -// %2, ... in these strings, we change them to %s ourselves) - -// although I don't know of any official documentation which mentions this -// location, uses it, so it isn't likely to change -static const wxChar *MIME_DATABASE_KEY = wxT("MIME\\Database\\Content Type\\"); - -class wxFileTypeImpl -{ -public: - // ctor - wxFileTypeImpl() { m_info = NULL; } - - // one of these Init() function must be called (ctor can't take any - // arguments because it's common) - - // initialize us with our file type name and extension - in this case - // we will read all other data from the registry - void Init(const wxString& strFileType, const wxString& ext) - { m_strFileType = strFileType; m_ext = ext; } - - // initialize us with a wxFileTypeInfo object - it contains all the - // data - void Init(const wxFileTypeInfo& info) - { m_info = &info; } - - // implement accessor functions - bool GetExtensions(wxArrayString& extensions); - bool GetMimeType(wxString *mimeType) const; - bool GetIcon(wxIcon *icon) const; - bool GetDescription(wxString *desc) const; - bool GetOpenCommand(wxString *openCmd, - const wxFileType::MessageParameters& params) const; - bool GetPrintCommand(wxString *printCmd, - const wxFileType::MessageParameters& params) const; - -private: - // helper function: reads the command corresponding to the specified verb - // from the registry (returns an empty string if not found) - wxString GetCommand(const wxChar *verb) const; - - // we use either m_info or read the data from the registry if m_info == NULL - const wxFileTypeInfo *m_info; - wxString m_strFileType, // may be empty - m_ext; -}; - -WX_DECLARE_EXPORTED_OBJARRAY(wxFileTypeInfo, wxArrayFileTypeInfo); -#include "wx/arrimpl.cpp" -WX_DEFINE_OBJARRAY(wxArrayFileTypeInfo); - -class wxMimeTypesManagerImpl -{ -public: - // nothing to do here, we don't load any data but just go and fetch it from - // the registry when asked for - wxMimeTypesManagerImpl() { } - - // implement containing class functions - wxFileType *GetFileTypeFromExtension(const wxString& ext); - wxFileType *GetFileTypeFromMimeType(const wxString& mimeType); - - size_t EnumAllFileTypes(wxArrayString& mimetypes); - - // this are NOPs under Windows - bool ReadMailcap(const wxString& filename, bool fallback = TRUE) - { return TRUE; } - bool ReadMimeTypes(const wxString& filename) - { return TRUE; } - - void AddFallback(const wxFileTypeInfo& ft) { m_fallbacks.Add(ft); } - -private: - wxArrayFileTypeInfo m_fallbacks; -}; - -#elif defined( __WXMAC__ ) - -WX_DECLARE_EXPORTED_OBJARRAY(wxFileTypeInfo, wxArrayFileTypeInfo); -#include "wx/arrimpl.cpp" -WX_DEFINE_OBJARRAY(wxArrayFileTypeInfo); - -class wxMimeTypesManagerImpl -{ -public : - wxMimeTypesManagerImpl() { } - - // implement containing class functions - wxFileType *GetFileTypeFromExtension(const wxString& ext); - wxFileType *GetFileTypeFromMimeType(const wxString& mimeType); - - size_t EnumAllFileTypes(wxArrayString& mimetypes); - - // this are NOPs under MacOS - bool ReadMailcap(const wxString& filename, bool fallback = TRUE) { return TRUE; } - bool ReadMimeTypes(const wxString& filename) { return TRUE; } - - void AddFallback(const wxFileTypeInfo& ft) { m_fallbacks.Add(ft); } - -private: - wxArrayFileTypeInfo m_fallbacks; -}; - -class wxFileTypeImpl -{ -public: - // initialize us with our file type name - void SetFileType(const wxString& strFileType) - { m_strFileType = strFileType; } - void SetExt(const wxString& ext) - { m_ext = ext; } - - // implement accessor functions - bool GetExtensions(wxArrayString& extensions); - bool GetMimeType(wxString *mimeType) const; - bool GetIcon(wxIcon *icon) const; - bool GetDescription(wxString *desc) const; - bool GetOpenCommand(wxString *openCmd, - const wxFileType::MessageParameters&) const - { return GetCommand(openCmd, "open"); } - bool GetPrintCommand(wxString *printCmd, - const wxFileType::MessageParameters&) const - { return GetCommand(printCmd, "print"); } - -private: - // helper function - bool GetCommand(wxString *command, const char *verb) const; - - wxString m_strFileType, m_ext; -}; - -#else // Unix // this class uses both mailcap and mime.types to gather information about file // types. @@ -348,7 +189,6 @@ private: MailCapEntry *m_next; // in the linked list }; -WX_DEFINE_ARRAY(MailCapEntry *, ArrayTypeEntries); // the base class which may be used to find an icon for the MIME type class wxMimeTypeIconHandler @@ -362,7 +202,6 @@ public: virtual void GetMimeInfoRecords(wxMimeTypesManagerImpl *manager) = 0; }; -WX_DEFINE_ARRAY(wxMimeTypeIconHandler *, ArrayIconHandlers); // the icon handler which uses GNOME MIME database class wxGNOMEIconHandler : public wxMimeTypeIconHandler @@ -411,818 +250,7 @@ private: static wxArrayString ms_infoExtensions; }; -// this is the real wxMimeTypesManager for Unix -class wxMimeTypesManagerImpl -{ -friend class wxFileTypeImpl; // give it access to m_aXXX variables - -public: - // ctor loads all info into memory for quicker access later on - // TODO it would be nice to load them all, but parse on demand only... - wxMimeTypesManagerImpl(); - - // implement containing class functions - wxFileType *GetFileTypeFromExtension(const wxString& ext); - wxFileType *GetFileTypeFromMimeType(const wxString& mimeType); - - size_t EnumAllFileTypes(wxArrayString& mimetypes); - - bool ReadMailcap(const wxString& filename, bool fallback = FALSE); - bool ReadMimeTypes(const wxString& filename); - - void AddFallback(const wxFileTypeInfo& filetype); - - // add information about the given mimetype - void AddMimeTypeInfo(const wxString& mimetype, - const wxString& extensions, - const wxString& description); - void AddMailcapInfo(const wxString& strType, - const wxString& strOpenCmd, - const wxString& strPrintCmd, - const wxString& strTest, - const wxString& strDesc); - - // accessors - // get the string containing space separated extensions for the given - // file type - wxString GetExtension(size_t index) { return m_aExtensions[index]; } - - // get the array of icon handlers - static ArrayIconHandlers& GetIconHandlers(); - -private: - wxArrayString m_aTypes, // MIME types - m_aDescriptions, // descriptions (just some text) - m_aExtensions; // space separated list of extensions - ArrayTypeEntries m_aEntries; // commands and tests for this file type - - // head of the linked list of the icon handlers - static ArrayIconHandlers ms_iconHandlers; -}; - -class wxFileTypeImpl -{ -public: - // initialization functions - void Init(wxMimeTypesManagerImpl *manager, size_t index) - { m_manager = manager; m_index = index; } - - // accessors - bool GetExtensions(wxArrayString& extensions); - bool GetMimeType(wxString *mimeType) const - { *mimeType = m_manager->m_aTypes[m_index]; return TRUE; } - bool GetIcon(wxIcon *icon) const; - bool GetDescription(wxString *desc) const - { *desc = m_manager->m_aDescriptions[m_index]; return TRUE; } - - bool GetOpenCommand(wxString *openCmd, - const wxFileType::MessageParameters& params) const - { - return GetExpandedCommand(openCmd, params, TRUE); - } - - bool GetPrintCommand(wxString *printCmd, - const wxFileType::MessageParameters& params) const - { - return GetExpandedCommand(printCmd, params, FALSE); - } - -private: - // get the entry which passes the test (may return NULL) - MailCapEntry *GetEntry(const wxFileType::MessageParameters& params) const; - - // choose the correct entry to use and expand the command - bool GetExpandedCommand(wxString *expandedCmd, - const wxFileType::MessageParameters& params, - bool open) const; - - wxMimeTypesManagerImpl *m_manager; - size_t m_index; // in the wxMimeTypesManagerImpl arrays -}; - -#endif // OS type - -// ============================================================================ -// common classes -// ============================================================================ - -// ---------------------------------------------------------------------------- -// wxFileTypeInfo -// ---------------------------------------------------------------------------- - -wxFileTypeInfo::wxFileTypeInfo(const char *mimeType, - const char *openCmd, - const char *printCmd, - const char *desc, - ...) - : m_mimeType(mimeType), - m_openCmd(openCmd), - m_printCmd(printCmd), - m_desc(desc) -{ - va_list argptr; - va_start(argptr, desc); - - for ( ;; ) - { - const char *ext = va_arg(argptr, const char *); - if ( !ext ) - { - // NULL terminates the list - break; - } - - m_exts.Add(ext); - } - - va_end(argptr); -} - -// ============================================================================ -// implementation of the wrapper classes -// ============================================================================ - -// ---------------------------------------------------------------------------- -// wxFileType -// ---------------------------------------------------------------------------- - -wxString wxFileType::ExpandCommand(const wxString& command, - const wxFileType::MessageParameters& params) -{ - bool hasFilename = FALSE; - - wxString str; - for ( const wxChar *pc = command.c_str(); *pc != wxT('\0'); pc++ ) { - if ( *pc == wxT('%') ) { - switch ( *++pc ) { - case wxT('s'): - // '%s' expands into file name (quoted because it might - // contain spaces) - except if there are already quotes - // there because otherwise some programs may get confused - // by double double quotes -#if 0 - if ( *(pc - 2) == wxT('"') ) - str << params.GetFileName(); - else - str << wxT('"') << params.GetFileName() << wxT('"'); -#endif - str << params.GetFileName(); - hasFilename = TRUE; - break; - - case wxT('t'): - // '%t' expands into MIME type (quote it too just to be - // consistent) - str << wxT('\'') << params.GetMimeType() << wxT('\''); - break; - - case wxT('{'): - { - const wxChar *pEnd = wxStrchr(pc, wxT('}')); - if ( pEnd == NULL ) { - wxString mimetype; - wxLogWarning(_("Unmatched '{' in an entry for " - "mime type %s."), - params.GetMimeType().c_str()); - str << wxT("%{"); - } - else { - wxString param(pc + 1, pEnd - pc - 1); - str << wxT('\'') << params.GetParamValue(param) << wxT('\''); - pc = pEnd; - } - } - break; - - case wxT('n'): - case wxT('F'): - // TODO %n is the number of parts, %F is an array containing - // the names of temp files these parts were written to - // and their mime types. - break; - - default: - wxLogDebug(wxT("Unknown field %%%c in command '%s'."), - *pc, command.c_str()); - str << *pc; - } - } - else { - str << *pc; - } - } - - // metamail(1) man page states that if the mailcap entry doesn't have '%s' - // the program will accept the data on stdin: so give it to it! - if ( !hasFilename && !str.IsEmpty() ) { - str << wxT(" < '") << params.GetFileName() << wxT('\''); - } - - return str; -} - -wxFileType::wxFileType() -{ - m_impl = new wxFileTypeImpl; -} - -wxFileType::~wxFileType() -{ - delete m_impl; -} - -bool wxFileType::GetExtensions(wxArrayString& extensions) -{ - return m_impl->GetExtensions(extensions); -} - -bool wxFileType::GetMimeType(wxString *mimeType) const -{ - return m_impl->GetMimeType(mimeType); -} - -bool wxFileType::GetIcon(wxIcon *icon) const -{ - return m_impl->GetIcon(icon); -} - -bool wxFileType::GetDescription(wxString *desc) const -{ - return m_impl->GetDescription(desc); -} -bool -wxFileType::GetOpenCommand(wxString *openCmd, - const wxFileType::MessageParameters& params) const -{ - return m_impl->GetOpenCommand(openCmd, params); -} - -bool -wxFileType::GetPrintCommand(wxString *printCmd, - const wxFileType::MessageParameters& params) const -{ - return m_impl->GetPrintCommand(printCmd, params); -} - -// ---------------------------------------------------------------------------- -// wxMimeTypesManager -// ---------------------------------------------------------------------------- - -bool wxMimeTypesManager::IsOfType(const wxString& mimeType, - const wxString& wildcard) -{ - wxASSERT_MSG( mimeType.Find(wxT('*')) == wxNOT_FOUND, - wxT("first MIME type can't contain wildcards") ); - - // all comparaisons are case insensitive (2nd arg of IsSameAs() is FALSE) - if ( wildcard.BeforeFirst(wxT('/')).IsSameAs(mimeType.BeforeFirst(wxT('/')), FALSE) ) - { - wxString strSubtype = wildcard.AfterFirst(wxT('/')); - - if ( strSubtype == wxT("*") || - strSubtype.IsSameAs(mimeType.AfterFirst(wxT('/')), FALSE) ) - { - // matches (either exactly or it's a wildcard) - return TRUE; - } - } - - return FALSE; -} - -wxMimeTypesManager::wxMimeTypesManager() -{ - m_impl = new wxMimeTypesManagerImpl; -} - -wxMimeTypesManager::~wxMimeTypesManager() -{ - delete m_impl; -} - -wxFileType * -wxMimeTypesManager::GetFileTypeFromExtension(const wxString& ext) -{ - return m_impl->GetFileTypeFromExtension(ext); -} - -wxFileType * -wxMimeTypesManager::GetFileTypeFromMimeType(const wxString& mimeType) -{ - return m_impl->GetFileTypeFromMimeType(mimeType); -} - -bool wxMimeTypesManager::ReadMailcap(const wxString& filename, bool fallback) -{ - return m_impl->ReadMailcap(filename, fallback); -} - -bool wxMimeTypesManager::ReadMimeTypes(const wxString& filename) -{ - return m_impl->ReadMimeTypes(filename); -} - -void wxMimeTypesManager::AddFallbacks(const wxFileTypeInfo *filetypes) -{ - for ( const wxFileTypeInfo *ft = filetypes; ft->IsValid(); ft++ ) { - m_impl->AddFallback(*ft); - } -} - -size_t wxMimeTypesManager::EnumAllFileTypes(wxArrayString& mimetypes) -{ - return m_impl->EnumAllFileTypes(mimetypes); -} - -// ============================================================================ -// real (OS specific) implementation -// ============================================================================ - -#ifdef __WXMSW__ - -wxString wxFileTypeImpl::GetCommand(const wxChar *verb) const -{ - // suppress possible error messages - wxLogNull nolog; - wxString strKey; - - if ( wxRegKey(wxRegKey::HKCR, m_ext + _T("\\shell")).Exists() ) - strKey = m_ext; - if ( wxRegKey(wxRegKey::HKCR, m_strFileType + _T("\\shell")).Exists() ) - strKey = m_strFileType; - - if ( !strKey ) - { - // no info - return wxEmptyString; - } - - strKey << wxT("\\shell\\") << verb; - wxRegKey key(wxRegKey::HKCR, strKey + _T("\\command")); - wxString command; - if ( key.Open() ) { - // it's the default value of the key - if ( key.QueryValue(wxT(""), command) ) { - // transform it from '%1' to '%s' style format string (now also - // test for %L - apparently MS started using it as well for the - // same purpose) - - // NB: we don't make any attempt to verify that the string is valid, - // i.e. doesn't contain %2, or second %1 or .... But we do make - // sure that we return a string with _exactly_ one '%s'! - bool foundFilename = FALSE; - size_t len = command.Len(); - for ( size_t n = 0; (n < len) && !foundFilename; n++ ) { - if ( command[n] == wxT('%') && - (n + 1 < len) && - (command[n + 1] == wxT('1') || - command[n + 1] == wxT('L')) ) { - // replace it with '%s' - command[n + 1] = wxT('s'); - - foundFilename = TRUE; - } - } - - // look whether we must issue some DDE requests to the application - // (and not just launch it) - strKey += _T("\\DDEExec"); - wxRegKey keyDDE(wxRegKey::HKCR, strKey); - if ( keyDDE.Open() ) { - wxString ddeCommand, ddeServer, ddeTopic; - keyDDE.QueryValue(_T(""), ddeCommand); - ddeCommand.Replace(_T("%1"), _T("%s")); - - wxRegKey(wxRegKey::HKCR, strKey + _T("\\Application")). - QueryValue(_T(""), ddeServer); - wxRegKey(wxRegKey::HKCR, strKey + _T("\\Topic")). - QueryValue(_T(""), ddeTopic); - - // HACK: we use a special feature of wxExecute which exists - // just because we need it here: it will establish DDE - // conversation with the program it just launched - command.Prepend(_T("WX_DDE#")); - command << _T('#') << ddeServer - << _T('#') << ddeTopic - << _T('#') << ddeCommand; - } - else if ( !foundFilename ) { - // we didn't find any '%1' - the application doesn't know which - // file to open (note that we only do it if there is no DDEExec - // subkey) - // - // HACK: append the filename at the end, hope that it will do - command << wxT(" %s"); - } - } - } - //else: no such file type or no value, will return empty string - - return command; -} - -bool -wxFileTypeImpl::GetOpenCommand(wxString *openCmd, - const wxFileType::MessageParameters& params) - const -{ - wxString cmd; - if ( m_info ) { - cmd = m_info->GetOpenCommand(); - } - else { - cmd = GetCommand(wxT("open")); - } - - *openCmd = wxFileType::ExpandCommand(cmd, params); - - return !openCmd->IsEmpty(); -} - -bool -wxFileTypeImpl::GetPrintCommand(wxString *printCmd, - const wxFileType::MessageParameters& params) - const -{ - wxString cmd; - if ( m_info ) { - cmd = m_info->GetPrintCommand(); - } - else { - cmd = GetCommand(wxT("print")); - } - - *printCmd = wxFileType::ExpandCommand(cmd, params); - - return !printCmd->IsEmpty(); -} - -// TODO this function is half implemented -bool wxFileTypeImpl::GetExtensions(wxArrayString& extensions) -{ - if ( m_info ) { - extensions = m_info->GetExtensions(); - - return TRUE; - } - else if ( m_ext.IsEmpty() ) { - // the only way to get the list of extensions from the file type is to - // scan through all extensions in the registry - too slow... - return FALSE; - } - else { - extensions.Empty(); - extensions.Add(m_ext); - - // it's a lie too, we don't return _all_ extensions... - return TRUE; - } -} - -bool wxFileTypeImpl::GetMimeType(wxString *mimeType) const -{ - if ( m_info ) { - // we already have it - *mimeType = m_info->GetMimeType(); - - return TRUE; - } - - // suppress possible error messages - wxLogNull nolog; - wxRegKey key(wxRegKey::HKCR, wxT(".") + m_ext); - if ( key.Open() && key.QueryValue(wxT("Content Type"), *mimeType) ) { - return TRUE; - } - else { - return FALSE; - } -} - -bool wxFileTypeImpl::GetIcon(wxIcon *icon) const -{ -#if wxUSE_GUI - if ( m_info ) { - // we don't have icons in the fallback resources - return FALSE; - } - - wxString strIconKey; - strIconKey << m_strFileType << wxT("\\DefaultIcon"); - - // suppress possible error messages - wxLogNull nolog; - wxRegKey key(wxRegKey::HKCR, strIconKey); - - if ( key.Open() ) { - wxString strIcon; - // it's the default value of the key - if ( key.QueryValue(wxT(""), strIcon) ) { - // the format is the following: , - // NB: icon index may be negative as well as positive and the full - // path may contain the environment variables inside '%' - wxString strFullPath = strIcon.BeforeLast(wxT(',')), - strIndex = strIcon.AfterLast(wxT(',')); - - // index may be omitted, in which case BeforeLast(',') is empty and - // AfterLast(',') is the whole string - if ( strFullPath.IsEmpty() ) { - strFullPath = strIndex; - strIndex = wxT("0"); - } - - wxString strExpPath = wxExpandEnvVars(strFullPath); - int nIndex = wxAtoi(strIndex); - - HICON hIcon = ExtractIcon(GetModuleHandle(NULL), strExpPath, nIndex); - switch ( (int)hIcon ) { - case 0: // means no icons were found - case 1: // means no such file or it wasn't a DLL/EXE/OCX/ICO/... - wxLogDebug(wxT("incorrect registry entry '%s': no such icon."), - key.GetName().c_str()); - break; - - default: - icon->SetHICON((WXHICON)hIcon); - return TRUE; - } - } - } - - // no such file type or no value or incorrect icon entry -#endif // wxUSE_GUI - - return FALSE; -} - -bool wxFileTypeImpl::GetDescription(wxString *desc) const -{ - if ( m_info ) { - // we already have it - *desc = m_info->GetDescription(); - - return TRUE; - } - - // suppress possible error messages - wxLogNull nolog; - wxRegKey key(wxRegKey::HKCR, m_strFileType); - - if ( key.Open() ) { - // it's the default value of the key - if ( key.QueryValue(wxT(""), *desc) ) { - return TRUE; - } - } - - return FALSE; -} - -// extension -> file type -wxFileType * -wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString& ext) -{ - // add the leading point if necessary - wxString str; - if ( ext[0u] != wxT('.') ) { - str = wxT('.'); - } - str << ext; - - // suppress possible error messages - wxLogNull nolog; - - bool knownExtension = FALSE; - - wxString strFileType; - wxRegKey key(wxRegKey::HKCR, str); - if ( key.Open() ) { - // it's the default value of the key - if ( key.QueryValue(wxT(""), strFileType) ) { - // create the new wxFileType object - wxFileType *fileType = new wxFileType; - fileType->m_impl->Init(strFileType, ext); - - return fileType; - } - else { - // this extension doesn't have a filetype, but it's known to the - // system and may be has some other useful keys (open command or - // content-type), so still return a file type object for it - knownExtension = TRUE; - } - } - - // check the fallbacks - // TODO linear search is potentially slow, perhaps we should use a sorted - // array? - size_t count = m_fallbacks.GetCount(); - for ( size_t n = 0; n < count; n++ ) { - if ( m_fallbacks[n].GetExtensions().Index(ext) != wxNOT_FOUND ) { - wxFileType *fileType = new wxFileType; - fileType->m_impl->Init(m_fallbacks[n]); - - return fileType; - } - } - - if ( knownExtension ) - { - wxFileType *fileType = new wxFileType; - fileType->m_impl->Init(wxEmptyString, ext); - - return fileType; - } - else - { - // unknown extension - return NULL; - } -} - -// MIME type -> extension -> file type -wxFileType * -wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString& mimeType) -{ - wxString strKey = MIME_DATABASE_KEY; - strKey << mimeType; - - // suppress possible error messages - wxLogNull nolog; - - wxString ext; - wxRegKey key(wxRegKey::HKCR, strKey); - if ( key.Open() ) { - if ( key.QueryValue(wxT("Extension"), ext) ) { - return GetFileTypeFromExtension(ext); - } - } - - // check the fallbacks - // TODO linear search is potentially slow, perhaps we should use a sorted - // array? - size_t count = m_fallbacks.GetCount(); - for ( size_t n = 0; n < count; n++ ) { - if ( wxMimeTypesManager::IsOfType(mimeType, - m_fallbacks[n].GetMimeType()) ) { - wxFileType *fileType = new wxFileType; - fileType->m_impl->Init(m_fallbacks[n]); - - return fileType; - } - } - - // unknown MIME type - return NULL; -} - -size_t wxMimeTypesManagerImpl::EnumAllFileTypes(wxArrayString& mimetypes) -{ - // enumerate all keys under MIME_DATABASE_KEY - wxRegKey key(wxRegKey::HKCR, MIME_DATABASE_KEY); - - wxString type; - long cookie; - bool cont = key.GetFirstKey(type, cookie); - while ( cont ) - { - mimetypes.Add(type); - - cont = key.GetNextKey(type, cookie); - } - - return mimetypes.GetCount(); -} - -#elif defined ( __WXMAC__ ) - -bool wxFileTypeImpl::GetCommand(wxString *command, const char *verb) const -{ - return FALSE; -} - -// @@ this function is half implemented -bool wxFileTypeImpl::GetExtensions(wxArrayString& extensions) -{ - return FALSE; -} - -bool wxFileTypeImpl::GetMimeType(wxString *mimeType) const -{ - if ( m_strFileType.Length() > 0 ) - { - *mimeType = m_strFileType ; - return TRUE ; - } - else - return FALSE; -} - -bool wxFileTypeImpl::GetIcon(wxIcon *icon) const -{ - // no such file type or no value or incorrect icon entry - return FALSE; -} - -bool wxFileTypeImpl::GetDescription(wxString *desc) const -{ - return FALSE; -} - -// extension -> file type -wxFileType * -wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString& e) -{ - wxString ext = e ; - ext = ext.Lower() ; - if ( ext == "txt" ) - { - wxFileType *fileType = new wxFileType; - fileType->m_impl->SetFileType("text/text"); - fileType->m_impl->SetExt(ext); - return fileType; - } - else if ( ext == "htm" || ext == "html" ) - { - wxFileType *fileType = new wxFileType; - fileType->m_impl->SetFileType("text/html"); - fileType->m_impl->SetExt(ext); - return fileType; - } - else if ( ext == "gif" ) - { - wxFileType *fileType = new wxFileType; - fileType->m_impl->SetFileType("image/gif"); - fileType->m_impl->SetExt(ext); - return fileType; - } - else if ( ext == "png" ) - { - wxFileType *fileType = new wxFileType; - fileType->m_impl->SetFileType("image/png"); - fileType->m_impl->SetExt(ext); - return fileType; - } - else if ( ext == "jpg" || ext == "jpeg" ) - { - wxFileType *fileType = new wxFileType; - fileType->m_impl->SetFileType("image/jpeg"); - fileType->m_impl->SetExt(ext); - return fileType; - } - else if ( ext == "bmp" ) - { - wxFileType *fileType = new wxFileType; - fileType->m_impl->SetFileType("image/bmp"); - fileType->m_impl->SetExt(ext); - return fileType; - } - else if ( ext == "tif" || ext == "tiff" ) - { - wxFileType *fileType = new wxFileType; - fileType->m_impl->SetFileType("image/tiff"); - fileType->m_impl->SetExt(ext); - return fileType; - } - else if ( ext == "xpm" ) - { - wxFileType *fileType = new wxFileType; - fileType->m_impl->SetFileType("image/xpm"); - fileType->m_impl->SetExt(ext); - return fileType; - } - else if ( ext == "xbm" ) - { - wxFileType *fileType = new wxFileType; - fileType->m_impl->SetFileType("image/xbm"); - fileType->m_impl->SetExt(ext); - return fileType; - } - - // unknown extension - return NULL; -} - -// MIME type -> extension -> file type -wxFileType * -wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString& mimeType) -{ - return NULL; -} - -size_t wxMimeTypesManagerImpl::EnumAllFileTypes(wxArrayString& mimetypes) -{ - wxFAIL_MSG( _T("TODO") ); // VZ: don't know anything about this for Mac - - return 0; -} - -#else // Unix - -// ============================================================================ -// Unix implementation -// ============================================================================ // ---------------------------------------------------------------------------- // various statics @@ -2428,11 +1456,6 @@ size_t wxMimeTypesManagerImpl::EnumAllFileTypes(wxArrayString& mimetypes) return mimetypes.GetCount(); } -#endif - // OS type - #endif // wxUSE_FILE && wxUSE_TEXTFILE -#endif - // __WIN16__ -- 2.45.2