44 //Determines the day of easter in year |
44 //Determines the day of easter in year |
45 //from http://aa.usno.navy.mil/faq/docs/easter.php,adapted to C/C++ |
45 //from http://aa.usno.navy.mil/faq/docs/easter.php,adapted to C/C++ |
46 QDate calculateEaster(long year) |
46 QDate calculateEaster(long year) |
47 { |
47 { |
48 int c, n, k, i, j, l, m, d; |
48 int c, n, k, i, j, l, m, d; |
49 |
49 |
50 c = year/100; |
50 c = year/100; |
51 n = year - 19*(year/19); |
51 n = year - 19*(year/19); |
52 k = (c - 17)/25; |
52 k = (c - 17)/25; |
53 i = c - c/4 - (c - k)/3 + 19*n + 15; |
53 i = c - c/4 - (c - k)/3 + 19*n + 15; |
54 i = i - 30*(i/30); |
54 i = i - 30*(i/30); |
63 } |
63 } |
64 |
64 |
65 //Checks season and assigns it to the variable season in "hwconsts.h" |
65 //Checks season and assigns it to the variable season in "hwconsts.h" |
66 void checkSeason() |
66 void checkSeason() |
67 { |
67 { |
68 QDate date = QDate::currentDate(); |
68 QDate date = QDate::currentDate(); |
69 |
69 |
70 //Christmas? |
70 //Christmas? |
71 if (date.month() == 12 && date.day() >= 24 |
71 if (date.month() == 12 && date.day() >= 24 |
72 && date.day() <= 26) |
72 && date.day() <= 26) |
73 season = SEASON_CHRISTMAS; |
73 season = SEASON_CHRISTMAS; |
74 //Hedgewars birthday? |
74 //Hedgewars birthday? |
75 else if (date.month() == 10 && date.day() == 31) |
75 else if (date.month() == 10 && date.day() == 31) |
76 { |
76 { |
77 season = SEASON_HWBDAY; |
77 season = SEASON_HWBDAY; |
78 years_since_foundation = date.year() - 2004; |
78 years_since_foundation = date.year() - 2004; |
79 } |
79 } |
80 //Easter? |
80 //Easter? |
81 else if (calculateEaster(date.year()) == date) |
81 else if (calculateEaster(date.year()) == date) |
82 season = SEASON_EASTER; |
82 season = SEASON_EASTER; |
83 else |
83 else |
84 season = SEASON_NONE; |
84 season = SEASON_NONE; |
85 } |
85 } |
86 |
86 |
87 bool checkForDir(const QString & dir) |
87 bool checkForDir(const QString & dir) |
88 { |
88 { |
89 QDir tmpdir; |
89 QDir tmpdir; |
90 if (!tmpdir.exists(dir)) |
90 if (!tmpdir.exists(dir)) |
91 if (!tmpdir.mkdir(dir)) |
91 if (!tmpdir.mkdir(dir)) |
92 { |
92 { |
93 QMessageBox::critical(0, |
93 QMessageBox::critical(0, |
94 QObject::tr("Error"), |
94 QObject::tr("Error"), |
95 QObject::tr("Cannot create directory %1").arg(dir), |
95 QObject::tr("Cannot create directory %1").arg(dir), |
96 QObject::tr("OK")); |
96 QObject::tr("OK")); |
97 return false; |
97 return false; |
98 } |
98 } |
99 return true; |
99 return true; |
100 } |
100 } |
101 |
101 |
102 int main(int argc, char *argv[]) { |
102 int main(int argc, char *argv[]) |
|
103 { |
103 HWApplication app(argc, argv); |
104 HWApplication app(argc, argv); |
104 app.setAttribute(Qt::AA_DontShowIconsInMenus,false); |
105 app.setAttribute(Qt::AA_DontShowIconsInMenus,false); |
105 |
106 |
106 QStringList arguments = app.arguments(); |
107 QStringList arguments = app.arguments(); |
107 QMap<QString, QString> parsedArgs; |
108 QMap<QString, QString> parsedArgs; |
108 { |
109 { |
109 QList<QString>::iterator i = arguments.begin(); |
110 QList<QString>::iterator i = arguments.begin(); |
110 while(i != arguments.end()) { |
111 while(i != arguments.end()) |
|
112 { |
111 QString arg = *i; |
113 QString arg = *i; |
112 |
114 |
113 QRegExp opt("--(\\S+)=(.+)"); |
115 QRegExp opt("--(\\S+)=(.+)"); |
114 if(opt.exactMatch(arg)) { |
116 if(opt.exactMatch(arg)) |
|
117 { |
115 parsedArgs[opt.cap(1)] = opt.cap(2); |
118 parsedArgs[opt.cap(1)] = opt.cap(2); |
116 i = arguments.erase(i); |
119 i = arguments.erase(i); |
117 } else { |
|
118 ++i; |
|
119 } |
120 } |
120 } |
121 else |
121 } |
122 { |
122 |
123 ++i; |
123 if(parsedArgs.contains("data-dir")) { |
124 } |
|
125 } |
|
126 } |
|
127 |
|
128 if(parsedArgs.contains("data-dir")) |
|
129 { |
124 QFileInfo f(parsedArgs["data-dir"]); |
130 QFileInfo f(parsedArgs["data-dir"]); |
125 if(!f.exists()) { |
131 if(!f.exists()) |
|
132 { |
126 qWarning() << "WARNING: Cannot open DATA_PATH=" << f.absoluteFilePath(); |
133 qWarning() << "WARNING: Cannot open DATA_PATH=" << f.absoluteFilePath(); |
127 } |
134 } |
128 *cDataDir = f.absoluteFilePath(); |
135 *cDataDir = f.absoluteFilePath(); |
129 custom_data = true; |
136 custom_data = true; |
130 } |
137 } |
131 |
138 |
132 if(parsedArgs.contains("config-dir")) { |
139 if(parsedArgs.contains("config-dir")) |
|
140 { |
133 QFileInfo f(parsedArgs["config-dir"]); |
141 QFileInfo f(parsedArgs["config-dir"]); |
134 *cConfigDir = f.absoluteFilePath(); |
142 *cConfigDir = f.absoluteFilePath(); |
135 custom_config = true; |
143 custom_config = true; |
136 } |
144 } |
137 |
145 |
187 checkForDir(cfgdir->absolutePath() + "/Logs"); |
195 checkForDir(cfgdir->absolutePath() + "/Logs"); |
188 } |
196 } |
189 |
197 |
190 datadir->cd(bindir->absolutePath()); |
198 datadir->cd(bindir->absolutePath()); |
191 datadir->cd(*cDataDir); |
199 datadir->cd(*cDataDir); |
192 if(!datadir->cd("hedgewars/Data")) { |
200 if(!datadir->cd("hedgewars/Data")) |
|
201 { |
193 QMessageBox::critical(0, QMessageBox::tr("Error"), |
202 QMessageBox::critical(0, QMessageBox::tr("Error"), |
194 QMessageBox::tr("Failed to open data directory:\n%1\n" |
203 QMessageBox::tr("Failed to open data directory:\n%1\n" |
195 "Please check your installation"). |
204 "Please check your installation"). |
196 arg(datadir->absolutePath()+"/hedgewars/Data")); |
205 arg(datadir->absolutePath()+"/hedgewars/Data")); |
197 return 1; |
206 return 1; |
198 } |
207 } |
199 |
208 |
200 HWDataManager & dataMgr = HWDataManager::instance(); |
209 HWDataManager & dataMgr = HWDataManager::instance(); |
201 |
210 |
202 { |
211 { |
203 QStringList themes; |
212 QStringList themes; |
204 |
213 |
205 themes.append(dataMgr.entryList( |
214 themes.append(dataMgr.entryList( |
206 "Themes", |
215 "Themes", |
207 QDir::AllDirs | QDir::NoDotAndDotDot) |
216 QDir::AllDirs | QDir::NoDotAndDotDot) |
208 ); |
217 ); |
209 |
218 |
210 QList<QPair<QIcon, QIcon> > icons; |
219 QList<QPair<QIcon, QIcon> > icons; |
211 |
220 |
212 themes.sort(); |
221 themes.sort(); |
213 for(int i = themes.size() - 1; i >= 0; --i) |
222 for(int i = themes.size() - 1; i >= 0; --i) |
214 { |
223 { |
215 QString file = dataMgr.findFileForRead( |
224 QString file = dataMgr.findFileForRead( |
216 QString("Themes/%1/icon.png").arg(themes.at(i)) |
225 QString("Themes/%1/icon.png").arg(themes.at(i)) |
217 ); |
226 ); |
218 |
227 |
219 if(QFile::exists(file)) |
228 if(QFile::exists(file)) |
220 { // load icon |
229 { |
|
230 // load icon |
221 QPair<QIcon, QIcon> ic; |
231 QPair<QIcon, QIcon> ic; |
222 ic.first = QIcon(file); |
232 ic.first = QIcon(file); |
223 |
233 |
224 // load preview icon |
234 // load preview icon |
225 ic.second = QIcon( |
235 ic.second = QIcon( |
226 dataMgr.findFileForRead( |
236 dataMgr.findFileForRead( |
227 QString("Themes/%1/icon@2x.png").arg(themes.at(i)) |
237 QString("Themes/%1/icon@2x.png").arg(themes.at(i)) |
228 ) |
238 ) |
229 ); |
239 ); |
230 |
240 |
231 icons.prepend(ic); |
241 icons.prepend(ic); |
232 } |
242 } |
233 else |
243 else |
234 { |
244 { |
244 themesModel->setData(themesModel->index(i), icons[i].second, Qt::UserRole); |
254 themesModel->setData(themesModel->index(i), icons[i].second, Qt::UserRole); |
245 } |
255 } |
246 } |
256 } |
247 |
257 |
248 mapList = new QStringList(dataMgr.entryList( |
258 mapList = new QStringList(dataMgr.entryList( |
249 QString("Maps"), |
259 QString("Maps"), |
250 QDir::Dirs | QDir::NoDotAndDotDot |
260 QDir::Dirs | QDir::NoDotAndDotDot |
251 ) |
261 ) |
252 ); |
262 ); |
253 |
263 |
254 scriptList = new QStringList(dataMgr.entryList( |
264 scriptList = new QStringList(dataMgr.entryList( |
255 QString("Scripts/Multiplayer"), |
265 QString("Scripts/Multiplayer"), |
256 QDir::Files, |
266 QDir::Files, |
257 QStringList("*.lua") |
267 QStringList("*.lua") |
258 ) |
268 ) |
259 ); |
269 ); |
260 |
270 |
261 QTranslator Translator; |
271 QTranslator Translator; |
262 { |
272 { |
263 QSettings settings(cfgdir->absolutePath() + "/hedgewars.ini", QSettings::IniFormat); |
273 QSettings settings(cfgdir->absolutePath() + "/hedgewars.ini", QSettings::IniFormat); |
264 QString cc = settings.value("misc/locale", QString()).toString(); |
274 QString cc = settings.value("misc/locale", QString()).toString(); |
288 CocoaInitializer initializer; |
298 CocoaInitializer initializer; |
289 #endif |
299 #endif |
290 |
300 |
291 QString style = ""; |
301 QString style = ""; |
292 QString fname; |
302 QString fname; |
293 |
303 |
294 checkSeason(); |
304 checkSeason(); |
295 //For each season, there is an extra stylesheet |
305 //For each season, there is an extra stylesheet |
296 //Todo: change background for easter and birthday |
306 //Todo: change background for easter and birthday |
297 //(simply replace res/BackgroundBirthday.png and res/BackgroundEaster.png |
307 //(simply replace res/BackgroundBirthday.png and res/BackgroundEaster.png |
298 //with an appropriate background |
308 //with an appropriate background |
299 switch (season) |
309 switch (season) |
300 { |
310 { |
301 case SEASON_CHRISTMAS : fname = "christmas.css"; |
311 case SEASON_CHRISTMAS : |
302 break; |
312 fname = "christmas.css"; |
303 case SEASON_EASTER : fname = "easter.css"; |
313 break; |
304 break; |
314 case SEASON_EASTER : |
305 case SEASON_HWBDAY : fname = "birthday.css"; |
315 fname = "easter.css"; |
306 break; |
316 break; |
307 default : fname = "qt.css"; |
317 case SEASON_HWBDAY : |
|
318 fname = "birthday.css"; |
|
319 break; |
|
320 default : |
|
321 fname = "qt.css"; |
308 } |
322 } |
309 |
323 |
310 // load external stylesheet if there is any |
324 // load external stylesheet if there is any |
311 QFile extFile(dataMgr.findFileForRead("css/" + fname)); |
325 QFile extFile(dataMgr.findFileForRead("css/" + fname)); |
312 |
326 |