00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef KLFLIB_H
00025 #define KLFLIB_H
00026
00027 #include <QImage>
00028 #include <QMap>
00029 #include <QUrl>
00030 #include <QDataStream>
00031 #include <QDateTime>
00032 #include <QFileInfo>
00033
00034 #include <klfdefs.h>
00035 #include <klfbackend.h>
00036
00037 #include <klfpobj.h>
00038 #include <klffactory.h>
00039 #include <klfstyle.h>
00040
00041
00042
00043 class KLFProgressReporter;
00044
00045
00046
00055 class KLF_EXPORT KLFLibEntry : public KLFPropertizedObject {
00056 public:
00061 enum PropertyId {
00062 Latex = 0,
00063 DateTime,
00064 Preview,
00065 PreviewSize,
00066 Category,
00067 Tags,
00068 Style
00069 };
00070
00071 KLFLibEntry(const QString& latex = QString(), const QDateTime& dt = QDateTime(),
00072 const QImage& preview = QImage(), const QSize& previewsize = QSize(),
00073 const QString& category = QString(), const QString& tags = QString(),
00074 const KLFStyle& style = KLFStyle());
00077 KLFLibEntry(const QString& latex, const QDateTime& dt, const QImage& preview,
00078 const KLFStyle& style);
00079 KLFLibEntry(const KLFLibEntry& copy);
00080 virtual ~KLFLibEntry();
00081
00082 inline QString latex() const { return property(Latex).toString(); }
00083 inline QDateTime dateTime() const { return property(DateTime).toDateTime(); }
00084 inline QImage preview() const { return property(Preview).value<QImage>(); }
00085 inline QSize previewSize() const { return property(PreviewSize).value<QSize>(); }
00086 inline QString category() const { return property(Category).toString(); }
00087 inline QString tags() const { return property(Tags).toString(); }
00088 inline KLFStyle style() const { return property(Style).value<KLFStyle>(); }
00089
00090 inline QString latexWithCategoryTagsComments() const
00091 { return latexAddCategoryTagsComment(latex(), category(), tags()); }
00092
00093 inline void setLatex(const QString& latex) { setProperty(Latex, latex); }
00094 inline void setDateTime(const QDateTime& dt) { setProperty(DateTime, dt); }
00095 inline void setPreview(const QImage& img) { setProperty(Preview, img); }
00096 inline void setPreviewSize(const QSize& sz) { setProperty(PreviewSize, sz); }
00101 inline void setCategory(const QString& s) { setProperty(Category, normalizeCategoryPath(s)); }
00102 inline void setTags(const QString& s) { setProperty(Tags, s); }
00103 inline void setStyle(const KLFStyle& style) { setProperty(Style, QVariant::fromValue(style)); }
00104
00111 int setEntryProperty(const QString& propName, const QVariant& value);
00112
00113
00116 static QString categoryFromLatex(const QString& latex);
00118 static QString tagsFromLatex(const QString& latex);
00120 static QString stripCategoryTagsFromLatex(const QString& latex);
00122 static QString latexAddCategoryTagsComment(const QString& latex, const QString& category,
00123 const QString& tags);
00132 static QString normalizeCategoryPath(const QString& categoryPath);
00133
00134 private:
00135
00136 void initRegisteredProperties();
00137 };
00138
00139 Q_DECLARE_METATYPE(KLFLibEntry)
00140 ;
00141
00142 typedef QList<KLFLibEntry> KLFLibEntryList;
00143
00144
00146 namespace KLFLib {
00148
00156 typedef qint32 entryId;
00157
00158
00163 struct StringMatch {
00164 StringMatch(const QVariant& value = QVariant(), Qt::MatchFlags flags = Qt::MatchExactly)
00165 : mFlags(flags), mValue(value), mValueString(value.toString()) { }
00166 StringMatch(const StringMatch& m) : mFlags(m.mFlags), mValue(m.mValue), mValueString(m.mValueString) { }
00167
00173 inline const Qt::MatchFlags matchFlags() const { return mFlags; }
00175 inline const QVariant matchValue() const { return mValue; }
00178 inline const QString matchValueString() const { return mValueString; }
00179
00180 protected:
00181 Qt::MatchFlags mFlags;
00182 QVariant mValue;
00183 QString mValueString;
00184 };
00185
00190 struct PropertyMatch : public StringMatch {
00192 PropertyMatch(int propId = -1, const StringMatch& match = StringMatch())
00193 : StringMatch(match), mPropertyId(propId) { }
00195 PropertyMatch(const PropertyMatch& other) : StringMatch(other), mPropertyId(other.mPropertyId) { }
00196
00198 inline int propertyId() const { return mPropertyId; }
00199
00200 protected:
00201 int mPropertyId;
00202 };
00203
00214 struct EntryMatchCondition
00215 {
00216 enum Type {
00217 MatchAllType = 0,
00218 PropertyMatchType,
00219 NegateMatchType,
00220 OrMatchType,
00221 AndMatchType
00222 };
00223
00225 inline Type type() const { return mType; }
00227 inline PropertyMatch propertyMatch() const { return mPropertyMatch; }
00229 inline QList<EntryMatchCondition> conditionList() const { return mConditionList; }
00230
00231 static EntryMatchCondition mkMatchAll();
00232 static EntryMatchCondition mkPropertyMatch(PropertyMatch pmatch);
00234 static EntryMatchCondition mkNegateMatch(const EntryMatchCondition& condition);
00235 static EntryMatchCondition mkOrMatch(QList<EntryMatchCondition> conditions);
00236 static EntryMatchCondition mkAndMatch(QList<EntryMatchCondition> conditions);
00237
00238 protected:
00239 EntryMatchCondition(Type type) : mType(type) { }
00240
00241 Type mType;
00242
00243 PropertyMatch mPropertyMatch;
00244 QList<EntryMatchCondition> mConditionList;
00245 };
00246
00247 }
00248
00250
00276 class KLF_EXPORT KLFLibEntrySorter
00277 {
00278 public:
00279 KLFLibEntrySorter(int propId = -1, Qt::SortOrder order = Qt::AscendingOrder);
00288 KLFLibEntrySorter(const KLFLibEntrySorter *clone);
00289 virtual ~KLFLibEntrySorter();
00290
00291 inline bool isValid() const { return (pPropId >= 0); }
00292
00294 inline int propId() const { return pPropId; }
00296 inline Qt::SortOrder order() const { return pOrder; }
00297
00299 virtual void setPropId(int propId);
00301 virtual void setOrder(Qt::SortOrder order);
00302
00304 virtual QString entryValue(const KLFLibEntry& entry, int propId) const;
00305
00307
00315 virtual bool compareLessThan(const KLFLibEntry& a, const KLFLibEntry& b,
00316 int propId, Qt::SortOrder order) const;
00317
00321 virtual bool operator()(const KLFLibEntry& a, const KLFLibEntry& b) const;
00322
00323
00324 private:
00325 const KLFLibEntrySorter * pCloneOf;
00326
00327 int pPropId;
00328 Qt::SortOrder pOrder;
00329 };
00330
00331
00332
00440 class KLF_EXPORT KLFLibResourceEngine : public QObject, public KLFPropertizedObject
00441 {
00442 Q_OBJECT
00443 public:
00444 typedef KLFLib::entryId entryId;
00445
00447 struct KLFLibEntryWithId {
00448 KLFLibEntryWithId(entryId i = -1, const KLFLibEntry& e = KLFLibEntry())
00449 : id(i), entry(e) { }
00450 entryId id;
00451 KLFLibEntry entry;
00452 };
00453
00456 enum ResourceProperty {
00457 PropTitle = 0,
00458 PropLocked,
00459 PropViewType,
00460 PropAccessShared
00461 };
00462
00469 enum SubResourceProperty {
00470 SubResPropTitle = 0,
00471 SubResPropLocked = 1,
00472 SubResPropViewType = 2
00473 };
00474
00476
00486 enum ResourceFeature {
00488
00491 FeatureReadOnly = 0x0001,
00493
00500 FeatureLocked = 0x0002,
00502
00504 FeatureSaveTo = 0x0004,
00506
00508 FeatureSubResources = 0x0008,
00510
00515 FeatureSubResourceProps = 0x0010,
00516 };
00517
00538 KLFLibResourceEngine(const QUrl& url, uint supportedfeatureflags, QObject *parent = NULL);
00539
00540 virtual ~KLFLibResourceEngine();
00541
00543
00550 virtual uint supportedFeatureFlags() const { return pFeatureFlags; }
00551
00553 enum WantUrlFormatFlag {
00555 WantUrlDefaultSubResource = 0x01,
00557 WantUrlReadOnly = 0x02
00558 };
00560
00571 virtual QUrl url(uint flags = 0x0) const;
00572
00574
00591 virtual uint compareUrlTo(const QUrl& other, uint interestFlags = 0xfffffff) const = 0;
00592
00594
00595 virtual bool isReadOnly() const { return pReadOnly; }
00596
00598
00599 virtual QString title() const { return KLFPropertizedObject::property(PropTitle).toString(); }
00600
00602
00610 virtual bool locked() const { return KLFPropertizedObject::property(PropLocked).toBool(); }
00611
00613 virtual QString viewType() const
00614 { return KLFPropertizedObject::property(PropViewType).toString(); }
00615
00617
00622 virtual bool accessShared() const
00623 { return KLFPropertizedObject::property(PropAccessShared).toBool(); }
00624
00626
00631 virtual QVariant resourceProperty(const QString& name) const;
00632
00633 enum ModifyType {
00634 AllActionsData = 0,
00635 UnknownModification = 0,
00636 InsertData,
00637 ChangeData,
00638 DeleteData
00639 };
00640
00662 virtual bool canModifyData(const QString& subResource, ModifyType modifytype) const;
00663
00678 virtual bool canModifyData(ModifyType modifytype) const;
00679
00692 virtual bool canModifyProp(int propId) const;
00693
00700 virtual bool canRegisterProperty(const QString& propName) const;
00701
00704 virtual QString suggestedViewTypeIdentifier() const { return QString(); }
00705
00718 virtual bool hasSubResource(const QString& subResource) const;
00719
00722 virtual QStringList subResourceList() const { return QStringList(); }
00723
00729 virtual QString defaultSubResource() const;
00730
00739 virtual bool compareDefaultSubResourceEquals(const QString& subResourceName) const;
00740
00747 virtual bool canCreateSubResource() const;
00748
00756 virtual bool canRenameSubResource(const QString& subResource) const;
00757
00765 virtual bool canDeleteSubResource(const QString& subResource) const;
00766
00771 virtual QVariant subResourceProperty(const QString& subResource, int propId) const;
00772
00780 virtual QList<int> subResourcePropertyIdList() const { return QList<int>(); }
00781
00794 virtual QString subResourcePropertyName(int propId) const;
00795
00809 virtual bool canModifySubResourceProperty(const QString& subResource, int propId) const;
00810
00812
00821 virtual KLFLibEntry entry(const QString& subResource, entryId id) = 0;
00822
00824
00830 virtual KLFLibEntry entry(entryId id);
00831
00833
00838 virtual bool hasEntry(const QString& subResource, entryId id) = 0;
00839
00841
00848 virtual bool hasEntry(entryId id);
00849
00851
00870 virtual QList<KLFLibEntryWithId> entries(const QString& subResource,
00871 const QList<KLFLib::entryId>& idList,
00872 const QList<int>& wantedEntryProperties = QList<int>()) = 0;
00873
00875
00896 virtual QList<KLFLibEntryWithId> entries(const QList<KLFLib::entryId>& idList,
00897 const QList<int>& wantedEntryProperties = QList<int>());
00898
00899
00933 struct Query
00934 {
00936 Query()
00937 : matchCondition(KLFLib::EntryMatchCondition::mkMatchAll()),
00938 skip(0),
00939 limit(-1),
00940 orderPropId(-1),
00941 orderDirection(Qt::AscendingOrder),
00942 wantedEntryProperties(QList<int>())
00943 {
00944 }
00945
00946 KLFLib::EntryMatchCondition matchCondition;
00947 int skip;
00948 int limit;
00949 int orderPropId;
00950 Qt::SortOrder orderDirection;
00951 QList<int> wantedEntryProperties;
00952 };
00953
00970 struct QueryResult
00971 {
00972 enum Flags { FillEntryIdList = 0x01, FillRawEntryList = 0x02, FillEntryWithIdList = 0x04 };
00973
00976 QueryResult(uint fill_flags = 0x00) : fillFlags(fill_flags) { }
00977 uint fillFlags;
00978
00979 QList<KLFLib::entryId> entryIdList;
00980 KLFLibEntryList rawEntryList;
00981 QList<KLFLibEntryWithId> entryWithIdList;
00982 };
00983
00984
00986
01036 virtual int query(const QString& subResource, const Query& query, QueryResult *result) = 0;
01037
01038
01056 virtual QList<QVariant> queryValues(const QString& subResource, int entryPropId) = 0;
01057
01058
01060
01065 virtual QList<KLFLib::entryId> allIds(const QString& subResource) = 0;
01066
01068
01077 virtual QList<KLFLib::entryId> allIds();
01078
01080
01085 virtual QList<KLFLibEntryWithId> allEntries(const QString& subResource,
01086 const QList<int>& wantedEntryProperties = QList<int>()) = 0;
01087
01089
01096 virtual QList<KLFLibEntryWithId> allEntries(const QList<int>& wantedEntryProperties = QList<int>());
01097
01098
01100 void blockProgressReportingForNextOperation();
01101
01103 void blockProgressReporting(bool block);
01104
01105 signals:
01107
01124 void dataChanged(const QString& subResource, int modificationType,
01125 const QList<KLFLib::entryId>& entryIdList);
01126
01128 void defaultSubResourceChanged(const QString& newDefaultSubResource);
01129
01131
01132 void resourcePropertyChanged(int propId);
01133
01135
01141 void subResourcePropertyChanged(const QString& subResource, int propId);
01142
01144
01147 void subResourceCreated(const QString& newSubResource);
01148
01150
01153 void subResourceRenamed(const QString& oldSubResourceName,
01154 const QString& newSubResourceName);
01155
01157
01160 void subResourceDeleted(const QString& subResource);
01161
01162
01193 void operationStartReportingProgress(KLFProgressReporter *progressReporter,
01194 const QString& descriptiveText);
01195
01196
01197
01198 public slots:
01199
01201
01211 virtual bool setTitle(const QString& title);
01212
01214
01226 virtual bool setLocked(bool locked);
01227
01237 virtual bool setViewType(const QString& viewType);
01238
01240
01245 virtual bool setReadOnly(bool readonly);
01246
01248
01264 virtual void setDefaultSubResource(const QString& subResource);
01265
01267
01282 virtual bool createSubResource(const QString& subResource, const QString& subResourceTitle);
01283
01285
01299 virtual bool createSubResource(const QString& subResource);
01300
01318 virtual bool renameSubResource(const QString& oldSubResourceName, const QString& newSubResourceName);
01319
01333 virtual bool deleteSubResource(const QString& subResource);
01334
01335
01343 virtual bool setSubResourceProperty(const QString& subResource, int propId, const QVariant& value);
01344
01345
01347
01356 virtual entryId insertEntry(const QString& subResource, const KLFLibEntry& entry);
01357
01359
01368 virtual entryId insertEntry(const KLFLibEntry& entry);
01369
01371
01388 virtual QList<entryId> insertEntries(const QString& subResource, const KLFLibEntryList& entrylist) = 0;
01389
01391
01400 virtual QList<entryId> insertEntries(const KLFLibEntryList& entrylist);
01401
01403
01421 virtual bool changeEntries(const QString& subResource, const QList<entryId>& idlist,
01422 const QList<int>& properties, const QList<QVariant>& values) = 0;
01423
01425
01433 virtual bool changeEntries(const QList<entryId>& idlist, const QList<int>& properties,
01434 const QList<QVariant>& values);
01435
01437
01452 virtual bool deleteEntries(const QString& subResource, const QList<entryId>& idlist) = 0;
01453
01455
01463 virtual bool deleteEntries(const QList<entryId>& idList);
01464
01474 virtual bool saveTo(const QUrl& newPath);
01475
01477
01488 virtual bool setResourceProperty(int propId, const QVariant& value);
01489
01491
01500 virtual bool loadResourceProperty(const QString& propName, const QVariant& value);
01501
01502
01503 protected:
01504
01506
01513 virtual bool saveResourceProperty(int propId, const QVariant& value) = 0;
01514
01517 enum ModifyStatus {
01518 MS_CanModify = 0,
01519 MS_IsLocked = 1,
01520 MS_SubResLocked = 2,
01521 MS_NotModifiable = 3
01522 };
01523
01525
01539 virtual ModifyStatus baseCanModifyStatus(bool inSubResource,
01540 const QString& subResource = QString()) const;
01541
01542 bool thisOperationProgressBlocked() const;
01543
01544 private:
01545 void initRegisteredProperties();
01546
01547 QUrl pUrl;
01548 uint pFeatureFlags;
01549 bool pReadOnly;
01550
01551 QString pDefaultSubResource;
01552
01553 mutable bool pProgressBlocked;
01554 bool pThisOperationProgressBlockedOnly;
01555
01556 KLF_DEBUG_DECLARE_REF_INSTANCE( QFileInfo(url().path()).fileName()+":"+defaultSubResource() ) ;
01557 };
01558
01559
01560 Q_DECLARE_METATYPE(KLFLibResourceEngine::KLFLibEntryWithId)
01561 ;
01562
01563
01564 KLF_EXPORT QDataStream& operator<<(QDataStream& stream,
01565 const KLFLibResourceEngine::KLFLibEntryWithId& entrywid);
01566 KLF_EXPORT QDataStream& operator>>(QDataStream& stream,
01567 KLFLibResourceEngine::KLFLibEntryWithId& entrywid);
01568
01569
01588 class KLF_EXPORT KLFLibResourceSimpleEngine : public KLFLibResourceEngine
01589 {
01590 Q_OBJECT
01591 public:
01592 KLFLibResourceSimpleEngine(const QUrl& url, uint supportedfeatureflags, QObject *parent = NULL)
01593 : KLFLibResourceEngine(url, supportedfeatureflags, parent)
01594 {
01595 }
01596 virtual ~KLFLibResourceSimpleEngine() { }
01597
01598
01599
01600 virtual QList<KLFLib::entryId> allIds(const QString& subResource);
01601 virtual bool hasEntry(const QString&, entryId id);
01602 virtual QList<KLFLibEntryWithId> entries(const QString&, const QList<KLFLib::entryId>& idList,
01603 const QList<int>& wantedEntryProperties = QList<int>());
01604
01607 class QueryResultListSorter
01608 {
01609 KLFLibEntrySorter *mSorter;
01610 QueryResult *mResult;
01611 uint fillflags;
01612 bool reference_is_rawentrylist;
01613 public:
01619 QueryResultListSorter(KLFLibEntrySorter *sorter, QueryResult *result);
01620
01621
01622
01624 inline bool operator()(const KLFLibEntry& a, const KLFLibEntry& b)
01625 { return mSorter->operator()(a, b); }
01626
01628 inline bool operator()(const KLFLibEntryWithId& a, const KLFLibEntryWithId& b)
01629 { return mSorter->operator()(a.entry, b.entry); }
01630
01633 int numberOfEntries();
01634
01653 void insertIntoOrderedResult(const KLFLibEntryWithId& entry);
01654 };
01655
01656 virtual int query(const QString& subResource, const Query& query, QueryResult *result);
01657
01658 virtual QList<QVariant> queryValues(const QString& subResource, int entryPropId);
01659
01660
01663 static int queryImpl(KLFLibResourceEngine *resource, const QString& subResource,
01664 const Query& query, QueryResult *result);
01665
01668 static QList<QVariant> queryValuesImpl(KLFLibResourceEngine *resource, const QString& subResource,
01669 int entryPropId);
01670
01671
01673 static bool testEntryMatchConditionImpl(const KLFLib::EntryMatchCondition& condition,
01674 const KLFLibEntry& libentry);
01675 };
01676
01677
01678
01685 class KLF_EXPORT KLFLibEngineFactory : public QObject, public KLFFactoryBase
01686 {
01687 Q_OBJECT
01688 public:
01693 typedef QMap<QString,QVariant> Parameters;
01694
01695 enum SchemeFunctions {
01696 FuncOpen = 0x01,
01697 FuncCreate = 0x02,
01698 FuncSaveTo = 0x04
01699 };
01700
01702 KLFLibEngineFactory(QObject *parent = NULL);
01704 virtual ~KLFLibEngineFactory();
01705
01711 virtual QStringList supportedTypes() const = 0;
01712
01722 virtual uint schemeFunctions(const QString& scheme) const;
01723
01726 virtual QString schemeTitle(const QString& scheme) const = 0;
01727
01733 virtual QString correspondingWidgetType(const QString& scheme) const = 0;
01734
01738 virtual KLFLibResourceEngine *openResource(const QUrl& location, QObject *parent = NULL) = 0;
01739
01741
01758 virtual KLFLibResourceEngine *createResource(const QString& scheme, const Parameters& parameters,
01759 QObject *parent = NULL);
01760
01762
01779 virtual bool saveResourceTo(KLFLibResourceEngine *resource, const QUrl& newLocation);
01780
01781
01782
01783
01790 static KLFLibEngineFactory *findFactoryFor(const QUrl& url);
01791
01794 static KLFLibEngineFactory *findFactoryFor(const QString& urlScheme);
01795
01797 static QStringList allSupportedSchemes();
01798
01801 static KLFLibResourceEngine *openURL(const QUrl& location, QObject *parent = NULL);
01802
01816 static QStringList listSubResources(const QUrl& url);
01817
01827 static QMap<QString,QString> listSubResourcesWithTitles(const QUrl& url);
01828
01829 private:
01830 static KLFFactoryManager pFactoryManager;
01831 };
01832
01833
01834
01835
01836
01837
01838 class QMimeData;
01839
01841 class KLF_EXPORT KLFAbstractLibEntryMimeEncoder
01842 {
01843 public:
01844 KLFAbstractLibEntryMimeEncoder();
01845 virtual ~KLFAbstractLibEntryMimeEncoder();
01846
01848 virtual QStringList supportedEncodingMimeTypes() const = 0;
01850 virtual QStringList supportedDecodingMimeTypes() const = 0;
01851
01852 virtual QByteArray encodeMime(const KLFLibEntryList& entryList, const QVariantMap& metaData,
01853 const QString& mimeType) const = 0;
01854
01855 virtual bool decodeMime(const QByteArray& data, const QString& mimeType,
01856 KLFLibEntryList *entryList, QVariantMap *metaData) const = 0;
01857
01858
01859 static QStringList allEncodingMimeTypes();
01860 static QStringList allDecodingMimeTypes();
01862 static QMimeData *createMimeData(const KLFLibEntryList& entryList, const QVariantMap& metaData);
01863 static bool canDecodeMimeData(const QMimeData *mimeData);
01864 static bool decodeMimeData(const QMimeData *mimeData, KLFLibEntryList *entryList,
01865 QVariantMap *metaData);
01866
01867 static KLFAbstractLibEntryMimeEncoder *findEncoderFor(const QString& mimeType,
01868 bool warnIfNotFound = true);
01869 static KLFAbstractLibEntryMimeEncoder *findDecoderFor(const QString& mimeType,
01870 bool warnIfNotFound = true);
01871 static QList<KLFAbstractLibEntryMimeEncoder*> encoderList();
01872 private:
01873
01874 static void registerEncoder(KLFAbstractLibEntryMimeEncoder *encoder);
01875
01876 static QList<KLFAbstractLibEntryMimeEncoder*> staticEncoderList;
01877 };
01878
01879
01880
01881
01882
01883 #ifdef KLF_DEBUG
01884 #include <QDebug>
01885 KLF_EXPORT QDebug& operator<<(QDebug& dbg, const KLFLib::StringMatch& smatch);
01886 KLF_EXPORT QDebug& operator<<(QDebug& dbg, const KLFLib::PropertyMatch& pmatch);
01887 KLF_EXPORT QDebug& operator<<(QDebug& dbg, const KLFLib::EntryMatchCondition& c);
01888 KLF_EXPORT QDebug& operator<<(QDebug& dbg, const KLFLibResourceEngine::Query& q);
01889 KLF_EXPORT QDebug& operator<<(QDebug& dbg, const KLFLibResourceEngine::KLFLibEntryWithId& ewid);
01890 #endif
01891
01892
01893
01894
01895 #endif
01896