Subversion Repositories Scribus

Rev

Rev 16262 | Rev 17506 | 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;
238
        spellDirs.append(m_shareDir + "dicts/spelling/");
239
#ifdef Q_OS_MAC
240
        d.setPath(macPortsPath);
241
        if (d.exists())
242
                spellDirs.append(macPortsPath);
243
        d.setPath(finkPath);
244
        if (d.exists())
245
                spellDirs.append(finkPath);
246
        d.setPath(osxLibreOfficePath);
247
        if (d.exists())
248
        {
249
                QStringList dictDirFilters("dict-*");
250
                QStringList dictDirList(d.entryList(dictDirFilters, QDir::Dirs, QDir::Name));
251
                QString dir;
252
                foreach (dir, dictDirList)
253
                        spellDirs.append(osxLibreOfficePath + "/" + dir + "/");
254
        }
255
        d.setPath(osxUserLibreOfficePath);
256
        if (d.exists())
257
        {
258
                QStringList dictDirFilters("dict-*");
259
                QStringList dictDirList(d.entryList(dictDirFilters, QDir::Dirs, QDir::Name));
260
                QString dir;
261
                foreach (dir, dictDirList)
262
                        spellDirs.append(osxUserLibreOfficePath + "/" + dir + "/");
263
        }
264
 
265
#elif defined(_WIN32)
266
        QString progFiles = getSpecialDir(CSIDL_PROGRAM_FILES);
267
        d.setPath(progFiles+windowsLOPath);
268
        if (d.exists())
269
        {
270
                QStringList dictDirFilters("dict-*");
271
                QStringList dictDirList(d.entryList(dictDirFilters, QDir::Dirs, QDir::Name));
272
                QString dir;
273
                foreach (dir, dictDirList)
274
                        spellDirs.append(progFiles+windowsLOPath + "/" + dir + "/");
275
        }
276
        if (d.exists())
277
                spellDirs.append(progFiles+windowsLOPath + "/" + dir + "/");
278
#elif defined(Q_WS_X11)
279
        d.setPath(linuxHunspellPath);
280
        if (d.exists())
281
                spellDirs.append(linuxHunspellPath);
282
        d.setPath(linuxMyspellPath);
283
        if (d.exists())
284
                spellDirs.append(linuxMyspellPath);
285
        d.setPath(linuxLocalPath);
286
        if (d.exists())
287
                spellDirs.append(linuxLocalPath);
288
#endif
289
        return spellDirs;
290
}
291
 
3096 fschmid 292
QStringList ScPaths::getSystemFontDirs(void)
293
{
294
        QStringList fontDirs;
3102 avox 295
#ifdef Q_OS_MAC
10398 cbradney 296
        fontDirs.append(QDir::homePath() + "/Library/Fonts/");
3096 fschmid 297
        fontDirs.append("/Library/Fonts/");
298
        fontDirs.append("/Network/Library/Fonts/");
299
        fontDirs.append("/System/Library/Fonts/");
300
#elif defined(_WIN32)
301
        fontDirs.append( getSpecialDir(CSIDL_FONTS) );
302
#endif
303
        return fontDirs;
304
}
305
 
306
QStringList ScPaths::getSystemProfilesDirs(void)
307
{
308
        QStringList iccProfDirs;
3102 avox 309
#ifdef Q_OS_MAC
10398 cbradney 310
        iccProfDirs.append(QDir::homePath()+"/Library/ColorSync/Profiles/");
3096 fschmid 311
        iccProfDirs.append("/System/Library/ColorSync/Profiles/");
312
        iccProfDirs.append("/Library/ColorSync/Profiles/");
313
#elif defined(Q_WS_X11)
10398 cbradney 314
        iccProfDirs.append(QDir::homePath()+"/color/icc/");
315
        iccProfDirs.append(QDir::homePath()+"/.color/icc/");
3096 fschmid 316
        iccProfDirs.append("/usr/share/color/icc/");
3961 mrdocs 317
        iccProfDirs.append("/usr/local/share/color/icc/");
3096 fschmid 318
#elif defined(_WIN32)
319
        // On Windows it's more complicated, profiles location depends on OS version
10293 jghali 320
        WCHAR sysDir[MAX_PATH + 1];
3096 fschmid 321
        OSVERSIONINFO osVersion;
322
        ZeroMemory( &osVersion, sizeof(OSVERSIONINFO));
323
        osVersion.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); // Necessary for GetVersionEx to succeed
324
        GetVersionEx(&osVersion);  // Get Windows version infos
10293 jghali 325
        GetSystemDirectoryW( sysDir, MAX_PATH ); // getSpecialDir(CSIDL_SYSTEM) fails on Win9x
326
        QString winSysDir = QString::fromUtf16((const ushort*) sysDir);
327
        winSysDir = winSysDir.replace('\\','/');
3096 fschmid 328
        if( osVersion.dwPlatformId == VER_PLATFORM_WIN32_NT     ) // Windows NT/2k/XP
329
        {
330
                if( osVersion.dwMajorVersion >= 5 ) // for 2k and XP dwMajorVersion == 5 
331
                        iccProfDirs.append( winSysDir + "/Spool/Drivers/Color/");
332
        }
333
        else if( osVersion.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS ) // Windows 9x/Me 
334
        {
335
                if( osVersion.dwMajorVersion >= 4 && osVersion.dwMinorVersion >= 10) // Win98 or WinMe
336
                        iccProfDirs.append( winSysDir + "/Color/");
337
        }
338
#endif
339
        return iccProfDirs;
340
}
341
 
15496 craig 342
QStringList ScPaths::getDirsFromEnvVar(const QString envVar, const QString dirToFind)
15483 craig 343
{
344
        QChar sep(':');
345
#ifdef _WIN32
346
        sep=';';
347
#endif
15496 craig 348
        QStringList dirs;
16262 jghali 349
#if defined(Q_OS_MAC) || defined(Q_OS_UNIX)
15496 craig 350
        QStringList env(QProcess::systemEnvironment());
351
        QString path_data;
15483 craig 352
        foreach (QString line, env)
353
        {
15496 craig 354
                if (line.indexOf(envVar) == 0)
355
                        path_data = line.mid(envVar.length()+1); //eg, Strip "XDG_DATA_DIRS="
15483 craig 356
        }
15496 craig 357
        QStringList splitpath_data(path_data.split(sep, QString::SkipEmptyParts));
15483 craig 358
        foreach (QString dir, splitpath_data)
359
        {
15496 craig 360
                QFileInfo info(dir+dirToFind);
15483 craig 361
                if (info.exists())
15496 craig 362
                        dirs.append(dir+dirToFind);
15483 craig 363
        }
364
#endif
15496 craig 365
        return dirs;
15483 craig 366
}
15496 craig 367
 
368
 
4805 cbradney 369
QStringList ScPaths::getSystemCreateSwatchesDirs(void)
4602 cbradney 370
{
371
        QStringList createDirs;
372
#ifdef Q_OS_MAC
10398 cbradney 373
        createDirs.append(QDir::homePath()+"/create/swatches/");
374
        createDirs.append(QDir::homePath()+"/.create/swatches/");
4602 cbradney 375
#elif defined(Q_WS_X11)
10398 cbradney 376
        createDirs.append(QDir::homePath()+"/create/swatches/");
377
        createDirs.append(QDir::homePath()+"/.create/swatches/");
4602 cbradney 378
        createDirs.append("/usr/share/create/swatches/");
379
        createDirs.append("/usr/local/share/create/swatches/");
380
#elif defined(_WIN32)
4689 mrdocs 381
        QString localAppData = getSpecialDir(CSIDL_LOCAL_APPDATA);
382
        QString commonAppData = getSpecialDir(CSIDL_COMMON_APPDATA);
383
        QString programFilesCommon = getSpecialDir(CSIDL_PROGRAM_FILES_COMMON);
384
        createDirs.append(getSpecialDir(CSIDL_APPDATA) + "create/swatches/");
385
        if ( !localAppData.isEmpty() )
386
                createDirs.append(localAppData + "create/swatches/");
387
        if ( !commonAppData.isEmpty() )
388
                createDirs.append(commonAppData + "create/swatches/");
389
        if ( !programFilesCommon.isEmpty() )
390
                createDirs.append(programFilesCommon + "create/swatches/");
4602 cbradney 391
#endif
392
        return createDirs;
393
}
394
 
5917 jghali 395
QString ScPaths::getApplicationDataDir(void)
396
{
397
#if defined(_WIN32)
398
        QString appData = getSpecialDir(CSIDL_APPDATA);
399
        if (QDir(appData).exists())
13066 subik 400
#ifdef APPLICATION_DATA_DIR
401
                return (appData + "/" + APPLICATION_DATA_DIR + "/");
402
#else
5917 jghali 403
                return (appData + "/Scribus/");
404
#endif
13066 subik 405
#endif
406
 
407
#ifdef APPLICATION_DATA_DIR
408
        return QDir::homePath() + "/" + APPLICATION_DATA_DIR + "/";
409
#else
13171 cbradney 410
        #ifdef Q_OS_MAC
411
                return (QDir::homePath() + "/Library/Preferences/Scribus/");
412
        #else
413
                return (QDir::homePath() + "/.scribus/");
414
        #endif
13066 subik 415
#endif
5917 jghali 416
}
417
 
14539 jghali 418
QString ScPaths::getImageCacheDir(void)
419
{
420
        return getApplicationDataDir() + "cache/img/";
421
}
422
 
13608 jghali 423
QString ScPaths::getPluginDataDir(void)
424
{
425
        return getApplicationDataDir() + "plugins/";
426
}
427
 
5917 jghali 428
QString ScPaths::getUserDocumentDir(void)
429
{
430
#if defined(_WIN32)
431
        QString userDocs = getSpecialDir(CSIDL_PERSONAL);
432
        if      (QDir(userDocs).exists())
433
                return userDocs;
434
#endif
10398 cbradney 435
        return (QDir::homePath() + "/");
5917 jghali 436
}
437
 
438
QString ScPaths::getTempFileDir(void)
439
{
440
#if defined(_WIN32)
441
        QString tempPath;
442
        WCHAR wTempPath[1024];
443
        DWORD result = GetTempPathW(1024, wTempPath);
444
        if ( result )
445
        {
10293 jghali 446
                tempPath = QString::fromUtf16((const unsigned short*) wTempPath);
5917 jghali 447
                tempPath.replace( '\\', '/' );
448
                tempPath += "/";
449
                // GetTempPath may return Windows directory, better not use this one
450
                // for temporary files
451
                if (QDir(tempPath).exists() && tempPath != getSpecialDir(CSIDL_WINDOWS))
452
                        return tempPath;
453
        }
13265 cbradney 454
#endif
5917 jghali 455
        return getApplicationDataDir();
456
}
457
 
4169 mrdocs 458
QString ScPaths::getSpecialDir(int folder)
3096 fschmid 459
{
460
        QString qstr;
461
#if defined(_WIN32)
10293 jghali 462
        WCHAR dir[256];
463
        if ( SHGetSpecialFolderPathW(NULL, dir, folder , false) )
4689 mrdocs 464
        {
10293 jghali 465
                qstr = QString::fromUtf16((const unsigned short*) dir);
4689 mrdocs 466
                if( !qstr.endsWith("\\") )
467
                        qstr += "\\";
468
                qstr.replace( '\\', '/' );
469
        }
3096 fschmid 470
#else
471
        Q_ASSERT(false);
472
#endif
473
        return qstr;
474
}