Scribus Developer Documentation - API
logo-pen

Scribus Developer Documentation - API

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