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