Subversion Repositories Scribus

Rev

Rev 24035 | Rev 24055 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4430 cbradney 1
/*
2
For general Scribus (>=1.3.2) copyright and licensing information please refer
3
to the COPYING file provided with the program. Following this notice may exist
4
a copyright and/or license notice that predates the release of Scribus 1.3.2
5
for which a new license (GPL+exception) is in place.
6
*/
1693 craig 7
#include "scpaths.h"
10223 cbradney 8
#include <QApplication>
10755 cbradney 9
#include <QDebug>
10223 cbradney 10
#include <QDir>
15483 craig 11
#include <QProcess>
21520 craig 12
#include <QStandardPaths>
1693 craig 13
 
21817 craig 14
#include "prefsmanager.h"
2688 craig 15
#include "scconfig.h"
21532 craig 16
#include "scribusapp.h"
2673 cbradney 17
 
1693 craig 18
// On Qt/Mac we need CoreFoundation to discover the location
19
// of the app bundle.
18204 fschmid 20
#ifdef Q_OS_MAC
1693 craig 21
#include <CoreFoundation/CoreFoundation.h>
22
#endif
23
 
3096 fschmid 24
#ifdef _WIN32
25
#include <windows.h>
26
#include <shlobj.h>
27
#endif
28
 
22666 jghali 29
#if defined(Q_OS_WIN32) || defined (Q_OS_OS2)
4194 fschmid 30
const char ScPaths::envPathSeparator = ';';
31
#else
32
const char ScPaths::envPathSeparator = ':';
33
#endif
34
 
22534 craig 35
// Init the singleton's "self" address to nullptr
36
ScPaths* ScPaths::m_instance = nullptr;
1693 craig 37
 
38
// Singleton's public constructor
39
const ScPaths& ScPaths::instance()
40
{
41
        if (!ScPaths::m_instance)
42
                ScPaths::m_instance = new ScPaths();
43
        return *ScPaths::m_instance;
44
}
45
 
46
// Singleton's public destructor
47
void ScPaths::destroy()
48
{
22607 craig 49
        delete ScPaths::m_instance;
1693 craig 50
}
51
 
52
// Protected "real" constructor
23028 jghali 53
ScPaths::ScPaths()
54
{
55
// On *nix, all paths are initialized to compile-time defaults passed in
1693 craig 56
// as preprocessor macros and set by autoconf.
23028 jghali 57
#if !defined(Q_OS_MAC) && !defined(_WIN32) && defined(WANT_RELOCATABLE)
58
        QString appPath = qApp->applicationDirPath();
59
        m_docDir = appPath + "/../" + QString(DOCDIR);
60
        m_iconDir = appPath + "/../" + QString(ICONDIR);
61
        m_libDir = appPath + "/../" + QString(LIBDIR);
62
        m_pluginDir = appPath + "/../" + QString(PLUGINDIR);
63
        m_qmlDir = appPath + "/../" + QString(QMLDIR);
64
        m_sampleScriptDir = appPath + "/../" + QString(SAMPLESDIR);
65
        m_scriptDir = appPath + "/../" + QString(SCRIPTSDIR);
66
        m_shareDir = appPath + "/../" + QString(SHAREDIR);
67
        m_templateDir = appPath + "/../" + QString(TEMPLATEDIR);
68
#elif !defined(Q_OS_MAC) && !defined(_WIN32)
69
        m_docDir = QString(DOCDIR);
70
        m_iconDir = QString(ICONDIR);
71
        m_libDir = QString(LIBDIR);
72
        m_pluginDir = QString(PLUGINDIR);
73
        m_qmlDir = QString(QMLDIR);
74
        m_sampleScriptDir = QString(SAMPLESDIR);
75
        m_scriptDir = QString(SCRIPTSDIR);
76
        m_shareDir = QString(SHAREDIR);
77
        m_templateDir = QString(TEMPLATEDIR);
78
#endif
79
 
1693 craig 80
// On MacOS/X, override the compile-time settings with a location
81
// obtained from the system.
18204 fschmid 82
#ifdef Q_OS_MAC
13179 cbradney 83
        QString pathPtr(bundleDir());
15415 craig 84
        qDebug() << QString("scpaths: bundle at %1").arg(pathPtr);
3794 cbradney 85
        m_shareDir = QString("%1/Contents/share/scribus/").arg(pathPtr);
12543 cbradney 86
        m_docDir = QString("%1/Contents/share/doc/scribus/").arg(pathPtr);
19781 craig 87
        m_fontDir = QString("%1/Contents/share/scribus/fonts/").arg(pathPtr);
3794 cbradney 88
        m_iconDir = QString("%1/Contents/share/scribus/icons/").arg(pathPtr);
89
        m_sampleScriptDir = QString("%1/Contents/share/scribus/samples/").arg(pathPtr);
90
        m_scriptDir = QString("%1/Contents/share/scribus/scripts/").arg(pathPtr);
91
        m_templateDir = QString("%1/Contents/share/scribus/templates/").arg(pathPtr);
19674 craig 92
        m_libDir = QString("%1/Contents/lib/").arg(pathPtr);
93
        m_pluginDir = QString("%1/Contents/lib/").arg(pathPtr);
18758 craig 94
        m_qmlDir = QString("%1/Contents/share/scribus/qml/").arg(pathPtr);
95
        //QApplication::setLibraryPaths(QStringList(QString("%1/Contents/lib/qtplugins/").arg(pathPtr)));
19674 craig 96
        QApplication::addLibraryPath(QString("%1/Contents/PlugIns/").arg(pathPtr));
3546 avox 97
        // on OSX this goes to the sys console, so user only sees it when they care -- AV
10755 cbradney 98
        qDebug() << QString("scpaths: doc dir=%1").arg(m_docDir);
99
        qDebug() << QString("scpaths: icon dir=%1").arg(m_iconDir);
100
        qDebug() << QString("scpaths: font dir=%1").arg(m_fontDir);
101
        qDebug() << QString("scpaths: sample dir=%1").arg(m_sampleScriptDir);
102
        qDebug() << QString("scpaths: script dir=%1").arg(m_scriptDir);
103
        qDebug() << QString("scpaths: template dir=%1").arg(m_templateDir);
104
        qDebug() << QString("scpaths: lib dir=%1").arg(m_libDir);
105
        qDebug() << QString("scpaths: plugin dir=%1").arg(m_pluginDir);
18758 craig 106
        qDebug() << QString("scpaths: QML dir=%1").arg(m_qmlDir);
10755 cbradney 107
        qDebug() << QString("scpaths: qtplugins=%1").arg(QApplication::libraryPaths().join(":"));
3546 avox 108
 
3096 fschmid 109
#elif defined(_WIN32)
22555 jghali 110
        QFileInfo appInfo(qApp->applicationDirPath());
3096 fschmid 111
        QString appPath = qApp->applicationDirPath();
22555 jghali 112
        QString cleanAppPath = appInfo.canonicalFilePath();
113
        if (!cleanAppPath.isEmpty())
114
                appPath = cleanAppPath;
115
 
3794 cbradney 116
        m_shareDir = QString("%1/share/").arg(appPath);
117
        m_docDir = QString("%1/share/doc/").arg(appPath);
118
        m_fontDir = QString("%1/share/fonts/").arg(appPath);
119
        m_iconDir = QString("%1/share/icons/").arg(appPath);
120
        m_sampleScriptDir = QString("%1/share/samples/").arg(appPath);
121
        m_scriptDir = QString("%1/share/scripts/").arg(appPath);
122
        m_templateDir = QString("%1/share/templates/").arg(appPath);
123
        m_libDir = QString("%1/libs/").arg(appPath);
124
        m_pluginDir = QString("%1/plugins/").arg(appPath);
18760 jghali 125
        m_qmlDir = QString("%1/share/qml/").arg(appPath);
18425 jghali 126
 
127
        QString qtpluginDir = QString("%1/qtplugins/").arg(appPath);
128
        if (QDir(qtpluginDir).exists())
129
                QApplication::setLibraryPaths( QStringList(qtpluginDir) );
3096 fschmid 130
#endif
7407 cbradney 131
 
22650 jghali 132
//      if (!m_shareDir.endsWith("/"))        m_shareDir += "/";
133
//      if (!m_docDir.endsWith("/"))          m_docDir += "/";
134
//      if (!m_fontDir.endsWith("/"))         m_fontDir += "/";
135
        if (!m_iconDir.endsWith("/"))         m_iconDir += "/";
136
//      if (!m_sampleScriptDir.endsWith("/")) m_sampleScriptDir += "/";
137
//      if (!m_scriptDir.endsWith("/"))       m_scriptDir += "/";
138
//      if (!m_templateDir.endsWith("/"))     m_templateDir += "/";
139
//      if (!m_libDir.endsWith("/"))          m_libDir += "/";
140
//      if (!m_pluginDir.endsWith("/"))       m_pluginDir += "/";
1693 craig 141
}
142
 
23116 craig 143
ScPaths::~ScPaths() = default;
1693 craig 144
 
22598 craig 145
QString ScPaths::bundleDir() const
13179 cbradney 146
{
147
        // On MacOS/X, override the compile-time settings with a location
148
// obtained from the system.
18204 fschmid 149
#ifdef Q_OS_MAC
13179 cbradney 150
        // Set up the various app paths to look inside the app bundle
151
        CFURLRef pluginRef = CFBundleCopyBundleURL(CFBundleGetMainBundle());
15413 craig 152
        CFStringRef macPath = CFURLCopyFileSystemPath(pluginRef, kCFURLPOSIXPathStyle);
153
        const char *pathPtr = CFStringGetCStringPtr(macPath, CFStringGetSystemEncoding());
22534 craig 154
        if (pathPtr!=nullptr && strlen(pathPtr)>0)
15413 craig 155
        {
156
                // make sure we get the Scribus.app directory, not some subdir
157
                // strip trailing '/':
158
                qDebug("Path = %s", pathPtr);
159
                char *p = const_cast<char*>(pathPtr + strlen(pathPtr) - 1);
160
                while (*p == '/')
161
                        --p;
162
                ++p;
13179 cbradney 163
                *p = '\0';
15413 craig 164
                if (strcmp("/bin", p-4) == 0) {
165
                        p -= 4;
166
                        *p = '\0';
167
                }
168
                if (strcmp("/MacOS", p-6) == 0) {
169
                        p -= 6;
170
                        *p = '\0';
171
                }
172
                if (strcmp("/Contents", p-9) == 0) {
173
                        p -= 9;
174
                        *p = '\0';
175
                }
176
                CFRelease(pluginRef);
177
                CFRelease(macPath);
178
                return QString("%1").arg(pathPtr);
13179 cbradney 179
        }
22638 craig 180
        char buf[2048];
181
        CFStringGetCString (macPath, buf, 2048, kCFStringEncodingUTF8);
182
        QString q_pathPtr=QString::fromUtf8(buf);
183
        if (q_pathPtr.endsWith("/bin"))
184
                q_pathPtr.chop(4);
185
        if (q_pathPtr.endsWith("/MacOS"))
186
                q_pathPtr.chop(6);
187
        if (q_pathPtr.endsWith("/Contents"))
188
                q_pathPtr.chop(9);
189
        CFRelease(pluginRef);
190
        CFRelease(macPath);
191
        return q_pathPtr;
13179 cbradney 192
#endif
23049 craig 193
        return QString();
13179 cbradney 194
}
195
 
20383 craig 196
QString ScPaths::defaultImageEditorApp()
197
{
198
#ifdef Q_OS_MAC
199
        QString appName("/Applications/GIMP.app");
200
        QFile app(appName);
201
        if (app.exists())
202
                return appName;
203
#endif
204
        return "gimp";
205
}
206
 
1693 craig 207
const QString&  ScPaths::docDir() const
208
{
209
        return m_docDir;
210
}
211
 
212
const QString&  ScPaths::iconDir() const
213
{
214
        return m_iconDir;
215
}
216
 
3794 cbradney 217
const QString&  ScPaths::fontDir() const
218
{
219
        return m_fontDir;
220
}
221
 
1693 craig 222
const QString&  ScPaths::libDir() const
223
{
224
        return m_libDir;
225
}
226
 
227
const QString&  ScPaths::pluginDir() const
228
{
229
        return m_pluginDir;
230
}
231
 
232
const QString&  ScPaths::sampleScriptDir() const
233
{
234
        return m_sampleScriptDir;
235
}
236
 
22832 craig 237
const QString& ScPaths::scriptDir() const
1693 craig 238
{
239
        return m_scriptDir;
240
}
241
 
22832 craig 242
const QString& ScPaths::templateDir() const
1693 craig 243
{
244
        return m_templateDir;
245
}
246
 
22832 craig 247
const QString& ScPaths::shareDir() const
2707 subik 248
{
249
        return m_shareDir;
250
}
3096 fschmid 251
 
22832 craig 252
const QString& ScPaths::qmlDir() const
18758 craig 253
{
254
        return m_qmlDir;
255
}
256
 
10997 jghali 257
QString ScPaths::translationDir() const
258
{
259
        return (m_shareDir + "translations/");
260
}
261
 
11120 malex 262
QString ScPaths::dictDir() const
263
{
264
        return(m_shareDir + "dicts/");
265
}
266
 
17501 craig 267
QStringList ScPaths::spellDirs() const
268
{
269
        QDir d;
270
        QStringList spellDirs;
21526 craig 271
        spellDirs.append(userDictDir(ScPaths::Spell, false));
17501 craig 272
        spellDirs.append(m_shareDir + "dicts/spelling/");
273
#ifdef Q_OS_MAC
21934 craig 274
        QString macPortsPath("/opt/local/share/hunspell/");
275
        QString finkPath("/sw/share/hunspell/");
276
        QString osxLibreOfficePath("/Applications/LibreOffice.app/Contents/Resources/extensions");
277
        QString osxUserLibreOfficePath(QDir::homePath()+"/Applications/LibreOffice.app/Contents/Resources/extensions");
17501 craig 278
        d.setPath(macPortsPath);
279
        if (d.exists())
280
                spellDirs.append(macPortsPath);
281
        d.setPath(finkPath);
282
        if (d.exists())
283
                spellDirs.append(finkPath);
284
        d.setPath(osxLibreOfficePath);
285
        if (d.exists())
286
        {
287
                QStringList dictDirFilters("dict-*");
22669 jghali 288
                const QStringList dictDirList(d.entryList(dictDirFilters, QDir::Dirs, QDir::Name));
289
                for (const QString& dir : dictDirList)
17501 craig 290
                        spellDirs.append(osxLibreOfficePath + "/" + dir + "/");
291
        }
292
        d.setPath(osxUserLibreOfficePath);
293
        if (d.exists())
294
        {
295
                QStringList dictDirFilters("dict-*");
22669 jghali 296
                const QStringList dictDirList(d.entryList(dictDirFilters, QDir::Dirs, QDir::Name));
297
                for (const QString& dir : dictDirList)
17501 craig 298
                        spellDirs.append(osxUserLibreOfficePath + "/" + dir + "/");
299
        }
300
 
301
#elif defined(_WIN32)
21937 jghali 302
        QString windowsLOPath("LibreOffice 3.5/share/extensions");
21527 craig 303
        QString progFiles = windowsSpecialDir(CSIDL_PROGRAM_FILES);
17501 craig 304
        d.setPath(progFiles+windowsLOPath);
305
        if (d.exists())
306
        {
307
                QStringList dictDirFilters("dict-*");
22669 jghali 308
                const QStringList dictDirList(d.entryList(dictDirFilters, QDir::Dirs, QDir::Name));
309
                for (const QString& dir : dictDirList)
17501 craig 310
                        spellDirs.append(progFiles+windowsLOPath + "/" + dir + "/");
311
        }
18204 fschmid 312
#elif defined(Q_OS_LINUX)
21934 craig 313
        QString linuxLocalPath("/usr/local/share/hunspell/");
314
        QString linuxHunspellPath("/usr/share/hunspell/");
315
        QString linuxMyspellPath("/usr/share/myspell/");
17501 craig 316
        d.setPath(linuxHunspellPath);
317
        if (d.exists())
318
                spellDirs.append(linuxHunspellPath);
319
        d.setPath(linuxMyspellPath);
320
        if (d.exists())
321
                spellDirs.append(linuxMyspellPath);
322
        d.setPath(linuxLocalPath);
323
        if (d.exists())
324
                spellDirs.append(linuxLocalPath);
325
#endif
326
        return spellDirs;
327
}
328
 
19800 craig 329
QStringList ScPaths::hyphDirs() const
330
{
331
        QDir d;
332
        QStringList hyphDirs;
21526 craig 333
        hyphDirs.append(userDictDir(ScPaths::Hyph, false));
19800 craig 334
        hyphDirs.append(m_shareDir + "dicts/hyph/");
335
#ifdef Q_OS_MAC
21939 craig 336
        QString macPortsPath("/opt/local/share/hunspell/");
337
        QString finkPath("/sw/share/hunspell/");
338
        QString osxLibreOfficePath("/Applications/LibreOffice.app/Contents/Resources/extensions");
339
        QString osxUserLibreOfficePath(QDir::homePath()+"/Applications/LibreOffice.app/Contents/Resources/extensions");
19800 craig 340
        d.setPath(macPortsPath);
341
        if (d.exists())
342
                hyphDirs.append(macPortsPath);
343
        d.setPath(finkPath);
344
        if (d.exists())
345
                hyphDirs.append(finkPath);
346
        d.setPath(osxLibreOfficePath);
347
        if (d.exists())
348
        {
349
                QStringList dictDirFilters("dict-*");
22669 jghali 350
                const QStringList dictDirList(d.entryList(dictDirFilters, QDir::Dirs, QDir::Name));
351
                for (const QString& dir : dictDirList)
19800 craig 352
                        hyphDirs.append(osxLibreOfficePath + "/" + dir + "/");
353
        }
354
        d.setPath(osxUserLibreOfficePath);
355
        if (d.exists())
356
        {
357
                QStringList dictDirFilters("dict-*");
22669 jghali 358
                const QStringList dictDirList(d.entryList(dictDirFilters, QDir::Dirs, QDir::Name));
359
                for (const QString& dir : dictDirList)
19800 craig 360
                        hyphDirs.append(osxUserLibreOfficePath + "/" + dir + "/");
361
        }
362
 
363
#elif defined(_WIN32)
21939 craig 364
        QString windowsLOPath("LibreOffice 3.5/share/extensions");
21527 craig 365
        QString progFiles = windowsSpecialDir(CSIDL_PROGRAM_FILES);
19800 craig 366
        d.setPath(progFiles+windowsLOPath);
367
        if (d.exists())
368
        {
369
                QStringList dictDirFilters("dict-*");
22669 jghali 370
                const QStringList dictDirList(d.entryList(dictDirFilters, QDir::Dirs, QDir::Name));
371
                for (const QString& dir : dictDirList)
19805 craig 372
                        hyphDirs.append(progFiles+windowsLOPath + "/" + dir + "/");
19800 craig 373
        }
374
#elif defined(Q_OS_LINUX)
21939 craig 375
        QString linuxLocalPath("/usr/local/share/hunspell/");
376
        QString linuxHunspellPath("/usr/share/hunspell/");
377
        QString linuxMyspellPath("/usr/share/myspell/");
378
        QString linuxHyphen1Path("/usr/share/hyphen/");
21078 jghali 379
        d.setPath(linuxHyphen1Path);
21077 craig 380
        if (d.exists())
381
                hyphDirs.append(linuxHyphen1Path);
19800 craig 382
        d.setPath(linuxHunspellPath);
383
        if (d.exists())
19805 craig 384
                hyphDirs.append(linuxHunspellPath);
19800 craig 385
        d.setPath(linuxMyspellPath);
386
        if (d.exists())
19805 craig 387
                hyphDirs.append(linuxMyspellPath);
19800 craig 388
        d.setPath(linuxLocalPath);
389
        if (d.exists())
19805 craig 390
                hyphDirs.append(linuxLocalPath);
19800 craig 391
#endif
392
        return hyphDirs;
393
}
394
 
22598 craig 395
QStringList ScPaths::systemFontDirs()
3096 fschmid 396
{
397
        QStringList fontDirs;
3102 avox 398
#ifdef Q_OS_MAC
10398 cbradney 399
        fontDirs.append(QDir::homePath() + "/Library/Fonts/");
3096 fschmid 400
        fontDirs.append("/Library/Fonts/");
401
        fontDirs.append("/Network/Library/Fonts/");
402
        fontDirs.append("/System/Library/Fonts/");
403
#elif defined(_WIN32)
22824 jghali 404
        QDir d;
405
        QString localFontDir = windowsSpecialDir(CSIDL_LOCAL_APPDATA)+"Microsoft/Windows/Fonts"; // Added by Windows 10 1809
406
        QString roamingFontDir = windowsSpecialDir(CSIDL_APPDATA)+"Microsoft/Windows/Fonts"; // Added by Windows 10 1809
407
        d.setPath(localFontDir);
408
        if (d.exists())
409
                fontDirs.append(localFontDir);
410
        d.setPath(roamingFontDir);
411
        if (d.exists())
412
                fontDirs.append(roamingFontDir);
22650 jghali 413
        fontDirs.append(windowsSpecialDir(CSIDL_FONTS));
3096 fschmid 414
#endif
415
        return fontDirs;
416
}
417
 
22598 craig 418
QStringList ScPaths::systemProfilesDirs()
3096 fschmid 419
{
420
        QStringList iccProfDirs;
3102 avox 421
#ifdef Q_OS_MAC
10398 cbradney 422
        iccProfDirs.append(QDir::homePath()+"/Library/ColorSync/Profiles/");
3096 fschmid 423
        iccProfDirs.append("/System/Library/ColorSync/Profiles/");
424
        iccProfDirs.append("/Library/ColorSync/Profiles/");
18204 fschmid 425
#elif defined(Q_OS_LINUX)
10398 cbradney 426
        iccProfDirs.append(QDir::homePath()+"/color/icc/");
427
        iccProfDirs.append(QDir::homePath()+"/.color/icc/");
18953 jghali 428
        iccProfDirs.append(QDir::homePath()+"/.local/share/icc/");
17620 craig 429
        iccProfDirs.append(QDir::homePath()+"/.local/share/color/icc/");
3096 fschmid 430
        iccProfDirs.append("/usr/share/color/icc/");
3961 mrdocs 431
        iccProfDirs.append("/usr/local/share/color/icc/");
18953 jghali 432
        iccProfDirs.append("/var/lib/color/icc/");
3096 fschmid 433
#elif defined(_WIN32)
434
        // On Windows it's more complicated, profiles location depends on OS version
10293 jghali 435
        WCHAR sysDir[MAX_PATH + 1];
3096 fschmid 436
        OSVERSIONINFO osVersion;
22650 jghali 437
        ZeroMemory(&osVersion, sizeof(OSVERSIONINFO));
3096 fschmid 438
        osVersion.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); // Necessary for GetVersionEx to succeed
439
        GetVersionEx(&osVersion);  // Get Windows version infos
22650 jghali 440
        GetSystemDirectoryW(sysDir, MAX_PATH); // windowsSpecialDir(CSIDL_SYSTEM) fails on Win9x
10293 jghali 441
        QString winSysDir = QString::fromUtf16((const ushort*) sysDir);
442
        winSysDir = winSysDir.replace('\\','/');
22650 jghali 443
        if (osVersion.dwPlatformId == VER_PLATFORM_WIN32_NT) // Windows NT/2k/XP
3096 fschmid 444
        {
22650 jghali 445
                if (osVersion.dwMajorVersion >= 5) // for 2k and XP dwMajorVersion == 5 
446
                        iccProfDirs.append(winSysDir + "/Spool/Drivers/Color/");
3096 fschmid 447
        }
22650 jghali 448
        else if (osVersion.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) // Windows 9x/Me 
3096 fschmid 449
        {
22650 jghali 450
                if (osVersion.dwMajorVersion >= 4 && osVersion.dwMinorVersion >= 10) // Win98 or WinMe
451
                        iccProfDirs.append(winSysDir + "/Color/");
3096 fschmid 452
        }
453
#endif
454
        return iccProfDirs;
455
}
456
 
22607 craig 457
QStringList ScPaths::dirsFromEnvVar(const QString& envVar, const QString& dirToFind)
15483 craig 458
{
22669 jghali 459
        QChar sep(ScPaths::envPathSeparator);
15496 craig 460
        QStringList dirs;
16262 jghali 461
#if defined(Q_OS_MAC) || defined(Q_OS_UNIX)
22669 jghali 462
        const QStringList env(QProcess::systemEnvironment());
15496 craig 463
        QString path_data;
22669 jghali 464
        for (const QString& line : env)
15483 craig 465
        {
15496 craig 466
                if (line.indexOf(envVar) == 0)
467
                        path_data = line.mid(envVar.length()+1); //eg, Strip "XDG_DATA_DIRS="
15483 craig 468
        }
22669 jghali 469
        const QStringList splitpath_data(path_data.split(sep, QString::SkipEmptyParts));
470
        for (QString dir : splitpath_data)
15483 craig 471
        {
15496 craig 472
                QFileInfo info(dir+dirToFind);
15483 craig 473
                if (info.exists())
15496 craig 474
                        dirs.append(dir+dirToFind);
15483 craig 475
        }
476
#endif
15496 craig 477
        return dirs;
15483 craig 478
}
15496 craig 479
 
480
 
21526 craig 481
 
22598 craig 482
QStringList ScPaths::systemCreatePalettesDirs()
4602 cbradney 483
{
484
        QStringList createDirs;
485
#ifdef Q_OS_MAC
10398 cbradney 486
        createDirs.append(QDir::homePath()+"/create/swatches/");
487
        createDirs.append(QDir::homePath()+"/.create/swatches/");
18204 fschmid 488
#elif defined(Q_OS_LINUX)
10398 cbradney 489
        createDirs.append(QDir::homePath()+"/create/swatches/");
490
        createDirs.append(QDir::homePath()+"/.create/swatches/");
4602 cbradney 491
        createDirs.append("/usr/share/create/swatches/");
492
        createDirs.append("/usr/local/share/create/swatches/");
493
#elif defined(_WIN32)
21527 craig 494
        QString localAppData = windowsSpecialDir(CSIDL_LOCAL_APPDATA);
495
        QString commonAppData = windowsSpecialDir(CSIDL_COMMON_APPDATA);
496
        QString programFilesCommon = windowsSpecialDir(CSIDL_PROGRAM_FILES_COMMON);
497
        createDirs.append(windowsSpecialDir(CSIDL_APPDATA) + "create/swatches/");
22650 jghali 498
        if (!localAppData.isEmpty())
4689 mrdocs 499
                createDirs.append(localAppData + "create/swatches/");
22650 jghali 500
        if (!commonAppData.isEmpty())
4689 mrdocs 501
                createDirs.append(commonAppData + "create/swatches/");
22650 jghali 502
        if (!programFilesCommon.isEmpty())
4689 mrdocs 503
                createDirs.append(programFilesCommon + "create/swatches/");
4602 cbradney 504
#endif
505
        return createDirs;
506
}
507
 
22598 craig 508
QString ScPaths::oldApplicationDataDir()
21530 craig 509
{
510
#ifdef Q_OS_WIN32
511
        QString appData = windowsSpecialDir(CSIDL_APPDATA);
512
        if (QDir(appData).exists())
513
#ifdef APPLICATION_DATA_DIR
514
        return (appData + "/" + APPLICATION_DATA_DIR + "/");
515
#else
516
        return (appData + "/Scribus/");
517
#endif
518
#endif
519
 
520
#ifdef APPLICATION_DATA_DIR
521
        return QDir::homePath() + "/" + APPLICATION_DATA_DIR + "/";
522
#else
523
        #ifdef Q_OS_MAC
524
                return (QDir::homePath() + "/Library/Preferences/Scribus/");
525
        #else
526
                return (QDir::homePath() + "/.scribus/");
527
        #endif
528
#endif
529
}
530
 
21526 craig 531
QString ScPaths::applicationDataDir(bool createIfNotExists)
5917 jghali 532
{
21526 craig 533
        QString dataDir;
13066 subik 534
#ifdef APPLICATION_DATA_DIR
22160 jghali 535
        dataDir =  QDir::homePath() + "/" + APPLICATION_DATA_DIR + "/";
13066 subik 536
#else
21526 craig 537
        dataDir =  QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/";
5917 jghali 538
#endif
21526 craig 539
        QDir prefsDirectory(dataDir);
540
        if (createIfNotExists && !prefsDirectory.exists())
541
                prefsDirectory.mkpath(prefsDirectory.absolutePath());
542
        return dataDir;
543
}
13066 subik 544
 
21526 craig 545
QString ScPaths::preferencesDir(bool createIfNotExists)
546
{
21532 craig 547
        //If we have been passed a dir on cmd line, use this. Start up errors if it does not exist.
548
        if (!ScQApp->userPrefsDir().isEmpty())
549
                return ScQApp->userPrefsDir();
21526 craig 550
        QString prefsDir;
551
#ifdef APPLICATION_CONFIG_DIR
552
        prefsDir =  QDir::homePath() + "/" + APPLICATION_CONFIG_DIR + "/";
13066 subik 553
#else
21527 craig 554
        //Jean wants to make all prefs for Scribus be in the roaming directory on Windows so return the same as applicationDataDir
21526 craig 555
        #ifdef Q_OS_WIN32
556
                prefsDir =  QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/";
13171 cbradney 557
        #else
21526 craig 558
                prefsDir =  QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/";
13171 cbradney 559
        #endif
13066 subik 560
#endif
21526 craig 561
        QDir prefsDirectory(prefsDir);
562
        if (createIfNotExists && !prefsDirectory.exists())
563
                prefsDirectory.mkpath(prefsDirectory.absolutePath());
564
        return prefsDir;
5917 jghali 565
}
566
 
22598 craig 567
QString ScPaths::imageCacheDir()
14539 jghali 568
{
21526 craig 569
        return applicationDataDir() + "cache/img/";
14539 jghali 570
}
571
 
21526 craig 572
QString ScPaths::pluginDataDir(bool createIfNotExists)
13608 jghali 573
{
21526 craig 574
        QDir useFilesDirectory(applicationDataDir() + "plugins/");
22650 jghali 575
        if (createIfNotExists && !useFilesDirectory.exists())
21526 craig 576
                useFilesDirectory.mkpath(useFilesDirectory.absolutePath());
24035 jghali 577
        return useFilesDirectory.absolutePath() + "/";
13608 jghali 578
}
579
 
21526 craig 580
QString ScPaths::userDictDir(ScPaths::DictType dictType, bool createIfNotExists)
17525 craig 581
{
20367 craig 582
        QString dictSuffix;
583
        if (dictType == ScPaths::Hyph)
584
                dictSuffix = "hyph/";
585
        if (dictType == ScPaths::Spell)
586
                dictSuffix = "spell/";
21526 craig 587
        QDir useFilesDirectory(applicationDataDir() + "dicts/" + dictSuffix);
22650 jghali 588
        if (createIfNotExists && !useFilesDirectory.exists())
21526 craig 589
                useFilesDirectory.mkpath(useFilesDirectory.absolutePath());
24035 jghali 590
        return useFilesDirectory.absolutePath() + "/";
17525 craig 591
}
592
 
21526 craig 593
QString ScPaths::userFontDir(bool createIfNotExists)
19821 craig 594
{
21526 craig 595
        QDir useFilesDirectory(applicationDataDir() + "fonts/");
22650 jghali 596
        if (createIfNotExists && !useFilesDirectory.exists())
21526 craig 597
                useFilesDirectory.mkpath(useFilesDirectory.absolutePath());
24035 jghali 598
        return useFilesDirectory.absolutePath() + "/";
19821 craig 599
}
600
 
21526 craig 601
QString ScPaths::userHelpFilesDir(bool createIfNotExists)
21028 craig 602
{
21526 craig 603
        QDir useFilesDirectory(applicationDataDir() + "helpfiles/");
22650 jghali 604
        if (createIfNotExists && !useFilesDirectory.exists())
21526 craig 605
                useFilesDirectory.mkpath(useFilesDirectory.absolutePath());
24035 jghali 606
        return useFilesDirectory.absolutePath() + "/";
21028 craig 607
}
608
 
21526 craig 609
QString ScPaths::userPaletteFilesDir(bool createIfNotExists)
21081 craig 610
{
21526 craig 611
        QDir useFilesDirectory(applicationDataDir() + "palettes/");
22650 jghali 612
        if (createIfNotExists && !useFilesDirectory.exists())
21081 craig 613
        {
21526 craig 614
                useFilesDirectory.mkpath(useFilesDirectory.absolutePath());
615
                useFilesDirectory.mkpath(useFilesDirectory.absolutePath() + "/locked");
21081 craig 616
        }
24035 jghali 617
        return useFilesDirectory.absolutePath() + "/";
21081 craig 618
}
619
 
21817 craig 620
QString ScPaths::userTemplateDir(bool createIfNotExists)
621
{
23060 craig 622
        if (PrefsManager::instance().appPrefs.pathPrefs.documentTemplates.isEmpty())
21817 craig 623
                return QString();
23060 craig 624
        QDir useFilesDirectory(PrefsManager::instance().appPrefs.pathPrefs.documentTemplates);
22650 jghali 625
        if (createIfNotExists && !useFilesDirectory.exists())
21817 craig 626
                useFilesDirectory.mkpath(useFilesDirectory.absolutePath());
24035 jghali 627
        return useFilesDirectory.absolutePath() + "/";
21817 craig 628
}
629
 
22598 craig 630
QString ScPaths::userDocumentDir()
5917 jghali 631
{
21520 craig 632
        QString userDocs = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
633
        if (QDir(userDocs).exists())
21524 craig 634
                return userDocs + "/";
635
        return QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + "/";
5917 jghali 636
}
637
 
21526 craig 638
QString ScPaths::scrapbookDir(bool createIfNotExists)
5917 jghali 639
{
21526 craig 640
        QDir useFilesDirectory(applicationDataDir() + "scrapbook/");
24038 jghali 641
        if (createIfNotExists)
21526 craig 642
        {
24038 jghali 643
                if (!useFilesDirectory.exists())
644
                        useFilesDirectory.mkpath(useFilesDirectory.absolutePath());
645
                if (!useFilesDirectory.exists("main"))
646
                        useFilesDirectory.mkpath(useFilesDirectory.absolutePath() + "/main");
647
                if (!useFilesDirectory.exists("tmp"))
648
                        useFilesDirectory.mkpath(useFilesDirectory.absolutePath() + "/tmp");
21526 craig 649
        }
24035 jghali 650
        return useFilesDirectory.absolutePath() + "/";
21526 craig 651
}
652
 
22598 craig 653
QString ScPaths::tempFileDir()
21526 craig 654
{
655
#ifdef Q_OS_WIN32
5917 jghali 656
        QString tempPath;
657
        WCHAR wTempPath[1024];
658
        DWORD result = GetTempPathW(1024, wTempPath);
22650 jghali 659
        if (result)
5917 jghali 660
        {
10293 jghali 661
                tempPath = QString::fromUtf16((const unsigned short*) wTempPath);
22650 jghali 662
                tempPath.replace('\\', '/');
5917 jghali 663
                tempPath += "/";
664
                // GetTempPath may return Windows directory, better not use this one
665
                // for temporary files
21527 craig 666
                if (QDir(tempPath).exists() && tempPath != windowsSpecialDir(CSIDL_WINDOWS))
5917 jghali 667
                        return tempPath;
668
        }
13265 cbradney 669
#endif
19969 craig 670
 
21526 craig 671
        QDir tempAppDirectory(applicationDataDir() + "temp/");
19969 craig 672
        if (!tempAppDirectory.exists())
673
                tempAppDirectory.mkpath(tempAppDirectory.absolutePath());
24035 jghali 674
        return tempAppDirectory.absolutePath() + "/";
5917 jghali 675
}
676
 
17525 craig 677
QString ScPaths::downloadDir()
678
{
21526 craig 679
        QDir downloadDirectory(applicationDataDir() + "downloads/");
17525 craig 680
        if (!downloadDirectory.exists())
681
                downloadDirectory.mkpath(downloadDirectory.absolutePath());
24035 jghali 682
        return downloadDirectory.absolutePath() + "/";
17525 craig 683
}
684
 
21526 craig 685
QString ScPaths::windowsSpecialDir(int folder)
3096 fschmid 686
{
687
        QString qstr;
21526 craig 688
#ifdef Q_OS_WIN32
10293 jghali 689
        WCHAR dir[256];
22650 jghali 690
        if (SHGetSpecialFolderPathW(nullptr, dir, folder , false))
4689 mrdocs 691
        {
10293 jghali 692
                qstr = QString::fromUtf16((const unsigned short*) dir);
22650 jghali 693
                if (!qstr.endsWith("\\"))
4689 mrdocs 694
                        qstr += "\\";
22650 jghali 695
                qstr.replace('\\', '/');
4689 mrdocs 696
        }
3096 fschmid 697
#else
698
        Q_ASSERT(false);
699
#endif
700
        return qstr;
701
}