File indexing completed on 2024-05-19 05:05:33

0001 /***************************************************************************
0002  *   SPDX-License-Identifier: GPL-2.0-or-later
0003  *                                                                         *
0004  *   SPDX-FileCopyrightText: 2004-2023 Thomas Fischer <fischer@unix-ag.uni-kl.de>
0005  *                                                                         *
0006  *   This program is free software; you can redistribute it and/or modify  *
0007  *   it under the terms of the GNU General Public License as published by  *
0008  *   the Free Software Foundation; either version 2 of the License, or     *
0009  *   (at your option) any later version.                                   *
0010  *                                                                         *
0011  *   This program is distributed in the hope that it will be useful,       *
0012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0014  *   GNU General Public License for more details.                          *
0015  *                                                                         *
0016  *   You should have received a copy of the GNU General Public License     *
0017  *   along with this program; if not, see <https://www.gnu.org/licenses/>. *
0018  ***************************************************************************/
0019 
0020 #include "fileexporterbibutils.h"
0021 
0022 #include <QBuffer>
0023 
0024 #include "fileexporterbibtex.h"
0025 #include "fileexporter_p.h"
0026 #include "logging_io.h"
0027 
0028 class FileExporterBibUtils::Private
0029 {
0030 private:
0031     // UNUSED FileExporterBibUtils *p;
0032 
0033 public:
0034     FileExporterBibTeX *bibtexExporter;
0035 
0036     Private(FileExporterBibUtils *parent)
0037     // UNUSED : p(parent)
0038     {
0039         bibtexExporter = new FileExporterBibTeX(parent);
0040         bibtexExporter->setEncoding(QStringLiteral("utf-8"));
0041     }
0042 
0043     ~Private() {
0044         delete bibtexExporter;
0045     }
0046 };
0047 
0048 FileExporterBibUtils::FileExporterBibUtils(QObject *parent)
0049         : FileExporter(parent), BibUtils(), d(new FileExporterBibUtils::Private(this))
0050 {
0051     // TODO
0052 }
0053 
0054 FileExporterBibUtils::~FileExporterBibUtils()
0055 {
0056     delete d;
0057 }
0058 
0059 bool FileExporterBibUtils::save(QIODevice *iodevice, const File *bibtexfile)
0060 {
0061     check_if_bibtexfile_or_iodevice_invalid(bibtexfile, iodevice);
0062 
0063     QBuffer buffer;
0064     bool result = buffer.open(QIODevice::WriteOnly);
0065     if (result) {
0066         result = d->bibtexExporter->save(&buffer, bibtexfile);
0067         buffer.close();
0068     }
0069     if (result) {
0070         if (buffer.open(QIODevice::ReadOnly)) {
0071             result = convert(buffer, BibUtils::Format::BibTeX, *iodevice, format());
0072             buffer.close();
0073         } else
0074             result = false;
0075     }
0076 
0077     return result;
0078 }
0079 
0080 bool FileExporterBibUtils::save(QIODevice *iodevice, const QSharedPointer<const Element> &element, const File *bibtexfile)
0081 {
0082     check_if_iodevice_invalid(iodevice);
0083 
0084     QBuffer buffer;
0085     bool result = buffer.open(QIODevice::WriteOnly);
0086     if (result) {
0087         result = d->bibtexExporter->save(&buffer, element, bibtexfile);
0088         buffer.close();
0089     }
0090     if (result) {
0091         if (buffer.open(QIODevice::ReadOnly)) {
0092             result = convert(buffer, BibUtils::Format::BibTeX, *iodevice, format());
0093         } else
0094             result = false;
0095     }
0096 
0097     return result;
0098 }