OSDN Git Service

Improved Russian & Spanish translations for the installer
[lamexp/LameXP.git] / src / Global_Utils.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2015 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 QScopedPointer<QIcon> g_lamexp_icon_data;
51 static QReadWriteLock        g_lamexp_icon_lock;
52
53 ///////////////////////////////////////////////////////////////////////////////
54 // GLOBAL FUNCTIONS
55 ///////////////////////////////////////////////////////////////////////////////
56
57 /*
58  * Computus according to H. Lichtenberg
59  */
60 static bool lamexp_computus(const QDate &date)
61 {
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);
73
74         if(OS > 31)
75         {
76                 return (date.month() == 4) && (date.day() == (OS - 31));
77         }
78         else
79         {
80                 return (date.month() == 3) && (date.day() == OS);
81         }
82 }
83
84 /*
85  * Check for Thanksgiving
86  */
87 static bool lamexp_thanksgiving(const QDate &date)
88 {
89         int day = 0;
90
91         switch(QDate(date.year(), 11, 1).dayOfWeek())
92         {
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;
100         }
101
102         return (date.month() == 11) && (date.day() == day);
103 }
104
105 /*
106  * Initialize app icon
107  */
108 const QIcon &lamexp_app_icon(void)
109 {
110         QReadLocker readLock(&g_lamexp_icon_lock);
111
112         //Already initialized?
113         if(!g_lamexp_icon_data.isNull())
114         {
115                 return *g_lamexp_icon_data;
116         }
117
118         readLock.unlock();
119         QWriteLocker writeLock(&g_lamexp_icon_lock);
120
121         while(g_lamexp_icon_data.isNull())
122         {
123                 QDate currentDate = QDate::currentDate();
124                 QTime currentTime = QTime::currentTime();
125         
126                 if(lamexp_thanksgiving(currentDate))
127                 {
128                         g_lamexp_icon_data.reset(new QIcon(":/MainIcon6.png"));
129                 }
130                 else if(((currentDate.month() == 12) && (currentDate.day() == 31) && (currentTime.hour() >= 20)) || ((currentDate.month() == 1) && (currentDate.day() == 1)  && (currentTime.hour() <= 19)))
131                 {
132                         g_lamexp_icon_data.reset(new QIcon(":/MainIcon5.png"));
133                 }
134                 else if(((currentDate.month() == 10) && (currentDate.day() == 31) && (currentTime.hour() >= 12)) || ((currentDate.month() == 11) && (currentDate.day() == 1)  && (currentTime.hour() <= 11)))
135                 {
136                         g_lamexp_icon_data.reset(new QIcon(":/MainIcon4.png"));
137                 }
138                 else if((currentDate.month() == 12) && (currentDate.day() >= 24) && (currentDate.day() <= 26))
139                 {
140                         g_lamexp_icon_data.reset(new QIcon(":/MainIcon3.png"));
141                 }
142                 else if(lamexp_computus(currentDate))
143                 {
144                         g_lamexp_icon_data.reset(new QIcon(":/MainIcon2.png"));
145                 }
146                 else
147                 {
148                         g_lamexp_icon_data.reset(new QIcon(":/MainIcon1.png"));
149                 }
150         }
151
152         return *g_lamexp_icon_data;
153 }
154
155 /*
156  * Version number to human-readable string
157  */
158 const QString lamexp_version2string(const QString &pattern, unsigned int version, const QString &defaultText, const QString &tag)
159 {
160         if(version == UINT_MAX)
161         {
162                 return defaultText;
163         }
164         
165         QString result = pattern;
166         const int digits = result.count(QChar(L'?'), Qt::CaseInsensitive);
167         
168         if(digits < 1)
169         {
170                 return result;
171         }
172         
173         int pos = 0, index = -1;
174         const QString versionStr = QString().sprintf("%0*u", digits, version);
175         Q_ASSERT(versionStr.length() == digits);
176         
177         while((index = result.indexOf(QChar(L'?'), Qt::CaseInsensitive)) >= 0)
178         {
179                 result[index] = versionStr[pos++];
180         }
181
182         if(!tag.isEmpty())
183         {
184                 if((index = result.indexOf(QChar(L'#'), Qt::CaseInsensitive)) >= 0)
185                 {
186                         result.remove(index, 1).insert(index, tag);
187                 }
188         }
189
190         return result;
191 }