]> git.saurik.com Git - wxWidgets.git/blobdiff - src/unix/mimetype.cpp
fixed FindOrCreatePen/Brush() for the case when the object couldn't be created succes...
[wxWidgets.git] / src / unix / mimetype.cpp
index ff2bb4218a962e812771f9281e2c2d65cf1b1b4e..fffd9d50ddad4ba6e20b1997a16e607aca224620 100644 (file)
@@ -679,45 +679,69 @@ void wxKDEIconHandler::LoadLinksForMimeSubtype(const wxString& dirbase,
     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;
+
+    wxString comment;
+#if wxUSE_INTL
+    wxLocale *locale = wxGetLocale();
+    if ( locale )
+    {
+        // try "Comment[locale name]" first
+        comment << _T("Comment[") + locale->GetName() + _T("]=");
 
-    // 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...)
+        posComment = text.Find(comment);
+    }
+#endif // wxUSE_INTL
 
-    wxString mime_extension, mime_desc;
+    if ( posComment == wxNOT_FOUND )
+    {
+        comment = _T("Comment=");
 
-    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
+        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
 
-    pos = text.Find(_T("Patterns="));
-    if (pos != wxNOT_FOUND)
+    // next find the extensions
+    wxString mime_extension;
+
+    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);
@@ -726,8 +750,8 @@ void wxKDEIconHandler::LoadLinksForMimeSubtype(const wxString& dirbase,
 
     // 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;
@@ -735,7 +759,7 @@ void wxKDEIconHandler::LoadLinksForMimeSubtype(const wxString& dirbase,
 
     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++;
@@ -1472,18 +1496,31 @@ bool wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName,
                  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
@@ -1505,6 +1542,12 @@ bool wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName,
                     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("/*");
@@ -1520,7 +1563,7 @@ bool wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName,
                             break;
 
                         case Field_Other:
-                            {
+                            if ( !curField.empty() ) {
                                 // "good" mailcap entry?
                                 bool ok = TRUE;
 
@@ -1595,6 +1638,7 @@ bool wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName,
                                     }
                                 }
                             }
+                            //else: the field is empty, ignore silently
 
                             // it already has this value
                             //currentToken = Field_Other;
@@ -1611,6 +1655,9 @@ bool wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName,
                 default:
                     curField += *pc;
             }
+
+            // continue in the same line
+            pc++;
         }
 
         // check that we really read something reasonable