]> git.lyx.org Git - lyx.git/blob - src/importer.C
prepare for 1.1.6pre2
[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 "minibuffer.h"
24 #include "bufferlist.h"
25 #include "support/filetools.h"
26 #include "lyx_gui_misc.h" //WriteAlert
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 displaypath = MakeDisplayPath(filename);
39         lv->getMiniBuffer()->Set(_("Importing"), displaypath, "...");
40
41         string lyxfile = ChangeExtension(filename, ".lyx");
42
43         string loader_format;
44         vector<string> loaders = Loaders();
45         if (find(loaders.begin(), loaders.end(), format) == loaders.end()) {
46                 for (vector<string>::const_iterator it = loaders.begin();
47                      it != loaders.end(); ++it) {
48                         if (converters.IsReachable(format, *it)) {
49                                 if (!converters.Convert(0, filename, filename,
50                                                         format, *it))
51                                         return false;
52                                 loader_format = *it;
53                                 break;
54                         }
55                 }
56                 if (loader_format.empty()) {
57                         WriteAlert(_("Can not import file"),
58                                    _("No information for importing from ")
59                                    + formats.PrettyName(format));
60                         return false;
61                 }
62         } else
63                 loader_format = format;
64
65
66         if (loader_format == "lyx") {
67                 Buffer * buffer = bufferlist.loadLyXFile(lyxfile);
68                 if (buffer)
69                         lv->view()->buffer(buffer);
70         } else {
71                 lv->view()->buffer(bufferlist.newFile(lyxfile, string(), true));
72                 bool as_paragraphs = loader_format == "textparagraph";
73                 string filename2 = (loader_format == format) ? filename
74                         : ChangeExtension(filename,
75                                           formats.Extension(loader_format));
76                 InsertAsciiFile(lv->view(), filename2, as_paragraphs);
77                 lv->getLyXFunc()->Dispatch(LFUN_MARK_OFF);
78         }
79
80         // we are done
81         lv->getMiniBuffer()->Set(displaypath, _("imported."));
82         return true;
83 }
84
85 #if 0
86 bool Importer::IsImportable(string const & format)
87 {
88         vector<string> loaders = Loaders();
89         for (vector<string>::const_iterator it = loaders.begin();
90              it != loaders.end(); ++it)
91                 if (converters.IsReachable(format, *it))
92                         return true;
93         return false;
94 }
95 #endif
96
97 vector<Format const *> const Importer::GetImportableFormats()
98 {
99         vector<string> loaders = Loaders();
100         vector<Format const *> result = 
101                 converters.GetReachableTo(loaders[0], true);
102         for (vector<string>::const_iterator it = loaders.begin() + 1;
103              it != loaders.end(); ++it) {
104                 vector<Format const *> r =
105                         converters.GetReachableTo(*it, false);
106                 result.insert(result.end(), r.begin(), r.end());
107         }
108         return result;
109 }
110
111
112 vector<string> const Importer::Loaders()
113 {
114         vector<string> v;
115         v.push_back("lyx");
116         v.push_back("text");
117         v.push_back("textparagraph");
118         return v;
119 }