File indexing completed on 2024-04-21 16:31:51

0001 /**
0002  * SPDX-FileCopyrightText: (C) 2003 Sébastien Laoût <slaout@linux62.org>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "global.h"
0008 
0009 #include <KConfig>
0010 #include <KMainWindow>
0011 
0012 #include <QApplication>
0013 #include <QStandardPaths>
0014 #include <QtCore/QDir>
0015 #include <QtCore/QString>
0016 
0017 #include "bnpview.h"
0018 #include "gitwrapper.h"
0019 #include "settings.h"
0020 
0021 /** Define initial values for global variables : */
0022 
0023 QString Global::s_customSavesFolder;
0024 DebugWindow *Global::debugWindow = nullptr;
0025 BackgroundManager *Global::backgroundManager = nullptr;
0026 BNPView *Global::bnpView = nullptr;
0027 KSharedConfig::Ptr Global::basketConfig;
0028 QCommandLineParser *Global::commandLineOpts = nullptr;
0029 MainWindow *Global::mainWnd = nullptr;
0030 
0031 void Global::setCustomSavesFolder(const QString &folder)
0032 {
0033     s_customSavesFolder = folder;
0034 }
0035 
0036 QString Global::savesFolder()
0037 {
0038     static QString *folder = nullptr; // Memorize the folder to do not have to re-compute it each time it's needed
0039 
0040     if (folder == nullptr) {                  // Initialize it if not yet done
0041         if (!s_customSavesFolder.isEmpty()) { // Passed by command line (for development & debug purpose)
0042             QDir dir;
0043             dir.mkdir(s_customSavesFolder);
0044             folder = new QString(s_customSavesFolder.endsWith("/") ? s_customSavesFolder : s_customSavesFolder + '/');
0045         } else if (!Settings::dataFolder().isEmpty()) { // Set by config option (in Basket -> Backup & Restore)
0046             folder = new QString(Settings::dataFolder().endsWith("/") ? Settings::dataFolder() : Settings::dataFolder() + '/');
0047         } else { // The default path (should be that for most computers)
0048             folder = new QString(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1Char('/') + "basket/");
0049             initializeGitIfNeeded(*folder);
0050         }
0051     }
0052     return *folder;
0053 }
0054 
0055 QString Global::basketsFolder()
0056 {
0057     return savesFolder() + "baskets/";
0058 }
0059 QString Global::backgroundsFolder()
0060 {
0061     return savesFolder() + "backgrounds/";
0062 }
0063 QString Global::templatesFolder()
0064 {
0065     return savesFolder() + "templates/";
0066 }
0067 QString Global::tempCutFolder()
0068 {
0069     return savesFolder() + "temp-cut/";
0070 }
0071 QString Global::gitFolder()
0072 {
0073     return savesFolder() + ".git/";
0074 }
0075 
0076 void Global::initializeGitIfNeeded(QString savesFolder)
0077 {
0078     if (!QDir(savesFolder + ".git/").exists()) {
0079         GitWrapper::initializeGitRepository(savesFolder);
0080     }
0081 }
0082 
0083 QString Global::openNoteIcon() // FIXME: Now an edit icon
0084 {
0085     return QVariant(Global::bnpView->m_actEditNote->icon()).toString();
0086 }
0087 
0088 KMainWindow *Global::activeMainWindow()
0089 {
0090     QWidget *res = qApp->activeWindow();
0091 
0092     if (res && res->inherits("KMainWindow")) {
0093         return static_cast<KMainWindow *>(res);
0094     }
0095     return nullptr;
0096 }
0097 
0098 MainWindow *Global::mainWindow()
0099 {
0100     return mainWnd;
0101 }
0102 
0103 KConfig *Global::config()
0104 {
0105     // The correct solution is to go and replace all KConfig* with KSharedConfig::Ptr, but that seems awfully annoying to do right now
0106     return Global::basketConfig.data();
0107 }