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