1 // Copyright (c) 2005-2021 Jay Berkenbilt
2 //
3 // This file is part of qpdf.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //   http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 // Versions of qpdf prior to version 7 were released under the terms
18 // of version 2.0 of the Artistic License. At your option, you may
19 // continue to consider qpdf to be licensed under those terms. Please
20 // see the manual for additional information.
21 
22 #ifndef QPDFEMBEDDEDFILEDOCUMENTHELPER_HH
23 #define QPDFEMBEDDEDFILEDOCUMENTHELPER_HH
24 
25 #include <qpdf/QPDFDocumentHelper.hh>
26 
27 #include <qpdf/QPDF.hh>
28 #include <qpdf/QPDFNameTreeObjectHelper.hh>
29 #include <qpdf/QPDFFileSpecObjectHelper.hh>
30 #include <qpdf/DLL.h>
31 
32 #include <memory>
33 #include <map>
34 
35 // This class provides a higher level interface around document-level
36 // file attachments, also known as embedded files. These are discussed
37 // in sections 7.7.4 and 7.11 of the ISO-32000 PDF specification.
38 
39 class QPDFEmbeddedFileDocumentHelper: public QPDFDocumentHelper
40 {
41   public:
42     QPDF_DLL
43     QPDFEmbeddedFileDocumentHelper(QPDF&);
44     QPDF_DLL
45     virtual ~QPDFEmbeddedFileDocumentHelper() = default;
46 
47     QPDF_DLL
48     bool hasEmbeddedFiles() const;
49 
50     QPDF_DLL
51     std::map<std::string,
52              std::shared_ptr<QPDFFileSpecObjectHelper>> getEmbeddedFiles();
53 
54     // If an embedded file with the given name exists, return a
55     // (shared) pointer to it. Otherwise, return nullptr.
56     QPDF_DLL
57     std::shared_ptr<QPDFFileSpecObjectHelper>
58     getEmbeddedFile(std::string const& name);
59 
60     // Add or replace an attachment
61     QPDF_DLL
62     void replaceEmbeddedFile(
63         std::string const& name, QPDFFileSpecObjectHelper const&);
64 
65     // Remove an embedded file if present. Return value is true if the
66     // file was present and was removed. This method not only removes
67     // the embedded file from the embedded files name tree but also
68     // nulls out the file specification dictionary. This means that
69     // any references to this file from file attachment annotations
70     // will also stop working. This is the best way to make the
71     // attachment actually disappear from the file and not just from
72     // the list of attachments.
73     QPDF_DLL
74     bool removeEmbeddedFile(std::string const& name);
75 
76   private:
77     void initEmbeddedFiles();
78 
79     class Members
80     {
81         friend class QPDFEmbeddedFileDocumentHelper;
82 
83       public:
84         QPDF_DLL
85         ~Members() = default;
86 
87       private:
88         Members();
89         Members(Members const&) = delete;
90 
91         std::shared_ptr<QPDFNameTreeObjectHelper> embedded_files;
92     };
93 
94     PointerHolder<Members> m;
95 };
96 
97 #endif // QPDFEMBEDDEDFILEDOCUMENTHELPER_HH
98