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>
45 ///////////////////////////////////////////////////////////////////////////////
47 ///////////////////////////////////////////////////////////////////////////////
56 ///////////////////////////////////////////////////////////////////////////////
58 ///////////////////////////////////////////////////////////////////////////////
61 * Remove forbidden characters from a filename
63 const QString lamexp_clean_filename(const QString &str)
66 QRegExp rx("\"(.+)\"");
69 newStr.replace("\\", "-");
70 newStr.replace(" / ", ", ");
71 newStr.replace("/", ",");
72 newStr.replace(":", "-");
73 newStr.replace("*", "x");
74 newStr.replace("?", "");
75 newStr.replace("<", "[");
76 newStr.replace(">", "]");
77 newStr.replace("|", "!");
78 newStr.replace(rx, "`\\1ยด");
79 newStr.replace("\"", "'");
81 return newStr.simplified();
85 * Remove forbidden characters from a file path
87 const QString lamexp_clean_filepath(const QString &str)
89 QStringList parts = QString(str).replace("\\", "/").split("/");
91 for(int i = 0; i < parts.count(); i++)
93 parts[i] = lamexp_clean_filename(parts[i]);
96 return parts.join("/");
100 * Get a list of all available Qt Text Codecs
102 QStringList lamexp_available_codepages(bool noAliases)
104 QStringList codecList;
106 QList<QByteArray> availableCodecs = QTextCodec::availableCodecs();
107 while(!availableCodecs.isEmpty())
109 QByteArray current = availableCodecs.takeFirst();
110 if(!(current.startsWith("system") || current.startsWith("System")))
112 codecList << QString::fromLatin1(current.constData(), current.size());
115 if(QTextCodec *currentCodec = QTextCodec::codecForName(current.constData()))
118 QList<QByteArray> aliases = currentCodec->aliases();
119 while(!aliases.isEmpty()) availableCodecs.removeAll(aliases.takeFirst());
129 * Make a window blink (to draw user's attention)
131 void lamexp_blink_window(QWidget *poWindow, unsigned int count, unsigned int delay)
133 static QMutex blinkMutex;
135 const double maxOpac = 1.0;
136 const double minOpac = 0.3;
137 const double delOpac = 0.1;
139 if(!blinkMutex.tryLock())
141 qWarning("Blinking is already in progress, skipping!");
147 const int steps = static_cast<int>(ceil(maxOpac - minOpac) / delOpac);
148 const int sleep = static_cast<int>(floor(static_cast<double>(delay) / static_cast<double>(steps)));
149 const double opacity = poWindow->windowOpacity();
151 for(unsigned int i = 0; i < count; i++)
153 for(double x = maxOpac; x >= minOpac; x -= delOpac)
155 poWindow->setWindowOpacity(x);
156 QApplication::processEvents();
160 for(double x = minOpac; x <= maxOpac; x += delOpac)
162 poWindow->setWindowOpacity(x);
163 QApplication::processEvents();
168 poWindow->setWindowOpacity(opacity);
169 QApplication::processEvents();
175 qWarning("Exception error while blinking!");
180 * Computus according to H. Lichtenberg
182 static bool lamexp_computus(const QDate &date)
187 int M = 15 + (3*K + 3) / 4 - (8*K + 13) / 25;
188 int D = (19*A + M) % 30;
189 int S = 2 - (3*K + 3) / 4;
190 int R = D / 29 + (D / 28 - D / 29) * (A / 11);
192 int SZ = 7 - (X + X / 4 + S) % 7;
193 int OE = 7 - (OG - SZ) % 7;
198 return (date.month() == 4) && (date.day() == (OS - 31));
202 return (date.month() == 3) && (date.day() == OS);
207 * Check for Thanksgiving
209 static bool lamexp_thanksgiving(const QDate &date)
213 switch(QDate(date.year(), 11, 1).dayOfWeek())
215 case 1: day = 25; break;
216 case 2: day = 24; break;
217 case 3: day = 23; break;
218 case 4: day = 22; break;
219 case 5: day = 28; break;
220 case 6: day = 27; break;
221 case 7: day = 26; break;
224 return (date.month() == 11) && (date.day() == day);
228 * Initialize app icon
230 const QIcon &lamexp_app_icon(void)
232 QReadLocker readLock(&g_lamexp_app_icon.lock);
234 //Already initialized?
235 if(g_lamexp_app_icon.appIcon)
237 return *g_lamexp_app_icon.appIcon;
241 QWriteLocker writeLock(&g_lamexp_app_icon.lock);
243 while(!g_lamexp_app_icon.appIcon)
245 QDate currentDate = QDate::currentDate();
246 QTime currentTime = QTime::currentTime();
248 if(lamexp_thanksgiving(currentDate))
250 g_lamexp_app_icon.appIcon = new QIcon(":/MainIcon6.png");
252 else if(((currentDate.month() == 12) && (currentDate.day() == 31) && (currentTime.hour() >= 20)) || ((currentDate.month() == 1) && (currentDate.day() == 1) && (currentTime.hour() <= 19)))
254 g_lamexp_app_icon.appIcon = new QIcon(":/MainIcon5.png");
256 else if(((currentDate.month() == 10) && (currentDate.day() == 31) && (currentTime.hour() >= 12)) || ((currentDate.month() == 11) && (currentDate.day() == 1) && (currentTime.hour() <= 11)))
258 g_lamexp_app_icon.appIcon = new QIcon(":/MainIcon4.png");
260 else if((currentDate.month() == 12) && (currentDate.day() >= 24) && (currentDate.day() <= 26))
262 g_lamexp_app_icon.appIcon = new QIcon(":/MainIcon3.png");
264 else if(lamexp_computus(currentDate))
266 g_lamexp_app_icon.appIcon = new QIcon(":/MainIcon2.png");
270 g_lamexp_app_icon.appIcon = new QIcon(":/MainIcon1.png");
274 return *g_lamexp_app_icon.appIcon;
277 ///////////////////////////////////////////////////////////////////////////////
279 ///////////////////////////////////////////////////////////////////////////////
281 extern "C" void _lamexp_global_init_utils(void)
283 LAMEXP_ZERO_MEMORY(g_lamexp_app_icon);
286 ///////////////////////////////////////////////////////////////////////////////
288 ///////////////////////////////////////////////////////////////////////////////
290 extern "C" void _lamexp_global_free_utils(void)
293 MUTILS_DELETE(g_lamexp_app_icon.appIcon);