27 #include <QDataStream> 31 #include <QMessageBox> 32 #include <QApplication> 76 return s <<
"KLFLegacyData::KLFLibraryItem(id="<<item.
id<<
"; latex="<<item.
latex<<
")" ;
81 return s <<
"KLFLegacyData::KLFLibraryResource(id="<<res.
id<<
"; name="<<res.
name<<
")";
107 return stream << item.
id << item.
name;
111 return stream >> item.
id >> item.
name;
156 stream >> style.
name;
191 klfDbg(
"loading from file "<<fname<<
" (our filename="<<filename<<
")") ;
194 if ( ! fimp.
open(QIODevice::ReadOnly) ) {
195 qWarning(
"Unable to open library file %s!", qPrintable(fname));
203 if (s1 ==
"KLATEXFORMULA_LIBRARY_EXPORT") {
207 stream >> vmaj >> vmin;
209 if (!stream.
atEnd() && stream.
status() == QDataStream::Ok)
211 }
else if (s1 ==
"KLATEXFORMULA_LIBRARY") {
215 stream >> vmaj >> vmin;
224 stream >> lib_max_id;
229 if (!stream.
atEnd() && stream.
status() == QDataStream::Ok)
231 }
else if (s1 ==
"KLATEXFORMULA_HISTORY") {
235 stream >> vmaj >> vmin;
238 stream >> lib_max_id >> history;
243 historyresource.
name =
tr(
"History");
246 library[historyresource] = history;
249 qWarning(
"Error: Library file `%s' is invalid library file or corrupt!", qPrintable(fname));
258 for (j = 0; j < ll.
size(); ++j) {
273 <<
" resources (our filename="<<filename<<
")") ;
275 if ( ! fsav.open(QIODevice::WriteOnly) ) {
276 qWarning(
"Can't write to file %s!", qPrintable(fname));
314 qWarning(
"%s: Saving an old \"history\" resource. Only one resource can be saved, " 317 tr(
"Saving an old \"history\" resource. Only one resource can be saved, " 318 "it will be the first: %1").arg(
resources[0].name));
321 stream <<
QString(
"KLATEXFORMULA_HISTORY") << (qint16)2 << (qint16)0
327 stream <<
QString(
"KLATEXFORMULA_LIBRARY") << (qint16)2 << (qint16)1;
339 qWarning(
"%s: bad library type %d! Falling back to '.klf'-library-export type",
343 stream <<
QString(
"KLATEXFORMULA_LIBRARY_EXPORT") << (qint16)2 << (qint16)1
353 klfDbg(
"in first resource wrote "<<llll.
size()<<
" items.");
370 !
QString::compare(resname, qApp->translate(
"KLFMainWin",
"History"), Qt::CaseInsensitive) )
375 !
QString::compare(resname, qApp->translate(
"KLFMainWin",
"Archive"), Qt::CaseInsensitive) )
404 klfDbgSt(
"Requested empty fname.") ;
408 qWarning(
"%s: file %s does not exist!",
KLF_FUNC_NAME, qPrintable(fname));
412 if (url.
scheme() !=
"klf+legacy") {
413 qWarning(
"KLFLibLegacyEngine::openUrl(): unsupported scheme %s!", qPrintable(url.
scheme()));
430 QString lrname = legacyResourceName;
437 qWarning()<<
KLF_FUNC_NAME<<
": file name "<<fileName<<
" is empty!";
449 lrname =
tr(
"Default Resource");
453 <<
"; legacyResourceName="<<legacyResourceName);
467 qWarning()<<
KLF_FUNC_NAME<<
": Couldn't get KLFLibLegacyFileDataPrivate instance for "<<fileName<<
"! Expect Crash!";
474 updateResourceProperty(-1);
477 if (d->resources.isEmpty()) {
486 d->haschanges =
true;
489 klfDbg(
"Opened KLFLibLegacyEngine resource `"<<fileName<<
"': d="<<d<<
"; resources="<<d->resources
490 <<
" (me="<<
this<<
", d="<<d<<
")\n") ;
499 if ( ! d->deref() ) {
500 klfDbg(
"last reference to the private liblegacyenginedataprivate object d="<<d<<
", " 501 "saving(?) and deleting. haschanges="<<d->haschanges) ;
502 klfDbg(
"resources dump:\n"<<d->resources<<
"\nlibrary:\n"<<d->library) ;
521 klfDbg(
"base cannot modify resource engine...") ;
531 ? !
QFileInfo(d->fileName()).isWritable()
549 return canModifyProp(-1);
556 int index = d->findResourceName(resource);
563 for (k = 0; k < ll.
size() && ll[k].id != (quint32)
id; ++k)
565 if (k == ll.
size()) {
569 return d->toLibEntry(ll[k]);
579 int rindex = d->findResourceName(resource);
586 for (k = 0; k < ll.
size(); ++k) {
588 e.
entry = d->toLibEntry(ll[k]);
602 for (k = 0; k < d->resources.size(); ++k)
603 list << d->resources[k].name;
610 return canModifyData(
QString(), ChangeData);
614 return canModifyData(subResource, ChangeData);
618 return subResource.
length() && hasSubResource(subResource) &&
619 canModifyData(subResource, DeleteData) && subResourceList().size() > 1;
631 for (k = 0; k < d->resources.size() && d->resources[k].id != newId; ++k)
633 if (k == d->resources.size())
638 qWarning()<<
KLF_FUNC_NAME<<
"("<<subResource<<
",..): no new ID could be found (!?!)";
642 res.
name = subResource;
643 res.
id = d->getReservedResourceId(subResource, newId);
645 d->resources.push_back(res);
647 d->haschanges =
true;
649 emit subResourceCreated(subResource);
657 int rindex = d->findResourceName(subResource);
659 qWarning()<<
KLF_FUNC_NAME<<
": can't find sub-resource "<<subResource<<
" in our data.";
667 resref.
name = subResourceNewName;
670 int possibleNewId = d->getReservedResourceId(subResource, -1);
671 if (possibleNewId != -1)
672 resref.
id = possibleNewId;
675 d->library[resref] = liblist;
677 emit subResourceRenamed(subResource, subResourceNewName);
684 klfDbg(
"sub-resource: "<<subResource) ;
687 if (!canDeleteSubResource(subResource)) {
688 klfDbg(
"Cannot delete sub-resource "<<subResource) ;
691 int rindex = d->findResourceName(subResource);
693 qWarning()<<
KLF_FUNC_NAME<<
": can't find sub-resource "<<subResource<<
" in our data.";
701 d->library.remove(res);
703 emit subResourceDeleted(subResource);
710 klfDbg(
"() our url="<<url()<<
"." ) ;
712 qWarning(
"KLFLibLegacyEngine::save: resource is read-only!");
722 d->autoSaveTimer->stop();
723 if (intervalms > 0) {
724 d->autoSaveTimer->setInterval(intervalms);
725 d->autoSaveTimer->start();
732 klfDbg(
"subResource="<<subResource<<
"; entrylist="<<entrylist) ;
736 if ( entrylist.
size() == 0 )
738 if (!canModifyData(subResource, InsertData)) {
739 klfDbg(
"cannot modify data.") ;
743 int index = d->findResourceName(subResource);
745 klfDbg(
"cannot find sub-resource: "<<subResource) ;
752 for (k = 0; k < entrylist.
size(); ++k) {
754 d->library[d->resources[index]] << item;
757 d->haschanges =
true;
759 emit dataChanged(subResource, InsertData, newIds);
761 klfDbg(
"finished inserting items. d="<<d<<
"; dumping resources:\n"<<d->resources
762 <<
"\nand library:\n"<<d->library) ;
770 if (idlist.
size() == 0)
772 if (!canModifyData(subResource, ChangeData))
777 int index = d->findResourceName(subResource);
786 for (k = 0; k < idlist.
size(); ++k) {
790 for (libindex = 0; libindex < ll.
size() && ll[libindex].id != (quint32)idlist[k]; ++libindex)
792 if (libindex == ll.
size()) {
793 qWarning()<<
KLF_FUNC_NAME<<
": Can't find entry with id "<<idlist[k];
798 for (j = 0; j < properties.
size(); ++j) {
799 switch (properties[j]) {
802 {
QString curcategory = d->library[d->resources[index]][libindex].category;
803 QString curtags = d->library[d->resources[index]][libindex].tags;
804 d->library[d->resources[index]][libindex].latex =
809 d->library[d->resources[index]][libindex].datetime = values[j].toDateTime();
812 d->library[d->resources[index]][libindex].preview =
QPixmap::fromImage(values[j].value<QImage>());
818 QString newcategory = values[j].toString();
819 QString curtags = d->library[d->resources[index]][libindex].tags;
820 d->library[d->resources[index]][libindex].latex =
822 d->library[d->resources[index]][libindex].category = newcategory;
829 QString curcategory = d->library[d->resources[index]][libindex].category;
830 QString newtags = values[j].toString();
831 d->library[d->resources[index]][libindex].latex =
833 d->library[d->resources[index]][libindex].tags = newtags;
838 d->library[d->resources[index]][libindex].style = d->toLegacyStyle(values[j].value<KLFStyle>());
841 qWarning()<<
KLF_FUNC_NAME<<
": Cannot set arbitrary property "<<propertyNameForId(properties[j])
850 klfDbg(
": Changed entries. Dump:" ) ;
853 for (kl = 0; kl < ll2.
size(); ++kl)
854 klfDbg(
"\t#"<<kl<<
": "<<ll2[kl].latex<<
" - "<<ll2[kl].category ) ;
857 d->haschanges =
true;
859 emit dataChanged(subResource, ChangeData, idlist);
870 if (!canModifyData(subResource, DeleteData))
873 int index = d->findResourceName(subResource);
882 for (k = 0; k < idlist.
size(); ++k) {
884 for (j = 0; j < ll->
size() && ll->operator[](j).
id != (quint32)idlist[k]; ++j)
886 if (j == ll->
size()) {
887 qWarning(
"KLFLibLegacyEngine::deleteEntries: Can't find ID %d in library list in current resource.",
896 d->haschanges =
true;
898 emit dataChanged(subResource, DeleteData, idlist);
907 if (newPath.
scheme() ==
"klf+legacy") {
917 QVariantMap m = d->metadata[
"ResProps"].toMap();
919 QString propName = propertyNameForId(propId);
923 if (m.contains(propName) && m[propName] == value)
928 d->metadata[
"ResProps"] =
QVariant(m);
929 d->haschanges =
true;
931 d->emitResourcePropertyChanged(propId);
939 klfDbg(
"property id="<<propId) ;
968 klfDbg(
"file "<<fileName);
971 klfDbg(
"has .klf extension.") ;
976 if ( ! f.
open(QIODevice::ReadOnly) ) {
985 klfDbg(
"read line: got magic "<<s1);
1016 return tr(
"KLatexFormula Library Export File");
1045 QString defsubres = parameters[
"klfDefaultSubResource"].toString();
1050 if ( !parameters.
contains(
"Filename") ) {
1052 <<
"KLFLibLegacyEngineFactory::createResource: bad parameters. They do not contain `Filename': " 1059 qWarning()<<
"KLFLibLegacyEngineFactory::createResource("<<scheme<<
","<<parameters<<
","<<parent<<
"):" virtual ~KLFLibLegacyEngine()
static QString categoryFromLatex(const QString &latex)
QList< KLFLibraryResource > KLFLibraryResourceList
void updateResourceProperty(int propId)
virtual bool canDeleteSubResource(const QString &subResource) const
int findResourceName(const QString &resname)
virtual QStringList subResourceList() const
KLF_EXPORT uint klfUrlCompare(const QUrl &url1, const QUrl &url2, uint interestFlags, const QStringList &interestQueryItems)
virtual bool createSubResource(const QString &subResource, const QString &subResourceTitle)
virtual void setProperty(const QString &propname, const QVariant &value)
#define klfDbg(streamableItems)
fromImage(const QImage &image, Qt::ImageConversionFlags flags=Qt::AutoColor)
#define KLF_DEBUG_BLOCK(msg)
LegacyLibType legacyLibType
static QString stripCategoryTagsFromLatex(const QString &latex)
virtual bool saveTo(const QUrl &newPath)
virtual bool deleteSubResource(const QString &subResource)
KLFLegacyData::KLFStyle style
hasQueryItem(const QString &key)
static QString tagsFromLatex(const QString &latex)
tr(const char *sourceText, const char *comment=0, int n=-1)
virtual KLFLibEntry entry(const QString &resource, entryId id)
bool load(const QString &fname=QString())
virtual uint compareUrlTo(const QUrl &other, uint interestFlags=0xfffffff) const
#define KLF_ASSERT_NOT_NULL(ptr, msg, failaction)
The Latex Code of the equation.
QDebug & operator<<(QDebug &s, const KLFLegacyData::KLFLibraryItem &item)
virtual bool deleteEntries(const QString &subResource, const QList< entryId > &idlist)
KLF_EXPORT QString klfUrlLocalFilePath(const QUrl &url)
static KLFLibLegacyFileDataPrivate * instanceFor(const QString fname, bool starttimer)
void resourcePropertyChanged(int propId)
The Legacy Library support for the KLFLib framework.
KLFLibLegacyEngineFactory(QObject *parent=NULL)
startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive)
setScheme(const QString &scheme)
KLFLegacyData::KLFLibrary library
endsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive)
compare(const QString &s1, const QString &s2, Qt::CaseSensitivity cs)
virtual QString schemeTitle(const QString &scheme) const
virtual QList< KLFLibEntryWithId > allEntries(const QString &resource, const QList< int > &wantedEntryProperties=QList< int >())
KLF_EXPORT bool operator<(const KLFLegacyData::KLFLibraryResource a, const KLFLegacyData::KLFLibraryResource b)
The Category to which eq. belongs (path-style string)
static QString canonicalFilePath(const QString &fname)
#define KLF_DEBUG_TIME_BLOCK(msg)
virtual bool canModifyProp(int propid) const
virtual bool changeEntries(const QString &subResource, const QList< entryId > &idlist, const QList< int > &properties, const QList< QVariant > &values)
static QString latexAddCategoryTagsComment(const QString &latex, const QString &category, const QString &tags)
virtual bool canModifyData(const QString &subResource, ModifyType modifytype) const
Tags about the equation (string)
virtual bool saveResourceProperty(int propId, const QVariant &value)
fromValue(const T &value)
virtual QString correspondingWidgetType(const QString &scheme) const
Provides a simple API for reading library resources.
virtual QStringList supportedTypes() const
static KLFLibLegacyEngine * openUrl(const QUrl &url, QObject *parent=NULL)
QMap< KLFLibraryResource, KLFLibraryList > KLFLibrary
KLF_EXPORT bool operator==(const KLFLegacyData::KLFLibraryItem &a, const KLFLegacyData::KLFLibraryItem &b)
virtual QList< entryId > insertEntries(const QString &subResource, const KLFLibEntryList &entries)
KLFLegacyData::KLFLibraryResourceList resources
virtual KLFLibResourceEngine * createResource(const QString &scheme, const Parameters ¶meters, QObject *parent=NULL)
An entry (single formula) in the library.
QString guessScheme(const QString &fileName) const
virtual bool renameSubResource(const QString &subResource, const QString &subResourceName)
#define klfDbgSt(streamableItems)
An Image Preview of equation (scaled down QImage)
virtual uint schemeFunctions(const QString &scheme) const
critical(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons=Ok, StandardButton defaultButton=NoButton)
void setAllProperties(const QMap< QString, QVariant > &propValues)
addQueryItem(const QString &key, const QString &value)
warning(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons=Ok, StandardButton defaultButton=NoButton)
virtual QVariant property(const QString &propName) const
A KLFLibEntry in combination with a KLFLib::entryId.
virtual bool canRegisterProperty(const QString &propName) const
virtual bool canModifyData(const QString &subRes, ModifyType modifytype) const
An abstract resource engine.
virtual bool canRenameSubResource(const QString &subResource) const
QString propertyNameForId(int propId) const
The Date/Time at which the equation was evaluated.
virtual bool canCreateSubResource() const
connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type=Qt::AutoCompatConnection)
virtual void setAutoSaveInterval(int intervalms)
KLF_EXPORT QDataStream & operator>>(QDataStream &stream, KLFLegacyData::KLFLibraryItem &item)
QList< KLFLibraryItem > KLFLibraryList
bool save(const QString &fname=QString())
virtual bool canModifyProp(int propId) const
queryItemValue(const QString &key)
localeAwareCompare(const QString &s1, const QString &s2)
int getReservedResourceId(const QString &resourceName, int defaultId)
KLF_EXPORT bool resources_equal_for_import(const KLFLegacyData::KLFLibraryResource a, const KLFLegacyData::KLFLibraryResource b)
static KLFLibLegacyEngine * createDotKLF(const QString &fileName, QString legacyResourceName, QObject *parent=NULL)
fromLocalFile(const QString &localFile)
virtual KLFLibResourceEngine * openResource(const QUrl &location, QObject *parent=NULL)