]> git.sesse.net Git - casparcg/blob - common/env.cpp
1ce341d46f349cf33805e96592640d3c41cb6f38
[casparcg] / common / env.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #include "stdafx.h"
23
24 #include "env.h"
25
26 #include "../version.h"
27
28 #include "except.h"
29 #include "log.h"
30 #include "string.h"
31 #include "os/filesystem.h"
32
33 #include <boost/property_tree/ptree.hpp>
34 #include <boost/property_tree/xml_parser.hpp>
35 #include <boost/filesystem.hpp>
36 #include <boost/filesystem/fstream.hpp>
37 #include <boost/thread/once.hpp>
38
39 #include <functional>
40 #include <iostream>
41 #include <fstream>
42
43 namespace caspar { namespace env {
44         
45 std::wstring media;
46 std::wstring log;
47 std::wstring ftemplate;
48 std::wstring data;
49 std::wstring font;
50 std::wstring thumbnails;
51 boost::property_tree::wptree pt;
52
53 void check_is_configured()
54 {
55         if(pt.empty())
56                 CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info(L"Enviroment properties has not been configured"));
57 }
58
59 void configure(const std::wstring& filename)
60 {
61         try
62         {
63                 auto initialPath = boost::filesystem::initial_path().wstring();
64         
65                 boost::filesystem::wifstream file(initialPath + L"/" + filename);
66                 boost::property_tree::read_xml(file, pt, boost::property_tree::xml_parser::trim_whitespace | boost::property_tree::xml_parser::no_comments);
67
68                 auto paths      = pt.get_child(L"configuration.paths");
69                 media           = paths.get(L"media-path", initialPath + L"/media/");
70                 log                     = paths.get(L"log-path", initialPath + L"/log/");
71                 ftemplate       = boost::filesystem::complete(paths.get(L"template-path", initialPath + L"/template/")).wstring();
72                 data            = paths.get(L"data-path", initialPath + L"/data/");
73                 font            = paths.get(L"font-path", initialPath + L"/font/");
74                 thumbnails      = paths.get(L"thumbnail-path", initialPath + L"/thumbnail/");
75         }
76         catch(...)
77         {
78                 CASPAR_LOG(error) << L" ### Invalid configuration file. ###";
79                 throw;
80         }
81
82         try
83         {
84                 auto found_media_path = find_case_insensitive(media);
85                 if (found_media_path)
86                         media = *found_media_path;
87                 else
88                         boost::filesystem::create_directories(media);
89
90                 auto found_template_path = find_case_insensitive(ftemplate);
91                 if (found_template_path)
92                         ftemplate = *found_template_path;
93                 else
94                         boost::filesystem::create_directories(ftemplate);
95
96                 auto found_data_path = find_case_insensitive(data);
97                 if (found_data_path)
98                         data = *found_data_path;
99                 else
100                         boost::filesystem::create_directories(data);
101
102                 auto found_font_path = find_case_insensitive(font);
103                 if (found_font_path)
104                         font = *found_font_path;
105                 else
106                         boost::filesystem::create_directories(font);
107
108                 auto found_thumbnails_path = find_case_insensitive(thumbnails);
109                 if (found_thumbnails_path)
110                         thumbnails = *found_thumbnails_path;
111                 else
112                         boost::filesystem::create_directories(thumbnails);
113
114                 auto found_log_path = find_case_insensitive(log);
115                 if (found_log_path)
116                         log = *found_log_path;
117                 else if (!boost::filesystem::create_directories(log))
118                         log = L"./";
119
120                 //Make sure that all paths have a trailing slash
121                 if(media.at(media.length()-1) != L'/')
122                         media.append(L"/");
123                 if(log.at(log.length()-1) != L'/')
124                         log.append(L"/");
125                 if(ftemplate.at(ftemplate.length()-1) != L'/')
126                         ftemplate.append(L"/");
127                 if(data.at(data.length()-1) != L'/')
128                         data.append(L"/");
129                 if(font.at(font.length()-1) != L'/')
130                         font.append(L"/");
131                 if(thumbnails.at(thumbnails.length()-1) != L'/')
132                         thumbnails.append(L"/");
133
134                 try
135                 {
136                         auto initialPath = boost::filesystem::initial_path().wstring();
137
138                         for(auto it = boost::filesystem::directory_iterator(initialPath); it != boost::filesystem::directory_iterator(); ++it)
139                         {
140                                 if(it->path().wstring().find(L".fth") != std::wstring::npos)
141                                 {
142                                         auto from_path = *it;
143                                         auto to_path = boost::filesystem::path(ftemplate + L"/" + it->path().wstring());
144
145                                         if(boost::filesystem::exists(to_path))
146                                                 boost::filesystem::remove(to_path);
147
148                                         boost::filesystem::copy_file(from_path, to_path);
149                                 }
150                         }
151                 }
152                 catch(...)
153                 {
154                         CASPAR_LOG_CURRENT_EXCEPTION();
155                         CASPAR_LOG(error) << L"Failed to copy template-hosts from initial-path to template-path.";
156                 }
157         }
158         catch(...)
159         {
160                 CASPAR_LOG_CURRENT_EXCEPTION();
161                 CASPAR_LOG(error) << L"Failed to create configured directories.";
162         }
163 }
164         
165 const std::wstring& media_folder()
166 {
167         check_is_configured();
168         return media;
169 }
170
171 const std::wstring& log_folder()
172 {
173         check_is_configured();
174         return log;
175 }
176
177 const std::wstring& template_folder()
178 {
179         check_is_configured();
180         return ftemplate;
181 }
182
183 const std::wstring& data_folder()
184 {
185         check_is_configured();
186         return data;
187 }
188
189 const std::wstring& font_folder()
190 {
191         check_is_configured();
192         return font;
193 }
194
195 const std::wstring& thumbnails_folder()
196 {
197         check_is_configured();
198         return thumbnails;
199 }
200
201 #define QUOTE(str) #str
202 #define EXPAND_AND_QUOTE(str) QUOTE(str)
203
204 const std::wstring& version()
205 {
206         static std::wstring ver = u16(
207                         EXPAND_AND_QUOTE(CASPAR_GEN)    "." 
208                         EXPAND_AND_QUOTE(CASPAR_MAYOR)  "." 
209                         EXPAND_AND_QUOTE(CASPAR_MINOR)  "." 
210                         CASPAR_REV      " " 
211                         CASPAR_TAG);
212         return ver;
213 }
214
215 const boost::property_tree::wptree& properties()
216 {
217         check_is_configured();
218         return pt;
219 }
220
221 }}