1 | /* poppler-outline.cc: qt interface to poppler |
2 | * |
3 | * Copyright (C) 2018 Adam Reichold <adam.reichold@t-online.de> |
4 | * Copyright (C) 2019 Oliver Sander <oliver.sander@tu-dresden.de> |
5 | * Copyright (C) 2019 Albert Astals Cid <aacid@kde.org> |
6 | * Copyright (C) 2024 g10 Code GmbH, Author: Sune Stolborg Vuorela <sune@vuorela.dk> |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify |
9 | * it under the terms of the GNU General Public License as published by |
10 | * the Free Software Foundation; either version 2, or (at your option) |
11 | * any later version. |
12 | * |
13 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU General Public License for more details. |
17 | * |
18 | * You should have received a copy of the GNU General Public License |
19 | * along with this program; if not, write to the Free Software |
20 | * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. |
21 | */ |
22 | |
23 | #include <poppler-qt6.h> |
24 | #include <poppler-link.h> |
25 | |
26 | #include "poppler-private.h" |
27 | #include "poppler-outline-private.h" |
28 | |
29 | #include "Link.h" |
30 | #include "Outline.h" |
31 | |
32 | namespace Poppler { |
33 | |
34 | OutlineItem::OutlineItem() : m_data { new OutlineItemData { nullptr, nullptr } } { } |
35 | |
36 | OutlineItem::OutlineItem(OutlineItemData *data) : m_data { data } { } |
37 | |
38 | OutlineItem::~OutlineItem() |
39 | { |
40 | delete m_data; |
41 | m_data = nullptr; |
42 | } |
43 | |
44 | OutlineItem::OutlineItem(const OutlineItem &other) : m_data { new OutlineItemData { *other.m_data } } { } |
45 | |
46 | OutlineItem &OutlineItem::operator=(const OutlineItem &other) |
47 | { |
48 | if (this == &other) { |
49 | return *this; |
50 | } |
51 | |
52 | auto *data = new OutlineItemData { *other.m_data }; |
53 | qSwap(value1&: m_data, value2&: data); |
54 | delete data; |
55 | |
56 | return *this; |
57 | } |
58 | |
59 | OutlineItem::OutlineItem(OutlineItem &&other) noexcept : m_data { other.m_data } |
60 | { |
61 | other.m_data = nullptr; |
62 | } |
63 | |
64 | OutlineItem &OutlineItem::operator=(OutlineItem &&other) noexcept |
65 | { |
66 | qSwap(value1&: m_data, value2&: other.m_data); |
67 | |
68 | return *this; |
69 | } |
70 | |
71 | bool OutlineItem::isNull() const |
72 | { |
73 | return !m_data->data; |
74 | } |
75 | |
76 | QString OutlineItem::name() const |
77 | { |
78 | QString &name = m_data->name; |
79 | |
80 | if (name.isEmpty()) { |
81 | if (const ::OutlineItem *data = m_data->data) { |
82 | name = unicodeToQString(u: data->getTitle()); |
83 | } |
84 | } |
85 | |
86 | return name; |
87 | } |
88 | |
89 | bool OutlineItem::isOpen() const |
90 | { |
91 | bool isOpen = false; |
92 | |
93 | if (const ::OutlineItem *data = m_data->data) { |
94 | isOpen = data->isOpen(); |
95 | } |
96 | |
97 | return isOpen; |
98 | } |
99 | |
100 | QSharedPointer<const LinkDestination> OutlineItem::destination() const |
101 | { |
102 | QSharedPointer<const LinkDestination> &destination = m_data->destination; |
103 | |
104 | if (!destination) { |
105 | if (const ::OutlineItem *data = m_data->data) { |
106 | if (const ::LinkAction *action = data->getAction()) { |
107 | if (action->getKind() == actionGoTo) { |
108 | const auto *linkGoTo = static_cast<const LinkGoTo *>(action); |
109 | destination.reset(t: new LinkDestination(LinkDestinationData(linkGoTo->getDest(), linkGoTo->getNamedDest(), m_data->documentData, false))); |
110 | } else if (action->getKind() == actionGoToR) { |
111 | const auto *linkGoToR = static_cast<const LinkGoToR *>(action); |
112 | const bool external = linkGoToR->getFileName() != nullptr; |
113 | destination.reset(t: new LinkDestination(LinkDestinationData(linkGoToR->getDest(), linkGoToR->getNamedDest(), m_data->documentData, external))); |
114 | } |
115 | } |
116 | } |
117 | } |
118 | |
119 | return destination; |
120 | } |
121 | |
122 | QString OutlineItem::externalFileName() const |
123 | { |
124 | QString &externalFileName = m_data->externalFileName; |
125 | |
126 | if (externalFileName.isEmpty()) { |
127 | if (const ::OutlineItem *data = m_data->data) { |
128 | if (const ::LinkAction *action = data->getAction()) { |
129 | if (action->getKind() == actionGoToR) { |
130 | if (const GooString *fileName = static_cast<const LinkGoToR *>(action)->getFileName()) { |
131 | externalFileName = UnicodeParsedString(s1: fileName); |
132 | } |
133 | } |
134 | } |
135 | } |
136 | } |
137 | |
138 | return externalFileName; |
139 | } |
140 | |
141 | QString OutlineItem::uri() const |
142 | { |
143 | QString &uri = m_data->uri; |
144 | |
145 | if (uri.isEmpty()) { |
146 | if (const ::OutlineItem *data = m_data->data) { |
147 | if (const ::LinkAction *action = data->getAction()) { |
148 | if (action->getKind() == actionURI) { |
149 | uri = UnicodeParsedString(s1: static_cast<const LinkURI *>(action)->getURI()); |
150 | } |
151 | } |
152 | } |
153 | } |
154 | |
155 | return uri; |
156 | } |
157 | |
158 | bool OutlineItem::hasChildren() const |
159 | { |
160 | bool result = false; |
161 | |
162 | if (::OutlineItem *data = m_data->data) { |
163 | result = data->hasKids(); |
164 | } |
165 | |
166 | return result; |
167 | } |
168 | |
169 | QVector<OutlineItem> OutlineItem::children() const |
170 | { |
171 | QVector<OutlineItem> result; |
172 | |
173 | if (::OutlineItem *data = m_data->data) { |
174 | data->open(); |
175 | if (const std::vector<::OutlineItem *> *kids = data->getKids()) { |
176 | for (void *kid : *kids) { |
177 | result.push_back(t: OutlineItem { new OutlineItemData { static_cast<::OutlineItem *>(kid), m_data->documentData } }); |
178 | } |
179 | } |
180 | } |
181 | |
182 | return result; |
183 | } |
184 | |
185 | } |
186 | |