mimetype.cpp
来自「A*算法 A*算法 A*算法 A*算法A*算法A*算法」· C++ 代码 · 共 2,012 行 · 第 1/5 页
CPP
2,012 行
}
if (bTemp)
{
// now got a file we can write to ....
wxMimeTypeCommands * entries = m_aEntries[index];
size_t iOpen;
wxString sCmd = entries->GetCommandForVerb(_T("open"), &iOpen);
wxString sTmp;
sTmp = m_aTypes[index];
wxString sOld;
int nIndex = file.pIndexOf(sTmp);
// get rid of all the unwanted entries...
if (nIndex == wxNOT_FOUND)
{
nIndex = (int) file.GetLineCount();
}
else
{
sOld = file[nIndex];
wxLogTrace(TRACE_MIME, wxT("--- Deleting from mailcap line '%d' ---"), nIndex);
while ( (sOld.Contains(wxT("\\"))) && (nIndex < (int) file.GetLineCount()) )
{
file.CommentLine(nIndex);
if (nIndex < (int) file.GetLineCount()) sOld = sOld + file[nIndex];
}
if (nIndex < (int) file.GetLineCount()) file.CommentLine (nIndex);
}
sTmp = sTmp + wxT(";") + sCmd; //includes wxT(" %s ");
// write it in the format that Netscape uses (default)
if (! ( m_mailcapStylesInited & wxMAILCAP_STANDARD ) )
{
if (! delete_index) file.InsertLine (sTmp, nIndex);
nIndex ++;
}
// write extended format
else
{
// todo FIX this code;
// ii) lost entries
// sOld holds all the entries, but our data store only has some
// eg test= is not stored
// so far we have written the mimetype and command out
wxStringTokenizer sT (sOld, wxT(";\\"));
if (sT.CountTokens () > 2)
{
// first one mimetype; second one command, rest unknown...
wxString s;
s = sT.GetNextToken();
s = sT.GetNextToken();
// first unknown
s = sT.GetNextToken();
while ( ! s.empty() )
{
bool bKnownToken = false;
if (s.Contains(wxT("description="))) bKnownToken = true;
if (s.Contains(wxT("x11-bitmap="))) bKnownToken = true;
size_t i;
for (i=0; i < entries->GetCount(); i++)
{
if (s.Contains(entries->GetVerb(i))) bKnownToken = true;
}
if (!bKnownToken)
{
sTmp = sTmp + wxT("; \\");
file.InsertLine (sTmp, nIndex);
sTmp = s;
}
s = sT.GetNextToken ();
}
}
if (! m_aDescriptions[index].empty() )
{
sTmp = sTmp + wxT("; \\");
file.InsertLine (sTmp, nIndex);
nIndex ++;
sTmp = wxT(" description=\"") + m_aDescriptions[index] + wxT("\"");
}
if (! m_aIcons[index].empty() )
{
sTmp = sTmp + wxT("; \\");
file.InsertLine (sTmp, nIndex);
nIndex ++;
sTmp = wxT(" x11-bitmap=\"") + m_aIcons[index] + wxT("\"");
}
if ( entries->GetCount() > 1 )
{
size_t i;
for (i=0; i < entries->GetCount(); i++)
if ( i != iOpen )
{
sTmp = sTmp + wxT("; \\");
file.InsertLine (sTmp, nIndex);
nIndex ++;
sTmp = wxT(" ") + entries->GetVerbCmd(i);
}
}
file.InsertLine (sTmp, nIndex);
nIndex ++;
}
bTemp = file.Write ();
file.Close ();
}
return bTemp;
}
wxFileType *
wxMimeTypesManagerImpl::Associate(const wxFileTypeInfo& ftInfo)
{
InitIfNeeded();
wxString strType = ftInfo.GetMimeType ();
wxString strDesc = ftInfo.GetDescription ();
wxString strIcon = ftInfo.GetIconFile ();
wxMimeTypeCommands *entry = new wxMimeTypeCommands ();
if ( ! ftInfo.GetOpenCommand().empty())
entry->Add(wxT("open=") + ftInfo.GetOpenCommand () + wxT(" %s "));
if ( ! ftInfo.GetPrintCommand ().empty())
entry->Add(wxT("print=") + ftInfo.GetPrintCommand () + wxT(" %s "));
// now find where these extensions are in the data store and remove them
wxArrayString sA_Exts = ftInfo.GetExtensions ();
wxString sExt, sExtStore;
size_t i, nIndex;
for (i=0; i < sA_Exts.GetCount(); i++)
{
sExt = sA_Exts.Item(i);
//clean up to just a space before and after
sExt.Trim().Trim(false);
sExt = wxT(' ') + sExt + wxT(' ');
for (nIndex = 0; nIndex < m_aExtensions.GetCount(); nIndex ++)
{
sExtStore = m_aExtensions.Item(nIndex);
if (sExtStore.Replace(sExt, wxT(" ") ) > 0) m_aExtensions.Item(nIndex) = sExtStore;
}
}
if ( !DoAssociation (strType, strIcon, entry, sA_Exts, strDesc) )
return NULL;
return GetFileTypeFromMimeType(strType);
}
bool wxMimeTypesManagerImpl::DoAssociation(const wxString& strType,
const wxString& strIcon,
wxMimeTypeCommands *entry,
const wxArrayString& strExtensions,
const wxString& strDesc)
{
int nIndex = AddToMimeData(strType, strIcon, entry, strExtensions, strDesc, true);
if ( nIndex == wxNOT_FOUND )
return false;
return WriteMimeInfo (nIndex, false);
}
bool wxMimeTypesManagerImpl::WriteMimeInfo(int nIndex, bool delete_mime )
{
bool ok = true;
if ( m_mailcapStylesInited & wxMAILCAP_STANDARD )
{
// write in metamail format;
if (WriteToMimeTypes (nIndex, delete_mime) )
if ( WriteToMailCap (nIndex, delete_mime) )
ok = false;
}
if ( m_mailcapStylesInited & wxMAILCAP_NETSCAPE )
{
// write in netsacpe format;
if (WriteToNSMimeTypes (nIndex, delete_mime) )
if ( WriteToMailCap (nIndex, delete_mime) )
ok = false;
}
if (m_mailcapStylesInited & wxMAILCAP_GNOME)
{
// Impossible to do for Gnome
}
if (m_mailcapStylesInited & wxMAILCAP_KDE)
{
// write in KDE format;
if (WriteKDEMimeFile (nIndex, delete_mime) )
ok = false;
}
return ok;
}
int wxMimeTypesManagerImpl::AddToMimeData(const wxString& strType,
const wxString& strIcon,
wxMimeTypeCommands *entry,
const wxArrayString& strExtensions,
const wxString& strDesc,
bool replaceExisting)
{
InitIfNeeded();
// ensure mimetype is always lower case
wxString mimeType = strType.Lower();
// is this a known MIME type?
int nIndex = m_aTypes.Index(mimeType);
if ( nIndex == wxNOT_FOUND )
{
// new file type
m_aTypes.Add(mimeType);
m_aIcons.Add(strIcon);
m_aEntries.Add(entry ? entry : new wxMimeTypeCommands);
// change nIndex so we can use it below to add the extensions
m_aExtensions.Add(wxEmptyString);
nIndex = m_aExtensions.size() - 1;
m_aDescriptions.Add(strDesc);
}
else // yes, we already have it
{
if ( replaceExisting )
{
// if new description change it
if ( !strDesc.empty())
m_aDescriptions[nIndex] = strDesc;
// if new icon change it
if ( !strIcon.empty())
m_aIcons[nIndex] = strIcon;
if ( entry )
{
delete m_aEntries[nIndex];
m_aEntries[nIndex] = entry;
}
}
else // add data we don't already have ...
{
// if new description add only if none
if ( m_aDescriptions[nIndex].empty() )
m_aDescriptions[nIndex] = strDesc;
// if new icon and no existing icon
if ( m_aIcons[nIndex].empty () )
m_aIcons[nIndex] = strIcon;
// add any new entries...
if ( entry )
{
wxMimeTypeCommands *entryOld = m_aEntries[nIndex];
size_t count = entry->GetCount();
for ( size_t i = 0; i < count; i++ )
{
const wxString& verb = entry->GetVerb(i);
if ( !entryOld->HasVerb(verb) )
{
entryOld->AddOrReplaceVerb(verb, entry->GetCmd(i));
}
}
// as we don't store it anywhere, it won't be deleted later as
// usual -- do it immediately instead
delete entry;
}
}
}
// always add the extensions to this mimetype
wxString& exts = m_aExtensions[nIndex];
// add all extensions we don't have yet
size_t count = strExtensions.GetCount();
for ( size_t i = 0; i < count; i++ )
{
wxString ext = strExtensions[i] + _T(' ');
if ( exts.Find(ext) == wxNOT_FOUND )
{
exts += ext;
}
}
// check data integrity
wxASSERT( m_aTypes.Count() == m_aEntries.Count() &&
m_aTypes.Count() == m_aExtensions.Count() &&
m_aTypes.Count() == m_aIcons.Count() &&
m_aTypes.Count() == m_aDescriptions.Count() );
return nIndex;
}
wxFileType *
wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString& ext)
{
if (ext.empty() )
return NULL;
InitIfNeeded();
size_t count = m_aExtensions.GetCount();
for ( size_t n = 0; n < count; n++ )
{
wxStringTokenizer tk(m_aExtensions[n], _T(' '));
while ( tk.HasMoreTokens() )
{
// consider extensions as not being case-sensitive
if ( tk.GetNextToken().IsSameAs(ext, false /* no case */) )
{
// found
wxFileType *fileType = new wxFileType;
fileType->m_impl->Init(this, n);
return fileType;
}
}
}
return NULL;
}
wxFileType *
wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString& mimeType)
{
InitIfNeeded();
wxFileType * fileType = NULL;
// mime types are not case-sensitive
wxString mimetype(mimeType);
mimetype.MakeLower();
// first look for an exact match
int index = m_aTypes.Index(mimetype);
if ( index != wxNOT_FOUND )
{
fileType = new wxFileType;
fileType->m_impl->Init(this, index);
}
// then try to find "text/*" as match for "text/plain" (for example)
// NB: if mimeType doesn't contain '/' at all, BeforeFirst() will return
// the whole string - ok.
index = wxNOT_FOUND;
wxString strCategory = mimetype.BeforeFirst(wxT('/'));
size_t nCount = m_aTypes.Count();
for ( size_t n = 0; n < nCount; n++ ) {
if ( (m_aTypes[n].BeforeFirst(wxT('/')) == strCategory ) &&
m_aTypes[n].AfterFirst(wxT('/')) == wxT("*") ) {
index = n;
break;
}
}
if ( index != wxNOT_FOUND )
{ // don't throw away fileType that was already found
if ( !fileType)
fileType = new wxFileType;
fileType->m_impl->Init(this, index);
}
return fileType;
}
wxString wxMimeTypesManagerImpl::GetCommand(const wxString & verb, size_t nIndex) const
{
wxString command, testcmd, sV, sTmp;
sV = verb + wxT("=");
// list of verb = command pairs for this mimetype
wxMimeTypeCommands * sPairs = m_aEntries [nIndex];
size_t i;
for ( i = 0; i < sPairs->GetCount (); i++ )
{
sTmp = sPairs->GetVerbCmd (i);
if ( sTmp.Contains(sV) )
command = sTmp.AfterFirst(wxT('='));
}
return command;
}
void wxMimeTypesManage
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?