]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiBibtex.cpp
Add support for bib files encoding
[lyx.git] / src / frontends / qt4 / GuiBibtex.cpp
1 /**
2  * \file GuiBibtex.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 Herbert Voß
8  * \author Angus Leeming
9  * \author Jürgen Spitzmüller
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "GuiBibtex.h"
17
18 #include "Buffer.h"
19 #include "BufferParams.h"
20 #include "CiteEnginesList.h"
21 #include "Encoding.h"
22 #include "FuncRequest.h"
23 #include "LyXRC.h"
24 #include "qt_helpers.h"
25 #include "Validator.h"
26
27 #include "ui_BibtexAddUi.h"
28
29 #include "ButtonPolicy.h"
30
31 #include "frontends/alert.h"
32
33 #include "insets/InsetBibtex.h"
34
35 #include "support/debug.h"
36 #include "support/ExceptionMessage.h"
37 #include "support/FileName.h"
38 #include "support/filetools.h" // changeExtension
39 #include "support/gettext.h"
40 #include "support/lstrings.h"
41
42 #include <QPushButton>
43 #include <QListWidget>
44 #include <QCheckBox>
45 #include <QLineEdit>
46
47 using namespace std;
48 using namespace lyx::support;
49
50 namespace lyx {
51 namespace frontend {
52
53
54 GuiBibtex::GuiBibtex(GuiView & lv)
55         : GuiDialog(lv, "bibtex", qt_("BibTeX Bibliography")),
56           params_(insetCode("bibtex"))
57 {
58         setupUi(this);
59
60         QDialog::setModal(true);
61         setWindowModality(Qt::WindowModal);
62
63         connect(okPB, SIGNAL(clicked()),
64                 this, SLOT(slotOK()));
65         connect(closePB, SIGNAL(clicked()),
66                 this, SLOT(slotClose()));
67         connect(stylePB, SIGNAL(clicked()),
68                 this, SLOT(browsePressed()));
69         connect(deletePB, SIGNAL(clicked()),
70                 this, SLOT(deletePressed()));
71         connect(upPB, SIGNAL(clicked()),
72                 this, SLOT(upPressed()));
73         connect(downPB, SIGNAL(clicked()),
74                 this, SLOT(downPressed()));
75         connect(styleCB, SIGNAL(editTextChanged(QString)),
76                 this, SLOT(change_adaptor()));
77         connect(databaseLW, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
78                 this, SLOT(databaseChanged()));
79         connect(bibtocCB, SIGNAL(clicked()),
80                 this, SLOT(change_adaptor()));
81         connect(btPrintCO, SIGNAL(activated(int)),
82                 this, SLOT(change_adaptor()));
83         connect(addBibPB, SIGNAL(clicked()),
84                 this, SLOT(addPressed()));
85         connect(rescanPB, SIGNAL(clicked()),
86                 this, SLOT(rescanClicked()));
87         connect(biblatexOptsLE, SIGNAL(textChanged(QString)),
88                 this, SLOT(change_adaptor()));
89         connect(bibEncodingCO, SIGNAL(activated(int)),
90                 this, SLOT(change_adaptor()));
91
92         add_ = new GuiBibtexAddDialog(this);
93         add_bc_.setPolicy(ButtonPolicy::OkCancelPolicy);
94         add_bc_.setOK(add_->addPB);
95         add_bc_.setCancel(add_->closePB);
96         add_bc_.addCheckedLineEdit(add_->bibED, 0);
97
98         connect(add_->bibED, SIGNAL(textChanged(QString)),
99                 this, SLOT(bibEDChanged()));
100         connect(add_->addPB, SIGNAL(clicked()),
101                 this, SLOT(addDatabase()));
102         connect(add_->addPB, SIGNAL(clicked()),
103                 add_, SLOT(accept()));
104         connect(add_->rescanPB, SIGNAL(clicked()),
105                 this, SLOT(rescanClicked()));
106         connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
107                 this, SLOT(addDatabase()));
108         connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
109                 add_, SLOT(accept()));
110         connect(add_->bibLW, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
111                 this, SLOT(availableChanged()));
112         connect(add_->browsePB, SIGNAL(clicked()),
113                 this, SLOT(browseBibPressed()));
114         connect(add_->closePB, SIGNAL(clicked()),
115                 add_, SLOT(reject()));
116
117         add_->bibLW->setToolTip(formatToolTip(qt_("This list consists of all databases that are indexed by LaTeX and thus are found without a file path. "
118                                     "This is usually everything in the bib/ subdirectory of LaTeX's texmf tree. "
119                                     "If you want to reuse your own database, this is the place you should store it.")));
120
121         bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
122         bc().setOK(okPB);
123         bc().setCancel(closePB);
124         bc().addReadOnly(databaseLW);
125         bc().addReadOnly(stylePB);
126         bc().addReadOnly(styleCB);
127         bc().addReadOnly(bibtocCB);
128         bc().addReadOnly(addBibPB);
129         bc().addReadOnly(bibEncodingCO);
130         // Delete/Up/Down are handled with more conditions in
131         // databaseChanged().
132
133         // Always put the default encoding in the first position.
134         bibEncodingCO->addItem(qt_("Document Encoding"), "default");
135         QMap<QString, QString> encodinglist;
136         for (auto const & encvar : encodings) {
137                 if (!encvar.unsafe() && !encvar.guiName().empty())
138                         encodinglist.insert(qt_(encvar.guiName()), toqstr(encvar.name()));
139         }
140         QMap<QString, QString>::const_iterator it = encodinglist.constBegin();
141         while (it != encodinglist.constEnd()) {
142                 bibEncodingCO->addItem(it.key(), it.value());
143                 ++it;
144         }
145
146         // Make sure the delete/up/down buttons are disabled if necessary.
147         databaseChanged();
148 }
149
150
151 void GuiBibtex::bibEDChanged()
152 {
153         // Indicate to the button controller that the contents have
154         // changed. The actual test of validity is carried out by
155         // the checkedLineEdit.
156         add_bc_.setValid(true);
157 }
158
159
160 void GuiBibtex::change_adaptor()
161 {
162         changed();
163 }
164
165
166 void GuiBibtex::browsePressed()
167 {
168         QString const file = browseBst(QString());
169
170         if (file.isEmpty())
171                 return;
172
173         QString const filen = changeExtension(file, "");
174         bool present = false;
175         unsigned int pres = 0;
176
177         for (int i = 0; i != styleCB->count(); ++i) {
178                 if (styleCB->itemText(i) == filen) {
179                         present = true;
180                         pres = i;
181                 }
182         }
183
184         if (!present)
185                 styleCB->insertItem(0, filen);
186
187         styleCB->setCurrentIndex(pres);
188         changed();
189 }
190
191
192 void GuiBibtex::browseBibPressed()
193 {
194         QString const file = browseBib(QString()).trimmed();
195
196         if (file.isEmpty())
197                 return;
198
199         QString const f = changeExtension(file, "");
200         bool present = false;
201
202         for (int i = 0; i < add_->bibLW->count(); ++i) {
203                 if (add_->bibLW->item(i)->text() == f)
204                         present = true;
205         }
206
207         if (!present) {
208                 add_->bibLW->addItem(f);
209                 changed();
210         }
211
212         add_->bibED->setText(f);
213 }
214
215
216 void GuiBibtex::addPressed()
217 {
218         add_bc_.setValid(false);
219         add_->exec();
220 }
221
222
223 void GuiBibtex::addDatabase()
224 {
225         int const sel = add_->bibLW->currentRow();
226         QString const file = add_->bibED->text().trimmed();
227
228         if (sel < 0 && file.isEmpty())
229                 return;
230
231         // Add the selected browser_bib keys to browser_database
232         // multiple selections are possible
233         for (int i = 0; i != add_->bibLW->count(); ++i) {
234                 QListWidgetItem * const item = add_->bibLW->item(i);
235                 if (add_->bibLW->isItemSelected(item)) {
236                         add_->bibLW->setItemSelected(item, false);
237                         QList<QListWidgetItem *> matches =
238                                 databaseLW->findItems(item->text(), Qt::MatchExactly);
239                         if (matches.empty()) {
240                                 QString label = item->text();
241                                 QListWidgetItem * db = new QListWidgetItem(label);
242                                 db->setFlags(db->flags() | Qt::ItemIsSelectable);
243                                 databaseLW->addItem(db);
244                         }
245                 }
246         }
247
248         if (!file.isEmpty()) {
249                 add_->bibED->clear();
250                 QString const f = changeExtension(file, "");
251                 QList<QListWidgetItem *> matches =
252                         databaseLW->findItems(f, Qt::MatchExactly);
253                 if (matches.empty()) {
254                         QListWidgetItem * db = new QListWidgetItem(f);
255                         db->setFlags(db->flags() | Qt::ItemIsSelectable);
256                         databaseLW->addItem(db);
257                 }
258         }
259
260         databaseChanged();
261         changed();
262 }
263
264
265 void GuiBibtex::deletePressed()
266 {
267         QListWidgetItem *cur = databaseLW->takeItem(databaseLW->currentRow());
268         if (cur) {
269                 delete cur;
270                 databaseChanged();
271                 changed();
272         }
273 }
274
275
276 void GuiBibtex::upPressed()
277 {
278         int row = databaseLW->currentRow();
279         QListWidgetItem *cur = databaseLW->takeItem(row);
280         databaseLW->insertItem(row - 1, cur);
281         databaseLW->setCurrentItem(cur);
282         changed();
283 }
284
285
286 void GuiBibtex::downPressed()
287 {
288         int row = databaseLW->currentRow();
289         QListWidgetItem *cur = databaseLW->takeItem(row);
290         databaseLW->insertItem(row + 1, cur);
291         databaseLW->setCurrentItem(cur);
292         changed();
293 }
294
295
296 void GuiBibtex::rescanClicked()
297 {
298         rescanBibStyles();
299         updateContents();
300 }
301
302
303 void GuiBibtex::databaseChanged()
304 {
305         bool readOnly = isBufferReadonly();
306         int count = databaseLW->count();
307         int row = databaseLW->currentRow();
308         deletePB->setEnabled(!readOnly && row != -1);
309         upPB->setEnabled(!readOnly && count > 1 && row > 0);
310         downPB->setEnabled(!readOnly && count > 1 && row < count - 1);
311 }
312
313
314 void GuiBibtex::availableChanged()
315 {
316         add_bc_.setValid(true);
317 }
318
319
320 void GuiBibtex::updateContents()
321 {
322         bool bibtopic = usingBibtopic();
323         bool biblatex = usingBiblatex();
324
325         if (biblatex)
326                 setTitle(qt_("Biblatex Bibliography"));
327         else
328                 setTitle(qt_("BibTeX Bibliography"));
329
330         databaseLW->clear();
331
332         docstring bibs = params_["bibfiles"];
333         docstring bib;
334
335         while (!bibs.empty()) {
336                 bibs = split(bibs, bib, ',');
337                 bib = trim(bib);
338                 if (!bib.empty()) {
339                         QListWidgetItem * db = new QListWidgetItem(toqstr(bib));
340                         db->setFlags(db->flags() | Qt::ItemIsSelectable);
341                         databaseLW->addItem(db);
342                 }
343         }
344
345         add_->bibLW->clear();
346
347         QStringList bibfiles = bibFiles();
348         for (int i = 0; i != bibfiles.count(); ++i)
349                 add_->bibLW->addItem(changeExtension(bibfiles[i], ""));
350
351         QString const bibstyle = styleFile();
352
353         bibtocCB->setChecked(bibtotoc() && !bibtopic);
354         bibtocCB->setEnabled(!bibtopic);
355
356         btPrintCO->clear();
357         btPrintCO->addItem(qt_("all cited references"), toqstr("btPrintCited"));
358         if (bibtopic)
359                 btPrintCO->addItem(qt_("all uncited references"), toqstr("btPrintNotCited"));
360         btPrintCO->addItem(qt_("all references"), toqstr("btPrintAll"));
361         if (usingBiblatex() && !buffer().masterParams().multibib.empty())
362                 btPrintCO->addItem(qt_("all reference units"), toqstr("bibbysection"));
363
364         docstring btprint = params_["btprint"];
365         if (btprint.empty())
366                 // default
367                 btprint = from_ascii("btPrintCited");
368         btPrintCO->setCurrentIndex(btPrintCO->findData(toqstr(btprint)));
369
370         docstring encoding = params_["encoding"];
371         if (encoding.empty())
372                 // default
373                 encoding = from_ascii("default");
374         bibEncodingCO->setCurrentIndex(bibEncodingCO->findData(toqstr(encoding)));
375
376         // Only useful for biblatex
377         biblatexOptsLA->setVisible(biblatex);
378         biblatexOptsLE->setVisible(biblatex);
379
380         // only useful for BibTeX
381         styleCB->setVisible(!biblatex);
382         styleLA->setVisible(!biblatex);
383         stylePB->setVisible(!biblatex);
384
385         if (!biblatex) {
386                 styleCB->clear();
387
388                 int item_nr = -1;
389
390                 QStringList const str = bibStyles();
391                 for (int i = 0; i != str.count(); ++i) {
392                         QString item = changeExtension(str[i], "");
393                         if (item == bibstyle)
394                                 item_nr = i;
395                         styleCB->addItem(item);
396                 }
397
398                 if (item_nr == -1 && !bibstyle.isEmpty()) {
399                         styleCB->addItem(bibstyle);
400                         item_nr = styleCB->count() - 1;
401                 }
402
403
404                 if (item_nr != -1)
405                         styleCB->setCurrentIndex(item_nr);
406                 else
407                         styleCB->clearEditText();
408         } else
409                 biblatexOptsLE->setText(toqstr(params_["biblatexopts"]));
410 }
411
412
413 void GuiBibtex::applyView()
414 {
415         docstring dbs;
416
417         unsigned int maxCount = databaseLW->count();
418         for (unsigned int i = 0; i < maxCount; i++) {
419                 if (i != 0)
420                         dbs += ',';
421                 QString item = databaseLW->item(i)->text();
422                 docstring bibfile = qstring_to_ucs4(item);
423                 dbs += bibfile;
424         }
425
426         params_["bibfiles"] = dbs;
427
428         docstring const bibstyle = qstring_to_ucs4(styleCB->currentText());
429         bool const bibtotoc = bibtocCB->isChecked();
430
431         if (bibtotoc && !bibstyle.empty()) {
432                 // both bibtotoc and style
433                 params_["options"] = "bibtotoc," + bibstyle;
434         } else if (bibtotoc) {
435                 // bibtotoc and no style
436                 params_["options"] = from_ascii("bibtotoc");
437         } else {
438                 // only style. An empty one is valid, because some
439                 // documentclasses have an own \bibliographystyle{}
440                 // command!
441                 params_["options"] = bibstyle;
442         }
443
444         params_["biblatexopts"] = qstring_to_ucs4(biblatexOptsLE->text());
445
446         params_["btprint"] = qstring_to_ucs4(btPrintCO->itemData(btPrintCO->currentIndex()).toString());
447
448         params_["encoding"] = qstring_to_ucs4(bibEncodingCO->itemData(bibEncodingCO->currentIndex()).toString());
449 }
450
451
452 bool GuiBibtex::isValid()
453 {
454         return databaseLW->count() != 0;
455 }
456
457
458 QString GuiBibtex::browseBib(QString const & in_name) const
459 {
460         QString const label1 = qt_("D&ocuments");
461         QString const dir1 = toqstr(lyxrc.document_path);
462         QStringList const filter(qt_("BibTeX Databases (*.bib)"));
463         return browseRelToParent(in_name, bufferFilePath(),
464                 qt_("Select a BibTeX database to add"), filter, false, label1, dir1);
465 }
466
467
468 QString GuiBibtex::browseBst(QString const & in_name) const
469 {
470         QString const label1 = qt_("D&ocuments");
471         QString const dir1 = toqstr(lyxrc.document_path);
472         QStringList const filter(qt_("BibTeX Styles (*.bst)"));
473         return browseRelToParent(in_name, bufferFilePath(),
474                 qt_("Select a BibTeX style"), filter, false, label1, dir1);
475 }
476
477
478 QStringList GuiBibtex::bibStyles() const
479 {
480         QStringList sdata = texFileList("bstFiles.lst");
481         // test whether we have a valid list, otherwise run rescan
482         if (sdata.isEmpty()) {
483                 rescanBibStyles();
484                 sdata = texFileList("bstFiles.lst");
485         }
486         for (int i = 0; i != sdata.size(); ++i)
487                 sdata[i] = onlyFileName(sdata[i]);
488         // sort on filename only (no path)
489         sdata.sort();
490         return sdata;
491 }
492
493
494 QStringList GuiBibtex::bibFiles() const
495 {
496         QStringList sdata = texFileList("bibFiles.lst");
497         // test whether we have a valid list, otherwise run rescan
498         if (sdata.isEmpty()) {
499                 rescanBibStyles();
500                 sdata = texFileList("bibFiles.lst");
501         }
502         for (int i = 0; i != sdata.size(); ++i)
503                 sdata[i] = onlyFileName(sdata[i]);
504         // sort on filename only (no path)
505         sdata.sort();
506         return sdata;
507 }
508
509
510 void GuiBibtex::rescanBibStyles() const
511 {
512         if (usingBiblatex())
513                 rescanTexStyles("bib");
514         else
515                 rescanTexStyles("bst bib");
516 }
517
518
519 bool GuiBibtex::usingBibtopic() const
520 {
521         return buffer().params().useBibtopic();
522 }
523
524
525 bool GuiBibtex::bibtotoc() const
526 {
527         return prefixIs(to_utf8(params_["options"]), "bibtotoc");
528 }
529
530
531 bool GuiBibtex::usingBiblatex() const
532 {
533         return buffer().masterBuffer()->params().useBiblatex();
534 }
535
536
537 QString GuiBibtex::styleFile() const
538 {
539         // the different bibtex packages have (and need) their
540         // own "plain" stylefiles
541         QString defaultstyle = toqstr(buffer().params().defaultBiblioStyle());
542
543         QString bst = toqstr(params_["options"]);
544         if (bibtotoc()){
545                 // bibstyle exists?
546                 int pos = bst.indexOf(',');
547                 if (pos != -1) {
548                         // FIXME: check
549                         // docstring bibtotoc = from_ascii("bibtotoc");
550                         // bst = split(bst, bibtotoc, ',');
551                         bst = bst.mid(pos + 1);
552                 } else {
553                         bst.clear();
554                 }
555         }
556
557         // propose default style file for new insets
558         // existing insets might have (legally) no bst files
559         // (if the class already provides a style)
560         if (bst.isEmpty() && params_["bibfiles"].empty())
561                 bst = defaultstyle;
562
563         return bst;
564 }
565
566
567 bool GuiBibtex::initialiseParams(std::string const & sdata)
568 {
569         InsetCommand::string2params(sdata, params_);
570         return true;
571 }
572
573
574 void GuiBibtex::dispatchParams()
575 {
576         std::string const lfun = InsetCommand::params2string(params_);
577         dispatch(FuncRequest(getLfun(), lfun));
578 }
579
580
581
582 Dialog * createGuiBibtex(GuiView & lv) { return new GuiBibtex(lv); }
583
584
585 } // namespace frontend
586 } // namespace lyx
587
588 #include "moc_GuiBibtex.cpp"