"Fossies" - the Fresh Open Source Software Archive

Member "cb2bib-2.0.1/src/c2bCoreCiter.cpp" (12 Feb 2021, 3965 Bytes) of package /linux/privat/cb2bib-2.0.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "c2bCoreCiter.cpp" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.0.0_vs_2.0.1.

    1 /***************************************************************************
    2  *   Copyright (C) 2004-2021 by Pere Constans
    3  *   constans@molspaces.com
    4  *   cb2Bib version 2.0.1. Licensed under the GNU GPL version 3.
    5  *   See the LICENSE file that comes with this distribution.
    6  *
    7  *   The LyX pipe procedure in citeToLyXPipe has been adapted from Tellico
    8  *   (Tellico (C) 2003-2005 by Robby Stephenson)
    9  ***************************************************************************/
   10 #include "c2bCoreCiter.h"
   11 
   12 #include "c2bFileDialog.h"
   13 
   14 #include <settings.h>
   15 
   16 #include <QApplication>
   17 #include <QClipboard>
   18 #include <QMessageBox>
   19 #include <QTextStream>
   20 
   21 // fifo to lyx
   22 #include <fcntl.h>
   23 
   24 #include <unistd.h>
   25 
   26 
   27 c2bCoreCiter::c2bCoreCiter(QWidget* parentw) : QObject(parentw)
   28 {
   29     _parentWP = parentw;
   30     _settingsP = settings::instance();
   31     _settingsP->setDefaultValue("c2bCoreCiter/LyXPipe",
   32                                 QDir::cleanPath(QDir::homePath() + QDir::separator() + ".lyx/lyxpipe.in"));
   33     _lyxpipe = _settingsP->value("c2bCoreCiter/LyXPipe").toString();
   34 
   35     _citeids = QRegExp("^([^<]*)<<([^\\|]*)\\|citeids\\|([^>]*)>>(.*)$");
   36 }
   37 
   38 c2bCoreCiter::~c2bCoreCiter() {}
   39 
   40 
   41 void c2bCoreCiter::cite(const QStringList& keys)
   42 {
   43     if (keys.count() == 0)
   44         return;
   45 
   46     _command = _settingsP->value("cb2Bib/CiteCommandPattern").toString();
   47     const QByteArray pipe(QFile::encodeName(_lyxpipe));
   48     if (QFile::exists(pipe) && _command == C2B_CITE_COMMAND_PATTERN)
   49         _cite_to_lyx_pipe(keys);
   50     else
   51         _cite_to_clipboard(keys);
   52 }
   53 
   54 void c2bCoreCiter::_cite_to_clipboard(const QStringList& keys) const
   55 {
   56     QString c;
   57     if (_command.isEmpty() || _command == C2B_CITE_COMMAND_PATTERN)
   58         c = QLatin1String("\\cite{") + keys.join(QLatin1String(", ")).trimmed() + QLatin1Char('}');
   59     else if (_command.contains(QLatin1String("<<citeid>>")))
   60         for (int i = 0; i < keys.count(); ++i)
   61             c += QString(_command).replace(QLatin1String("<<citeid>>"), keys.at(i)) + QLatin1Char(' ');
   62     else if (_citeids.indexIn(_command) >= 0)
   63     {
   64         c = _citeids.cap(2) + keys.at(0); // Note keys.count() > 0
   65         for (int i = 1; i < keys.count(); ++i)
   66             c += _citeids.cap(3) + QLatin1Char(' ') + _citeids.cap(2) + keys.at(i);
   67         c = _citeids.cap(1) + c.trimmed() + _citeids.cap(4);
   68     }
   69 
   70     if (c.isEmpty())
   71     {
   72         QMessageBox::warning(_parentWP, tr("Warning - cb2Bib"), tr("Cite Command Pattern is misspecified"),
   73                              QMessageBox::Ok);
   74         return;
   75     }
   76 
   77     QClipboard* cb(QApplication::clipboard());
   78     cb->setText(c.trimmed(), QClipboard::Clipboard);
   79 }
   80 
   81 void c2bCoreCiter::_cite_to_lyx_pipe(const QStringList& keys) const
   82 {
   83     // This procedure was adapted from Tellico
   84     // Tellico (C) 2003-2005 by Robby Stephenson
   85     const QByteArray pipe(QFile::encodeName(_lyxpipe));
   86     const QString errorStr(tr("Unable to write to the server pipe at '%1'.").arg(QString(pipe)));
   87 
   88     if (!QFile::exists(pipe))
   89     {
   90         QMessageBox::warning(_parentWP, tr("Warning - cb2Bib"), errorStr, QMessageBox::Ok);
   91         return;
   92     }
   93 
   94     int pipeFd = ::open(pipe, O_WRONLY);
   95     QFile file(QString::fromUtf8(pipe));
   96     if (file.open(pipeFd, QIODevice::WriteOnly))
   97     {
   98         // pybliographer uses comma-space, and pyblink expects the space there
   99         const QString c(keys.join(", ").trimmed());
  100         QTextStream st(&file);
  101         st << QString::fromLatin1("LYXCMD:cb2bib:citation-insert:%1\n").arg(c).toLatin1();
  102         file.flush();
  103         file.close();
  104     }
  105     else
  106         QMessageBox::warning(_parentWP, tr("Warning - cb2Bib"), errorStr, QMessageBox::Ok);
  107     ::close(pipeFd);
  108 }
  109 
  110 void c2bCoreCiter::setLyXPipe()
  111 {
  112     const QString new_pn(c2bFileDialog::getSystemFilename(_parentWP, "Select LyX pipe", _lyxpipe, "All (*)"));
  113     if (!new_pn.isEmpty())
  114     {
  115         _lyxpipe = new_pn;
  116         _settingsP->setValue("c2bCoreCiter/LyXPipe", _lyxpipe);
  117     }
  118 }