[KLF Application][KLF Tools][KLF Backend][KLF Home]
KLatexFormula Project

src/klfliblegacyengine_p.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   file klfliblegacyengine_p.h
00003  *   This file is part of the KLatexFormula Project.
00004  *   Copyright (C) 2010 by Philippe Faist
00005  *   philippe.faist at bluewin.ch
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  *   This program is distributed in the hope that it will be useful,       *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU General Public License for more details.                          *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU General Public License     *
00018  *   along with this program; if not, write to the                         *
00019  *   Free Software Foundation, Inc.,                                       *
00020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00021  ***************************************************************************/
00022 /* $Id: klfliblegacyengine_p.h 562 2010-11-21 19:01:41Z philippe $ */
00023 
00024 
00029 #ifndef KLFLIBLEGACYENGINE_P_H
00030 #define KLFLIBLEGACYENGINE_P_H
00031 
00032 #include <QObject>
00033 #include <QMap>
00034 #include <QFileInfo>
00035 
00036 #include "klfliblegacyengine.h"
00037 
00039 class KLFLibLegacyFileDataPrivate : public QObject
00040 {
00041   Q_OBJECT
00042 public:
00050   static inline KLFLibLegacyFileDataPrivate * instanceFor(const QString fname, bool starttimer)
00051   {
00052     QString f = canonicalFilePath(fname);
00053     klfDbg("fname="<<fname<<"; canonical f="<<f<<"; starttimer="<<starttimer) ;
00054     if (f.isEmpty()) {
00055       qWarning()<<KLF_FUNC_NAME<<": error getting canonical file path for "<<fname<<".";
00056       return NULL;
00057     }
00058     if (staticFileDataObjects.contains(f))
00059       return staticFileDataObjects[f];
00060     KLFLibLegacyFileDataPrivate *d = new KLFLibLegacyFileDataPrivate(f);
00061     if (starttimer && !d->autoSaveTimer->isActive())
00062       d->autoSaveTimer->start(180000); // 180s = 3min
00063     return d;
00064   }
00065 
00074   static QString canonicalFilePath(const QString& fname)
00075   {
00076     QFileInfo fi(fname);
00077     if (fi.exists())
00078       return fi.canonicalFilePath();
00079     // non-existing file. Rely on existing directory
00080     QString containdir = fi.absolutePath();
00081     klfDbg("non-existing file "<<fname<<": containing dir="<<containdir) ;
00082     QFileInfo di(containdir);
00083     if (!di.exists() || !di.isDir()) {
00084       qWarning()<<KLF_FUNC_NAME<<": Path "<<fname<<": directory "<<containdir<<" does not exist.";
00085       return QString();
00086     }
00087     QString canonical = QFileInfo(containdir).canonicalFilePath();
00088     if (canonical.isEmpty()) {
00089       qWarning()<<KLF_FUNC_NAME<<": Error getting "<<containdir<<"'s canonical path.";
00090       return QString();
00091     }
00092     if (!canonical.endsWith("/"))
00093       canonical += "/";
00094     canonical += fi.fileName();
00095     return canonical;
00096   }
00097 
00099   ~KLFLibLegacyFileDataPrivate()
00100   {
00101     klfDbg("destroying. Possibly save? haschanges="<<haschanges) ;
00102     if (haschanges)
00103       save();
00104 
00105     staticFileDataObjects.remove(filename);
00106     delete autoSaveTimer;
00107   }
00108 
00110   inline void ref() { ++refcount; }
00113   inline int deref() { return --refcount; }
00114 
00115   inline QString fileName() const { return filename; }
00116 
00117 
00118   enum LegacyLibType { LocalHistoryType = 1, LocalLibraryType, ExportLibraryType };
00119 
00120   bool haschanges;
00121 
00123   KLFLegacyData::KLFLibrary library;
00125   KLFLegacyData::KLFLibraryResourceList resources;
00126 
00136   QVariantMap metadata;
00137 
00138   LegacyLibType legacyLibType;
00139 
00140   QTimer *autoSaveTimer;
00141 
00143   int findResourceName(const QString& resname);
00144   int getReservedResourceId(const QString& resourceName, int defaultId);
00145 
00146 
00147 
00148   static inline KLFLibEntry toLibEntry(const KLFLegacyData::KLFLibraryItem& item)
00149   {
00150     return KLFLibEntry(KLFLibEntry::stripCategoryTagsFromLatex(item.latex), item.datetime,
00151                        item.preview.toImage(), item.preview.size(), item.category,
00152                        item.tags, toStyle(item.style));
00153   }
00154   static inline KLFLegacyData::KLFLibraryItem toLegacyLibItem(const KLFLibEntry& entry)
00155   {
00156     KLFLegacyData::KLFLibraryItem item;
00157     item.id = KLFLegacyData::KLFLibraryItem::MaxId++;
00158     // ensure latex has category & tags information
00159     item.latex = KLFLibEntry::latexAddCategoryTagsComment(KLFLibEntry::stripCategoryTagsFromLatex(entry.latex()),
00160                                                           entry.category(), entry.tags()) ;
00161     item.category = entry.category();
00162     item.tags = entry.tags();
00163     item.preview = QPixmap::fromImage(entry.preview());
00164     item.datetime = entry.dateTime();
00165     item.style = toLegacyStyle(entry.style());
00166     return item;
00167   }
00168   static inline KLFLegacyData::KLFStyle toLegacyStyle(const KLFStyle& style)
00169   {
00170     KLFLegacyData::KLFStyle oldstyle;
00171     oldstyle.name = style.name;
00172     oldstyle.fg_color = style.fg_color;
00173     oldstyle.bg_color = style.bg_color;
00174     oldstyle.mathmode = style.mathmode;
00175     oldstyle.preamble = style.preamble;
00176     oldstyle.dpi = style.dpi;
00177     return oldstyle;
00178   }
00179   static inline KLFStyle toStyle(const KLFLegacyData::KLFStyle& oldstyle)
00180   {
00181     KLFStyle style;
00182     style.name = oldstyle.name;
00183     style.fg_color = oldstyle.fg_color;
00184     style.bg_color = oldstyle.bg_color;
00185     style.mathmode = oldstyle.mathmode;
00186     style.preamble = oldstyle.preamble;
00187     style.dpi = oldstyle.dpi;
00188     return style;
00189   }
00190 
00191 signals:
00192   void resourcePropertyChanged(int propId);
00193 
00194 public slots:
00199   bool load(const QString& fname = QString());
00200 
00202   bool save(const QString& fname = QString());
00203 
00204   void emitResourcePropertyChanged(int propId) { emit resourcePropertyChanged(propId); }
00205 
00206 private:
00207   KLFLibLegacyFileDataPrivate() { }
00208 
00209   KLFLibLegacyFileDataPrivate(const QString& fname) : refcount(0), filename(fname)
00210   {
00211     klfDbg(" filename is "<<filename ) ;
00212 
00213     staticFileDataObjects[filename] = this;
00214 
00215     if (QFile::exists(fname))
00216       load(); // load the data
00217 
00218     // by default, we're a .klf export type
00219     legacyLibType = ExportLibraryType;
00220 
00221     // prepare the autosave timer
00222     autoSaveTimer = new QTimer(NULL);
00223     autoSaveTimer->setSingleShot(false);
00224     connect(autoSaveTimer, SIGNAL(timeout()), this, SLOT(save()));
00225   }
00226 
00227   int refcount;
00228 
00229   QString filename;
00230 
00231   static QMap<QString,KLFLibLegacyFileDataPrivate*> staticFileDataObjects;
00232 
00233 };
00234 
00235 
00236 
00237 
00238 #endif

Generated by doxygen 1.7.3