Files
sethLabels/model/FileUtil.cpp
T
Jaye Evins 1c902230fe Big-Ugly Style Update (#278)
* Bulk replaced tabs with spaces
* Bulk removed trailing whitespace from lines
* Replaced c-style comments with c++-style comments in file banners
* Replace nested namespace definitions with single concise definitions (C++17), this keeps the indentation more manageable
* Cleanup ordering and spacing of include directives
* Bulk renaming of header file extensions from '.h' to '.hpp'.
* Update CODING-STYLE.md
* Update target_compile_features from cxx_std_11 to cxx_std_20.
* Refresh .clang-format file.  Still needs a lot of tweaking.
2026-01-07 19:43:34 -05:00

123 lines
3.9 KiB
C++

// FileUtil.cpp
//
// Copyright (C) 2015 Jaye Evins <evins@snaught.com>
//
// This file is part of gLabels-qt.
//
// gLabels-qt is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// gLabels-qt is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with gLabels-qt. If not, see <http://www.gnu.org/licenses/>.
//
#include "FileUtil.hpp"
#include "Config.hpp"
#include <QApplication>
#include <QStandardPaths>
namespace glabels::model
{
QString FileUtil::addExtension( const QString& rawFilename, const QString& extension )
{
if ( rawFilename.endsWith( extension ) )
{
return rawFilename;
}
return rawFilename + extension;
}
QDir FileUtil::systemTemplatesDir()
{
QDir dir;
// First, try finding templates directory relative to application path
dir.cd( QApplication::applicationDirPath() );
if ( (dir.dirName() == "bin") &&
dir.cdUp() && dir.cd( "share" ) && dir.cd( "glabels-qt" ) && dir.cd( "templates" ) )
{
return dir;
}
// Next, try running out of the source directory.
if ( dir.cd( Config::PROJECT_SOURCE_DIR ) && dir.cd( "templates" ) )
{
return dir;
}
qFatal( "Cannot locate system template directory!" );
return QDir("/");
}
QDir FileUtil::manualUserTemplatesDir()
{
// Location for manually created user-defined templates
QDir dir( QStandardPaths::writableLocation(QStandardPaths::HomeLocation) );
dir.mkpath( ".glabels" );
dir.cd( ".glabels" );
return dir;
}
QDir FileUtil::userTemplatesDir()
{
// Location for user-defined templates created using TemplateDesigner
QDir dir( QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) );
dir.mkpath( "." );
return dir;
}
QDir FileUtil::translationsDir()
{
QDir dir;
// First, try finding translations directory relative to application path
dir.cd( QApplication::applicationDirPath() );
if ( (dir.dirName() == "bin") &&
dir.cdUp() && dir.cd( "share" ) && dir.cd( "glabels-qt" ) && dir.cd( "translations" ) )
{
return dir;
}
// Next, try running out of the source directory.
if ( dir.cd( Config::PROJECT_BUILD_DIR ) && dir.cd( "translations" ) )
{
return dir;
}
qFatal( "Cannot locate system template directory!" );
return QDir("/");
}
QString FileUtil::makeRelativeIfInDir( const QDir& dir,
const QString& filename )
{
QString relativeFilePath = dir.relativeFilePath( filename ); // Note: directory separators canonicalized to slash by Qt path methods
if ( !relativeFilePath.startsWith( "../" ) )
{
return relativeFilePath;
}
return filename;
}
}