Subversion Repositories Scribus

Rev

Rev 19720 | Rev 22825 | 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)
336
        fontDirs.append( getSpecialDir(CSIDL_FONTS) );
337
#endif
338
        return fontDirs;
339
}
340
 
341
QStringList ScPaths::getSystemProfilesDirs(void)
342
{
343
        QStringList iccProfDirs;
3102 avox 344
#ifdef Q_OS_MAC
10398 cbradney 345
        iccProfDirs.append(QDir::homePath()+"/Library/ColorSync/Profiles/");
3096 fschmid 346
        iccProfDirs.append("/System/Library/ColorSync/Profiles/");
347
        iccProfDirs.append("/Library/ColorSync/Profiles/");
348
#elif defined(Q_WS_X11)
10398 cbradney 349
        iccProfDirs.append(QDir::homePath()+"/color/icc/");
350
        iccProfDirs.append(QDir::homePath()+"/.color/icc/");
18952 jghali 351
        iccProfDirs.append(QDir::homePath()+"/.local/share/icc/");
17619 craig 352
        iccProfDirs.append(QDir::homePath()+"/.local/share/color/icc/");
3096 fschmid 353
        iccProfDirs.append("/usr/share/color/icc/");
3961 mrdocs 354
        iccProfDirs.append("/usr/local/share/color/icc/");
18952 jghali 355
        iccProfDirs.append("/var/lib/color/icc/");
3096 fschmid 356
#elif defined(_WIN32)
357
        // On Windows it's more complicated, profiles location depends on OS version
10293 jghali 358
        WCHAR sysDir[MAX_PATH + 1];
3096 fschmid 359
        OSVERSIONINFO osVersion;
360
        ZeroMemory( &osVersion, sizeof(OSVERSIONINFO));
361
        osVersion.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); // Necessary for GetVersionEx to succeed
362
        GetVersionEx(&osVersion);  // Get Windows version infos
10293 jghali 363
        GetSystemDirectoryW( sysDir, MAX_PATH ); // getSpecialDir(CSIDL_SYSTEM) fails on Win9x
364
        QString winSysDir = QString::fromUtf16((const ushort*) sysDir);
365
        winSysDir = winSysDir.replace('\\','/');
3096 fschmid 366
        if( osVersion.dwPlatformId == VER_PLATFORM_WIN32_NT     ) // Windows NT/2k/XP
367
        {
368
                if( osVersion.dwMajorVersion >= 5 ) // for 2k and XP dwMajorVersion == 5 
369
                        iccProfDirs.append( winSysDir + "/Spool/Drivers/Color/");
370
        }
371
        else if( osVersion.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS ) // Windows 9x/Me 
372
        {
373
                if( osVersion.dwMajorVersion >= 4 && osVersion.dwMinorVersion >= 10) // Win98 or WinMe
374
                        iccProfDirs.append( winSysDir + "/Color/");
375
        }
376
#endif
377
        return iccProfDirs;
378
}
379
 
4805 cbradney 380
QStringList ScPaths::getSystemCreateSwatchesDirs(void)
4602 cbradney 381
{
382
        QStringList createDirs;
383
#ifdef Q_OS_MAC
10398 cbradney 384
        createDirs.append(QDir::homePath()+"/create/swatches/");
385
        createDirs.append(QDir::homePath()+"/.create/swatches/");
4602 cbradney 386
#elif defined(Q_WS_X11)
10398 cbradney 387
        createDirs.append(QDir::homePath()+"/create/swatches/");
388
        createDirs.append(QDir::homePath()+"/.create/swatches/");
4602 cbradney 389
        createDirs.append("/usr/share/create/swatches/");
390
        createDirs.append("/usr/local/share/create/swatches/");
391
#elif defined(_WIN32)
4689 mrdocs 392
        QString localAppData = getSpecialDir(CSIDL_LOCAL_APPDATA);
393
        QString commonAppData = getSpecialDir(CSIDL_COMMON_APPDATA);
394
        QString programFilesCommon = getSpecialDir(CSIDL_PROGRAM_FILES_COMMON);
395
        createDirs.append(getSpecialDir(CSIDL_APPDATA) + "create/swatches/");
396
        if ( !localAppData.isEmpty() )
397
                createDirs.append(localAppData + "create/swatches/");
398
        if ( !commonAppData.isEmpty() )
399
                createDirs.append(commonAppData + "create/swatches/");
400
        if ( !programFilesCommon.isEmpty() )
401
                createDirs.append(programFilesCommon + "create/swatches/");
4602 cbradney 402
#endif
403
        return createDirs;
404
}
405
 
5917 jghali 406
QString ScPaths::getApplicationDataDir(void)
407
{
408
#if defined(_WIN32)
409
        QString appData = getSpecialDir(CSIDL_APPDATA);
410
        if (QDir(appData).exists())
16450 craig 411
        #ifdef APPLICATION_DATA_DIR
13066 subik 412
                return (appData + "/" + APPLICATION_DATA_DIR + "/");
16450 craig 413
        #else
5917 jghali 414
                return (appData + "/Scribus/");
16450 craig 415
        #endif
5917 jghali 416
#endif
13066 subik 417
 
418
#ifdef APPLICATION_DATA_DIR
419
        return QDir::homePath() + "/" + APPLICATION_DATA_DIR + "/";
420
#else
13171 cbradney 421
        #ifdef Q_OS_MAC
422
                return (QDir::homePath() + "/Library/Preferences/Scribus/");
423
        #else
424
                return (QDir::homePath() + "/.scribus/");
425
        #endif
13066 subik 426
#endif
5917 jghali 427
}
428
 
17524 craig 429
QString ScPaths::getUserDictDir(bool createIfNotExists)
430
{
431
        QDir userDictDirectory(getApplicationDataDir() + "dicts/");
432
        if(createIfNotExists)
433
        {
434
                if (!userDictDirectory.exists())
435
                        userDictDirectory.mkpath(userDictDirectory.absolutePath());
436
        }
437
        return userDictDirectory.absolutePath()+"/";
438
}
439
 
5917 jghali 440
QString ScPaths::getUserDocumentDir(void)
441
{
442
#if defined(_WIN32)
443
        QString userDocs = getSpecialDir(CSIDL_PERSONAL);
444
        if      (QDir(userDocs).exists())
445
                return userDocs;
446
#endif
10398 cbradney 447
        return (QDir::homePath() + "/");
5917 jghali 448
}
449
 
450
QString ScPaths::getTempFileDir(void)
451
{
452
#if defined(_WIN32)
453
        QString tempPath;
454
        WCHAR wTempPath[1024];
455
        DWORD result = GetTempPathW(1024, wTempPath);
456
        if ( result )
457
        {
10293 jghali 458
                tempPath = QString::fromUtf16((const unsigned short*) wTempPath);
5917 jghali 459
                tempPath.replace( '\\', '/' );
460
                tempPath += "/";
461
                // GetTempPath may return Windows directory, better not use this one
462
                // for temporary files
463
                if (QDir(tempPath).exists() && tempPath != getSpecialDir(CSIDL_WINDOWS))
464
                        return tempPath;
465
        }
13265 cbradney 466
#endif
5917 jghali 467
        return getApplicationDataDir();
468
}
469
 
17524 craig 470
QString ScPaths::downloadDir()
471
{
472
        QDir downloadDirectory(getApplicationDataDir() + "downloads/");
473
        if (!downloadDirectory.exists())
474
                downloadDirectory.mkpath(downloadDirectory.absolutePath());
475
        return downloadDirectory.absolutePath()+"/";
476
}
477
 
4169 mrdocs 478
QString ScPaths::getSpecialDir(int folder)
3096 fschmid 479
{
480
        QString qstr;
481
#if defined(_WIN32)
10293 jghali 482
        WCHAR dir[256];
483
        if ( SHGetSpecialFolderPathW(NULL, dir, folder , false) )
4689 mrdocs 484
        {
10293 jghali 485
                qstr = QString::fromUtf16((const unsigned short*) dir);
4689 mrdocs 486
                if( !qstr.endsWith("\\") )
487
                        qstr += "\\";
488
                qstr.replace( '\\', '/' );
489
        }
3096 fschmid 490
#else
491
        Q_ASSERT(false);
492
#endif
493
        return qstr;
494
}