OpenWalnut  1.4.0
WPathHelper.h
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #ifndef WPATHHELPER_H
00026 #define WPATHHELPER_H
00027 
00028 #include <string>
00029 #include <vector>
00030 
00031 #include <boost/filesystem.hpp>
00032 #include <boost/shared_ptr.hpp>
00033 
00034 /**
00035  * Singleton class helping to find files and paths. It is a useful to to search for resources and the central place to "hardcode" relative paths.
00036  * It contains global paths only. Modules have their OWN local paths.
00037  */
00038 class WPathHelper
00039 {
00040 public:
00041     /**
00042      * Destructor.
00043      */
00044     virtual ~WPathHelper();
00045 
00046     /**
00047      * Returns instance of the path helper. If it does not exists, it will be created.
00048      *
00049      * \return the running path helper instance.
00050      */
00051     static boost::shared_ptr< WPathHelper > getPathHelper();
00052 
00053     /**
00054      * Set the current application path. This should be called only once. The home path hereby is NOT the users home. It is a directory, where
00055      * OW can write user specific data. A good default here is to specify USERHOME/.OpenWalnut for example.
00056      *
00057      * \param appPath the application path
00058      * \param homePath the OW home path
00059      */
00060     void setBasePaths( boost::filesystem::path appPath, boost::filesystem::path homePath );
00061 
00062     /**
00063      * Set the current application path. This should be called only once. The home path hereby is NOT the users home. It is a directory, where
00064      * OW can write user specific data. A good default here is to specify USERHOME/.OpenWalnut for example.
00065      *
00066      * \param appPath the application path
00067      * \param homePath the OW home path
00068      */
00069     void setBasePathsOSXBundle( boost::filesystem::path appPath, boost::filesystem::path homePath );
00070 
00071     /**
00072      * The path where the binary file resides in. This is for example /usr/bin.
00073      *
00074      * \return the application path.
00075      */
00076     static boost::filesystem::path getAppPath();
00077 
00078     /**
00079      * The path where font files reside in.
00080      *
00081      * \return the font path.
00082      */
00083     static boost::filesystem::path getFontPath();
00084 
00085     /**
00086      * Paths to all known fonts.
00087      */
00088     typedef struct
00089     {
00090         /**
00091          * The default font to use in most cases.
00092          */
00093         boost::filesystem::path Default;
00094 
00095         /**
00096          * The Regular font (not bold, not italic)
00097          */
00098         boost::filesystem::path Regular;
00099 
00100         /**
00101          * Italic font.
00102          */
00103         boost::filesystem::path Italic;
00104 
00105         /**
00106          * Bold font.
00107          */
00108         boost::filesystem::path Bold;
00109     }
00110     Fonts;
00111 
00112     /**
00113      * The paths to all fonts supported.
00114      *
00115      * \return the file paths to all fonts
00116      */
00117     static Fonts getAllFonts();
00118 
00119     /**
00120      * The path to the global shaders. Modules usually have their own local shader directory.
00121      *
00122      * \return global shader path.
00123      */
00124     static boost::filesystem::path getShaderPath();
00125 
00126     /**
00127      * The path to the globally installed modules. This does not respect any environment variables or config options! Use this only to search
00128      * global modules. To get a list of all module search paths, including user defined ones, use getAllModulePaths().
00129      *
00130      * \return path to globally installed modules.
00131      */
00132     static boost::filesystem::path getModulePath();
00133 
00134     /**
00135      * The path to the OW dir in the user's home. This will not be the home dir directly. It is something like $HOME/.OpenWalnut.
00136      *
00137      * \return OW home path
00138      */
00139     static boost::filesystem::path getHomePath();
00140 
00141     /**
00142      * This returns a list of search paths for modules. This list is defined by the environment variable "OW_MODULE_PATH" and the list of additional
00143      * module paths. All of these
00144      * directories CAN contain modules. On startup, they get searched in the specified order.
00145      *
00146      * \return list of search paths for modules
00147      */
00148     static std::vector< boost::filesystem::path > getAllModulePaths();
00149 
00150     /**
00151      * This method adds the given path to the list of module paths. This way, arbitrary paths can be specified to search for modules. Each path
00152      * is searched recursively.
00153      *
00154      * \param path the path to add.
00155      */
00156     void addAdditionalModulePath( const boost::filesystem::path& path );
00157 
00158     /**
00159      * Returns the list of paths added using addAdditionalModulePath. This does NOT contain the paths in OW_MODULE_PATH. Use getAllModulePaths
00160      * for this.
00161      *
00162      * \return the list of additional paths
00163      */
00164     const std::vector< boost::filesystem::path >& getAdditionalModulePaths() const;
00165 
00166     /**
00167      * The path to the OW libs. You normally should not need this.
00168      *
00169      * \return the path to the libs.
00170      */
00171     static boost::filesystem::path getLibPath();
00172 
00173     /**
00174      * The path where shared files reside in.
00175      *
00176      * \return the shared files path.
00177      */
00178     static boost::filesystem::path getSharePath();
00179 
00180     /**
00181      * The path where the doc files reside in.
00182      *
00183      * \return the doc file path.
00184      */
00185     static boost::filesystem::path getDocPath();
00186 
00187     /**
00188      * The path where the config files reside in.
00189      *
00190      * \return the config file path.
00191      */
00192     static boost::filesystem::path getConfigPath();
00193 
00194     /**
00195      * The path to a given module's resources. This should be used to get a share-like path for the module. The path is relative to the
00196      * module's library path. This method is most useful for the module loader. You should not query your own resource path with this function.
00197      * Use your module instance's m_localPath.
00198      *
00199      * \param moduleLibPath the path to the lib. Can be relative or absolute. This must be the directory the lib contains and NOT the path to the
00200      * lib itself
00201      * \param packageName the name of the resource. This is usually the package name.
00202      *
00203      * \return the absolute path for the given module path.
00204      */
00205     static boost::filesystem::path getModuleResourcePath( boost::filesystem::path moduleLibPath, std::string packageName );
00206 
00207 protected:
00208     /**
00209      * Constructors are protected because this is a Singleton.
00210      */
00211     WPathHelper();
00212 
00213 private:
00214     /**
00215      * Application path. NOT the path of the binary. The application path is the directory in which the binary is placed.
00216      * The binary path is m_appPath+"/openwalnut".
00217      */
00218     boost::filesystem::path m_appPath;
00219 
00220     /**
00221      * The path where all the shared files reside in.
00222      */
00223     boost::filesystem::path m_sharePath;
00224 
00225     /**
00226      * The path where all the documentation files reside in.
00227      */
00228     boost::filesystem::path m_docPath;
00229 
00230     /**
00231      * The path where all the config files reside in.
00232      */
00233     boost::filesystem::path m_configPath;
00234 
00235     /**
00236      * The path to the globally installed modules.
00237      */
00238     boost::filesystem::path m_modulePath;
00239 
00240     /**
00241      * The path to the OW libs.
00242      */
00243     boost::filesystem::path m_libPath;
00244 
00245     /**
00246      * The path of a user specific OW directory.
00247      */
00248     boost::filesystem::path m_homePath;
00249 
00250     /**
00251      * The path to module resources, relative to the module libraries path.
00252      */
00253     boost::filesystem::path m_moduleResourcePathRelative;
00254 
00255     /**
00256      * A list of additional paths to search for modules. This does not contain the paths in the environment variable OW_MODULE_PATH. This method
00257      * is not thread-safe. You should only use it before the module factory loads the modules.
00258      */
00259     std::vector< boost::filesystem::path > m_additionalModulePaths;
00260 
00261     /**
00262      * Singleton instance of WPathHelper.
00263      */
00264     static boost::shared_ptr< WPathHelper > m_instance;
00265 };
00266 
00267 #endif  // WPATHHELPER_H
00268