CEBL  2.1
Preferences.cpp
Go to the documentation of this file.
1 
7 //local includes
8 #include "../CEBLIncludes.hpp"
9 #include "Preferences.hpp"
10 #include "../TextUtils.hpp"
11 #include "../Exceptions.hpp"
12 
13 //std includes
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <fstream>
17 #include <exception>
18 #include <iostream>
19 
20 //boost filesystem and program options includes
21 #define BOOST_FILESYSTEM_VERSION 2
22 #include <boost/filesystem/operations.hpp>
23 #include <boost/filesystem/convenience.hpp>
24 #include <boost/filesystem/fstream.hpp>
25 #include <boost/filesystem/path.hpp>
26 
27 
28 //namespaces
29 using namespace std;
30 namespace fs = boost::filesystem;
31 
32 // print out vector
33 template<class T>
34 ostream& operator<<(ostream& os, const vector<T>& v)
35 {
36  copy(v.begin(), v.end(), ostream_iterator<T>(os, " "));
37  return os;
38 }
39 
40 //Constructor
42 {
43  have_conf_dir = true;
44  session_filename = "";
45  default_channels_filename = string(HOMEDIR) + "/.cebl/conf/channels-19.conf";
46  string_table_filename = string(HOMEDIR) + "/.cebl/conf/string-table-en.txt";
47 }
48 
49 //Destructor
51 {
52 
53 }
54 
55 //process command line
56 bool Preferences::processCL(int ac, char ** av)
57 {
58  session_filename = "";
59  try {
60 
61  desc.add_options()
62  ("help", "Print Help")
63  ("version", "Print Version String")
64  ;
65 
66  po::options_description hidden("Hidden options");
67  hidden.add_options()
68  ("session-file", po::value< vector<string> >(), "session file")
69  ;
70 
71  //----------
72 
73  po::options_description visible("CEBL Options");
74  visible.add(desc).add(hidden);
75 
76  po::positional_options_description p;
77  p.add("session-file", -1);
78 
79  po::variables_map vm;
80  po::store(po::command_line_parser(ac, av).
81  options(visible).positional(p).run(), vm);
82  po::notify(vm);
83 
84 
85  if (vm.count("help"))
86  {
87  cout << desc << "\n";
88  return false;
89  }
90  if (vm.count("version"))
91  {
92  cout << PACKAGE_STRING << endl;
93  return false;
94  }
95  if (vm.count("session-file"))
96  {
97  session_filename = vm["session-file"].as< vector<string> >()[0];
98  }
99 
100  // read all other options from the option list
101  for(unsigned i=0;i<option_list.size();i++)
102  {
103  string name = option_list[i];
104  set_options[name] = "None";
105  if(vm.count(name))
106  {
107  set_options[name] = vm[name].as< vector<string> >()[0];
108  }
109  }
110  }
111  catch(exception& e) {
112  cerr << "error: " << e.what() << "\n";
113  return 1;
114  }
115  catch(...) {
116  cerr << "Exception of unknown type!\n";
117  }
118  return true;
119 }
120 
121 
122 
123 //Iniialize Preferences
125 {
126 
127  //set members from env
128  char *pref_path = NULL;
129  bool pref_path_specified = true;
130  pref_path = getenv("CEBL_PREF_DIR");
131  if(pref_path==NULL)
132  pref_path_specified = false;
133 
134 
135  if(!pref_path_specified)
136  {
137  string home_path = string(HOMEDIR);
138  if(home_path=="")
139  {
140  throw FileException("Failed to open home directory.");
141  }
142  conf_dir = home_path+"/.cebl/";
143  }
144  else
145  {
146  conf_dir = pref_path;
147  }
148 
149  //check if this is first-run
150  //check for ini directory
151  fs::path full_path;
152  full_path = fs::system_complete(fs::path(conf_dir, fs::native));
153  //make sure directory exists
154  if(!fs::exists(full_path))
155  {
156  if(false)
157  {
158  have_conf_dir = false;
159  return true;
160  }
161  //create config directory
162  else
163  {
164  //try creating the directory
165  try
166  {
167  fs::create_directory(full_path);
168  fs::create_directory(fs::system_complete(fs::path(conf_dir+"/plugins", fs::native)));
169  fs::create_directory(fs::system_complete(fs::path(conf_dir+"/plugins/classifiers", fs::native)));
170  fs::create_directory(fs::system_complete(fs::path(conf_dir+"/plugins/filters", fs::native)));
171  fs::create_directory(fs::system_complete(fs::path(conf_dir+"/plugins/features", fs::native)));
172 
173  }
174  catch(...)
175 
176  {
177  //Confirm("Could Not Create Directory","Could not create directory "+conf_dir+". Continue without being able to save peferences?",GTK_MESSAGE_ERROR)
178  throw FileException("Could not create cebl configuration directory.");
179  }
180  }
181  }
182  //make sure it is a directory
183  if(!fs::is_directory(full_path))
184  {
185  throw FileException(conf_dir+" is not a directory. Please remove the file "+conf_dir+".");
186  }
187 
188  //--------------------------
189  //directory exists for sure
190  string default_path = static_cast<string>(DATAROOTDIR)+"/"+PACKAGE;
191  // paths.push_back(default_path);
192 
193  paths.push_back(conf_dir);
194 
195  string conf_file = conf_dir+"/cebl.conf";
196  full_path = fs::system_complete(fs::path(conf_file, fs::native));
197  if(!fs::exists(full_path))
198  {
199  //need to create conf file
200  try
201  {
202 
203  }
204  catch(...)
205  {
206  throw FileException("Failed to create initial configuration file.\n");
207  }
208  }
209  else
210  {
211  try
212  {
213  po::options_description config("Configuration");
214  config.add_options()
215  ("path",
216  po::value< vector<string> >()->composing(),
217  "include path")
218  ;
219 
220  po::options_description config_file_options;
221  config_file_options.add(config);
222 
223 
224  po::variables_map vm;
225 
226  ifstream ifs(conf_file.c_str());
227  store(parse_config_file(ifs, config_file_options), vm);
228  notify(vm);
229  //save options
230  if(vm.count("path"))
231  {
232  cout << "Path = " << vm["path"].as< string >();
233  }
234  }
235  catch(exception& e)
236  {
237  cout << e.what() << "\n";
238 
239  }
240 
241  }
242 
243  return true;
244 }
245 
246 
248 {
249  return this->default_channels_filename;
250 }
251 
252 
253 void Preferences::addOption(string name, string description, char short_name)
254 {
255  string full_name = name;
256  if(short_name != '\0')
257  full_name = full_name+","+short_name;
258 
259  option_list.push_back(name);
260  desc.add_options()
261  (full_name.c_str(), po::value< vector<string> >(), description.c_str());
262 }
263 
264 string Preferences::getOption(string name)
265 {
266  return set_options[name];
267 }