2020#include < locale>
2121#include < fstream>
2222#include < sstream>
23+ #include < sys/stat.h>
2324
2425#ifdef WIN32
2526#include < windows.h>
3334#endif
3435
3536std::string Configuration::absolutePath;
37+ std::string Configuration::userPath;
38+ bool Configuration::isUserLayout_ = false ;
3639
3740Configuration::Configuration ()
3841{
@@ -54,13 +57,13 @@ void Configuration::initialize()
5457 {
5558 absolutePath = environment;
5659 }
57- #if defined(__linux) || defined(__APPLE__)
60+ /* #if defined(__linux) || defined(__APPLE__)
5861 // Or check for home based flat file works on linux/mac
5962 else if (retrofe_path && std::getline( retrofe_path, absolutePath ))
6063 {
61- retrofe_path.close ();
64+ retrofe_path.close();
6265 }
63- #endif
66+ #endif*/
6467 // Or check executable for path
6568 else
6669 {
@@ -94,6 +97,13 @@ void Configuration::initialize()
9497
9598 absolutePath = sPath ;
9699 }
100+
101+ /* * Get user path */
102+ struct stat info;
103+ if (stat (home_load.c_str (), &info) == 0 ){
104+ // if(IsPathExist(home_load)){
105+ userPath = home_load;
106+ }
97107}
98108
99109bool Configuration::import (std::string keyPrefix, std::string file)
@@ -136,7 +146,7 @@ bool Configuration::import(std::string collection, std::string keyPrefix, std::s
136146 return retVal;
137147}
138148
139- bool Configuration::importLayouts (std::string folder, std::string file, bool mustExist)
149+ bool Configuration::importLayouts (std::string folder, std::string file, bool userLayout, bool mustExist)
140150{
141151 bool retVal = true ;
142152 int lineCount = 0 ;
@@ -169,21 +179,32 @@ bool Configuration::importLayouts(std::string folder, std::string file, bool mus
169179
170180 if (line.empty () || (line.find_first_not_of (" \t\r " ) == std::string::npos))
171181 {
172- retVal = true ;
182+ retVal = true ;
173183 }
174184 else
175185 {
176- std::string layoutName = trimEnds (line);
177- std::string layoutPath = Utils::combinePath (folder, layoutName);
178186
179- /* Set new layoutPath */
180- layouts_.push_back (layoutPath);
181187
182- std::stringstream ss;
183- ss << " Dump layouts: " << " \" " << layoutPath << " \" " ;
188+ std::string layoutName = trimEnds (line);
189+ std::string layoutPath = Utils::combinePath (folder, layoutName);
190+
191+ /* * Check if dir exists */
192+ struct stat info;
193+ if (stat (layoutPath.c_str (), &info) != 0 ){
194+ // if(!IsPathExist(layoutPath)){
195+ printf (" Layout path: %s does not exist\n " , layoutPath.c_str ());
196+ Logger::write (Logger::ZONE_ERROR, " Configuration" , " Layout path: " + layoutPath + " does not exist" );
197+ continue ;
198+ }
199+
200+ /* Set new layoutPath */
201+ layouts_.push_back ( LayoutPair (layoutPath, userLayout) );
202+
203+ std::stringstream ss;
204+ ss << " Dump layouts: " << " \" " << layoutPath << " \" " ;
184205
185- Logger::write (Logger::ZONE_INFO, " Configuration" , ss.str ());
186- retVal = true ;
206+ Logger::write (Logger::ZONE_INFO, " Configuration" , ss.str ());
207+ retVal = true ;
187208 }
188209 }
189210
@@ -230,46 +251,74 @@ bool Configuration::importCurrentLayout(std::string folder, std::string file, bo
230251 {
231252 retVal = false ;
232253 }
233- // finding layout in existing list
254+ // Check if layout is in existing list
234255 else
235256 {
236257 std::string seekedLayoutName = trimEnds (line);
237- std::string layoutPathFound;
238258 bool layoutFoundInList = false ;
259+ bool userLayout = false ;
260+ std::string layoutPath;
239261
240- // check existing layout list */
241- for (std::vector<std::string>::iterator it = layouts_.begin (); it != layouts_.end (); ++it){
242- std::string curLayoutName = Utils::getFileName (*it);
262+ /* * Check existing layout list */
263+ for (std::vector<LayoutPair>::iterator it = layouts_.begin (); it != layouts_.end (); ++it){
264+ std::string curLayoutName = Utils::getFileName ((*it).first );
265+ userLayout = (*it).second ;
243266 if (!curLayoutName.compare (seekedLayoutName)){
244- layoutPathFound = *it;
245267 layoutFoundInList = true ;
246268 break ;
247269 }
248270 index++;
249271 }
250272
251- if (layoutFoundInList){
273+ /* * Reset default theme if not found in list */
274+ if (!layoutFoundInList){
275+ Logger::write (Logger::ZONE_ERROR, " Configuration" , " Layout \" " + seekedLayoutName + " \" not found in list! Resetting \" Classic\" theme by default" );
276+ printf (" Layout \" %s\" not found in list!\n " , seekedLayoutName.c_str ());
252277
253- /* remove layout properties if they already exist */
254- if (properties_. find ( " layout " ) != properties_. end ())
255- {
256- properties_. erase ( " layout " ) ;
257- }
278+ seekedLayoutName = std::string ( " Classic " );
279+ userLayout = false ;
280+ layoutPath = Utils::combinePath (Configuration::absolutePath, " layouts " , seekedLayoutName);
281+ index = 0 ;
282+ }
258283
259- /* Set new pair <key, value> for key = layout */
260- properties_.insert (PropertiesPair (" layout" , seekedLayoutName));
284+ /* * Check if Layout path exists */
285+ if (userLayout){
286+ layoutPath = Utils::combinePath (Configuration::userPath, " layouts" , seekedLayoutName);
287+ }
288+ else {
289+ layoutPath = Utils::combinePath (Configuration::absolutePath, " layouts" , seekedLayoutName);
290+ }
291+ printf (" Layout directory is \" %s\" \n " , layoutPath.c_str ());
292+ struct stat info;
293+ if (stat (layoutPath.c_str (), &info) != 0 ){
294+ // if(IsPathExist(layoutPath)){
295+ Logger::write (Logger::ZONE_ERROR, " Configuration" , " Layout directory\" " + layoutPath + " \" was not found! Resetting \" Classic\" theme by default" );
296+ printf (" Layout directory \" %s\" was not found!\n " , layoutPath.c_str ());
297+ }
261298
262- std::stringstream ss;
263- // printf("Found layout: %s at idx %d\n", layoutPathFound.c_str(), index);
264- ss << " Found layout: " << " \" " << layoutPathFound << " \" in layouts list at idx " << index;
265- Logger::write (Logger::ZONE_INFO, " Configuration" , ss.str ());
266- retVal = true ;
299+ /* Remove layout properties if they already exist */
300+ if (properties_.find (" layout" ) != properties_.end ())
301+ {
302+ properties_.erase (" layout" );
303+ }
304+ if (properties_.find (" userTheme" ) != properties_.end ())
305+ {
306+ properties_.erase (" userTheme" );
307+ }
267308
268- break ;
269- }
270- else {
271- index = 0 ;
272- }
309+ /* Set new pair <key, value> for key = layout */
310+ properties_.insert (PropertiesPair (" layout" , seekedLayoutName));
311+ properties_.insert (PropertiesPair (" userTheme" , userLayout?" yes" :" no" ));
312+
313+ Configuration::isUserLayout_ = userLayout;
314+
315+ std::stringstream ss;
316+ // printf("Found layout: %s at idx %d\n", layoutPathFound.c_str(), index);
317+ ss << " Found layout: " << " \" " << layoutPath << " \" in layouts list at idx " << index;
318+ Logger::write (Logger::ZONE_INFO, " Configuration" , ss.str ());
319+ retVal = true ;
320+
321+ break ;
273322 }
274323 }
275324
0 commit comments