]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiBibtex.cpp
Use <cstdint> instead of <boost/cstdint.hpp>
[lyx.git] / src / frontends / qt4 / GuiBibtex.cpp
index 8e3e49033f38a37157bc817709baf3df4a83c5c8..8680a384d286e96c63dd92f456788b606d86a933 100644 (file)
@@ -4,9 +4,9 @@
  * Licence details can be found in the file COPYING.
  *
  * \author John Levon
- * \author Herbert Voß
+ * \author Herbert Voß
  * \author Angus Leeming
- * \author Jürgen Spitzmüller
+ * \author Jürgen Spitzmüller
  *
  * Full author contact details are available in file CREDITS.
  */
 
 #include "Buffer.h"
 #include "BufferParams.h"
-#include "debug.h"
-#include "ui_BibtexAddUi.h"
+#include "CiteEnginesList.h"
+#include "Encoding.h"
+#include "FuncRequest.h"
+#include "GuiApplication.h"
+#include "LyXRC.h"
 #include "qt_helpers.h"
 #include "Validator.h"
-#include "LyXRC.h"
-#include "gettext.h"
 
 #include "ButtonPolicy.h"
 
-#include "support/filetools.h" // changeExtension
-#include "support/lstrings.h"
-#include "support/FileFilterList.h"
+#include "frontends/alert.h"
 
-#include "frontend_helpers.h"
+#include "insets/InsetBibtex.h"
 
+#include "support/debug.h"
+#include "support/ExceptionMessage.h"
+#include "support/FileName.h"
+#include "support/filetools.h" // changeExtension
+#include "support/gettext.h"
+#include "support/lstrings.h"
 
+#include <QDialogButtonBox>
 #include <QPushButton>
 #include <QListWidget>
 #include <QCheckBox>
-#include <QCloseEvent>
 #include <QLineEdit>
 
-using std::pair;
-using std::string;
-using std::vector;
-
+using namespace std;
+using namespace lyx::support;
 
 namespace lyx {
 namespace frontend {
 
-using support::changeExtension;
-using support::contains;
-using support::FileFilterList;
-using support::onlyFilename;
-using support::prefixIs;
-using support::split;
-using support::trim;
-
 
-GuiBibtex::GuiBibtex(LyXView & lv)
-       : GuiDialog(lv, "bibtex"), ControlCommand(*this, "bibtex")
+GuiBibtex::GuiBibtex(GuiView & lv)
+       : GuiDialog(lv, "bibtex", qt_("BibTeX Bibliography")),
+         params_(insetCode("bibtex"))
 {
        setupUi(this);
 
-       setViewTitle( _("BibTeX Bibliography"));
-       setController(this, false);
-
        QDialog::setModal(true);
+       setWindowModality(Qt::WindowModal);
+
+       // The filter bar
+       filter_ = new FancyLineEdit(this);
+       filter_->setButtonPixmap(FancyLineEdit::Right, getPixmap("images/", "editclear", "svgz,png"));
+       filter_->setButtonVisible(FancyLineEdit::Right, true);
+       filter_->setButtonToolTip(FancyLineEdit::Right, qt_("Clear text"));
+       filter_->setAutoHideButton(FancyLineEdit::Right, true);
+       filter_->setPlaceholderText(qt_("All avail. databases"));
 
-       connect(okPB, SIGNAL(clicked()),
-               this, SLOT(slotOK()));
-       connect(closePB, SIGNAL(clicked()),
-               this, SLOT(slotClose()));
+       filterBarL->addWidget(filter_, 0);
+       findKeysLA->setBuddy(filter_);
+
+       connect(buttonBox, SIGNAL(clicked(QAbstractButton *)),
+               this, SLOT(slotButtonBox(QAbstractButton *)));
        connect(stylePB, SIGNAL(clicked()),
-               this, SLOT(browsePressed()));
-       connect(deletePB, SIGNAL(clicked()),
-               this, SLOT(deletePressed()));
+               this, SLOT(browseBstPressed()));
        connect(styleCB, SIGNAL(editTextChanged(QString)),
                this, SLOT(change_adaptor()));
-       connect(databaseLW, SIGNAL(itemSelectionChanged()),
-               this, SLOT(databaseChanged()));
        connect(bibtocCB, SIGNAL(clicked()),
                this, SLOT(change_adaptor()));
        connect(btPrintCO, SIGNAL(activated(int)),
                this, SLOT(change_adaptor()));
-       connect(addBibPB, SIGNAL(clicked()),
-               this, SLOT(addPressed()));
-
-       add_ = new GuiBibtexAddDialog(this);
-       add_bc_.setPolicy(ButtonPolicy::OkCancelPolicy);
-       add_bc_.setOK(add_->addPB);
-       add_bc_.setCancel(add_->closePB);
-       add_bc_.addCheckedLineEdit(add_->bibED, 0);
-
-       connect(add_->bibED, SIGNAL(textChanged(const QString &)),
-               this, SLOT(bibEDChanged()));
-       connect(add_->addPB, SIGNAL(clicked()),
-               this, SLOT(addDatabase()));
-       connect(add_->addPB, SIGNAL(clicked()),
-               add_, SLOT(accept()));
-       connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
-               this, SLOT(addDatabase()));
-       connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
-               add_, SLOT(accept()));
-       connect(add_->bibLW, SIGNAL(itemSelectionChanged()),
-               this, SLOT(availableChanged()));
-       connect(add_->browsePB, SIGNAL(clicked()),
+       connect(rescanPB, SIGNAL(clicked()),
+               this, SLOT(rescanClicked()));
+       connect(biblatexOptsLE, SIGNAL(textChanged(QString)),
+               this, SLOT(change_adaptor()));
+       connect(bibEncodingCO, SIGNAL(activated(int)),
+               this, SLOT(change_adaptor()));
+       connect(browseBibPB, SIGNAL(clicked()),
                this, SLOT(browseBibPressed()));
-       connect(add_->closePB, SIGNAL(clicked()),
-               add_, SLOT(reject()));
+
+       selected_model_.insertColumns(0, 1);
+       selectionManager = new GuiSelectionManager(this, availableLV, selectedLV,
+                       addBibPB, deletePB, upPB, downPB, &available_model_, &selected_model_);
+       connect(selectionManager, SIGNAL(selectionChanged()),
+               this, SLOT(databaseChanged()));
+       connect(selectionManager, SIGNAL(updateHook()),
+               this, SLOT(selUpdated()));
+       connect(selectionManager, SIGNAL(okHook()),
+               this, SLOT(on_buttonBox_accepted()));
+
+       connect(filter_, SIGNAL(rightButtonClicked()),
+               this, SLOT(resetFilter()));
+       connect(filter_, SIGNAL(textEdited(QString)),
+               this, SLOT(filterChanged(QString)));
+       connect(filter_, SIGNAL(returnPressed()),
+               this, SLOT(filterPressed()));
+#if (QT_VERSION < 0x050000)
+       connect(filter_, SIGNAL(downPressed()),
+               availableLV, SLOT(setFocus()));
+#else
+       connect(filter_, &FancyLineEdit::downPressed,
+               availableLV, [=](){ focusAndHighlight(availableLV); });
+#endif
+
+       availableLV->setToolTip(formatToolTip(qt_("This list consists of all databases that are indexed by LaTeX and thus are found without a file path. "
+                                   "This is usually everything in the bib/ subdirectory of LaTeX's texmf tree. "
+                                   "If you want to reuse your own database, this is the place you should store it.")));
 
        bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
-       bc().setOK(okPB);
-       bc().setCancel(closePB);
-       bc().addReadOnly(databaseLW);
+       bc().setOK(buttonBox->button(QDialogButtonBox::Ok));
+       bc().setApply(buttonBox->button(QDialogButtonBox::Apply));
+       bc().setCancel(buttonBox->button(QDialogButtonBox::Cancel));
        bc().addReadOnly(stylePB);
        bc().addReadOnly(styleCB);
        bc().addReadOnly(bibtocCB);
-       bc().addReadOnly(addBibPB);
-       bc().addReadOnly(deletePB);
+       bc().addReadOnly(bibEncodingCO);
+
+#if (QT_VERSION < 0x050000)
+       selectedLV->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
+#else
+       selectedLV->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
+#endif
+
+       // Always put the default encoding in the first position.
+       bibEncodingCO->addItem(qt_("Document Encoding"), "default");
+       for (auto const & encvar : encodings) {
+               if (!encvar.unsafe() && !encvar.guiName().empty())
+                       encodings_.insert(qt_(encvar.guiName()), toqstr(encvar.name()));
+       }
+       QMap<QString, QString>::const_iterator it = encodings_.constBegin();
+       while (it != encodings_.constEnd()) {
+               bibEncodingCO->addItem(it.key(), it.value());
+               ++it;
+       }
+
+       setFocusProxy(filter_);
 }
 
 
-void GuiBibtex::bibEDChanged()
+void GuiBibtex::init()
 {
-       // Indicate to the button controller that the contents have
-       // changed. The actual test of validity is carried out by
-       // the checkedLineEdit.
-       add_bc_.setValid(true);
+       all_bibs_ = bibFiles(false);
+       available_model_.setStringList(all_bibs_);
+
+       QString bibs = toqstr(params_["bibfiles"]);
+       if (bibs.isEmpty())
+               selected_bibs_.clear();
+       else
+               selected_bibs_ = bibs.split(",");
+       setSelectedBibs(selected_bibs_);
+
+       buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false);
+       buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
+       selectionManager->update();
 }
 
 
 void GuiBibtex::change_adaptor()
 {
+       setButtons();
        changed();
 }
 
 
-void GuiBibtex::browsePressed()
+void GuiBibtex::setButtons()
 {
-       docstring const file = browseBst(docstring());
+       int const srows = selectedLV->model()->rowCount();
+       buttonBox->button(QDialogButtonBox::Apply)->setEnabled(srows > 0);
+       buttonBox->button(QDialogButtonBox::Ok)->setEnabled(srows > 0);
+}
 
-       if (!file.empty()) {
-               // FIXME UNICODE
-               docstring const filen = from_utf8(changeExtension(to_utf8(file), ""));
-               bool present = false;
-               unsigned int pres = 0;
 
-               for (int i = 0; i != styleCB->count(); ++i) {
-                       if (qstring_to_ucs4(styleCB->itemText(i)) == filen) {
-                               present = true;
-                               pres = i;
-                       }
-               }
+void GuiBibtex::selUpdated()
+{
+       selectionManager->update();
+       editPB->setEnabled(deletePB->isEnabled());
+       changed();
+}
 
-               if (!present)
-                       styleCB->insertItem(0, toqstr(filen));
 
-               styleCB->setCurrentIndex(pres);
-               changed();
-       }
+void GuiBibtex::on_buttonBox_accepted()
+{
+       applyView();
+       clearSelection();
+       hide();
 }
 
 
-void GuiBibtex::browseBibPressed()
+void GuiBibtex::browseBstPressed()
 {
-       docstring const file = trim(browseBib(docstring()));
+       QString const file = browseBst(QString());
 
-       if (!file.empty()) {
-               // FIXME UNICODE
-               QString const f = toqstr(changeExtension(to_utf8(file), ""));
-               bool present = false;
+       if (file.isEmpty())
+               return;
 
-               for (int i = 0; i < add_->bibLW->count(); ++i) {
-                       if (add_->bibLW->item(i)->text() == f)
-                               present = true;
-               }
+       QString const filen = changeExtension(file, "");
+       bool present = false;
+       int pres = 0;
 
-               if (!present) {
-                       add_->bibLW->addItem(f);
-                       changed();
+       for (int i = 0; i != styleCB->count(); ++i) {
+               if (styleCB->itemText(i) == filen) {
+                       present = true;
+                       pres = i;
                }
-
-               add_->bibED->setText(f);
        }
-}
 
+       if (!present)
+               styleCB->insertItem(0, filen);
 
-void GuiBibtex::addPressed()
-{
-       add_bc_.setValid(false);
-       add_->exec();
+       styleCB->setCurrentIndex(pres);
+       changed();
 }
 
 
-void GuiBibtex::addDatabase()
+void GuiBibtex::browseBibPressed()
 {
-       int const sel = add_->bibLW->currentRow();
-       docstring const file = trim(qstring_to_ucs4(add_->bibED->text()));
+       QString const file = browseBib(QString()).trimmed();
 
-       if (sel < 0 && file.empty())
+       if (file.isEmpty())
                return;
 
-       // Add the selected browser_bib keys to browser_database
-       // multiple selections are possible
-       for (int i = 0; i != add_->bibLW->count(); ++i) {
-               QListWidgetItem * const item = add_->bibLW->item(i);
-               if (add_->bibLW->isItemSelected(item)) {
-                       add_->bibLW->setItemSelected(item, false);
-                       QList<QListWidgetItem *> matches =
-                               databaseLW->findItems(item->text(), Qt::MatchExactly);
-                       if (matches.empty())
-                               databaseLW->addItem(item->text());
-               }
-       }
+       QString const f = changeExtension(file, "");
 
-       if (!file.empty()) {
-               add_->bibED->clear();
-               QString const f = toqstr(from_utf8(changeExtension(to_utf8(file), "")));
-               QList<QListWidgetItem *> matches =
-                       databaseLW->findItems(f, Qt::MatchExactly);
-               if (matches.empty())
-                       databaseLW->addItem(f);
+       if (!selected_bibs_.contains(f)) {
+               selected_bibs_.append(f);
+               setSelectedBibs(selected_bibs_);
+               changed();
        }
-
-       changed();
 }
 
 
-void GuiBibtex::deletePressed()
+void GuiBibtex::on_editPB_clicked()
 {
-       databaseLW->takeItem(databaseLW->currentRow());
-       changed();
+       QModelIndexList selIdx =
+               selectedLV->selectionModel()->selectedIndexes();
+       if (selIdx.isEmpty())
+               return;
+       QModelIndex idx = selIdx.first();
+       QString sel = idx.data().toString();
+       FuncRequest fr(LFUN_INSET_EDIT, fromqstr(sel));
+       dispatch(fr);
 }
 
 
-
-void GuiBibtex::databaseChanged()
+void GuiBibtex::rescanClicked()
 {
-       deletePB->setEnabled(!isBufferReadonly() && databaseLW->currentRow() != -1);
+       rescanBibStyles();
+       updateContents();
 }
 
 
-void GuiBibtex::availableChanged()
+void GuiBibtex::clearSelection()
 {
-       add_bc_.setValid(true);
+       selected_bibs_.clear();
+       setSelectedBibs(selected_bibs_);
 }
 
 
-void GuiBibtex::closeEvent(QCloseEvent *e)
+void GuiBibtex::setSelectedBibs(QStringList const sl)
 {
-       slotClose();
-       e->accept();
+       selected_model_.clear();
+       QStringList headers;
+       headers << qt_("Database")
+               << qt_("File Encoding");
+       selected_model_.setHorizontalHeaderLabels(headers);
+       bool const moreencs = usingBiblatex() && sl.count() > 1;
+       selectedLV->setColumnHidden(1, !moreencs);
+       selectedLV->verticalHeader()->setVisible(false);
+       selectedLV->horizontalHeader()->setVisible(moreencs);
+       if (moreencs) {
+               bibEncodingLA->setText(qt_("General E&ncoding:"));
+               bibEncodingCO->setToolTip(qt_("If your bibliography databases use a different "
+                                             "encoding than the LyX document, specify it here. "
+                                             "If indivivual databases have different encodings, "
+                                             "you can set it in the list above."));
+       } else {
+               bibEncodingLA->setText(qt_("E&ncoding:"));
+               bibEncodingCO->setToolTip(qt_("If your bibliography databases use a different "
+                                             "encoding than the LyX document, specify it here"));
+       }
+       QStringList::const_iterator it  = sl.begin();
+       QStringList::const_iterator end = sl.end();
+       for (int i = 0; it != end; ++it, ++i) {
+               QStandardItem * si = new QStandardItem();
+               si->setData(*it);
+               si->setText(*it);
+               si->setToolTip(*it);
+               si->setEditable(false);
+               selected_model_.insertRow(i, si);
+               QComboBox * cb = new QComboBox;
+               cb->addItem(qt_("General Encoding"), "general");
+               cb->addItem(qt_("Document Encoding"), "auto");
+               QMap<QString, QString>::const_iterator it = encodings_.constBegin();
+               while (it != encodings_.constEnd()) {
+                       cb->addItem(it.key(), it.value());
+                       ++it;
+               }
+               cb->setToolTip(qt_("If this bibliography database uses a different "
+                                  "encoding than specified below, set it here"));
+               selectedLV->setIndexWidget(selected_model_.index(i, 1), cb);
+       }
+       editPB->setEnabled(deletePB->isEnabled());
 }
 
 
-void GuiBibtex::updateContents()
+QStringList GuiBibtex::selectedBibs()
 {
-       bool bibtopic = usingBibtopic();
+       QStringList res;
+       for (int i = 0; i != selected_model_.rowCount(); ++i) {
+               QStandardItem const * item = selected_model_.item(i);
+               if (item)
+                       res.append(item->text());
+       }
+       return res;
+}
 
-       databaseLW->clear();
 
-       docstring bibs = params()["bibfiles"];
-       docstring bib;
+void GuiBibtex::databaseChanged()
+{
+       selected_bibs_ = selectedBibs();
+       setSelectedBibs(selected_bibs_);
+}
 
-       while (!bibs.empty()) {
-               bibs = split(bibs, bib, ',');
-               bib = trim(bib);
-               if (!bib.empty())
-                       databaseLW->addItem(toqstr(bib));
-       }
 
-       add_->bibLW->clear();
+void GuiBibtex::updateContents()
+{
+       bool bibtopic = usingBibtopic();
+       bool biblatex = usingBiblatex();
 
-       vector<string> bib_str;
-       getBibFiles(bib_str);
-       for (vector<string>::const_iterator it = bib_str.begin();
-               it != bib_str.end(); ++it) {
-               string bibItem(changeExtension(*it, ""));
-               add_->bibLW->addItem(toqstr(bibItem));
-       }
+       if (biblatex)
+               setTitle(qt_("Biblatex Bibliography"));
+       else
+               setTitle(qt_("BibTeX Bibliography"));
 
-       string bibstyle = getStylefile();
+       QString const bibstyle = styleFile();
 
        bibtocCB->setChecked(bibtotoc() && !bibtopic);
        bibtocCB->setEnabled(!bibtopic);
 
-       docstring btprint(params()["btprint"]);
-       int btp = 0;
-       if (btprint == "btPrintNotCited")
-               btp = 1;
-       else if (btprint == "btPrintAll")
-               btp = 2;
-
-       btPrintCO->setCurrentIndex(btp);
-       btPrintCO->setEnabled(bibtopic);
-
-       styleCB->clear();
+       btPrintCO->clear();
+       btPrintCO->addItem(qt_("all cited references"), toqstr("btPrintCited"));
+       if (bibtopic)
+               btPrintCO->addItem(qt_("all uncited references"), toqstr("btPrintNotCited"));
+       btPrintCO->addItem(qt_("all references"), toqstr("btPrintAll"));
+       if (usingBiblatex() && !buffer().masterParams().multibib.empty())
+               btPrintCO->addItem(qt_("all reference units"), toqstr("bibbysection"));
+
+       docstring btprint = params_["btprint"];
+       if (btprint.empty())
+               // default
+               btprint = from_ascii("btPrintCited");
+       btPrintCO->setCurrentIndex(btPrintCO->findData(toqstr(btprint)));
+
+       docstring encoding = params_["encoding"];
+       if (encoding.empty())
+               // default
+               encoding = from_ascii("default");
+       bibEncodingCO->setCurrentIndex(bibEncodingCO->findData(toqstr(encoding)));
+
+       // Only useful for biblatex
+       biblatexOptsLA->setVisible(biblatex);
+       biblatexOptsLE->setVisible(biblatex);
+
+       // only useful for BibTeX
+       bstGB->setVisible(!biblatex);
+
+       if (!biblatex) {
+               styleCB->clear();
+
+               int item_nr = -1;
+
+               QStringList const str = bibStyles();
+               for (int i = 0; i != str.count(); ++i) {
+                       QString item = changeExtension(str[i], "");
+                       if (item == bibstyle)
+                               item_nr = i;
+                       styleCB->addItem(item);
+               }
 
-       int item_nr(-1);
+               if (item_nr == -1 && !bibstyle.isEmpty()) {
+                       styleCB->addItem(bibstyle);
+                       item_nr = styleCB->count() - 1;
+               }
 
-       vector<string> str;
-       getBibStyles(str);
-       for (vector<string>::const_iterator it = str.begin();
-               it != str.end(); ++it) {
-               string item(changeExtension(*it, ""));
-               if (item == bibstyle)
-                       item_nr = int(it - str.begin());
-               styleCB->addItem(toqstr(item));
-       }
 
-       if (item_nr == -1 && !bibstyle.empty()) {
-               styleCB->addItem(toqstr(bibstyle));
-               item_nr = styleCB->count() - 1;
-       }
+               if (item_nr != -1)
+                       styleCB->setCurrentIndex(item_nr);
+               else
+                       styleCB->clearEditText();
+       } else
+               biblatexOptsLE->setText(toqstr(params_["biblatexopts"]));
 
-       if (item_nr != -1)
-               styleCB->setCurrentIndex(item_nr);
-       else
-               styleCB->clearEditText();
+       setFileEncodings(getVectorFromString(params_["file_encodings"], from_ascii("\t")));
+       editPB->setEnabled(deletePB->isEnabled());
 }
 
 
 void GuiBibtex::applyView()
 {
-       docstring dbs = qstring_to_ucs4(databaseLW->item(0)->text());
-
-       unsigned int maxCount = databaseLW->count();
-       for (unsigned int i = 1; i < maxCount; i++) {
-               dbs += ',';
-               dbs += qstring_to_ucs4(databaseLW->item(i)->text());
+       docstring dbs;
+
+       int maxCount = selected_bibs_.count();
+       for (int i = 0; i < maxCount; i++) {
+               if (i != 0)
+                       dbs += ',';
+               QString item = selected_bibs_.at(i);
+               docstring bibfile = qstring_to_ucs4(item);
+               dbs += bibfile;
        }
 
-       params()["bibfiles"] = dbs;
+       params_["bibfiles"] = dbs;
 
-       docstring const bibstyle(qstring_to_ucs4(styleCB->currentText()));
-       bool const bibtotoc(bibtocCB->isChecked());
+       docstring const bibstyle = qstring_to_ucs4(styleCB->currentText());
+       bool const bibtotoc = bibtocCB->isChecked();
 
-       if (bibtotoc && (!bibstyle.empty())) {
+       if (bibtotoc && !bibstyle.empty()) {
                // both bibtotoc and style
-               params()["options"] = "bibtotoc," + bibstyle;
+               params_["options"] = "bibtotoc," + bibstyle;
        } else if (bibtotoc) {
                // bibtotoc and no style
-               params()["options"] = from_ascii("bibtotoc");
+               params_["options"] = from_ascii("bibtotoc");
        } else {
                // only style. An empty one is valid, because some
                // documentclasses have an own \bibliographystyle{}
                // command!
-               params()["options"] = bibstyle;
+               params_["options"] = bibstyle;
        }
 
-       // bibtopic allows three kinds of sections:
-       // 1. sections that include all cited references of the database(s)
-       // 2. sections that include all uncited references of the database(s)
-       // 3. sections that include all references of the database(s), cited or not
-       int btp = btPrintCO->currentIndex();
-
-       switch (btp) {
-       case 0:
-               params()["btprint"] = from_ascii("btPrintCited");
-               break;
-       case 1:
-               params()["btprint"] = from_ascii("btPrintNotCited");
-               break;
-       case 2:
-               params()["btprint"] = from_ascii("btPrintAll");
-               break;
-       }
+       params_["biblatexopts"] = qstring_to_ucs4(biblatexOptsLE->text());
 
-       if (!usingBibtopic())
-               params()["btprint"] = docstring();
+       params_["btprint"] = qstring_to_ucs4(btPrintCO->itemData(btPrintCO->currentIndex()).toString());
+
+       params_["encoding"] = qstring_to_ucs4(bibEncodingCO->itemData(bibEncodingCO->currentIndex()).toString());
+
+       if (usingBiblatex())
+               params_["file_encodings"] = getStringFromVector(getFileEncodings(), from_ascii("\t"));
 }
 
 
-bool GuiBibtex::isValid()
+QString GuiBibtex::browseBib(QString const & in_name) const
 {
-       return databaseLW->count() != 0;
+       QString const label1 = qt_("D&ocuments");
+       QString const dir1 = toqstr(lyxrc.document_path);
+       QStringList const filter(qt_("BibTeX Databases (*.bib)"));
+       return browseRelToParent(in_name, bufferFilePath(),
+               qt_("Select a BibTeX database to add"), filter, false, label1, dir1);
 }
 
 
-docstring const GuiBibtex::browseBib(docstring const & in_name) const
+QString GuiBibtex::browseBst(QString const & in_name) const
 {
-       // FIXME UNICODE
-       pair<docstring, docstring> dir1(_("Documents|#o#O"),
-                                 from_utf8(lyxrc.document_path));
-       FileFilterList const filter(_("BibTeX Databases (*.bib)"));
-       return browseRelFile(in_name, from_utf8(bufferFilepath()),
-                            _("Select a BibTeX database to add"),
-                            filter, false, dir1);
+       QString const label1 = qt_("D&ocuments");
+       QString const dir1 = toqstr(lyxrc.document_path);
+       QStringList const filter(qt_("BibTeX Styles (*.bst)"));
+       return browseRelToParent(in_name, bufferFilePath(),
+               qt_("Select a BibTeX style"), filter, false, label1, dir1);
 }
 
 
-docstring const GuiBibtex::browseBst(docstring const & in_name) const
+QStringList GuiBibtex::bibStyles() const
 {
-       // FIXME UNICODE
-       pair<docstring, docstring> dir1(_("Documents|#o#O"),
-                                 from_utf8(lyxrc.document_path));
-       FileFilterList const filter(_("BibTeX Styles (*.bst)"));
-       return browseRelFile(in_name, from_utf8(bufferFilepath()),
-                            _("Select a BibTeX style"), filter, false, dir1);
+       QStringList sdata = texFileList("bstFiles.lst");
+       // test whether we have a valid list, otherwise run rescan
+       if (sdata.isEmpty()) {
+               rescanBibStyles();
+               sdata = texFileList("bstFiles.lst");
+       }
+       for (int i = 0; i != sdata.size(); ++i)
+               sdata[i] = onlyFileName(sdata[i]);
+       // sort on filename only (no path)
+       sdata.sort();
+       return sdata;
 }
 
 
-void GuiBibtex::getBibStyles(vector<string> & data) const
+QStringList GuiBibtex::bibFiles(bool const extension) const
 {
-       data.clear();
-
-       getTexFileList("bstFiles.lst", data);
-       // test, if we have a valid list, otherwise run rescan
-       if (data.empty()) {
+       QStringList sdata = texFileList("bibFiles.lst");
+       // test whether we have a valid list, otherwise run rescan
+       if (sdata.isEmpty()) {
                rescanBibStyles();
-               getTexFileList("bstFiles.lst", data);
-       }
-       vector<string>::iterator it  = data.begin();
-       vector<string>::iterator end = data.end();
-       for (; it != end; ++it) {
-               *it = onlyFilename(*it);
+               sdata = texFileList("bibFiles.lst");
        }
+       for (int i = 0; i != sdata.size(); ++i)
+               sdata[i] = extension ? onlyFileName(sdata[i])
+                                    : changeExtension(onlyFileName(sdata[i]), "");
        // sort on filename only (no path)
-       std::sort(data.begin(), data.end());
+       sdata.sort();
+       return sdata;
 }
 
 
-void GuiBibtex::getBibFiles(vector<string> & data) const
+vector<docstring> GuiBibtex::getFileEncodings()
 {
-       data.clear();
-
-       getTexFileList("bibFiles.lst", data);
-       // test, if we have a valid list, otherwise run rescan
-       if (data.empty()) {
-               rescanBibStyles();
-               getTexFileList("bibFiles.lst", data);
+       vector<docstring> res;
+       for (int i = 0; i != selected_model_.rowCount(); ++i) {
+               QStandardItem const * key = selected_model_.item(i, 0);
+               QComboBox * cb = qobject_cast<QComboBox*>(selectedLV->indexWidget(selected_model_.index(i, 1)));
+               QString fenc = cb ? cb->itemData(cb->currentIndex()).toString() : QString();
+               if (key && !key->text().isEmpty() && !fenc.isEmpty() && fenc != "general")
+                       res.push_back(qstring_to_ucs4(key->text()) + " " + qstring_to_ucs4(fenc));
        }
-       vector<string>::iterator it  = data.begin();
-       vector<string>::iterator end = data.end();
-       for (; it != end; ++it) {
-               *it = onlyFilename(*it);
+       return res;
+}
+
+
+void GuiBibtex::setFileEncodings(vector<docstring> const m)
+{
+       for (docstring const & s: m) {
+               docstring key;
+               QString enc = toqstr(split(s, key, ' '));
+               QModelIndexList qmil =
+                               selected_model_.match(selected_model_.index(0, 0),
+                                                    Qt::DisplayRole, toqstr(key), 1,
+                                                    Qt::MatchFlags(Qt::MatchExactly | Qt::MatchWrap));
+               if (!qmil.empty()) {
+                       QComboBox * cb = qobject_cast<QComboBox*>(selectedLV->indexWidget(selected_model_.index(qmil.front().row(), 1)));
+                       cb->setCurrentIndex(cb->findData(enc));
+               }
        }
-       // sort on filename only (no path)
-       std::sort(data.begin(), data.end());
 }
 
 
 void GuiBibtex::rescanBibStyles() const
 {
-       rescanTexStyles();
+       if (usingBiblatex())
+               rescanTexStyles("bib");
+       else
+               rescanTexStyles("bst bib");
 }
 
 
+void GuiBibtex::findText(QString const & text)
+{
+       QStringList const result = bibFiles(false).filter(text);
+       available_model_.setStringList(result);
+}
+
+
+void GuiBibtex::filterChanged(const QString & text)
+{
+       if (!text.isEmpty()) {
+               findText(filter_->text());
+               return;
+       }
+       findText(filter_->text());
+       filter_->setFocus();
+}
+
+
+void GuiBibtex::filterPressed()
+{
+       findText(filter_->text());
+}
+
+
+void GuiBibtex::resetFilter()
+{
+       filter_->setText(QString());
+       findText(filter_->text());
+}
+
+
+
 bool GuiBibtex::usingBibtopic() const
 {
-       return buffer().params().use_bibtopic;
+       return buffer().params().useBibtopic();
 }
 
 
 bool GuiBibtex::bibtotoc() const
 {
-       return prefixIs(to_utf8(params()["options"]), "bibtotoc");
+       return prefixIs(to_utf8(params_["options"]), "bibtotoc");
+}
+
+
+bool GuiBibtex::usingBiblatex() const
+{
+       return buffer().masterBuffer()->params().useBiblatex();
 }
 
 
-string const GuiBibtex::getStylefile() const
+QString GuiBibtex::styleFile() const
 {
        // the different bibtex packages have (and need) their
        // own "plain" stylefiles
-       biblio::CiteEngine const engine = buffer().params().getEngine();
-       docstring defaultstyle;
-       switch (engine) {
-       case biblio::ENGINE_BASIC:
-               defaultstyle = from_ascii("plain");
-               break;
-       case biblio::ENGINE_NATBIB_AUTHORYEAR:
-               defaultstyle = from_ascii("plainnat");
-               break;
-       case biblio::ENGINE_NATBIB_NUMERICAL:
-               defaultstyle = from_ascii("plainnat");
-               break;
-       case biblio::ENGINE_JURABIB:
-               defaultstyle = from_ascii("jurabib");
-               break;
-       }
+       QString defaultstyle = toqstr(buffer().params().defaultBiblioStyle());
 
-       docstring bst = params()["options"];
+       QString bst = toqstr(params_["options"]);
        if (bibtotoc()){
                // bibstyle exists?
-               if (contains(bst, ',')) {
-                       docstring bibtotoc = from_ascii("bibtotoc");
-                       bst = split(bst, bibtotoc, ',');
-               } else
-                       bst.erase();
+               int pos = bst.indexOf(',');
+               if (pos != -1) {
+                       // FIXME: check
+                       // docstring bibtotoc = from_ascii("bibtotoc");
+                       // bst = split(bst, bibtotoc, ',');
+                       bst = bst.mid(pos + 1);
+               } else {
+                       bst.clear();
+               }
        }
 
        // propose default style file for new insets
        // existing insets might have (legally) no bst files
        // (if the class already provides a style)
-       if (bst.empty() && params()["bibfiles"].empty())
+       if (bst.isEmpty() && params_["bibfiles"].empty())
                bst = defaultstyle;
 
-       // FIXME UNICODE
-       return to_utf8(bst);
+       return bst;
+}
+
+
+bool GuiBibtex::initialiseParams(std::string const & sdata)
+{
+       InsetCommand::string2params(sdata, params_);
+       init();
+       return true;
+}
+
+
+void GuiBibtex::dispatchParams()
+{
+       std::string const lfun = InsetCommand::params2string(params_);
+       dispatch(FuncRequest(getLfun(), lfun));
 }
 
 
-Dialog * createGuiBibtex(LyXView & lv) { return new GuiBibtex(lv); }
+Dialog * createGuiBibtex(GuiView & lv) { return new GuiBibtex(lv); }
 
 
 } // namespace frontend
 } // namespace lyx
 
-#include "GuiBibtex_moc.cpp"
+#include "moc_GuiBibtex.cpp"