]> git.sesse.net Git - casparcg/blob - common/env.cpp
[flash] Moved template host copying to flash module startup instead from env setup...
[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 initial;
46 std::wstring media;
47 std::wstring log;
48 std::wstring ftemplate;
49 std::wstring data;
50 std::wstring font;
51 std::wstring thumbnails;
52 boost::property_tree::wptree pt;
53
54 void check_is_configured()
55 {
56         if(pt.empty())
57                 CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info(L"Enviroment properties has not been configured"));
58 }
59
60 void configure(const std::wstring& filename)
61 {
62         try
63         {
64                 initial = boost::filesystem::initial_path().wstring();
65
66                 boost::filesystem::wifstream file(initial + L"/" + filename);
67                 boost::property_tree::read_xml(file, pt, boost::property_tree::xml_parser::trim_whitespace | boost::property_tree::xml_parser::no_comments);
68
69                 auto paths      = pt.get_child(L"configuration.paths");
70                 media           = paths.get(L"media-path", initial + L"/media/");
71                 log                     = paths.get(L"log-path", initial + L"/log/");
72                 ftemplate       = boost::filesystem::complete(paths.get(L"template-path", initial + L"/template/")).wstring();
73                 data            = paths.get(L"data-path", initial + L"/data/");
74                 font            = paths.get(L"font-path", initial + L"/font/");
75                 thumbnails      = paths.get(L"thumbnail-path", initial + L"/thumbnail/");
76         }
77         catch(...)
78         {
79                 CASPAR_LOG(error) << L" ### Invalid configuration file. ###";
80                 throw;
81         }
82
83         try
84         {
85                 auto found_media_path = find_case_insensitive(media);
86                 if (found_media_path)
87                         media = *found_media_path;
88                 else
89                         boost::filesystem::create_directories(media);
90
91                 auto found_template_path = find_case_insensitive(ftemplate);
92                 if (found_template_path)
93                         ftemplate = *found_template_path;
94                 else
95                         boost::filesystem::create_directories(ftemplate);
96
97                 auto found_data_path = find_case_insensitive(data);
98                 if (found_data_path)
99                         data = *found_data_path;
100                 else
101                         boost::filesystem::create_directories(data);
102
103                 auto found_font_path = find_case_insensitive(font);
104                 if (found_font_path)
105                         font = *found_font_path;
106                 else
107                         boost::filesystem::create_directories(font);
108
109                 auto found_thumbnails_path = find_case_insensitive(thumbnails);
110                 if (found_thumbnails_path)
111                         thumbnails = *found_thumbnails_path;
112                 else
113                         boost::filesystem::create_directories(thumbnails);
114
115                 auto found_log_path = find_case_insensitive(log);
116                 if (found_log_path)
117                         log = *found_log_path;
118                 else if (!boost::filesystem::create_directories(log))
119                         log = L"./";
120
121                 //Make sure that all paths have a trailing slash
122                 if(media.at(media.length()-1) != L'/')
123                         media.append(L"/");
124                 if(log.at(log.length()-1) != L'/')
125                         log.append(L"/");
126                 if(ftemplate.at(ftemplate.length()-1) != L'/')
127                         ftemplate.append(L"/");
128                 if(data.at(data.length()-1) != L'/')
129                         data.append(L"/");
130                 if(font.at(font.length()-1) != L'/')
131                         font.append(L"/");
132                 if(thumbnails.at(thumbnails.length()-1) != L'/')
133                         thumbnails.append(L"/");
134         }
135         catch(...)
136         {
137                 CASPAR_LOG_CURRENT_EXCEPTION();
138                 CASPAR_LOG(error) << L"Failed to create configured directories.";
139         }
140 }
141
142 const std::wstring& initial_folder()
143 {
144         check_is_configured();
145         return initial;
146 }
147
148 const std::wstring& media_folder()
149 {
150         check_is_configured();
151         return media;
152 }
153
154 const std::wstring& log_folder()
155 {
156         check_is_configured();
157         return log;
158 }
159
160 const std::wstring& template_folder()
161 {
162         check_is_configured();
163         return ftemplate;
164 }
165
166 const std::wstring& data_folder()
167 {
168         check_is_configured();
169         return data;
170 }
171
172 const std::wstring& font_folder()
173 {
174         check_is_configured();
175         return font;
176 }
177
178 const std::wstring& thumbnails_folder()
179 {
180         check_is_configured();
181         return thumbnails;
182 }
183
184 #define QUOTE(str) #str
185 #define EXPAND_AND_QUOTE(str) QUOTE(str)
186
187 const std::wstring& version()
188 {
189         static std::wstring ver = u16(
190                         EXPAND_AND_QUOTE(CASPAR_GEN)    "."
191                         EXPAND_AND_QUOTE(CASPAR_MAYOR)  "."
192                         EXPAND_AND_QUOTE(CASPAR_MINOR)  "."
193                         CASPAR_REV      " "
194                         CASPAR_TAG);
195         return ver;
196 }
197
198 const boost::property_tree::wptree& properties()
199 {
200         check_is_configured();
201         return pt;
202 }
203
204 }}