1 /*
2  * Xournal++
3  *
4  * Extracts a preview of an .xoj file, used by xournalpp-thumbnailer and xournalpp
5  * Because of this xournal type checks cannot be used
6  *
7  * @author Xournal++ Team
8  * https://github.com/xournalpp/xournalpp
9  *
10  * @license GNU GPLv2 or later
11  */
12 
13 #pragma once
14 
15 #include <string>
16 
17 #include <glib.h>
18 
19 #include "filesystem.h"
20 
21 using std::string;
22 
23 enum PreviewExtractResult {
24 
25     /**
26      * Successfully read an image from file
27      */
28     PREVIEW_RESULT_IMAGE_READ = 0,
29 
30     /**
31      * File extension is wrong
32      */
33     PREVIEW_RESULT_BAD_FILE_EXTENSION,
34 
35     /**
36      * The file could not be openend / found
37      */
38     PREVIEW_RESULT_COULD_NOT_OPEN_FILE,
39 
40     /**
41      * The preview could not be extracted
42      */
43     PREVIEW_RESULT_ERROR_READING_PREVIEW,
44 
45     /**
46      * The file contains no preview
47      */
48     PREVIEW_RESULT_NO_PREVIEW,
49 };
50 
51 class XojPreviewExtractor {
52 public:
53     XojPreviewExtractor();
54     ~XojPreviewExtractor();
55 
56 public:
57     /**
58      * Try to read the preview from file
59      * @param file .xoj File
60      * @return If an image was read, or the error
61      */
62     PreviewExtractResult readFile(const fs::path& file);
63 
64     /**
65      * Try to read the preview from byte buffer
66      * @param buffer Buffer
67      * @param len Buffer len
68      * @return If an image was read, or the error
69      */
70     PreviewExtractResult readPreview(char* buffer, int len);
71 
72     /**
73      * @return The preview data, should be a binary PNG
74      */
75     unsigned char* getData(gsize& dataLen);
76 
77     // Member
78 private:
79     /**
80      * Preview data
81      */
82     unsigned char* data = nullptr;
83     gsize dataLen = 0;
84 };
85