OSDN Git Service

Updated changelog.
[lamexp/LameXP.git] / src / Global_Utils.cpp
index 0d3c76a..727401f 100644 (file)
@@ -1,12 +1,12 @@
 ///////////////////////////////////////////////////////////////////////////////
 // LameXP - Audio Encoder Front-End
-// Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
+// Copyright (C) 2004-2023 LoRd_MuldeR <MuldeR2@GMX.de>
 //
 // This program is free software; you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
+// it under the terms of the GNU GENERAL PUBLIC LICENSE as published by
 // the Free Software Foundation; either version 2 of the License, or
-// (at your option) any later version, but always including the *additional*
-// restrictions defined in the "License.txt" file.
+// (at your option) any later version; always including the non-optional
+// LAMEXP GNU GENERAL PUBLIC LICENSE ADDENDUM. See "License.txt" file!
 //
 // This program is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -37,6 +37,7 @@
 
 //MUtils
 #include <MUtils/Global.h>
+#include <MUtils/OSSupport.h>
 
 //CRT includes
 #include <time.h>
 // GLOBAL VARS
 ///////////////////////////////////////////////////////////////////////////////
 
-static struct
-{
-       QIcon *appIcon;
-       QReadWriteLock lock;
-}
-g_lamexp_app_icon;
+static QScopedPointer<QIcon> g_lamexp_icon_data;
+static QReadWriteLock        g_lamexp_icon_lock;
 
 ///////////////////////////////////////////////////////////////////////////////
 // GLOBAL FUNCTIONS
 ///////////////////////////////////////////////////////////////////////////////
 
 /*
- * Remove forbidden characters from a filename
- */
-const QString lamexp_clean_filename(const QString &str)
-{
-       QString newStr(str);
-       QRegExp rx("\"(.+)\"");
-       rx.setMinimal(true);
-
-       newStr.replace("\\", "-");
-       newStr.replace(" / ", ", ");
-       newStr.replace("/", ",");
-       newStr.replace(":", "-");
-       newStr.replace("*", "x");
-       newStr.replace("?", "");
-       newStr.replace("<", "[");
-       newStr.replace(">", "]");
-       newStr.replace("|", "!");
-       newStr.replace(rx, "`\\1ยด");
-       newStr.replace("\"", "'");
-       
-       return newStr.simplified();
-}
-
-/*
- * Remove forbidden characters from a file path
- */
-const QString lamexp_clean_filepath(const QString &str)
-{
-       QStringList parts = QString(str).replace("\\", "/").split("/");
-
-       for(int i = 0; i < parts.count(); i++)
-       {
-               parts[i] = lamexp_clean_filename(parts[i]);
-       }
-
-       return parts.join("/");
-}
-
-/*
- * Get a list of all available Qt Text Codecs
- */
-QStringList lamexp_available_codepages(bool noAliases)
-{
-       QStringList codecList;
-       
-       QList<QByteArray> availableCodecs = QTextCodec::availableCodecs();
-       while(!availableCodecs.isEmpty())
-       {
-               QByteArray current = availableCodecs.takeFirst();
-               if(!(current.startsWith("system") || current.startsWith("System")))
-               {
-                       codecList << QString::fromLatin1(current.constData(), current.size());
-                       if(noAliases)
-                       {
-                               if(QTextCodec *currentCodec = QTextCodec::codecForName(current.constData()))
-                               {
-                                       
-                                       QList<QByteArray> aliases = currentCodec->aliases();
-                                       while(!aliases.isEmpty()) availableCodecs.removeAll(aliases.takeFirst());
-                               }
-                       }
-               }
-       }
-
-       return codecList;
-}
-
-/*
- * Make a window blink (to draw user's attention)
- */
-void lamexp_blink_window(QWidget *poWindow, unsigned int count, unsigned int delay)
-{
-       static QMutex blinkMutex;
-
-       const double maxOpac = 1.0;
-       const double minOpac = 0.3;
-       const double delOpac = 0.1;
-
-       if(!blinkMutex.tryLock())
-       {
-               qWarning("Blinking is already in progress, skipping!");
-               return;
-       }
-       
-       try
-       {
-               const int steps = static_cast<int>(ceil(maxOpac - minOpac) / delOpac);
-               const int sleep = static_cast<int>(floor(static_cast<double>(delay) / static_cast<double>(steps)));
-               const double opacity = poWindow->windowOpacity();
-       
-               for(unsigned int i = 0; i < count; i++)
-               {
-                       for(double x = maxOpac; x >= minOpac; x -= delOpac)
-                       {
-                               poWindow->setWindowOpacity(x);
-                               QApplication::processEvents();
-                               lamexp_sleep(sleep);
-                       }
-
-                       for(double x = minOpac; x <= maxOpac; x += delOpac)
-                       {
-                               poWindow->setWindowOpacity(x);
-                               QApplication::processEvents();
-                               lamexp_sleep(sleep);
-                       }
-               }
-
-               poWindow->setWindowOpacity(opacity);
-               QApplication::processEvents();
-               blinkMutex.unlock();
-       }
-       catch(...)
-       {
-               blinkMutex.unlock();
-               qWarning("Exception error while blinking!");
-       }
-}
-
-/*
  * Computus according to H. Lichtenberg
  */
 static bool lamexp_computus(const QDate &date)
@@ -229,66 +107,85 @@ static bool lamexp_thanksgiving(const QDate &date)
  */
 const QIcon &lamexp_app_icon(void)
 {
-       QReadLocker readLock(&g_lamexp_app_icon.lock);
+       QReadLocker readLock(&g_lamexp_icon_lock);
 
        //Already initialized?
-       if(g_lamexp_app_icon.appIcon)
+       if(!g_lamexp_icon_data.isNull())
        {
-               return *g_lamexp_app_icon.appIcon;
+               return *g_lamexp_icon_data;
        }
 
        readLock.unlock();
-       QWriteLocker writeLock(&g_lamexp_app_icon.lock);
+       QWriteLocker writeLock(&g_lamexp_icon_lock);
 
-       while(!g_lamexp_app_icon.appIcon)
+       while(g_lamexp_icon_data.isNull())
        {
                QDate currentDate = QDate::currentDate();
                QTime currentTime = QTime::currentTime();
        
                if(lamexp_thanksgiving(currentDate))
                {
-                       g_lamexp_app_icon.appIcon = new QIcon(":/MainIcon6.png");
+                       g_lamexp_icon_data.reset(new QIcon(":/MainIcon6.png"));
                }
                else if(((currentDate.month() == 12) && (currentDate.day() == 31) && (currentTime.hour() >= 20)) || ((currentDate.month() == 1) && (currentDate.day() == 1)  && (currentTime.hour() <= 19)))
                {
-                       g_lamexp_app_icon.appIcon = new QIcon(":/MainIcon5.png");
+                       g_lamexp_icon_data.reset(new QIcon(":/MainIcon5.png"));
                }
                else if(((currentDate.month() == 10) && (currentDate.day() == 31) && (currentTime.hour() >= 12)) || ((currentDate.month() == 11) && (currentDate.day() == 1)  && (currentTime.hour() <= 11)))
                {
-                       g_lamexp_app_icon.appIcon = new QIcon(":/MainIcon4.png");
+                       g_lamexp_icon_data.reset(new QIcon(":/MainIcon4.png"));
                }
                else if((currentDate.month() == 12) && (currentDate.day() >= 24) && (currentDate.day() <= 26))
                {
-                       g_lamexp_app_icon.appIcon = new QIcon(":/MainIcon3.png");
+                       g_lamexp_icon_data.reset(new QIcon(":/MainIcon3.png"));
                }
                else if(lamexp_computus(currentDate))
                {
-                       g_lamexp_app_icon.appIcon = new QIcon(":/MainIcon2.png");
+                       g_lamexp_icon_data.reset(new QIcon(":/MainIcon2.png"));
                }
                else
                {
-                       g_lamexp_app_icon.appIcon = new QIcon(":/MainIcon1.png");
+                       g_lamexp_icon_data.reset(new QIcon(":/MainIcon1.png"));
                }
        }
 
-       return *g_lamexp_app_icon.appIcon;
+       return *g_lamexp_icon_data;
 }
 
-///////////////////////////////////////////////////////////////////////////////
-// INITIALIZATION
-///////////////////////////////////////////////////////////////////////////////
-
-extern "C" void _lamexp_global_init_utils(void)
+/*
+ * Version number to human-readable string
+ */
+const QString lamexp_version2string(const QString &pattern, unsigned int version, const QString &defaultText, const QString &tag)
 {
-       LAMEXP_ZERO_MEMORY(g_lamexp_app_icon);
-}
+       if(version == UINT_MAX)
+       {
+               return defaultText;
+       }
+       
+       QString result = pattern;
+       const int digits = result.count(QChar(L'?'), Qt::CaseInsensitive);
+       
+       if(digits < 1)
+       {
+               return result;
+       }
+       
+       int pos = 0, index = -1;
+       const QString versionStr = QString().sprintf("%0*u", digits, version);
+       Q_ASSERT(versionStr.length() == digits);
+       
+       while((index = result.indexOf(QChar(L'?'), Qt::CaseInsensitive)) >= 0)
+       {
+               result[index] = versionStr[pos++];
+       }
 
-///////////////////////////////////////////////////////////////////////////////
-// FINALIZATION
-///////////////////////////////////////////////////////////////////////////////
+       if(!tag.isEmpty())
+       {
+               if((index = result.indexOf(QChar(L'#'), Qt::CaseInsensitive)) >= 0)
+               {
+                       result.remove(index, 1).insert(index, tag);
+               }
+       }
 
-extern "C" void _lamexp_global_free_utils(void)
-{
-       //Free memory
-       MUTILS_DELETE(g_lamexp_app_icon.appIcon);
+       return result;
 }