Subversion Repositories Scribus

Rev

Rev 19761 | 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>
1693 craig 11
 
2688 craig 12
#include "scconfig.h"
2673 cbradney 13
 
1693 craig 14
// On Qt/Mac we need CoreFoundation to discover the location
15
// of the app bundle.
9596 avox 16
#ifdef Q_WS_MAC
1693 craig 17
#include <CoreFoundation/CoreFoundation.h>
18
#endif
19
 
3096 fschmid 20
#ifdef _WIN32
21
#include <windows.h>
22
#include <shlobj.h>
23
#endif
24
 
4194 fschmid 25
#ifdef _WIN32
26
const char ScPaths::envPathSeparator = ';';
27
#else
28
const char ScPaths::envPathSeparator = ':';
29
#endif
30
 
1693 craig 31
// Init the singleton's "self" address to NULL
32
ScPaths* ScPaths::m_instance = NULL;
33
 
34
// Singleton's public constructor
35
const ScPaths& ScPaths::instance()
36
{
37
        if (!ScPaths::m_instance)
38
                ScPaths::m_instance = new ScPaths();
39
        return *ScPaths::m_instance;
40
}
41
 
42
// Singleton's public destructor
43
void ScPaths::destroy()
44
{
45
        if (ScPaths::m_instance)
46
                delete ScPaths::m_instance;
47
}
48
 
49
// Protected "real" constructor
50
// All paths are initialized to compile-time defaults passed in
51
// as preprocessor macros and set by autoconf.
52
ScPaths::ScPaths() :
53
        m_docDir(DOCDIR),
19761 craig 54
        m_fontDir(),
1693 craig 55
        m_iconDir(ICONDIR),
56
        m_libDir(LIBDIR),
57
        m_pluginDir(PLUGINDIR),
58
        m_sampleScriptDir(SAMPLESDIR),
59
        m_scriptDir(SCRIPTSDIR),
2707 subik 60
        m_templateDir(TEMPLATEDIR),
61
        m_shareDir(SHAREDIR)
1693 craig 62
{
63
// On MacOS/X, override the compile-time settings with a location
64
// obtained from the system.
9596 avox 65
#ifdef Q_WS_MAC
13179 cbradney 66
        QString pathPtr(bundleDir());
67
        /*
1693 craig 68
        // Set up the various app paths to look inside the app bundle
69
        CFURLRef pluginRef = CFBundleCopyBundleURL(CFBundleGetMainBundle());
70
        CFStringRef macPath = CFURLCopyFileSystemPath(pluginRef,
71
                                               kCFURLPOSIXPathStyle);
72
        const char *pathPtr = CFStringGetCStringPtr(macPath,
73
                                               CFStringGetSystemEncoding());
3546 avox 74
 
75
        // make sure we get the Scribus.app directory, not some subdir
76
 
77
        // strip trailing '/':
3459 avox 78
        char *p = const_cast<char*>(pathPtr + strlen(pathPtr) - 1);
2673 cbradney 79
        while (*p == '/')
80
                --p;
81
        ++p;
82
        *p = '\0';
83
        if (strcmp("/bin", p-4) == 0) {
84
                p -= 4;
85
                *p = '\0';
86
        }
87
        if (strcmp("/MacOS", p-6) == 0) {
88
                p -= 6;
89
                *p = '\0';
90
        }
91
        if (strcmp("/Contents", p-9) == 0) {
92
                p -= 9;
93
                *p = '\0';
94
        }
13179 cbradney 95
*/
10755 cbradney 96
        qDebug() << QString("scpaths: bundle at %1:").arg(pathPtr);
3794 cbradney 97
        m_shareDir = QString("%1/Contents/share/scribus/").arg(pathPtr);
12543 cbradney 98
        m_docDir = QString("%1/Contents/share/doc/scribus/").arg(pathPtr);
19761 craig 99
        m_fontDir = QString("%1/Contents/share/scribus/fonts/").arg(pathPtr);
3794 cbradney 100
        m_iconDir = QString("%1/Contents/share/scribus/icons/").arg(pathPtr);
101
        m_sampleScriptDir = QString("%1/Contents/share/scribus/samples/").arg(pathPtr);
102
        m_scriptDir = QString("%1/Contents/share/scribus/scripts/").arg(pathPtr);
103
        m_templateDir = QString("%1/Contents/share/scribus/templates/").arg(pathPtr);
19720 craig 104
        m_libDir = QString("%1/Contents/lib/").arg(pathPtr);
105
        m_pluginDir = QString("%1/Contents/lib/").arg(pathPtr);
18968 craig 106
//      QApplication::setLibraryPaths(QStringList(QString("%1/Contents/lib/qtplugins/").arg(pathPtr)));
19720 craig 107
        QApplication::addLibraryPath(QString("%1/Contents/PlugIns/").arg(pathPtr));
13179 cbradney 108
//      CFRelease(pluginRef);
109
//      CFRelease(macPath);
3546 avox 110
 
111
        // on OSX this goes to the sys console, so user only sees it when they care -- AV
10755 cbradney 112
        qDebug() << QString("scpaths: doc dir=%1").arg(m_docDir);
113
        qDebug() << QString("scpaths: icon dir=%1").arg(m_iconDir);
114
        qDebug() << QString("scpaths: font dir=%1").arg(m_fontDir);
115
        qDebug() << QString("scpaths: sample dir=%1").arg(m_sampleScriptDir);
116
        qDebug() << QString("scpaths: script dir=%1").arg(m_scriptDir);
117
        qDebug() << QString("scpaths: template dir=%1").arg(m_templateDir);
118
        qDebug() << QString("scpaths: lib dir=%1").arg(m_libDir);
119
        qDebug() << QString("scpaths: plugin dir=%1").arg(m_pluginDir);
120
        qDebug() << QString("scpaths: qtplugins=%1").arg(QApplication::libraryPaths().join(":"));
3546 avox 121
 
3096 fschmid 122
#elif defined(_WIN32)
123
        QString appPath = qApp->applicationDirPath();
3794 cbradney 124
        m_shareDir = QString("%1/share/").arg(appPath);
125
        m_docDir = QString("%1/share/doc/").arg(appPath);
126
        m_fontDir = QString("%1/share/fonts/").arg(appPath);
127
        m_iconDir = QString("%1/share/icons/").arg(appPath);
128
        m_sampleScriptDir = QString("%1/share/samples/").arg(appPath);
129
        m_scriptDir = QString("%1/share/scripts/").arg(appPath);
130
        m_templateDir = QString("%1/share/templates/").arg(appPath);
131
        m_libDir = QString("%1/libs/").arg(appPath);
132
        m_pluginDir = QString("%1/plugins/").arg(appPath);
18435 jghali 133
 
134
        QString qtpluginDir = QString("%1/qtplugins/").arg(appPath);
135
        if (QDir(qtpluginDir).exists())
136
                QApplication::setLibraryPaths( QStringList(qtpluginDir) );
3096 fschmid 137
#endif
7407 cbradney 138
 
139
//      if(!m_shareDir.endsWith("/"))        m_shareDir += "/";
140
//      if(!m_docDir.endsWith("/"))          m_docDir += "/";
141
//      if(!m_fontDir.endsWith("/"))         m_fontDir += "/";
7430 cbradney 142
        if(!m_iconDir.endsWith("/"))         m_iconDir += "/";
7407 cbradney 143
//      if(!m_sampleScriptDir.endsWith("/")) m_sampleScriptDir += "/";
144
//      if(!m_scriptDir.endsWith("/"))       m_scriptDir += "/";
145
//      if(!m_templateDir.endsWith("/"))     m_templateDir += "/";
146
//      if(!m_libDir.endsWith("/"))          m_libDir += "/";
147
//      if(!m_pluginDir.endsWith("/"))       m_pluginDir += "/";
1693 craig 148
}
149
 
150
ScPaths::~ScPaths() {};
151
 
13179 cbradney 152
QString ScPaths::bundleDir(void) const
153
{
154
        // On MacOS/X, override the compile-time settings with a location
15414 craig 155
        // obtained from the system.
13179 cbradney 156
#ifdef Q_WS_MAC
157
        // Set up the various app paths to look inside the app bundle
158
        CFURLRef pluginRef = CFBundleCopyBundleURL(CFBundleGetMainBundle());
15414 craig 159
        CFStringRef macPath = CFURLCopyFileSystemPath(pluginRef, kCFURLPOSIXPathStyle);
160
        const char *pathPtr = CFStringGetCStringPtr(macPath, CFStringGetSystemEncoding());
161
        if (pathPtr!=NULL && strlen(pathPtr)>0)
162
        {
163
                // make sure we get the Scribus.app directory, not some subdir
164
                // strip trailing '/':
165
                qDebug("Path = %s", pathPtr);
166
                char *p = const_cast<char*>(pathPtr + strlen(pathPtr) - 1);
167
                while (*p == '/')
168
                        --p;
169
                ++p;
13179 cbradney 170
                *p = '\0';
15414 craig 171
                if (strcmp("/bin", p-4) == 0) {
172
                        p -= 4;
173
                        *p = '\0';
174
                }
175
                if (strcmp("/MacOS", p-6) == 0) {
176
                        p -= 6;
177
                        *p = '\0';
178
                }
179
                if (strcmp("/Contents", p-9) == 0) {
180
                        p -= 9;
181
                        *p = '\0';
182
                }
183
                CFRelease(pluginRef);
184
                CFRelease(macPath);
185
                return QString("%1").arg(pathPtr);
13179 cbradney 186
        }
15414 craig 187
        else
188
        {
189
                char buf[2048];
190
                CFStringGetCString (macPath, buf, 2048, kCFStringEncodingUTF8);
191
                QString q_pathPtr=QString::fromUtf8(buf);
192
                if (q_pathPtr.endsWith("/bin"))
193
                        q_pathPtr.chop(4);
194
                if (q_pathPtr.endsWith("/MacOS"))
195
                        q_pathPtr.chop(6);
196
                if (q_pathPtr.endsWith("/Contents"))
197
                        q_pathPtr.chop(9);
198
                CFRelease(pluginRef);
199
                CFRelease(macPath);
200
                return q_pathPtr;
13179 cbradney 201
        }
202
#endif
203
        return QString::null;
204
}
205
 
1693 craig 206
const QString&  ScPaths::docDir() const
207
{
208
        return m_docDir;
209
}
210
 
211
const QString&  ScPaths::iconDir() const
212
{
213
        return m_iconDir;
214
}
215
 
3794 cbradney 216
const QString&  ScPaths::fontDir() const
217
{
218
        return m_fontDir;
219
}
220
 
1693 craig 221
const QString&  ScPaths::libDir() const
222
{
223
        return m_libDir;
224
}
225
 
226
const QString&  ScPaths::pluginDir() const
227
{
228
        return m_pluginDir;
229
}
230
 
231
const QString&  ScPaths::sampleScriptDir() const
232
{
233
        return m_sampleScriptDir;
234
}
235
 
236
const QString&  ScPaths::scriptDir() const
237
{
238
        return m_scriptDir;
239
}
240
 
241
const QString&  ScPaths::templateDir() const
242
{
243
        return m_templateDir;
244
}
245
 
2707 subik 246
const QString&  ScPaths::shareDir() const
247
{
248
        return m_shareDir;
249
}
3096 fschmid 250
 
10997 jghali 251
QString ScPaths::translationDir() const
252
{
253
        return (m_shareDir + "translations/");
254
}
255
 
11120 malex 256
QString ScPaths::dictDir() const
257
{
258
        return(m_shareDir + "dicts/");
259
}
260
 
17455 craig 261
QStringList ScPaths::spellDirs() const
262
{
263
        //dictionaryPaths
264
        QString macPortsPath("/opt/local/share/hunspell/");
265
        QString finkPath("/sw/share/hunspell/");
266
        QString osxLibreOfficePath("/Applications/LibreOffice.app/Contents/share/extensions");
267
        QString osxUserLibreOfficePath(QDir::homePath()+"/Applications/LibreOffice.app/Contents/share/extensions");
268
        QString linuxLocalPath("/usr/local/share/hunspell/");
17524 craig 269
        QString linuxHunspellPath("/usr/share/hunspell/");
270
        QString linuxMyspellPath("/usr/share/myspell/");
17455 craig 271
        QString windowsLOPath("LibreOffice 3.5/share/extensions");
272
        QDir d;
273
        QStringList spellDirs;
17524 craig 274
        spellDirs.append(getUserDictDir(false));
17455 craig 275
        spellDirs.append(m_shareDir + "dicts/spelling/");
276
#ifdef Q_OS_MAC
277
        d.setPath(macPortsPath);
278
        if (d.exists())
279
                spellDirs.append(macPortsPath);
280
        d.setPath(finkPath);
281
        if (d.exists())
282
                spellDirs.append(finkPath);
283
        d.setPath(osxLibreOfficePath);
284
        if (d.exists())
17494 craig 285
        {
17495 craig 286
                QStringList dictDirFilters("dict-*");
17494 craig 287
                QStringList dictDirList(d.entryList(dictDirFilters, QDir::Dirs, QDir::Name));
288
                QString dir;
289
                foreach (dir, dictDirList)
290
                        spellDirs.append(osxLibreOfficePath + "/" + dir + "/");
291
        }
17455 craig 292
        d.setPath(osxUserLibreOfficePath);
293
        if (d.exists())
17494 craig 294
        {
17495 craig 295
                QStringList dictDirFilters("dict-*");
17494 craig 296
                QStringList dictDirList(d.entryList(dictDirFilters, QDir::Dirs, QDir::Name));
297
                QString dir;
298
                foreach (dir, dictDirList)
299
                        spellDirs.append(osxUserLibreOfficePath + "/" + dir + "/");
300
        }
301
 
17455 craig 302
#elif defined(_WIN32)
303
        QString progFiles = getSpecialDir(CSIDL_PROGRAM_FILES);
304
        d.setPath(progFiles+windowsLOPath);
305
        if (d.exists())
17494 craig 306
        {
17495 craig 307
                QStringList dictDirFilters("dict-*");
17494 craig 308
                QStringList dictDirList(d.entryList(dictDirFilters, QDir::Dirs, QDir::Name));
309
                QString dir;
310
                foreach (dir, dictDirList)
311
                        spellDirs.append(progFiles+windowsLOPath + "/" + dir + "/");
312
        }
17455 craig 313
#elif defined(Q_WS_X11)
17524 craig 314
        d.setPath(linuxHunspellPath);
17455 craig 315
        if (d.exists())
17524 craig 316
                spellDirs.append(linuxHunspellPath);
317
        d.setPath(linuxMyspellPath);
318
        if (d.exists())
319
                spellDirs.append(linuxMyspellPath);
17455 craig 320
        d.setPath(linuxLocalPath);
321
        if (d.exists())
322
                spellDirs.append(linuxLocalPath);
323
#endif
324
        return spellDirs;
325
}
326
 
3096 fschmid 327
QStringList ScPaths::getSystemFontDirs(void)
328
{
329
        QStringList fontDirs;
3102 avox 330
#ifdef Q_OS_MAC
10398 cbradney 331
        fontDirs.append(QDir::homePath() + "/Library/Fonts/");
3096 fschmid 332
        fontDirs.append("/Library/Fonts/");
333
        fontDirs.append("/Network/Library/Fonts/");
334
        fontDirs.append("/System/Library/Fonts/");
335
#elif defined(_WIN32)
22825 jghali 336
        QDir d;
337
        QString localFontDir = getSpecialDir(CSIDL_LOCAL_APPDATA)+"Microsoft/Windows/Fonts"; // Added by Windows 10 1809
338
        QString roamingFontDir = getSpecialDir(CSIDL_APPDATA)+"Microsoft/Windows/Fonts"; // Added by Windows 10 1809
339
        d.setPath(localFontDir);
340
        if (d.exists())
341
                fontDirs.append(localFontDir);
342
        d.setPath(roamingFontDir);
343
        if (d.exists())
344
                fontDirs.append(roamingFontDir);
3096 fschmid 345
        fontDirs.append( getSpecialDir(CSIDL_FONTS) );
346
#endif
347
        return fontDirs;
348
}
349
 
350
QStringList ScPaths::getSystemProfilesDirs(void)
351
{
352
        QStringList iccProfDirs;
3102 avox 353
#ifdef Q_OS_MAC
10398 cbradney 354
        iccProfDirs.append(QDir::homePath()+"/Library/ColorSync/Profiles/");
3096 fschmid 355
        iccProfDirs.append("/System/Library/ColorSync/Profiles/");
356
        iccProfDirs.append("/Library/ColorSync/Profiles/");
357
#elif defined(Q_WS_X11)
10398 cbradney 358
        iccProfDirs.append(QDir::homePath()+"/color/icc/");
359
        iccProfDirs.append(QDir::homePath()+"/.color/icc/");
18952 jghali 360
        iccProfDirs.append(QDir::homePath()+"/.local/share/icc/");
17619 craig 361
        iccProfDirs.append(QDir::homePath()+"/.local/share/color/icc/");
3096 fschmid 362
        iccProfDirs.append("/usr/share/color/icc/");
3961 mrdocs 363
        iccProfDirs.append("/usr/local/share/color/icc/");
18952 jghali 364
        iccProfDirs.append("/var/lib/color/icc/");
3096 fschmid 365
#elif defined(_WIN32)
366
        // On Windows it's more complicated, profiles location depends on OS version
10293 jghali 367
        WCHAR sysDir[MAX_PATH + 1];
3096 fschmid 368
        OSVERSIONINFO osVersion;
369
        ZeroMemory( &osVersion, sizeof(OSVERSIONINFO));
370
        osVersion.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); // Necessary for GetVersionEx to succeed
371
        GetVersionEx(&osVersion);  // Get Windows version infos
10293 jghali 372
        GetSystemDirectoryW( sysDir, MAX_PATH ); // getSpecialDir(CSIDL_SYSTEM) fails on Win9x
373
        QString winSysDir = QString::fromUtf16((const ushort*) sysDir);
374
        winSysDir = winSysDir.replace('\\','/');
3096 fschmid 375
        if( osVersion.dwPlatformId == VER_PLATFORM_WIN32_NT     ) // Windows NT/2k/XP
376
        {
377
                if( osVersion.dwMajorVersion >= 5 ) // for 2k and XP dwMajorVersion == 5 
378
                        iccProfDirs.append( winSysDir + "/Spool/Drivers/Color/");
379
        }
380
        else if( osVersion.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS ) // Windows 9x/Me 
381
        {
382
                if( osVersion.dwMajorVersion >= 4 && osVersion.dwMinorVersion >= 10) // Win98 or WinMe
383
                        iccProfDirs.append( winSysDir + "/Color/");
384
        }
385
#endif
386
        return iccProfDirs;
387
}
388
 
4805 cbradney 389
QStringList ScPaths::getSystemCreateSwatchesDirs(void)
4602 cbradney 390
{
391
        QStringList createDirs;
392
#ifdef Q_OS_MAC
10398 cbradney 393
        createDirs.append(QDir::homePath()+"/create/swatches/");
394
        createDirs.append(QDir::homePath()+"/.create/swatches/");
4602 cbradney 395
#elif defined(Q_WS_X11)
10398 cbradney 396
        createDirs.append(QDir::homePath()+"/create/swatches/");
397
        createDirs.append(QDir::homePath()+"/.create/swatches/");
4602 cbradney 398
        createDirs.append("/usr/share/create/swatches/");
399
        createDirs.append("/usr/local/share/create/swatches/");
400
#elif defined(_WIN32)
4689 mrdocs 401
        QString localAppData = getSpecialDir(CSIDL_LOCAL_APPDATA);
402
        QString commonAppData = getSpecialDir(CSIDL_COMMON_APPDATA);
403
        QString programFilesCommon = getSpecialDir(CSIDL_PROGRAM_FILES_COMMON);
404
        createDirs.append(getSpecialDir(CSIDL_APPDATA) + "create/swatches/");
405
        if ( !localAppData.isEmpty() )
406
                createDirs.append(localAppData + "create/swatches/");
407
        if ( !commonAppData.isEmpty() )
408
                createDirs.append(commonAppData + "create/swatches/");
409
        if ( !programFilesCommon.isEmpty() )
410
                createDirs.append(programFilesCommon + "create/swatches/");
4602 cbradney 411
#endif
412
        return createDirs;
413
}
414
 
5917 jghali 415
QString ScPaths::getApplicationDataDir(void)
416
{
417
#if defined(_WIN32)
418
        QString appData = getSpecialDir(CSIDL_APPDATA);
419
        if (QDir(appData).exists())
16450 craig 420
        #ifdef APPLICATION_DATA_DIR
13066 subik 421
                return (appData + "/" + APPLICATION_DATA_DIR + "/");
16450 craig 422
        #else
5917 jghali 423
                return (appData + "/Scribus/");
16450 craig 424
        #endif
5917 jghali 425
#endif
13066 subik 426
 
427
#ifdef APPLICATION_DATA_DIR
428
        return QDir::homePath() + "/" + APPLICATION_DATA_DIR + "/";
429
#else
13171 cbradney 430
        #ifdef Q_OS_MAC
431
                return (QDir::homePath() + "/Library/Preferences/Scribus/");
432
        #else
433
                return (QDir::homePath() + "/.scribus/");
434
        #endif
13066 subik 435
#endif
5917 jghali 436
}
437
 
17524 craig 438
QString ScPaths::getUserDictDir(bool createIfNotExists)
439
{
440
        QDir userDictDirectory(getApplicationDataDir() + "dicts/");
441
        if(createIfNotExists)
442
        {
443
                if (!userDictDirectory.exists())
444
                        userDictDirectory.mkpath(userDictDirectory.absolutePath());
445
        }
446
        return userDictDirectory.absolutePath()+"/";
447
}
448
 
5917 jghali 449
QString ScPaths::getUserDocumentDir(void)
450
{
451
#if defined(_WIN32)
452
        QString userDocs = getSpecialDir(CSIDL_PERSONAL);
453
        if      (QDir(userDocs).exists())
454
                return userDocs;
455
#endif
10398 cbradney 456
        return (QDir::homePath() + "/");
5917 jghali 457
}
458
 
459
QString ScPaths::getTempFileDir(void)
460
{
461
#if defined(_WIN32)
462
        QString tempPath;
463
        WCHAR wTempPath[1024];
464
        DWORD result = GetTempPathW(1024, wTempPath);
465
        if ( result )
466
        {
10293 jghali 467
                tempPath = QString::fromUtf16((const unsigned short*) wTempPath);
5917 jghali 468
                tempPath.replace( '\\', '/' );
469
                tempPath += "/";
470
                // GetTempPath may return Windows directory, better not use this one
471
                // for temporary files
472
                if (QDir(tempPath).exists() && tempPath != getSpecialDir(CSIDL_WINDOWS))
473
                        return tempPath;
474
        }
13265 cbradney 475
#endif
5917 jghali 476
        return getApplicationDataDir();
477
}
478
 
17524 craig 479
QString ScPaths::downloadDir()
480
{
481
        QDir downloadDirectory(getApplicationDataDir() + "downloads/");
482
        if (!downloadDirectory.exists())
483
                downloadDirectory.mkpath(downloadDirectory.absolutePath());
484
        return downloadDirectory.absolutePath()+"/";
485
}
486
 
4169 mrdocs 487
QString ScPaths::getSpecialDir(int folder)
3096 fschmid 488
{
489
        QString qstr;
490
#if defined(_WIN32)
10293 jghali 491
        WCHAR dir[256];
492
        if ( SHGetSpecialFolderPathW(NULL, dir, folder , false) )
4689 mrdocs 493
        {
10293 jghali 494
                qstr = QString::fromUtf16((const unsigned short*) dir);
4689 mrdocs 495
                if( !qstr.endsWith("\\") )
496
                        qstr += "\\";
497
                qstr.replace( '\\', '/' );
498
        }
3096 fschmid 499
#else
500
        Q_ASSERT(false);
501
#endif
502
        return qstr;
503
}