]> git.lyx.org Git - lyx.git/blob - src/importer.C
fixed two consecutive spaces handling
[lyx.git] / src / importer.C
1 /* This file is part of
2  * ====================================================== 
3  * 
4  *           LyX, The Document Processor
5  *        
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-2000 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include <algorithm>
18
19 #include "importer.h"
20 #include "converter.h"
21 #include "LyXView.h"
22 #include "lyxfunc.h"
23 #include "bufferlist.h"
24 #include "support/filetools.h"
25 #include "lyx_gui_misc.h" //WriteAlert
26 #include "gettext.h"
27
28 using std::vector;
29 using std::find;
30
31 extern BufferList bufferlist;
32 extern void InsertAsciiFile(BufferView *, string const &, bool);
33
34
35 bool Importer::Import(LyXView * lv, string const & filename, 
36                       string const & format)
37 {
38         string const displaypath = MakeDisplayPath(filename);
39         string const s1 = _("Importing") + ' ' + displaypath + "...";
40         lv->getLyXFunc()->Dispatch(LFUN_MESSAGE, s1);
41
42         string const lyxfile = ChangeExtension(filename, ".lyx");
43
44         string loader_format;
45         vector<string> loaders = Loaders();
46         if (find(loaders.begin(), loaders.end(), format) == loaders.end()) {
47                 for (vector<string>::const_iterator it = loaders.begin();
48                      it != loaders.end(); ++it) {
49                         if (converters.IsReachable(format, *it)) {
50                                 if (!converters.Convert(0, filename, filename,
51                                                         format, *it))
52                                         return false;
53                                 loader_format = *it;
54                                 break;
55                         }
56                 }
57                 if (loader_format.empty()) {
58                         WriteAlert(_("Can not import file"),
59                                    _("No information for importing from ")
60                                    + formats.PrettyName(format));
61                         return false;
62                 }
63         } else
64                 loader_format = format;
65
66
67         if (loader_format == "lyx") {
68                 Buffer * buffer = bufferlist.loadLyXFile(lyxfile);
69                 if (buffer)
70                         lv->view()->buffer(buffer);
71         } else {
72                 lv->view()->buffer(bufferlist.newFile(lyxfile, string(), true));
73                 bool as_paragraphs = loader_format == "textparagraph";
74                 string filename2 = (loader_format == format) ? filename
75                         : ChangeExtension(filename,
76                                           formats.Extension(loader_format));
77                 InsertAsciiFile(lv->view(), filename2, as_paragraphs);
78                 lv->getLyXFunc()->Dispatch(LFUN_MARK_OFF);
79         }
80
81         // we are done
82         lv->getLyXFunc()->Dispatch(LFUN_MESSAGE, _("imported."));
83         return true;
84 }
85
86 #if 0
87 bool Importer::IsImportable(string const & format)
88 {
89         vector<string> loaders = Loaders();
90         for (vector<string>::const_iterator it = loaders.begin();
91              it != loaders.end(); ++it)
92                 if (converters.IsReachable(format, *it))
93                         return true;
94         return false;
95 }
96 #endif
97
98 vector<Format const *> const Importer::GetImportableFormats()
99 {
100         vector<string> loaders = Loaders();
101         vector<Format const *> result = 
102                 converters.GetReachableTo(loaders[0], true);
103         for (vector<string>::const_iterator it = loaders.begin() + 1;
104              it != loaders.end(); ++it) {
105                 vector<Format const *> r =
106                         converters.GetReachableTo(*it, false);
107                 result.insert(result.end(), r.begin(), r.end());
108         }
109         return result;
110 }
111
112
113 vector<string> const Importer::Loaders()
114 {
115         vector<string> v;
116         v.push_back("lyx");
117         v.push_back("text");
118         v.push_back("textparagraph");
119         return v;
120 }