| 1 | /* |
| 2 | * File: Preferences.cpp |
| 3 | * |
| 4 | * Author: Jacob Dekel |
| 5 | * Created on: Aug 7, 2009 |
| 6 | * |
| 7 | * Copyright (c) 2009-2013 JacobJacob Dekel |
| 8 | * $Id$ |
| 9 | * |
| 10 | * This program is free software: you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation, either version 3 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include "Preferences.h" |
| 26 | #include "HerculesStudio.h" |
| 27 | #include "SystemUtils.h" |
| 28 | #include "ConfigurationEditor.h" |
| 29 | #include "Stationery.h" |
| 30 | #include "PrinterItem.h" |
| 31 | #include "DecolationRules.h" |
| 32 | |
| 33 | #include <QDir> |
| 34 | #include <QSettings> |
| 35 | |
| 36 | #include <iostream> |
| 37 | #include <sstream> |
| 38 | #include <stdexcept> |
| 39 | #include <cstdlib> |
| 40 | |
| 41 | Preferences * Preferences::instance = NULL; |
| 42 | |
| 43 | const char * Preferences::sKeywords[] = { |
| 44 | "HerculesDir", |
| 45 | "ConfigurationDir", |
| 46 | "LogsDir" , |
| 47 | "LogTimeStamp", |
| 48 | "RegsViews", |
| 49 | "Version", |
| 50 | "LogFont", |
| 51 | "RegistersFont", |
| 52 | "PswFont", |
| 53 | "CommandFont", |
| 54 | "MipgAsGauge", |
| 55 | "PswMode", |
| 56 | "SplitLog", |
| 57 | "AutosaveLog", |
| 58 | "Theme", |
| 59 | "LogFileLines", |
| 60 | "GreenLed", |
| 61 | "Animate", |
| 62 | "DarkBackground", |
| 63 | "IplDevice", |
| 64 | "LastPrinterName", |
| 65 | "BallonDecolation", |
| 66 | "BaloonStationery"}; |
| 67 | |
| 68 | const char * Preferences::sRegsViews[] = { |
| 69 | "ViewGR32", |
| 70 | "ViewCR32", |
| 71 | "ViewFR32", |
| 72 | "ViewAR32", |
| 73 | "ViewGR64", |
| 74 | "ViewCR64", |
| 75 | "ViewFR64", |
| 76 | "ViewPsw" |
| 77 | }; |
| 78 | |
| 79 | #define PrefSet(FUNC,TOKEN) \ |
| 80 | void Preferences::FUNC(const std::string& p) \ |
| 81 | { \ |
| 82 | mSettings->setValue(sKeywords[TOKEN], p.c_str()); \ |
| 83 | } |
| 84 | |
| 85 | #define PrefSetInt(FUNC,TOKEN, TYPE) \ |
| 86 | void Preferences::FUNC(const TYPE p) \ |
| 87 | { \ |
| 88 | mSettings->setValue(sKeywords[TOKEN], p); \ |
| 89 | } |
| 90 | |
| 91 | #define PrefSetBool(FUNC,TOKEN) \ |
| 92 | void Preferences::FUNC(const bool p) \ |
| 93 | { \ |
| 94 | mSettings->setValue(sKeywords[TOKEN], p); \ |
| 95 | } |
| 96 | |
| 97 | #define Pref(FUNC,TOKEN) \ |
| 98 | std::string Preferences::FUNC() const\ |
| 99 | { \ |
| 100 | return mSettings->value(sKeywords[TOKEN]).toString().toStdString(); \ |
| 101 | } |
| 102 | |
| 103 | #define PrefInt(FUNC,TOKEN, TYPE) \ |
| 104 | int Preferences::FUNC(void) const \ |
| 105 | { \ |
| 106 | return mSettings->value(sKeywords[TOKEN]).toInt(); \ |
| 107 | } |
| 108 | |
| 109 | #define PrefBool(FUNC,TOKEN) \ |
| 110 | bool Preferences::FUNC() const\ |
| 111 | { \ |
| 112 | return mSettings->value(sKeywords[TOKEN]).toBool(); \ |
| 113 | } |
| 114 | |
| 115 | #define GenFontKey(KEY,TYPE) \ |
| 116 | const char *key = fontObjectToString(fontObject); \ |
| 117 | std::string fullKey = "fonts/"; \ |
| 118 | fullKey += key; \ |
| 119 | fullKey += TYPE; |
| 120 | |
| 121 | Preferences::Preferences() |
| 122 | { |
| 123 | mSettings = new QSettings("org.mvsdasd","HerculesStudio"); |
| 124 | if (mSettings->value("Version").toString().compare("1.4") != 0) |
| 125 | { |
| 126 | if (mSettings->value("version").toString().compare("1.3") != 0) |
| 127 | { |
| 128 | QString oldFile = QDir::homePath() + "/.config/HercStudio.pref"; |
| 129 | if (SystemUtils::fileExists(oldFile)) |
| 130 | { |
| 131 | convert(); |
| 132 | } |
| 133 | else |
| 134 | { |
| 135 | // set default values |
| 136 | setLogsDir(QDir::homePath().toStdString()+"/Desktop"); |
| 137 | setPswMode(Psw::StatusBar); |
| 138 | } |
| 139 | mSettings->setValue(sKeywords[Version],"1.3"); |
| 140 | } |
| 141 | mSettings->setValue(sKeywords[GreenLed], false); |
| 142 | mSettings->setValue(sKeywords[Animate], false); |
| 143 | mSettings->setValue(sKeywords[DarkBackground], false); |
| 144 | mSettings->setValue(sKeywords[Version],"1.4"); |
| 145 | } |
| 146 | if (logFileLines() <= 0) setLogFileLines(5000); |
| 147 | if (this->fontName(ConfigurationFontObject).size() == 0) setFontName(ConfigurationFontObject,"Monospace"); |
| 148 | } |
| 149 | |
| 150 | Preferences::~Preferences() |
| 151 | { |
| 152 | |
| 153 | } |
| 154 | |
| 155 | Preferences& Preferences::getInstance() |
| 156 | { |
| 157 | if (instance == NULL) |
| 158 | instance = new Preferences(); |
| 159 | return *instance; |
| 160 | } |
| 161 | |
| 162 | Pref(hercDir,HerculesDir) |
| 163 | Pref(configDir_,ConfigurationDir) |
| 164 | Pref(logsDir,LogsDir) |
| 165 | PrefBool(logTimestamp,LogTimestamp) |
| 166 | PrefBool(splitLog,SplitLog) |
| 167 | PrefBool(autosaveLog, AutosaveLog) |
| 168 | PrefBool(mipsAsGauge, MipsAsGauge) |
| 169 | PrefInt(logFileLines, LogFileLines, int) |
| 170 | PrefBool(greenLed, GreenLed) |
| 171 | PrefBool(animate, Animate) |
| 172 | PrefBool(darkBackground, DarkBackground) |
| 173 | Pref(iplDevice,IplDevice) |
| 174 | Pref(lastPrinterName, LastPrinterName) |
| 175 | PrefBool(balloonDecolation, BalloonDecolation) |
| 176 | PrefBool(balloonStationery, BalloonStationery) |
| 177 | |
| 178 | PrefSet(setHercDir,HerculesDir) |
| 179 | PrefSet(setConfigDir,ConfigurationDir) |
| 180 | PrefSet(setLogsDir,LogsDir) |
| 181 | PrefSetInt(setPswMode, PswMode, Psw::PswMode) |
| 182 | PrefSetBool(setLogTimestamp, LogTimestamp) |
| 183 | PrefSetBool(setSplitLog, SplitLog) |
| 184 | PrefSetBool(setAutosaveLog,AutosaveLog) |
| 185 | PrefSetBool(setMipsAsGauge,MipsAsGauge) |
| 186 | PrefSetInt(setLogFileLines,LogFileLines,int) |
| 187 | PrefSetBool(setGreenLed, GreenLed) |
| 188 | PrefSetBool(setAnimate, Animate) |
| 189 | PrefSetBool(setDarkBackground, DarkBackground) |
| 190 | PrefSet(setIplDevice, IplDevice) |
| 191 | PrefSet(setLastPrinterName, LastPrinterName) |
| 192 | PrefSetBool(setBalloonDecolation, BalloonDecolation) |
| 193 | PrefSetBool(setBalloonStationery, BalloonStationery) |
| 194 | |
| 195 | std::string Preferences::configDir() const |
| 196 | { |
| 197 | if (mVolatileConfigDir =="") |
| 198 | return configDir_(); |
| 199 | else |
| 200 | return mVolatileConfigDir; |
| 201 | } |
| 202 | |
| 203 | |
| 204 | void Preferences::setVolatileConfigDir(const std::string& configDir) |
| 205 | { |
| 206 | mVolatileConfigDir = configDir; |
| 207 | } |
| 208 | |
| 209 | Psw::PswMode Preferences::pswMode() const |
| 210 | { |
| 211 | return static_cast<Psw::PswMode>((mSettings->value(sKeywords[PswMode]).toInt())); |
| 212 | } |
| 213 | |
| 214 | void Preferences::setRegs(Views v, bool view) |
| 215 | { |
| 216 | std::string key = "registers/"; |
| 217 | key += sRegsViews[v]; |
| 218 | mSettings->setValue(key.c_str(),view); |
| 219 | } |
| 220 | |
| 221 | bool Preferences::regs(Views v) const |
| 222 | { |
| 223 | std::string key = "registers/"; |
| 224 | key += sRegsViews[v]; |
| 225 | return mSettings->value(key.c_str()).toBool(); |
| 226 | } |
| 227 | |
| 228 | void Preferences::setFontName(FontObject fontObject, const std::string& fontName) |
| 229 | { |
| 230 | GenFontKey(key,"/Name") |
| 231 | mSettings->setValue(fullKey.c_str(),fontName.c_str()); |
| 232 | } |
| 233 | std::string Preferences::fontName(FontObject fontObject) const |
| 234 | { |
| 235 | GenFontKey(key,"/Name") |
| 236 | return mSettings->value(fullKey.c_str()).toString().toStdString(); |
| 237 | } |
| 238 | |
| 239 | void Preferences::setFontSize(FontObject fontObject, int fontSize) |
| 240 | { |
| 241 | GenFontKey(key,"/Size"); |
| 242 | mSettings->setValue(fullKey.c_str(),fontSize); |
| 243 | } |
| 244 | |
| 245 | int Preferences::fontSize(FontObject fontObject) const |
| 246 | { |
| 247 | GenFontKey(key,"/Size"); |
| 248 | if (mSettings->contains(fullKey.c_str())) |
| 249 | return mSettings->value(fullKey.c_str()).toInt(); |
| 250 | else |
| 251 | return 9; |
| 252 | } |
| 253 | |
| 254 | void Preferences::setBold(FontObject fontObject, bool bold) |
| 255 | { |
| 256 | GenFontKey(key,"/Bold"); |
| 257 | mSettings->setValue(fullKey.c_str(),bold); |
| 258 | } |
| 259 | |
| 260 | bool Preferences::fontIsBold(FontObject fontObject) const |
| 261 | { |
| 262 | GenFontKey(key,"/Bold"); |
| 263 | return mSettings->value(fullKey.c_str()).toBool(); |
| 264 | } |
| 265 | |
| 266 | void Preferences::setItalic(FontObject fontObject, bool italic) |
| 267 | { |
| 268 | GenFontKey(key,"/Italic"); |
| 269 | mSettings->setValue(fullKey.c_str(),italic); |
| 270 | } |
| 271 | |
| 272 | bool Preferences::fontIsItalic(FontObject fontObject) const |
| 273 | { |
| 274 | GenFontKey(key,"/Italic"); |
| 275 | return mSettings->value(fullKey.c_str()).toBool(); |
| 276 | } |
| 277 | |
| 278 | const char * Preferences::fontObjectToString(FontObject fontObject) const |
| 279 | { |
| 280 | switch(fontObject) |
| 281 | { |
| 282 | case RegsFontObject: return "RegsFont"; break; |
| 283 | case PswFontObject: return "PswFont"; break; |
| 284 | case CommandFontObject: return "CommandFont"; break; |
| 285 | case ConfigurationFontObject: return "ConfigurationFont"; break; |
| 286 | default: return "LogFont"; break; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | // for backward compatibility |
| 291 | void Preferences::write() |
| 292 | { |
| 293 | |
| 294 | } |
| 295 | |
| 296 | Preferences::Themes Preferences::theme() const |
| 297 | { |
| 298 | switch (mSettings->value(sKeywords[Theme]).toInt()) |
| 299 | { |
| 300 | case 1: |
| 301 | return Modern; |
| 302 | break; |
| 303 | default: |
| 304 | return Classic; |
| 305 | break; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | void Preferences::setTheme(Themes theme) |
| 310 | { |
| 311 | mSettings->setValue(sKeywords[Theme],theme); |
| 312 | } |
| 313 | |
| 314 | bool Preferences::setStationery(const QString& name, const QString& stationeryString, bool replace) |
| 315 | { |
| 316 | QString key = "Stationery/" + name; |
| 317 | if (stationeryString.length() != 0 || replace) |
| 318 | { |
| 319 | mSettings->setValue(key, stationeryString); |
| 320 | |
| 321 | return true; |
| 322 | } |
| 323 | else |
| 324 | { |
| 325 | return false; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | bool Preferences::setStationery(const QString& name, const Stationery& stationery, bool) |
| 330 | { |
| 331 | mSettings->beginGroup("Stationery"); |
| 332 | mSettings->beginGroup(name); |
| 333 | mSettings->setValue("Name", stationery.mName); |
| 334 | mSettings->setValue("PaperSize", stationery.mPaperSize); |
| 335 | mSettings->setValue("Size1", stationery.mSize1); |
| 336 | mSettings->setValue("Size2", stationery.mSize2); |
| 337 | mSettings->setValue("TopMargin", stationery.mTopMargin); |
| 338 | mSettings->setValue("Milimeters", stationery.mMillimeters); |
| 339 | mSettings->setValue("Portrait", stationery.mPortrait); |
| 340 | mSettings->setValue("BarsColor", stationery.mBarsColor); |
| 341 | mSettings->setValue("BarsRGB", stationery.mBarsRGB); |
| 342 | mSettings->setValue("Decorated", stationery.mDecorated); |
| 343 | mSettings->setValue("TractorFeedHols", stationery.mTractorFeedHoles); |
| 344 | mSettings->endGroup(); |
| 345 | mSettings->endGroup(); |
| 346 | return true; |
| 347 | } |
| 348 | |
| 349 | StationeryPtr Preferences::stationery(QString name) |
| 350 | { |
| 351 | |
| 352 | mSettings->beginGroup("Stationery"); |
| 353 | mSettings->beginGroup(name); |
| 354 | |
| 355 | StationeryPtr ret(new Stationery( |
| 356 | mSettings->value("Name").toString(), |
| 357 | mSettings->value("PaperSize").toString(), |
| 358 | mSettings->value("Size1").toString(), |
| 359 | mSettings->value("Size2").toString(), |
| 360 | mSettings->value("TopMargin").toInt(), |
| 361 | mSettings->value("Milimeters").toBool(), |
| 362 | mSettings->value("Portrait").toBool(), |
| 363 | mSettings->value("BarsColor").toString(), |
| 364 | mSettings->value("BarsRGB").toString(), |
| 365 | mSettings->value("Decorated").toBool(), |
| 366 | mSettings->value("TractorFeedHols").toBool())); |
| 367 | |
| 368 | |
| 369 | mSettings->endGroup(); |
| 370 | mSettings->endGroup(); |
| 371 | return ret; |
| 372 | } |
| 373 | |
| 374 | QStringList Preferences::stationeryList() |
| 375 | { |
| 376 | defaultStationery(); |
| 377 | mSettings->beginGroup("Stationery"); |
| 378 | QStringList keys = mSettings->childGroups(); |
| 379 | for (int i = 0; i < keys.size(); ++i) |
| 380 | { |
| 381 | hOutDebug(3, "key=" << keys[i].toStdString()); |
| 382 | } |
| 383 | mSettings->endGroup(); |
| 384 | return keys; |
| 385 | } |
| 386 | |
| 387 | void Preferences::deleteStationery(const QString& item) |
| 388 | { |
| 389 | mSettings->beginGroup("Stationery"); |
| 390 | mSettings->remove(item); |
| 391 | mSettings->endGroup(); |
| 392 | } |
| 393 | |
| 394 | bool Preferences::setPrinter(const QString& name, const PrinterItem& printerItem, bool) |
| 395 | { |
| 396 | mSettings->beginGroup("Printers"); |
| 397 | mSettings->beginGroup(name); |
| 398 | mSettings->setValue("Name", printerItem.mName); |
| 399 | mSettings->setValue("Ip", printerItem.mIp); |
| 400 | mSettings->setValue("Port", printerItem.mPort); |
| 401 | mSettings->setValue("Stationery", printerItem.mStationery); |
| 402 | mSettings->setValue("Lines-Per-Inch", printerItem.mLinesPerInch); |
| 403 | mSettings->setValue("Font-Size", printerItem.mFontSize); |
| 404 | mSettings->setValue("Spool-Directory", printerItem.mSpoolDir); |
| 405 | mSettings->setValue("Decolation-Name", printerItem.mDecolationName); |
| 406 | mSettings->endGroup(); |
| 407 | mSettings->endGroup(); |
| 408 | return true; |
| 409 | } |
| 410 | |
| 411 | PrinterItemPtr Preferences::printer(QString name) |
| 412 | { |
| 413 | PrinterItemPtr ret(new PrinterItem()); //TODO : proper constructor |
| 414 | mSettings->beginGroup("Printers"); |
| 415 | mSettings->beginGroup(name); |
| 416 | if (name.length() > 0 && (mSettings->value("Name").toString().compare(name) == 0)) |
| 417 | { |
| 418 | ret->mName = mSettings->value("Name").toString(); |
| 419 | ret->mIp = mSettings->value("Ip").toString(); |
| 420 | ret->mPort = mSettings->value("Port").toInt(); |
| 421 | ret->mStationery = mSettings->value("Stationery").toString(); |
| 422 | ret->mLinesPerInch = mSettings->value("Lines-Per-Inch").toInt(); |
| 423 | ret->mFontSize = mSettings->value("Font-Size").toInt(); |
| 424 | ret->mSpoolDir = mSettings->value("Spool-Directory").toString(); |
| 425 | ret->mDecolationName = mSettings->value("Decolation-Name").toString(); |
| 426 | } |
| 427 | mSettings->endGroup(); |
| 428 | mSettings->endGroup(); |
| 429 | return ret; |
| 430 | } |
| 431 | |
| 432 | QStringList Preferences::printerList() |
| 433 | { |
| 434 | mSettings->beginGroup("Printers"); |
| 435 | QStringList keys = mSettings->childGroups(); |
| 436 | for (int i = 0; i < keys.size(); ++i) |
| 437 | { |
| 438 | hOutDebug(2, "printer key=" << keys[i].toStdString()); |
| 439 | } |
| 440 | mSettings->endGroup(); |
| 441 | return keys; |
| 442 | } |
| 443 | |
| 444 | void Preferences::deletePrinter(const QString& item) |
| 445 | { |
| 446 | mSettings->beginGroup("Printers"); |
| 447 | mSettings->remove(item); |
| 448 | mSettings->endGroup(); |
| 449 | } |
| 450 | |
| 451 | QStringList Preferences::decolationList() |
| 452 | { |
| 453 | defaultDecolations(); |
| 454 | mSettings->beginGroup("Decolations"); |
| 455 | QStringList groups = mSettings->childGroups(); |
| 456 | for (int i = 0; i < groups.size(); ++i) |
| 457 | { |
| 458 | hOutDebug(3, "key=" << groups[i].toStdString()); |
| 459 | } |
| 460 | mSettings->endGroup(); |
| 461 | return groups; |
| 462 | } |
| 463 | |
| 464 | void Preferences::defaultStationery() |
| 465 | { |
| 466 | mSettings->beginGroup("Stationery"); |
| 467 | mSettings->remove("Legacy Green"); |
| 468 | mSettings->setValue("Legacy Green/BarsColor","Green"); |
| 469 | mSettings->setValue("Legacy Green/BarsRGB","#ddffdd"); |
| 470 | mSettings->setValue("Legacy Green/Decorated","true"); |
| 471 | mSettings->setValue("Legacy Green/Milimeters","false"); |
| 472 | mSettings->setValue("Legacy Green/Name","Legacy Green"); |
| 473 | mSettings->setValue("Legacy Green/PaperSize","Std FanFold"); |
| 474 | mSettings->setValue("Legacy Green/Portrait","false"); |
| 475 | mSettings->setValue("Legacy Green/Size1","11"); |
| 476 | mSettings->setValue("Legacy Green/Size2","14.875"); |
| 477 | mSettings->setValue("Legacy Green/TopMargin",8); |
| 478 | mSettings->setValue("Legacy Green/TractorFeedHols","true"); |
| 479 | mSettings->endGroup(); |
| 480 | |
| 481 | } |
| 482 | |
| 483 | void Preferences::defaultDecolations() |
| 484 | { |
| 485 | mSettings->beginGroup("Decolations"); |
| 486 | mSettings->remove("mvs38"); |
| 487 | mSettings->setValue("mvs38/Accounting/Date/Column","68"); |
| 488 | mSettings->setValue("mvs38/Accounting/Date/Length","21"); |
| 489 | mSettings->setValue("mvs38/Accounting/Date/Line","30"); |
| 490 | mSettings->setValue("mvs38/Accounting/Jobid/Column","19"); |
| 491 | mSettings->setValue("mvs38/Accounting/Jobid/Length","4"); |
| 492 | mSettings->setValue("mvs38/Accounting/Jobid/Line","30"); |
| 493 | mSettings->setValue("mvs38/Accounting/Jobname/Column","25"); |
| 494 | mSettings->setValue("mvs38/Accounting/Jobname/Length","8"); |
| 495 | mSettings->setValue("mvs38/Accounting/Jobname/Line","30"); |
| 496 | mSettings->setValue("mvs38/FileNameTemplate","$Jobname$.pdf"); |
| 497 | mSettings->setValue("mvs38/Seperator/Pages","1"); |
| 498 | mSettings->setValue("mvs38/Seperator/Triggers/Trigger-0/Column","1"); |
| 499 | mSettings->setValue("mvs38/Seperator/Triggers/Trigger-0/Line","30"); |
| 500 | mSettings->setValue("mvs38/Seperator/Triggers/Trigger-0/Text","****"); |
| 501 | mSettings->setValue("mvs38/Seperator/Triggers/Trigger-0/Type","String"); |
| 502 | mSettings->setValue("mvs38/Seperator/Triggers/Trigger-1/Column","9"); |
| 503 | mSettings->setValue("mvs38/Seperator/Triggers/Trigger-1/Line","30"); |
| 504 | mSettings->setValue("mvs38/Seperator/Triggers/Trigger-1/Text","END"); |
| 505 | mSettings->setValue("mvs38/Seperator/Triggers/Trigger-1/Type","String"); |
| 506 | mSettings->setValue("mvs38/Seperator/Triggers/Trigger-2/Column","1"); |
| 507 | mSettings->setValue("mvs38/Seperator/Triggers/Trigger-2/Line","31"); |
| 508 | mSettings->setValue("mvs38/Seperator/Triggers/Trigger-2/Text","****"); |
| 509 | mSettings->setValue("mvs38/Seperator/Triggers/Trigger-2/Type","String"); |
| 510 | mSettings->setValue("mvs38/Seperator/Triggers/Trigger-3/Column","9"); |
| 511 | mSettings->setValue("mvs38/Seperator/Triggers/Trigger-3/Line","31"); |
| 512 | mSettings->setValue("mvs38/Seperator/Triggers/Trigger-3/Text","END"); |
| 513 | mSettings->setValue("mvs38/Seperator/Triggers/Trigger-3/Type","String"); |
| 514 | mSettings->setValue("mvs38/Seperator/Triggers/Trigger-4/Column","1"); |
| 515 | mSettings->setValue("mvs38/Seperator/Triggers/Trigger-4/Line","32"); |
| 516 | mSettings->setValue("mvs38/Seperator/Triggers/Trigger-4/Text","****"); |
| 517 | mSettings->setValue("mvs38/Seperator/Triggers/Trigger-4/Type","String"); |
| 518 | mSettings->setValue("mvs38/Seperator/Triggers/Trigger-5/Column","9"); |
| 519 | mSettings->setValue("mvs38/Seperator/Triggers/Trigger-5/Line","32"); |
| 520 | mSettings->setValue("mvs38/Seperator/Triggers/Trigger-5/Text","END"); |
| 521 | mSettings->setValue("mvs38/Seperator/Triggers/Trigger-5/Type","String"); |
| 522 | mSettings->remove("mvs-legacy"); |
| 523 | mSettings->setValue("mvs-legacy/Accounting/date/Column","18"); |
| 524 | mSettings->setValue("mvs-legacy/Accounting/date/Length","11"); |
| 525 | mSettings->setValue("mvs-legacy/Accounting/date/Line","51"); |
| 526 | mSettings->setValue("mvs-legacy/Accounting/jobid/Column","18"); |
| 527 | mSettings->setValue("mvs-legacy/Accounting/jobid/Length","8"); |
| 528 | mSettings->setValue("mvs-legacy/Accounting/jobid/Line","33"); |
| 529 | mSettings->setValue("mvs-legacy/Accounting/jobname/Column","18"); |
| 530 | mSettings->setValue("mvs-legacy/Accounting/jobname/Length","8"); |
| 531 | mSettings->setValue("mvs-legacy/Accounting/jobname/Line","34"); |
| 532 | mSettings->setValue("mvs-legacy/Accounting/userid/Column","18"); |
| 533 | mSettings->setValue("mvs-legacy/Accounting/userid/Length","8"); |
| 534 | mSettings->setValue("mvs-legacy/Accounting/userid/Line","35"); |
| 535 | mSettings->setValue("mvs-legacy/FileNameTemplate","$date$/$jobname$-$jobid$.pdf"); |
| 536 | mSettings->setValue("mvs-legacy/Seperator/Pages","2"); |
| 537 | mSettings->setValue("mvs-legacy/Seperator/Triggers/Trigger-0/Column","1"); |
| 538 | mSettings->setValue("mvs-legacy/Seperator/Triggers/Trigger-0/Line","35"); |
| 539 | mSettings->setValue("mvs-legacy/Seperator/Triggers/Trigger-0/Text","* USER ID:"); |
| 540 | mSettings->setValue("mvs-legacy/Seperator/Triggers/Trigger-0/Type","String"); |
| 541 | mSettings->setValue("mvs-legacy/Seperator/Triggers/Trigger-1/Column","1"); |
| 542 | mSettings->setValue("mvs-legacy/Seperator/Triggers/Trigger-1/Line","34"); |
| 543 | mSettings->setValue("mvs-legacy/Seperator/Triggers/Trigger-1/Text","* JOB NAME:"); |
| 544 | mSettings->setValue("mvs-legacy/Seperator/Triggers/Trigger-1/Type","String"); |
| 545 | mSettings->setValue("mvs-legacy/Seperator/Triggers/Trigger-2/Column","1"); |
| 546 | mSettings->setValue("mvs-legacy/Seperator/Triggers/Trigger-2/Line","33"); |
| 547 | mSettings->setValue("mvs-legacy/Seperator/Triggers/Trigger-2/Text","* JOBID:"); |
| 548 | mSettings->setValue("mvs-legacy/Seperator/Triggers/Trigger-2/Type","String"); |
| 549 | mSettings->setValue("mvs-legacy/Seperator/Triggers/Trigger-3/Column","1"); |
| 550 | mSettings->setValue("mvs-legacy/Seperator/Triggers/Trigger-3/Line","31"); |
| 551 | mSettings->setValue("mvs-legacy/Seperator/Triggers/Trigger-3/Text","**END*******END"); |
| 552 | mSettings->setValue("mvs-legacy/Seperator/Triggers/Trigger-3/Type","String"); |
| 553 | mSettings->endGroup(); |
| 554 | } |
| 555 | |
| 556 | void Preferences::deleteDecolation(const QString& item) |
| 557 | { |
| 558 | mSettings->beginGroup("Decolations"); |
| 559 | mSettings->remove(item); |
| 560 | QStringList groups = mSettings->childGroups(); |
| 561 | hOutDebug(0,"left:" << groups.count()); |
| 562 | mSettings->endGroup(); |
| 563 | } |
| 564 | |
| 565 | DecolationRules * Preferences::decolation(QString name) |
| 566 | { |
| 567 | DecolationRules * decolation = new DecolationRules(); |
| 568 | decolation->setName(name); |
| 569 | mSettings->beginGroup("Decolations"); |
| 570 | mSettings->beginGroup(name); |
| 571 | |
| 572 | decolation->setFileNameTemplate(mSettings->value("FileNameTemplate").toString()); |
| 573 | |
| 574 | mSettings->beginGroup("Seperator"); |
| 575 | decolation->mSeperator.mPages = mSettings->value("Pages").toInt(); |
| 576 | mSettings->beginGroup("Triggers"); |
| 577 | |
| 578 | hOutDebug(5, "Triggers count when getting decolation: " << mSettings->allKeys().count()); |
| 579 | |
| 580 | for (int i=0; i<mSettings->childGroups().count(); i++) |
| 581 | { |
| 582 | mSettings->beginGroup(QString("Trigger-") + QString::number(i)); |
| 583 | hOutDebug(5,"keys:" << mSettings->allKeys().count()); |
| 584 | Trigger t; |
| 585 | t.setLine(mSettings->value("Line").toInt()); |
| 586 | t.setColumn(mSettings->value("Column").toInt()); |
| 587 | t.setTriggerType(Trigger::encodeTriggerType(mSettings->value("Type").toString())); //TODO |
| 588 | t.setText(mSettings->value("Text").toString()); |
| 589 | decolation->mSeperator.mTriggers.insert(i,t); |
| 590 | mSettings->endGroup(); // one trigger |
| 591 | } |
| 592 | mSettings->endGroup(); //triggers |
| 593 | mSettings->endGroup(); //seperator |
| 594 | |
| 595 | mSettings->beginGroup("Accounting"); |
| 596 | |
| 597 | for (int i=0; i<mSettings->childGroups().count(); i++) |
| 598 | { |
| 599 | QString group = mSettings->childGroups().at(i); |
| 600 | mSettings->beginGroup(group); |
| 601 | hOutDebug(5,"group:" << group.toStdString() << " keys:" << mSettings->allKeys().count()); |
| 602 | AccountingField field; |
| 603 | field.mName = group; |
| 604 | field.mLine = mSettings->value("Line").toInt(); |
| 605 | field.mColumn = mSettings->value("Column").toInt(); |
| 606 | field.mLength = mSettings->value("Length").toInt(); |
| 607 | decolation->mAccounting.mFields.push_back(field); |
| 608 | mSettings->endGroup(); |
| 609 | } |
| 610 | mSettings->endGroup(); //Accounting |
| 611 | mSettings->endGroup(); //decolation name |
| 612 | mSettings->endGroup(); //Decolations |
| 613 | |
| 614 | return decolation; |
| 615 | } |
| 616 | |
| 617 | void Preferences::setDecolation(DecolationRules &decolation) |
| 618 | { |
| 619 | mSettings->beginGroup("Decolations"); |
| 620 | mSettings->beginGroup(decolation.name()); |
| 621 | mSettings->remove(""); |
| 622 | |
| 623 | mSettings->setValue("FileNameTemplate", decolation.filenameTemplate()); |
| 624 | |
| 625 | mSettings->beginGroup("Seperator"); |
| 626 | mSettings->setValue("Pages", decolation.mSeperator.mPages); |
| 627 | mSettings->beginGroup("Triggers"); |
| 628 | for (int i=0; i<decolation.mSeperator.mTriggers.count(); i++) |
| 629 | { |
| 630 | mSettings->beginGroup(QString("Trigger-") + QString::number(i)); |
| 631 | mSettings->setValue("Line", decolation.mSeperator.mTriggers[i].line()); |
| 632 | mSettings->setValue("Column", decolation.mSeperator.mTriggers[i].column()); |
| 633 | mSettings->setValue("Type", Trigger::decodeTriggerType(decolation.mSeperator.mTriggers[i].triggerType())); |
| 634 | mSettings->setValue("Text", decolation.mSeperator.mTriggers[i].text()); |
| 635 | mSettings->endGroup(); // one trigger |
| 636 | } |
| 637 | mSettings->endGroup(); //triggers |
| 638 | mSettings->endGroup(); //seperator |
| 639 | |
| 640 | mSettings->beginGroup("Accounting"); |
| 641 | for (int i=0; i<decolation.mAccounting.mFields.count(); i++) |
| 642 | { |
| 643 | mSettings->beginGroup(decolation.mAccounting.mFields[i].mName); |
| 644 | mSettings->setValue("Line", decolation.mAccounting.mFields[i].mLine); |
| 645 | mSettings->setValue("Column", decolation.mAccounting.mFields[i].mColumn); |
| 646 | mSettings->setValue("Length", decolation.mAccounting.mFields[i].mLength); |
| 647 | mSettings->endGroup(); |
| 648 | } |
| 649 | mSettings->endGroup(); //Accounting |
| 650 | mSettings->endGroup(); //decolation name |
| 651 | mSettings->endGroup(); //Decolations |
| 652 | } |
| 653 | |
| 654 | void Preferences::convert() |
| 655 | { |
| 656 | hOutDebug(2,"converting old preferences"); |
| 657 | Preferences_1_2_0& oldPreferences = Preferences_1_2_0::getInstance(); |
| 658 | setHercDir(oldPreferences.hercDir()); |
| 659 | setConfigDir(oldPreferences.configDir()); |
| 660 | setLogsDir(oldPreferences.logsDir()); |
| 661 | for (FontObject f=LogFontObject; f<CommandFontObject; f=static_cast<FontObject>(f+1) ) |
| 662 | { |
| 663 | setFontName(f,oldPreferences.fontName(static_cast<Preferences_1_2_0::FontObject>(f))); |
| 664 | setFontSize(f,oldPreferences.fontSize(static_cast<Preferences_1_2_0::FontObject>(f))); |
| 665 | setBold(f,oldPreferences.fontIsBold(static_cast<Preferences_1_2_0::FontObject>(f))); |
| 666 | setItalic(f,oldPreferences.fontIsItalic(static_cast<Preferences_1_2_0::FontObject>(f))); |
| 667 | } |
| 668 | setLogTimestamp(oldPreferences.logTimestamp()); |
| 669 | setRegs(ViewGR32,oldPreferences.regs(Preferences_1_2_0::ViewGR32)); |
| 670 | setRegs(ViewCR32,oldPreferences.regs(Preferences_1_2_0::ViewCR32)); |
| 671 | setRegs(ViewFR32,oldPreferences.regs(Preferences_1_2_0::ViewFR32)); |
| 672 | setRegs(ViewAR32,oldPreferences.regs(Preferences_1_2_0::ViewAR32)); |
| 673 | setRegs(ViewGR64,oldPreferences.regs(Preferences_1_2_0::ViewGR64)); |
| 674 | setRegs(ViewCR64,oldPreferences.regs(Preferences_1_2_0::ViewCR64)); |
| 675 | setRegs(ViewFR64,oldPreferences.regs(Preferences_1_2_0::ViewFR64)); |
| 676 | setRegs(ViewPsw,oldPreferences.regs(Preferences_1_2_0::ViewPsw)); |
| 677 | setMipsAsGauge(oldPreferences.mipsAsGauge()); |
| 678 | setPswMode(oldPreferences.pswMode()); |
| 679 | setSplitLog(false); |
| 680 | setAutosaveLog(oldPreferences.autosaveLog()); |
| 681 | setTheme(Classic); |
| 682 | setFontName(ConfigurationFontObject,"Mono"); |
| 683 | } |
| 684 | |
| 685 | void Preferences::setHistory(QVector<QString> &history) |
| 686 | { |
| 687 | QList<QString> list; |
| 688 | for (int i=0; i<history.size(); i++) |
| 689 | list.append(history[i]); |
| 690 | |
| 691 | mSettings->setValue(QString("History"),QVariant(list)); |
| 692 | } |
| 693 | |
| 694 | void Preferences::getHistory(QVector<QString> &history) |
| 695 | { |
| 696 | QList<QVariant> list; |
| 697 | list = mSettings->value("History").toList(); |
| 698 | for (int i=0; i<list.size(); i++) |
| 699 | history.append(list[i].toString()); |
| 700 | |
| 701 | } |
| 702 | |
| 703 | ////////////////////////////////////////////////////////////////////// |
| 704 | #include "FontPreferences.h" |
| 705 | |
| 706 | Preferences_1_2_0 * Preferences_1_2_0::instance = NULL; |
| 707 | |
| 708 | const char Preferences_1_2_0::cFontLog[] = "LogFont="; |
| 709 | const char Preferences_1_2_0::cFontRegs[] = "RegistersFont="; |
| 710 | const char Preferences_1_2_0::cFontPsw[] = "PswFont="; |
| 711 | const char Preferences_1_2_0::cFontCommand[] = "CommandFont="; |
| 712 | const char Preferences_1_2_0::cMipsAsGauge[] = "MipgAsGauge="; |
| 713 | const char Preferences_1_2_0::cPswMode[] = "PswMode="; |
| 714 | const char Preferences_1_2_0::cSplitLog[] = "SplitLog="; |
| 715 | const char Preferences_1_2_0::cAutosaveLog[] = "AutosaveLog="; |
| 716 | |
| 717 | const char * Preferences_1_2_0::sKeywords[] = { |
| 718 | "HerculesDir=", |
| 719 | "ConfigurationDir=", |
| 720 | "LogsDir=" , |
| 721 | "Font=", |
| 722 | "FontSize=", |
| 723 | "FontVariation=", |
| 724 | "LogTimeStamp=", |
| 725 | "RegsViews=", |
| 726 | "Version=", |
| 727 | cFontLog, |
| 728 | cFontRegs, |
| 729 | cFontPsw, |
| 730 | cFontCommand, |
| 731 | cMipsAsGauge, |
| 732 | cPswMode, |
| 733 | cSplitLog, |
| 734 | cAutosaveLog}; |
| 735 | const char * Preferences_1_2_0::sFileName = "HercStudio.pref"; |
| 736 | |
| 737 | Preferences_1_2_0::Preferences_1_2_0() : |
| 738 | mPath(""), |
| 739 | mVolatileConfigDir("") |
| 740 | { |
| 741 | assertConfDir(); |
| 742 | for (unsigned int i=0; i< NumberOfLines ; i++) |
| 743 | { |
| 744 | mPrefs.push_back(""); |
| 745 | } |
| 746 | mPath = QDir::homePath() + "/.config"; |
| 747 | setHercDir(""); |
| 748 | setConfigDir(""); |
| 749 | setLogsDir(QDir::homePath().toStdString()+"/Desktop"); |
| 750 | setFontName(LogFontObject, "(system default)"); |
| 751 | setFontSize(LogFontObject,9); |
| 752 | setPswMode(Psw::StatusBar); |
| 753 | readPref(); |
| 754 | } |
| 755 | |
| 756 | Preferences_1_2_0::~Preferences_1_2_0() |
| 757 | { |
| 758 | |
| 759 | } |
| 760 | |
| 761 | Preferences_1_2_0& Preferences_1_2_0::getInstance() |
| 762 | { |
| 763 | if (instance == NULL) |
| 764 | instance = new Preferences_1_2_0(); |
| 765 | return *instance; |
| 766 | } |
| 767 | |
| 768 | void Preferences_1_2_0::readPref() |
| 769 | { |
| 770 | QFile file(getFileName()); |
| 771 | file.open(QIODevice::ReadOnly); |
| 772 | |
| 773 | for (unsigned int i=0; i<NumberOfLines; i++) |
| 774 | { |
| 775 | char line[PREF_LINE_LENGTH]; |
| 776 | if ( file.readLine(line, PREF_LINE_LENGTH) == -1 ) |
| 777 | break; |
| 778 | if (strlen(line) < 2) // skip null/blank lines |
| 779 | { |
| 780 | i--; |
| 781 | continue; |
| 782 | } |
| 783 | std::string value = getValue(line, sKeywords[i]); |
| 784 | mPrefs[i] = value; |
| 785 | } |
| 786 | file.close(); |
| 787 | if (getVersion() == "") |
| 788 | { |
| 789 | hOutDebug(1,"converting preferences"); |
| 790 | mPrefs[Version] = "1.2"; |
| 791 | Preferences_1_0& oldP = Preferences_1_0::getInstance(); |
| 792 | setHercDir(oldP.hercDir() ); |
| 793 | setConfigDir(oldP.configDir()); |
| 794 | setLogsDir(oldP.logsDir()); |
| 795 | FontPreferences fp(""); |
| 796 | fp.setFontName( oldP.fontName() ); |
| 797 | fp.setFontSize( oldP.fontSize() ); |
| 798 | fp.setFontBoldness( oldP.fontIsBold() ); |
| 799 | fp.setFontItalic( oldP.fontIsItalic() ); |
| 800 | mPrefs[LogFont] = fp.prefLine(); |
| 801 | |
| 802 | FontPreferences dummyFP(""); |
| 803 | mPrefs[RegsFont] = dummyFP.prefLine(); |
| 804 | mPrefs[PswFont] = dummyFP.prefLine(); |
| 805 | mPrefs[CommandFont] = dummyFP.prefLine(); |
| 806 | setMipsAsGauge(false); |
| 807 | setPswMode(Psw::StatusBar); |
| 808 | setSplitLog(true); |
| 809 | setAutosaveLog(false); |
| 810 | write(); |
| 811 | } |
| 812 | else if (getVersion() == "1.1") |
| 813 | { |
| 814 | hOutDebug(1,"converting preferences"); |
| 815 | mPrefs[Version] = "1.2"; |
| 816 | setMipsAsGauge(false); |
| 817 | setPswMode(Psw::StatusBar); |
| 818 | setSplitLog(true); |
| 819 | setAutosaveLog(false); |
| 820 | write(); |
| 821 | } |
| 822 | |
| 823 | |
| 824 | std::string value; |
| 825 | try |
| 826 | { |
| 827 | value = mPrefs.at(RegsViews); |
| 828 | } |
| 829 | catch (const std::out_of_range& ) |
| 830 | { |
| 831 | value = "0"; |
| 832 | } |
| 833 | mView = std::strtol(value.c_str(), NULL, 0) ; |
| 834 | } |
| 835 | |
| 836 | std::string Preferences_1_2_0::getValue(char * line, const char * keyword) |
| 837 | { |
| 838 | if (strncmp(line,keyword,strlen(keyword)) != 0) |
| 839 | { |
| 840 | std::string err = "preferences keyword: "; |
| 841 | err += keyword; |
| 842 | std::cerr << err << std::endl; |
| 843 | throw new std::string(err); |
| 844 | } |
| 845 | char *p = line+strlen(keyword); |
| 846 | std::string ret(p); |
| 847 | if (ret.at(ret.length()-1) == '\n') |
| 848 | ret = ret.substr(0,ret.length()-1); |
| 849 | hOutDebug(4,"keyword:" << keyword << " value=" << ret); |
| 850 | return ret; |
| 851 | } |
| 852 | |
| 853 | void Preferences_1_2_0::write() |
| 854 | { |
| 855 | QFile file(getFileName()); |
| 856 | file.open(QIODevice::WriteOnly); |
| 857 | for(unsigned int i=0; i<NumberOfLines; i++) |
| 858 | { |
| 859 | std::string line = sKeywords[i]; |
| 860 | line += mPrefs[i]; |
| 861 | |
| 862 | if (line.find("\n") == std::string::npos) |
| 863 | line += "\n"; |
| 864 | file.write(line.c_str()); |
| 865 | } |
| 866 | file.close(); |
| 867 | } |
| 868 | |
| 869 | QString Preferences_1_2_0::getFileName() |
| 870 | { |
| 871 | QString ret = mPath; |
| 872 | ret += "/"; |
| 873 | ret += sFileName; |
| 874 | hOutDebug(4,"file name=" << ret.toStdString()); |
| 875 | return ret; |
| 876 | } |
| 877 | |
| 878 | void Preferences_1_2_0::setHercDir(const std::string& hercDir) |
| 879 | { |
| 880 | mPrefs[HerculesDir] = hercDir; |
| 881 | } |
| 882 | |
| 883 | void Preferences_1_2_0::setConfigDir(const std::string& configDir) |
| 884 | { |
| 885 | mPrefs[ConfigurationDir] = configDir; |
| 886 | mVolatileConfigDir = configDir; |
| 887 | } |
| 888 | |
| 889 | void Preferences_1_2_0::setVolatileConfigDir(const std::string& configDir) |
| 890 | { |
| 891 | mVolatileConfigDir = configDir; |
| 892 | } |
| 893 | |
| 894 | const std::string& Preferences_1_2_0::configDir() |
| 895 | { |
| 896 | if (mVolatileConfigDir =="") |
| 897 | return mPrefs[ConfigurationDir]; |
| 898 | else |
| 899 | return mVolatileConfigDir; |
| 900 | } |
| 901 | |
| 902 | void Preferences_1_2_0::setLogsDir(const std::string& logsDir) |
| 903 | { |
| 904 | mPrefs[LogsDir] = logsDir; |
| 905 | } |
| 906 | |
| 907 | |
| 908 | void Preferences_1_2_0::assertConfDir() |
| 909 | { |
| 910 | //TODO |
| 911 | } |
| 912 | |
| 913 | std::string Preferences_1_2_0::fontName(FontObject fontObject) |
| 914 | { |
| 915 | FontPreferences fp(mPrefs[fontObjectToIndex(fontObject)]); |
| 916 | return fp.fontName(); |
| 917 | } |
| 918 | |
| 919 | void Preferences_1_2_0::setFontName(FontObject fontObject, const std::string& fontName) |
| 920 | { |
| 921 | FontPreferences fp(mPrefs[fontObjectToIndex(fontObject)]); |
| 922 | fp.setFontName(fontName); |
| 923 | mPrefs[fontObjectToIndex(fontObject)] = fp.prefLine(); |
| 924 | |
| 925 | } |
| 926 | |
| 927 | int Preferences_1_2_0::fontSize(FontObject fontObject) |
| 928 | { |
| 929 | FontPreferences fp(mPrefs[fontObjectToIndex(fontObject)]); |
| 930 | hOutDebug(3, "Preferences::fontSize " << fp.fontSize()); |
| 931 | return fp.fontSize(); |
| 932 | } |
| 933 | |
| 934 | void Preferences_1_2_0::setFontSize(FontObject fontObject, int size) |
| 935 | { |
| 936 | FontPreferences fp(mPrefs[fontObjectToIndex(fontObject)]); |
| 937 | fp.setFontSize(size); |
| 938 | mPrefs[fontObjectToIndex(fontObject)] = fp.prefLine(); |
| 939 | hOutDebug(3, "Preferences::setFontSize " << fp.prefLine()); |
| 940 | } |
| 941 | |
| 942 | void Preferences_1_2_0::setBold(FontObject fontObject, bool bold) |
| 943 | { |
| 944 | FontPreferences fp(mPrefs[fontObjectToIndex(fontObject)]); |
| 945 | fp.setFontBoldness(bold); |
| 946 | mPrefs[fontObjectToIndex(fontObject)] = fp.prefLine(); |
| 947 | } |
| 948 | |
| 949 | bool Preferences_1_2_0::fontIsBold(FontObject fontObject) |
| 950 | { |
| 951 | FontPreferences fp(mPrefs[fontObjectToIndex(fontObject)]); |
| 952 | return fp.fontIsBold(); |
| 953 | } |
| 954 | |
| 955 | void Preferences_1_2_0::setItalic(FontObject fontObject, bool italic) |
| 956 | { |
| 957 | FontPreferences fp(mPrefs[fontObjectToIndex(fontObject)]); |
| 958 | fp.setFontItalic(italic); |
| 959 | mPrefs[fontObjectToIndex(fontObject)] = fp.prefLine(); |
| 960 | } |
| 961 | |
| 962 | bool Preferences_1_2_0::fontIsItalic(FontObject fontObject) |
| 963 | { |
| 964 | FontPreferences fp(mPrefs[fontObjectToIndex(fontObject)]); |
| 965 | return fp.fontIsItalic(); |
| 966 | } |
| 967 | |
| 968 | bool Preferences_1_2_0::logTimestamp() |
| 969 | { |
| 970 | return mPrefs[LogTimestamp] == "TRUE"; |
| 971 | } |
| 972 | |
| 973 | void Preferences_1_2_0::setLogTimestamp(bool isTrue) |
| 974 | { |
| 975 | mPrefs[LogTimestamp] = isTrue ? "TRUE" : "FALSE" ; |
| 976 | } |
| 977 | |
| 978 | void Preferences_1_2_0::setRegs(Views v, bool view) |
| 979 | { |
| 980 | if (view) |
| 981 | mView |= v; |
| 982 | else |
| 983 | mView &= 255-v; |
| 984 | std::stringstream ss; |
| 985 | ss << mView; |
| 986 | mPrefs[RegsViews] = ss.str(); |
| 987 | write(); |
| 988 | } |
| 989 | |
| 990 | bool Preferences_1_2_0::regs(Views v) |
| 991 | { |
| 992 | return (mView & v); |
| 993 | } |
| 994 | |
| 995 | const std::string& Preferences_1_2_0::getVersion() |
| 996 | { |
| 997 | return mPrefs[Version]; |
| 998 | } |
| 999 | |
| 1000 | Preferences_1_2_0::keys Preferences_1_2_0::fontObjectToIndex(FontObject fontObject) |
| 1001 | { |
| 1002 | switch(fontObject) |
| 1003 | { |
| 1004 | case RegsFontObject: return RegsFont; break; |
| 1005 | case PswFontObject: return PswFont; break; |
| 1006 | case CommandFontObject: return CommandFont; break; |
| 1007 | default: return LogFont; break; |
| 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | void Preferences_1_2_0::setMipsAsGauge(bool gauge) |
| 1012 | { |
| 1013 | mPrefs[MipsAsGauge] = (gauge ? "TRUE" : "FALSE"); |
| 1014 | } |
| 1015 | |
| 1016 | bool Preferences_1_2_0::mipsAsGauge() |
| 1017 | { |
| 1018 | return mPrefs[MipsAsGauge] == "TRUE"; |
| 1019 | } |
| 1020 | |
| 1021 | void Preferences_1_2_0::setPswMode(Psw::PswMode mode) |
| 1022 | { |
| 1023 | mPrefs[PswMode] = ( mode == Psw::Docked ? "0" : "1"); |
| 1024 | } |
| 1025 | |
| 1026 | Psw::PswMode Preferences_1_2_0::pswMode() |
| 1027 | { |
| 1028 | return ( (strcmp(mPrefs[PswMode].c_str(),"0") == 0 ) ? |
| 1029 | Psw::Docked : |
| 1030 | Psw::StatusBar ) ; |
| 1031 | } |
| 1032 | |
| 1033 | void Preferences_1_2_0::setSplitLog(bool split) |
| 1034 | { |
| 1035 | mPrefs[SplitLog] = ( split ? "TRUE" : "FALSE"); |
| 1036 | } |
| 1037 | |
| 1038 | bool Preferences_1_2_0::splitLog() |
| 1039 | { |
| 1040 | return ( (strcmp(mPrefs[SplitLog].c_str(),"TRUE") == 0 ) ? |
| 1041 | true : |
| 1042 | false ) ; |
| 1043 | } |
| 1044 | |
| 1045 | void Preferences_1_2_0::setAutosaveLog(bool autosave) |
| 1046 | { |
| 1047 | mPrefs[AutosaveLog] = ( autosave ? "TRUE" : "FALSE"); |
| 1048 | } |
| 1049 | |
| 1050 | bool Preferences_1_2_0::autosaveLog() |
| 1051 | { |
| 1052 | return ( (strcmp(mPrefs[AutosaveLog].c_str(),"TRUE") == 0 ) ? |
| 1053 | true : |
| 1054 | false ) ; |
| 1055 | } |
| 1056 | ///////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 1057 | |
| 1058 | Preferences_1_0 * Preferences_1_0::instance = NULL; |
| 1059 | const char * Preferences_1_0::sKeywords[] = { |
| 1060 | "HerculesDir=", |
| 1061 | "ConfigurationDir=", |
| 1062 | "LogsDir=" , |
| 1063 | "Font=", |
| 1064 | "FontSize=", |
| 1065 | "FontVariation=", |
| 1066 | "LogTimeStamp=", |
| 1067 | "RegsViews="}; |
| 1068 | const char * Preferences_1_0::sFileName = "HercStudio.pref"; |
| 1069 | |
| 1070 | Preferences_1_0::Preferences_1_0() : |
| 1071 | mPath(""), |
| 1072 | mVolatileConfigDir("") |
| 1073 | { |
| 1074 | assertConfDir(); |
| 1075 | for (unsigned int i=0; i< NumberOfLines ; i++) |
| 1076 | { |
| 1077 | mPrefs.push_back(""); |
| 1078 | } |
| 1079 | mPath = QDir::homePath().toStdString() + "/.config"; |
| 1080 | setHercDir(""); |
| 1081 | setConfigDir(""); |
| 1082 | setLogsDir(QDir::homePath().toStdString()+"/Desktop"); |
| 1083 | setFontName("(system default)"); |
| 1084 | setFontSize(9); |
| 1085 | readPref(); |
| 1086 | } |
| 1087 | |
| 1088 | Preferences_1_0::~Preferences_1_0() |
| 1089 | { |
| 1090 | } |
| 1091 | |
| 1092 | |
| 1093 | Preferences_1_0& Preferences_1_0::getInstance() |
| 1094 | { |
| 1095 | if (instance == NULL) |
| 1096 | instance = new Preferences_1_0(); |
| 1097 | return *instance; |
| 1098 | } |
| 1099 | |
| 1100 | void Preferences_1_0::readPref() |
| 1101 | { |
| 1102 | QFile file(getFileName()); |
| 1103 | file.open(QIODevice::ReadOnly); |
| 1104 | |
| 1105 | for (unsigned int i=0; i<NumberOfLines; i++) |
| 1106 | { |
| 1107 | char line[Preferences_1_2_0::PREF_LINE_LENGTH]; |
| 1108 | if ( file.readLine(line, Preferences_1_2_0::PREF_LINE_LENGTH) == -1 ) |
| 1109 | break; |
| 1110 | if (strlen(line) < 2) // skip null/blank lines |
| 1111 | { |
| 1112 | i--; |
| 1113 | continue; |
| 1114 | } |
| 1115 | std::string value = getValue(line, sKeywords[i]); |
| 1116 | mPrefs[i] = value; |
| 1117 | } |
| 1118 | file.close(); |
| 1119 | |
| 1120 | |
| 1121 | std::string value; |
| 1122 | try |
| 1123 | { |
| 1124 | value = mPrefs.at(RegsViews); |
| 1125 | } |
| 1126 | catch (const std::out_of_range&) |
| 1127 | { |
| 1128 | value = "0"; |
| 1129 | } |
| 1130 | mView = std::strtol(value.c_str(), NULL, 0) ; |
| 1131 | } |
| 1132 | |
| 1133 | std::string Preferences_1_0::getValue(char * line, const char * keyword) |
| 1134 | { |
| 1135 | if (strncmp(line,keyword,strlen(keyword)) != 0) |
| 1136 | { |
| 1137 | std::string err = "preferences keyword: "; |
| 1138 | err += keyword; |
| 1139 | std::cerr << err << std::endl; |
| 1140 | throw new std::string(err); |
| 1141 | } |
| 1142 | char *p = line+strlen(keyword); |
| 1143 | std::string ret(p); |
| 1144 | if (ret.at(ret.length()-1) == '\n') |
| 1145 | ret = ret.substr(0,ret.length()-1); |
| 1146 | hOutDebug(4,"keyword:" << keyword << " value=" << ret); |
| 1147 | return ret; |
| 1148 | } |
| 1149 | |
| 1150 | void Preferences_1_0::write() |
| 1151 | { |
| 1152 | QString fn(mPath.c_str()); |
| 1153 | |
| 1154 | QFile file(getFileName()); |
| 1155 | file.open(QIODevice::WriteOnly); |
| 1156 | for(unsigned int i=0; i<NumberOfLines; i++) |
| 1157 | { |
| 1158 | std::string line = sKeywords[i]; |
| 1159 | line += mPrefs[i]; |
| 1160 | |
| 1161 | if (line.find("\n") == std::string::npos) |
| 1162 | line += "\n"; |
| 1163 | file.write(line.c_str()); |
| 1164 | } |
| 1165 | file.close(); |
| 1166 | } |
| 1167 | |
| 1168 | QString Preferences_1_0::getFileName() |
| 1169 | { |
| 1170 | QString ret = mPath.c_str(); |
| 1171 | ret += "/"; |
| 1172 | ret += sFileName; |
| 1173 | hOutDebug(4,"file name=" << ret.toStdString()); |
| 1174 | return ret; |
| 1175 | } |
| 1176 | |
| 1177 | void Preferences_1_0::setHercDir(const std::string& hercDir) |
| 1178 | { |
| 1179 | mPrefs[HerculesDir] = hercDir; |
| 1180 | } |
| 1181 | |
| 1182 | void Preferences_1_0::setConfigDir(const std::string& configDir) |
| 1183 | { |
| 1184 | mPrefs[ConfigurationDir] = configDir; |
| 1185 | mVolatileConfigDir = configDir; |
| 1186 | } |
| 1187 | |
| 1188 | void Preferences_1_0::setVolatileConfigDir(const std::string& configDir) |
| 1189 | { |
| 1190 | mVolatileConfigDir = configDir; |
| 1191 | } |
| 1192 | |
| 1193 | const std::string& Preferences_1_0::configDir() |
| 1194 | { |
| 1195 | if (mVolatileConfigDir =="") |
| 1196 | return mPrefs[ConfigurationDir]; |
| 1197 | else |
| 1198 | return mVolatileConfigDir; |
| 1199 | } |
| 1200 | |
| 1201 | void Preferences_1_0::setLogsDir(const std::string& logsDir) |
| 1202 | { |
| 1203 | mPrefs[LogsDir] = logsDir; |
| 1204 | } |
| 1205 | |
| 1206 | |
| 1207 | void Preferences_1_0::assertConfDir() |
| 1208 | { |
| 1209 | } |
| 1210 | |
| 1211 | std::string& Preferences_1_0::fontName() |
| 1212 | { |
| 1213 | return mPrefs[Font]; |
| 1214 | } |
| 1215 | |
| 1216 | void Preferences_1_0::setFontName(const std::string& font) |
| 1217 | { |
| 1218 | mPrefs[Font] = font; |
| 1219 | } |
| 1220 | |
| 1221 | int Preferences_1_0::fontVariation() |
| 1222 | { |
| 1223 | return ConfigurationEditor::parseNum(mPrefs[FontVariation],10); |
| 1224 | } |
| 1225 | |
| 1226 | void Preferences_1_0::setFontVariation(int fontVariation) |
| 1227 | { |
| 1228 | std::stringstream ss; |
| 1229 | ss << fontVariation; |
| 1230 | mPrefs[FontVariation] = ss.str(); |
| 1231 | } |
| 1232 | |
| 1233 | int Preferences_1_0::fontSize() |
| 1234 | { |
| 1235 | return ConfigurationEditor::parseNum(mPrefs[FontSize],10); |
| 1236 | } |
| 1237 | |
| 1238 | void Preferences_1_0::setFontSize(int size) |
| 1239 | { |
| 1240 | std::stringstream ss; |
| 1241 | ss << size; |
| 1242 | mPrefs[FontSize] = ss.str(); |
| 1243 | } |
| 1244 | |
| 1245 | bool Preferences_1_0::fontIsBold() |
| 1246 | { |
| 1247 | return ( (fontVariation() & Bold ) != 0 ); |
| 1248 | } |
| 1249 | |
| 1250 | bool Preferences_1_0::fontIsItalic() |
| 1251 | { |
| 1252 | return ( (fontVariation() & Italic ) != 0 ); |
| 1253 | } |
| 1254 | |
| 1255 | bool Preferences_1_0::logTimestamp() |
| 1256 | { |
| 1257 | return mPrefs[LogTimestamp] == "TRUE"; |
| 1258 | } |
| 1259 | |
| 1260 | void Preferences_1_0::setLogTimestamp(bool isTrue) |
| 1261 | { |
| 1262 | mPrefs[LogTimestamp] = isTrue ? "TRUE" : "FALSE" ; |
| 1263 | } |
| 1264 | |
| 1265 | void Preferences_1_0::setRegs(Views v, bool view) |
| 1266 | { |
| 1267 | if (view) |
| 1268 | mView |= v; |
| 1269 | else |
| 1270 | mView &= 255-v; |
| 1271 | std::stringstream ss; |
| 1272 | ss << mView; |
| 1273 | mPrefs[RegsViews] = ss.str(); |
| 1274 | write(); |
| 1275 | } |
| 1276 | |
| 1277 | bool Preferences_1_0::regs(Views v) |
| 1278 | { |
| 1279 | return (mView & v); |
| 1280 | } |
| 1281 |