Moved all the Sound-specific functions into MUtilities library.
[LameXP.git] / src / Global_Utils.cpp
blob79c0535fe32487587909041a33009a9ca361c2d9
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2014 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 struct
52 QIcon *appIcon;
53 QReadWriteLock lock;
55 g_lamexp_app_icon;
57 ///////////////////////////////////////////////////////////////////////////////
58 // GLOBAL FUNCTIONS
59 ///////////////////////////////////////////////////////////////////////////////
62 * Get a list of all available Qt Text Codecs
64 QStringList lamexp_available_codepages(bool noAliases)
66 QStringList codecList;
68 QList<QByteArray> availableCodecs = QTextCodec::availableCodecs();
69 while(!availableCodecs.isEmpty())
71 QByteArray current = availableCodecs.takeFirst();
72 if(!(current.startsWith("system") || current.startsWith("System")))
74 codecList << QString::fromLatin1(current.constData(), current.size());
75 if(noAliases)
77 if(QTextCodec *currentCodec = QTextCodec::codecForName(current.constData()))
80 QList<QByteArray> aliases = currentCodec->aliases();
81 while(!aliases.isEmpty()) availableCodecs.removeAll(aliases.takeFirst());
87 return codecList;
91 * Make a window blink (to draw user's attention)
93 void lamexp_blink_window(QWidget *poWindow, unsigned int count, unsigned int delay)
95 static QMutex blinkMutex;
97 const double maxOpac = 1.0;
98 const double minOpac = 0.3;
99 const double delOpac = 0.1;
101 if(!blinkMutex.tryLock())
103 qWarning("Blinking is already in progress, skipping!");
104 return;
109 const int steps = static_cast<int>(ceil(maxOpac - minOpac) / delOpac);
110 const int sleep = static_cast<int>(floor(static_cast<double>(delay) / static_cast<double>(steps)));
111 const double opacity = poWindow->windowOpacity();
113 for(unsigned int i = 0; i < count; i++)
115 for(double x = maxOpac; x >= minOpac; x -= delOpac)
117 poWindow->setWindowOpacity(x);
118 QApplication::processEvents();
119 MUtils::OS::sleep_ms(sleep);
122 for(double x = minOpac; x <= maxOpac; x += delOpac)
124 poWindow->setWindowOpacity(x);
125 QApplication::processEvents();
126 MUtils::OS::sleep_ms(sleep);
130 poWindow->setWindowOpacity(opacity);
131 QApplication::processEvents();
132 blinkMutex.unlock();
134 catch(...)
136 blinkMutex.unlock();
137 qWarning("Exception error while blinking!");
142 * Computus according to H. Lichtenberg
144 static bool lamexp_computus(const QDate &date)
146 int X = date.year();
147 int A = X % 19;
148 int K = X / 100;
149 int M = 15 + (3*K + 3) / 4 - (8*K + 13) / 25;
150 int D = (19*A + M) % 30;
151 int S = 2 - (3*K + 3) / 4;
152 int R = D / 29 + (D / 28 - D / 29) * (A / 11);
153 int OG = 21 + D - R;
154 int SZ = 7 - (X + X / 4 + S) % 7;
155 int OE = 7 - (OG - SZ) % 7;
156 int OS = (OG + OE);
158 if(OS > 31)
160 return (date.month() == 4) && (date.day() == (OS - 31));
162 else
164 return (date.month() == 3) && (date.day() == OS);
169 * Check for Thanksgiving
171 static bool lamexp_thanksgiving(const QDate &date)
173 int day = 0;
175 switch(QDate(date.year(), 11, 1).dayOfWeek())
177 case 1: day = 25; break;
178 case 2: day = 24; break;
179 case 3: day = 23; break;
180 case 4: day = 22; break;
181 case 5: day = 28; break;
182 case 6: day = 27; break;
183 case 7: day = 26; break;
186 return (date.month() == 11) && (date.day() == day);
190 * Initialize app icon
192 const QIcon &lamexp_app_icon(void)
194 QReadLocker readLock(&g_lamexp_app_icon.lock);
196 //Already initialized?
197 if(g_lamexp_app_icon.appIcon)
199 return *g_lamexp_app_icon.appIcon;
202 readLock.unlock();
203 QWriteLocker writeLock(&g_lamexp_app_icon.lock);
205 while(!g_lamexp_app_icon.appIcon)
207 QDate currentDate = QDate::currentDate();
208 QTime currentTime = QTime::currentTime();
210 if(lamexp_thanksgiving(currentDate))
212 g_lamexp_app_icon.appIcon = new QIcon(":/MainIcon6.png");
214 else if(((currentDate.month() == 12) && (currentDate.day() == 31) && (currentTime.hour() >= 20)) || ((currentDate.month() == 1) && (currentDate.day() == 1) && (currentTime.hour() <= 19)))
216 g_lamexp_app_icon.appIcon = new QIcon(":/MainIcon5.png");
218 else if(((currentDate.month() == 10) && (currentDate.day() == 31) && (currentTime.hour() >= 12)) || ((currentDate.month() == 11) && (currentDate.day() == 1) && (currentTime.hour() <= 11)))
220 g_lamexp_app_icon.appIcon = new QIcon(":/MainIcon4.png");
222 else if((currentDate.month() == 12) && (currentDate.day() >= 24) && (currentDate.day() <= 26))
224 g_lamexp_app_icon.appIcon = new QIcon(":/MainIcon3.png");
226 else if(lamexp_computus(currentDate))
228 g_lamexp_app_icon.appIcon = new QIcon(":/MainIcon2.png");
230 else
232 g_lamexp_app_icon.appIcon = new QIcon(":/MainIcon1.png");
236 return *g_lamexp_app_icon.appIcon;
239 ///////////////////////////////////////////////////////////////////////////////
240 // INITIALIZATION
241 ///////////////////////////////////////////////////////////////////////////////
243 extern "C" void _lamexp_global_init_utils(void)
245 MUTILS_ZERO_MEMORY(g_lamexp_app_icon);
248 ///////////////////////////////////////////////////////////////////////////////
249 // FINALIZATION
250 ///////////////////////////////////////////////////////////////////////////////
252 extern "C" void _lamexp_global_free_utils(void)
254 //Free memory
255 MUTILS_DELETE(g_lamexp_app_icon.appIcon);