libcrn  3.9.5
A document image processing library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNConfig.cpp
Go to the documentation of this file.
1 /* Copyright 2006-2014 INSA Lyon
2  *
3  * This file is part of libcrn.
4  *
5  * libcrn 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, or
8  * (at your option) any later version.
9  *
10  * libcrn 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 libcrn. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * file: CRNConfig.cpp
19  * \author Yann LEYDIER
20  */
21 
22 #include <CRNConfig.h>
23 #include <CRNException.h>
24 #include <CRNIO/CRNIO.h>
25 #include <CRNi18n.h>
26 
27 #ifdef CRN_USING_GDKPB
28 # include <gdk-pixbuf/gdk-pixbuf.h>
29 #endif
30 #ifdef CRN_ENABLE_OPENMP
31 # include <omp.h>
32 #endif
33 
34 using namespace crn;
35 
36 Config::Config():
37  conf("crn")
38 {
39  if (!conf.Load())
40  {
41  // default init
42  conf.SetData(topDirKey(), Path(CRN_PROJECT_PATH));
43  if (!IO::Access(CRN_PROJECT_PATH, IO::EXISTS))
44  {
45  try
46  {
47  IO::Mkdir(CRN_PROJECT_PATH);
48  }
49  catch (...) {}
50  }
51  conf.SetData(localeDirKey(), Path(CRN_LOCALE_FULL_PATH));
52  conf.SetData(staticDataDirKey(), Path(CRN_DATA_FULL_PATH));
53  conf.SetData(verboseKey(), Prop3::True());
54  conf.Save();
55  }
56  IO::IsVerbose() = conf.GetProp3(verboseKey()).IsTrue();
57  // misc init
58 #ifdef CRN_USING_GDKPB
59 # if !GLIB_VERSION_2_36
60  g_type_init();
61 # endif
62 #endif
63 
64  // i18n
65  char *loc = getenv("LANG");
66  CRNdout << "libcrn LANG env: " << (loc == nullptr ? "none" : loc) << std::endl;
67 
68 #if !defined(CRN_USING_GLIB_INTL) && !defined(CRN_USING_LIBINTL)
69  loc = setlocale(LC_ALL, "C");
70 #else
71  loc = setlocale(LC_ALL, "");
72 #endif
73 
74  if (!loc)
75  CRNdout << "libcrn setlocale failed" << std::endl;
76  else
77  CRNdout << "libcrn locale = " << loc << std::endl;
78 
79  loc = CRNbindtextdomain(GETTEXT_PACKAGE, conf.GetPath(localeDirKey()).CStr());
80  if (!loc)
81  CRNdout << "libcrn: no bound path. should be " << conf.GetPath(localeDirKey()).CStr() << std::endl;
82  else
83  CRNdout << "libcrn: path set to " << loc << std::endl;
84  loc = CRNbind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
85  if (!loc)
86  CRNdout << "libcrn: no bound codeset. should be UTF-8" << std::endl;
87  else
88  CRNdout << "libcrn: Codeset = " << loc << std::endl;
89  loc = CRNtextdomain(GETTEXT_PACKAGE);
90  if (!loc)
91  CRNdout << "libcrn textdomain failed" << std::endl;
92  else
93  CRNdout << "libcrn text domain = " << loc << std::endl;
94  CRNdout << _("Using default language.") << std::endl;
95 }
96 
97 Config::~Config()
98 {
99 }
100 
101 Config& Config::getInstance()
102 {
103  static Config conf;
104  return conf;
105 }
106 
107 String Config::topDirKey()
108 {
109  return U"FilesPath";
110 }
111 
112 String Config::localeDirKey()
113 {
114  return U"LocalePath";
115 }
116 
117 String Config::staticDataDirKey()
118 {
119  return U"StaticDataPath";
120 }
121 
122 String Config::verboseKey()
123 {
124  return U"Verbose";
125 }
126 
128 {
129  getInstance().conf.SetData(verboseKey(), Prop3(IO::IsVerbose()));
130  return getInstance().conf.Save().IsNotEmpty();
131 }
132 
136 void Config::SetTopDataPath(const Path &dir)
137 {
138  getInstance().conf.SetData(topDirKey(), dir);
139 }
140 
145 {
146  return getInstance().conf.GetPath(topDirKey());
147 }
148 
153 {
154  return getInstance().conf.GetPath(localeDirKey());
155 }
156 
161 {
162  getInstance().conf.SetData(localeDirKey(), dir);
163 }
164 
169 {
170  return getInstance().conf.GetPath(staticDataDirKey());
171 }
172 
177 {
178  getInstance().conf.SetData(staticDataDirKey(), dir);
179 }
180 
183  ConfigurationFile::Initialize();
184  getInstance();
void SetData(const String &key, T value)
Sets a key/value pair.
static crn::Path GetStaticDataPath()
Gets the data directory name.
Definition: CRNConfig.cpp:168
static void SetTopDataPath(const Path &dir)
Sets the top directory name.
Definition: CRNConfig.cpp:136
#define _(String)
Definition: CRNi18n.h:51
static bool Save()
Saves to user's local config.
Definition: CRNConfig.cpp:127
bool IsNotEmpty() const noexcept
Checks if the string is not empty.
#define CRN_END_CLASS_CONSTRUCTOR(classname)
Defines a class constructor.
Definition: CRNObject.h:198
char * CRNbindtextdomain(const char *, const char *)
Definition: CRNi18n.h:54
A UTF32 character string class.
Definition: CRNString.h:61
static bool & IsVerbose()
Controls whether CRNVerbose prints something or not.
Definition: CRNIO.cpp:58
Global configuration utility class.
Definition: CRNConfig.h:38
#define CRNdout
Definition: CRNIO.h:143
static void SetStaticDataPath(const crn::Path &dir)
Sets the data directory name.
Definition: CRNConfig.cpp:176
A convenience class for file paths.
Definition: CRNPath.h:39
Path Save()
Saves the file to the user's personal space.
static void SetLocalePath(const crn::Path &dir)
Sets the translation files path.
Definition: CRNConfig.cpp:160
char * CRNtextdomain(const char *)
Definition: CRNi18n.h:53
static Path GetTopDataPath()
Gets the top directory name.
Definition: CRNConfig.cpp:144
char * CRNbind_textdomain_codeset(const char *, const char *)
Definition: CRNi18n.h:55
A ternary proposition.
Definition: CRNProp3.h:40
static crn::Path GetLocalePath()
Gets the translation files path.
Definition: CRNConfig.cpp:152
Configuration file management utility class.
Path GetPath(const String &key) const
Gets a path.
#define CRN_BEGIN_CLASS_CONSTRUCTOR(classname)
Defines a class constructor.
Definition: CRNObject.h:185