1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
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 ///////////////////////////////////////////////////////////////////////////////
26 #include <QApplication>
32 #include <QProcessEnvironment>
33 #include <QReadWriteLock>
39 #include <MUtils/Global.h>
40 #include <MUtils/OSSupport.h>
46 ///////////////////////////////////////////////////////////////////////////////
48 ///////////////////////////////////////////////////////////////////////////////
57 ///////////////////////////////////////////////////////////////////////////////
59 ///////////////////////////////////////////////////////////////////////////////
62 * Remove forbidden characters from a filename
64 const QString lamexp_clean_filename(const QString &str)
67 QRegExp rx("\"(.+)\"");
70 newStr.replace("\\", "-");
71 newStr.replace(" / ", ", ");
72 newStr.replace("/", ",");
73 newStr.replace(":", "-");
74 newStr.replace("*", "x");
75 newStr.replace("?", "");
76 newStr.replace("<", "[");
77 newStr.replace(">", "]");
78 newStr.replace("|", "!");
79 newStr.replace(rx, "`\\1ยด");
80 newStr.replace("\"", "'");
82 return newStr.simplified();
86 * Remove forbidden characters from a file path
88 const QString lamexp_clean_filepath(const QString &str)
90 QStringList parts = QString(str).replace("\\", "/").split("/");
92 for(int i = 0; i < parts.count(); i++)
94 parts[i] = lamexp_clean_filename(parts[i]);
97 return parts.join("/");
101 * Get a list of all available Qt Text Codecs
103 QStringList lamexp_available_codepages(bool noAliases)
105 QStringList codecList;
107 QList<QByteArray> availableCodecs = QTextCodec::availableCodecs();
108 while(!availableCodecs.isEmpty())
110 QByteArray current = availableCodecs.takeFirst();
111 if(!(current.startsWith("system") || current.startsWith("System")))
113 codecList << QString::fromLatin1(current.constData(), current.size());
116 if(QTextCodec *currentCodec = QTextCodec::codecForName(current.constData()))
119 QList<QByteArray> aliases = currentCodec->aliases();
120 while(!aliases.isEmpty()) availableCodecs.removeAll(aliases.takeFirst());
130 * Make a window blink (to draw user's attention)
132 void lamexp_blink_window(QWidget *poWindow, unsigned int count, unsigned int delay)
134 static QMutex blinkMutex;
136 const double maxOpac = 1.0;
137 const double minOpac = 0.3;
138 const double delOpac = 0.1;
140 if(!blinkMutex.tryLock())
142 qWarning("Blinking is already in progress, skipping!");
148 const int steps = static_cast<int>(ceil(maxOpac - minOpac) / delOpac);
149 const int sleep = static_cast<int>(floor(static_cast<double>(delay) / static_cast<double>(steps)));
150 const double opacity = poWindow->windowOpacity();
152 for(unsigned int i = 0; i < count; i++)
154 for(double x = maxOpac; x >= minOpac; x -= delOpac)
156 poWindow->setWindowOpacity(x);
157 QApplication::processEvents();
158 MUtils::OS::sleep_ms(sleep);
161 for(double x = minOpac; x <= maxOpac; x += delOpac)
163 poWindow->setWindowOpacity(x);
164 QApplication::processEvents();
165 MUtils::OS::sleep_ms(sleep);
169 poWindow->setWindowOpacity(opacity);
170 QApplication::processEvents();
176 qWarning("Exception error while blinking!");
181 * Computus according to H. Lichtenberg
183 static bool lamexp_computus(const QDate &date)
188 int M = 15 + (3*K + 3) / 4 - (8*K + 13) / 25;
189 int D = (19*A + M) % 30;
190 int S = 2 - (3*K + 3) / 4;
191 int R = D / 29 + (D / 28 - D / 29) * (A / 11);
193 int SZ = 7 - (X + X / 4 + S) % 7;
194 int OE = 7 - (OG - SZ) % 7;
199 return (date.month() == 4) && (date.day() == (OS - 31));
203 return (date.month() == 3) && (date.day() == OS);
208 * Check for Thanksgiving
210 static bool lamexp_thanksgiving(const QDate &date)
214 switch(QDate(date.year(), 11, 1).dayOfWeek())
216 case 1: day = 25; break;
217 case 2: day = 24; break;
218 case 3: day = 23; break;
219 case 4: day = 22; break;
220 case 5: day = 28; break;
221 case 6: day = 27; break;
222 case 7: day = 26; break;
225 return (date.month() == 11) && (date.day() == day);
229 * Initialize app icon
231 const QIcon &lamexp_app_icon(void)
233 QReadLocker readLock(&g_lamexp_app_icon.lock);
235 //Already initialized?
236 if(g_lamexp_app_icon.appIcon)
238 return *g_lamexp_app_icon.appIcon;
242 QWriteLocker writeLock(&g_lamexp_app_icon.lock);
244 while(!g_lamexp_app_icon.appIcon)
246 QDate currentDate = QDate::currentDate();
247 QTime currentTime = QTime::currentTime();
249 if(lamexp_thanksgiving(currentDate))
251 g_lamexp_app_icon.appIcon = new QIcon(":/MainIcon6.png");
253 else if(((currentDate.month() == 12) && (currentDate.day() == 31) && (currentTime.hour() >= 20)) || ((currentDate.month() == 1) && (currentDate.day() == 1) && (currentTime.hour() <= 19)))
255 g_lamexp_app_icon.appIcon = new QIcon(":/MainIcon5.png");
257 else if(((currentDate.month() == 10) && (currentDate.day() == 31) && (currentTime.hour() >= 12)) || ((currentDate.month() == 11) && (currentDate.day() == 1) && (currentTime.hour() <= 11)))
259 g_lamexp_app_icon.appIcon = new QIcon(":/MainIcon4.png");
261 else if((currentDate.month() == 12) && (currentDate.day() >= 24) && (currentDate.day() <= 26))
263 g_lamexp_app_icon.appIcon = new QIcon(":/MainIcon3.png");
265 else if(lamexp_computus(currentDate))
267 g_lamexp_app_icon.appIcon = new QIcon(":/MainIcon2.png");
271 g_lamexp_app_icon.appIcon = new QIcon(":/MainIcon1.png");
275 return *g_lamexp_app_icon.appIcon;
278 ///////////////////////////////////////////////////////////////////////////////
280 ///////////////////////////////////////////////////////////////////////////////
282 extern "C" void _lamexp_global_init_utils(void)
284 MUTILS_ZERO_MEMORY(g_lamexp_app_icon);
287 ///////////////////////////////////////////////////////////////////////////////
289 ///////////////////////////////////////////////////////////////////////////////
291 extern "C" void _lamexp_global_free_utils(void)
294 MUTILS_DELETE(g_lamexp_app_icon.appIcon);