Skip to content

Commit

Permalink
Add option to force utf8 filenames (nickbnf#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
variar committed Oct 27, 2020
1 parent 49f67f9 commit c10b30e
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 18 deletions.
44 changes: 27 additions & 17 deletions src/app/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ struct CliParameters {
bool follow_file = false;
int64_t log_level = static_cast<int64_t>( logWARNING );

std::vector<QString> filenames;
std::vector<std::string> filenames;

CliParameters() = default;

Expand Down Expand Up @@ -154,19 +154,9 @@ struct CliParameters {
[this]( auto count ) { log_level = static_cast<int64_t>( logWARNING ) + count; },
"output more debug (include multiple times for more verbosity e.g. -dddd)" );

std::vector<std::string> raw_filenames;
options.add_option( "files", raw_filenames, "files to open" );
options.add_option( "files", filenames, "files to open" );

options.parse( argc, argv );

for ( const auto& file : raw_filenames ) {
const auto rawName = QByteArray{ file.data(), static_cast<int>( file.size() ) };
const auto decodedName = QFile::decodeName( rawName );
if ( !decodedName.isEmpty() ) {
const auto fileInfo = QFileInfo( decodedName );
filenames.emplace_back( fileInfo.absoluteFilePath() );
}
}
}
};

Expand All @@ -191,15 +181,35 @@ int main( int argc, char* argv[] )

LOG( logINFO ) << "Klogg instance " << app.instanceId();

Configuration::getSynced();
const auto& config = Configuration::get();

const auto decodeFilename
= [assumeUtf8 = config.assumeUtf8Filenames()]( const QByteArray& rawName ) {
#ifdef Q_OS_LINUX
return assumeUtf8 ? QString::fromUtf8( rawName ) : QFile::decodeName( rawName );
#else
Q_UNUSED(assumeUtf8)
return QFile::decodeName( rawName );
#endif
};

std::vector<QString> initialFiles;
for ( const auto& file : parameters.filenames ) {
const auto rawName = QByteArray{ file.data(), static_cast<int>( file.size() ) };
const auto decodedName = decodeFilename( rawName );
if ( !decodedName.isEmpty() ) {
const auto fileInfo = QFileInfo( decodedName );
initialFiles.emplace_back( fileInfo.absoluteFilePath() );
}
}

if ( !parameters.multi_instance && app.isSecondary() ) {
LOG( logINFO ) << "Found another klogg, pid " << app.primaryPid();
app.sendFilesToPrimaryInstance( parameters.filenames );
app.sendFilesToPrimaryInstance( initialFiles );
}
else {
Configuration::getSynced();

// Load the existing session if needed
const auto& config = Configuration::get();
plog::EnableLogging( config.enableLogging(), config.loggingLevel() );

MainWindow* mw = nullptr;
Expand All @@ -215,7 +225,7 @@ int main( int argc, char* argv[] )
mw->show();
}

for ( const auto& filename : parameters.filenames ) {
for ( const auto& filename : initialFiles ) {
mw->loadInitialFile( filename, parameters.follow_file );
}

Expand Down
16 changes: 16 additions & 0 deletions src/ui/include/optionsdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,22 @@
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="fileSettingsBox">
<property name="title">
<string>Misc</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_16">
<item>
<widget class="QCheckBox" name="assumeUtf8FilenamesCheckBox">
<property name="text">
<string>Assume utf-8 filenames</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<spacer name="verticalSpacer_3">
<property name="orientation">
Expand Down
6 changes: 5 additions & 1 deletion src/ui/src/optionsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ void OptionsDialog::updateDialogFromConfig()
if ( familyIndex != -1 )
fontFamilyBox->setCurrentIndex( familyIndex );

updateFontSize(fontInfo.family());
updateFontSize( fontInfo.family() );

int sizeIndex = fontSizeBox->findText( QString::number( fontInfo.pointSize() ) );
if ( sizeIndex != -1 )
Expand Down Expand Up @@ -262,6 +262,8 @@ void OptionsDialog::updateDialogFromConfig()

// downloads
verifySslCheckBox->setChecked( config.verifySslPeers() );

assumeUtf8FilenamesCheckBox->setChecked( config.assumeUtf8Filenames() );
}

//
Expand Down Expand Up @@ -336,6 +338,8 @@ void OptionsDialog::updateConfigFromDialog()

config.setVerifySslPeers( verifySslCheckBox->isChecked() );

config.setAssumeUtf8Filenames( assumeUtf8FilenamesCheckBox->isChecked() );

config.setStyle( styleComboBox->currentText() );

config.save();
Expand Down
11 changes: 11 additions & 0 deletions src/utils/include/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,15 @@ class Configuration final : public Persistable<Configuration> {
scaleFactorRounding_ = rounding;
}

bool assumeUtf8Filenames() const
{
return assumeUtf8Filenames_;
}
void setAssumeUtf8Filenames( bool enable )
{
assumeUtf8Filenames_ = enable;
}

// Reads/writes the current config in the QSettings object passed
void saveToStorage( QSettings& settings ) const;
void retrieveFromStorage( QSettings& settings );
Expand Down Expand Up @@ -437,6 +446,8 @@ class Configuration final : public Persistable<Configuration> {
bool enableQtHighDpi_ = true;

int scaleFactorRounding_ = 1;

bool assumeUtf8Filenames_ = false;
};

#endif
5 changes: 5 additions & 0 deletions src/utils/src/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ void Configuration::retrieveFromStorage( QSettings& settings )
.value( "filewatch.fastModificationDetection", Default.fastModificationDetection_ )
.toBool();

assumeUtf8Filenames_
= settings.value( "file.utf8Filenames", Default.assumeUtf8Filenames_ ).toBool();

loadLastSession_ = settings.value( "session.loadLast", Default.loadLastSession_ ).toBool();
allowMultipleWindows_
= settings.value( "session.multipleWindows", Default.allowMultipleWindows_ ).toBool();
Expand Down Expand Up @@ -216,6 +219,8 @@ void Configuration::saveToStorage( QSettings& settings ) const

settings.setValue( "versionchecker.enabled", enableVersionChecking_ );

settings.setValue( "file.utf8Filenames", assumeUtf8Filenames_ );

settings.setValue( "archives.extract", extractArchives_ );
settings.setValue( "archives.extractAlways", extractArchivesAlways_ );

Expand Down

0 comments on commit c10b30e

Please sign in to comment.