Subversion Repositories Scribus

Rev

Rev 5917 | Rev 7407 | 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"
2673 cbradney 8
#include <qapplication.h>
3096 fschmid 9
#include <qdir.h>
1693 craig 10
 
2688 craig 11
#include "scconfig.h"
2673 cbradney 12
 
1693 craig 13
// On Qt/Mac we need CoreFoundation to discover the location
14
// of the app bundle.
2673 cbradney 15
#ifdef BUILD_MAC_BUNDLE
1693 craig 16
#include <CoreFoundation/CoreFoundation.h>
17
#endif
18
 
3096 fschmid 19
#ifdef _WIN32
20
#include <windows.h>
21
#include <shlobj.h>
22
#endif
23
 
4194 fschmid 24
#ifdef _WIN32
25
const char ScPaths::envPathSeparator = ';';
26
#else
27
const char ScPaths::envPathSeparator = ':';
28
#endif
29
 
1693 craig 30
// Init the singleton's "self" address to NULL
31
ScPaths* ScPaths::m_instance = NULL;
32
 
33
// Singleton's public constructor
34
const ScPaths& ScPaths::instance()
35
{
36
        if (!ScPaths::m_instance)
37
                ScPaths::m_instance = new ScPaths();
38
        return *ScPaths::m_instance;
39
}
40
 
41
// Singleton's public destructor
42
void ScPaths::destroy()
43
{
44
        if (ScPaths::m_instance)
45
                delete ScPaths::m_instance;
46
}
47
 
48
// Protected "real" constructor
49
// All paths are initialized to compile-time defaults passed in
50
// as preprocessor macros and set by autoconf.
51
ScPaths::ScPaths() :
52
        m_docDir(DOCDIR),
53
        m_iconDir(ICONDIR),
54
        m_libDir(LIBDIR),
55
        m_pluginDir(PLUGINDIR),
56
        m_sampleScriptDir(SAMPLESDIR),
57
        m_scriptDir(SCRIPTSDIR),
2707 subik 58
        m_templateDir(TEMPLATEDIR),
59
        m_shareDir(SHAREDIR)
1693 craig 60
{
61
// On MacOS/X, override the compile-time settings with a location
62
// obtained from the system.
2673 cbradney 63
#ifdef BUILD_MAC_BUNDLE
1693 craig 64
        // Set up the various app paths to look inside the app bundle
65
        CFURLRef pluginRef = CFBundleCopyBundleURL(CFBundleGetMainBundle());
66
        CFStringRef macPath = CFURLCopyFileSystemPath(pluginRef,
67
                                               kCFURLPOSIXPathStyle);
68
        const char *pathPtr = CFStringGetCStringPtr(macPath,
69
                                               CFStringGetSystemEncoding());
3546 avox 70
 
71
        // make sure we get the Scribus.app directory, not some subdir
72
 
73
        // strip trailing '/':
3459 avox 74
        char *p = const_cast<char*>(pathPtr + strlen(pathPtr) - 1);
2673 cbradney 75
        while (*p == '/')
76
                --p;
77
        ++p;
78
        *p = '\0';
79
        if (strcmp("/bin", p-4) == 0) {
80
                p -= 4;
81
                *p = '\0';
82
        }
83
        if (strcmp("/MacOS", p-6) == 0) {
84
                p -= 6;
85
                *p = '\0';
86
        }
87
        if (strcmp("/Contents", p-9) == 0) {
88
                p -= 9;
89
                *p = '\0';
90
        }
2707 subik 91
 
3546 avox 92
        qDebug(QString("scpaths: bundle at %1:").arg(pathPtr));
3794 cbradney 93
        m_shareDir = QString("%1/Contents/share/scribus/").arg(pathPtr);
94
        m_docDir = QString("%1/Contents/share/scribus/doc/").arg(pathPtr);
95
        m_iconDir = QString("%1/Contents/share/scribus/icons/").arg(pathPtr);
96
        m_sampleScriptDir = QString("%1/Contents/share/scribus/samples/").arg(pathPtr);
97
        m_scriptDir = QString("%1/Contents/share/scribus/scripts/").arg(pathPtr);
98
        m_templateDir = QString("%1/Contents/share/scribus/templates/").arg(pathPtr);
99
        m_libDir = QString("%1/Contents/lib/scribus/").arg(pathPtr);
100
        m_pluginDir = QString("%1/Contents/lib/scribus/plugins/").arg(pathPtr);
2673 cbradney 101
        QApplication::setLibraryPaths(QString("%1/Contents/lib/qtplugins/").arg(pathPtr));
1693 craig 102
        CFRelease(pluginRef);
103
        CFRelease(macPath);
3546 avox 104
 
105
        // on OSX this goes to the sys console, so user only sees it when they care -- AV
2673 cbradney 106
        qDebug(QString("scpaths: doc dir=%1").arg(m_docDir));
107
        qDebug(QString("scpaths: icon dir=%1").arg(m_iconDir));
3831 cbradney 108
        qDebug(QString("scpaths: font dir=%1").arg(m_fontDir));
2673 cbradney 109
        qDebug(QString("scpaths: sample dir=%1").arg(m_sampleScriptDir));
110
        qDebug(QString("scpaths: script dir=%1").arg(m_scriptDir));
111
        qDebug(QString("scpaths: template dir=%1").arg(m_templateDir));
112
        qDebug(QString("scpaths: lib dir=%1").arg(m_libDir));
113
        qDebug(QString("scpaths: plugin dir=%1").arg(m_pluginDir));
114
        qDebug(QString("scpaths: qtplugins=%1").arg(QApplication::libraryPaths().join(":")));
3546 avox 115
 
3096 fschmid 116
#elif defined(_WIN32)
117
        QString appPath = qApp->applicationDirPath();
3794 cbradney 118
        m_shareDir = QString("%1/share/").arg(appPath);
119
        m_docDir = QString("%1/share/doc/").arg(appPath);
120
        m_fontDir = QString("%1/share/fonts/").arg(appPath);
121
        m_iconDir = QString("%1/share/icons/").arg(appPath);
122
        m_sampleScriptDir = QString("%1/share/samples/").arg(appPath);
123
        m_scriptDir = QString("%1/share/scripts/").arg(appPath);
124
        m_templateDir = QString("%1/share/templates/").arg(appPath);
125
        m_libDir = QString("%1/libs/").arg(appPath);
126
        m_pluginDir = QString("%1/plugins/").arg(appPath);
4169 mrdocs 127
        QApplication::setLibraryPaths(QString("%1/qtplugins/").arg(appPath));
3096 fschmid 128
#endif
1693 craig 129
}
130
 
131
ScPaths::~ScPaths() {};
132
 
7117 jghali 133
QString ScPaths::separatorsToSlashes(const QString& path)
134
{
135
        QString qtPath(path);
136
        qtPath.replace( QDir::separator(), '/' );
137
        return qtPath;
138
}
139
 
1693 craig 140
const QString&  ScPaths::docDir() const
141
{
142
        return m_docDir;
143
}
144
 
145
const QString&  ScPaths::iconDir() const
146
{
147
        return m_iconDir;
148
}
149
 
3794 cbradney 150
const QString&  ScPaths::fontDir() const
151
{
152
        return m_fontDir;
153
}
154
 
1693 craig 155
const QString&  ScPaths::libDir() const
156
{
157
        return m_libDir;
158
}
159
 
160
const QString&  ScPaths::pluginDir() const
161
{
162
        return m_pluginDir;
163
}
164
 
165
const QString&  ScPaths::sampleScriptDir() const
166
{
167
        return m_sampleScriptDir;
168
}
169
 
170
const QString&  ScPaths::scriptDir() const
171
{
172
        return m_scriptDir;
173
}
174
 
175
const QString&  ScPaths::templateDir() const
176
{
177
        return m_templateDir;
178
}
179
 
2707 subik 180
const QString&  ScPaths::shareDir() const
181
{
182
        return m_shareDir;
183
}
3096 fschmid 184
 
185
QStringList ScPaths::getSystemFontDirs(void)
186
{
187
        QStringList fontDirs;
3102 avox 188
#ifdef Q_OS_MAC
3096 fschmid 189
        fontDirs.append(QDir::homeDirPath() + "/Library/Fonts/");
190
        fontDirs.append("/Library/Fonts/");
191
        fontDirs.append("/Network/Library/Fonts/");
192
        fontDirs.append("/System/Library/Fonts/");
193
#elif defined(_WIN32)
194
        fontDirs.append( getSpecialDir(CSIDL_FONTS) );
195
#endif
196
        return fontDirs;
197
}
198
 
199
QStringList ScPaths::getSystemProfilesDirs(void)
200
{
201
        QStringList iccProfDirs;
3102 avox 202
#ifdef Q_OS_MAC
3096 fschmid 203
        iccProfDirs.append(QDir::homeDirPath()+"/Library/ColorSync/Profiles/");
204
        iccProfDirs.append("/System/Library/ColorSync/Profiles/");
205
        iccProfDirs.append("/Library/ColorSync/Profiles/");
206
#elif defined(Q_WS_X11)
207
        iccProfDirs.append(QDir::homeDirPath()+"/color/icc/");
208
        iccProfDirs.append(QDir::homeDirPath()+"/.color/icc/");
209
        iccProfDirs.append("/usr/share/color/icc/");
3961 mrdocs 210
        iccProfDirs.append("/usr/local/share/color/icc/");
3096 fschmid 211
#elif defined(_WIN32)
212
        // On Windows it's more complicated, profiles location depends on OS version
213
        char sysDir[MAX_PATH + 1];
214
        OSVERSIONINFO osVersion;
215
        ZeroMemory( &osVersion, sizeof(OSVERSIONINFO));
216
        osVersion.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); // Necessary for GetVersionEx to succeed
217
        GetVersionEx(&osVersion);  // Get Windows version infos
218
        GetSystemDirectory( sysDir, MAX_PATH ); // getSpecialDir(CSIDL_SYSTEM) fails on Win9x
219
        QString winSysDir = QString(sysDir).replace('\\','/');
220
        if( osVersion.dwPlatformId == VER_PLATFORM_WIN32_NT     ) // Windows NT/2k/XP
221
        {
222
                if( osVersion.dwMajorVersion >= 5 ) // for 2k and XP dwMajorVersion == 5 
223
                        iccProfDirs.append( winSysDir + "/Spool/Drivers/Color/");
224
        }
225
        else if( osVersion.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS ) // Windows 9x/Me 
226
        {
227
                if( osVersion.dwMajorVersion >= 4 && osVersion.dwMinorVersion >= 10) // Win98 or WinMe
228
                        iccProfDirs.append( winSysDir + "/Color/");
229
        }
230
#endif
231
        return iccProfDirs;
232
}
233
 
4805 cbradney 234
QStringList ScPaths::getSystemCreateSwatchesDirs(void)
4602 cbradney 235
{
236
        QStringList createDirs;
237
#ifdef Q_OS_MAC
5658 avox 238
        createDirs.append(QDir::homeDirPath()+"/create/swatches/");
239
        createDirs.append(QDir::homeDirPath()+"/.create/swatches/");
4602 cbradney 240
#elif defined(Q_WS_X11)
241
        createDirs.append(QDir::homeDirPath()+"/create/swatches/");
242
        createDirs.append(QDir::homeDirPath()+"/.create/swatches/");
243
        createDirs.append("/usr/share/create/swatches/");
244
        createDirs.append("/usr/local/share/create/swatches/");
245
#elif defined(_WIN32)
4689 mrdocs 246
        QString localAppData = getSpecialDir(CSIDL_LOCAL_APPDATA);
247
        QString commonAppData = getSpecialDir(CSIDL_COMMON_APPDATA);
248
        QString programFilesCommon = getSpecialDir(CSIDL_PROGRAM_FILES_COMMON);
249
        createDirs.append(getSpecialDir(CSIDL_APPDATA) + "create/swatches/");
250
        if ( !localAppData.isEmpty() )
251
                createDirs.append(localAppData + "create/swatches/");
252
        if ( !commonAppData.isEmpty() )
253
                createDirs.append(commonAppData + "create/swatches/");
254
        if ( !programFilesCommon.isEmpty() )
255
                createDirs.append(programFilesCommon + "create/swatches/");
4602 cbradney 256
#endif
257
        return createDirs;
258
}
259
 
5917 jghali 260
QString ScPaths::getApplicationDataDir(void)
261
{
262
#if defined(_WIN32)
263
        QString appData = getSpecialDir(CSIDL_APPDATA);
264
        if (QDir(appData).exists())
265
                return (appData + "/Scribus/");
266
#endif
267
        return (QDir::homeDirPath() + "/.scribus/");
268
}
269
 
270
QString ScPaths::getUserDocumentDir(void)
271
{
272
#if defined(_WIN32)
273
        QString userDocs = getSpecialDir(CSIDL_PERSONAL);
274
        if      (QDir(userDocs).exists())
275
                return userDocs;
276
#endif
277
        return (QDir::homeDirPath() + "/");
278
}
279
 
280
QString ScPaths::getTempFileDir(void)
281
{
282
#if defined(_WIN32)
283
        QString tempPath;
284
        WCHAR wTempPath[1024];
285
        DWORD result = GetTempPathW(1024, wTempPath);
286
        if ( result )
287
        {
288
                tempPath = QString::fromUcs2(wTempPath);
289
                tempPath.replace( '\\', '/' );
290
                tempPath += "/";
291
                // GetTempPath may return Windows directory, better not use this one
292
                // for temporary files
293
                if (QDir(tempPath).exists() && tempPath != getSpecialDir(CSIDL_WINDOWS))
294
                        return tempPath;
295
        }
296
        return getApplicationDataDir();
297
#else
298
        return (QDir::homeDirPath() + "/.scribus/");
299
#endif
300
}
301
 
4169 mrdocs 302
QString ScPaths::getSpecialDir(int folder)
3096 fschmid 303
{
304
        QString qstr;
305
#if defined(_WIN32)
306
        char dir[256];
4689 mrdocs 307
        if ( SHGetSpecialFolderPath(NULL, dir, folder , false) )
308
        {
309
                qstr = dir;
310
                if( !qstr.endsWith("\\") )
311
                        qstr += "\\";
312
                qstr.replace( '\\', '/' );
313
        }
3096 fschmid 314
#else
315
        Q_ASSERT(false);
316
#endif
317
        return qstr;
318
}