+// kde writing; see http://webcvs.kde.org/cgi-bin/cvsweb.cgi/~checkout~/kdelibs/kio/DESKTOP_ENTRY_STANDARD
+// for now write to .kdelnk but should eventually do .desktop instead (in preference??)
+
+bool wxMimeTypesManagerImpl::CheckKDEDirsExist( const wxString &sOK, const wxString &sTest )
+{
+    if (sTest.empty())
+    {
+        return wxDir::Exists(sOK);
+    }
+    else
+    {
+        wxString sStart = sOK + wxT("/") + sTest.BeforeFirst(wxT('/'));
+        if (!wxDir::Exists(sStart))
+            wxMkdir(sStart);
+        wxString sEnd = sTest.AfterFirst(wxT('/'));
+        return CheckKDEDirsExist(sStart, sEnd);
+    }
+}
+
+bool wxMimeTypesManagerImpl::WriteKDEMimeFile(int index, bool delete_index)
+{
+    wxMimeTextFile appoutfile, mimeoutfile;
+    wxString sHome = wxGetHomeDir();
+    wxString sTmp = wxT(".kde/share/mimelnk/");
+    wxString sMime = m_aTypes[index];
+    CheckKDEDirsExist(sHome, sTmp + sMime.BeforeFirst(wxT('/')) );
+    sTmp = sHome + wxT('/') + sTmp + sMime + wxT(".kdelnk");
+
+    bool bTemp;
+    bool bMimeExists = mimeoutfile.Open(sTmp);
+    if (!bMimeExists)
+    {
+        bTemp = mimeoutfile.Create(sTmp);
+        // some unknown error eg out of disk space
+        if (!bTemp)
+            return false;
+    }
+
+    sTmp = wxT(".kde/share/applnk/");
+    CheckKDEDirsExist(sHome, sTmp + sMime.AfterFirst(wxT('/')) );
+    sTmp = sHome + wxT('/') + sTmp + sMime.AfterFirst(wxT('/')) + wxT(".kdelnk");
+
+    bool bAppExists;
+    bAppExists = appoutfile.Open(sTmp);
+    if (!bAppExists)
+    {
+        bTemp = appoutfile.Create(sTmp);
+        // some unknown error eg out of disk space
+        if (!bTemp)
+            return false;
+    }
+
+    // fixed data; write if new file
+    if (!bMimeExists)
+    {
+        mimeoutfile.AddLine(wxT("#KDE Config File"));
+        mimeoutfile.AddLine(wxT("[KDE Desktop Entry]"));
+        mimeoutfile.AddLine(wxT("Version=1.0"));
+        mimeoutfile.AddLine(wxT("Type=MimeType"));
+        mimeoutfile.AddLine(wxT("MimeType=") + sMime);
+    }
+
+    if (!bAppExists)
+    {
+        mimeoutfile.AddLine(wxT("#KDE Config File"));
+        mimeoutfile.AddLine(wxT("[KDE Desktop Entry]"));
+        appoutfile.AddLine(wxT("Version=1.0"));
+        appoutfile.AddLine(wxT("Type=Application"));
+        appoutfile.AddLine(wxT("MimeType=") + sMime + wxT(';'));
+    }
+
+    // variable data
+    // ignore locale
+    mimeoutfile.CommentLine(wxT("Comment="));
+    if (!delete_index)
+        mimeoutfile.AddLine(wxT("Comment=") + m_aDescriptions[index]);
+    appoutfile.CommentLine(wxT("Name="));
+    if (!delete_index)
+        appoutfile.AddLine(wxT("Comment=") + m_aDescriptions[index]);
+
+    sTmp = m_aIcons[index];
+    // we can either give the full path, or the shortfilename if its in
+    // one of the directories we search
+    mimeoutfile.CommentLine(wxT("Icon=") );
+    if (!delete_index)
+        mimeoutfile.AddLine(wxT("Icon=") + sTmp );
+    appoutfile.CommentLine(wxT("Icon=") );
+    if (!delete_index)
+        appoutfile.AddLine(wxT("Icon=") + sTmp );
+
+    sTmp = wxT(" ") + m_aExtensions[index];
+
+    wxStringTokenizer tokenizer(sTmp, wxT(" "));
+    sTmp = wxT("Patterns=");
+    mimeoutfile.CommentLine(sTmp);
+    while ( tokenizer.HasMoreTokens() )
+    {
+        // holds an extension; need to change it to *.ext;
+        wxString e = wxT("*.") + tokenizer.GetNextToken() + wxT(";");
+        sTmp = sTmp + e;
+    }
+
+    if (!delete_index)
+        mimeoutfile.AddLine(sTmp);
+
+    wxMimeTypeCommands * entries = m_aEntries[index];
+    // if we don't find open just have an empty string ... FIX this
+    sTmp = entries->GetCommandForVerb(wxT("open"));
+    sTmp.Replace( wxT("%s"), wxT("%f") );
+
+    mimeoutfile.CommentLine(wxT("DefaultApp=") );
+    if (!delete_index)
+        mimeoutfile.AddLine(wxT("DefaultApp=") + sTmp);
+
+    sTmp.Replace( wxT("%f"), wxT("") );
+    appoutfile.CommentLine(wxT("Exec="));
+    if (!delete_index)
+        appoutfile.AddLine(wxT("Exec=") + sTmp);
+
+    if (entries->GetCount() > 1)
+    {
+        //other actions as well as open
+    }
+
+    bTemp = false;
+    if (mimeoutfile.Write())
+        bTemp = true;
+    mimeoutfile.Close();
+    if (appoutfile.Write())
+        bTemp = true;
+    appoutfile.Close();
+
+    return bTemp;
+}
+
+void wxMimeTypesManagerImpl::LoadKDELinksForMimeSubtype(const wxString& dirbase,