]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlBibtex.C
frontends/controllers/ControlBibtex.[Ch]: new members bibtotoc(), getStylefile(),
[lyx.git] / src / frontends / controllers / ControlBibtex.C
1 /**
2  * \file ControlBibtex.C
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 "biblio.h"
17
18 #include "buffer.h"
19 #include "bufferparams.h"
20
21 #include "lyxrc.h"
22 #include "helper_funcs.h"
23 #include "tex_helpers.h"
24 #include "gettext.h"
25
26 #include "support/filefilterlist.h"
27 #include "support/filetools.h"
28 #include "support/lstrings.h"
29
30 using std::pair;
31 using std::string;
32 using std::vector;
33
34
35 namespace lyx {
36
37 using support::contains;
38 using support::FileFilterList;
39 using support::OnlyFilename;
40 using support::prefixIs;
41 using support::split;
42
43 namespace frontend {
44
45
46 ControlBibtex::ControlBibtex(Dialog & d)
47         : ControlCommand(d, "bibtex")
48 {}
49
50
51 string const ControlBibtex::browseBib(string const & in_name) const
52 {
53         pair<string, string> dir1(_("Documents|#o#O"),
54                                   string(lyxrc.document_path));
55         FileFilterList const filter(_("BibTeX Databases (*.bib)"));
56         return browseRelFile(in_name, kernel().bufferFilepath(),
57                              _("Select a BibTeX database to add"),
58                              filter, false, dir1);
59 }
60
61
62 string const ControlBibtex::browseBst(string const & in_name) const
63 {
64         pair<string, string> dir1(_("Documents|#o#O"),
65                                   string(lyxrc.document_path));
66         FileFilterList const filter(_("BibTeX Styles (*.bst)"));
67         return browseRelFile(in_name, 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(params().getOptions(), "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_enum const & engine =
135                 biblio::getEngine(kernel().buffer());
136         string defaultstyle;
137         switch (engine) {
138         case biblio::ENGINE_BASIC:
139                 defaultstyle = "plain";
140                 break;
141         case biblio::ENGINE_NATBIB_AUTHORYEAR:
142                 defaultstyle = "plainnat";
143                 break;
144         case biblio::ENGINE_NATBIB_NUMERICAL:
145                 defaultstyle = "plainnat";
146                 break;
147         case biblio::ENGINE_JURABIB:
148                 defaultstyle = "jurabib";
149                 break;
150         }
151
152         string bst = params().getOptions();
153         if (bibtotoc()){
154                 // bibstyle exists?
155                 if (contains(bst,',')) {
156                         string bibtotoc = "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().getContents().empty())
166                 bst = defaultstyle;
167
168         return bst;
169 }
170
171 } // namespace frontend
172 } // namespace lyx