|
| 1 | +/* |
| 2 | + * Copyright (C) 2006 Tommi Maekitalo |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or |
| 5 | + * modify it under the terms of the GNU General Public License as |
| 6 | + * published by the Free Software Foundation; either version 2 of the |
| 7 | + * License, or (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, but |
| 10 | + * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied |
| 11 | + * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and |
| 12 | + * NON-INFRINGEMENT. See the GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | + * |
| 18 | + */ |
| 19 | + |
| 20 | +#ifndef ZIM_ARTICLE_H |
| 21 | +#define ZIM_ARTICLE_H |
| 22 | + |
| 23 | +#include <string> |
| 24 | +#include <zim/zim.h> |
| 25 | +#include <zim/dirent.h> |
| 26 | +#include <zim/qunicode.h> |
| 27 | +#include <zim/file.h> |
| 28 | + |
| 29 | +namespace zim |
| 30 | +{ |
| 31 | + class Article |
| 32 | + { |
| 33 | + public: |
| 34 | + typedef Dirent::CompressionType CompressionType; |
| 35 | + typedef Dirent::MimeType MimeType; |
| 36 | + |
| 37 | + private: |
| 38 | + Dirent dirent; |
| 39 | + std::string data; |
| 40 | + mutable std::string uncompressedData; |
| 41 | + File file; |
| 42 | + size_type idx; |
| 43 | + bool dataRead; |
| 44 | + |
| 45 | + void uncompressData() const; |
| 46 | + |
| 47 | + public: |
| 48 | + Article() : dataRead(true) { } |
| 49 | + Article(size_type idx_, const Dirent& dirent_, const File& file_, |
| 50 | + const std::string& data_, bool dummy) |
| 51 | + : dirent(dirent_), |
| 52 | + data(data_), |
| 53 | + file(file_), |
| 54 | + idx(idx_), |
| 55 | + dataRead(true) |
| 56 | + { } |
| 57 | + Article(size_type idx_, const Dirent& dirent_, const File& file_) |
| 58 | + : dirent(dirent_), |
| 59 | + file(file_), |
| 60 | + idx(idx_), |
| 61 | + dataRead(false) |
| 62 | + { } |
| 63 | + |
| 64 | + Dirent& getDirent() { return dirent; } |
| 65 | + const Dirent& getDirent() const { return dirent; } |
| 66 | + void setDirent(const Dirent& d) { dirent = d; } |
| 67 | + |
| 68 | + const std::string& |
| 69 | + getParameter() const { return dirent.getParameter(); } |
| 70 | + void setParameter(const std::string& p) { dirent.setParameter(p); } |
| 71 | + |
| 72 | + offset_type getDataOffset() const { return dirent.getOffset(); } |
| 73 | + void setDataOffset(offset_type o) { dirent.setOffset(o); } |
| 74 | + |
| 75 | + size_type getDataLen() const { return dirent.getSize(); } |
| 76 | + |
| 77 | + CompressionType getCompression() const { return dirent.getCompression(); } |
| 78 | + bool isCompressionZip() const { return dirent.isCompressionZip(); } |
| 79 | + bool isCompressionBzip2() const { return dirent.isCompressionBzip2(); } |
| 80 | + bool isCompressionLzma() const { return dirent.isCompressionLzma(); } |
| 81 | + bool isCompressed() const { return isCompressionZip() || isCompressionLzma(); } |
| 82 | + |
| 83 | + QUnicodeString getTitle() const { return QUnicodeString(dirent.getTitle()); } |
| 84 | + void setTitle(const QUnicodeString& title) { dirent.setTitle(title.getValue()); } |
| 85 | + |
| 86 | + MimeType getLibraryMimeType() const { return dirent.getMimeType(); } |
| 87 | + void setLibraryMimeType(MimeType m) { dirent.setMimeType(m); } |
| 88 | + const std::string& |
| 89 | + getMimeType() const; |
| 90 | + |
| 91 | + bool getRedirectFlag() const { return dirent.getRedirectFlag(); } |
| 92 | + void setRedirectFlag(bool sw = true) { dirent.setRedirectFlag(sw); } |
| 93 | + |
| 94 | + char getNamespace() const { return dirent.getNamespace(); } |
| 95 | + void setNamespace(char ns) { dirent.setNamespace(ns); } |
| 96 | + |
| 97 | + size_type getRedirectIndex() const { return dirent.getRedirectIndex(); } |
| 98 | + void setRedirectIndex(size_type o) { dirent.setRedirectIndex(o); } |
| 99 | + |
| 100 | + Article getRedirectArticle() const; |
| 101 | + |
| 102 | + size_type getArticleOffset() const { return dirent.getArticleOffset(); } |
| 103 | + void setArticleOffset(size_type o) { dirent.setArticleOffset(o); } |
| 104 | + |
| 105 | + size_type getArticleSize() const { return dirent.getArticleSize(); } |
| 106 | + void setArticleSize(size_type s) { dirent.setArticleSize(s); } |
| 107 | + |
| 108 | + operator bool() { return getDataOffset() != 0; } |
| 109 | + |
| 110 | + bool operator< (const Article& a) const |
| 111 | + { return getNamespace() < a.getNamespace() |
| 112 | + || getNamespace() == a.getNamespace() |
| 113 | + && getTitle() < a.getTitle(); } |
| 114 | + |
| 115 | + const std::string& getRawData() const; |
| 116 | + |
| 117 | + std::string getData() const; |
| 118 | + void setData(const std::string& data_) { data = data_; dataRead = true; } |
| 119 | + |
| 120 | + size_type getUncompressedLen() const |
| 121 | + { |
| 122 | + uncompressData(); |
| 123 | + return uncompressedData.size(); |
| 124 | + } |
| 125 | + |
| 126 | + const File& getFile() const { return file; } |
| 127 | + File& getFile() { return file; } |
| 128 | + void setFile(const File& f) { file = f; } |
| 129 | + |
| 130 | + size_type getIndex() const { return idx; } |
| 131 | + void setIndex(size_type i) { idx = i; } |
| 132 | + |
| 133 | + QUnicodeString getUrl() const { return QUnicodeString(std::string(1, getNamespace()) + '/' + getDirent().getTitle()); } |
| 134 | + |
| 135 | + offset_type getIndexOffset() const { return getFile().getFileheader().getIndexPos() + idx * 4; } |
| 136 | + offset_type getDirentOffset() const { return getFile().getDirentOffset(idx); } |
| 137 | + }; |
| 138 | + |
| 139 | +} |
| 140 | + |
| 141 | +#endif // ZIM_ARTICLE_H |
| 142 | + |
0 commit comments