]> git.sesse.net Git - casparcg/blob - common/env.cpp
* Upgraded to Visual Studio 2013
[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
32 #include <boost/property_tree/ptree.hpp>
33 #include <boost/property_tree/xml_parser.hpp>
34 #include <boost/filesystem.hpp>
35 #include <boost/thread/once.hpp>
36
37 #include <functional>
38 #include <iostream>
39
40 namespace caspar { namespace env {
41         
42 std::wstring media;
43 std::wstring log;
44 std::wstring ftemplate;
45 std::wstring data;
46 std::wstring font;
47 std::wstring thumbnails;
48 boost::property_tree::wptree pt;
49
50 void check_is_configured()
51 {
52         if(pt.empty())
53                 CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info(L"Enviroment properties has not been configured"));
54 }
55
56 void configure(const std::wstring& filename)
57 {
58         try
59         {
60                 auto initialPath = boost::filesystem::initial_path().wstring();
61         
62                 std::wifstream file(initialPath + L"\\" + filename);
63                 boost::property_tree::read_xml(file, pt, boost::property_tree::xml_parser::trim_whitespace | boost::property_tree::xml_parser::no_comments);
64
65                 auto paths      = pt.get_child(L"configuration.paths");
66                 media           = paths.get(L"media-path", initialPath + L"\\media\\");
67                 log                     = paths.get(L"log-path", initialPath + L"\\log\\");
68                 ftemplate       = boost::filesystem::complete(paths.get(L"template-path", initialPath + L"\\template\\")).wstring();            
69                 data            = paths.get(L"data-path", initialPath + L"\\data\\");
70                 font            = paths.get(L"font-path", initialPath + L"\\fonts\\");
71                 thumbnails      = paths.get(L"thumbnails-path", initialPath + L"\\data\\");
72
73                 //Make sure that all paths have a trailing backslash
74                 if(media.at(media.length()-1) != L'\\')
75                         media.append(L"\\");
76                 if(log.at(log.length()-1) != L'\\')
77                         log.append(L"\\");
78                 if(ftemplate.at(ftemplate.length()-1) != L'\\')
79                         ftemplate.append(L"\\");
80                 if(data.at(data.length()-1) != L'\\')
81                         data.append(L"\\");
82                 if(font.at(font.length()-1) != L'\\')
83                         font.append(L"\\");
84                 if(thumbnails.at(thumbnails.length()-1) != L'\\')
85                         thumbnails.append(L"\\");
86
87                 try
88                 {
89                         for(auto it = boost::filesystem::directory_iterator(initialPath); it != boost::filesystem::directory_iterator(); ++it)
90                         {
91                                 if(it->path().wstring().find(L".fth") != std::wstring::npos)                    
92                                 {
93                                         auto from_path = *it;
94                                         auto to_path = boost::filesystem::path(ftemplate + L"/" + it->path().wstring());
95                                 
96                                         if(boost::filesystem::exists(to_path))
97                                                 boost::filesystem::remove(to_path);
98
99                                         boost::filesystem::copy_file(from_path, to_path);
100                                 }       
101                         }
102                 }
103                 catch(...)
104                 {
105                         CASPAR_LOG_CURRENT_EXCEPTION();
106                         CASPAR_LOG(error) << L"Failed to copy template-hosts from initial-path to template-path.";
107                 }
108         }
109         catch(...)
110         {
111                 CASPAR_LOG(error) << L" ### Invalid configuration file. ###";
112                 throw;
113         }
114
115         try
116         {
117                 try
118                 {
119                         auto log_path = boost::filesystem::path(log);
120                         if(!boost::filesystem::exists(log_path))
121                                 boost::filesystem::create_directories(log_path);
122                 }
123                 catch(...)
124                 {
125                         log = L"./";
126                 }
127
128                 auto media_path = boost::filesystem::path(media);
129                 if(!boost::filesystem::exists(media_path))
130                         boost::filesystem::create_directories(media_path);
131                                 
132                 auto template_path = boost::filesystem::path(ftemplate);
133                 if(!boost::filesystem::exists(template_path))
134                         boost::filesystem::create_directories(template_path);
135                 
136                 auto data_path = boost::filesystem::path(data);
137                 if(!boost::filesystem::exists(data_path))
138                         boost::filesystem::create_directories(data_path);
139
140                 auto font_path = boost::filesystem::path(font);
141                 if(!boost::filesystem::exists(font_path))
142                         boost::filesystem::create_directories(font_path);
143
144                 auto thumbnails_path = boost::filesystem::path(thumbnails);
145                 if(!boost::filesystem::exists(thumbnails_path))
146                         boost::filesystem::create_directories(thumbnails_path);
147         }
148         catch(...)
149         {
150                 CASPAR_LOG_CURRENT_EXCEPTION();
151                 CASPAR_LOG(error) << L"Failed to create configured directories.";
152         }
153 }
154         
155 const std::wstring& media_folder()
156 {
157         check_is_configured();
158         return media;
159 }
160
161 const std::wstring& log_folder()
162 {
163         check_is_configured();
164         return log;
165 }
166
167 const std::wstring& template_folder()
168 {
169         check_is_configured();
170         return ftemplate;
171 }
172
173 const std::wstring& data_folder()
174 {
175         check_is_configured();
176         return data;
177 }
178
179 const std::wstring& font_folder()
180 {
181         check_is_configured();
182         return font;
183 }
184
185 const std::wstring& thumbnails_folder()
186 {
187         check_is_configured();
188         return thumbnails;
189 }
190
191 #define QUOTE(str) #str
192 #define EXPAND_AND_QUOTE(str) QUOTE(str)
193
194 const std::wstring& version()
195 {
196         static std::wstring ver = u16(
197                         EXPAND_AND_QUOTE(CASPAR_GEN)    "." 
198                         EXPAND_AND_QUOTE(CASPAR_MAYOR)  "." 
199                         EXPAND_AND_QUOTE(CASPAR_MINOR)  "." 
200                         CASPAR_REV      " " 
201                         CASPAR_TAG);
202         return ver;
203 }
204
205 const boost::property_tree::wptree& properties()
206 {
207         check_is_configured();
208         return pt;
209 }
210
211 std::wstring system_font_folder()
212 {
213         return L"C:\\windows\\Fonts\\";
214 }
215
216 }}