1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-10-13 14:13:32 +02:00

Define FileNames::FileType and related utility functions

This commit is contained in:
Paul Licameli
2019-12-27 10:09:35 -05:00
parent 1c7fd66b6e
commit fc668f09b0
4 changed files with 227 additions and 4 deletions

View File

@@ -41,6 +41,7 @@ and ImportLOF.cpp.
#include "ImportPlugin.h"
#include <algorithm>
#include <unordered_set>
#include <wx/textctrl.h>
#include <wx/string.h>
@@ -145,6 +146,78 @@ void Importer::GetSupportedImportFormats(FormatList *formatList)
}
}
FileNames::FileTypes
Importer::GetFileTypes( const FileNames::FileType &extraType )
{
// Construct the filter
FileNames::FileTypes fileTypes{
FileNames::AllFiles,
// Will fill in the list of extensions later:
{ XO("All supported files"), {} }
};
if ( !extraType.extensions.empty() )
fileTypes.push_back( extraType );
FormatList l;
GetSupportedImportFormats(&l);
using ExtensionSet = std::unordered_set< FileExtension >;
FileExtensions allList = extraType.extensions, newList;
ExtensionSet allSet{ allList.begin(), allList.end() }, newSet;
for ( const auto &format : l ) {
newList.clear();
newSet.clear();
for ( const auto &extension : format.formatExtensions ) {
if ( newSet.insert( extension ).second )
newList.push_back( extension );
if ( allSet.insert( extension ).second )
allList.push_back( extension );
}
fileTypes.push_back( { format.formatName, newList } );
}
fileTypes[1].extensions = allList;
return fileTypes;
}
void Importer::SetLastOpenType( const FileNames::FileType &type )
{
// PRL: Preference key /LastOpenType, unusually, stores a localized
// string!
// The bad consequences of a change of locale are not severe -- only that
// a default choice of file type for an open dialog is not remembered
gPrefs->Write(wxT("/LastOpenType"), type.description.Translation());
gPrefs->Flush();
}
void Importer::SetDefaultOpenType( const FileNames::FileType &type )
{
// PRL: Preference key /DefaultOpenType, unusually, stores a localized
// string!
// The bad consequences of a change of locale are not severe -- only that
// a default choice of file type for an open dialog is not remembered
gPrefs->Write(wxT("/DefaultOpenType"), type.description.Translation());
gPrefs->Flush();
}
size_t Importer::SelectDefaultOpenType( const FileNames::FileTypes &fileTypes )
{
wxString defaultValue;
if ( !fileTypes.empty() )
defaultValue = fileTypes[0].description.Translation();
wxString type = gPrefs->Read(wxT("/DefaultOpenType"), defaultValue);
// Convert the type to the filter index
auto begin = fileTypes.begin();
auto index = std::distance(
begin,
std::find_if( begin, fileTypes.end(),
[&type](const FileNames::FileType &fileType){
return fileType.description.Translation() == type; } ) );
return (index == fileTypes.size()) ? 0 : index;
}
void Importer::StringToList(wxString &str, wxString &delims, wxArrayString &list, wxStringTokenizerMode mod)
{
wxStringTokenizer toker;
@@ -389,10 +462,6 @@ bool Importer::Import(const FilePath &fName,
for (const auto &plugin : sImportPluginList())
{
// PRL: Preference keys /DefaultOpenType and /LastOpenType, unusually,
// store localized strings!
// The bad consequences of a change of locale are not severe -- only that
// a default choice of file type for an open dialog is not remembered
if (plugin->GetPluginFormatDescription().Translation() == type )
{
// This plugin corresponds to user-selected filter, try it first.

View File

@@ -18,6 +18,7 @@
#include <wx/tokenzr.h> // for enum wxStringTokenizerMode
#include "../widgets/wxPanelWrapper.h" // to inherit
#include "../FileNames.h" // for FileType
class wxArrayString;
class wxListBox;
@@ -120,6 +121,31 @@ public:
*/
void GetSupportedImportFormats(FormatList *formatList);
/**
* Constructs a list of types, for use by file opening dialogs, that includes
* all supported file types
*/
FileNames::FileTypes
GetFileTypes( const FileNames::FileType &extraType = {} );
/**
* Remember a file type in preferences
*/
static void
SetLastOpenType( const FileNames::FileType &type );
/**
* Remember a file type in preferences
*/
static void
SetDefaultOpenType( const FileNames::FileType &type );
/**
* Choose index of preferred type
*/
static size_t
SelectDefaultOpenType( const FileNames::FileTypes &fileTypes );
/**
* Reads extended import filters from gPrefs into internal
* list mExtImportItems