]> git.lyx.org Git - lyx.git/blobdiff - src/format.C
Small formatting and comment cleanup.
[lyx.git] / src / format.C
index f4e88d68add0abfd0232a09c56fabfdc32ec682d..fa327785dadd188a85976ccb42f90777708a94f2 100644 (file)
@@ -18,6 +18,7 @@
 #include "gettext.h"
 #include "lyxsocket.h"
 
+#include "frontends/Application.h"
 #include "frontends/Alert.h" //to be removed?
 
 #include "support/filetools.h"
 
 #include <boost/filesystem/operations.hpp>
 
-using lyx::support::absolutePath;
-using lyx::support::bformat;
-using lyx::support::compare_ascii_no_case;
-using lyx::support::contains;
-using lyx::support::libScriptSearch;
-using lyx::support::makeDisplayPath;
-using lyx::support::onlyPath;
-using lyx::support::quoteName;
-using lyx::support::subst;
-using lyx::support::Systemcall;
-using lyx::support::token;
+
+namespace lyx {
+
+using support::absolutePath;
+using support::bformat;
+using support::compare_ascii_no_case;
+using support::contains;
+using support::FileName;
+using support::libScriptSearch;
+using support::makeDisplayPath;
+using support::onlyPath;
+using support::quoteName;
+using support::subst;
+using support::Systemcall;
+using support::token;
 
 using std::string;
 using std::distance;
 
+namespace Alert = frontend::Alert;
 namespace fs = boost::filesystem;
-namespace os = lyx::support::os;
-
-extern LyXServerSocket * lyxsocket;
+namespace os = support::os;
 
 namespace {
 
@@ -93,9 +97,9 @@ bool operator<(Format const & a, Format const & b)
 
 Format::Format(string const & n, string const & e, string const & p,
               string const & s, string const & v, string const & ed,
-              bool d)
+               int flags)
        : name_(n), extension_(e), prettyname_(p), shortcut_(s), viewer_(v),
-         editor_(ed), document_(d)
+         editor_(ed), flags_(flags)
 {}
 
 
@@ -133,17 +137,17 @@ Format const * Formats::getFormat(string const & name) const
 }
 
 
-string Formats::getFormatFromFile(string const & filename) const
+string Formats::getFormatFromFile(FileName const & filename) const
 {
        if (filename.empty())
                return string();
 
-       string const format = lyx::support::getFormatFromContents(filename);
+       string const format = support::getFormatFromContents(filename);
        if (!format.empty())
                return format;
 
        // try to find a format from the file extension.
-       string const ext(lyx::support::getExtension(filename));
+       string const ext(support::getExtension(filename.absFilename()));
        if (!ext.empty()) {
                // this is ambigous if two formats have the same extension,
                // but better than nothing
@@ -209,24 +213,25 @@ int Formats::getNumber(string const & name) const
 void Formats::add(string const & name)
 {
        if (!getFormat(name))
-               add(name, name, name, string(), string(), string(), true);
+               add(name, name, name, string(), string(), string(),
+                   Format::document);
 }
 
 
 void Formats::add(string const & name, string const & extension,
                  string const & prettyname, string const & shortcut,
-                 string const & viewer, string const & editor, bool document)
+                  string const & viewer, string const & editor,
+                  int flags)
 {
        FormatList::iterator it =
                find_if(formatlist.begin(), formatlist.end(),
                        FormatNamesEqual(name));
        if (it == formatlist.end())
                formatlist.push_back(Format(name, extension, prettyname,
-                                           shortcut, viewer, editor,
-                                           document));
+                                           shortcut, viewer, editor, flags));
        else
                *it = Format(name, extension, prettyname, shortcut, viewer,
-                            editor, document);
+                            editor, flags);
 }
 
 
@@ -257,14 +262,13 @@ void Formats::setViewer(string const & name, string const & command)
 }
 
 
-bool Formats::view(Buffer const & buffer, string const & filename,
+bool Formats::view(Buffer const & buffer, FileName const & filename,
                   string const & format_name) const
 {
-       BOOST_ASSERT(absolutePath(filename));
-       if (filename.empty() || !fs::exists(filename)) {
-               Alert::error(lyx::to_utf8(_("Cannot view file")),
-                       bformat(lyx::to_utf8(_("File does not exist: %1$s")),
-                               filename));
+       if (filename.empty() || !fs::exists(filename.toFilesystemEncoding())) {
+               Alert::error(_("Cannot view file"),
+                       bformat(_("File does not exist: %1$s"),
+                               from_utf8(filename.absFilename())));
                return false;
        }
 
@@ -275,19 +279,19 @@ bool Formats::view(Buffer const & buffer, string const & filename,
        if (!format || format->viewer().empty()) {
 // FIXME: I believe this is the wrong place to show alerts, it should be done
 // by the caller (this should be "utility" code)
-               Alert::error(lyx::to_utf8(_("Cannot view file")),
-                       bformat(lyx::to_utf8(_("No information for viewing %1$s")),
+               Alert::error(_("Cannot view file"),
+                       bformat(_("No information for viewing %1$s"),
                                prettyName(format_name)));
                return false;
        }
        // viewer is 'auto'
        if (format->viewer() == "auto") {
-               if (os::autoOpenFile(filename, os::VIEW))
+               if (os::autoOpenFile(filename.absFilename(), os::VIEW))
                        return true;
                else {
-                       Alert::error(lyx::to_utf8(_("Cannot view file")),
-                               bformat(lyx::to_utf8(_("Auto-view file %1$s failed")),
-                                       filename));
+                       Alert::error(_("Cannot view file"),
+                               bformat(_("Auto-view file %1$s failed"),
+                                       from_utf8(filename.absFilename())));
                        return false;
                }
        }
@@ -308,18 +312,19 @@ bool Formats::view(Buffer const & buffer, string const & filename,
        if (!contains(command, token_from))
                command += ' ' + token_from;
 
-       command = subst(command, token_from, quoteName(filename));
-       command = subst(command, token_path, quoteName(onlyPath(filename)));
-       command = subst(command, token_socket, quoteName(lyxsocket->address()));
+       command = subst(command, token_from, quoteName(filename.toFilesystemEncoding()));
+       command = subst(command, token_path, quoteName(onlyPath(filename.toFilesystemEncoding())));
+       command = subst(command, token_socket, quoteName(theLyXServerSocket().address()));
        lyxerr[Debug::FILES] << "Executing command: " << command << std::endl;
-       buffer.message(lyx::to_utf8(_("Executing command: ")) + command);
+       // FIXME UNICODE utf8 can be wrong for files
+       buffer.message(_("Executing command: ") + from_utf8(command));
 
        Systemcall one;
        int const res = one.startscript(Systemcall::DontWait, command);
 
        if (res) {
-               Alert::error(lyx::to_utf8(_("Cannot view file")),
-                            bformat(lyx::to_utf8(_("An error occurred whilst running %1$s")),
+               Alert::error(_("Cannot view file"),
+                            bformat(_("An error occurred whilst running %1$s"),
                               makeDisplayPath(command, 50)));
                return false;
        }
@@ -327,14 +332,13 @@ bool Formats::view(Buffer const & buffer, string const & filename,
 }
 
 
-bool Formats::edit(Buffer const & buffer, string const & filename,
+bool Formats::edit(Buffer const & buffer, FileName const & filename,
                         string const & format_name) const
 {
-       BOOST_ASSERT(absolutePath(filename));
-       if (filename.empty() || !fs::exists(filename)) {
-               Alert::error(lyx::to_utf8(_("Cannot edit file")),
-                       bformat(lyx::to_utf8(_("File does not exist: %1$s")),
-                               filename));
+       if (filename.empty() || !fs::exists(filename.toFilesystemEncoding())) {
+               Alert::error(_("Cannot edit file"),
+                       bformat(_("File does not exist: %1$s"),
+                               from_utf8(filename.absFilename())));
                return false;
        }
 
@@ -345,19 +349,19 @@ bool Formats::edit(Buffer const & buffer, string const & filename,
        if (!format || format->editor().empty()) {
 // FIXME: I believe this is the wrong place to show alerts, it should
 // be done by the caller (this should be "utility" code)
-               Alert::error(lyx::to_utf8(_("Cannot edit file")),
-                       bformat(lyx::to_utf8(_("No information for editing %1$s")),
+               Alert::error(_("Cannot edit file"),
+                       bformat(_("No information for editing %1$s"),
                                prettyName(format_name)));
                return false;
        }
        // editor is 'auto'
        if (format->editor() == "auto") {
-               if (os::autoOpenFile(filename, os::EDIT))
+               if (os::autoOpenFile(filename.absFilename(), os::EDIT))
                        return true;
                else {
-                       Alert::error(lyx::to_utf8(_("Cannot edit file")),
-                               bformat(lyx::to_utf8(_("Auto-edit file %1$s failed")),
-                                       filename));
+                       Alert::error(_("Cannot edit file"),
+                               bformat(_("Auto-edit file %1$s failed"),
+                                       from_utf8(filename.absFilename())));
                        return false;
                }
        }
@@ -367,18 +371,19 @@ bool Formats::edit(Buffer const & buffer, string const & filename,
        if (!contains(command, token_from))
                command += ' ' + token_from;
 
-       command = subst(command, token_from, quoteName(filename));
-       command = subst(command, token_path, quoteName(onlyPath(filename)));
-       command = subst(command, token_socket, quoteName(lyxsocket->address()));
+       command = subst(command, token_from, quoteName(filename.toFilesystemEncoding()));
+       command = subst(command, token_path, quoteName(onlyPath(filename.toFilesystemEncoding())));
+       command = subst(command, token_socket, quoteName(theLyXServerSocket().address()));
        lyxerr[Debug::FILES] << "Executing command: " << command << std::endl;
-       buffer.message(lyx::to_utf8(_("Executing command: ")) + command);
+       // FIXME UNICODE utf8 can be wrong for files
+       buffer.message(_("Executing command: ") + from_utf8(command));
 
        Systemcall one;
        int const res = one.startscript(Systemcall::DontWait, command);
 
        if (res) {
-               Alert::error(lyx::to_utf8(_("Cannot edit file")),
-                            bformat(lyx::to_utf8(_("An error occurred whilst running %1$s")),
+               Alert::error(_("Cannot edit file"),
+                            bformat(_("An error occurred whilst running %1$s"),
                               makeDisplayPath(command, 50)));
                return false;
        }
@@ -386,13 +391,13 @@ bool Formats::edit(Buffer const & buffer, string const & filename,
 }
 
 
-string const Formats::prettyName(string const & name) const
+docstring const Formats::prettyName(string const & name) const
 {
        Format const * format = getFormat(name);
        if (format)
-               return format->prettyname();
+               return from_utf8(format->prettyname());
        else
-               return name;
+               return from_utf8(name);
 }
 
 
@@ -411,3 +416,6 @@ string const Formats::extension(string const & name) const
 Formats formats;
 
 Formats system_formats;
+
+
+} // namespace lyx