File indexing completed on 2024-05-12 05:09:22
0001 /*************************************************************************** 0002 Copyright (C) 2005-2009 Robby Stephenson <robby@periapsis.org> 0003 ***************************************************************************/ 0004 0005 /*************************************************************************** 0006 * * 0007 * This program is free software; you can redistribute it and/or * 0008 * modify it under the terms of the GNU General Public License as * 0009 * published by the Free Software Foundation; either version 2 of * 0010 * the License or (at your option) version 3 or any later version * 0011 * accepted by the membership of KDE e.V. (or its successor approved * 0012 * by the membership of KDE e.V.), which shall act as a proxy * 0013 * defined in Section 14 of version 3 of the license. * 0014 * * 0015 * This program is distributed in the hope that it will be useful, * 0016 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0018 * GNU General Public License for more details. * 0019 * * 0020 * You should have received a copy of the GNU General Public License * 0021 * along with this program. If not, see <http://www.gnu.org/licenses/>. * 0022 * * 0023 ***************************************************************************/ 0024 0025 #include "lyxpipe.h" 0026 #include "../collection.h" 0027 #include "../utils/bibtexhandler.h" 0028 #include "../config/tellico_config.h" 0029 #include "../tellico_debug.h" 0030 0031 #include <KLocalizedString> 0032 0033 #include <QFile> 0034 #include <QTextStream> 0035 0036 using Tellico::Cite::Lyxpipe; 0037 0038 Lyxpipe::Lyxpipe() : Action(), m_hasError(false) { 0039 } 0040 0041 bool Lyxpipe::cite(Tellico::Data::EntryList entries_) { 0042 m_hasError = false; 0043 if(entries_.isEmpty()) { 0044 return false; 0045 } 0046 0047 Data::CollPtr coll = entries_.front()->collection(); 0048 if(!coll || coll->type() != Data::Collection::Bibtex) { 0049 myDebug() << "collection must be a bibliography!"; 0050 return false; 0051 } 0052 0053 QString lyxpipe = Config::lyxpipe(); 0054 lyxpipe += QLatin1String(".in"); 0055 // myDebug() << lyxpipe; 0056 0057 m_errorString = i18n("<qt>Tellico is unable to write to the server pipe at <b>%1</b>.</qt>", lyxpipe); 0058 0059 QFile file(lyxpipe); 0060 if(!file.exists()) { 0061 m_hasError = true; 0062 return false; 0063 } 0064 0065 if(!file.open(QIODevice::WriteOnly)) { 0066 m_hasError = true; 0067 return false; 0068 } 0069 0070 QString output; 0071 QTextStream ts(&file); 0072 foreach(Data::EntryPtr entry, entries_) { 0073 QString s = BibtexHandler::bibtexKey(entry); 0074 if(s.isEmpty()) { 0075 continue; 0076 } 0077 output += s; 0078 if(!output.isEmpty()) { 0079 // pybliographer uses comma-space, and pyblink expects the space there 0080 output += QLatin1String(", "); 0081 } 0082 } 0083 if(output.isEmpty()) { 0084 myDebug() << "no available bibtex keys!"; 0085 return false; 0086 } else { 0087 output.truncate(output.length()-2); // remove last comma and space 0088 } 0089 0090 // ts << "LYXSRV:tellico:hello\n"; 0091 ts << "LYXCMD:tellico:citation-insert:"; 0092 ts << output.toLocal8Bit(); 0093 ts << "\n"; 0094 // ts << "LYXSRV:tellico:bye\n"; 0095 ts.flush(); 0096 file.close(); 0097 return true; 0098 } 0099 0100 bool Lyxpipe::hasError() const { 0101 return m_hasError; 0102 } 0103 0104 QString Lyxpipe::errorString() const { 0105 return m_hasError ? m_errorString : QString(); 0106 }