Back to index

scribus-ng  1.3.4.dfsg+svn20071115
scpaths.cpp
Go to the documentation of this file.
00001 /*
00002 For general Scribus (>=1.3.2) copyright and licensing information please refer
00003 to the COPYING file provided with the program. Following this notice may exist
00004 a copyright and/or license notice that predates the release of Scribus 1.3.2
00005 for which a new license (GPL+exception) is in place.
00006 */
00007 #include "scpaths.h"
00008 #include <qapplication.h>
00009 #include <qdir.h>
00010 
00011 #include "scconfig.h"
00012 
00013 // On Qt/Mac we need CoreFoundation to discover the location
00014 // of the app bundle.
00015 #ifdef BUILD_MAC_BUNDLE
00016 #include <CoreFoundation/CoreFoundation.h>
00017 #endif
00018 
00019 #ifdef _WIN32
00020 #include <windows.h>
00021 #include <shlobj.h>
00022 #endif
00023 
00024 #ifdef _WIN32
00025 const char ScPaths::envPathSeparator = ';';
00026 #else
00027 const char ScPaths::envPathSeparator = ':';
00028 #endif
00029 
00030 // Init the singleton's "self" address to NULL
00031 ScPaths* ScPaths::m_instance = NULL;
00032 
00033 // Singleton's public constructor
00034 const ScPaths& ScPaths::instance()
00035 {
00036        if (!ScPaths::m_instance)
00037               ScPaths::m_instance = new ScPaths();
00038        return *ScPaths::m_instance;
00039 }
00040 
00041 // Singleton's public destructor
00042 void ScPaths::destroy()
00043 {
00044        if (ScPaths::m_instance)
00045               delete ScPaths::m_instance;
00046 }
00047 
00048 // Protected "real" constructor
00049 // All paths are initialized to compile-time defaults passed in
00050 // as preprocessor macros and set by autoconf.
00051 ScPaths::ScPaths() :
00052        m_docDir(DOCDIR),
00053        m_iconDir(ICONDIR),
00054        m_libDir(LIBDIR),
00055        m_pluginDir(PLUGINDIR),
00056        m_sampleScriptDir(SAMPLESDIR),
00057        m_scriptDir(SCRIPTSDIR),
00058        m_templateDir(TEMPLATEDIR),
00059        m_shareDir(SHAREDIR)
00060 {
00061 // On MacOS/X, override the compile-time settings with a location
00062 // obtained from the system.
00063 #ifdef BUILD_MAC_BUNDLE
00064        // Set up the various app paths to look inside the app bundle
00065        CFURLRef pluginRef = CFBundleCopyBundleURL(CFBundleGetMainBundle());
00066        CFStringRef macPath = CFURLCopyFileSystemPath(pluginRef,
00067                                               kCFURLPOSIXPathStyle);
00068        const char *pathPtr = CFStringGetCStringPtr(macPath,
00069                                               CFStringGetSystemEncoding());
00070 
00071        // make sure we get the Scribus.app directory, not some subdir
00072 
00073        // strip trailing '/':
00074        char *p = const_cast<char*>(pathPtr + strlen(pathPtr) - 1);
00075        while (*p == '/')
00076               --p;
00077        ++p;
00078        *p = '\0';
00079        if (strcmp("/bin", p-4) == 0) {
00080               p -= 4;
00081               *p = '\0';
00082        }
00083        if (strcmp("/MacOS", p-6) == 0) {
00084               p -= 6;
00085               *p = '\0';
00086        }
00087        if (strcmp("/Contents", p-9) == 0) {
00088               p -= 9;
00089               *p = '\0';
00090        }
00091 
00092        qDebug(QString("scpaths: bundle at %1:").arg(pathPtr));
00093        m_shareDir = QString("%1/Contents/share/scribus/").arg(pathPtr);
00094        m_docDir = QString("%1/Contents/share/scribus/doc/").arg(pathPtr);
00095        m_iconDir = QString("%1/Contents/share/scribus/icons/").arg(pathPtr);
00096        m_sampleScriptDir = QString("%1/Contents/share/scribus/samples/").arg(pathPtr);
00097        m_scriptDir = QString("%1/Contents/share/scribus/scripts/").arg(pathPtr);
00098        m_templateDir = QString("%1/Contents/share/scribus/templates/").arg(pathPtr);
00099        m_libDir = QString("%1/Contents/lib/scribus/").arg(pathPtr);
00100        m_pluginDir = QString("%1/Contents/lib/scribus/plugins/").arg(pathPtr);
00101        QApplication::setLibraryPaths(QString("%1/Contents/lib/qtplugins/").arg(pathPtr));
00102        CFRelease(pluginRef);
00103        CFRelease(macPath);
00104 
00105        // on OSX this goes to the sys console, so user only sees it when they care -- AV
00106        qDebug(QString("scpaths: doc dir=%1").arg(m_docDir));
00107        qDebug(QString("scpaths: icon dir=%1").arg(m_iconDir));
00108        qDebug(QString("scpaths: font dir=%1").arg(m_fontDir));
00109        qDebug(QString("scpaths: sample dir=%1").arg(m_sampleScriptDir));
00110        qDebug(QString("scpaths: script dir=%1").arg(m_scriptDir));
00111        qDebug(QString("scpaths: template dir=%1").arg(m_templateDir));
00112        qDebug(QString("scpaths: lib dir=%1").arg(m_libDir));
00113        qDebug(QString("scpaths: plugin dir=%1").arg(m_pluginDir));
00114        qDebug(QString("scpaths: qtplugins=%1").arg(QApplication::libraryPaths().join(":")));
00115 
00116 #elif defined(_WIN32)
00117        QString appPath = qApp->applicationDirPath();
00118        m_shareDir = QString("%1/share/").arg(appPath);
00119        m_docDir = QString("%1/share/doc/").arg(appPath);
00120        m_fontDir = QString("%1/share/fonts/").arg(appPath);
00121        m_iconDir = QString("%1/share/icons/").arg(appPath);
00122        m_sampleScriptDir = QString("%1/share/samples/").arg(appPath);
00123        m_scriptDir = QString("%1/share/scripts/").arg(appPath);
00124        m_templateDir = QString("%1/share/templates/").arg(appPath);
00125        m_libDir = QString("%1/libs/").arg(appPath);
00126        m_pluginDir = QString("%1/plugins/").arg(appPath);
00127        QApplication::setLibraryPaths(QString("%1/qtplugins/").arg(appPath));
00128 #endif
00129        
00130 //     if(!m_shareDir.endsWith("/"))        m_shareDir += "/";
00131 //     if(!m_docDir.endsWith("/"))          m_docDir += "/";
00132 //     if(!m_fontDir.endsWith("/"))         m_fontDir += "/";
00133        if(!m_iconDir.endsWith("/"))         m_iconDir += "/";
00134 //     if(!m_sampleScriptDir.endsWith("/")) m_sampleScriptDir += "/";
00135 //     if(!m_scriptDir.endsWith("/"))       m_scriptDir += "/";
00136 //     if(!m_templateDir.endsWith("/"))     m_templateDir += "/";
00137 //     if(!m_libDir.endsWith("/"))          m_libDir += "/";
00138 //     if(!m_pluginDir.endsWith("/"))       m_pluginDir += "/";
00139 }
00140 
00141 ScPaths::~ScPaths() {};
00142 
00143 QString ScPaths::separatorsToSlashes(const QString& path)
00144 {
00145        QString qtPath(path);
00146        qtPath.replace( QDir::separator(), '/' );
00147        return qtPath;
00148 }
00149 
00150 const QString&  ScPaths::docDir() const
00151 {
00152        return m_docDir;
00153 }
00154 
00155 const QString&  ScPaths::iconDir() const
00156 {
00157        return m_iconDir;
00158 }
00159 
00160 const QString&  ScPaths::fontDir() const
00161 {
00162        return m_fontDir;
00163 }
00164 
00165 const QString&  ScPaths::libDir() const
00166 {
00167        return m_libDir;
00168 }
00169 
00170 const QString&  ScPaths::pluginDir() const
00171 {
00172        return m_pluginDir;
00173 }
00174 
00175 const QString&  ScPaths::sampleScriptDir() const
00176 {
00177        return m_sampleScriptDir;
00178 }
00179 
00180 const QString&  ScPaths::scriptDir() const
00181 {
00182        return m_scriptDir;
00183 }
00184 
00185 const QString&  ScPaths::templateDir() const
00186 {
00187        return m_templateDir;
00188 }
00189 
00190 const QString&  ScPaths::shareDir() const
00191 {
00192        return m_shareDir;
00193 }
00194 
00195 QString ScPaths::translationDir() const
00196 {
00197        return(m_shareDir + "translations/");
00198 }
00199 
00200 
00201 QStringList ScPaths::getSystemFontDirs(void)
00202 {
00203        QStringList fontDirs;
00204 #ifdef Q_OS_MAC
00205        fontDirs.append(QDir::homeDirPath() + "/Library/Fonts/");
00206        fontDirs.append("/Library/Fonts/");
00207        fontDirs.append("/Network/Library/Fonts/");
00208        fontDirs.append("/System/Library/Fonts/");
00209 #elif defined(_WIN32)
00210        fontDirs.append( getSpecialDir(CSIDL_FONTS) );
00211 #endif
00212        return fontDirs;
00213 }
00214 
00215 QStringList ScPaths::getSystemProfilesDirs(void)
00216 {
00217        QStringList iccProfDirs;
00218 #ifdef Q_OS_MAC
00219        iccProfDirs.append(QDir::homeDirPath()+"/Library/ColorSync/Profiles/");
00220        iccProfDirs.append("/System/Library/ColorSync/Profiles/");
00221        iccProfDirs.append("/Library/ColorSync/Profiles/");
00222 #elif defined(Q_WS_X11)
00223        iccProfDirs.append(QDir::homeDirPath()+"/color/icc/");
00224        iccProfDirs.append(QDir::homeDirPath()+"/.color/icc/");
00225        iccProfDirs.append("/usr/share/color/icc/");
00226        iccProfDirs.append("/usr/local/share/color/icc/");
00227 #elif defined(_WIN32)
00228        // On Windows it's more complicated, profiles location depends on OS version
00229        char sysDir[MAX_PATH + 1];
00230        OSVERSIONINFO osVersion;
00231        ZeroMemory( &osVersion, sizeof(OSVERSIONINFO));
00232        osVersion.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); // Necessary for GetVersionEx to succeed
00233        GetVersionEx(&osVersion);  // Get Windows version infos
00234        GetSystemDirectory( sysDir, MAX_PATH ); // getSpecialDir(CSIDL_SYSTEM) fails on Win9x
00235        QString winSysDir = QString(sysDir).replace('\\','/');
00236        if( osVersion.dwPlatformId == VER_PLATFORM_WIN32_NT     ) // Windows NT/2k/XP
00237        {
00238               if( osVersion.dwMajorVersion >= 5 ) // for 2k and XP dwMajorVersion == 5 
00239                      iccProfDirs.append( winSysDir + "/Spool/Drivers/Color/");
00240        }
00241        else if( osVersion.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS ) // Windows 9x/Me 
00242        {
00243               if( osVersion.dwMajorVersion >= 4 && osVersion.dwMinorVersion >= 10) // Win98 or WinMe
00244                      iccProfDirs.append( winSysDir + "/Color/");
00245        }
00246 #endif
00247        return iccProfDirs;
00248 }
00249 
00250 QStringList ScPaths::getSystemCreateSwatchesDirs(void)
00251 {
00252        QStringList createDirs;
00253 #ifdef Q_OS_MAC
00254        createDirs.append(QDir::homeDirPath()+"/create/swatches/");
00255        createDirs.append(QDir::homeDirPath()+"/.create/swatches/");
00256 #elif defined(Q_WS_X11)
00257        createDirs.append(QDir::homeDirPath()+"/create/swatches/");
00258        createDirs.append(QDir::homeDirPath()+"/.create/swatches/");
00259        createDirs.append("/usr/share/create/swatches/");
00260        createDirs.append("/usr/local/share/create/swatches/");
00261 #elif defined(_WIN32)
00262        QString localAppData = getSpecialDir(CSIDL_LOCAL_APPDATA);
00263        QString commonAppData = getSpecialDir(CSIDL_COMMON_APPDATA);
00264        QString programFilesCommon = getSpecialDir(CSIDL_PROGRAM_FILES_COMMON);
00265        createDirs.append(getSpecialDir(CSIDL_APPDATA) + "create/swatches/");
00266        if ( !localAppData.isEmpty() )
00267               createDirs.append(localAppData + "create/swatches/");
00268        if ( !commonAppData.isEmpty() )
00269               createDirs.append(commonAppData + "create/swatches/");
00270        if ( !programFilesCommon.isEmpty() )
00271               createDirs.append(programFilesCommon + "create/swatches/");
00272 #endif
00273        return createDirs;
00274 }
00275 
00276 QString ScPaths::getApplicationDataDir(void)
00277 {
00278 #if defined(_WIN32)
00279        QString appData = getSpecialDir(CSIDL_APPDATA);
00280        if (QDir(appData).exists())
00281               return (appData + "/Scribus/");
00282 #endif
00283        return (QDir::homeDirPath() + "/.scribus/");
00284 }
00285 
00286 QString ScPaths::getUserDocumentDir(void)
00287 {
00288 #if defined(_WIN32)
00289        QString userDocs = getSpecialDir(CSIDL_PERSONAL);
00290        if     (QDir(userDocs).exists())
00291               return userDocs;
00292 #endif
00293        return (QDir::homeDirPath() + "/");
00294 }
00295 
00296 QString ScPaths::getTempFileDir(void)
00297 {
00298 #if defined(_WIN32)
00299        QString tempPath;
00300        WCHAR wTempPath[1024];
00301        DWORD result = GetTempPathW(1024, wTempPath);
00302        if ( result )
00303        {
00304               tempPath = QString::fromUcs2((const unsigned short*) wTempPath);
00305               tempPath.replace( '\\', '/' );
00306               tempPath += "/";
00307               // GetTempPath may return Windows directory, better not use this one
00308               // for temporary files
00309               if (QDir(tempPath).exists() && tempPath != getSpecialDir(CSIDL_WINDOWS))
00310                      return tempPath;
00311        }
00312        return getApplicationDataDir();
00313 #else
00314        return (QDir::homeDirPath() + "/.scribus/");
00315 #endif
00316 }
00317 
00318 QString ScPaths::getSpecialDir(int folder)
00319 {
00320        QString qstr;
00321 #if defined(_WIN32)
00322        char dir[256];
00323        if ( SHGetSpecialFolderPath(NULL, dir, folder , false) )
00324        {
00325               qstr = dir;
00326               if( !qstr.endsWith("\\") )
00327                      qstr += "\\";
00328               qstr.replace( '\\', '/' );
00329        }
00330 #else
00331        Q_ASSERT(false);
00332 #endif
00333        return qstr;
00334 }