]> git.lyx.org Git - lyx.git/blob - src/support/Package.cpp
Fix 'Export As...' in non-English localizations
[lyx.git] / src / support / Package.cpp
1 // -*- C++ -*-
2 /**
3  * \file package.cpp
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Angus Leeming
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "support/Package.h"
15
16 #include "support/debug.h"
17 #include "support/environment.h"
18 #include "support/ExceptionMessage.h"
19 #include "support/filetools.h"
20 #include "support/gettext.h"
21 #include "support/lassert.h"
22 #include "support/lstrings.h"
23 #include "support/os.h"
24
25 #if defined (USE_WINDOWS_PACKAGING)
26 # include "support/os_win32.h"
27 #endif
28
29
30 #include <list>
31
32 #if !defined (USE_WINDOWS_PACKAGING) && \
33     !defined (USE_MACOSX_PACKAGING) && \
34     !defined (USE_POSIX_PACKAGING)
35 #error USE_FOO_PACKAGING must be defined for FOO = WINDOWS, MACOSX or POSIX.
36 #endif
37
38 #if defined (USE_MACOSX_PACKAGING)
39 # include "support/qstring_helpers.h"
40 # include <QDir>
41 # include <QDesktopServices>
42 #endif
43
44 using namespace std;
45
46 namespace lyx {
47 namespace support {
48
49 namespace {
50
51 Package package_;
52 bool initialised_ = false;
53
54 } // namespace anon
55
56
57 void init_package(string const & command_line_arg0,
58                   string const & command_line_system_support_dir,
59                   string const & command_line_user_support_dir)
60 {
61         package_ = Package(command_line_arg0,
62                            command_line_system_support_dir,
63                            command_line_user_support_dir);
64         initialised_ = true;
65 }
66
67
68 Package const & package()
69 {
70         LAPPERR(initialised_);
71         return package_;
72 }
73
74
75 namespace {
76
77 FileName const abs_path_from_binary_name(string const & exe);
78
79
80 bool inBuildDir(FileName const & abs_binary, FileName &, FileName &);
81
82 FileName findLyxBinary(FileName const & abs_binary);
83
84 FileName const get_document_dir(FileName const & home_dir);
85
86 FileName const get_locale_dir(FileName const & system_support_dir);
87
88 FileName const get_system_support_dir(FileName const & abs_binary,
89                                     string const & command_line_system_support_dir);
90
91 FileName const get_default_user_support_dir(FileName const & home_dir);
92
93 bool userSupportDir(FileName const & default_user_support_dir,
94                      string const & command_line_user_support_dir, FileName & result);
95
96 string const & with_version_suffix();
97
98 string const fix_dir_name(string const & name);
99
100 } // namespace anon
101
102
103 Package::Package(string const & command_line_arg0,
104                  string const & command_line_system_support_dir,
105                  string const & command_line_user_support_dir)
106         : explicit_user_support_dir_(false)
107 {
108         // Specification of temp_dir_ may be reset by LyXRC,
109         // but the default is fixed for a given OS.
110         system_temp_dir_ = FileName::tempPath();
111         temp_dir_ = system_temp_dir_;
112         document_dir_ = get_document_dir(get_home_dir());
113
114         FileName const abs_binary = abs_path_from_binary_name(command_line_arg0);
115         binary_dir_ = FileName(onlyPath(abs_binary.absFileName()));
116
117         // the LyX package directory
118         lyx_dir_ = FileName(addPath(binary_dir_.absFileName(), "../"));
119         lyx_dir_ = FileName(lyx_dir_.realPath());
120
121         // Is LyX being run in-place from the build tree?
122         in_build_dir_ = inBuildDir(abs_binary, build_support_dir_, system_support_dir_);
123
124         if (!in_build_dir_) {
125                 system_support_dir_ =
126                         get_system_support_dir(abs_binary,
127                                                command_line_system_support_dir);
128         }
129
130         // Find the LyX executable
131         lyx_binary_ = findLyxBinary(abs_binary);
132
133         locale_dir_ = get_locale_dir(system_support_dir_);
134
135         FileName const default_user_support_dir =
136                 get_default_user_support_dir(get_home_dir());
137
138         explicit_user_support_dir_ = userSupportDir(default_user_support_dir,
139                                      command_line_user_support_dir, user_support_dir_);
140
141         FileName const configure_script(addName(system_support().absFileName(), "configure.py"));
142         configure_command_ = os::python() + ' ' +
143                         quoteName(configure_script.toFilesystemEncoding(), quote_python) +
144                         with_version_suffix() + " --binary-dir=" +
145                         quoteName(FileName(binary_dir().absFileName()).toFilesystemEncoding());
146
147         LYXERR(Debug::INIT, "<package>\n"
148                 << "\tbinary_dir " << binary_dir().absFileName() << '\n'
149                 << "\tsystem_support " << system_support().absFileName() << '\n'
150                 << "\tbuild_support " << build_support().absFileName() << '\n'
151                 << "\tuser_support " << user_support().absFileName() << '\n'
152                 << "\tlocale_dir " << locale_dir().absFileName() << '\n'
153                 << "\tdocument_dir " << document_dir().absFileName() << '\n'
154                 << "\ttemp_dir " << temp_dir().absFileName() << '\n'
155                 << "\thome_dir " << get_home_dir().absFileName() << '\n'
156                 << "</package>\n");
157 }
158
159
160 void Package::set_temp_dir(FileName const & temp_dir) const
161 {
162         if (temp_dir.empty())
163                 temp_dir_ = system_temp_dir_;
164         else
165                 temp_dir_ = temp_dir;
166 }
167
168
169 FileName Package::messages_file(string const & c) const
170 {
171         if (in_build_dir_) {
172                 FileName res = FileName(lyx_dir().absFileName() + "/../po/" + c + ".gmo");
173                 if (!res.isReadableFile())
174                         res = FileName(top_srcdir().absFileName() + "/po/" + c + ".gmo");
175                 return res;
176         } else
177                 return FileName(locale_dir_.absFileName() + "/" + c
178                         + "/LC_MESSAGES/" PACKAGE ".mo");
179 }
180
181
182 // The specification of home_dir_ is fixed for a given OS.
183 // A typical example on Windows: "C:/Documents and Settings/USERNAME"
184 // and on a Posix-like machine: "/home/USERNAME".
185 FileName const & Package::get_home_dir()
186 {
187 #if defined (USE_WINDOWS_PACKAGING)
188         static FileName const home_dir(getEnv("USERPROFILE"));
189 #elif defined (USE_MACOSX_PACKAGING)
190         static FileName const home_dir(fromqstr(QDir::homePath()));
191 #else // Posix-like.
192         static FileName const home_dir(getEnv("HOME"));
193 #endif
194         return home_dir;
195 }
196
197
198 namespace {
199
200 // These next functions contain the stuff that is substituted at
201 // configuration-time.
202 FileName const hardcoded_localedir()
203 {
204         // FIXME UNICODE
205         // The build system needs to make sure that this is in utf8 encoding.
206         return FileName(LYX_ABS_INSTALLED_LOCALEDIR);
207 }
208
209
210 FileName const hardcoded_system_support_dir()
211 {
212         // FIXME UNICODE
213         // The build system needs to make sure that this is in utf8 encoding.
214         return FileName(LYX_ABS_INSTALLED_DATADIR);
215 }
216
217
218 string const & with_version_suffix()
219 {
220         static string const program_suffix = PROGRAM_SUFFIX;
221         static string const with_version_suffix =
222                 " --with-version-suffix=" PROGRAM_SUFFIX;
223         return program_suffix.empty() ? program_suffix : with_version_suffix;
224 }
225
226 } // namespace anon
227
228
229 FileName const & Package::top_srcdir()
230 {
231         // FIXME UNICODE
232         // The build system needs to make sure that this is in utf8 encoding.
233         static FileName const dir(LYX_ABS_TOP_SRCDIR);
234         return dir;
235 }
236
237
238 namespace {
239
240 bool check_command_line_dir(string const & dir,
241                             string const & file,
242                             string const & command_line_switch);
243
244 FileName const extract_env_var_dir(string const & env_var);
245
246 bool check_env_var_dir(FileName const & dir,
247                        string const & env_var);
248
249 bool check_env_var_dir(FileName const & dir,
250                        string const & file,
251                        string const & env_var);
252
253 string const relative_locale_dir();
254
255 string const relative_system_support_dir();
256
257
258 /**
259  * Convert \p name to internal path and strip a trailing slash, since it
260  * comes from user input (commandline or environment).
261  * \p name is encoded in utf8.
262  */
263 string const fix_dir_name(string const & name)
264 {
265         return rtrim(os::internal_path(name), "/");
266 }
267
268
269
270 bool isBuildDir(FileName const & abs_binary, string const & dir_location,
271         FileName & build_support_dir)
272 {
273     string search_dir = onlyPath(abs_binary.absFileName()) + dir_location;
274
275     // Makefile by automake
276     build_support_dir = FileName(addPath(search_dir, "lib"));
277     if (!fileSearch(build_support_dir.absFileName(), "Makefile").empty()) {
278         return true;
279     }
280     //  cmake file, no Makefile in lib
281     FileName build_boost_dir = FileName(addPath(search_dir, "boost"));
282     if (!fileSearch(build_boost_dir.absFileName(), "cmake_install.cmake").empty()) {
283         return true;
284     }
285
286     return false;
287 }
288
289 bool inBuildDir(FileName const & abs_binary,
290         FileName & build_support_dir, FileName & system_support_dir)
291 {
292         string const check_text = "Checking whether LyX is run in place...";
293
294         // We're looking for "Makefile" in a directory
295         //   binary_dir/../lib
296         // We're also looking for "chkconfig.ltx" in a directory
297         //   top_srcdir()/lib
298         // If both are found, then we're running LyX in-place.
299
300         // Note that the name of the lyx binary may be a symbolic link.
301         // If that is the case, then we follow the links too.
302     FileName binary = abs_binary;
303         while (true) {
304                 // Try and find "lyxrc.defaults".
305                 if( isBuildDir(binary, "../", build_support_dir) ||
306             isBuildDir(binary, "../../", build_support_dir))
307         {
308                         // Try and find "chkconfig.ltx".
309                         system_support_dir =
310                                 FileName(addPath(Package::top_srcdir().absFileName(), "lib"));
311
312                         if (!fileSearch(system_support_dir.absFileName(), "chkconfig.ltx").empty()) {
313                                 LYXERR(Debug::INIT, check_text << " yes");
314                                 return true;
315                         }
316                 }
317
318                 // Check whether binary is a symbolic link.
319                 // If so, resolve it and repeat the exercise.
320                 if (!binary.isSymLink())
321                         break;
322
323                 FileName link;
324                 if (readLink(binary, link)) {
325                         binary = link;
326                 } else {
327                         // Unable to resolve the link.
328                         break;
329                 }
330         }
331
332         LYXERR(Debug::INIT, check_text << " no");
333         system_support_dir = FileName();
334         build_support_dir = FileName();
335
336     return false;
337 }
338
339
340 bool doesFileExist(FileName & result, string const & search_dir, string const & name)
341 {
342     result = fileSearch(search_dir, name);
343     if (!result.empty()) {
344         return true;
345     }
346     return false;
347 }
348
349
350 bool lyxBinaryPath(FileName & lyx_binary, string const & search_dir, string const & ext)
351 {
352     lyx_binary = FileName();
353     if(false) {   
354     } else if (doesFileExist(lyx_binary, search_dir, "lyx" + ext)) {
355     } else if (doesFileExist(lyx_binary, search_dir, "LyX" + ext)) {
356     } else if (doesFileExist(lyx_binary, search_dir, "lyx" + string(PROGRAM_SUFFIX) + ext)) {
357     } else if (doesFileExist(lyx_binary, search_dir, "LyX" + string(PROGRAM_SUFFIX) + ext)){
358     }
359     return !lyx_binary.empty() ? true : false;
360 }
361
362
363 FileName findLyxBinary(FileName const & abs_binary)
364 {
365     string ext;
366     string checkname = abs_binary.toFilesystemEncoding();
367     int check_len = checkname.length();
368     int prgsuffixlen = string(PROGRAM_SUFFIX).length();
369     if ((prgsuffixlen > 0) && (check_len > prgsuffixlen) &&
370        (checkname.substr(check_len-prgsuffixlen) == string(PROGRAM_SUFFIX))) {
371         ext = "";
372     }
373     else if (!abs_binary.extension().empty()) {
374         ext = "." + abs_binary.extension();
375     }
376     
377     string binary_dir = onlyPath(abs_binary.absFileName());
378       
379     FileName lyx_binary;
380     if (lyxBinaryPath(lyx_binary, binary_dir, ext))
381         return lyx_binary;
382
383     string search_dir = onlyPath(FileName(addPath(binary_dir, "/../")).absFileName());
384     if (lyxBinaryPath(lyx_binary, search_dir, ext))
385         return lyx_binary;
386     
387     return FileName();
388 }
389
390
391 // Specification of document_dir_ may be reset by LyXRC,
392 // but the default is fixed for a given OS.
393 FileName const get_document_dir(FileName const & home_dir)
394 {
395 #if defined (USE_WINDOWS_PACKAGING)
396         (void)home_dir; // Silence warning about unused variable.
397         os::GetFolderPath win32_folder_path;
398         return FileName(win32_folder_path(os::GetFolderPath::PERSONAL));
399 #elif defined (USE_MACOSX_PACKAGING) && (QT_VERSION >= 0x050000)
400         (void)home_dir; // Silence warning about unused variable.
401         return FileName(fromqstr(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)));
402 #elif defined (USE_MACOSX_PACKAGING)
403         (void)home_dir; // Silence warning about unused variable.
404         return FileName(fromqstr(QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation)));
405 #else // Posix-like.
406         return home_dir;
407 #endif
408 }
409
410
411
412 // Several sources are probed to ascertain the locale directory.
413 // The only requirement is that the result is indeed a directory.
414 FileName const get_locale_dir(FileName const & system_support_dir)
415 {
416         // 1. Use the "LYX_LOCALEDIR" environment variable.
417         FileName const path_env = extract_env_var_dir("LYX_LOCALEDIR");
418         if (!path_env.empty() && check_env_var_dir(path_env, "LYX_LOCALEDIR"))
419                 return path_env;
420
421         // 2. Search for system_support_dir / <relative locale dir>
422         // The <relative locale dir> is OS-dependent. (On Unix, it will
423         // be "../locale/".)
424         FileName path(addPath(system_support_dir.absFileName(),
425                 relative_locale_dir()));
426
427         if (path.exists() && path.isDirectory())
428                 return path;
429
430         // 3. Fall back to the hard-coded LOCALEDIR.
431         path = hardcoded_localedir();
432         if (path.exists() && path.isDirectory())
433                 return path;
434
435         return FileName();
436 }
437
438
439 // Extracts the absolute path from the foo of "-sysdir foo" or "-userdir foo"
440 FileName const abs_path_from_command_line(string const & command_line)
441 {
442         if (command_line.empty())
443                 return FileName();
444
445         string const str_path = fix_dir_name(command_line);
446         return makeAbsPath(str_path);
447 }
448
449
450 // Does the grunt work for abs_path_from_binary_name()
451 FileName const get_binary_path(string const & exe)
452 {
453 #if defined (USE_WINDOWS_PACKAGING)
454         // The executable may have been invoked either with or
455         // without the .exe extension.
456         // Ensure that it is present.
457         string const as_internal_path = os::internal_path(exe);
458         string const exe_path = suffixIs(as_internal_path, ".exe") ?
459                 as_internal_path : as_internal_path + ".exe";
460 #else
461         string const exe_path = os::internal_path(exe);
462 #endif
463         if (FileName::isAbsolute(exe_path))
464                 return FileName(exe_path);
465
466         // Two possibilities present themselves.
467         // 1. The binary is relative to the CWD.
468         FileName const abs_exe_path = makeAbsPath(exe_path);
469         if (abs_exe_path.exists())
470                 return abs_exe_path;
471
472         // 2. exe must be the name of the binary only and it
473         // can be found on the PATH.
474         string const exe_name = onlyFileName(exe_path);
475         if (exe_name != exe_path)
476                 return FileName();
477
478         vector<string> const path = getEnvPath("PATH");
479         vector<string>::const_iterator it = path.begin();
480         vector<string>::const_iterator const end = path.end();
481         for (; it != end; ++it) {
482                 // This will do nothing if *it is already absolute.
483                 string const exe_dir = makeAbsPath(*it).absFileName();
484
485                 FileName const exe_path(addName(exe_dir, exe_name));
486                 if (exe_path.exists())
487                         return exe_path;
488         }
489
490         // Didn't find anything.
491         return FileName();
492 }
493
494
495 // Extracts the absolute path to the binary name received as argv[0].
496 FileName const abs_path_from_binary_name(string const & exe)
497 {
498         FileName const abs_binary = get_binary_path(exe);
499         if (abs_binary.empty()) {
500                 // FIXME UNICODE
501                 throw ExceptionMessage(ErrorException,
502                         _("LyX binary not found"),
503                         bformat(_("Unable to determine the path to the LyX binary from the command line %1$s"),
504                                 from_utf8(exe)));
505         }
506         return abs_binary;
507 }
508
509
510 // A plethora of directories is searched to ascertain the system
511 // lyxdir which is defined as the first directory to contain
512 // "chkconfig.ltx".
513 FileName const
514 get_system_support_dir(FileName const & abs_binary,
515                   string const & command_line_system_support_dir)
516 {
517         string const chkconfig_ltx = "chkconfig.ltx";
518
519         // searched_dirs is used for diagnostic purposes only in the case
520         // that "chkconfig.ltx" is not found.
521         list<FileName> searched_dirs;
522
523         // 1. Use the -sysdir command line parameter.
524         FileName path = abs_path_from_command_line(command_line_system_support_dir);
525         if (!path.empty()) {
526                 searched_dirs.push_back(path);
527                 if (check_command_line_dir(path.absFileName(), chkconfig_ltx, "-sysdir"))
528                         return path;
529         }
530
531         // 2. Use the "LYX_DIR_${major}${minor}x" environment variable.
532         path = extract_env_var_dir(LYX_DIR_VER);
533         if (!path.empty()) {
534                 searched_dirs.push_back(path);
535                 if (check_env_var_dir(path, chkconfig_ltx, LYX_DIR_VER))
536                         return path;
537         }
538
539         // 3. Search relative to the lyx binary.
540         // We're looking for "chkconfig.ltx" in a directory
541         //   OnlyPath(abs_binary) / <relative dir> / PACKAGE /
542         // PACKAGE is hardcoded in config.h. Eg "lyx" or "lyx-1.3.6cvs".
543         // <relative dir> is OS-dependent; on Unix, it will be "../share/".
544         string const relative_lyxdir = relative_system_support_dir();
545
546         // One subtlety to be aware of. The name of the lyx binary may be
547         // a symbolic link. If that is the case, then we follow the links too.
548         FileName binary = abs_binary;
549         while (true) {
550                 // Try and find "chkconfig.ltx".
551                 string const binary_dir = onlyPath(binary.absFileName());
552
553                 FileName const lyxdir(addPath(binary_dir, relative_lyxdir));
554                 searched_dirs.push_back(lyxdir);
555
556                 if (!fileSearch(lyxdir.absFileName(), chkconfig_ltx).empty()) {
557                         // Success! "chkconfig.ltx" has been found.
558                         return lyxdir;
559                 }
560
561                 // Check whether binary is a symbolic link.
562                 // If so, resolve it and repeat the exercise.
563                 if (!binary.isSymLink())
564                         break;
565
566                 FileName link;
567                 if (readLink(binary, link)) {
568                         binary = link;
569                 } else {
570                         // Unable to resolve the link.
571                         break;
572                 }
573         }
574
575         // 4. Repeat the exercise on the directory itself.
576         FileName binary_dir(onlyPath(abs_binary.absFileName()));
577         while (true) {
578                 // This time test whether the directory is a symbolic link
579                 // *before* looking for "chkconfig.ltx".
580                 // (We've looked relative to the original already.)
581                 if (!binary.isSymLink())
582                         break;
583
584                 FileName link;
585                 if (readLink(binary_dir, link)) {
586                         binary_dir = link;
587                 } else {
588                         // Unable to resolve the link.
589                         break;
590                 }
591
592                 // Try and find "chkconfig.ltx".
593                 FileName const lyxdir(addPath(binary_dir.absFileName(),
594                         relative_lyxdir));
595                 searched_dirs.push_back(lyxdir);
596
597                 if (!fileSearch(lyxdir.absFileName(), chkconfig_ltx).empty()) {
598                         // Success! "chkconfig.ltx" has been found.
599                         return lyxdir;
600                 }
601         }
602
603         // 5. In desparation, try the hard-coded system support dir.
604         path = hardcoded_system_support_dir();
605         if (!fileSearch(path.absFileName(), chkconfig_ltx).empty())
606                 return path;
607
608         // Everything has failed :-(
609         // So inform the user and exit.
610         string searched_dirs_str;
611         typedef list<FileName>::const_iterator iterator;
612         iterator const begin = searched_dirs.begin();
613         iterator const end = searched_dirs.end();
614         for (iterator it = begin; it != end; ++it) {
615                 if (it != begin)
616                         searched_dirs_str += "\n\t";
617                 searched_dirs_str += it->absFileName();
618         }
619
620         // FIXME UNICODE
621         throw ExceptionMessage(ErrorException, _("No system directory"),
622                 bformat(_("Unable to determine the system directory "
623                                 "having searched\n"
624                                 "\t%1$s\n"
625                                 "Use the '-sysdir' command line parameter or "
626                                 "set the environment variable\n%2$s "
627                                 "to the LyX system directory containing the "
628                                 "file `chkconfig.ltx'."),
629                           from_utf8(searched_dirs_str), from_ascii(LYX_DIR_VER)));
630
631         // Keep the compiler happy.
632         return FileName();
633 }
634
635
636 // Returns the absolute path to the user lyxdir, together with a flag
637 // indicating whether this directory was specified explicitly (as -userdir
638 // or through an environment variable) or whether it was deduced.
639 bool userSupportDir(FileName const & default_user_support_dir,
640         string const & command_line_user_support_dir, FileName & result)
641 {
642         // 1. Use the -userdir command line parameter.
643         result = abs_path_from_command_line(command_line_user_support_dir);
644         if (!result.empty())
645                 return true;
646
647         // 2. Use the LYX_USERDIR_${major}${minor}x environment variable.
648         result = extract_env_var_dir(LYX_USERDIR_VER);
649         if (!result.empty())
650                 return true;
651
652         // 3. Use the OS-dependent default_user_support_dir
653         result = default_user_support_dir;
654         return false;
655 }
656
657
658 // $HOME/.lyx on POSIX but on Win32 it will be something like
659 // "C:/Documents and Settings/USERNAME/Application Data/LyX"
660 FileName const get_default_user_support_dir(FileName const & home_dir)
661 {
662 #if defined (USE_WINDOWS_PACKAGING)
663         (void)home_dir; // Silence warning about unused variable.
664
665         os::GetFolderPath win32_folder_path;
666         return FileName(addPath(win32_folder_path(os::GetFolderPath::APPDATA), PACKAGE));
667
668 #elif defined (USE_MACOSX_PACKAGING) && (QT_VERSION >= 0x050000)
669         (void)home_dir; // Silence warning about unused variable.
670         return FileName(addPath(fromqstr(QStandardPaths::writableLocation(QStandardPaths::DataLocation)), PACKAGE));
671
672 #elif defined (USE_MACOSX_PACKAGING)
673         (void)home_dir; // Silence warning about unused variable.
674         return FileName(addPath(fromqstr(QDesktopServices::storageLocation(QDesktopServices::DataLocation)), PACKAGE));
675
676 #else // USE_POSIX_PACKAGING
677         return FileName(addPath(home_dir.absFileName(), string(".") + PACKAGE));
678 #endif
679 }
680
681
682 // Check that directory @c dir contains @c file.
683 // Else emit an error message about an invalid @c command_line_switch.
684 bool check_command_line_dir(string const & dir,
685                             string const & file,
686                             string const & command_line_switch)
687 {
688         FileName const abs_path = fileSearch(dir, file);
689         if (abs_path.empty()) {
690                 // FIXME UNICODE
691                 throw ExceptionMessage(ErrorException, _("File not found"), bformat(
692                         _("Invalid %1$s switch.\nDirectory %2$s does not contain %3$s."),
693                         from_utf8(command_line_switch), from_utf8(dir),
694                         from_utf8(file)));
695         }
696
697         return !abs_path.empty();
698 }
699
700
701 // The environment variable @c env_var expands to a (single) file path.
702 FileName const extract_env_var_dir(string const & env_var)
703 {
704         string const dir = fix_dir_name(getEnv(env_var));
705         return dir.empty() ? FileName() : makeAbsPath(dir);
706 }
707
708
709 // Check that directory @c dir contains @c file.
710 // Else emit a warning about an invalid @c env_var.
711 bool check_env_var_dir(FileName const & dir,
712                        string const & file,
713                        string const & env_var)
714 {
715         FileName const abs_path = fileSearch(dir.absFileName(), file);
716         if (abs_path.empty()) {
717                 // FIXME UNICODE
718                 throw ExceptionMessage(WarningException, _("File not found"), bformat(
719                         _("Invalid %1$s environment variable.\n"
720                                 "Directory %2$s does not contain %3$s."),
721                         from_utf8(env_var), from_utf8(dir.absFileName()),
722                         from_utf8(file)));
723         }
724
725         return !abs_path.empty();
726 }
727
728
729 // Check that directory @c dir is indeed a directory.
730 // Else emit a warning about an invalid @c env_var.
731 bool check_env_var_dir(FileName const & dir,
732                        string const & env_var)
733 {
734         bool const success = dir.exists() && dir.isDirectory();
735
736         if (!success) {
737                 // Put this string on a single line so that the gettext
738                 // search mechanism in po/Makefile.in.in will register
739                 // Package.cpp.in as a file containing strings that need
740                 // translation.
741                 // FIXME UNICODE
742                 docstring const fmt =
743                         _("Invalid %1$s environment variable.\n%2$s is not a directory.");
744
745                 throw ExceptionMessage(WarningException, _("Directory not found"), bformat(
746                         fmt, from_utf8(env_var), from_utf8(dir.absFileName())));
747         }
748
749         return success;
750 }
751
752
753 // The locale directory relative to the LyX system directory.
754 string const relative_locale_dir()
755 {
756 #if defined (USE_WINDOWS_PACKAGING) || defined (USE_MACOSX_PACKAGING)
757         return "locale/";
758 #else
759         return "../locale/";
760 #endif
761 }
762
763
764 // The system lyxdir is relative to the directory containing the LyX binary.
765 string const relative_system_support_dir()
766 {
767         string result;
768
769 #if defined (USE_WINDOWS_PACKAGING) || defined (USE_MACOSX_PACKAGING)
770         result = "../Resources/";
771 #else // Posix-like.
772         result = addPath("../share/", PACKAGE);
773 #endif
774
775         return result;
776 }
777
778 } // namespace anon
779
780 } // namespace support
781 } // namespace lyx