Skip to content

Commit 285c5df

Browse files
zim::Archive::getIllustrations()
1 parent 36592f8 commit 285c5df

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

include/zim/archive.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include "zim.h"
2626
#include "entry.h"
27+
#include "illustration.h"
2728
#include "uuid.h"
2829

2930
#include <string>
@@ -101,6 +102,7 @@ namespace zim
101102
public:
102103
template<EntryOrder order> class EntryRange;
103104
template<EntryOrder order> class iterator;
105+
typedef std::vector<IllustrationInfo> Illustrations;
104106

105107
/** Archive constructor.
106108
*
@@ -359,6 +361,13 @@ namespace zim
359361
std::set<unsigned int> getIllustrationSizes() const;
360362

361363

364+
/** Return the list of available illustations in the archive.
365+
*
366+
* @return A vector of IllustrationInfo data.
367+
*/
368+
Illustrations getIllustrations() const;
369+
370+
362371
/** Get an entry using its "path" index.
363372
*
364373
* Use the index of the entry to get the idx'th entry

src/archive.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,32 @@ namespace zim
233233
return ret;
234234
}
235235

236+
Archive::Illustrations Archive::getIllustrations() const {
237+
Illustrations r;
238+
for(auto e = m_impl->findx('M', "Illustration_").second; ; ++e ) {
239+
try {
240+
const auto path = getEntryByPath(entry_index_type(e)).getPath();
241+
if (path.find("Illustration_") != 0) {
242+
break;
243+
}
244+
try {
245+
r.push_back(zim::IllustrationInfo::fromMetadataItemName(path));
246+
} catch (...) {}
247+
} catch (const std::out_of_range& e) {
248+
break;
249+
}
250+
}
251+
const IllustrationInfo faviconLikeIllustration{48, 48, 1.0, {}};
252+
if (std::find(r.begin(), r.end(), faviconLikeIllustration) == r.end()) {
253+
try {
254+
// raise a exception if we cannot find the (old format) favicon.
255+
findFavicon(*m_impl);
256+
r.push_back(faviconLikeIllustration);
257+
} catch(EntryNotFound&) {}
258+
}
259+
return r;
260+
}
261+
236262
bool Archive::hasIllustration(unsigned int size) const {
237263
try {
238264
getIllustrationItem(size);

test/archive.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,10 @@ TEST_F(ZimArchive, openCreatedArchive)
200200
ASSERT_EQ(archive.getUuid(), uuid);
201201
ASSERT_EQ(archive.getMetadataKeys(), std::vector<std::string>({"Counter", "Illustration_48x48@1", "Illustration_96x96@1", "Title"}));
202202
ASSERT_EQ(archive.getIllustrationSizes(), std::set<unsigned int>({48, 96}));
203+
ASSERT_EQ(archive.getIllustrations(), zim::Archive::Illustrations({
204+
{48, 48, 1.0, {}},
205+
{96, 96, 1.0, {}},
206+
}));
203207
ASSERT_TRUE(archive.hasMainEntry());
204208

205209
ASSERT_EQ(archive.getMetadata("Title"), "This is a title");
@@ -612,6 +616,9 @@ TEST_F(ZimArchive, illustration)
612616
ASSERT_EQ(illustrationItem.getPath(), "Illustration_48x48@1") << ctx;
613617
}
614618
ASSERT_EQ(archive.getIllustrationSizes(), std::set<unsigned int>({48}));
619+
ASSERT_EQ(archive.getIllustrations(), zim::Archive::Illustrations({
620+
{48, 48, 1.0, {}},
621+
}));
615622
}
616623
}
617624
}

0 commit comments

Comments
 (0)