UBSettings.cpp 48.5 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1
/*
Craig Watson's avatar
Craig Watson committed
2 3
 * Copyright (C) 2015-2016 Département de l'Instruction Publique (DIP-SEM)
 *
Claudio Valerio's avatar
Claudio Valerio committed
4
 * Copyright (C) 2013 Open Education Foundation
5
 *
Claudio Valerio's avatar
Claudio Valerio committed
6 7
 * Copyright (C) 2010-2013 Groupement d'Intérêt Public pour
 * l'Education Numérique en Afrique (GIP ENA)
8
 *
Claudio Valerio's avatar
Claudio Valerio committed
9 10 11
 * This file is part of OpenBoard.
 *
 * OpenBoard is free software: you can redistribute it and/or modify
Claudio Valerio's avatar
Claudio Valerio committed
12 13
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License,
14 15 16
 * with a specific linking exception for the OpenSSL project's
 * "OpenSSL" library (or with modified versions of it that use the
 * same license as the "OpenSSL" library).
Claudio Valerio's avatar
Claudio Valerio committed
17
 *
Claudio Valerio's avatar
Claudio Valerio committed
18
 * OpenBoard is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Claudio Valerio's avatar
Claudio Valerio committed
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Claudio Valerio's avatar
Claudio Valerio committed
21
 * GNU General Public License for more details.
22
 *
Claudio Valerio's avatar
Claudio Valerio committed
23
 * You should have received a copy of the GNU General Public License
Claudio Valerio's avatar
Claudio Valerio committed
24
 * along with OpenBoard. If not, see <http://www.gnu.org/licenses/>.
Claudio Valerio's avatar
Claudio Valerio committed
25 26
 */

27

Claudio Valerio's avatar
Claudio Valerio committed
28

Claudio Valerio's avatar
Claudio Valerio committed
29

Claudio Valerio's avatar
Claudio Valerio committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include "UBSettings.h"

#include <QtGui>


#include "frameworks/UBPlatformUtils.h"
#include "frameworks/UBFileSystemUtils.h"
#include "frameworks/UBCryptoUtils.h"

#include "UB.h"
#include "UBSetting.h"

#include "tools/UBToolsManager.h"

44 45
#include "core/memcheck.h"

Claudio Valerio's avatar
Claudio Valerio committed
46 47 48
QPointer<UBSettings> UBSettings::sSingleton = 0;

int UBSettings::pointerDiameter = 40;
49
int UBSettings::crossSize = 24;
50
int UBSettings::defaultCrossSize = 24;
51
int UBSettings::minCrossSize = 12;
52
int UBSettings::maxCrossSize = 96; //TODO: user-settable?
53
int UBSettings::colorPaletteSize = 5;
Claudio Valerio's avatar
Claudio Valerio committed
54 55 56 57 58 59 60 61 62
int UBSettings::objectFrameWidth = 20;
int UBSettings::boardMargin = 10;

QString UBSettings::documentGroupName = QString("Subject");
QString UBSettings::documentName = QString("Lesson");
QString UBSettings::documentSize = QString("Size");
QString UBSettings::documentIdentifer = QString("ID");
QString UBSettings::documentVersion = QString("Version");
QString UBSettings::documentUpdatedAt = QString("UpdatedAt");
63
QString UBSettings::documentDate = QString("date");
Claudio Valerio's avatar
Claudio Valerio committed
64 65 66 67 68 69

QString UBSettings::trashedDocumentGroupNamePrefix = QString("_Trash:");

QString UBSettings::uniboardDocumentNamespaceUri = "http://uniboard.mnemis.com/document";
QString UBSettings::uniboardApplicationNamespaceUri = "http://uniboard.mnemis.com/application";

70
QString UBSettings::undoCommandTransactionName = "UndoTransaction";
Claudio Valerio's avatar
Claudio Valerio committed
71 72 73 74

const int UBSettings::sDefaultFontPixelSize = 36;
const char *UBSettings::sDefaultFontFamily = "Arial";

Claudio Valerio's avatar
Claudio Valerio committed
75
QString UBSettings::currentFileVersion = "4.8.0";
Claudio Valerio's avatar
Claudio Valerio committed
76

77
QBrush UBSettings::eraserBrushDarkBackground = QBrush(QColor(127, 127, 127, 80));
Claudio Valerio's avatar
Claudio Valerio committed
78 79
QBrush UBSettings::eraserBrushLightBackground = QBrush(QColor(255, 255, 255, 30));

80 81
QPen UBSettings::eraserPenDarkBackground = QPen(QColor(255, 255, 255, 127));
QPen UBSettings::eraserPenLightBackground = QPen(QColor(0, 0, 0, 127));
Claudio Valerio's avatar
Claudio Valerio committed
82

83 84
QColor UBSettings::markerCircleBrushColorDarkBackground = QColor(127, 127, 127, 80);
QColor UBSettings::markerCircleBrushColorLightBackground = QColor(255, 255, 255, 30);
85

86 87
QColor UBSettings::markerCirclePenColorDarkBackground = QColor(255, 255, 255, 127);
QColor UBSettings::markerCirclePenColorLightBackground = QColor(0, 0, 0, 127);
88

89 90 91 92 93 94
QColor UBSettings::penCircleBrushColorDarkBackground = QColor(127, 127, 127, 80);
QColor UBSettings::penCircleBrushColorLightBackground = QColor(255, 255, 255, 30);

QColor UBSettings::penCirclePenColorDarkBackground = QColor(255, 255, 255, 127);
QColor UBSettings::penCirclePenColorLightBackground = QColor(0, 0, 0, 127);

Claudio Valerio's avatar
Claudio Valerio committed
95 96 97 98 99 100 101 102 103 104 105 106
QColor UBSettings::documentSizeMarkColorDarkBackground = QColor(44, 44, 44, 200);
QColor UBSettings::documentSizeMarkColorLightBackground = QColor(241, 241, 241);

QColor UBSettings::paletteColor = QColor(127, 127, 127, 127);
QColor UBSettings::opaquePaletteColor = QColor(66, 66, 66, 200);

QColor UBSettings::documentViewLightColor = QColor(241, 241, 241);

QPointer<QSettings> UBSettings::sAppSettings = 0;

const int UBSettings::maxThumbnailWidth = 400;
const int UBSettings::defaultThumbnailWidth = 150;
shibakaneki's avatar
shibakaneki committed
107
const int UBSettings::defaultLibraryIconSize = 80;
Claudio Valerio's avatar
Claudio Valerio committed
108 109 110 111 112 113 114 115 116

const int UBSettings::defaultGipWidth = 150;
const int UBSettings::defaultSoundWidth = 50;
const int UBSettings::defaultImageWidth = 150;
const int UBSettings::defaultShapeWidth = 50;
const int UBSettings::defaultWidgetIconWidth = 110;
const int UBSettings::defaultVideoWidth = 80;

const int UBSettings::thumbnailSpacing = 20;
117
const int UBSettings::longClickInterval = 1200;
Claudio Valerio's avatar
Claudio Valerio committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

const qreal UBSettings::minScreenRatio = 1.33; // 800/600 or 1024/768

QStringList UBSettings::bitmapFileExtensions;
QStringList UBSettings::vectoFileExtensions;
QStringList UBSettings::imageFileExtensions;
QStringList UBSettings::widgetFileExtensions;
QStringList UBSettings::interactiveContentFileExtensions;

QColor UBSettings::treeViewBackgroundColor = QColor(209, 215, 226); //in synch with css tree view background

int UBSettings::objectInControlViewMargin = 100;

QString UBSettings::appPingMessage = "__uniboard_ping";

133 134
UBSettings* UBSettings::settings()
{
135
    if (!sSingleton) {
136
        sSingleton = new UBSettings(qApp);
137
    }
138 139 140 141 142 143 144 145 146 147 148
    return sSingleton;
}

void UBSettings::destroy()
{
    if (sSingleton)
        delete sSingleton;
    sSingleton = NULL;
}


Claudio Valerio's avatar
Claudio Valerio committed
149 150 151 152
QSettings* UBSettings::getAppSettings()
{
    if (!UBSettings::sAppSettings)
    {
Claudio Valerio's avatar
Claudio Valerio committed
153 154
        QString tmpSettings = QDir::tempPath() + "/" + qApp->applicationName() + ".config";
        QString appSettings = UBPlatformUtils::applicationResourcesDirectory() + "/etc/" + qApp->applicationName() + ".config";
Claudio Valerio's avatar
Claudio Valerio committed
155 156 157 158 159 160 161 162

        // tmpSettings exists when upgrading Uniboard on Mac (see UBPlatformUtils_mac.mm updater:willInstallUpdate:)
        if (QFile::exists(tmpSettings))
        {
            QFile::rename(tmpSettings, appSettings);
        }

        UBSettings::sAppSettings = new QSettings(appSettings, QSettings::IniFormat, 0);
163
        UBSettings::sAppSettings->setIniCodec("utf-8");
164 165

        qDebug() << "sAppSettings location: " << appSettings;
Claudio Valerio's avatar
Claudio Valerio committed
166 167 168 169 170 171 172 173 174
    }

    return UBSettings::sAppSettings;
}


UBSettings::UBSettings(QObject *parent)
    : QObject(parent)
{
175 176
    InitKeyboardPaletteKeyBtnSizes();

Claudio Valerio's avatar
Claudio Valerio committed
177 178
    mAppSettings = UBSettings::getAppSettings();

Claudio Valerio's avatar
Claudio Valerio committed
179
    QString userSettingsFile = UBSettings::userDataDirectory() + "/"+qApp->applicationName()+"User.config";
Claudio Valerio's avatar
Claudio Valerio committed
180 181 182 183 184 185 186 187 188 189

    mUserSettings = new QSettings(userSettingsFile, QSettings::IniFormat, parent);

    init();
}


UBSettings::~UBSettings()
{
    delete mAppSettings;
190 191 192 193 194 195 196 197 198 199

    if(supportedKeyboardSizes)
        delete supportedKeyboardSizes;
}

void UBSettings::InitKeyboardPaletteKeyBtnSizes()
{
    supportedKeyboardSizes = new QStringList();
    supportedKeyboardSizes->append("29x29");
    supportedKeyboardSizes->append("41x41");
Claudio Valerio's avatar
Claudio Valerio committed
200 201
}

202 203 204 205
void UBSettings::ValidateKeyboardPaletteKeyBtnSize()
{
    // if boardKeyboardPaletteKeyBtnSize is not initialized, or supportedKeyboardSizes not initialized or empty
    if( !boardKeyboardPaletteKeyBtnSize ||
Claudio Valerio's avatar
Claudio Valerio committed
206
        !supportedKeyboardSizes ||
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
        supportedKeyboardSizes->size() == 0 ) return;

    // get original size value
    QString origValue = boardKeyboardPaletteKeyBtnSize->get().toString();

    // parse available size values, for make sure original value is valid
    for(int i = 0; i < supportedKeyboardSizes->size(); i++)
    {
        int compareCode = QString::compare(origValue, supportedKeyboardSizes->at(i));
        if(compareCode == 0) return;
    }

    // if original value is invalid, than set it value to first value from avaliable list
    boardKeyboardPaletteKeyBtnSize->set(supportedKeyboardSizes->at(0));
}
Claudio Valerio's avatar
Claudio Valerio committed
222 223 224

void UBSettings::init()
{
225
    productWebUrl =  new UBSetting(this, "App", "ProductWebAddress", "http://www.openboard.ch");
Claudio Valerio's avatar
Claudio Valerio committed
226 227 228

    softwareHomeUrl = productWebUrl->get().toString();

229
    tutorialUrl = new UBSetting(this,"App","TutorialUrl","http://www.openboard.ch");
230

231 232 233
    documentSizes.insert(DocumentSizeRatio::Ratio4_3, QSize(1280, 960)); // 1.33
    documentSizes.insert(DocumentSizeRatio::Ratio16_9, QSize((960 / 9 * 16), 960)); // 1.77

Claudio Valerio's avatar
Claudio Valerio committed
234 235
    appToolBarPositionedAtTop = new UBSetting(this, "App", "ToolBarPositionedAtTop", true);
    appToolBarDisplayText = new UBSetting(this, "App", "ToolBarDisplayText", true);
236
    appEnableAutomaticSoftwareUpdates = new UBSetting(this, "App", "EnableAutomaticSoftwareUpdates", false);
237
    appSoftwareUpdateURL = new UBSetting(this, "App", "SoftwareUpdateURL", "http://www.openboard.ch/update.json");
238
    appHideCheckForSoftwareUpdate = new UBSetting(this, "App", "HideCheckForSoftwareUpdate", false);
239
    appHideSwapDisplayScreens = new UBSetting(this, "App", "HideSwapDisplayScreens", true);
240
    appToolBarOrientationVertical = new UBSetting(this, "App", "ToolBarOrientationVertical", false);
241 242
    appPreferredLanguage = new UBSetting(this,"App","PreferredLanguage", "");

Claudio Valerio's avatar
Claudio Valerio committed
243
    rightLibPaletteBoardModeWidth = new UBSetting(this, "Board", "RightLibPaletteBoardModeWidth", 270);
244
    rightLibPaletteBoardModeIsCollapsed = new UBSetting(this,"Board", "RightLibPaletteBoardModeIsCollapsed",true);
Claudio Valerio's avatar
Claudio Valerio committed
245
    rightLibPaletteDesktopModeWidth = new UBSetting(this, "Board", "RightLibPaletteDesktopModeWidth", 270);
246
    rightLibPaletteDesktopModeIsCollapsed = new UBSetting(this,"Board", "RightLibPaletteDesktopModeIsCollapsed",true);
Claudio Valerio's avatar
Claudio Valerio committed
247
    leftLibPaletteBoardModeWidth = new UBSetting(this, "Board", "LeftLibPaletteBoardModeWidth",270);
248
    leftLibPaletteBoardModeIsCollapsed = new UBSetting(this,"Board","LeftLibPaletteBoardModeIsCollapsed",true);
Claudio Valerio's avatar
Claudio Valerio committed
249
    leftLibPaletteDesktopModeWidth = new UBSetting(this, "Board", "LeftLibPaletteDesktopModeWidth",270);
250
    leftLibPaletteDesktopModeIsCollapsed = new UBSetting(this,"Board","LeftLibPaletteDesktopModeIsCollapsed",true);
Claudio Valerio's avatar
Claudio Valerio committed
251 252 253 254

    appIsInSoftwareUpdateProcess = new UBSetting(this, "App", "IsInSoftwareUpdateProcess", false);
    appLastSessionDocumentUUID = new UBSetting(this, "App", "LastSessionDocumentUUID", "");
    appLastSessionPageIndex = new UBSetting(this, "App", "LastSessionPageIndex", 0);
Claudio Valerio's avatar
Claudio Valerio committed
255
    appUseMultiscreen = new UBSetting(this, "App", "UseMultiscreenMode", true);
256 257 258

    appStartupHintsEnabled = new UBSetting(this,"App","EnableStartupHints",true);

Claudio Valerio's avatar
Claudio Valerio committed
259
    appLookForOpenSankoreInstall = new UBSetting(this, "App", "LookForOpenSankoreInstall", true);
Claudio Valerio's avatar
Claudio Valerio committed
260

Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
261 262
    appStartMode = new UBSetting(this, "App", "StartMode", "");

263 264
    featureSliderPosition = new UBSetting(this, "Board", "FeatureSliderPosition", 40);

Claudio Valerio's avatar
Claudio Valerio committed
265 266 267 268 269 270 271 272 273 274 275 276 277
    boardPenFineWidth = new UBSetting(this, "Board", "PenFineWidth", 1.5);
    boardPenMediumWidth = new UBSetting(this, "Board", "PenMediumWidth", 3.0);
    boardPenStrongWidth = new UBSetting(this, "Board", "PenStrongWidth", 8.0);

    boardMarkerFineWidth = new UBSetting(this, "Board", "MarkerFineWidth", 12.0);
    boardMarkerMediumWidth = new UBSetting(this, "Board", "MarkerMediumWidth", 24.0);
    boardMarkerStrongWidth = new UBSetting(this, "Board", "MarkerStrongWidth", 48.0);

    boardPenPressureSensitive = new UBSetting(this, "Board", "PenPressureSensitive", true);
    boardMarkerPressureSensitive = new UBSetting(this, "Board", "MarkerPressureSensitive", false);

    boardUseHighResTabletEvent = new UBSetting(this, "Board", "UseHighResTabletEvent", true);

278
    boardInterpolatePenStrokes = new UBSetting(this, "Board", "InterpolatePenStrokes", true);
279 280
    boardSimplifyPenStrokes = new UBSetting(this, "Board", "SimplifyPenStrokes", true);
    boardSimplifyPenStrokesThresholdAngle = new UBSetting(this, "Board", "SimplifyPenStrokesThresholdAngle", 2);
281
    boardSimplifyPenStrokesThresholdWidthDifference = new UBSetting(this, "Board", "SimplifyPenStrokesThresholdWidthDifference", 2.0);
282 283 284

    boardInterpolateMarkerStrokes = new UBSetting(this, "Board", "InterpolateMarkerStrokes", true);
    boardSimplifyMarkerStrokes = new UBSetting(this, "Board", "SimplifyMarkerStrokes", true);
285

286 287
    boardKeyboardPaletteKeyBtnSize = new UBSetting(this, "Board", "KeyboardPaletteKeyBtnSize", "16x16");
    ValidateKeyboardPaletteKeyBtnSize();
Claudio Valerio's avatar
Claudio Valerio committed
288

289
    pageSize = new UBSetting(this, "Board", "DefaultPageSize", documentSizes.value(DocumentSizeRatio::Ratio4_3));
Aleksei Kanash's avatar
Aleksei Kanash committed
290

291
    boardCrossColorDarkBackground = new UBSetting(this, "Board", "CrossColorDarkBackground", "#C8C0C0C0");
Craig Watson's avatar
Craig Watson committed
292
    boardCrossColorLightBackground = new UBSetting(this, "Board", "CrossColorLightBackground", "#A5E1FF");
293

294
    QStringList gridLightBackgroundColors;
295
    gridLightBackgroundColors << "#000000" << "#FF0000" << "#004080" << "#008000" << "#FFDD00" << "#C87400" << "#800040" << "#008080" << "#A5E1FF";
296 297 298
    boardGridLightBackgroundColors = new UBColorListSetting(this, "Board", "GridLightBackgroundColors", gridLightBackgroundColors, -1.0);

    QStringList gridDarkBackgroundColors;
299
    gridDarkBackgroundColors << "#FFFFFF" << "#FF3400" << "#66C0FF" << "#81FF5C" << "#FFFF00" << "#B68360" << "#FF497E" << "#8D69FF" << "#C8C0C0C0";
300 301
    boardGridDarkBackgroundColors = new UBColorListSetting(this, "Board", "GridDarkBackgroundColors", gridDarkBackgroundColors, -1.0);

Claudio Valerio's avatar
Claudio Valerio committed
302
    QStringList penLightBackgroundColors;
303
    penLightBackgroundColors << "#000000" << "#FF0000" <<"#004080" << "#008000" << "#FFDD00" << "#C87400" << "#800040" << "#008080"  << "#5F2D0A" << "#FFFFFF";
Claudio Valerio's avatar
Claudio Valerio committed
304 305 306
    boardPenLightBackgroundColors = new UBColorListSetting(this, "Board", "PenLightBackgroundColors", penLightBackgroundColors, 1.0);

    QStringList penDarkBackgroundColors;
307
    penDarkBackgroundColors << "#FFFFFF" << "#FF3400" <<"#66C0FF" << "#81FF5C" << "#FFFF00" << "#B68360" << "#FF497E" << "#8D69FF" << "#000000";
Claudio Valerio's avatar
Claudio Valerio committed
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
    boardPenDarkBackgroundColors = new UBColorListSetting(this, "Board", "PenDarkBackgroundColors", penDarkBackgroundColors, 1.0);

    boardMarkerAlpha = new UBSetting(this, "Board", "MarkerAlpha", 0.5);

    QStringList markerLightBackgroundColors;
    markerLightBackgroundColors << "#E3FF00" << "#FF0000" <<"#004080" << "#008000" << "#C87400" << "#800040" << "#008080"  << "#000000";
    boardMarkerLightBackgroundColors = new UBColorListSetting(this, "Board", "MarkerLightBackgroundColors", markerLightBackgroundColors, boardMarkerAlpha->get().toDouble());

    QStringList markerDarkBackgroundColors;
    markerDarkBackgroundColors << "#FFFF00" << "#FF4400" <<"#66C0FF" << "#81FF5C" << "#B68360" << "#FF497E" << "#8D69FF" << "#FFFFFF";
    boardMarkerDarkBackgroundColors = new UBColorListSetting(this, "Board", "MarkerDarkBackgroundColors", markerDarkBackgroundColors, boardMarkerAlpha->get().toDouble());

    QStringList penLightBackgroundSelectedColors;
    QStringList penDarkBackgroundSelectedColors;

    QStringList markerLightBackgroundSelectedColors;
    QStringList markerDarkBackgroundSelectedColors;

    for (int i = 0; i < colorPaletteSize; i++)
    {
        penLightBackgroundSelectedColors << penLightBackgroundColors[i];
        penDarkBackgroundSelectedColors << penDarkBackgroundColors[i];
        markerLightBackgroundSelectedColors << markerLightBackgroundColors[i];
        markerDarkBackgroundSelectedColors << markerDarkBackgroundColors[i];
    }

    boardPenLightBackgroundSelectedColors = new UBColorListSetting(this, "Board", "PenLightBackgroundSelectedColors", penLightBackgroundSelectedColors);
    boardPenDarkBackgroundSelectedColors = new UBColorListSetting(this, "Board", "PenDarkBackgroundSelectedColors", penDarkBackgroundSelectedColors);

    boardMarkerLightBackgroundSelectedColors = new UBColorListSetting(this, "Board", "MarkerLightBackgroundSelectedColors", markerLightBackgroundSelectedColors, boardMarkerAlpha->get().toDouble());
    boardMarkerDarkBackgroundSelectedColors = new UBColorListSetting(this, "Board", "MarkerDarkBackgroundSelectedColors", markerDarkBackgroundSelectedColors, boardMarkerAlpha->get().toDouble());

340 341
    showEraserPreviewCircle = new UBSetting(this, "Board", "ShowEraserPreviewCircle", true);
    showMarkerPreviewCircle = new UBSetting(this, "Board", "ShowMarkerPreviewCircle", true);
342 343
    showPenPreviewCircle = new UBSetting(this, "Board", "ShowPenPreviewCircle", true);
    penPreviewFromSize = new UBSetting(this, "Board", "PenPreviewFromSize", 5);
344

Claudio Valerio's avatar
Claudio Valerio committed
345 346 347 348
    webUseExternalBrowser = new UBSetting(this, "Web", "UseExternalBrowser", false);

    bool defaultShowPageImmediatelyOnMirroredScreen = true;

349
#if defined(Q_OS_LINUX)
Claudio Valerio's avatar
Claudio Valerio committed
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
    // screen duplication is very slow on X11
    defaultShowPageImmediatelyOnMirroredScreen = false;
#endif

    webShowPageImmediatelyOnMirroredScreen = new UBSetting(this, "Web", "ShowPageImediatelyOnMirroredScreen", defaultShowPageImmediatelyOnMirroredScreen);

    webHomePage = new UBSetting(this, "Web", "Homepage", softwareHomeUrl);
    webBookmarksPage = new UBSetting(this, "Web", "BookmarksPage", "http://www.myuniboard.com");
    webAddBookmarkUrl = new UBSetting(this, "Web", "AddBookmarkURL", "http://www.myuniboard.com/bookmarks/save/?url=");
    webShowAddBookmarkButton = new UBSetting(this, "Web", "ShowAddBookmarkButton", false);

    pageCacheSize = new UBSetting(this, "App", "PageCacheSize", 20);

    bitmapFileExtensions << "jpg" << "jpeg" <<  "png" <<  "tiff" << "tif" << "bmp" << "gif";
    vectoFileExtensions << "svg" <<  "svgz";
    imageFileExtensions << bitmapFileExtensions << vectoFileExtensions;

    widgetFileExtensions << "wdgt" << "wgt" << "pwgt";
    interactiveContentFileExtensions << widgetFileExtensions << "swf";

    boardZoomFactor = new UBSetting(this, "Board", "ZoomFactor", QVariant(1.41));

    if (boardZoomFactor->get().toDouble() <= 1.)
        boardZoomFactor->set(1.41);

    int defaultRefreshRateInFramePerSecond = 8;

377
#if defined(Q_OS_LINUX)
Claudio Valerio's avatar
Claudio Valerio committed
378 379 380 381 382 383
    // screen duplication is very slow on X11
    defaultRefreshRateInFramePerSecond = 2;
#endif

    mirroringRefreshRateInFps = new UBSetting(this, "Mirroring", "RefreshRateInFramePerSecond", QVariant(defaultRefreshRateInFramePerSecond));

384 385 386 387 388
    lastImportFilePath = new UBSetting(this, "Import", "LastImportFilePath", QVariant(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)));
    lastImportFolderPath = new UBSetting(this, "Import", "LastImportFolderPath", QVariant(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)));
    lastExportFilePath = new UBSetting(this, "Export", "LastExportFilePath", QVariant(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)));
    lastExportDirPath = new UBSetting(this, "Export", "LastExportDirPath", QVariant(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)));
    lastImportToLibraryPath = new UBSetting(this, "Library", "LastImportToLibraryPath", QVariant(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)));
Claudio Valerio's avatar
Claudio Valerio committed
389

390 391 392
    lastPicturePath = new UBSetting(this, "Library", "LastPicturePath", QVariant(QStandardPaths::writableLocation(QStandardPaths::PicturesLocation)));
    lastWidgetPath = new UBSetting(this, "Library", "LastWidgetPath", QVariant(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)));
    lastVideoPath = new UBSetting(this, "Library", "LastVideoPath", QVariant(QStandardPaths::writableLocation(QStandardPaths::MoviesLocation)));
Claudio Valerio's avatar
Claudio Valerio committed
393 394 395 396

    appOnlineUserName = new UBSetting(this, "App", "OnlineUserName", "");

    boardShowToolsPalette = new UBSetting(this, "Board", "ShowToolsPalette", "false");
397
    magnifierDrawingMode = new UBSetting(this, "Board", "MagnifierDrawingMode", "0");
398
    autoSaveInterval = new UBSetting(this, "Board", "AutoSaveIntervalInMinutes", "3");
Claudio Valerio's avatar
Claudio Valerio committed
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414

    svgViewBoxMargin = new UBSetting(this, "SVG", "ViewBoxMargin", "50");

    pdfMargin = new UBSetting(this, "PDF", "Margin", "20");
    pdfPageFormat = new UBSetting(this, "PDF", "PageFormat", "A4");
    pdfResolution = new UBSetting(this, "PDF", "Resolution", "300");

    podcastFramesPerSecond = new UBSetting(this, "Podcast", "FramesPerSecond", 10);
    podcastVideoSize = new UBSetting(this, "Podcast", "VideoSize", "Medium");
    podcastAudioRecordingDevice = new UBSetting(this, "Podcast", "AudioRecordingDevice", "Default");

    podcastWindowsMediaBitsPerSecond = new UBSetting(this, "Podcast", "WindowsMediaBitsPerSecond", 1700000);
    podcastQuickTimeQuality = new UBSetting(this, "Podcast", "QuickTimeQuality", "High");

    podcastPublishToYoutube = new UBSetting(this, "Podcast", "PublishToYouTube", false);
    youTubeUserEMail = new UBSetting(this, "YouTube", "UserEMail", "");
Claudio Valerio's avatar
Claudio Valerio committed
415
    youTubeCredentialsPersistence = new UBSetting(this,"YouTube", "CredentialsPersistence",false);
Claudio Valerio's avatar
Claudio Valerio committed
416

417 418
    communityUser = new UBSetting(this, "Community", "Username", "");
    communityPsw = new UBSetting(this, "Community", "Password", "");
Claudio Valerio's avatar
Claudio Valerio committed
419
    communityCredentialsPersistence = new UBSetting(this,"Community", "CredentialsPersistence",false);
420

Claudio Valerio's avatar
Claudio Valerio committed
421 422 423 424 425 426 427 428 429
    QStringList uris = UBToolsManager::manager()->allToolIDs();

    favoritesNativeToolUris = new UBSetting(this, "App", "FavoriteToolURIs", uris);

    // removed in version 4.4.b.2
    mUserSettings->remove("Podcast/RecordMicrophone");

    documentThumbnailWidth = new UBSetting(this, "Document", "ThumbnailWidth", UBSettings::defaultThumbnailWidth);

430 431
    libraryShowDetailsForLocalItems = new UBSetting(this, "Library", "ShowDetailsForLocalItems", false);

Claudio Valerio's avatar
Claudio Valerio committed
432 433 434 435 436 437 438 439 440
    imageThumbnailWidth = new UBSetting(this, "Library", "ImageThumbnailWidth", UBSettings::defaultImageWidth);
    videoThumbnailWidth = new UBSetting(this, "Library", "VideoThumbnailWidth", UBSettings::defaultVideoWidth);
    shapeThumbnailWidth = new UBSetting(this, "Library", "ShapeThumbnailWidth", UBSettings::defaultShapeWidth);
    gipThumbnailWidth = new UBSetting(this, "Library", "ImageThumbnailWidth", UBSettings::defaultGipWidth);
    soundThumbnailWidth = new UBSetting(this, "Library", "SoundThumbnailWidth", UBSettings::defaultSoundWidth);;

    podcastPublishToIntranet = new UBSetting(this, "IntranetPodcast", "PublishToIntranet", false);
    intranetPodcastPublishingUrl = new UBSetting(this, "IntranetPodcast", "PublishingUrl", "");
    intranetPodcastAuthor = new UBSetting(this, "IntranetPodcast", "Author", "");
441

Claudio Valerio's avatar
Claudio Valerio committed
442
    KeyboardLocale = new UBSetting(this, "Board", "StartupKeyboardLocale", 0);
443
    swapControlAndDisplayScreens = new UBSetting(this, "App", "SwapControlAndDisplayScreens", false);
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
444 445

    angleTolerance = new UBSetting(this, "App", "AngleTolerance", 4);
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
446
    historyLimit = new UBSetting(this, "Web", "HistoryLimit", 15);
root's avatar
root committed
447

shibakaneki's avatar
shibakaneki committed
448
    libIconSize = new UBSetting(this, "Library", "LibIconSize", defaultLibraryIconSize);
Claudio Valerio's avatar
Claudio Valerio committed
449

450
    useSystemOnScreenKeyboard = new UBSetting(this, "App", "UseSystemOnScreenKeyboard", true);
451

Claudio Valerio's avatar
Claudio Valerio committed
452
    cleanNonPersistentSettings();
453
    checkNewSettings();
Claudio Valerio's avatar
Claudio Valerio committed
454 455 456
}


457 458
/**
 * @brief Returns the value for the *key* setting, or *defaultValue* if the key doesn't exist
459 460 461 462 463 464
 *
 * The value is also added to the local settings queue, to prevent future disk I/O when accessing
 * that same setting.
 *
 * If the value doesn't exist in the application settings (i.e it was not present in the config file),
 * it is also added there.
465
 */
466
QVariant UBSettings::value ( const QString & key, const QVariant & defaultValue)
Claudio Valerio's avatar
Claudio Valerio committed
467
{
468 469 470 471 472 473
    // Check first the settings queue, then the app settings, then the user settings.
    // If the key exists in neither of these, then defaultValue is returned.

    if (mSettingsQueue.contains(key))
        return mSettingsQueue.value(key);

474
    // If the setting doesn't exist in the App settings, add it there
Claudio Valerio's avatar
Claudio Valerio committed
475 476
    if (!sAppSettings->contains(key) && !(defaultValue == QVariant()))
        sAppSettings->setValue(key, defaultValue);
Claudio Valerio's avatar
Claudio Valerio committed
477

Craig Watson's avatar
Craig Watson committed
478
    // Get the value from the user settings (or if it doesn't exist there, from the app settings)
479 480
    QVariant val = mUserSettings->value(key, sAppSettings->value(key, defaultValue));

Craig Watson's avatar
Craig Watson committed
481
    // Add the value to the settings queue for faster access next time it is requested
482 483 484
    mSettingsQueue[key] = val;

    return val;
Claudio Valerio's avatar
Claudio Valerio committed
485 486 487 488 489
}


void UBSettings::setValue (const QString & key, const QVariant & value)
{
490 491
    // Save the setting to the queue only; a call to save() is necessary to persist the settings
    mSettingsQueue[key] = value;
Claudio Valerio's avatar
Claudio Valerio committed
492 493
}

494 495 496 497 498 499 500 501
/**
 * @brief Save all the queued settings to disk
 */
void UBSettings::save()
{
    QHash<QString, QVariant>::const_iterator it = mSettingsQueue.constBegin();

    while (it != mSettingsQueue.constEnd()) {
502 503 504 505 506 507 508 509 510 511 512 513 514 515
        /*
         * We save the setting to the user settings if
         * a) it is different from the (non-null) value stored in the user settings, or
         * b) it doesn't currently exist in the user settings AND has changed from the app settings
        */
        if (mUserSettings->contains(it.key())
                && it.value() != mUserSettings->value(it.key()))
        {
            mUserSettings->setValue(it.key(), it.value());
        }

        else if (!mUserSettings->contains(it.key())
                 && it.value() != mAppSettings->value(it.key()))
        {
516
            mUserSettings->setValue(it.key(), it.value());
517
        }
518

519 520 521 522 523 524 525 526 527
        ++it;
    }

    // Force save to file
    mUserSettings->sync();

    qDebug() << "User settings saved";
}

Claudio Valerio's avatar
Claudio Valerio committed
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
int UBSettings::penWidthIndex()
{
    return value("Board/PenLineWidthIndex", 0).toInt();
}


void UBSettings::setPenWidthIndex(int index)
{
    if (index != penWidthIndex())
    {
        setValue("Board/PenLineWidthIndex", index);
    }
}


qreal UBSettings::currentPenWidth()
{
    qreal width = 0;

    switch (penWidthIndex())
    {
        case UBWidth::Fine:
            width = boardPenFineWidth->get().toDouble();
            break;
        case UBWidth::Medium:
            width = boardPenMediumWidth->get().toDouble();
            break;
        case UBWidth::Strong:
            width = boardPenStrongWidth->get().toDouble();
            break;
        default:
            Q_ASSERT(false);
            //failsafe
            width = boardPenFineWidth->get().toDouble();
            break;
    }

    return width;
}


int UBSettings::penColorIndex()
{
    return value("Board/PenColorIndex", 0).toInt();
}


void UBSettings::setPenColorIndex(int index)
{
    if (index != penColorIndex())
    {
        setValue("Board/PenColorIndex", index);
    }
}


QColor UBSettings::currentPenColor()
{
    return penColor(isDarkBackground());
}


QColor UBSettings::penColor(bool onDarkBackground)
{
    QList<QColor> colors = penColors(onDarkBackground);
    return colors.at(penColorIndex());
}


QList<QColor> UBSettings::penColors(bool onDarkBackground)
{
    if (onDarkBackground)
    {
        return boardPenDarkBackgroundSelectedColors->colors();
    }
    else
    {
        return boardPenLightBackgroundSelectedColors->colors();
    }
}


int UBSettings::markerWidthIndex()
{
    return value("Board/MarkerLineWidthIndex", 0).toInt();
}


void UBSettings::setMarkerWidthIndex(int index)
{
    if (index != markerWidthIndex())
    {
        setValue("Board/MarkerLineWidthIndex", index);
    }
}


qreal UBSettings::currentMarkerWidth()
{
    qreal width = 0;

    switch (markerWidthIndex())
    {
        case UBWidth::Fine:
            width = boardMarkerFineWidth->get().toDouble();
            break;
        case UBWidth::Medium:
            width = boardMarkerMediumWidth->get().toDouble();
            break;
        case UBWidth::Strong:
            width = boardMarkerStrongWidth->get().toDouble();
            break;
        default:
            Q_ASSERT(false);
            //failsafe
            width = boardMarkerFineWidth->get().toDouble();
            break;
    }

    return width;
}


int UBSettings::markerColorIndex()
{
    return value("Board/MarkerColorIndex", 0).toInt();
}


void UBSettings::setMarkerColorIndex(int index)
{
    if (index != markerColorIndex())
    {
        setValue("Board/MarkerColorIndex", index);
    }
}


QColor UBSettings::currentMarkerColor()
{
    return markerColor(isDarkBackground());
}


QColor UBSettings::markerColor(bool onDarkBackground)
{
    QList<QColor> colors = markerColors(onDarkBackground);
    return colors.at(markerColorIndex());
}


QList<QColor> UBSettings::markerColors(bool onDarkBackground)
{
    if (onDarkBackground)
    {
        return boardMarkerDarkBackgroundSelectedColors->colors();
    }
    else
    {
        return boardMarkerLightBackgroundSelectedColors->colors();
    }
}

691 692
//----------------------------------------//
// eraser
Claudio Valerio's avatar
Claudio Valerio committed
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764

int UBSettings::eraserWidthIndex()
{
    return value("Board/EraserCircleWidthIndex", 1).toInt();
}

void UBSettings::setEraserWidthIndex(int index)
{
    setValue("Board/EraserCircleWidthIndex", index);
}

qreal UBSettings::eraserFineWidth()
{
    return value("Board/EraserFineWidth", 16).toDouble();
}

void UBSettings::setEraserFineWidth(qreal width)
{
    setValue("Board/EraserFineWidth", width);
}

qreal UBSettings::eraserMediumWidth()
{
    return value("Board/EraserMediumWidth", 64).toDouble();
}

void UBSettings::setEraserMediumWidth(qreal width)
{
    setValue("Board/EraserMediumWidth", width);
}

qreal UBSettings::eraserStrongWidth()
{
    return value("Board/EraserStrongWidth", 128).toDouble();
}

void UBSettings::setEraserStrongWidth(qreal width)
{
    setValue("Board/EraserStrongWidth", width);
}

qreal UBSettings::currentEraserWidth()
{
    qreal width = 0;

    switch (eraserWidthIndex())
    {
        case UBWidth::Fine:
            width = eraserFineWidth();
            break;
        case UBWidth::Medium:
            width = eraserMediumWidth();
            break;
        case UBWidth::Strong:
            width = eraserStrongWidth();
            break;
        default:
            Q_ASSERT(false);
            //failsafe
            width = eraserFineWidth();
            break;
    }

    return width;
}

bool UBSettings::isDarkBackground()
{
    return value("Board/DarkBackground", 0).toBool();
}


Craig Watson's avatar
Craig Watson committed
765
UBPageBackground UBSettings::pageBackground()
Claudio Valerio's avatar
Claudio Valerio committed
766
{
Craig Watson's avatar
Craig Watson committed
767 768 769 770 771 772 773 774
    QString val = value("Board/PageBackground", 0).toString();

    if (val == "crossed")
        return UBPageBackground::crossed;
    else if (val == "ruled")
        return UBPageBackground::ruled;
    else
        return UBPageBackground::plain;
Claudio Valerio's avatar
Claudio Valerio committed
775 776 777 778 779 780 781 782 783 784
}


void UBSettings::setDarkBackground(bool isDarkBackground)
{
    setValue("Board/DarkBackground", isDarkBackground);
    emit colorContextChanged();
}


Craig Watson's avatar
Craig Watson committed
785
void UBSettings::setPageBackground(UBPageBackground background)
Claudio Valerio's avatar
Claudio Valerio committed
786
{
Craig Watson's avatar
Craig Watson committed
787 788 789 790 791 792 793 794 795 796
    QString val;

    if (background == UBPageBackground::crossed)
        val = "crossed";
    else if (background == UBPageBackground::ruled)
        val = "ruled";
    else
        val = "plain";

    setValue("Board/PageBackground", val);
Claudio Valerio's avatar
Claudio Valerio committed
797 798 799 800 801 802 803 804
}


void UBSettings::setPenPressureSensitive(bool sensitive)
{
    boardPenPressureSensitive->set(sensitive);
}

805 806 807 808 809 810 811 812 813
void UBSettings::setPenPreviewCircle(bool circle)
{
    showPenPreviewCircle->set(circle);
}

void UBSettings::setPenPreviewFromSize(int size)
{
    penPreviewFromSize->set(size);
}
Claudio Valerio's avatar
Claudio Valerio committed
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855

void UBSettings::setMarkerPressureSensitive(bool sensitive)
{
    boardMarkerPressureSensitive->set(sensitive);
}


bool UBSettings::isStylusPaletteVisible()
{
    return value("Board/StylusPaletteIsVisible", true).toBool();
}


void UBSettings::setStylusPaletteVisible(bool visible)
{
    setValue("Board/StylusPaletteIsVisible", visible);
}


QString UBSettings::fontFamily()
{
    return value("Board/FontFamily", sDefaultFontFamily).toString();
}


void UBSettings::setFontFamily(const QString &family)
{
    setValue("Board/FontFamily", family);
}


int UBSettings::fontPixelSize()
{
    return value("Board/FontPixelSize", sDefaultFontPixelSize).toInt();
}


void UBSettings::setFontPixelSize(int pixelSize)
{
    setValue("Board/FontPixelSize", pixelSize);
}

Ivan Ilin's avatar
Ivan Ilin committed
856 857 858 859 860 861 862 863 864
int UBSettings::fontPointSize()
{
    return value("Board/FontPointSize", 12).toInt();
}

void UBSettings::setFontPointSize(int pointSize)
{
    setValue("Board/FontPointSize", pointSize);
}
Claudio Valerio's avatar
Claudio Valerio committed
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889

bool UBSettings::isBoldFont()
{
    return value("Board/FontIsBold", false).toBool();
}


void UBSettings::setBoldFont(bool bold)
{
    setValue("Board/FontIsBold", bold);
}


bool UBSettings::isItalicFont()
{
    return value("Board/FontIsItalic", false).toBool();
}


void UBSettings::setItalicFont(bool italic)
{
    setValue("Board/FontIsItalic", italic);
}


890
QString UBSettings::userDataDirectory()
Claudio Valerio's avatar
Claudio Valerio committed
891
{
892 893
    static QString dataDirPath = "";
    if(dataDirPath.isEmpty()){
894 895
        if (getAppSettings() && getAppSettings()->contains("App/DataDirectory")) {
            qDebug() << "getAppSettings()->contains(App/DataDirectory):" << getAppSettings()->contains("App/DataDirectory");
896 897
            dataDirPath = getAppSettings()->value("App/DataDirectory").toString();
            dataDirPath = replaceWildcard(dataDirPath);
Claudio Valerio's avatar
Claudio Valerio committed
898

899 900 901 902
            if(checkDirectory(dataDirPath))
                return dataDirPath;
            else
                qCritical() << "Impossible to create datadirpath " << dataDirPath;
Claudio Valerio's avatar
Claudio Valerio committed
903 904

        }
905
        dataDirPath = UBFileSystemUtils::normalizeFilePath(QStandardPaths::writableLocation(QStandardPaths::DataLocation));
906 907
        if (qApp->organizationName().size() > 0)
            dataDirPath.replace(qApp->organizationName() + "/", "");
Claudio Valerio's avatar
Claudio Valerio committed
908
    }
909
    return dataDirPath;
Claudio Valerio's avatar
Claudio Valerio committed
910 911 912
}


913
QString UBSettings::userImageDirectory()
Claudio Valerio's avatar
Claudio Valerio committed
914
{
915 916 917 918
    static QString imageDirectory = "";
    if(imageDirectory.isEmpty()){
        if (sAppSettings && getAppSettings()->contains("App/UserImageDirectory")) {
            imageDirectory = getAppSettings()->value("App/UserImageDirectory").toString();
Claudio Valerio's avatar
Claudio Valerio committed
919

920 921 922 923 924
            imageDirectory = replaceWildcard(imageDirectory);
            if(checkDirectory(imageDirectory))
                return imageDirectory;
            else
                qCritical() << "failed to create image directory " << imageDirectory;
Claudio Valerio's avatar
Claudio Valerio committed
925 926
        }

927
        imageDirectory = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation) + "/" + qApp->applicationName();
928
        checkDirectory(imageDirectory);
Claudio Valerio's avatar
Claudio Valerio committed
929
    }
930
    return imageDirectory;
Claudio Valerio's avatar
Claudio Valerio committed
931 932 933
}


934
QString UBSettings::userVideoDirectory()
Claudio Valerio's avatar
Claudio Valerio committed
935
{
936 937 938 939 940 941 942 943 944
    static QString videoDirectory = "";
    if(videoDirectory.isEmpty()){
        if (sAppSettings && getAppSettings()->contains("App/UserVideoDirectory")) {
            videoDirectory = getAppSettings()->value("App/UserVideoDirectory").toString();
            videoDirectory = replaceWildcard(videoDirectory);
            if(checkDirectory(videoDirectory))
                return videoDirectory;
            else
                qCritical() << "failed to create video directory " << videoDirectory;
Claudio Valerio's avatar
Claudio Valerio committed
945 946 947
        }


948
        videoDirectory = QStandardPaths::writableLocation(QStandardPaths::MoviesLocation);
Claudio Valerio's avatar
Claudio Valerio committed
949

950
        if(videoDirectory.isEmpty())
951
            videoDirectory = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation) + "/" + tr("My Movies");
Claudio Valerio's avatar
Claudio Valerio committed
952
        else
953
            videoDirectory = videoDirectory + "/" + qApp->applicationName();
Claudio Valerio's avatar
Claudio Valerio committed
954

955
        checkDirectory(videoDirectory);
Claudio Valerio's avatar
Claudio Valerio committed
956
    }
957
    return videoDirectory;
Claudio Valerio's avatar
Claudio Valerio committed
958 959
}

960
QString UBSettings::userAudioDirectory()
Claudio Valerio's avatar
Claudio Valerio committed
961
{
962 963 964 965
    static QString audioDirectory = "";
    if(audioDirectory.isEmpty()){
        if (sAppSettings && getAppSettings()->contains("App/UserAudioDirectory")) {
            audioDirectory = getAppSettings()->value("App/UserAudioDirectory").toString();
Claudio Valerio's avatar
Claudio Valerio committed
966

967 968 969 970 971
            audioDirectory = replaceWildcard(audioDirectory);
            if(checkDirectory(audioDirectory))
                return audioDirectory;
            else
                qCritical() << "failed to create image directory " << audioDirectory;
Claudio Valerio's avatar
Claudio Valerio committed
972 973
        }

974
        audioDirectory = QStandardPaths::writableLocation(QStandardPaths::MusicLocation) + "/" + qApp->applicationName();
975
        checkDirectory(audioDirectory);
Claudio Valerio's avatar
Claudio Valerio committed
976
    }
977 978
    return audioDirectory;
}
Claudio Valerio's avatar
Claudio Valerio committed
979 980


981 982 983 984 985
QString UBSettings::userPodcastRecordingDirectory()
{
    static QString dirPath = "";
    if(dirPath.isEmpty()){
        if (sAppSettings && getAppSettings()->contains("Podcast/RecordingDirectory"))
Claudio Valerio's avatar
Claudio Valerio committed
986
        {
987 988 989 990 991 992 993
            dirPath = getAppSettings()->value("Podcast/RecordingDirectory").toString();
            dirPath = replaceWildcard(dirPath);
            if(checkDirectory(dirPath))
                return dirPath;
            else
                qCritical() << "failed to create dir " << dirPath;

Claudio Valerio's avatar
Claudio Valerio committed
994
        }
995
        dirPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
996
        checkDirectory(dirPath);
Claudio Valerio's avatar
Claudio Valerio committed
997
    }
998
    return dirPath;
Claudio Valerio's avatar
Claudio Valerio committed
999 1000
}

1001
QString UBSettings::userDocumentDirectory()
Claudio Valerio's avatar
Claudio Valerio committed
1002
{
1003 1004 1005 1006 1007
    static QString documentDirectory = "";
    if(documentDirectory.isEmpty()){
        documentDirectory = userDataDirectory() + "/document";
        checkDirectory(documentDirectory);
    }
1008
    qDebug() << "userDocumentDirectory()" << documentDirectory;
1009
    return documentDirectory;
Claudio Valerio's avatar
Claudio Valerio committed
1010 1011
}

1012
QString UBSettings::userFavoriteListFilePath()
Claudio Valerio's avatar
Claudio Valerio committed
1013
{
1014 1015 1016 1017
    static QString filePath = "";
    if(filePath.isEmpty()){
        QString dirPath = userDataDirectory() + "/libraryPalette";
        checkDirectory(dirPath);
1018
        filePath = dirPath + "/favorite.dat";
1019
    }
Claudio Valerio's avatar
Claudio Valerio committed
1020 1021 1022
    return filePath;
}

1023
QString UBSettings::userTrashDirPath()
Claudio Valerio's avatar
Claudio Valerio committed
1024
{
1025 1026
    static QString trashPath = "";
    if(trashPath.isEmpty()){
1027
        trashPath = userDataDirectory() + "/libraryPalette/trash";
1028 1029
        checkDirectory(trashPath);
    }
Claudio Valerio's avatar
Claudio Valerio committed
1030 1031 1032 1033
    return trashPath;
}


1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
QString UBSettings::userGipLibraryDirectory()
{
    static QString dirPath = "";
    if(dirPath.isEmpty()){
        dirPath = userDataDirectory() + "/library/gips";
        checkDirectory(dirPath);
    }
    return dirPath;
}


1045
QString UBSettings::applicationShapeLibraryDirectory()
Claudio Valerio's avatar
Claudio Valerio committed
1046 1047 1048 1049 1050
{
    QString defaultRelativePath = QString("./library/shape");

    QString configPath = value("Library/ShapeDirectory", QVariant(defaultRelativePath)).toString();

1051
    if (configPath.startsWith(".")) {
Claudio Valerio's avatar
Claudio Valerio committed
1052 1053
        return UBPlatformUtils::applicationResourcesDirectory() + configPath.right(configPath.size() - 1);
    }
1054
    else {
Claudio Valerio's avatar
Claudio Valerio committed
1055 1056 1057 1058
        return configPath;
    }
}

Claudio Valerio's avatar
Claudio Valerio committed
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
QString UBSettings::applicationCustomizationDirectory()
{
    QString defaultRelativePath = QString("/customizations");
    return UBPlatformUtils::applicationResourcesDirectory() + defaultRelativePath;

}

QString UBSettings::applicationCustomFontDirectory()
{
    QString defaultFontDirectory = "/fonts";
    return applicationCustomizationDirectory() + defaultFontDirectory;
}

1072
QString UBSettings::userSearchDirectory()
1073
{
1074 1075 1076 1077 1078
    static QString dirPath = "";
    if(dirPath.isEmpty()){
        dirPath = UBPlatformUtils::applicationResourcesDirectory() + "/library/search";
        checkDirectory(dirPath);
    }
1079 1080 1081
    return dirPath;
}

1082
QString UBSettings::applicationImageLibraryDirectory()
Claudio Valerio's avatar
Claudio Valerio committed
1083
{
1084
    QString defaultRelativePath = QString("./library/pictures");
Claudio Valerio's avatar
Claudio Valerio committed
1085

Claudio Valerio's avatar
Claudio Valerio committed
1086
    QString configPath = value("Library/ImageDirectory", QVariant(defaultRelativePath)).toString();
Claudio Valerio's avatar
Claudio Valerio committed
1087

1088
    if (configPath.startsWith(".")) {
Claudio Valerio's avatar
Claudio Valerio committed
1089 1090
        return UBPlatformUtils::applicationResourcesDirectory() + configPath.right(configPath.size() - 1);
    }
1091
    else {
Claudio Valerio's avatar
Claudio Valerio committed
1092 1093 1094 1095
        return configPath;
    }
}

1096
QString UBSettings::userAnimationDirectory()
Claudio Valerio's avatar
Claudio Valerio committed
1097
{
1098 1099 1100 1101
    static QString animationDirectory = "";
    if(animationDirectory.isEmpty()){
        animationDirectory = userDataDirectory() + "/animationUserDirectory";
        checkDirectory(animationDirectory);
Claudio Valerio's avatar
Claudio Valerio committed
1102
    }
1103 1104 1105
    return animationDirectory;
}

1106
QString UBSettings::userInteractiveDirectory()
Claudio Valerio's avatar
Claudio Valerio committed
1107
{
1108 1109 1110 1111 1112 1113 1114 1115 1116
    static QString interactiveDirectory = "";
    if(interactiveDirectory.isEmpty()){
        if (sAppSettings && getAppSettings()->contains("App/UserInteractiveContentDirectory")) {
            interactiveDirectory = getAppSettings()->value("App/UserInteractiveContentDirectory").toString();
            interactiveDirectory = replaceWildcard(interactiveDirectory);
            if(checkDirectory(interactiveDirectory))
                return interactiveDirectory;
            else
                qCritical() << "failed to create directory " << interactiveDirectory;
Claudio Valerio's avatar
Claudio Valerio committed
1117
        }
1118 1119
        interactiveDirectory = userDataDirectory() + "/interactive content";
        checkDirectory(interactiveDirectory);
Claudio Valerio's avatar
Claudio Valerio committed
1120
    }
1121
    return interactiveDirectory;
Claudio Valerio's avatar
Claudio Valerio committed
1122 1123 1124
}


1125
QString UBSettings::applicationInteractivesDirectory()
1126
{
1127
    QString defaultRelativePath = QString("./library/interactivities");
1128

1129
    QString configPath = value("Library/InteractivitiesDirectory", QVariant(defaultRelativePath)).toString();
1130

1131
    if (configPath.startsWith(".")) {
1132 1133
        return UBPlatformUtils::applicationResourcesDirectory() + configPath.right(configPath.size() - 1);
    }
1134
    else {
1135 1136 1137 1138
        return configPath;
    }
}

1139
QString UBSettings::applicationApplicationsLibraryDirectory()
Claudio Valerio's avatar
Claudio Valerio committed
1140
{
1141
    QString defaultRelativePath = QString("./library/applications");
Claudio Valerio's avatar
Claudio Valerio committed
1142

1143
    QString configPath = value("Library/ApplicationsDirectory", QVariant(defaultRelativePath)).toString();
Claudio Valerio's avatar
Claudio Valerio committed
1144

1145
    if (configPath.startsWith(".")) {
Claudio Valerio's avatar
Claudio Valerio committed
1146 1147
        return UBPlatformUtils::applicationResourcesDirectory() + configPath.right(configPath.size() - 1);
    }
1148
    else {
Claudio Valerio's avatar
Claudio Valerio committed
1149 1150 1151 1152 1153
        return configPath;
    }
}


1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
QString UBSettings::applicationAudiosLibraryDirectory()
{
    QString defaultRelativePath = QString("./library/audios");

    QString configPath = value("Library/AudiosDirectory", QVariant(defaultRelativePath)).toString();

    if (configPath.startsWith(".")) {
        return UBPlatformUtils::applicationResourcesDirectory() + configPath.right(configPath.size() - 1);
    }
    else {
        return configPath;
    }
}

QString UBSettings::applicationVideosLibraryDirectory()
{
    QString defaultRelativePath = QString("./library/videos");

    QString configPath = value("Library/VideosDirectory", QVariant(defaultRelativePath)).toString();

    if (configPath.startsWith(".")) {
        return UBPlatformUtils::applicationResourcesDirectory() + configPath.right(configPath.size() - 1);
    }
    else {
        return configPath;
    }
}

QString UBSettings::applicationAnimationsLibraryDirectory()
{
    QString defaultRelativePath = QString("./library/animations");

    QString configPath = value("Library/AnimationsDirectory", QVariant(defaultRelativePath)).toString();

    if (configPath.startsWith(".")) {
        return UBPlatformUtils::applicationResourcesDirectory() + configPath.right(configPath.size() - 1);
    }
    else {
        return configPath;
    }
}

1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
QString UBSettings::applicationStartupHintsDirectory()
{
    QString defaultRelativePath = QString("./startupHints");

    QString configPath = value("StartupHintsDirectory", QVariant(defaultRelativePath)).toString();

    if (configPath.startsWith(".")) {
        return UBPlatformUtils::applicationResourcesDirectory() + configPath.right(configPath.size() - 1);
    }
    else
        return configPath;
}

1209
QString UBSettings::userInteractiveFavoritesDirectory()
Claudio Valerio's avatar
Claudio Valerio committed
1210
{
1211 1212 1213 1214 1215 1216 1217 1218 1219
    static QString dirPath = "";
    if(dirPath.isEmpty()){
        if (sAppSettings && getAppSettings()->contains("App/UserInteractiveFavoritesDirectory")) {
            dirPath = getAppSettings()->value("App/UserInteractiveFavoritesDirectory").toString();
            dirPath = replaceWildcard(dirPath);
            if(checkDirectory(dirPath))
                return dirPath;
            else
                qCritical() << "failed to create directory " << dirPath;
Claudio Valerio's avatar
Claudio Valerio committed
1220 1221
        }

1222 1223
        dirPath = userDataDirectory() + "/interactive favorites";
        checkDirectory(dirPath);
Claudio Valerio's avatar
Claudio Valerio committed
1224
    }
1225
    return dirPath;
Claudio Valerio's avatar
Claudio Valerio committed
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
}


QNetworkProxy* UBSettings::httpProxy()
{
    QNetworkProxy* proxy = 0;

    if (mAppSettings->value("Proxy/Enabled", false).toBool()) {

        proxy = new QNetworkProxy();

        if (mAppSettings->value("Proxy/Type", "HTTP").toString() == "Socks5")
            proxy->setType(QNetworkProxy::Socks5Proxy);
        else
            proxy->setType(QNetworkProxy::HttpProxy);

        proxy->setHostName(mAppSettings->value("Proxy/HostName").toString());
        proxy->setPort(mAppSettings->value("Proxy/Port", 1080).toInt());
        proxy->setUser(mAppSettings->value("Proxy/UserName").toString());
        proxy->setPassword(mAppSettings->value("Proxy/Password").toString());
    }

    return proxy;
}


void UBSettings::setPassword(const QString& id, const QString& password)
{
    QString encrypted = UBCryptoUtils::instance()->symetricEncrypt(password);

    mUserSettings->setValue(QString("Vault/") + id, encrypted);
}


void UBSettings::removePassword(const QString& id)
{
    mUserSettings->remove(QString("Vault/") + id);
}


QString UBSettings::password(const QString& id)
{
    QString encrypted = mUserSettings->value(QString("Vault/") + id).toString();

    QString result = "";

    if (encrypted.length() > 0)
        result =  UBCryptoUtils::instance()->symetricDecrypt(encrypted);

    return result;
}


QString UBSettings::proxyUsername()
{
    QString idUsername = "http.proxy.user";
    return password(idUsername);
}


void UBSettings::setProxyUsername(const QString& username)
{
    QString idUsername = "http.proxy.user";

    if (username.length() > 0)
        setPassword(idUsername, username);
    else
        removePassword(idUsername);
}


QString UBSettings::proxyPassword()
{
    QString idPassword = "http.proxy.pass";
    return password(idPassword);
}


void UBSettings::setProxyPassword(const QString& password)
{
    QString idPassword = "http.proxy.pass";

    if (password.length() > 0)
       setPassword(idPassword, password);
    else
        removePassword(idPassword);

}

1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
QString UBSettings::communityUsername()
{
    return communityUser->get().toString();
}

void UBSettings::setCommunityUsername(const QString &username)
{
    communityUser->set(QVariant(username));
}

QString UBSettings::communityPassword()
{
    return communityPsw->get().toString();
}

void UBSettings::setCommunityPassword(const QString &password)
{
    communityPsw->set(QVariant(password));
}
1334

Claudio Valerio's avatar
Claudio Valerio committed
1335 1336
void UBSettings::setCommunityPersistence(const bool persistence)
{
Claudio Valerio's avatar
Claudio Valerio committed
1337
    communityCredentialsPersistence->set(QVariant(persistence));
Claudio Valerio's avatar
Claudio Valerio committed
1338 1339
}

shibakaneki's avatar
shibakaneki committed
1340
int UBSettings::libraryIconSize(){
Claudio Valerio's avatar
Claudio Valerio committed
1341
    return libIconSize->get().toInt();
shibakaneki's avatar
shibakaneki committed
1342 1343 1344
}

void UBSettings::setLibraryIconsize(const int& size){
Claudio Valerio's avatar
Claudio Valerio committed
1345
    libIconSize->set(QVariant(size));
shibakaneki's avatar
shibakaneki committed
1346
}
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361

bool UBSettings::checkDirectory(QString& dirPath)
{
    bool result = true;
    QDir dir(dirPath);
    if(!dir.exists())
        result = dir.mkpath(dirPath);
    return result;
}

QString UBSettings::replaceWildcard(QString& path)
{
    QString result(path);

    if (result.startsWith("{Documents}")) {
1362
        result = result.replace("{Documents}", QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation));
1363 1364
    }
    else if(result.startsWith("{Home}")) {
1365
        result = result.replace("{Home}", QStandardPaths::writableLocation(QStandardPaths::HomeLocation));
1366
    }
1367
    else if(result.startsWith("{Desktop}")) {
1368
        result = result.replace("{Desktop}", QStandardPaths::writableLocation(QStandardPaths::DesktopLocation));
1369
    }
1370 1371 1372 1373 1374 1375 1376

    if(result.contains("{UserLoginName}") && UBPlatformUtils::osUserLoginName().length() > 0) {
        result = result.replace("{UserLoginName}", UBPlatformUtils::osUserLoginName());
    }

    return result;
}
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
1377

Claudio Valerio's avatar
Claudio Valerio committed
1378 1379
void UBSettings::closing()
{
1380
    save();
Claudio Valerio's avatar
Claudio Valerio committed
1381 1382 1383 1384 1385
    cleanNonPersistentSettings();
}

void UBSettings::cleanNonPersistentSettings()
{
Claudio Valerio's avatar
Claudio Valerio committed
1386
    if(!communityCredentialsPersistence->get().toBool()){
Claudio Valerio's avatar
Claudio Valerio committed
1387 1388 1389
        communityPsw->set(QVariant(""));
        communityUser->set(QVariant(""));
    }
Claudio Valerio's avatar
Claudio Valerio committed
1390 1391 1392 1393 1394

    if(!youTubeCredentialsPersistence->get().toBool()){
        removePassword(youTubeUserEMail->get().toString());
        youTubeUserEMail->set(QVariant(""));
    }
Claudio Valerio's avatar
Claudio Valerio committed
1395
}
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422

/**
 * @brief Permanently remove a setting, from local memory and config files
 * @param setting The setting to remove
 */
void UBSettings::removeSetting(const QString &setting)
{
    if (sAppSettings->contains(setting))
        sAppSettings->remove(setting);

    if (mUserSettings->contains(setting))
        mUserSettings->remove(setting);

    if (mSettingsQueue.contains(setting))
        mSettingsQueue.remove(setting);
}

void UBSettings::checkNewSettings()
{
    /*
     * Some settings were modified in new versions and OpenBoard can crash
     * if an old settings file is used. This function checks these settings and
     * if necessary, resets them to the values initialized in UBSettings::init().
     *
     * Thus this method can be removed when it is no longer deemed useful.
     */

Craig Watson's avatar
Craig Watson committed
1423
    // OB 1.3 introduced an extra pen color;  for simplicity, if the old settings
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
    // are still present (i.e only 4 selected pen colors), we just reset all color settings.
    // Having too few colors actually causes OpenBoard to crash, hence these measures.

    QList<UBColorListSetting*> colorSettings;
    colorSettings << boardPenDarkBackgroundSelectedColors
                  << boardPenLightBackgroundSelectedColors
                  << boardMarkerDarkBackgroundSelectedColors
                  << boardMarkerLightBackgroundSelectedColors;

    foreach (UBColorListSetting* setting, colorSettings) {
        if (setting->colors().size() < 5)
Craig Watson's avatar
Craig Watson committed
1435
            setting->reset();
1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464
    }

    colorSettings.clear();

    // Next we check whether all the new colors were added; i.e if some default
    // colors are not also in the config file, we add them to it.

    // This is not nearly as critical as the above issue, but the users (or admins) seemingly
    // can't be trusted to delete or modify their own config file when upgrading, so they might
    // wonder why they don't have the new colors in their app.

    colorSettings << boardPenDarkBackgroundColors
                  << boardPenLightBackgroundColors
                  << boardMarkerDarkBackgroundColors
                  << boardMarkerLightBackgroundColors;

    foreach (UBColorListSetting* setting, colorSettings) {
        QStringList defaultColors = qvariant_cast<QStringList>(setting->defaultValue());
        QStringList currentColors(qvariant_cast<QStringList>(setting->get())); // copy

        foreach (QString c, defaultColors) {
            if (!currentColors.contains(c))
                currentColors.append(QString(c));
        }

        setting->set(currentColors);
    }


Craig Watson's avatar
Craig Watson committed
1465
    // A typo was corrected in version 1.3
1466 1467
    removeSetting("Board/useSystemOnScreenKeybard");

Craig Watson's avatar
Craig Watson committed
1468 1469 1470 1471

    // CrossedBackground changed in 1.4 (no longer a bool but an enum; can be crossed or ruled)
    removeSetting("Board/CrossedBackground");

1472
}