Subversion Repositories Scribus

Rev

Rev 8335 | Rev 11059 | 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
7407 cbradney 129
 
130
//      if(!m_shareDir.endsWith("/"))        m_shareDir += "/";
131
//      if(!m_docDir.endsWith("/"))          m_docDir += "/";
132
//      if(!m_fontDir.endsWith("/"))         m_fontDir += "/";
7430 cbradney 133
        if(!m_iconDir.endsWith("/"))         m_iconDir += "/";
7407 cbradney 134
//      if(!m_sampleScriptDir.endsWith("/")) m_sampleScriptDir += "/";
135
//      if(!m_scriptDir.endsWith("/"))       m_scriptDir += "/";
136
//      if(!m_templateDir.endsWith("/"))     m_templateDir += "/";
137
//      if(!m_libDir.endsWith("/"))          m_libDir += "/";
138
//      if(!m_pluginDir.endsWith("/"))       m_pluginDir += "/";
1693 craig 139
}
140
 
141
ScPaths::~ScPaths() {};
142
 
7117 jghali 143
QString ScPaths::separatorsToSlashes(const QString& path)
144
{
145
        QString qtPath(path);
146
        qtPath.replace( QDir::separator(), '/' );
147
        return qtPath;
148
}
149
 
1693 craig 150
const QString&  ScPaths::docDir() const
151
{
152
        return m_docDir;
153
}
154
 
155
const QString&  ScPaths::iconDir() const
156
{
157
        return m_iconDir;
158
}
159
 
3794 cbradney 160
const QString&  ScPaths::fontDir() const
161
{
162
        return m_fontDir;
163
}
164
 
1693 craig 165
const QString&  ScPaths::libDir() const
166
{
167
        return m_libDir;
168
}
169
 
170
const QString&  ScPaths::pluginDir() const
171
{
172
        return m_pluginDir;
173
}
174
 
175
const QString&  ScPaths::sampleScriptDir() const
176
{
177
        return m_sampleScriptDir;
178
}
179
 
180
const QString&  ScPaths::scriptDir() const
181
{
182
        return m_scriptDir;
183
}
184
 
185
const QString&  ScPaths::templateDir() const
186
{
187
        return m_templateDir;
188
}
189
 
2707 subik 190
const QString&  ScPaths::shareDir() const
191
{
192
        return m_shareDir;
193
}
3096 fschmid 194
 
11051 malex 195
QString ScPaths::translationDir() const
196
{
197
        return(m_shareDir + "translations/");
198
 }
199
 
200
 
3096 fschmid 201
QStringList ScPaths::getSystemFontDirs(void)
202
{
203
        QStringList fontDirs;
3102 avox 204
#ifdef Q_OS_MAC
3096 fschmid 205
        fontDirs.append(QDir::homeDirPath() + "/Library/Fonts/");
206
        fontDirs.append("/Library/Fonts/");
207
        fontDirs.append("/Network/Library/Fonts/");
208
        fontDirs.append("/System/Library/Fonts/");
209
#elif defined(_WIN32)
210
        fontDirs.append( getSpecialDir(CSIDL_FONTS) );
211
#endif
212
        return fontDirs;
213
}
214
 
215
QStringList ScPaths::getSystemProfilesDirs(void)
216
{
217
        QStringList iccProfDirs;
3102 avox 218
#ifdef Q_OS_MAC
3096 fschmid 219
        iccProfDirs.append(QDir::homeDirPath()+"/Library/ColorSync/Profiles/");
220
        iccProfDirs.append("/System/Library/ColorSync/Profiles/");
221
        iccProfDirs.append("/Library/ColorSync/Profiles/");
222
#elif defined(Q_WS_X11)
223
        iccProfDirs.append(QDir::homeDirPath()+"/color/icc/");
224
        iccProfDirs.append(QDir::homeDirPath()+"/.color/icc/");
225
        iccProfDirs.append("/usr/share/color/icc/");
3961 mrdocs 226
        iccProfDirs.append("/usr/local/share/color/icc/");
3096 fschmid 227
#elif defined(_WIN32)
228
        // On Windows it's more complicated, profiles location depends on OS version
229
        char sysDir[MAX_PATH + 1];
230
        OSVERSIONINFO osVersion;
231
        ZeroMemory( &osVersion, sizeof(OSVERSIONINFO));
232
        osVersion.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); // Necessary for GetVersionEx to succeed
233
        GetVersionEx(&osVersion);  // Get Windows version infos
234
        GetSystemDirectory( sysDir, MAX_PATH ); // getSpecialDir(CSIDL_SYSTEM) fails on Win9x
235
        QString winSysDir = QString(sysDir).replace('\\','/');
236
        if( osVersion.dwPlatformId == VER_PLATFORM_WIN32_NT     ) // Windows NT/2k/XP
237
        {
238
                if( osVersion.dwMajorVersion >= 5 ) // for 2k and XP dwMajorVersion == 5 
239
                        iccProfDirs.append( winSysDir + "/Spool/Drivers/Color/");
240
        }
241
        else if( osVersion.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS ) // Windows 9x/Me 
242
        {
243
                if( osVersion.dwMajorVersion >= 4 && osVersion.dwMinorVersion >= 10) // Win98 or WinMe
244
                        iccProfDirs.append( winSysDir + "/Color/");
245
        }
246
#endif
247
        return iccProfDirs;
248
}
249
 
4805 cbradney 250
QStringList ScPaths::getSystemCreateSwatchesDirs(void)
4602 cbradney 251
{
252
        QStringList createDirs;
253
#ifdef Q_OS_MAC
5658 avox 254
        createDirs.append(QDir::homeDirPath()+"/create/swatches/");
255
        createDirs.append(QDir::homeDirPath()+"/.create/swatches/");
4602 cbradney 256
#elif defined(Q_WS_X11)
257
        createDirs.append(QDir::homeDirPath()+"/create/swatches/");
258
        createDirs.append(QDir::homeDirPath()+"/.create/swatches/");
259
        createDirs.append("/usr/share/create/swatches/");
260
        createDirs.append("/usr/local/share/create/swatches/");
261
#elif defined(_WIN32)
4689 mrdocs 262
        QString localAppData = getSpecialDir(CSIDL_LOCAL_APPDATA);
263
        QString commonAppData = getSpecialDir(CSIDL_COMMON_APPDATA);
264
        QString programFilesCommon = getSpecialDir(CSIDL_PROGRAM_FILES_COMMON);
265
        createDirs.append(getSpecialDir(CSIDL_APPDATA) + "create/swatches/");
266
        if ( !localAppData.isEmpty() )
267
                createDirs.append(localAppData + "create/swatches/");
268
        if ( !commonAppData.isEmpty() )
269
                createDirs.append(commonAppData + "create/swatches/");
270
        if ( !programFilesCommon.isEmpty() )
271
                createDirs.append(programFilesCommon + "create/swatches/");
4602 cbradney 272
#endif
273
        return createDirs;
274
}
275
 
5917 jghali 276
QString ScPaths::getApplicationDataDir(void)
277
{
278
#if defined(_WIN32)
279
        QString appData = getSpecialDir(CSIDL_APPDATA);
280
        if (QDir(appData).exists())
281
                return (appData + "/Scribus/");
282
#endif
283
        return (QDir::homeDirPath() + "/.scribus/");
284
}
285
 
286
QString ScPaths::getUserDocumentDir(void)
287
{
288
#if defined(_WIN32)
289
        QString userDocs = getSpecialDir(CSIDL_PERSONAL);
290
        if      (QDir(userDocs).exists())
291
                return userDocs;
292
#endif
293
        return (QDir::homeDirPath() + "/");
294
}
295
 
296
QString ScPaths::getTempFileDir(void)
297
{
298
#if defined(_WIN32)
299
        QString tempPath;
300
        WCHAR wTempPath[1024];
301
        DWORD result = GetTempPathW(1024, wTempPath);
302
        if ( result )
303
        {
8335 jghali 304
                tempPath = QString::fromUcs2((const unsigned short*) wTempPath);
5917 jghali 305
                tempPath.replace( '\\', '/' );
306
                tempPath += "/";
307
                // GetTempPath may return Windows directory, better not use this one
308
                // for temporary files
309
                if (QDir(tempPath).exists() && tempPath != getSpecialDir(CSIDL_WINDOWS))
310
                        return tempPath;
311
        }
312
        return getApplicationDataDir();
313
#else
314
        return (QDir::homeDirPath() + "/.scribus/");
315
#endif
316
}
317
 
4169 mrdocs 318
QString ScPaths::getSpecialDir(int folder)
3096 fschmid 319
{
320
        QString qstr;
321
#if defined(_WIN32)
322
        char dir[256];
4689 mrdocs 323
        if ( SHGetSpecialFolderPath(NULL, dir, folder , false) )
324
        {
325
                qstr = dir;
326
                if( !qstr.endsWith("\\") )
327
                        qstr += "\\";
328
                qstr.replace( '\\', '/' );
329
        }
3096 fschmid 330
#else
331
        Q_ASSERT(false);
332
#endif
333
        return qstr;
334
}