+void wxFileTypeImpl::Init(const wxString& strFileType, const wxString& ext)
+{
+ // VZ: does it? (FIXME)
+ wxCHECK_RET( !ext.IsEmpty(), _T("needs an extension") );
+
+ if ( ext[0u] != wxT('.') ) {
+ m_ext = wxT('.');
+ }
+ m_ext << ext;
+
+ m_strFileType = strFileType;
+ if ( !strFileType ) {
+ m_strFileType = m_ext.AfterFirst('.') + "_auto_file";
+ }
+}
+
+wxString wxFileTypeImpl::GetVerbPath(const wxString& verb) const
+{
+ wxString path;
+ path << m_strFileType << _T("\\shell\\") << verb << _T("\\command");
+ return path;
+}
+
+size_t wxFileTypeImpl::GetAllCommands(wxArrayString *verbs,
+ wxArrayString *commands,
+ const wxFileType::MessageParameters& params) const
+{
+ wxCHECK_MSG( !m_ext.IsEmpty(), 0, _T("GetAllCommands() needs an extension") );
+
+ if ( m_strFileType.IsEmpty() )
+ {
+ // get it from the registry
+ wxFileTypeImpl *self = wxConstCast(this, wxFileTypeImpl);
+ wxRegKey rkey(wxRegKey::HKCR, m_ext);
+ if ( !rkey.Exists() || !rkey.QueryValue(_T(""), self->m_strFileType) )
+ {
+ wxLogDebug(_T("Can't get the filetype for extension '%s'."),
+ m_ext.c_str());
+
+ return 0;
+ }
+ }
+
+ // enum all subkeys of HKCR\filetype\shell
+ size_t count = 0;
+ wxRegKey rkey(wxRegKey::HKCR, m_strFileType + _T("\\shell"));
+ long dummy;
+ wxString verb;
+ bool ok = rkey.GetFirstKey(verb, dummy);
+ while ( ok )
+ {
+ wxString command = wxFileType::ExpandCommand(GetCommand(verb), params);
+
+ // we want the open bverb to eb always the first
+
+ if ( verb.CmpNoCase(_T("open")) == 0 )
+ {
+ if ( verbs )
+ verbs->Insert(verb, 0);
+ if ( commands )
+ commands->Insert(command, 0);
+ }
+ else // anything else than "open"
+ {
+ if ( verbs )
+ verbs->Add(verb);
+ if ( commands )
+ commands->Add(command);
+ }
+
+ ok = rkey.GetNextKey(verb, dummy);
+ }
+
+ return count;
+}
+
+// ----------------------------------------------------------------------------
+// modify the registry database
+// ----------------------------------------------------------------------------
+
+bool wxFileTypeImpl::EnsureExtKeyExists()
+{
+ wxRegKey rkey(wxRegKey::HKCR, m_ext);
+ if ( !rkey.Exists() )
+ {
+ if ( !rkey.Create() || !rkey.SetValue(_T(""), m_strFileType) )
+ {
+ wxLogError(_("Failed to create registry entry for '%s' files."),
+ m_ext.c_str());
+ return FALSE;
+ }
+ }
+
+ return TRUE;
+}
+
+bool wxFileTypeImpl::SetCommand(const wxString& cmd,
+ const wxString& verb,
+ bool overwriteprompt)
+{
+ wxCHECK_MSG( !m_ext.IsEmpty() && !verb.IsEmpty(), FALSE,
+ _T("SetCommand() needs an extension and a verb") );
+
+ if ( !EnsureExtKeyExists() )
+ return FALSE;
+
+ wxRegKey rkey(wxRegKey::HKCR, GetVerbPath(verb));
+
+ if ( rkey.Exists() && overwriteprompt )
+ {
+#if wxUSE_GUI
+ wxString old;
+ rkey.QueryValue(wxT(""), old);
+ if ( wxMessageBox
+ (
+ wxString::Format(
+ _("Do you want to overwrite the command used to %s "
+ "files with extension \"%s\" (current value is '%s', "
+ "new value is '%s')?"),
+ verb.c_str(),
+ m_ext.c_str(),
+ old.c_str(),
+ cmd.c_str()),
+ _("Confirm registry update"),
+ wxYES_NO | wxICON_QUESTION
+ ) != wxYES )
+#endif // wxUSE_GUI
+ {
+ // cancelled by user
+ return FALSE;
+ }
+ }
+
+ // TODO:
+ // 1. translate '%s' to '%1' instead of always adding it
+ // 2. create DDEExec value if needed (undo GetCommand)
+ return rkey.Create() && rkey.SetValue(_T(""), cmd + _T(" \"%1\"") );
+}
+
+bool wxFileTypeImpl::SetMimeType(const wxString& mimeTypeOrig)
+{
+ wxCHECK_MSG( !m_ext.IsEmpty(), FALSE, _T("SetMimeType() needs extension") );
+
+ if ( !EnsureExtKeyExists() )
+ return FALSE;
+
+ // VZ: is this really useful? (FIXME)
+ wxString mimeType;
+ if ( !mimeTypeOrig )
+ {
+ // make up a default value for it
+ wxString cmd;
+ wxSplitPath(GetCommand(_T("open")), NULL, &cmd, NULL);
+ mimeType << _T("application/x-") << cmd;
+ }
+ else
+ {
+ mimeType = mimeTypeOrig;
+ }
+
+ wxRegKey rkey(wxRegKey::HKCR, m_ext);
+ return rkey.Create() && rkey.SetValue(_T("Content Type"), mimeType);
+}
+
+bool wxFileTypeImpl::SetDefaultIcon(const wxString& cmd, int index)
+{
+ wxCHECK_MSG( !m_ext.IsEmpty(), FALSE, _T("SetMimeType() needs extension") );
+ wxCHECK_MSG( wxFileExists(cmd), FALSE, _T("Icon file not found.") );
+
+ if ( !EnsureExtKeyExists() )
+ return FALSE;
+
+ wxRegKey rkey(wxRegKey::HKCR, m_strFileType + _T("\\DefaultIcon"));
+
+ return rkey.Create() &&
+ rkey.SetValue(_T(""),
+ wxString::Format(_T("%s,%d"), cmd.c_str(), index));
+}
+
+// ----------------------------------------------------------------------------
+// remove file association
+// ----------------------------------------------------------------------------
+
+bool wxFileTypeImpl::RemoveCommand(const wxString& verb)
+{
+ wxCHECK_MSG( !m_ext.IsEmpty() && !verb.IsEmpty(), FALSE,
+ _T("RemoveCommand() needs an extension and a verb") );
+
+ wxString sKey = m_strFileType;
+ wxRegKey rkey(wxRegKey::HKCR, GetVerbPath(verb));
+
+ // if the key already doesn't exist, it's a success
+ return !rkey.Exists() || rkey.DeleteSelf();
+}
+
+bool wxFileTypeImpl::RemoveMimeType()
+{
+ wxCHECK_MSG( !m_ext.IsEmpty(), FALSE, _T("RemoveMimeType() needs extension") );
+
+ wxRegKey rkey(wxRegKey::HKCR, m_ext);
+ return !rkey.Exists() || rkey.DeleteSelf();
+}
+
+bool wxFileTypeImpl::RemoveDefaultIcon()
+{
+ wxCHECK_MSG( !m_ext.IsEmpty(), FALSE,
+ _T("RemoveDefaultIcon() needs extension") );
+
+ wxRegKey rkey (wxRegKey::HKCR, m_strFileType + _T("\\DefaultIcon"));
+ return !rkey.Exists() || rkey.DeleteSelf();
+}
+