All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OSSettingsDir.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2011-2012 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/common/OSSettingsDir.cpp
22 
23  \brief A singleton class for discovering Operational System settings dir.
24 */
25 
26 // TerraLib
27 #include "OSSettingsDir.h"
28 
29 // STL
30 #include <cstdlib>
31 
32 // Boost
33 #include <boost/filesystem.hpp>
34 
35 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
36 // Windows
37 #include <Shlobj.h>
38 #endif
39 
41 {
42  return m_userSettingsPath;
43 }
44 
46 {
47  return m_systemSettingsPath;
48 }
49 
51 {
52 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
53  char userSettingsPath[MAX_PATH];
54 
55 //there is also a tag: CSIDL_APPDATA -> maybe older versions of XP may depends on it!
56  HRESULT result = SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, userSettingsPath);
57 
58  if((result == S_OK) && (userSettingsPath != 0) && boost::filesystem::is_directory(userSettingsPath))
59  m_userSettingsPath = userSettingsPath;
60 
61  char systemSettingsPath[MAX_PATH];
62 
63  result = SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA, NULL, SHGFP_TYPE_CURRENT, systemSettingsPath);
64 
65  if((result == S_OK) && (systemSettingsPath != 0) && boost::filesystem::is_directory(systemSettingsPath))
66  m_systemSettingsPath = systemSettingsPath;
67 
68 #elif TE_PLATFORM == TE_PLATFORMCODE_LINUX
69  const char* userSettingsPath = std::getenv("$HOME");
70 
71  if((userSettingsPath != 0) && boost::filesystem::is_directory(userSettingsPath))
72  {
73  boost::filesystem::path p(userSettingsPath);
74  p /= ".config";
75 
76  m_userSettingsPath = p.string();
77  }
78 
79  if(boost::filesystem::is_directory("/etc/xdg"))
80  m_userSettingsPath = "/etc/xdg";
81 
82 #elif TE_PLATFORM == TE_PLATFORMCODE_APPLE
83  const char* userSettingsPath = std::getenv("$HOME");
84 
85  if((userSettingsPath != 0) && boost::filesystem::is_directory(userSettingsPath))
86  {
87  boost::filesystem::path p(userSettingsPath);
88  p /= ".config";
89 
90  m_userSettingsPath = p.string();
91  }
92 
93  if(boost::filesystem::is_directory("/etc/xdg"))
94  m_userSettingsPath = "/etc/xdg";
95 
96 #else
97  #error "Platform not supported! Contact TerraLib Team"
98 #endif
99 
100 }
101 
103 {
104 }
105 
const std::string & getUserSettingsPath() const
It returns the folder location to store per user data.
A singleton class for discovering the Operational System settings directories.
OSSettingsDir()
It initializes the singleton.
std::string m_userSettingsPath
Folder to output data by user.
Definition: OSSettingsDir.h:87
const std::string & getSystemSettingsPath() const
It returns the folder location to store application data applied to all users.