Subversion Repositories Scribus

Rev

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