]> git.lyx.org Git - lyx.git/blob - src/importer.C
Change _() to return a docstring. Fixup callers with the help of lyx::to_utf8.
[lyx.git] / src / importer.C
1 /**
2  * \file importer.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author unknown
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "importer.h"
16 #include "converter.h"
17 #include "format.h"
18 #include "frontends/LyXView.h"
19 #include "funcrequest.h"
20 #include "lyx_cb.h"
21
22 #include "bufferlist.h"
23 #include "support/filetools.h"
24 #include "frontends/Alert.h"
25 #include "gettext.h"
26 #include "BufferView.h"
27 #include "buffer_funcs.h"
28
29 using lyx::support::bformat;
30 using lyx::support::changeExtension;
31 using lyx::support::makeDisplayPath;
32
33 using std::find;
34 using std::string;
35 using std::vector;
36
37
38 extern BufferList bufferlist;
39
40
41 bool Importer::Import(LyXView * lv, string const & filename,
42                       string const & format, ErrorList & errorList)
43 {
44         string const displaypath = makeDisplayPath(filename);
45         lv->message(bformat(lyx::to_utf8(_("Importing %1$s...")), displaypath));
46
47         string const lyxfile = changeExtension(filename, ".lyx");
48
49         string loader_format;
50         vector<string> loaders = Loaders();
51         if (find(loaders.begin(), loaders.end(), format) == loaders.end()) {
52                 for (vector<string>::const_iterator it = loaders.begin();
53                      it != loaders.end(); ++it) {
54                         if (converters.isReachable(format, *it)) {
55                                 if (!converters.convert(0, filename, filename,
56                                                         format, *it, errorList))
57                                         return false;
58                                 loader_format = *it;
59                                 break;
60                         }
61                 }
62                 if (loader_format.empty()) {
63                         Alert::error(lyx::to_utf8(_("Couldn't import file")),
64                                      bformat(lyx::to_utf8(_("No information for importing the format %1$s.")),
65                                          formats.prettyName(format)));
66                         return false;
67                 }
68         } else
69                 loader_format = format;
70
71
72         if (loader_format == "lyx") {
73                 lv->loadLyXFile(lyxfile);
74         } else {
75                 Buffer * const b = newFile(lyxfile, string(), true);
76                 if (b)
77                         lv->setBuffer(b);
78                 else
79                         return false;
80                 bool as_paragraphs = loader_format == "textparagraph";
81                 string filename2 = (loader_format == format) ? filename
82                         : changeExtension(filename,
83                                           formats.extension(loader_format));
84                 insertAsciiFile(lv->view(), filename2, as_paragraphs);
85                 lv->dispatch(FuncRequest(LFUN_MARK_OFF));
86         }
87
88         // we are done
89         lv->message(lyx::to_utf8(_("imported.")));
90         return true;
91 }
92
93
94 vector<Format const *> const Importer::GetImportableFormats()
95 {
96         vector<string> loaders = Loaders();
97         vector<Format const *> result =
98                 converters.getReachableTo(loaders[0], true);
99         for (vector<string>::const_iterator it = loaders.begin() + 1;
100              it != loaders.end(); ++it) {
101                 vector<Format const *> r =
102                         converters.getReachableTo(*it, false);
103                 result.insert(result.end(), r.begin(), r.end());
104         }
105         return result;
106 }
107
108
109 vector<string> const Importer::Loaders()
110 {
111         vector<string> v;
112         v.push_back("lyx");
113         v.push_back("text");
114         v.push_back("textparagraph");
115         return v;
116 }