// private classes
// ----------------------------------------------------------------------------
-
-// this class uses both mailcap and mime.types to gather information about file
+// This class uses both mailcap and mime.types to gather information about file
// types.
//
-// The information about mailcap file was extracted from metamail(1) sources and
-// documentation.
+// The information about mailcap file was extracted from metamail(1) sources
+// and documentation and subsequently revised when I found the RFC 1524
+// describing it.
//
// Format of mailcap file: spaces are ignored, each line is either a comment
// (starts with '#') or a line of the form <field1>;<field2>;...;<fieldN>.
// * print=xxx is the command to be used to print (and not view) the data of
// this type (parameter/filename expansion is done here too)
// * edit=xxx is the command to open/edit the data of this type
-// * needsterminal means that a new console must be created for the viewer
+// * needsterminal means that a new interactive console must be created for
+// the viewer
// * copiousoutput means that the viewer doesn't interact with the user but
// produces (possibly) a lof of lines of output on stdout (i.e. "cat" is a
// good example), thus it might be a good idea to use some kind of paging
// * compose and composetyped fields are used to determine the program to be
// called to create a new message pert in the specified format (unused).
//
-// Parameter/filename xpansion:
+// Parameter/filename expansion:
// * %s is replaced with the (full) file name
// * %t is replaced with MIME type/subtype of the entry
// * for multipart type only %n is replaced with the nnumber of parts and %F is
// * %{parameter} is replaced with the value of parameter taken from
// Content-type header line of the message.
//
-// FIXME any docs with real descriptions of these files??
//
// There are 2 possible formats for mime.types file, one entry per line (used
-// for global mime.types) and "expanded" format where an entry takes multiple
-// lines (used for users mime.types).
+// for global mime.types and called Mosaic format) and "expanded" format where
+// an entry takes multiple lines (used for users mime.types and called
+// Netscape format).
//
// For both formats spaces are ignored and lines starting with a '#' are
// comments. Each record has one of two following forms:
// a) for "brief" format:
// <mime type> <space separated list of extensions>
// b) for "expanded" format:
-// type=<mime type> \ desc="<description>" \ exts="ext"
+// type=<mime type> \
+// desc="<description>" \
+// exts="<comma separated list of extensions>"
//
// We try to autodetect the format of mime.types: if a non-comment line starts
// with "type=" we assume the second format, otherwise the first one.
*icon = icn;
#else
// helpful for testing in console mode
- wxLogDebug(_T("Found GNOME icon for '%s': '%s'\n"),
+ wxLogTrace(TRACE_MIME, _T("Found GNOME icon for '%s': '%s'\n"),
mimetype.c_str(), iconname.c_str());
#endif
if ( !file.ReadAll(&text) )
return;
- int pos;
- const wxChar *pc;
+ // first find the description string: it is the value in either "Comment="
+ // line or "Comment[<locale_name>]=" one
+ int posComment = wxNOT_FOUND;
- // before trying to find an icon, grab mimetype information
- // (because BFU's machine would hardly have well-edited mime.types but (s)he might
- // have edited it in control panel...)
+ wxString comment;
+#if wxUSE_INTL
+ wxLocale *locale = wxGetLocale();
+ if ( locale )
+ {
+ // try "Comment[locale name]" first
+ comment << _T("Comment[") + locale->GetName() + _T("]=");
- wxString mime_extension, mime_desc;
+ posComment = text.Find(comment);
+ }
+#endif // wxUSE_INTL
- pos = wxNOT_FOUND;
- if (wxGetLocale() != NULL)
- mime_desc = _T("Comment[") + wxGetLocale()->GetName() + _T("]=");
- if (pos == wxNOT_FOUND) mime_desc = _T("Comment=");
- pos = text.Find(mime_desc);
- if (pos == wxNOT_FOUND) mime_desc = wxEmptyString;
- else
+ if ( posComment == wxNOT_FOUND )
+ {
+ comment = _T("Comment=");
+
+ posComment = text.Find(comment);
+ }
+
+ wxString mime_desc;
+ if ( posComment != wxNOT_FOUND )
{
- pc = text.c_str() + pos + mime_desc.Length();
- mime_desc = wxEmptyString;
- while ( *pc && *pc != _T('\n') ) mime_desc += *pc++;
+ // found desc: it follows the comment until the end of line
+ const wxChar *pc = text.c_str() + posComment + comment.length();
+ while ( *pc && *pc != _T('\n') )
+ {
+ mime_desc += *pc++;
+ }
}
+ //else: no description
+
+ // next find the extensions
+ wxString mime_extension;
- pos = text.Find(_T("Patterns="));
- if (pos != wxNOT_FOUND)
+ int posExts = text.Find(_T("Patterns="));
+ if ( posExts != wxNOT_FOUND )
{
wxString exts;
- pc = text.c_str() + pos + 9;
- while ( *pc && *pc != _T('\n') ) exts += *pc++;
- wxStringTokenizer tokenizer(exts, _T(";"));
- wxString e;
+ const wxChar *pc = text.c_str() + posExts + 9; // strlen("Patterns=")
+ while ( *pc && *pc != _T('\n') )
+ {
+ exts += *pc++;
+ }
- while (tokenizer.HasMoreTokens())
+ wxStringTokenizer tokenizer(exts, _T(";"));
+ while ( tokenizer.HasMoreTokens() )
{
- e = tokenizer.GetNextToken();
- if (e.Left(2) != _T("*.")) continue; // don't support too difficult patterns
+ wxString e = tokenizer.GetNextToken();
+ if ( e.Left(2) != _T("*.") )
+ continue; // don't support too difficult patterns
+
+ if ( !mime_extension.empty() )
+ {
+ // separate from the previous ext
+ mime_extension << _T(' ');
+ }
+
mime_extension << e.Mid(2);
- mime_extension << _T(' ');
}
- mime_extension.RemoveLast();
}
ms_infoTypes.Add(mimetype);
// ok, now we can take care of icon:
- pos = text.Find(_T("Icon="));
- if ( pos == wxNOT_FOUND )
+ int posIcon = text.Find(_T("Icon="));
+ if ( posIcon == wxNOT_FOUND )
{
// no icon info
return;
wxString icon;
- pc = text.c_str() + pos + 5; // 5 == strlen("Icon=")
+ const wxChar *pc = text.c_str() + posIcon + 5; // 5 == strlen("Icon=")
while ( *pc && *pc != _T('\n') )
{
icon += *pc++;
*icon = icn;
#else
// helpful for testing in console mode
- wxLogDebug(_T("Found KDE icon for '%s': '%s'\n"),
+ wxLogTrace(TRACE_MIME, _T("Found KDE icon for '%s': '%s'\n"),
mimetype.c_str(), iconname.c_str());
#endif
return ms_iconHandlers;
}
-// read system and user mailcaps (TODO implement mime.types support)
wxMimeTypesManagerImpl::wxMimeTypesManagerImpl()
+{
+ m_initialized = FALSE;
+}
+
+// read system and user mailcaps and other files
+void wxMimeTypesManagerImpl::Initialize()
{
// directories where we look for mailcap and mime.types by default
// (taken from metamail(1) sources)
+ //
+ // although RFC 1524 specifies the search path of
+ // /etc/:/usr/etc:/usr/local/etc only, it doesn't hurt to search in more
+ // places - OTOH, the RFC also says that this path can be changed with
+ // MAILCAPS environment variable (containing the colon separated full
+ // filenames to try) which is not done yet (TODO?)
static const wxChar *aStandardLocations[] =
{
wxT("/etc"),
wxMimeTypesManagerImpl::~wxMimeTypesManagerImpl()
{
size_t cnt = m_aEntries.GetCount();
- for (size_t i = 0; i < cnt; i++) delete m_aEntries[i];
+ for (size_t i = 0; i < cnt; i++)
+ delete m_aEntries[i];
}
+wxFileType *
+wxMimeTypesManagerImpl::Associate(const wxFileTypeInfo& ftInfo)
+{
+ wxFAIL_MSG( _T("unimplemented") ); // TODO
+
+ return NULL;
+}
wxFileType *
wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString& ext)
{
+ InitIfNeeded();
+
wxFileType *fileType = NULL;
size_t count = m_aExtensions.GetCount();
for ( size_t n = 0; n < count; n++ ) {
wxFileType *
wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString& mimeType)
{
+ InitIfNeeded();
+
// mime types are not case-sensitive
wxString mimetype(mimeType);
mimetype.MakeLower();
void wxMimeTypesManagerImpl::AddFallback(const wxFileTypeInfo& filetype)
{
+ InitIfNeeded();
+
wxString extensions;
const wxArrayString& exts = filetype.GetExtensions();
size_t nExts = exts.GetCount();
const wxString& strExtensions,
const wxString& strDesc)
{
+ InitIfNeeded();
+
int index = m_aTypes.Index(strMimeType);
if ( index == wxNOT_FOUND ) {
// add a new entry
const wxString& strTest,
const wxString& strDesc)
{
+ InitIfNeeded();
+
MailCapEntry *entry = new MailCapEntry(strOpenCmd, strPrintCmd, strTest);
int nIndex = m_aTypes.Index(strType);
}
}
- // although it doesn't seem to be covered by RFCs, some programs
- // (notably Netscape) create their entries with several comma
- // separated extensions (RFC mention the spaces only)
+ // depending on the format (Mosaic or Netscape) either space or comma
+ // is used to separate the extensions
strExtensions.Replace(wxT(","), wxT(" "));
// also deal with the leading dot
strTest,
strDesc,
curField; // accumulator
- for ( bool cont = TRUE; cont; pc++ ) {
+ bool cont = TRUE;
+ while ( cont ) {
switch ( *pc ) {
case wxT('\\'):
// interpret the next character literally (notice that
// backslash can be used for line continuation)
if ( *++pc == wxT('\0') ) {
- // fetch the next line.
-
- // pc currently points to nowhere, but after the next
- // pc++ in the for line it will point to the beginning
- // of the next line in the file
- pc = file[++nLine].c_str() - 1;
+ // fetch the next line if there is one
+ if ( nLine == nLineCount - 1 ) {
+ // something is wrong, bail out
+ cont = FALSE;
+
+ wxLogDebug(wxT("Mailcap file %s, line %d: "
+ "'\\' on the end of the last line "
+ "ignored."),
+ strFileName.c_str(),
+ nLine + 1);
+ }
+ else {
+ // pass to the beginning of the next line
+ pc = file[++nLine].c_str();
+
+ // skip pc++ at the end of the loop
+ continue;
+ }
}
else {
// just a normal character
switch ( currentToken ) {
case Field_Type:
strType = curField;
+ if ( strType.empty() ) {
+ // I don't think that this is a valid mailcap
+ // entry, but try to interpret it somehow
+ strType = _T('*');
+ }
+
if ( strType.Find(wxT('/')) == wxNOT_FOUND ) {
// we interpret "type" as "type/*"
strType += wxT("/*");
break;
case Field_Other:
- {
+ if ( !curField.empty() ) {
// "good" mailcap entry?
bool ok = TRUE;
}
}
}
+ //else: the field is empty, ignore silently
// it already has this value
//currentToken = Field_Other;
default:
curField += *pc;
}
+
+ // continue in the same line
+ pc++;
}
// check that we really read something reasonable
// support for flags:
// 1. create an xterm for 'needsterminal'
// 2. append "| $PAGER" for 'copiousoutput'
+ //
+ // Note that the RFC says that having both needsterminal and
+ // copiousoutput is probably a mistake, so it seems that running
+ // programs with copiousoutput inside an xterm as it is done now
+ // is a bad idea (FIXME)
if ( copiousoutput ) {
const wxChar *p = wxGetenv(_T("PAGER"));
strOpenCmd << _T(" | ") << (p ? p : _T("more"));
size_t wxMimeTypesManagerImpl::EnumAllFileTypes(wxArrayString& mimetypes)
{
+ InitIfNeeded();
+
mimetypes.Empty();
wxString type;
return mimetypes.GetCount();
}
+// ----------------------------------------------------------------------------
+// writing to MIME type files
+// ----------------------------------------------------------------------------
+
+bool wxFileTypeImpl::Unassociate()
+{
+ wxFAIL_MSG( _T("unimplemented") ); // TODO
+
+ return FALSE;
+}
+
// ----------------------------------------------------------------------------
// private functions
// ----------------------------------------------------------------------------