[go: up one dir, main page]

Menu

[80c337]: / filesandpaths.cpp  Maximize  Restore  History

Download this file

222 lines (205 with data), 8.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#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 );
}