OSDN Git Service

74cb658e82e76d8cd37f22b821ed7beb12849495
[lamexp/LameXP.git] / src / Global_Utils.cpp
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.
10 //
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.
15 //
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.
19 //
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
22
23 #include "Global.h"
24
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>
37
38 //MUtils
39 #include <MUtils/Global.h>
40 #include <MUtils/OSSupport.h>
41
42 //CRT includes
43 #include <time.h>
44 #include <process.h>
45
46 ///////////////////////////////////////////////////////////////////////////////
47 // GLOBAL VARS
48 ///////////////////////////////////////////////////////////////////////////////
49
50 static struct
51 {
52         QIcon *appIcon;
53         QReadWriteLock lock;
54 }
55 g_lamexp_app_icon;
56
57 ///////////////////////////////////////////////////////////////////////////////
58 // GLOBAL FUNCTIONS
59 ///////////////////////////////////////////////////////////////////////////////
60
61 /*
62  * Remove forbidden characters from a filename
63  */
64 const QString lamexp_clean_filename(const QString &str)
65 {
66         QString newStr(str);
67         QRegExp rx("\"(.+)\"");
68         rx.setMinimal(true);
69
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("\"", "'");
81         
82         return newStr.simplified();
83 }
84
85 /*
86  * Remove forbidden characters from a file path
87  */
88 const QString lamexp_clean_filepath(const QString &str)
89 {
90         QStringList parts = QString(str).replace("\\", "/").split("/");
91
92         for(int i = 0; i < parts.count(); i++)
93         {
94                 parts[i] = lamexp_clean_filename(parts[i]);
95         }
96
97         return parts.join("/");
98 }
99
100 /*
101  * Get a list of all available Qt Text Codecs
102  */
103 QStringList lamexp_available_codepages(bool noAliases)
104 {
105         QStringList codecList;
106         
107         QList<QByteArray> availableCodecs = QTextCodec::availableCodecs();
108         while(!availableCodecs.isEmpty())
109         {
110                 QByteArray current = availableCodecs.takeFirst();
111                 if(!(current.startsWith("system") || current.startsWith("System")))
112                 {
113                         codecList << QString::fromLatin1(current.constData(), current.size());
114                         if(noAliases)
115                         {
116                                 if(QTextCodec *currentCodec = QTextCodec::codecForName(current.constData()))
117                                 {
118                                         
119                                         QList<QByteArray> aliases = currentCodec->aliases();
120                                         while(!aliases.isEmpty()) availableCodecs.removeAll(aliases.takeFirst());
121                                 }
122                         }
123                 }
124         }
125
126         return codecList;
127 }
128
129 /*
130  * Make a window blink (to draw user's attention)
131  */
132 void lamexp_blink_window(QWidget *poWindow, unsigned int count, unsigned int delay)
133 {
134         static QMutex blinkMutex;
135
136         const double maxOpac = 1.0;
137         const double minOpac = 0.3;
138         const double delOpac = 0.1;
139
140         if(!blinkMutex.tryLock())
141         {
142                 qWarning("Blinking is already in progress, skipping!");
143                 return;
144         }
145         
146         try
147         {
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();
151         
152                 for(unsigned int i = 0; i < count; i++)
153                 {
154                         for(double x = maxOpac; x >= minOpac; x -= delOpac)
155                         {
156                                 poWindow->setWindowOpacity(x);
157                                 QApplication::processEvents();
158                                 MUtils::OS::sleep_ms(sleep);
159                         }
160
161                         for(double x = minOpac; x <= maxOpac; x += delOpac)
162                         {
163                                 poWindow->setWindowOpacity(x);
164                                 QApplication::processEvents();
165                                 MUtils::OS::sleep_ms(sleep);
166                         }
167                 }
168
169                 poWindow->setWindowOpacity(opacity);
170                 QApplication::processEvents();
171                 blinkMutex.unlock();
172         }
173         catch(...)
174         {
175                 blinkMutex.unlock();
176                 qWarning("Exception error while blinking!");
177         }
178 }
179
180 /*
181  * Computus according to H. Lichtenberg
182  */
183 static bool lamexp_computus(const QDate &date)
184 {
185         int X = date.year();
186         int A = X % 19;
187         int K = X / 100;
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);
192         int OG = 21 + D - R;
193         int SZ = 7 - (X + X / 4 + S) % 7;
194         int OE = 7 - (OG - SZ) % 7;
195         int OS = (OG + OE);
196
197         if(OS > 31)
198         {
199                 return (date.month() == 4) && (date.day() == (OS - 31));
200         }
201         else
202         {
203                 return (date.month() == 3) && (date.day() == OS);
204         }
205 }
206
207 /*
208  * Check for Thanksgiving
209  */
210 static bool lamexp_thanksgiving(const QDate &date)
211 {
212         int day = 0;
213
214         switch(QDate(date.year(), 11, 1).dayOfWeek())
215         {
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;
223         }
224
225         return (date.month() == 11) && (date.day() == day);
226 }
227
228 /*
229  * Initialize app icon
230  */
231 const QIcon &lamexp_app_icon(void)
232 {
233         QReadLocker readLock(&g_lamexp_app_icon.lock);
234
235         //Already initialized?
236         if(g_lamexp_app_icon.appIcon)
237         {
238                 return *g_lamexp_app_icon.appIcon;
239         }
240
241         readLock.unlock();
242         QWriteLocker writeLock(&g_lamexp_app_icon.lock);
243
244         while(!g_lamexp_app_icon.appIcon)
245         {
246                 QDate currentDate = QDate::currentDate();
247                 QTime currentTime = QTime::currentTime();
248         
249                 if(lamexp_thanksgiving(currentDate))
250                 {
251                         g_lamexp_app_icon.appIcon = new QIcon(":/MainIcon6.png");
252                 }
253                 else if(((currentDate.month() == 12) && (currentDate.day() == 31) && (currentTime.hour() >= 20)) || ((currentDate.month() == 1) && (currentDate.day() == 1)  && (currentTime.hour() <= 19)))
254                 {
255                         g_lamexp_app_icon.appIcon = new QIcon(":/MainIcon5.png");
256                 }
257                 else if(((currentDate.month() == 10) && (currentDate.day() == 31) && (currentTime.hour() >= 12)) || ((currentDate.month() == 11) && (currentDate.day() == 1)  && (currentTime.hour() <= 11)))
258                 {
259                         g_lamexp_app_icon.appIcon = new QIcon(":/MainIcon4.png");
260                 }
261                 else if((currentDate.month() == 12) && (currentDate.day() >= 24) && (currentDate.day() <= 26))
262                 {
263                         g_lamexp_app_icon.appIcon = new QIcon(":/MainIcon3.png");
264                 }
265                 else if(lamexp_computus(currentDate))
266                 {
267                         g_lamexp_app_icon.appIcon = new QIcon(":/MainIcon2.png");
268                 }
269                 else
270                 {
271                         g_lamexp_app_icon.appIcon = new QIcon(":/MainIcon1.png");
272                 }
273         }
274
275         return *g_lamexp_app_icon.appIcon;
276 }
277
278 ///////////////////////////////////////////////////////////////////////////////
279 // INITIALIZATION
280 ///////////////////////////////////////////////////////////////////////////////
281
282 extern "C" void _lamexp_global_init_utils(void)
283 {
284         MUTILS_ZERO_MEMORY(g_lamexp_app_icon);
285 }
286
287 ///////////////////////////////////////////////////////////////////////////////
288 // FINALIZATION
289 ///////////////////////////////////////////////////////////////////////////////
290
291 extern "C" void _lamexp_global_free_utils(void)
292 {
293         //Free memory
294         MUTILS_DELETE(g_lamexp_app_icon.appIcon);
295 }