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

0001 /***************************************************************************
0002  *   SPDX-License-Identifier: GPL-2.0-or-later
0003  *                                                                         *
0004  *   SPDX-FileCopyrightText: 2004-2020 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 #ifndef KBIBTEX_NETWORKING_ASSOCIATEDFILES_H
0021 #define KBIBTEX_NETWORKING_ASSOCIATEDFILES_H
0022 
0023 #include <QUrl>
0024 
0025 #include <Entry>
0026 
0027 #ifdef HAVE_KF
0028 #include "kbibtexnetworking_export.h"
0029 #endif // HAVE_KF
0030 
0031 class QWidget;
0032 
0033 class File;
0034 
0035 /**
0036  * Given a remote or local filename/URL, this class will, (1) at the user's
0037  * discretion, move or copy this file next to the bibliography's file into
0038  * the same directory, (2) rename the copied file (again, at the user's
0039  * discretion) to either match the corresponding entry's id or follow a name
0040  * provided by the user and (3) modify the entry to include a reference
0041  * (either relative or absolute path) to the newly moved/copied file or its
0042  * original filename/URL.
0043  *
0044  * @author Thomas Fischer <fischer@unix-ag.uni-kl.de>
0045  */
0046 class KBIBTEXNETWORKING_EXPORT AssociatedFiles
0047 {
0048 public:
0049     enum class PathType {
0050         Absolute, ///< Use absolute filenames/paths if possible
0051         Relative ///< Use relative filenames/paths if possible
0052     };
0053     enum class RenameOperation {
0054         KeepName, ///< Do not rename a file
0055         EntryId, ///< Rename the file following the entry's id
0056         UserDefined ///< Rename after a string provided by the user
0057     };
0058     enum class MoveCopyOperation {
0059         None, ///< Do not move or copy a file, use a reference only
0060         Copy, ///< Copy the file next to the bibiliograpy file
0061         Move /// Same as copy, but delete original
0062     };
0063 
0064     /**
0065      * Based on a given URL to an external document, compute an URL used for association
0066      * and insert it into the given entry, either as local file or as URL.
0067      *
0068      * @param documentUrl URL to a document like 'http://www.example.com/publication.pdf'
0069      * @param entry bibliography entry where the URL is to be associated with
0070      * @param bibTeXFile valid bibliography, preferably with property 'File::Url' set
0071      * @param pathType request either a relative or an absolute path
0072      * @return the computed URL string
0073      */
0074     static QString insertUrl(const QUrl &documentUrl, QSharedPointer<Entry> &entry, const File *bibTeXFile, PathType pathType);
0075 
0076     /**
0077      * Compute how the URL string to be associated to a bibliographic entry may look
0078      * like for a given document URL, a given bibliography, and whether the URL string
0079      * should be preferably relative or absolute.
0080      * @param documentUrl URL to a document like 'http://www.example.com/publication.pdf'
0081      * @param bibTeXFile valid bibliography, preferably with property 'File::Url' set
0082      * @param pathType request either a relative or an absolute path
0083      * @return the computed URL string
0084      */
0085     static QString computeAssociateString(const QUrl &documentUrl, const File *bibTeXFile, PathType pathType);
0086 
0087     /**
0088      * For a given (remote) source URL and given various information such as which
0089      * bibliographic entry and file the local copy will be associated with, determine
0090      * a destination URL where the source document may be copied to.
0091      * This function will neither modify the bibliographic entry or file, nor do the
0092      * actual copying.
0093      *
0094      * @param sourceUrl The remote location of the document
0095      * @param entryId the identifier of the bibliography entry
0096      * @param bibTeXFile the bibliographic file
0097      * @param renameOperation what type of renaming is requested
0098      * @param userDefinedFilename an optional custom basename
0099      * @return A pair of URLs: refined source URL and computed destination URL
0100      */
0101     static QPair<QUrl, QUrl> computeSourceDestinationUrls(const QUrl &sourceUrl, const QString &entryId, const File *bibTeXFile, RenameOperation renameOperation, const QString &userDefinedFilename);
0102 
0103     static QUrl copyDocument(const QUrl &document, const QString &entryId, const File *bibTeXFile, RenameOperation renameOperation, MoveCopyOperation moveCopyOperation, QWidget *widget, const QString &userDefinedFilename = QString());
0104 
0105 private:
0106     /**
0107      * Translate a given URL of a document (e.g. a PDF file) to a string
0108      * representation pointing to the relative location of this document.
0109      * A "base URL", i.e. the bibliography's file location has to be provided
0110      * in order to calculate the relative location of the document.
0111      * "Upwards relativity" (i.e. paths containing "..") is not supported for this
0112      * functions output; in this case, an absolute path will be generated as fallback.
0113      *
0114      * @param document The document's URL
0115      * @param baseUrl The base URL
0116      * @return The document URL's string representation relative to the base URL
0117      */
0118     static QString relativeFilename(const QUrl &document, const QUrl &baseUrl);
0119     /**
0120      * Translate a given URL of a document (e.g. a PDF file) to a string
0121      * representation pointing to the absolute location of this document.
0122      * A "base URL", i.e. the bibliography's file location may be provided to
0123      * resolve relative document URLs.
0124      *
0125      * @param document The document's URL
0126      * @param baseUrl The base URL
0127      * @return The document URL's string representation in absolute form
0128      */
0129     static QString absoluteFilename(const QUrl &document, const QUrl &baseUrl);
0130 };
0131 
0132 #endif // KBIBTEX_NETWORKING_ASSOCIATEDFILES_H