]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlBibtex.C
1d13cf3d6063c05ebe6fb160fc51c012ae5f475c
[lyx.git] / src / frontends / controllers / ControlBibtex.C
1 /**
2  * \file ControlBibtex.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  * \author Angus Leeming
8  * \author Herbert Voß
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "ControlBibtex.h"
16 #include "frontend_helpers.h"
17
18 #include "buffer.h"
19 #include "bufferparams.h"
20
21 #include "lyxrc.h"
22 #include "gettext.h"
23
24 #include "support/filefilterlist.h"
25 #include "support/filetools.h"
26 #include "support/lstrings.h"
27
28 using std::pair;
29 using std::string;
30 using std::vector;
31
32
33 namespace lyx {
34
35 using support::contains;
36 using support::FileFilterList;
37 using support::onlyFilename;
38 using support::prefixIs;
39 using support::split;
40
41 namespace frontend {
42
43
44 ControlBibtex::ControlBibtex(Dialog & d)
45         : ControlCommand(d, "bibtex", "bibtex")
46 {}
47
48
49 docstring const ControlBibtex::browseBib(docstring const & in_name) const
50 {
51         // FIXME UNICODE
52         pair<docstring, docstring> dir1(_("Documents|#o#O"),
53                                   lyx::from_utf8(lyxrc.document_path));
54         FileFilterList const filter(_("BibTeX Databases (*.bib)"));
55         return browseRelFile(in_name, lyx::from_utf8(kernel().bufferFilepath()),
56                              _("Select a BibTeX database to add"),
57                              filter, false, dir1);
58 }
59
60
61 docstring const ControlBibtex::browseBst(docstring const & in_name) const
62 {
63         // FIXME UNICODE
64         pair<docstring, docstring> dir1(_("Documents|#o#O"),
65                                   lyx::from_utf8(lyxrc.document_path));
66         FileFilterList const filter(_("BibTeX Styles (*.bst)"));
67         return browseRelFile(in_name, lyx::from_utf8(kernel().bufferFilepath()),
68                              _("Select a BibTeX style"), filter, false, dir1);
69 }
70
71
72 void ControlBibtex::getBibStyles(vector<string> & data) const
73 {
74         data.clear();
75
76         getTexFileList("bstFiles.lst", data);
77         // test, if we have a valid list, otherwise run rescan
78         if (data.empty()) {
79                 rescanBibStyles();
80                 getTexFileList("bstFiles.lst", data);
81         }
82         vector<string>::iterator it  = data.begin();
83         vector<string>::iterator end = data.end();
84         for (; it != end; ++it) {
85                 *it = onlyFilename(*it);
86         }
87         // sort on filename only (no path)
88         std::sort(data.begin(), data.end());
89 }
90
91
92 void ControlBibtex::getBibFiles(vector<string> & data) const
93 {
94         data.clear();
95
96         getTexFileList("bibFiles.lst", data);
97         // test, if we have a valid list, otherwise run rescan
98         if (data.empty()) {
99                 rescanBibStyles();
100                 getTexFileList("bibFiles.lst", data);
101         }
102         vector<string>::iterator it  = data.begin();
103         vector<string>::iterator end = data.end();
104         for (; it != end; ++it) {
105                 *it = onlyFilename(*it);
106         }
107         // sort on filename only (no path)
108         std::sort(data.begin(), data.end());
109 }
110
111
112 void ControlBibtex::rescanBibStyles() const
113 {
114         rescanTexStyles();
115 }
116
117
118 bool ControlBibtex::usingBibtopic() const
119 {
120         return kernel().buffer().params().use_bibtopic;
121 }
122
123
124 bool ControlBibtex::bibtotoc() const
125 {
126         return prefixIs(lyx::to_utf8(params()["options"]), "bibtotoc");
127 }
128
129
130 string const ControlBibtex::getStylefile() const
131 {
132         // the different bibtex packages have (and need) their
133         // own "plain" stylefiles
134         biblio::CiteEngine const engine =
135                 kernel().buffer().params().getEngine();
136         docstring defaultstyle;
137         switch (engine) {
138         case biblio::ENGINE_BASIC:
139                 defaultstyle = lyx::from_ascii("plain");
140                 break;
141         case biblio::ENGINE_NATBIB_AUTHORYEAR:
142                 defaultstyle = lyx::from_ascii("plainnat");
143                 break;
144         case biblio::ENGINE_NATBIB_NUMERICAL:
145                 defaultstyle = lyx::from_ascii("plainnat");
146                 break;
147         case biblio::ENGINE_JURABIB:
148                 defaultstyle = lyx::from_ascii("jurabib");
149                 break;
150         }
151
152         docstring bst = params()["options"];
153         if (bibtotoc()){
154                 // bibstyle exists?
155                 if (contains(bst, ',')) {
156                         docstring bibtotoc = lyx::from_ascii("bibtotoc");
157                         bst = split(bst, bibtotoc, ',');
158                 } else
159                         bst.erase();
160         }
161
162         // propose default style file for new insets
163         // existing insets might have (legally) no bst files
164         // (if the class already provides a style)
165         if (bst.empty() && params()["bibfiles"].empty())
166                 bst = defaultstyle;
167
168         // FIXME UNICODE
169         return lyx::to_utf8(bst);
170 }
171
172 } // namespace frontend
173 } // namespace lyx