#include "filesandpaths.h"
#include <QCoreApplication>
#include <QFileInfo>
#include <QStringList>
#include <QDateTime>
#include <QDir>
#ifdef QT_GUI_LIB
#include <QFontDatabase>
#include <QStandardPaths>
#include <QSettings>
#endif // QT_GUI_LIB
QString applicationFolder ()
{
#if ( defined Q_OS_MAC ) && ( defined QT_GUI_LIB )
// only take this branch if QtGui defined because tests don't use app bundle
return QCoreApplication::applicationDirPath() + "/../../../";
#else
return QCoreApplication::applicationDirPath();
#endif
}
QString builtInFontsFolder ()
{
static QString result;
if ( result.isEmpty() ) {
// if a preprocessor symbol tells us where to find the fonts, obey that over all else
#ifdef BUILTIN_FONTS_FOLDER
result = BUILTIN_FONTS_FOLDER;
#else
// assume this is a Lurch installation, and try to find it in the appropriate place
result = QCoreApplication::applicationDirPath();
#ifdef Q_OS_WIN
result += "/builtin-fonts";
#else
#ifdef Q_OS_MAC
result += "/../Resources/builtin-fonts";
#else
result += "/builtin-fonts";
#endif
#endif
if ( !QFile( result ).exists() ) {
// if we couldn't find it, then assume this is a developer's working copy
result = QCoreApplication::applicationDirPath();
#ifdef Q_OS_WIN
if ( result.endsWith( "debug" ) || result.endsWith( "release" ) )
result += "/..";
result += "/../../externals/builtin-fonts";
#else
#ifdef Q_OS_MAC
result += "/../../../../../builtin-fonts";
#else
result += "/../../externals/builtin-fonts";
#endif
#endif
}
#endif
result = QFileInfo( result ).canonicalFilePath();
}
return result;
}
#define NUM_BUILTIN_FONTS 23
// find all built-in font files you can in the given set of search paths
QStringList builtInFontFilesInFolders ( const QStringList& paths )
{
static const QStringList fonts = QStringList() << "DejaVuSans-Bold.ttf"
<< "DejaVuSans-BoldOblique.ttf"
<< "DejaVuSans-ExtraLight.ttf"
<< "DejaVuSans-Oblique.ttf"
<< "DejaVuSans.ttf"
<< "DejaVuSansCondensed-Bold.ttf"
<< "DejaVuSansCondensed-BoldOblique.ttf"
<< "DejaVuSansCondensed-Oblique.ttf"
<< "DejaVuSansCondensed.ttf"
<< "DejaVuSansMono-Bold.ttf"
<< "DejaVuSansMono-BoldOblique.ttf"
<< "DejaVuSansMono-Oblique.ttf"
<< "DejaVuSansMono.ttf"
<< "DejaVuSerif-Bold.ttf"
<< "DejaVuSerif-BoldItalic.ttf"
<< "DejaVuSerif-Italic.ttf"
<< "DejaVuSerif.ttf"
<< "DejaVuSerifCondensed-Bold.ttf"
<< "DejaVuSerifCondensed-BoldItalic.ttf"
<< "DejaVuSerifCondensed-Italic.ttf"
<< "DejaVuSerifCondensed.ttf"
<< "Chunk.ttf"
<< "Symbola.ttf";
QStringList result;
foreach ( const QString& font, fonts ) {
foreach ( const QString& path, paths ) {
QString fullPath = path + "/" + font;
if ( QFile( fullPath ).exists() ) {
result << fullPath;
break; // don't try to find the same font in another folder
}
}
}
return result;
}
QStringList builtInFontFiles ()
{
static QStringList result;
if ( result.count() < NUM_BUILTIN_FONTS )
result = builtInFontFilesInFolders( QStringList() << builtInFontsFolder() );
return result;
}
QString installBuiltInFonts ()
{
#ifdef QT_GUI_LIB
QString result;
QStringList fonts = builtInFontFiles();
foreach ( const QString& font, fonts )
if ( QFontDatabase::addApplicationFont( font ) == -1 )
result += "Could not load font " + font + ".\n";
if ( fonts.count() != NUM_BUILTIN_FONTS )
result += QString( "Could not find %1 built-in fonts." )
.arg( NUM_BUILTIN_FONTS - fonts.count() );
return result;
#else
return QString( "Could not load built-in fonts without QT_GUI_LIB." );
#endif
}
QString defaultSearchPath ()
{
static QString result;
if ( result.isEmpty() ) {
// this must handle two cases, a developer's working copy and an installation
// if the file is placed relative to a libraries folder in what seems to be a working
// copy of a repository, assume that. this way, developers can work in a working copy,
// while still having an installed lurch elsewhere on their system, independently.
result = QFileInfo( applicationFolder() + "/../../libraries" ).canonicalFilePath();
if ( result.isEmpty() ) {
// no such folder, so this is probably not a working copy;
// try to find a libraries folder as if this were an installation
#ifdef Q_OS_MAC
result = QFileInfo( QCoreApplication::applicationDirPath()
+ "/../Resources/libraries" ).canonicalFilePath();
#else
result = QFileInfo( QCoreApplication::applicationDirPath()
+ "/libraries" ).canonicalFilePath();
#endif
}
}
return result;
}
QString defaultHelpSearchPath ()
{
return defaultSearchPath() + QDir::separator() + "help";
}
bool fileOnList ( const QString& filename, const QStringList& filenames )
{
QFileInfo fi( filename );
foreach ( const QString& fn, filenames )
if ( QFileInfo( fn ) == fi )
return true;
return false;
}
QString autoSaveFilePath ()
{
// thanks to Ben Meyer on Qt Labs Blogs for the post that gave most of this code
#ifdef QT_GUI_LIB
QString directory = QStandardPaths::writableLocation( QStandardPaths::DataLocation );
#else
// we're probably in a unit test, so just use the folder of the application (the test)
QString directory = applicationFolder();
#endif
// above line always returns a nonempty value on all non-embedded platforms.
// if the directory does not exist, create it:
if ( !QFile::exists( directory ) )
QDir().mkpath( directory );
// return result
return QFileInfo( QString( "%1/Autosave on %2.lurch" )
.arg( directory ).arg( QDateTime::currentDateTime().toString(
"ddd MMMM d yyyy hh-mm-ss-zzz" ) ) ).absoluteFilePath();
}
QStringList allAutoSaveFiles ()
{
#ifdef QT_GUI_LIB
QString directory = QStandardPaths::writableLocation( QStandardPaths::DataLocation );
#else
// we're probably in a unit test, so just use the folder of the application (the test)
QString directory = applicationFolder();
#endif
QStringList filenames = QDir( directory ).entryList(
QStringList() << "Autosave on *.lurch", QDir::Files );
for ( int i = 0 ; i < filenames.count() ; i++ )
filenames[i] = directory + "/" + filenames[i];
return filenames;
}
QString temporaryFolder ()
{
#ifdef QT_GUI_LIB
return QStandardPaths::writableLocation( QStandardPaths::TempLocation );
#else
// we're probably in a unit test, so just use the folder of the application (the test)
return applicationFolder();
#endif
}
QString lastUsedPath ()
{
return QSettings().value( "/main/last_used_path" ).toString();
}
void setLastUsedPath ( QString newPath )
{
QSettings().setValue( "/main/last_used_path", newPath );
}