Adapt for latest changes in MUtilities library.
[LameXP.git] / src / Global_Utils.cpp
blob2aba4a0abc071e6c26fe644afa0a26ad7314859f
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2019 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
23 #include "Global.h"
25 //Qt includes
26 #include <QApplication>
27 #include <QDate>
28 #include <QDir>
29 #include <QFileInfo>
30 #include <QIcon>
31 #include <QMutex>
32 #include <QProcessEnvironment>
33 #include <QReadWriteLock>
34 #include <QTextCodec>
35 #include <QUuid>
36 #include <QWidget>
38 //MUtils
39 #include <MUtils/Global.h>
40 #include <MUtils/OSSupport.h>
42 //CRT includes
43 #include <time.h>
44 #include <process.h>
46 ///////////////////////////////////////////////////////////////////////////////
47 // GLOBAL VARS
48 ///////////////////////////////////////////////////////////////////////////////
50 static QScopedPointer<QIcon> g_lamexp_icon_data;
51 static QReadWriteLock g_lamexp_icon_lock;
53 ///////////////////////////////////////////////////////////////////////////////
54 // GLOBAL FUNCTIONS
55 ///////////////////////////////////////////////////////////////////////////////
58 * Computus according to H. Lichtenberg
60 static bool lamexp_computus(const QDate &date)
62 int X = date.year();
63 int A = X % 19;
64 int K = X / 100;
65 int M = 15 + (3*K + 3) / 4 - (8*K + 13) / 25;
66 int D = (19*A + M) % 30;
67 int S = 2 - (3*K + 3) / 4;
68 int R = D / 29 + (D / 28 - D / 29) * (A / 11);
69 int OG = 21 + D - R;
70 int SZ = 7 - (X + X / 4 + S) % 7;
71 int OE = 7 - (OG - SZ) % 7;
72 int OS = (OG + OE);
74 if(OS > 31)
76 return (date.month() == 4) && (date.day() == (OS - 31));
78 else
80 return (date.month() == 3) && (date.day() == OS);
85 * Check for Thanksgiving
87 static bool lamexp_thanksgiving(const QDate &date)
89 int day = 0;
91 switch(QDate(date.year(), 11, 1).dayOfWeek())
93 case 1: day = 25; break;
94 case 2: day = 24; break;
95 case 3: day = 23; break;
96 case 4: day = 22; break;
97 case 5: day = 28; break;
98 case 6: day = 27; break;
99 case 7: day = 26; break;
102 return (date.month() == 11) && (date.day() == day);
106 * Initialize app icon
108 const QIcon &lamexp_app_icon(void)
110 QReadLocker readLock(&g_lamexp_icon_lock);
112 //Already initialized?
113 if(!g_lamexp_icon_data.isNull())
115 return *g_lamexp_icon_data;
118 readLock.unlock();
119 QWriteLocker writeLock(&g_lamexp_icon_lock);
121 while(g_lamexp_icon_data.isNull())
123 QDate currentDate = QDate::currentDate();
124 QTime currentTime = QTime::currentTime();
126 if(lamexp_thanksgiving(currentDate))
128 g_lamexp_icon_data.reset(new QIcon(":/MainIcon6.png"));
130 else if(((currentDate.month() == 12) && (currentDate.day() == 31) && (currentTime.hour() >= 20)) || ((currentDate.month() == 1) && (currentDate.day() == 1) && (currentTime.hour() <= 19)))
132 g_lamexp_icon_data.reset(new QIcon(":/MainIcon5.png"));
134 else if(((currentDate.month() == 10) && (currentDate.day() == 31) && (currentTime.hour() >= 12)) || ((currentDate.month() == 11) && (currentDate.day() == 1) && (currentTime.hour() <= 11)))
136 g_lamexp_icon_data.reset(new QIcon(":/MainIcon4.png"));
138 else if((currentDate.month() == 12) && (currentDate.day() >= 24) && (currentDate.day() <= 26))
140 g_lamexp_icon_data.reset(new QIcon(":/MainIcon3.png"));
142 else if(lamexp_computus(currentDate))
144 g_lamexp_icon_data.reset(new QIcon(":/MainIcon2.png"));
146 else
148 g_lamexp_icon_data.reset(new QIcon(":/MainIcon1.png"));
152 return *g_lamexp_icon_data;
156 * Version number to human-readable string
158 const QString lamexp_version2string(const QString &pattern, unsigned int version, const QString &defaultText, const QString &tag)
160 if(version == UINT_MAX)
162 return defaultText;
165 QString result = pattern;
166 const int digits = result.count(QChar(L'?'), Qt::CaseInsensitive);
168 if(digits < 1)
170 return result;
173 int pos = 0, index = -1;
174 const QString versionStr = QString().sprintf("%0*u", digits, version);
175 Q_ASSERT(versionStr.length() == digits);
177 while((index = result.indexOf(QChar(L'?'), Qt::CaseInsensitive)) >= 0)
179 result[index] = versionStr[pos++];
182 if(!tag.isEmpty())
184 if((index = result.indexOf(QChar(L'#'), Qt::CaseInsensitive)) >= 0)
186 result.remove(index, 1).insert(index, tag);
190 return result;