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

src/klfconfig.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   file klfconfig.cpp
00003  *   This file is part of the KLatexFormula Project.
00004  *   Copyright (C) 2007 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$ */
00023 
00024 #include <iostream>
00025 
00026 #include <QApplication>
00027 #include <QMessageBox>
00028 #include <QObject>
00029 #include <QDir>
00030 #include <QTextStream>
00031 #include <QFont>
00032 #include <QFontDatabase>
00033 #include <QMap>
00034 #include <QString>
00035 #include <QListView> // icon view flow
00036 #include <QLocale>
00037 #include <QDesktopServices> // "My Documents" or "Documents" directory
00038 
00039 #include <klfmainwin.h>
00040 
00041 #include <klfutil.h>
00042 #include "klfmain.h"
00043 #include "klfconfig.h"
00044 
00045 
00046 static const char * __klf_fallback_share_dir =
00047 #if defined(Q_OS_WIN32) || defined(Q_OS_WIN64)  // windows
00048         "..";   // note: program is in a bin/ directory by default (this is for nsis-installer)
00049 #elif defined(Q_OS_MAC) || defined(Q_OS_DARWIN) // Mac OS X
00050         "../Resources";
00051 #else  // unix-like system
00052         "../share/klatexformula";
00053 #endif
00054 
00055 
00056 static const char * __klf_share_dir =
00057 #ifdef KLF_SHARE_DIR  // defined by the build system
00058         KLF_SHARE_DIR;
00059 #else
00060         NULL;
00061 #endif
00062 
00063 static QString __klf_share_dir_cached;
00064 
00065 KLF_EXPORT QString klf_share_dir_abspath()
00066 {
00067   if (!__klf_share_dir_cached.isEmpty())
00068     return __klf_share_dir_cached;
00069 
00070   klfDbg(klfFmtCC("cmake-share-dir=%s; fallback-share-dir=%s\n", __klf_share_dir,
00071                   __klf_fallback_share_dir)) ;
00072 
00073   QString sharedir;
00074   if (__klf_share_dir != NULL)
00075     sharedir = QLatin1String(__klf_share_dir);
00076   else
00077     sharedir = QLatin1String(__klf_fallback_share_dir);
00078 
00079   __klf_share_dir_cached = klfPrefixedPath(sharedir); // prefixed by app-dir-path
00080   klfDbg("share dir is "<<__klf_share_dir_cached) ;
00081   return __klf_share_dir_cached;
00082 }
00083 
00084 
00085 
00086 // global variable to access our config
00087 // remember to initialize it in main() in main.cpp !
00088 KLFConfig klfconfig;
00089 
00090 
00091 
00092 /*void settings_write_QTextCharFormat(QSettings& s, const QString& basename,
00093   const QTextCharFormat& charfmt)
00094   {
00095   s.setValue(basename+"_charformat", charfmt);
00096   }
00097 */
00098 static QTextCharFormat settings_read_QTextCharFormat(QSettings& s, const QString& basename,
00099                                                      const QTextCharFormat& dflt)
00100 {
00101   QVariant val = s.value(basename+"_charformat", dflt);
00102   QTextFormat tf = val.value<QTextFormat>();
00103   return tf.toCharFormat();
00104 }
00105 /*
00106  template<class T>
00107  void settings_write_list(QSettings& s, const QString& basename, const QList<T>& list)
00108  {
00109  QList<QVariant> l;
00110  int k;
00111  for (k = 0; k < list.size(); ++k)
00112  l.append(QVariant(list[k]));
00113  s.setValue(basename+"_list", l);
00114  }
00115 */
00116 
00117 template<class T>
00118 static QList<T> settings_read_list(QSettings& s, const QString& basename, const QList<T>& dflt)
00119 {
00120   QList<QVariant> l = s.value(basename+"_list", QList<QVariant>()).toList();
00121   if (l.size() == 0)
00122     return dflt;
00123   QList<T> list;
00124   int k;
00125   for (k = 0; k < l.size(); ++k)
00126     list.append(l[k].value<T>());
00127   return list;
00128 }
00129 
00130 
00131 // -----------------------------------------------------
00132 
00133 
00134 KLFConfig::KLFConfig()
00135 {
00136 }
00137 
00138 
00139 #define KLFCONFIG_TEST_FIXED_FONT(found_fcode, fdb, fcode, f, fps)      \
00140   if (!found_fcode && fdb.isFixedPitch(f)) {                            \
00141     fcode = QFont(f, fps);                                              \
00142     found_fcode = true;                                                 \
00143   }
00144 
00145 static int adjust_font_size(QFont f, int idealMXHeightPx)
00146 {
00147   // idealMXHeightPx is the ideal height of the string "MX" in pixels.
00148   int ps = QFontInfo(f).pointSize();
00149   // start with a little bit smaller font
00150   ps -= 3;
00151   const int cutoff = 20; //just a cutoff to be sure
00152   // and increase font size up to something "ideal"
00153   while (ps < cutoff && QFontMetrics(f).size(Qt::TextSingleLine, "MX").height() < idealMXHeightPx) {
00154     f.setPointSize(++ps);
00155     klfDbg(f.family()<<": Will try instead with font size="<<ps) ;
00156   }
00157   if (ps >= cutoff)
00158     ps = 10; // the default point size in bad case scenario
00159 
00160   return ps;
00161 }
00162 
00163 
00164 void KLFConfig::loadDefaults()
00165 {
00166   KLF_DEBUG_TIME_BLOCK(KLF_FUNC_NAME) ;
00167 
00168   homeConfigDir = QDir::homePath() + "/.klatexformula";
00169   globalShareDir = klf_share_dir_abspath();
00170   homeConfigSettingsFile = homeConfigDir + "/klatexformula.conf";
00171   homeConfigSettingsFileIni = homeConfigDir + "/config";
00172   homeConfigDirRCCResources = homeConfigDir + "/rccresources";
00173   homeConfigDirPlugins = homeConfigDir + "/plugins";
00174   homeConfigDirPluginData = homeConfigDir + "/plugindata";
00175   homeConfigDirI18n = homeConfigDir + "/i18n";
00176 
00177   //debug: QMessageBox::information(0, "", QString("global share dir=")+globalShareDir);
00178 
00179   if (qApp->inherits("QApplication")) { // and not QCoreApplication...
00180 
00181     QFontDatabase fdb;
00182     QFont f = QApplication::font();
00183 
00184     //#ifdef Q_WS_X11
00185     //    // setting pixel size avoids bug with Qt/X11 of fonts having their metrics badly calculated (...?)
00186     //    f.setPixelSize(15);
00187     //#endif
00188 
00189     defaultStdFont = f;
00190 
00191     QFont cmuappfont = f;
00192     if (fdb.families().contains("CMU Sans Serif")) {
00193       // CMU Sans Serif is available ;-)
00194       int fps = QFontInfo(f).pointSize();
00195       cmuappfont = QFont("CMU Sans Serif", fps);
00196       // ideal height of the string "MX" in pixels. This value was CAREFULLY ADJUSTED.
00197       // please change it only if you feel sure. (fonts have to look nice on most platforms)
00198 #ifdef Q_WS_X11
00199       int fIdealHeight = 17;
00200 #else
00201       int fIdealHeight = 15;
00202 #endif
00203       fps = adjust_font_size(cmuappfont, fIdealHeight);
00204       cmuappfont.setPointSize(fps);
00205     }
00206 
00207     QFont fcode;
00208     bool found_fcode = false;
00209     int ps = 11;
00210     KLFCONFIG_TEST_FIXED_FONT(found_fcode, fdb, fcode, "Courier 10 Pitch", ps);
00211     KLFCONFIG_TEST_FIXED_FONT(found_fcode, fdb, fcode, "ETL Fixed", ps);
00212     KLFCONFIG_TEST_FIXED_FONT(found_fcode, fdb, fcode, "Courier New", ps);
00213     KLFCONFIG_TEST_FIXED_FONT(found_fcode, fdb, fcode, "Efont Fixed", ps);
00214     KLFCONFIG_TEST_FIXED_FONT(found_fcode, fdb, fcode, "Adobe Courier", ps);
00215     KLFCONFIG_TEST_FIXED_FONT(found_fcode, fdb, fcode, "Courier", ps);
00216     KLFCONFIG_TEST_FIXED_FONT(found_fcode, fdb, fcode, "Misc Fixed", ps);
00217     KLFCONFIG_TEST_FIXED_FONT(found_fcode, fdb, fcode, "Monospace", ps);
00218     if ( ! found_fcode )
00219       fcode = f;
00220     // guess good font size for code font
00221 #ifdef Q_WS_X11
00222     int fcodeIdealHeight = 20; // the ideal height of the string "MX" in pixels
00223 #else
00224     int fcodeIdealHeight = 18; // the ideal height of the string "MX" in pixels
00225 #endif
00226 // #if QT_VERSION < 0x040500
00227 //     fcodeIdealHeight += 2; // fix for Qt 4.4
00228 // #endif
00229     ps = adjust_font_size(fcode, fcodeIdealHeight);
00230     fcode.setPointSize(ps);
00231     QFont fcodeMain = fcode;
00232     fcodeMain.setPointSize(ps+1);
00233 
00234     // by default, this is first run!
00235     Core.thisVersionMajFirstRun = true;
00236     Core.thisVersionMajMinFirstRun = true;
00237     Core.thisVersionMajMinRelFirstRun = true;
00238     Core.thisVersionExactFirstRun = true;
00239 
00240     Core.libraryFileName = "library.klf.db";
00241     Core.libraryLibScheme = "klf+sqlite";
00242 
00243     defaultCMUFont = cmuappfont;
00244     defaultTTFont = fcode;
00245 
00246     UI.locale = QLocale::system().name();
00247     klfDbg("System locale: "<<QLocale::system().name());
00248 #ifdef KLF_NO_CMU_FONT
00249     UI.useSystemAppFont = true;
00250     UI.applicationFont = defaultStdFont;
00251 #else
00252     UI.useSystemAppFont = false;
00253     UI.applicationFont = cmuappfont;
00254 #endif
00255     UI.latexEditFont = fcodeMain;
00256     UI.preambleEditFont = fcode;
00257     UI.previewTooltipMaxSize = QSize(800, 600);
00258     UI.labelOutputFixedSize = QSize(280, 80);
00259     UI.lastSaveDir = QDir::homePath();
00260     UI.symbolsPerLine = 6;
00261     UI.userColorList = QList<QColor>();
00262     UI.userColorList.append(QColor(0,0,0));
00263     UI.userColorList.append(QColor(255,255,255));
00264     UI.userColorList.append(QColor(170,0,0));
00265     UI.userColorList.append(QColor(0,0,128));
00266     UI.userColorList.append(QColor(0,0,255));
00267     UI.userColorList.append(QColor(0,85,0));
00268     UI.userColorList.append(QColor(255,85,0));
00269     UI.userColorList.append(QColor(0,255,255));
00270     UI.userColorList.append(QColor(85,0,127));
00271     UI.userColorList.append(QColor(128,255,255));
00272     UI.colorChooseWidgetRecent = QList<QColor>();
00273     UI.colorChooseWidgetCustom = QList<QColor>();
00274     UI.maxUserColors = 12;
00275     UI.enableToolTipPreview = false;
00276     UI.enableRealTimePreview = true;
00277     UI.autosaveLibraryMin = 5;
00278     UI.showHintPopups = true;
00279     UI.clearLatexOnly = false;
00280     UI.copyExportProfile = "default";
00281     UI.dragExportProfile = "default";
00282     UI.glowEffect = false;
00283     UI.glowEffectColor = QColor(128, 255, 128, 12);
00284     UI.glowEffectRadius = 4;
00285     UI.customMathModes = QStringList();
00286     UI.showExportProfilesLabel = true;
00287     UI.menuExportProfileAffectsDrag = true;
00288     UI.menuExportProfileAffectsCopy = true;
00289 
00290     SyntaxHighlighter.configFlags = 0x05;
00291     SyntaxHighlighter.fmtKeyword = QTextCharFormat();
00292     SyntaxHighlighter.fmtKeyword.setForeground(QColor(0, 0, 128));
00293     SyntaxHighlighter.fmtComment = QTextCharFormat();
00294     SyntaxHighlighter.fmtComment.setForeground(QColor(180, 0, 0));
00295     SyntaxHighlighter.fmtComment.setFontItalic(true);
00296     SyntaxHighlighter.fmtParenMatch = QTextCharFormat();
00297     SyntaxHighlighter.fmtParenMatch.setBackground(QColor(180, 238, 180));
00298     SyntaxHighlighter.fmtParenMismatch = QTextCharFormat();
00299     SyntaxHighlighter.fmtParenMismatch.setBackground(QColor(255, 20, 147));
00300     SyntaxHighlighter.fmtLonelyParen = QTextCharFormat();
00301     SyntaxHighlighter.fmtLonelyParen.setForeground(QColor(255, 0, 255));
00302     SyntaxHighlighter.fmtLonelyParen.setFontWeight(QFont::Bold);
00303   }
00304 
00305   // invalid value, by convention ".". if the config is not read from file, then settings will
00306   // be detected in detectMissingSettings()
00307   BackendSettings.tempDir = ".";
00308   BackendSettings.execLatex = ".";
00309   BackendSettings.execDvips = ".";
00310   BackendSettings.execGs = ".";
00311   BackendSettings.execEpstopdf = ".";
00312   BackendSettings.execenv = QStringList();
00313 
00314   BackendSettings.lborderoffset = 0;
00315   BackendSettings.tborderoffset = 0;
00316   BackendSettings.rborderoffset = 0;
00317   BackendSettings.bborderoffset = 0;
00318   BackendSettings.outlineFonts = true;
00319 
00320   LibraryBrowser.colorFound = QColor(128, 255, 128);
00321   LibraryBrowser.colorNotFound = QColor(255, 128, 128);
00322   LibraryBrowser.restoreURLs = false;
00323   LibraryBrowser.confirmClose = true;
00324   LibraryBrowser.groupSubCategories = true;
00325   LibraryBrowser.iconViewFlow = QListView::TopToBottom;
00326   LibraryBrowser.historyTagCopyToArchive = true;
00327   LibraryBrowser.lastFileDialogPath = // "My Documents":
00328     QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
00329   LibraryBrowser.treePreviewSizePercent = 75;
00330   LibraryBrowser.listPreviewSizePercent = 75;
00331   LibraryBrowser.iconPreviewSizePercent = 100;
00332 
00333   Plugins.pluginConfig = QMap< QString, QMap<QString,QVariant> >();
00334 }
00335 
00336 void KLFConfig::detectMissingSettings()
00337 {
00338   int neededsettings = 
00339     !BackendSettings.tempDir.compare(".")      << 0 |
00340     !BackendSettings.execLatex.compare(".")    << 1 |
00341     !BackendSettings.execDvips.compare(".")    << 2 |
00342     !BackendSettings.execGs.compare(".")       << 3 |
00343     !BackendSettings.execEpstopdf.compare(".") << 4 ;
00344 
00345   if (neededsettings) {
00346     KLFBackend::klfSettings defaultsettings;
00347     KLFBackend::detectSettings(&defaultsettings);
00348     if (neededsettings & (1<<0))
00349       BackendSettings.tempDir = defaultsettings.tempdir;
00350     if (neededsettings & (1<<1))
00351       BackendSettings.execLatex = defaultsettings.latexexec;
00352     if (neededsettings & (1<<2))
00353       BackendSettings.execDvips = defaultsettings.dvipsexec;
00354     if (neededsettings & (1<<3))
00355       BackendSettings.execGs = defaultsettings.gsexec;
00356     if (neededsettings & (1<<4))
00357       BackendSettings.execEpstopdf = defaultsettings.epstopdfexec;
00358     BackendSettings.execenv << defaultsettings.execenv;
00359   }
00360 
00361 }
00362 
00363 
00364 
00365 int KLFConfig::ensureHomeConfigDir()
00366 {
00367   if ( !klfEnsureDir(homeConfigDir) )
00368     return -1;
00369   if ( !klfEnsureDir(homeConfigDirRCCResources) )
00370     return -1;
00371   if ( !klfEnsureDir(homeConfigDirPlugins) )
00372     return -1;
00373   if ( !klfEnsureDir(homeConfigDirPluginData) )
00374     return -1;
00375   if ( !klfEnsureDir(homeConfigDirI18n) )
00376     return -1;
00377 
00378   return 0;
00379 }
00380 
00381 
00382 bool KLFConfig::checkExePaths()
00383 {
00384   return QFileInfo(BackendSettings.execLatex).isExecutable() &&
00385     QFileInfo(BackendSettings.execDvips).isExecutable() &&
00386     QFileInfo(BackendSettings.execGs).isExecutable() &&
00387     ( BackendSettings.execEpstopdf.isEmpty() ||
00388       QFileInfo(BackendSettings.execEpstopdf).isExecutable()) ;
00389 }
00390 
00391 
00392 
00393 
00394 int KLFConfig::readFromConfig()
00395 {
00396   klfDbgT(" reading config.") ;
00397 
00398   ensureHomeConfigDir();
00399 
00400   QString globalconfig = globalShareDir+"/klatexformula.conf";
00401   klfDbg("Testing for global config file "<<globalconfig);
00402   if (QFile::exists(globalconfig)) {
00403     klfDbg("Reading configuration from "<<globalconfig);
00404     // pre-load global settings
00405     readFromConfig_v2(globalconfig);
00406   }
00407 
00408   if (QFile::exists(homeConfigSettingsFile)) {
00409     klfDbg("Reading configuration from "<<homeConfigSettingsFile<<" ...");
00410     return readFromConfig_v2(homeConfigSettingsFile);
00411   }
00412   if (QFile::exists(homeConfigSettingsFileIni)) {
00413     return readFromConfig_v1();
00414   }
00415 
00416   return -1;
00417 }
00418 
00419 template<class T>
00420 static void klf_config_read(QSettings &s, const QString& baseName, T *target,
00421                               const char * listOrMapType = NULL)
00422 {
00423   //  qDebug("klf_config_read<...>(%s)", qPrintable(baseName));
00424   QVariant defVal = QVariant::fromValue<T>(*target);
00425   QVariant valstrv = s.value(baseName, QVariant());
00426   //  klfDbg( "\tRead value "<<valstr ) ;
00427   if (valstrv.isNull()) {
00428     // no such entry in config
00429     klfDbg("No entry "<<baseName<<" in config.") ;
00430     return;
00431   }
00432   QString valstr = valstrv.toString();
00433   QVariant val = klfLoadVariantFromText(valstr.toLatin1(), defVal.typeName(), listOrMapType);
00434   if (val.isValid())
00435     *target = val.value<T>();
00436 }
00437 template<>
00438 void klf_config_read<QTextCharFormat>(QSettings &s, const QString& baseName,
00439                                         QTextCharFormat *target,
00440                                         const char * listOrMapType)
00441 {
00442   qDebug("klf_config_read<QTextCharFormat>(%s)", qPrintable(baseName));
00443   QTextFormat fmt = *target;
00444   klf_config_read(s, baseName, &fmt);
00445   *target = fmt.toCharFormat();
00446 }
00447 
00448 template<class T>
00449 static void klf_config_read_list(QSettings &s, const QString& baseName, QList<T> *target)
00450 {
00451   QVariantList vlist = klfListToVariantList(*target);
00452   klf_config_read(s, baseName, &vlist, QVariant::fromValue<T>(T()).typeName());
00453   *target = klfVariantListToList<T>(vlist);
00454 }
00455 
00456 
00457 template<class T>
00458 static void klf_config_write(QSettings &s, const QString& baseName, const T * value)
00459 {
00460   QVariant val = QVariant::fromValue<T>(*value);
00461   QByteArray datastr = klfSaveVariantToText(val);
00462   s.setValue(baseName, QVariant::fromValue<QString>(QString::fromLocal8Bit(datastr)));
00463 }
00464 
00465 template<class T>
00466 static void klf_config_write_list(QSettings &s, const QString& baseName, const QList<T> * target)
00467 {
00468   QVariantList vlist = klfListToVariantList(*target);
00469   klf_config_write(s, baseName, &vlist);
00470 }
00471 
00472 static QString firstRunConfigKey(int N)
00473 {
00474   QString s = QString("versionFirstRun-%1_").arg(N);
00475   if (N >= 4)
00476     return s + QLatin1String(KLF_VERSION_STRING);
00477 
00478   if (N-- > 0)
00479     s += QString("%1").arg(KLF_VERSION_MAJ);
00480   if (N-- > 0)
00481     s += QString(".%1").arg(KLF_VERSION_MIN);
00482   if (N-- > 0)
00483     s += QString(".%1").arg(KLF_VERSION_REL);
00484   return s;
00485 }
00486 
00487 int KLFConfig::readFromConfig_v2(const QString& fname)
00488 {
00489   KLF_DEBUG_TIME_BLOCK(KLF_FUNC_NAME) ;
00490 
00491   QSettings s(fname, QSettings::IniFormat);
00492 
00493   qDebug("Reading base configuration");
00494 
00495   s.beginGroup("Core");
00496   klf_config_read(s, firstRunConfigKey(1), &Core.thisVersionMajFirstRun);
00497   klf_config_read(s, firstRunConfigKey(2), &Core.thisVersionMajMinFirstRun);
00498   klf_config_read(s, firstRunConfigKey(3), &Core.thisVersionMajMinRelFirstRun);
00499   klf_config_read(s, firstRunConfigKey(4), &Core.thisVersionExactFirstRun);
00500   klf_config_read(s, "libraryfilename", &Core.libraryFileName);
00501   klf_config_read(s, "librarylibscheme", &Core.libraryLibScheme);
00502   s.endGroup();
00503 
00504   s.beginGroup("UI");
00505   klf_config_read(s, "locale", &UI.locale);
00506   klf_config_read(s, "usesystemfont", &UI.useSystemAppFont);
00507   klf_config_read(s, "applicationfont", &UI.applicationFont);
00508   klf_config_read(s, "latexeditfont", &UI.latexEditFont);
00509   klf_config_read(s, "preambleeditfont", &UI.preambleEditFont);
00510   klf_config_read(s, "previewtooltipmaxsize", &UI.previewTooltipMaxSize);
00511   klf_config_read(s, "lbloutputfixedsize", &UI.labelOutputFixedSize);
00512   klf_config_read(s, "lastsavedir", &UI.lastSaveDir);
00513   klf_config_read(s, "symbolsperline", &UI.symbolsPerLine);
00514   klf_config_read_list(s, "usercolorlist", &UI.userColorList);
00515   klf_config_read_list(s, "colorchoosewidgetrecent", &UI.colorChooseWidgetRecent);
00516   klf_config_read_list(s, "colorchoosewidgetcustom", &UI.colorChooseWidgetCustom);
00517   klf_config_read(s, "maxusercolors", &UI.maxUserColors);
00518   klf_config_read(s, "enabletooltippreview", &UI.enableToolTipPreview);
00519   klf_config_read(s, "enablerealtimepreview", &UI.enableRealTimePreview);
00520   klf_config_read(s, "autosavelibrarymin", &UI.autosaveLibraryMin);
00521   klf_config_read(s, "showhintpopups", &UI.showHintPopups);
00522   klf_config_read(s, "clearlatexonly", &UI.clearLatexOnly);
00523   klf_config_read(s, "copyexportprofile", &UI.copyExportProfile);
00524   klf_config_read(s, "dragexportprofile", &UI.dragExportProfile);
00525   klf_config_read(s, "gloweffect", &UI.glowEffect);
00526   klf_config_read(s, "gloweffectcolor", &UI.glowEffectColor);
00527   klfDbg("Read glow effect color from config: color="<<UI.glowEffectColor
00528          <<", alpha="<<UI.glowEffectColor.alpha());
00529   klf_config_read(s, "gloweffectradius", &UI.glowEffectRadius);
00530   klf_config_read(s, "custommathmodes", &UI.customMathModes);
00531   klf_config_read(s, "showexportprofileslabel", &UI.showExportProfilesLabel);
00532   klf_config_read(s, "menuexportprofileaffectsdrag", &UI.menuExportProfileAffectsDrag);
00533   klf_config_read(s, "menuexportprofileaffectscopy", &UI.menuExportProfileAffectsCopy);
00534   s.endGroup();
00535 
00536   s.beginGroup("SyntaxHighlighter");
00537   klf_config_read(s, "configflags", &SyntaxHighlighter.configFlags);
00538   klf_config_read<QTextCharFormat>(s, "keyword", &SyntaxHighlighter.fmtKeyword);
00539   klf_config_read<QTextCharFormat>(s, "comment", &SyntaxHighlighter.fmtComment);
00540   klf_config_read<QTextCharFormat>(s, "parenmatch", &SyntaxHighlighter.fmtParenMatch);
00541   klf_config_read<QTextCharFormat>(s, "parenmismatch", &SyntaxHighlighter.fmtParenMismatch);
00542   klf_config_read<QTextCharFormat>(s, "lonelyparen", &SyntaxHighlighter.fmtLonelyParen);
00543   s.endGroup();
00544 
00545   s.beginGroup("BackendSettings");
00546   klf_config_read(s, "tempdir", &BackendSettings.tempDir);
00547   klf_config_read(s, "latexexec", &BackendSettings.execLatex);
00548   klf_config_read(s, "dvipsexec", &BackendSettings.execDvips);
00549   klf_config_read(s, "gsexec", &BackendSettings.execGs);
00550   klf_config_read(s, "epstopdfexec", &BackendSettings.execEpstopdf);
00551   klf_config_read(s, "execenv", &BackendSettings.execenv);
00552   klf_config_read(s, "lborderoffset", &BackendSettings.lborderoffset);
00553   klf_config_read(s, "tborderoffset", &BackendSettings.tborderoffset);
00554   klf_config_read(s, "rborderoffset", &BackendSettings.rborderoffset);
00555   klf_config_read(s, "bborderoffset", &BackendSettings.bborderoffset);
00556   klf_config_read(s, "outlinefonts", &BackendSettings.outlineFonts);
00557   s.endGroup();
00558 
00559   s.beginGroup("LibraryBrowser");
00560   klf_config_read(s, "colorfound", &LibraryBrowser.colorFound);
00561   klf_config_read(s, "colornotfound", &LibraryBrowser.colorNotFound);
00562   klf_config_read(s, "restoreurls", &LibraryBrowser.restoreURLs);
00563   klf_config_read(s, "confirmclose", &LibraryBrowser.confirmClose);
00564   klf_config_read(s, "groupsubcategories", &LibraryBrowser.groupSubCategories);
00565   klf_config_read(s, "iconviewflow", &LibraryBrowser.iconViewFlow);
00566   klf_config_read(s, "historytagcopytoarchive", &LibraryBrowser.historyTagCopyToArchive);
00567   klf_config_read(s, "lastfiledialogpath", &LibraryBrowser.lastFileDialogPath);
00568   klf_config_read(s, "treepreviewsizepercent", &LibraryBrowser.treePreviewSizePercent);
00569   klf_config_read(s, "listpreviewsizepercent", &LibraryBrowser.listPreviewSizePercent);
00570   klf_config_read(s, "iconpreviewsizepercent", &LibraryBrowser.iconPreviewSizePercent);
00571   s.endGroup();
00572 
00573   // Special treatment for Plugins.pluginConfig
00574   // for reading, we cannot rely on klf_plugins since we are called before plugins are loaded!
00575   int k, j;
00576   QDir plugindatadir = QDir(homeConfigDirPluginData);
00577   QStringList plugindirs = plugindatadir.entryList(QDir::Dirs);
00578   for (k = 0; k < plugindirs.size(); ++k) {
00579     if (plugindirs[k] == "." || plugindirs[k] == "..")
00580       continue;
00581     qDebug("Reading config for plugin %s", qPrintable(plugindirs[k]));
00582     QString fn = plugindatadir.absoluteFilePath(plugindirs[k])+"/"+plugindirs[k]+".conf";
00583     if ( ! QFile::exists(fn) ) {
00584       qDebug("\tskipping plugin %s since the file %s does not exist.",
00585              qPrintable(plugindirs[k]), qPrintable(fn));
00586       continue;
00587     }
00588     QSettings psettings(fn, QSettings::IniFormat);
00589     QVariantMap pconfmap;
00590     QStringList keys = psettings.allKeys();
00591     for (j = 0; j < keys.size(); ++j) {
00592       pconfmap[keys[j]] = psettings.value(keys[j]);
00593     }
00594     Plugins.pluginConfig[plugindirs[k]] = pconfmap;
00595   }
00596 
00597   // POST-CONFIG-READ SETUP
00598 
00599   // forbid empty locale
00600   if (klfconfig.UI.locale.isEmpty())
00601     klfconfig.UI.locale = "en_US";
00602   // set Qt default locale to ours
00603   QLocale::setDefault(klfconfig.UI.locale);
00604 
00605   return 0;
00606 }
00607 
00608 
00609 int KLFConfig::writeToConfig()
00610 {
00611   ensureHomeConfigDir();
00612   QSettings s(homeConfigSettingsFile, QSettings::IniFormat);
00613 
00614   bool thisVersionFirstRunFalse = false;
00615 
00616   s.beginGroup("Core");
00617   klf_config_write(s, firstRunConfigKey(1), &thisVersionFirstRunFalse);
00618   klf_config_write(s, firstRunConfigKey(2), &thisVersionFirstRunFalse);
00619   klf_config_write(s, firstRunConfigKey(3), &thisVersionFirstRunFalse);
00620   klf_config_write(s, firstRunConfigKey(4), &thisVersionFirstRunFalse);
00621   klf_config_write(s, "libraryfilename", &Core.libraryFileName);
00622   klf_config_write(s, "librarylibscheme", &Core.libraryLibScheme);
00623   s.endGroup();
00624 
00625   s.beginGroup("UI");
00626   klf_config_write(s, "locale", &UI.locale);
00627   klf_config_write(s, "usesystemfont", &UI.useSystemAppFont);
00628   klf_config_write(s, "applicationfont", &UI.applicationFont);
00629   klf_config_write(s, "latexeditfont", &UI.latexEditFont);
00630   klf_config_write(s, "preambleeditfont", &UI.preambleEditFont);
00631   klf_config_write(s, "previewtooltipmaxsize", &UI.previewTooltipMaxSize);
00632   klf_config_write(s, "lbloutputfixedsize", &UI.labelOutputFixedSize);
00633   klf_config_write(s, "lastsavedir", &UI.lastSaveDir);
00634   klf_config_write(s, "symbolsperline", &UI.symbolsPerLine);
00635   klf_config_write_list(s, "usercolorlist", &UI.userColorList);
00636   klf_config_write_list(s, "colorchoosewidgetrecent", &UI.colorChooseWidgetRecent);
00637   klf_config_write_list(s, "colorchoosewidgetcustom", &UI.colorChooseWidgetCustom);
00638   klf_config_write(s, "maxusercolors", &UI.maxUserColors);
00639   klf_config_write(s, "enabletooltippreview", &UI.enableToolTipPreview);
00640   klf_config_write(s, "enablerealtimepreview", &UI.enableRealTimePreview);
00641   klf_config_write(s, "autosavelibrarymin", &UI.autosaveLibraryMin);
00642   klf_config_write(s, "showhintpopups", &UI.showHintPopups);
00643   klf_config_write(s, "clearlatexonly", &UI.clearLatexOnly);
00644   klf_config_write(s, "copyexportprofile", &UI.copyExportProfile);
00645   klf_config_write(s, "dragexportprofile", &UI.dragExportProfile);
00646   klf_config_write(s, "gloweffect", &UI.glowEffect);
00647   klf_config_write(s, "gloweffectcolor", &UI.glowEffectColor);
00648   klf_config_write(s, "gloweffectradius", &UI.glowEffectRadius);
00649   klf_config_write(s, "custommathmodes", &UI.customMathModes);
00650   klf_config_write(s, "showexportprofileslabel", &UI.showExportProfilesLabel);
00651   klf_config_write(s, "menuexportprofileaffectsdrag", &UI.menuExportProfileAffectsDrag);
00652   klf_config_write(s, "menuexportprofileaffectscopy", &UI.menuExportProfileAffectsCopy);
00653   s.endGroup();
00654 
00655   s.beginGroup("SyntaxHighlighter");
00656   klf_config_write(s, "configflags", &SyntaxHighlighter.configFlags);
00657   klf_config_write<QTextFormat>(s, "keyword", &SyntaxHighlighter.fmtKeyword);
00658   klf_config_write<QTextFormat>(s, "comment", &SyntaxHighlighter.fmtComment);
00659   klf_config_write<QTextFormat>(s, "parenmatch", &SyntaxHighlighter.fmtParenMatch);
00660   klf_config_write<QTextFormat>(s, "parenmismatch", &SyntaxHighlighter.fmtParenMismatch);
00661   klf_config_write<QTextFormat>(s, "lonelyparen", &SyntaxHighlighter.fmtLonelyParen);
00662   s.endGroup();
00663 
00664   s.beginGroup("BackendSettings");
00665   klf_config_write(s, "tempdir", &BackendSettings.tempDir);
00666   klf_config_write(s, "latexexec", &BackendSettings.execLatex);
00667   klf_config_write(s, "dvipsexec", &BackendSettings.execDvips);
00668   klf_config_write(s, "gsexec", &BackendSettings.execGs);
00669   klf_config_write(s, "epstopdfexec", &BackendSettings.execEpstopdf);
00670   klf_config_write(s, "execenv", &BackendSettings.execenv);
00671   klf_config_write(s, "lborderoffset", &BackendSettings.lborderoffset);
00672   klf_config_write(s, "tborderoffset", &BackendSettings.tborderoffset);
00673   klf_config_write(s, "rborderoffset", &BackendSettings.rborderoffset);
00674   klf_config_write(s, "bborderoffset", &BackendSettings.bborderoffset); 
00675   klf_config_write(s, "outlinefonts", &BackendSettings.outlineFonts);
00676   s.endGroup();
00677 
00678   s.beginGroup("LibraryBrowser");
00679   klf_config_write(s, "colorfound", &LibraryBrowser.colorFound);
00680   klf_config_write(s, "colornotfound", &LibraryBrowser.colorNotFound);
00681   klf_config_write(s, "restoreurls", &LibraryBrowser.restoreURLs);
00682   klf_config_write(s, "confirmclose", &LibraryBrowser.confirmClose);
00683   klf_config_write(s, "groupsubcategories", &LibraryBrowser.groupSubCategories);
00684   klf_config_write(s, "iconviewflow", &LibraryBrowser.iconViewFlow);
00685   klf_config_write(s, "historytagcopytoarchive", &LibraryBrowser.historyTagCopyToArchive);
00686   klf_config_write(s, "lastfiledialogpath", &LibraryBrowser.lastFileDialogPath);
00687   klf_config_write(s, "treepreviewsizepercent", &LibraryBrowser.treePreviewSizePercent);
00688   klf_config_write(s, "listpreviewsizepercent", &LibraryBrowser.listPreviewSizePercent);
00689   klf_config_write(s, "iconpreviewsizepercent", &LibraryBrowser.iconPreviewSizePercent);
00690   s.endGroup();
00691 
00692   // Special treatment for Plugins.pluginConfig
00693   int k;
00694   for (k = 0; k < klf_plugins.size(); ++k) {
00695     QString fn = homeConfigDirPluginData+"/"+klf_plugins[k].name+"/"+klf_plugins[k].name+".conf";
00696     QSettings psettings(fn, QSettings::IniFormat);
00697     QVariantMap pconfmap = Plugins.pluginConfig[klf_plugins[k].name];
00698     QVariantMap::const_iterator it;
00699     for (it = pconfmap.begin(); it != pconfmap.end(); ++it) {
00700       psettings.setValue(it.key(), it.value());
00701     }
00702     psettings.sync();
00703   }
00704 
00705   s.sync();
00706   return 0;
00707 }
00708 
00709 
00710 
00711 
00712 KLFPluginConfigAccess KLFConfig::getPluginConfigAccess(const QString& name)
00713 {
00714   return KLFPluginConfigAccess(this, name);
00715 }
00716 
00717 
00718 // --------------------------------------
00719 
00720 
00721 
00722 
00723 KLFPluginConfigAccess::KLFPluginConfigAccess()
00724 {
00725   _config = NULL;
00726   _pluginname = QString::null;
00727 }
00728 KLFPluginConfigAccess::KLFPluginConfigAccess(const KLFPluginConfigAccess& other)
00729   : _config(other._config), _pluginname(other._pluginname)
00730 {
00731 }
00732 KLFPluginConfigAccess::~KLFPluginConfigAccess()
00733 {
00734 }
00735 
00736 KLFPluginConfigAccess::KLFPluginConfigAccess(KLFConfig *configObject, const QString& pluginName)
00737 {
00738   _config = configObject;
00739   _pluginname = pluginName;
00740 }
00741 
00742 
00743 
00744 QString KLFPluginConfigAccess::homeConfigDir() const
00745 {
00746   if ( _config == NULL ) {
00747     qWarning("KLFPluginConfigAccess::homeConfigDir: Invalid Config Pointer!\n");
00748     return QString();
00749   }
00750 
00751   return _config->homeConfigDir;
00752 }
00753 
00754 QString KLFPluginConfigAccess::globalShareDir() const
00755 {
00756   if ( _config == NULL ) {
00757     qWarning("KLFPluginConfigAccess::homeConfigDir: Invalid Config Pointer!\n");
00758     return QString();
00759   }
00760 
00761   return _config->globalShareDir;
00762 }
00763 
00764 QString KLFPluginConfigAccess::tempDir() const
00765 {
00766   if ( _config == NULL ) {
00767     qWarning("KLFPluginConfigAccess::tempDir: Invalid Config Pointer!\n");
00768     return QString();
00769   }
00770 
00771   return _config->BackendSettings.tempDir;
00772 }
00773 
00774 QString KLFPluginConfigAccess::homeConfigPluginDataDir(bool createIfNeeded) const
00775 {
00776   if ( _config == NULL ) {
00777     qWarning("KLFPluginConfigAccess::homeConfigPluginDataDir: Invalid Config Pointer!\n");
00778     return QString();
00779   }
00780 
00781   QString d = _config->homeConfigDirPluginData + "/" + _pluginname;
00782   if ( createIfNeeded && ! klfEnsureDir(d) ) {
00783     qWarning("KLFPluginConfigAccess::homeConfigPluginDataDir: Can't create directory: `%s'",
00784              qPrintable(d));
00785     return QString();
00786   }
00787   return d;
00788 }
00789 
00790 QVariant KLFPluginConfigAccess::readValue(const QString& key) const
00791 {
00792   if ( _config == NULL ) {
00793     qWarning("KLFPluginConfigAccess::readValue: Invalid Config Pointer!\n");
00794     return QVariant();
00795   }
00796 
00797   if ( ! _config->Plugins.pluginConfig[_pluginname].contains(key) )
00798     return QVariant();
00799 
00800   return _config->Plugins.pluginConfig[_pluginname][key];
00801 }
00802 
00803 QVariant KLFPluginConfigAccess::makeDefaultValue(const QString& key, const QVariant& defaultValue)
00804 {
00805   if ( _config == NULL ) {
00806     qWarning("KLFPluginConfigAccess::makeDefaultValue: Invalid Config Pointer!\n");
00807     return QVariant();
00808   }
00809 
00810   if (_config->Plugins.pluginConfig[_pluginname].contains(key))
00811     return _config->Plugins.pluginConfig[_pluginname][key];
00812 
00813   // assign the value into the plugin config, and return it
00814   return ( _config->Plugins.pluginConfig[_pluginname][key] = defaultValue );
00815 }
00816 void KLFPluginConfigAccess::writeValue(const QString& key, const QVariant& value)
00817 {
00818   if ( _config == NULL ) {
00819     qWarning("KLFPluginConfigAccess::writeValue: Invalid Config Pointer!\n");
00820     return;
00821   }
00822 
00823   _config->Plugins.pluginConfig[_pluginname][key] = value;
00824 }
00825 
00826 
00827 
00828 
00829 
00830 
00831 
00832 
00833 // ----------------------
00834 
00835 
00836 
00837 
00838 
00839 
00840 
00841 
00842 
00843 // KEPT FOR COMPATIBILITY WITH OLDER VERSIONS
00844 
00845 int KLFConfig::readFromConfig_v1()
00846 {
00847   QSettings s(homeConfigSettingsFileIni, QSettings::IniFormat);
00848 
00849   s.beginGroup("UI");
00850   UI.locale = s.value("locale", UI.locale).toString();
00851   // ingnore KLF 3.1 app font setting, we have our nice CMU Sans Serif font ;-)
00852   //  UI.applicationFont = s.value("applicationfont", UI.applicationFont).value<QFont>();
00853   UI.latexEditFont = s.value("latexeditfont", UI.latexEditFont).value<QFont>();
00854   UI.preambleEditFont = s.value("preambleeditfont", UI.preambleEditFont).value<QFont>();
00855   UI.previewTooltipMaxSize = s.value("previewtooltipmaxsize", UI.previewTooltipMaxSize).toSize();
00856   UI.labelOutputFixedSize = s.value("lbloutputfixedsize", UI.labelOutputFixedSize ).toSize();
00857   UI.lastSaveDir = s.value("lastsavedir", UI.lastSaveDir).toString();
00858   UI.symbolsPerLine = s.value("symbolsperline", UI.symbolsPerLine).toInt();
00859   UI.userColorList = settings_read_list(s, "usercolorlist", UI.userColorList);
00860   UI.colorChooseWidgetRecent = settings_read_list(s, "colorchoosewidgetrecent", UI.colorChooseWidgetRecent);
00861   UI.colorChooseWidgetCustom = settings_read_list(s, "colorchoosewidgetcustom", UI.colorChooseWidgetCustom);
00862   UI.maxUserColors = s.value("maxusercolors", UI.maxUserColors).toInt();
00863   UI.enableToolTipPreview = s.value("enabletooltippreview", UI.enableToolTipPreview).toBool();
00864   UI.enableRealTimePreview = s.value("enablerealtimepreview", UI.enableRealTimePreview).toBool();
00865   UI.autosaveLibraryMin = s.value("autosavelibrarymin", UI.autosaveLibraryMin).toInt();
00866   s.endGroup();
00867 
00868   s.beginGroup("SyntaxHighlighter");
00869   SyntaxHighlighter.configFlags = s.value("configflags", SyntaxHighlighter.configFlags).toUInt();
00870   SyntaxHighlighter.fmtKeyword = settings_read_QTextCharFormat(s, "keyword", SyntaxHighlighter.fmtKeyword);
00871   SyntaxHighlighter.fmtComment = settings_read_QTextCharFormat(s, "comment", SyntaxHighlighter.fmtComment);
00872   SyntaxHighlighter.fmtParenMatch = settings_read_QTextCharFormat(s, "parenmatch", SyntaxHighlighter.fmtParenMatch);
00873   SyntaxHighlighter.fmtParenMismatch = settings_read_QTextCharFormat(s, "parenmismatch", SyntaxHighlighter.fmtParenMismatch);
00874   SyntaxHighlighter.fmtLonelyParen = settings_read_QTextCharFormat(s, "lonelyparen", SyntaxHighlighter.fmtLonelyParen);
00875   s.endGroup();
00876 
00877   s.beginGroup("BackendSettings");
00878   BackendSettings.tempDir = s.value("tempdir", BackendSettings.tempDir).toString();
00879   BackendSettings.execLatex = s.value("latexexec", BackendSettings.execLatex).toString();
00880   BackendSettings.execDvips = s.value("dvipsexec", BackendSettings.execDvips).toString();
00881   BackendSettings.execGs = s.value("gsexec", BackendSettings.execGs).toString();
00882   BackendSettings.execEpstopdf = s.value("epstopdfexec", BackendSettings.execEpstopdf).toString();
00883   BackendSettings.lborderoffset = s.value("lborderoffset", BackendSettings.lborderoffset).toInt();
00884   BackendSettings.tborderoffset = s.value("tborderoffset", BackendSettings.tborderoffset).toInt();
00885   BackendSettings.rborderoffset = s.value("rborderoffset", BackendSettings.rborderoffset).toInt();
00886   BackendSettings.bborderoffset = s.value("bborderoffset", BackendSettings.bborderoffset).toInt();
00887   s.endGroup();
00888 
00889   s.beginGroup("LibraryBrowser");
00890   LibraryBrowser.colorFound = s.value("colorfound", LibraryBrowser.colorFound).value<QColor>();
00891   LibraryBrowser.colorNotFound = s.value("colornotfound", LibraryBrowser.colorNotFound).value<QColor>();
00892   s.endGroup();
00893 
00894   // Special treatment for Plugins.pluginConfig
00895   s.beginGroup("Plugins/Config");
00896   QStringList pluginList = s.childGroups();
00897   s.endGroup();
00898   int j;
00899   for (j = 0; j < pluginList.size(); ++j) {
00900     QString name = pluginList[j];
00901     s.beginGroup( QString("Plugins/Config/%1").arg(name) );
00902     QMap<QString,QVariant> thispluginconfig;
00903     QStringList plconfkeys = s.childKeys();
00904     int k;
00905     for (k = 0; k < plconfkeys.size(); ++k) {
00906       thispluginconfig[plconfkeys[k]] = s.value(plconfkeys[k]);
00907     }
00908     klfconfig.Plugins.pluginConfig[name] = thispluginconfig;
00909     s.endGroup();
00910   }
00911 
00912   return 0;
00913 }
00914 

Generated by doxygen 1.7.3