wxMimeTypesManager::AddFallbacks() added, also corrected a minor bug/incompatible
behaviour in wxFileType and added a MIME database query demo to typetest sample git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@3312 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -70,7 +70,8 @@ default ones which are loaded automatically) containing MIME
|
|||||||
information in either mailcap(5) or mime.types(5) format.
|
information in either mailcap(5) or mime.types(5) format.
|
||||||
|
|
||||||
\helpref{ReadMailcap}{wxmimetypesmanagerreadmailcap}\\
|
\helpref{ReadMailcap}{wxmimetypesmanagerreadmailcap}\\
|
||||||
\helpref{ReadMimeTypes}{wxmimetypesmanagerreadmimetypes}
|
\helpref{ReadMimeTypes}{wxmimetypesmanagerreadmimetypes}\\
|
||||||
|
\helpref{AddFallbacks}{wxmimetypesmanageraddfallbacks}
|
||||||
|
|
||||||
%%%%% MEMBERS HERE %%%%%
|
%%%%% MEMBERS HERE %%%%%
|
||||||
\helponly{\insertatlevel{2}{
|
\helponly{\insertatlevel{2}{
|
||||||
@@ -93,6 +94,17 @@ additional mailcap/mime.types files.
|
|||||||
|
|
||||||
Destructor is not virtual, so this class should not be derived from.
|
Destructor is not virtual, so this class should not be derived from.
|
||||||
|
|
||||||
|
\membersection{wxMimeTypesManager::AddFallbacks}\label{wxmimetypesmanageraddfallbacks}
|
||||||
|
|
||||||
|
\func{void}{AddFallbacks}{\param{const wxFileTypeInfo *}{fallbacks}}
|
||||||
|
|
||||||
|
This function may be used to provdie hard-wired fallbacks for the MIME types
|
||||||
|
and extensions that might not be present in the system MIME database.
|
||||||
|
|
||||||
|
% TODO
|
||||||
|
|
||||||
|
Please see the typetest sample for an example of using it.
|
||||||
|
|
||||||
\membersection{wxMimeTypesManager::GetFileTypeFromExtension}\label{wxmimetypesmanagergetfiletypefromextension}
|
\membersection{wxMimeTypesManager::GetFileTypeFromExtension}\label{wxmimetypesmanagergetfiletypefromextension}
|
||||||
|
|
||||||
\func{wxFileType*}{GetFileTypeFromExtension}{\param{const wxString\&}{ extension}}
|
\func{wxFileType*}{GetFileTypeFromExtension}{\param{const wxString\&}{ extension}}
|
||||||
|
@@ -127,6 +127,18 @@ public:
|
|||||||
|
|
||||||
bool IsValid() const { return !m_mimeType.IsEmpty(); }
|
bool IsValid() const { return !m_mimeType.IsEmpty(); }
|
||||||
|
|
||||||
|
// accessors
|
||||||
|
// get the MIME type
|
||||||
|
const wxString& GetMimeType() const { return m_mimeType; }
|
||||||
|
// get the open command
|
||||||
|
const wxString& GetOpenCommand() const { return m_openCmd; }
|
||||||
|
// get the print command
|
||||||
|
const wxString& GetPrintCommand() const { return m_printCmd; }
|
||||||
|
// get the description
|
||||||
|
const wxString& GetDescription() const { return m_desc; }
|
||||||
|
// get the array of all extensions
|
||||||
|
const wxArrayString& GetExtensions() const { return m_exts; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
wxString m_mimeType, // the MIME type in "type/subtype" form
|
wxString m_mimeType, // the MIME type in "type/subtype" form
|
||||||
m_openCmd, // command to use for opening the file (%s allowed)
|
m_openCmd, // command to use for opening the file (%s allowed)
|
||||||
@@ -187,7 +199,7 @@ public:
|
|||||||
// function.
|
// function.
|
||||||
//
|
//
|
||||||
// The filetypes array should be terminated by a NULL entry
|
// The filetypes array should be terminated by a NULL entry
|
||||||
bool AddFallbacks(const wxFileTypeInfo *filetypes);
|
void AddFallbacks(const wxFileTypeInfo *filetypes);
|
||||||
|
|
||||||
// dtor (not virtual, shouldn't be derived from)
|
// dtor (not virtual, shouldn't be derived from)
|
||||||
~wxMimeTypesManager();
|
~wxMimeTypesManager();
|
||||||
|
@@ -27,6 +27,7 @@
|
|||||||
#include "wx/time.h"
|
#include "wx/time.h"
|
||||||
#include "wx/date.h"
|
#include "wx/date.h"
|
||||||
#include "wx/variant.h"
|
#include "wx/variant.h"
|
||||||
|
#include "wx/mimetype.h"
|
||||||
|
|
||||||
#include "typetest.h"
|
#include "typetest.h"
|
||||||
|
|
||||||
@@ -60,9 +61,10 @@ BEGIN_EVENT_TABLE(MyApp, wxApp)
|
|||||||
EVT_MENU(TYPES_UNICODE, MyApp::DoUnicodeDemo)
|
EVT_MENU(TYPES_UNICODE, MyApp::DoUnicodeDemo)
|
||||||
#endif
|
#endif
|
||||||
EVT_MENU(TYPES_STREAM, MyApp::DoStreamDemo)
|
EVT_MENU(TYPES_STREAM, MyApp::DoStreamDemo)
|
||||||
|
EVT_MENU(TYPES_MIME, MyApp::DoMIMEDemo)
|
||||||
END_EVENT_TABLE()
|
END_EVENT_TABLE()
|
||||||
|
|
||||||
bool MyApp::OnInit(void)
|
bool MyApp::OnInit()
|
||||||
{
|
{
|
||||||
// Create the main frame window
|
// Create the main frame window
|
||||||
MyFrame *frame = new MyFrame((wxFrame *) NULL, "wxWindows Types Demo",
|
MyFrame *frame = new MyFrame((wxFrame *) NULL, "wxWindows Types Demo",
|
||||||
@@ -76,18 +78,23 @@ bool MyApp::OnInit(void)
|
|||||||
|
|
||||||
file_menu->Append(TYPES_ABOUT, "&About");
|
file_menu->Append(TYPES_ABOUT, "&About");
|
||||||
file_menu->AppendSeparator();
|
file_menu->AppendSeparator();
|
||||||
file_menu->Append(TYPES_DATE, "&Date test");
|
file_menu->Append(TYPES_QUIT, "E&xit\tAlt-X");
|
||||||
file_menu->Append(TYPES_TIME, "&Time test");
|
|
||||||
file_menu->Append(TYPES_VARIANT, "&Variant test");
|
wxMenu *test_menu = new wxMenu;
|
||||||
file_menu->Append(TYPES_BYTEORDER, "&Byteorder test");
|
test_menu->Append(TYPES_DATE, "&Date test");
|
||||||
|
test_menu->Append(TYPES_TIME, "&Time test");
|
||||||
|
test_menu->Append(TYPES_VARIANT, "&Variant test");
|
||||||
|
test_menu->Append(TYPES_BYTEORDER, "&Byteorder test");
|
||||||
#if wxUSE_UNICODE
|
#if wxUSE_UNICODE
|
||||||
file_menu->Append(TYPES_UNICODE, "&Unicode test");
|
test_menu->Append(TYPES_UNICODE, "&Unicode test");
|
||||||
#endif
|
#endif
|
||||||
file_menu->Append(TYPES_STREAM, "&Stream test");
|
test_menu->Append(TYPES_STREAM, "&Stream test");
|
||||||
file_menu->AppendSeparator();
|
test_menu->AppendSeparator();
|
||||||
file_menu->Append(TYPES_QUIT, "E&xit");
|
test_menu->Append(TYPES_MIME, "&MIME database test");
|
||||||
|
|
||||||
wxMenuBar *menu_bar = new wxMenuBar;
|
wxMenuBar *menu_bar = new wxMenuBar;
|
||||||
menu_bar->Append(file_menu, "&File");
|
menu_bar->Append(file_menu, "&File");
|
||||||
|
menu_bar->Append(test_menu, "&Tests");
|
||||||
frame->SetMenuBar(menu_bar);
|
frame->SetMenuBar(menu_bar);
|
||||||
|
|
||||||
m_textCtrl = new wxTextCtrl(frame, -1, "", wxPoint(0, 0), wxDefaultSize, wxTE_MULTILINE);
|
m_textCtrl = new wxTextCtrl(frame, -1, "", wxPoint(0, 0), wxDefaultSize, wxTE_MULTILINE);
|
||||||
@@ -133,7 +140,7 @@ void MyApp::DoStreamDemo(wxCommandEvent& WXUNUSED(event))
|
|||||||
text_output << d << "\n";
|
text_output << d << "\n";
|
||||||
std_file_output << d << "\n";
|
std_file_output << d << "\n";
|
||||||
|
|
||||||
float f = 0.00001;
|
float f = (float)0.00001;
|
||||||
tmp.Printf( _T("Float: %f\n"), f );
|
tmp.Printf( _T("Float: %f\n"), f );
|
||||||
textCtrl.WriteText( tmp );
|
textCtrl.WriteText( tmp );
|
||||||
text_output << f << "\n";
|
text_output << f << "\n";
|
||||||
@@ -205,7 +212,7 @@ void MyApp::DoStreamDemo(wxCommandEvent& WXUNUSED(event))
|
|||||||
file_output.SeekO( 0 );
|
file_output.SeekO( 0 );
|
||||||
wxDataOutputStream data_output( buf_output );
|
wxDataOutputStream data_output( buf_output );
|
||||||
|
|
||||||
wxInt16 i16 = 0xFFFF;
|
wxInt16 i16 = (short)0xFFFF;
|
||||||
tmp.Printf( _T("Signed int16: %d\n"), (int)i16 );
|
tmp.Printf( _T("Signed int16: %d\n"), (int)i16 );
|
||||||
textCtrl.WriteText( tmp );
|
textCtrl.WriteText( tmp );
|
||||||
data_output.Write16( i16 );
|
data_output.Write16( i16 );
|
||||||
@@ -275,6 +282,74 @@ void MyApp::DoUnicodeDemo(wxCommandEvent& WXUNUSED(event))
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
void MyApp::DoMIMEDemo(wxCommandEvent& WXUNUSED(event))
|
||||||
|
{
|
||||||
|
static wxString s_defaultExt = "xyz";
|
||||||
|
|
||||||
|
wxString ext = wxGetTextFromUser("Enter a file extension: ",
|
||||||
|
"MIME database test",
|
||||||
|
s_defaultExt);
|
||||||
|
if ( !!ext )
|
||||||
|
{
|
||||||
|
s_defaultExt = ext;
|
||||||
|
|
||||||
|
// init MIME database if not done yet
|
||||||
|
if ( !m_mimeDatabase )
|
||||||
|
{
|
||||||
|
m_mimeDatabase = new wxMimeTypesManager;
|
||||||
|
|
||||||
|
static const wxFileTypeInfo fallbacks[] =
|
||||||
|
{
|
||||||
|
wxFileTypeInfo("application/xyz",
|
||||||
|
"XyZ %s",
|
||||||
|
"XyZ -p %s",
|
||||||
|
"The one and only XYZ format file",
|
||||||
|
"xyz", "123", NULL),
|
||||||
|
wxFileTypeInfo("text/html",
|
||||||
|
"lynx %s",
|
||||||
|
"lynx -dump %s | lpr",
|
||||||
|
"HTML document (from fallback)",
|
||||||
|
"htm", "html", NULL),
|
||||||
|
|
||||||
|
// must terminate the table with this!
|
||||||
|
wxFileTypeInfo()
|
||||||
|
};
|
||||||
|
|
||||||
|
m_mimeDatabase->AddFallbacks(fallbacks);
|
||||||
|
}
|
||||||
|
|
||||||
|
wxTextCtrl& textCtrl = * GetTextCtrl();
|
||||||
|
|
||||||
|
wxFileType *filetype = m_mimeDatabase->GetFileTypeFromExtension(ext);
|
||||||
|
if ( !filetype )
|
||||||
|
{
|
||||||
|
textCtrl << "Unknown extension '" << ext << "'\n";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
wxString type, desc, open;
|
||||||
|
filetype->GetMimeType(&type);
|
||||||
|
filetype->GetDescription(&desc);
|
||||||
|
|
||||||
|
wxString filename = "filename";
|
||||||
|
filename << "." << ext;
|
||||||
|
wxFileType::MessageParameters params(filename, type);
|
||||||
|
filetype->GetOpenCommand(&open, params);
|
||||||
|
|
||||||
|
textCtrl << "MIME information about extension '" << ext << "'\n"
|
||||||
|
<< "\tMIME type: " << ( !type ? "unknown"
|
||||||
|
: type.c_str() ) << '\n'
|
||||||
|
<< "\tDescription: " << ( !desc ? "" : desc.c_str() )
|
||||||
|
<< '\n'
|
||||||
|
<< "\tCommand to open: " << ( !open ? "no" : open.c_str() )
|
||||||
|
<< '\n';
|
||||||
|
|
||||||
|
delete filetype;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//else: cancelled by user
|
||||||
|
}
|
||||||
|
|
||||||
void MyApp::DoByteOrderDemo(wxCommandEvent& WXUNUSED(event))
|
void MyApp::DoByteOrderDemo(wxCommandEvent& WXUNUSED(event))
|
||||||
{
|
{
|
||||||
wxTextCtrl& textCtrl = * GetTextCtrl();
|
wxTextCtrl& textCtrl = * GetTextCtrl();
|
||||||
@@ -346,7 +421,6 @@ void MyApp::DoDateDemo(wxCommandEvent& WXUNUSED(event))
|
|||||||
a = a - 25;
|
a = a - 25;
|
||||||
textCtrl << a.FormatDate(wxEUROPEAN) << " (European)\n";
|
textCtrl << a.FormatDate(wxEUROPEAN) << " (European)\n";
|
||||||
|
|
||||||
|
|
||||||
// Using subtraction of two date objects
|
// Using subtraction of two date objects
|
||||||
wxDate a1 = wxString("7/13/1991");
|
wxDate a1 = wxString("7/13/1991");
|
||||||
wxDate a2 = a1 + 14;
|
wxDate a2 = a1 + 14;
|
||||||
@@ -481,6 +555,12 @@ void MyApp::DoVariantDemo(wxCommandEvent& WXUNUSED(event) )
|
|||||||
// Implicit conversion
|
// Implicit conversion
|
||||||
long l = var1;
|
long l = var1;
|
||||||
|
|
||||||
|
// suppress compile warnings about unused variables
|
||||||
|
if ( l < v )
|
||||||
|
{
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
wxStringList stringList;
|
wxStringList stringList;
|
||||||
stringList.Add(_T("one")); stringList.Add(_T("two")); stringList.Add(_T("three"));
|
stringList.Add(_T("one")); stringList.Add(_T("two")); stringList.Add(_T("three"));
|
||||||
var1 = stringList;
|
var1 = stringList;
|
||||||
|
@@ -20,7 +20,10 @@
|
|||||||
class MyApp: public wxApp
|
class MyApp: public wxApp
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
bool OnInit(void);
|
MyApp() { m_textCtrl = NULL; m_mimeDatabase = NULL; }
|
||||||
|
|
||||||
|
bool OnInit();
|
||||||
|
int OnExit() { delete m_mimeDatabase; return wxApp::OnExit(); }
|
||||||
|
|
||||||
void DoDateDemo(wxCommandEvent& event);
|
void DoDateDemo(wxCommandEvent& event);
|
||||||
void DoTimeDemo(wxCommandEvent& event);
|
void DoTimeDemo(wxCommandEvent& event);
|
||||||
@@ -30,14 +33,16 @@ public:
|
|||||||
#if wxUSE_UNICODE
|
#if wxUSE_UNICODE
|
||||||
void DoUnicodeDemo(wxCommandEvent& event);
|
void DoUnicodeDemo(wxCommandEvent& event);
|
||||||
#endif
|
#endif
|
||||||
|
void DoMIMEDemo(wxCommandEvent& event);
|
||||||
|
|
||||||
wxTextCtrl* GetTextCtrl() const { return m_textCtrl; }
|
wxTextCtrl* GetTextCtrl() const { return m_textCtrl; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
wxTextCtrl* m_textCtrl;
|
wxTextCtrl* m_textCtrl;
|
||||||
|
wxMimeTypesManager *m_mimeDatabase;
|
||||||
|
|
||||||
DECLARE_DYNAMIC_CLASS(MyApp)
|
DECLARE_DYNAMIC_CLASS(MyApp)
|
||||||
DECLARE_EVENT_TABLE()
|
DECLARE_EVENT_TABLE()
|
||||||
};
|
};
|
||||||
|
|
||||||
DECLARE_APP(MyApp)
|
DECLARE_APP(MyApp)
|
||||||
@@ -54,20 +59,23 @@ public:
|
|||||||
void OnAbout(wxCommandEvent& event);
|
void OnAbout(wxCommandEvent& event);
|
||||||
|
|
||||||
DECLARE_EVENT_TABLE()
|
DECLARE_EVENT_TABLE()
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// ID for the menu commands
|
// ID for the menu commands
|
||||||
#define TYPES_QUIT wxID_EXIT
|
enum
|
||||||
#define TYPES_TEXT 101
|
{
|
||||||
#define TYPES_ABOUT 102
|
TYPES_QUIT = wxID_EXIT,
|
||||||
|
TYPES_TEXT = 101,
|
||||||
|
TYPES_ABOUT,
|
||||||
|
|
||||||
#define TYPES_DATE 103
|
TYPES_DATE,
|
||||||
#define TYPES_TIME 104
|
TYPES_TIME,
|
||||||
#define TYPES_VARIANT 105
|
TYPES_VARIANT,
|
||||||
#define TYPES_BYTEORDER 106
|
TYPES_BYTEORDER,
|
||||||
#define TYPES_UNICODE 107
|
TYPES_UNICODE,
|
||||||
#define TYPES_STREAM 108
|
TYPES_STREAM,
|
||||||
|
TYPES_MIME
|
||||||
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
// _WX_TYPETEST_H_
|
// _WX_TYPETEST_H_
|
||||||
|
@@ -83,13 +83,20 @@ class wxFileTypeImpl
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
// ctor
|
// ctor
|
||||||
wxFileTypeImpl() { }
|
wxFileTypeImpl() { m_info = NULL; }
|
||||||
|
|
||||||
// initialize us with our file type name
|
// one of these Init() function must be called (ctor can't take any
|
||||||
void SetFileType(const wxString& strFileType)
|
// arguments because it's common)
|
||||||
{ m_strFileType = strFileType; }
|
|
||||||
void SetExt(const wxString& ext)
|
// initialize us with our file type name and extension - in this case
|
||||||
{ m_ext = ext; }
|
// 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
|
// implement accessor functions
|
||||||
bool GetExtensions(wxArrayString& extensions);
|
bool GetExtensions(wxArrayString& extensions);
|
||||||
@@ -97,19 +104,25 @@ public:
|
|||||||
bool GetIcon(wxIcon *icon) const;
|
bool GetIcon(wxIcon *icon) const;
|
||||||
bool GetDescription(wxString *desc) const;
|
bool GetDescription(wxString *desc) const;
|
||||||
bool GetOpenCommand(wxString *openCmd,
|
bool GetOpenCommand(wxString *openCmd,
|
||||||
const wxFileType::MessageParameters&) const
|
const wxFileType::MessageParameters& params) const;
|
||||||
{ return GetCommand(openCmd, _T("open")); }
|
|
||||||
bool GetPrintCommand(wxString *printCmd,
|
bool GetPrintCommand(wxString *printCmd,
|
||||||
const wxFileType::MessageParameters&) const
|
const wxFileType::MessageParameters& params) const;
|
||||||
{ return GetCommand(printCmd, _T("print")); }
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// helper function
|
// helper function: reads the command corresponding to the specified verb
|
||||||
bool GetCommand(wxString *command, const wxChar *verb) const;
|
// from the registry (returns an empty string if not found)
|
||||||
|
wxString GetCommand(const wxChar *verb) const;
|
||||||
|
|
||||||
wxString m_strFileType, m_ext;
|
// we use either m_info or read the data from the registry if m_info == NULL
|
||||||
|
const wxFileTypeInfo *m_info;
|
||||||
|
wxString m_strFileType,
|
||||||
|
m_ext;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
WX_DECLARE_OBJARRAY(wxFileTypeInfo, wxArrayFileTypeInfo);
|
||||||
|
#include "wx/arrimpl.cpp"
|
||||||
|
WX_DEFINE_OBJARRAY(wxArrayFileTypeInfo);
|
||||||
|
|
||||||
class wxMimeTypesManagerImpl
|
class wxMimeTypesManagerImpl
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -126,6 +139,11 @@ public:
|
|||||||
{ return TRUE; }
|
{ return TRUE; }
|
||||||
bool ReadMimeTypes(const wxString& filename)
|
bool ReadMimeTypes(const wxString& filename)
|
||||||
{ return TRUE; }
|
{ return TRUE; }
|
||||||
|
|
||||||
|
void AddFallback(const wxFileTypeInfo& ft) { m_fallbacks.Add(ft); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
wxArrayFileTypeInfo m_fallbacks;
|
||||||
};
|
};
|
||||||
|
|
||||||
#else // Unix
|
#else // Unix
|
||||||
@@ -278,6 +296,18 @@ public:
|
|||||||
bool ReadMailcap(const wxString& filename, bool fallback = FALSE);
|
bool ReadMailcap(const wxString& filename, bool fallback = FALSE);
|
||||||
bool ReadMimeTypes(const wxString& filename);
|
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
|
// accessors
|
||||||
// get the string containing space separated extensions for the given
|
// get the string containing space separated extensions for the given
|
||||||
// file type
|
// file type
|
||||||
@@ -333,6 +363,42 @@ private:
|
|||||||
|
|
||||||
#endif // OS type
|
#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
|
// implementation of the wrapper classes
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
@@ -518,13 +584,20 @@ bool wxMimeTypesManager::ReadMimeTypes(const wxString& filename)
|
|||||||
return m_impl->ReadMimeTypes(filename);
|
return m_impl->ReadMimeTypes(filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void wxMimeTypesManager::AddFallbacks(const wxFileTypeInfo *filetypes)
|
||||||
|
{
|
||||||
|
for ( const wxFileTypeInfo *ft = filetypes; ft->IsValid(); ft++ ) {
|
||||||
|
m_impl->AddFallback(*ft);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
// real (OS specific) implementation
|
// real (OS specific) implementation
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
|
||||||
#ifdef __WXMSW__
|
#ifdef __WXMSW__
|
||||||
|
|
||||||
bool wxFileTypeImpl::GetCommand(wxString *command, const wxChar *verb) const
|
wxString wxFileTypeImpl::GetCommand(const wxChar *verb) const
|
||||||
{
|
{
|
||||||
// suppress possible error messages
|
// suppress possible error messages
|
||||||
wxLogNull nolog;
|
wxLogNull nolog;
|
||||||
@@ -532,40 +605,84 @@ bool wxFileTypeImpl::GetCommand(wxString *command, const wxChar *verb) const
|
|||||||
strKey << m_strFileType << _T("\\shell\\") << verb << _T("\\command");
|
strKey << m_strFileType << _T("\\shell\\") << verb << _T("\\command");
|
||||||
wxRegKey key(wxRegKey::HKCR, strKey);
|
wxRegKey key(wxRegKey::HKCR, strKey);
|
||||||
|
|
||||||
|
wxString command;
|
||||||
if ( key.Open() ) {
|
if ( key.Open() ) {
|
||||||
// it's the default value of the key
|
// it's the default value of the key
|
||||||
if ( key.QueryValue(_T(""), *command) ) {
|
if ( key.QueryValue(_T(""), command) ) {
|
||||||
// transform it from '%1' to '%s' style format string
|
// transform it from '%1' to '%s' style format string
|
||||||
|
|
||||||
// NB: we don't make any attempt to verify that the string is valid,
|
// 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
|
// i.e. doesn't contain %2, or second %1 or .... But we do make
|
||||||
// sure that we return a string with _exactly_ one '%s'!
|
// sure that we return a string with _exactly_ one '%s'!
|
||||||
size_t len = command->Len();
|
bool foundFilename = FALSE;
|
||||||
for ( size_t n = 0; n < len; n++ ) {
|
size_t len = command.Len();
|
||||||
if ( command->GetChar(n) == _T('%') &&
|
for ( size_t n = 0; (n < len) && !foundFilename; n++ ) {
|
||||||
(n + 1 < len) && command->GetChar(n + 1) == _T('1') ) {
|
if ( command[n] == _T('%') &&
|
||||||
|
(n + 1 < len) && command[n + 1] == _T('1') ) {
|
||||||
// replace it with '%s'
|
// replace it with '%s'
|
||||||
command->SetChar(n + 1, _T('s'));
|
command[n + 1] = _T('s');
|
||||||
|
|
||||||
return TRUE;
|
foundFilename = TRUE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( !foundFilename ) {
|
||||||
// we didn't find any '%1'!
|
// we didn't find any '%1'!
|
||||||
// HACK: append the filename at the end, hope that it will do
|
// HACK: append the filename at the end, hope that it will do
|
||||||
*command << _T(" %s");
|
command << _T(" %s");
|
||||||
|
}
|
||||||
return TRUE;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// no such file type or no value
|
// no such file type or no value
|
||||||
return FALSE;
|
return command;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
wxFileTypeImpl::GetOpenCommand(wxString *openCmd,
|
||||||
|
const wxFileType::MessageParameters& params)
|
||||||
|
const
|
||||||
|
{
|
||||||
|
wxString cmd;
|
||||||
|
if ( m_info ) {
|
||||||
|
cmd = m_info->GetOpenCommand();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
cmd = GetCommand(_T("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(_T("print"));
|
||||||
|
}
|
||||||
|
|
||||||
|
*printCmd = wxFileType::ExpandCommand(cmd, params);
|
||||||
|
|
||||||
|
return !printCmd->IsEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO this function is half implemented
|
// TODO this function is half implemented
|
||||||
bool wxFileTypeImpl::GetExtensions(wxArrayString& extensions)
|
bool wxFileTypeImpl::GetExtensions(wxArrayString& extensions)
|
||||||
{
|
{
|
||||||
if ( m_ext.IsEmpty() ) {
|
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
|
// the only way to get the list of extensions from the file type is to
|
||||||
// scan through all extensions in the registry - too slow...
|
// scan through all extensions in the registry - too slow...
|
||||||
return FALSE;
|
return FALSE;
|
||||||
@@ -581,6 +698,13 @@ bool wxFileTypeImpl::GetExtensions(wxArrayString& extensions)
|
|||||||
|
|
||||||
bool wxFileTypeImpl::GetMimeType(wxString *mimeType) const
|
bool wxFileTypeImpl::GetMimeType(wxString *mimeType) const
|
||||||
{
|
{
|
||||||
|
if ( m_info ) {
|
||||||
|
// we already have it
|
||||||
|
*mimeType = m_info->GetMimeType();
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
// suppress possible error messages
|
// suppress possible error messages
|
||||||
wxLogNull nolog;
|
wxLogNull nolog;
|
||||||
wxRegKey key(wxRegKey::HKCR, /*m_strFileType*/ _T(".") + m_ext);
|
wxRegKey key(wxRegKey::HKCR, /*m_strFileType*/ _T(".") + m_ext);
|
||||||
@@ -594,6 +718,11 @@ bool wxFileTypeImpl::GetMimeType(wxString *mimeType) const
|
|||||||
|
|
||||||
bool wxFileTypeImpl::GetIcon(wxIcon *icon) const
|
bool wxFileTypeImpl::GetIcon(wxIcon *icon) const
|
||||||
{
|
{
|
||||||
|
if ( m_info ) {
|
||||||
|
// we don't have icons in the fallback resources
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
wxString strIconKey;
|
wxString strIconKey;
|
||||||
strIconKey << m_strFileType << _T("\\DefaultIcon");
|
strIconKey << m_strFileType << _T("\\DefaultIcon");
|
||||||
|
|
||||||
@@ -642,6 +771,13 @@ bool wxFileTypeImpl::GetIcon(wxIcon *icon) const
|
|||||||
|
|
||||||
bool wxFileTypeImpl::GetDescription(wxString *desc) const
|
bool wxFileTypeImpl::GetDescription(wxString *desc) const
|
||||||
{
|
{
|
||||||
|
if ( m_info ) {
|
||||||
|
// we already have it
|
||||||
|
*desc = m_info->GetDescription();
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
// suppress possible error messages
|
// suppress possible error messages
|
||||||
wxLogNull nolog;
|
wxLogNull nolog;
|
||||||
wxRegKey key(wxRegKey::HKCR, m_strFileType);
|
wxRegKey key(wxRegKey::HKCR, m_strFileType);
|
||||||
@@ -677,8 +813,20 @@ wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString& ext)
|
|||||||
if ( key.QueryValue(_T(""), strFileType) ) {
|
if ( key.QueryValue(_T(""), strFileType) ) {
|
||||||
// create the new wxFileType object
|
// create the new wxFileType object
|
||||||
wxFileType *fileType = new wxFileType;
|
wxFileType *fileType = new wxFileType;
|
||||||
fileType->m_impl->SetFileType(strFileType);
|
fileType->m_impl->Init(strFileType, ext);
|
||||||
fileType->m_impl->SetExt(ext);
|
|
||||||
|
return fileType;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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;
|
return fileType;
|
||||||
}
|
}
|
||||||
@@ -710,6 +858,20 @@ wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString& mimeType)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
// unknown MIME type
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -903,6 +1065,77 @@ wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString& mimeType)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void wxMimeTypesManagerImpl::AddFallback(const wxFileTypeInfo& filetype)
|
||||||
|
{
|
||||||
|
const wxArrayString& exts = filetype.GetExtensions();
|
||||||
|
size_t nExts = exts.GetCount();
|
||||||
|
for ( size_t nExt = 0; nExt < nExts; nExt++ ) {
|
||||||
|
if ( nExt > 0 ) {
|
||||||
|
extensions += _T(' ');
|
||||||
|
}
|
||||||
|
extensions += exts[nExt];
|
||||||
|
}
|
||||||
|
|
||||||
|
AddMimeTypeInfo(filetype.GetMimeType(),
|
||||||
|
extensions,
|
||||||
|
filetype.GetDescription());
|
||||||
|
|
||||||
|
AddMailcapInfo(filetype.GetMimeType(),
|
||||||
|
filetype.GetOpenCommand(),
|
||||||
|
filetype.GetPrintCommand(),
|
||||||
|
_T(""),
|
||||||
|
filetype.GetDescription());
|
||||||
|
}
|
||||||
|
|
||||||
|
void wxMimeTypesManagerImpl::AddMimeTypeInfo(const wxString& strMimeType,
|
||||||
|
const wxString& strExtensions,
|
||||||
|
const wxString& strDesc)
|
||||||
|
{
|
||||||
|
int index = m_aTypes.Index(strMimeType);
|
||||||
|
if ( index == wxNOT_FOUND ) {
|
||||||
|
// add a new entry
|
||||||
|
m_aTypes.Add(strMimeType);
|
||||||
|
m_aEntries.Add(NULL);
|
||||||
|
m_aExtensions.Add(strExtensions);
|
||||||
|
m_aDescriptions.Add(strDesc);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// modify an existing one
|
||||||
|
if ( !strDesc.IsEmpty() ) {
|
||||||
|
m_aDescriptions[index] = strDesc; // replace old value
|
||||||
|
}
|
||||||
|
m_aExtensions[index] += strExtensions;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void wxMimeTypesManagerImpl::AddMailcapInfo(const wxString& strType,
|
||||||
|
const wxString& strOpenCmd,
|
||||||
|
const wxString& strPrintCmd,
|
||||||
|
const wxString& strTest,
|
||||||
|
const wxString& strDesc)
|
||||||
|
{
|
||||||
|
MailCapEntry *entry = new MailCapEntry(strOpenCmd, strPrintCmd, strTest);
|
||||||
|
|
||||||
|
int nIndex = m_aTypes.Index(strType);
|
||||||
|
if ( nIndex == wxNOT_FOUND ) {
|
||||||
|
// new file type
|
||||||
|
m_aTypes.Add(strType);
|
||||||
|
|
||||||
|
m_aEntries.Add(entry);
|
||||||
|
m_aExtensions.Add(_T(""));
|
||||||
|
m_aDescriptions.Add(strDesc);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// always append the entry in the tail of the list - info added with
|
||||||
|
// this function can only come from AddFallbacks()
|
||||||
|
MailCapEntry *entryOld = m_aEntries[nIndex];
|
||||||
|
if ( entryOld )
|
||||||
|
entry->Append(entryOld);
|
||||||
|
else
|
||||||
|
m_aEntries[nIndex] = entry;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool wxMimeTypesManagerImpl::ReadMimeTypes(const wxString& strFileName)
|
bool wxMimeTypesManagerImpl::ReadMimeTypes(const wxString& strFileName)
|
||||||
{
|
{
|
||||||
wxLogTrace(_T("--- Parsing mime.types file '%s' ---"), strFileName.c_str());
|
wxLogTrace(_T("--- Parsing mime.types file '%s' ---"), strFileName.c_str());
|
||||||
@@ -1042,21 +1275,7 @@ bool wxMimeTypesManagerImpl::ReadMimeTypes(const wxString& strFileName)
|
|||||||
strExtensions.erase(0, 1);
|
strExtensions.erase(0, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
int index = m_aTypes.Index(strMimeType);
|
AddMimeTypeInfo(strMimeType, strExtensions, strDesc);
|
||||||
if ( index == wxNOT_FOUND ) {
|
|
||||||
// add a new entry
|
|
||||||
m_aTypes.Add(strMimeType);
|
|
||||||
m_aEntries.Add(NULL);
|
|
||||||
m_aExtensions.Add(strExtensions);
|
|
||||||
m_aDescriptions.Add(strDesc);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// modify an existing one
|
|
||||||
if ( !strDesc.IsEmpty() ) {
|
|
||||||
m_aDescriptions[index] = strDesc; // replace old value
|
|
||||||
}
|
|
||||||
m_aExtensions[index] += strExtensions;
|
|
||||||
}
|
|
||||||
|
|
||||||
// finished with this line
|
// finished with this line
|
||||||
pc = NULL;
|
pc = NULL;
|
||||||
@@ -1269,6 +1488,8 @@ bool wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName,
|
|||||||
strPrintCmd,
|
strPrintCmd,
|
||||||
strTest);
|
strTest);
|
||||||
|
|
||||||
|
// NB: because of complications below (we must get entries priority
|
||||||
|
// right), we can't use AddMailcapInfo() here, unfortunately.
|
||||||
strType.MakeLower();
|
strType.MakeLower();
|
||||||
int nIndex = m_aTypes.Index(strType);
|
int nIndex = m_aTypes.Index(strType);
|
||||||
if ( nIndex == wxNOT_FOUND ) {
|
if ( nIndex == wxNOT_FOUND ) {
|
||||||
|
Reference in New Issue
Block a user