~sithlord48/hyne/master

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
/****************************************************************************
 ** Hyne Final Fantasy VIII Save Editor
 ** Copyright (C) 2009-2013 Arzel Jérôme <myst6re@gmail.com>
 **
 ** This program is free software: you can redistribute it and/or modify
 ** it under the terms of the GNU General Public License as published by
 ** the Free Software Foundation, either version 3 of the License, or
 ** (at your option) any later version.
 **
 ** This program is distributed in the hope that it will be useful,
 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 ** GNU General Public License for more details.
 **
 ** You should have received a copy of the GNU General Public License
 ** along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************/

#include "Window.h"
#include "Parameters.h"
#include "SelectSavesDialog.h"
#include "HeaderDialog.h"
#include "MetadataDialog.h"

Window::Window() :
	QWidget(), taskbarButton(0), saves(0), saveList(0), editor(0)
{
	setTitle();
	setMinimumSize(768, 502);
	resize(768, 502);
	setAcceptDrops(true);

	menuBar = new QMenuBar(0);
	QMenu *menu;
	QAction *action, *actionSlot1=0, *actionSlot2=0;

	/* MENU 'FICHIER' */
	
	menu = menuBar->addMenu(tr("&Fichier"));
#ifdef Q_OS_MAC
	QMenu *fileMenu = menu;
#endif

	bool isInstalled = !Config::ff8Installations().isEmpty();
	
	QAction *actionNew = menu->addAction(tr("&Nouveau..."), this, SLOT(newFile()), QKeySequence::New);
	QAction *actionOpen = menu->addAction(QApplication::style()->standardIcon(QStyle::SP_DialogOpenButton), tr("&Ouvrir..."), this, SLOT(open()), QKeySequence::Open);
	actionReload = menu->addAction(QApplication::style()->standardIcon(QStyle::SP_BrowserReload), tr("&Recharger depuis le disque"), this, SLOT(reload()), QKeySequence::Refresh);
	actionReload->setEnabled(false);
	actionSave = menu->addAction(QApplication::style()->standardIcon(QStyle::SP_DialogSaveButton), tr("&Enregistrer"), this, SLOT(save()), QKeySequence::Save);
	actionSave->setEnabled(false);
	actionSaveAs = menu->addAction(tr("E&xporter..."), this, SLOT(exportAs()), QKeySequence::SaveAs);
	actionSaveAs->setEnabled(false);
	menuRecent = menu->addMenu(tr("O&uverts récemment"));
	fillMenuRecent();
	connect(menuRecent, SIGNAL(triggered(QAction*)), SLOT(openRecentFile(QAction*)));
	menu->addSeparator();
	actionProperties = menu->addAction(tr("&Propriétés..."), this, SLOT(properties()));
	actionProperties->setEnabled(false);
	action = menu->addAction(tr("S&igner des sauv. pour le Cloud..."), this, SLOT(updateMetadata()));
	addAction(action);
	if(isInstalled) {
		action = menu->addAction(QIcon(":/images/ff8.png"), tr("&Lancer Final Fantasy VIII"), this, SLOT(runFF8()), Qt::Key_F8);
		action->setShortcutContext(Qt::ApplicationShortcut);
		addAction(action);
	}
	menu->addAction(tr("Nou&velle fenêtre"), this, SLOT(newWindow()));
	action = menu->addAction(tr("Ple&in écran"), this, SLOT(fullScreen()), Qt::Key_F11);
	action->setShortcutContext(Qt::ApplicationShortcut);
	addAction(action);
	menu->addSeparator();
	actionClose = menu->addAction(QApplication::style()->standardIcon(QStyle::SP_DialogCloseButton), tr("&Fermer"), this, SLOT(closeFile()), QKeySequence::Close);
	actionClose->setEnabled(false);
	menu->addAction(tr("&Quitter"), this, SLOT(close()), QKeySequence::Quit)->setMenuRole(QAction::QuitRole);
	
	/* MENU 'SLOT' */
	
	if(isInstalled) {
		actionSlot1 = menuBar->addAction(tr("Fente &1"), this, SLOT(slot1()));
		actionSlot2 = menuBar->addAction(tr("Fente &2"), this, SLOT(slot2()));
	}

	/* MENU 'PARAMETRES' */
	
	menu = menuBar->addMenu(tr("&Paramètres"));
	
	actionMode = menu->addAction(tr("&Mode Avancé"), this, SLOT(mode(bool)));
	actionMode->setCheckable(true);
	actionMode->setChecked(Config::mode());
	
	menuFrame = menu->addMenu(tr("&Images par seconde"));
		action = menuFrame->addAction(tr("&Auto"));
		action->setCheckable(true);
		action->setChecked(Config::freq_auto());
		action->setData(0);
		action = menuFrame->addAction(tr("&NTSC/PC (60 images/s)"));
		action->setCheckable(true);
		action->setChecked(Config::freq()==60);
		action->setData(60);
		action = menuFrame->addAction(tr("&PAL (50 images/s)"));
		action->setCheckable(true);
		action->setChecked(Config::freq()==50);
		action->setData(50);
	connect(menuFrame, SIGNAL(triggered(QAction*)), SLOT(changeFrame(QAction*)));
	
	actionFont = menu->addAction(tr("&Police haute résolution"), this, SLOT(font(bool)));
	actionFont->setCheckable(true);
	actionFont->setChecked(!Config::value(Config::Font).isEmpty());
	
	menuLang = menu->addMenu(tr("&Langue"));
	foreach(const QString &str, availableLanguages()) {
		action = menuLang->addAction(str.left(str.lastIndexOf("|")));
		QString lang = str.mid(str.lastIndexOf("|") + 1);
		action->setData(lang);
		action->setCheckable(true);
		action->setChecked(Config::value(Config::Lang) == lang);
	}
	connect(menuLang, SIGNAL(triggered(QAction*)), SLOT(changeLanguage(QAction*)));

	if(Config::ff8Installations().size() > 1) {
		menuVersion = menu->addMenu(tr("Version PC"));
		foreach(const FF8Installation &installation, Config::ff8Installations()) {
			action = menuVersion->addAction(installation.typeString());
			action->setCheckable(true);
			action->setChecked(Config::ff8Installation() == installation);
		}
		connect(menuVersion, SIGNAL(triggered(QAction*)), SLOT(changeFF8Version(QAction*)));
	}

	/* MENU '?' */
	
#ifndef Q_OS_MAC
	menuBar->addAction(tr("&?"), this, SLOT(about()))->setMenuRole(QAction::AboutRole);
#else
	fileMenu->addAction(tr("&?"), this, SLOT(about()))->setMenuRole(QAction::AboutRole);
#endif

	startWidget = new StartWidget(this);
	startWidget->addAction(actionNew);
	startWidget->addAction(actionOpen);
	if(isInstalled) {
		startWidget->addAction(actionSlot1);
		startWidget->addAction(actionSlot2);
	}
	
	stackedLayout = new QStackedLayout(this);
	stackedLayout->setMenuBar(menuBar);
	stackedLayout->addWidget(startWidget);

	restoreGeometry(Config::valueVar(Config::Geometry).toByteArray());
}

Window::~Window()
{
	Config::setValue(Config::Geometry, saveGeometry());
	Config::saveRecentFiles();
	Config::sync();// flush data
}

void Window::showEvent(QShowEvent *event)
{
	event->accept();
#if defined(Q_OS_WIN) && (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
	if(!taskbarButton && QSysInfo::windowsVersion() >= QSysInfo::WV_WINDOWS7) {
		taskbarButton = new QTaskbarButton(windowHandle());
	}
#endif
}

void Window::closeEvent(QCloseEvent *event)
{
	if(stackedLayout->currentWidget() == editor) {
		saveView();
		event->ignore();
	}
	else if(!closeFile(true))
		event->ignore();
	else
		event->accept();
}

void Window::dragEnterEvent(QDragEnterEvent *event)
{
	if(event->mimeData()->hasUrls() &&
			(stackedLayout->currentWidget() == startWidget
			 || (saveList && stackedLayout->currentWidget() == saveList)))
		event->acceptProposedAction();
}

void Window::dropEvent(QDropEvent *event)
{
	if(stackedLayout->currentWidget() == editor)	return;

	const QMimeData *mimeData = event->mimeData();

	if(mimeData->hasUrls())
	{
		QList<QUrl> urlList = mimeData->urls();
		if(!urlList.isEmpty()) {
			QString path = QDir::cleanPath(urlList.first().path());
			if(!path.startsWith(QDir::rootPath()))
				path = path.mid(1);
			if(path.startsWith(QDir::rootPath())) {
				openFile(path);
			}
		}
	}

	event->acceptProposedAction();
}

void Window::setTitle(const int currentSaveEdited)
{
	setWindowTitle(PROG_NAME %
				   (!saves ? QString() : " - [*]" % saves->path()) %
				   (currentSaveEdited >= 0 ? tr(" - save %1").arg(currentSaveEdited+1, 2, 10, QChar('0')) : QString())
				   );
}

void Window::setModified(bool modified)
{
	setWindowModified(modified);
	actionSave->setEnabled(modified);
}

void Window::newFile()
{
	QDialog dialog(this, Qt::Dialog | Qt::WindowCloseButtonHint);
	dialog.setWindowTitle(tr("Nouveau"));

	QLineEdit name(tr("Sans nom"), &dialog);
	name.selectAll();
	QRadioButton oneSave(tr("1 sauvegarde"), &dialog);
	QRadioButton multiSaves(tr("15 sauvegardes"), &dialog);
	multiSaves.setChecked(true);
	QDialogButtonBox buttonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, &dialog);

	QGridLayout layout(&dialog);
	layout.addWidget(&name, 0, 0, 1, 2);
	layout.addWidget(&oneSave, 1, 0);
	layout.addWidget(&multiSaves, 1, 1);
	layout.addWidget(&buttonBox, 2, 0, 1, 2);

	connect(&buttonBox, SIGNAL(accepted()), &dialog, SLOT(accept()));
	connect(&buttonBox, SIGNAL(rejected()), &dialog, SLOT(reject()));

	if(dialog.exec() == QDialog::Accepted) {
		if(!closeFile())	return;

		saves = new SavecardData(oneSave.isChecked() ? 1 : 15);
		if(!name.text().isEmpty()) {
			saves->setName(name.text());
		}

		if(!saves->isOpen())
		{
			QMessageBox::warning(this, tr("Erreur"), saves->errorString());
			closeFile();
		}
		else
		{
			saveView();
			saveList->setSavecard(saves);
			setIsOpen(true);
			setModified(saves->isModified());
		}
	}
}

void Window::open(OpenType slot)
{
	QString path;
	FF8Installation installation;

	if(slot == Slot1 || slot == Slot2)
	{
		installation = Config::ff8Installation();
		if(!installation.isValid())	return;

		path = installation.savePath((int)slot);
	}
	else
	{
		QString loadPath = Config::value(Config::LoadPath);
		if(!loadPath.isEmpty())
			path = loadPath;
		else if(!path.isEmpty())
			path.append("/Save");
		path = QFileDialog::getOpenFileName(this, tr("Ouvrir"), path,
											tr("Fichiers compatibles (*.mcr *.ddf *.gme *.mc *.mcd *.mci *.ps *.psm *.vm1 *.psv save?? *.ff8 *.mem *.vgs *.vmp *.000 *.001 *.002 *.003 *.004);;"
											   "FF8 PS memorycard (*.mcr *.ddf *.mc *.mcd *.mci *.ps *.psm *.vm1);;"
											   "FF8 PC save (save?? *.ff8);;"
											   "FF8 vgs memorycard (*.mem *.vgs);;"
											   "FF8 gme memorycard (*.gme);;"
											   "FF8 PSN memorycard (*.vmp);;"
											   "FF8 PS3 memorycard/pSX save state (*.psv);;"
											   "ePSXe save state (*.000 *.001 *.002 *.003 *.004);;"
											   "Tous les fichiers (*)"));
		if(path.isNull())		return;

		int index = path.lastIndexOf('/');
		Config::setValue(Config::LoadPath, index == -1 ? path : path.left(index));
	}

	openFile(path, slot, installation);
}

bool Window::closeFile(bool quit)
{
	if(saves && saves->isModified()) {
		QMessageBox::StandardButton b = QMessageBox::question(this, tr("Enregistrer ?"),
															  tr("Voulez-vous enregistrer '%1' avant de fermer ?")
															  .arg(saves->type() == SavecardData::PcSlot
																   ? tr("fente")
																   : saves->name()),
															  QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
		switch(b) {
		case QMessageBox::Yes:
			save();
			break;
		case QMessageBox::Cancel:
			return false;
		default:
			break;
		}
	}

	if(quit)	return true;

	setIsOpen(false);

	return true;
}

void Window::openFile(const QString &path, OpenType openType, const FF8Installation &installation)
{
	if(!closeFile())	return;

	saves = new SavecardData(path, quint8(openType), installation);
	if(saves->type() == SavecardData::Unknown) {
		QMessageBox::StandardButton rep = QMessageBox::question(this, tr("Erreur"), tr("Fichier de type inconnu.\nVoulez-vous l'analyser pour obtenir le bon format ?"), QMessageBox::Yes | QMessageBox::No);
		if(rep != QMessageBox::Yes) {
			return;
		}

		saves->getFormatFromRaw();
	}

	if(!saves->isOpen())
	{
		QMessageBox::warning(this, tr("Erreur"), saves->errorString());
		setIsOpen(false);
	}
	else
	{
		saveView();
		saveList->setSavecard(saves);

		if(saves->type() == SavecardData::Psv || saves->type() == SavecardData::Vmp) {
			QMessageBox::information(this, tr("Sauvegarde hasardeuse"),
									 tr("Le format %1 est protégé, "
										"l'enregistrement sera partiel "
										"et risque de ne pas fonctionner.")
									 .arg(saves->extension()));
		}
		setIsOpen(true);
		setModified(saves->isModified());

		if(openType == File) {
			Config::addRecentFile(saves->path());
			fillMenuRecent();
		}
	}
}

void Window::setIsOpen(bool open)
{
	if(open) {
		stackedLayout->setCurrentWidget(saveList);
		actionSaveAs->setEnabled(true);
		actionProperties->setEnabled(true);
		actionClose->setEnabled(true);
		actionReload->setEnabled(true);

		setTitle();
	} else {
		stackedLayout->setCurrentWidget(startWidget);
		if(editor)	editor->hide();

		if(saves)
		{
			if(saveList)	saveList->setSavecard(0);
			delete saves;
			saves = 0;
		}

		actionSaveAs->setEnabled(false);
		actionProperties->setEnabled(false);
		actionClose->setEnabled(false);
		actionReload->setEnabled(false);

		setTitle();
		setModified(false);
	}
}

void Window::openRecentFile(QAction *action)
{
	openFile( Config::recentFile(action->data().toUInt()) );
}

void Window::reload()
{
	if(saves) {
		OpenType openType;
		if(saves->type() == SavecardData::PcSlot) {
			openType = saves->slotNumber() == 1 ? Slot1 : Slot2;
		} else {
			openType = File;
		}

		openFile(QString(saves->path()), openType, FF8Installation(saves->ff8Installation()));
	}
}

bool Window::exportAs()
{
	QString types, path, selectedFilter,
			ps = tr("PS memorycard (*.mcr *.ddf *.mc *.mcd *.mci *.ps *.psm *.vm1)"),
			vgs = tr("VGS memorycard (*.vgs *.mem)"),
			gme = tr("GME memorycard (*.gme)"),
			vmp = tr("PSN memorycard (*.vmp)"),
			pc = tr("FF8 PC save (*.ff8 *)"),
			psv = tr("PSN save (*.psv)");
	SavecardData::Type type = saves->type(), newType;

	types = pc+";;"+ps+";;"+vgs+";;"+gme+";;"+vmp+";;"+psv;

	if(type == SavecardData::PcSlot
			|| type == SavecardData::Pc)	selectedFilter = pc;
	else if(type == SavecardData::Psv)		selectedFilter = psv;
	else if(type == SavecardData::Vgs)		selectedFilter = vgs;
	else if(type == SavecardData::Gme)		selectedFilter = gme;
	else if(type == SavecardData::Vmp)		selectedFilter = vmp;
	else									selectedFilter = ps;
	
	path = Config::value(Config::SavePath).isEmpty() ? saves->dirname() : Config::value(Config::SavePath)+"/";
	if(saves->type() == SavecardData::Undefined) {
		path = path+saves->name()+".mcr";
	} else {
		path = path+saves->name();
	}
	path = QFileDialog::getSaveFileName(this, tr("Exporter"), path, types, &selectedFilter);
	if(path.isNull())		return false;

	if(selectedFilter == ps)			newType = SavecardData::Ps;
	else if(selectedFilter == vgs)		newType = SavecardData::Vgs;
	else if(selectedFilter == gme)		newType = SavecardData::Gme;
	else if(selectedFilter == vmp)		newType = SavecardData::Vmp;
	else if(selectedFilter == pc)		newType = SavecardData::Pc;
	else if(selectedFilter == psv)		newType = SavecardData::Psv;
	else {
		qWarning() << "Bad selected filter!" << selectedFilter;
		return false;
	}

	if(newType == SavecardData::Vmp || newType == SavecardData::Psv) {
		QMessageBox::StandardButton reponse = QMessageBox::information(this, tr("Sauvegarde hasardeuse"),
											   tr("Les formats VMP et PSV sont protégés, "
												  "l'enregistrement sera partiel et risque "
												  "de ne pas fonctionner.\n"
												  "Continuer quand même ?"),
											   QMessageBox::Yes | QMessageBox::No);
		if(reponse != QMessageBox::Yes)  return exportAs();
	}
	
	int index = path.lastIndexOf('/');
	Config::setValue(Config::SavePath, index == -1 ? path : path.left(index));

	return exportAs(newType, path);
}

bool Window::exportAs(SavecardData::Type newType, const QString &path)
{
	SavecardData::Type type = saves->type();

	if(newType == SavecardData::Gme) {
		bool abort;
		QByteArray desc = descGme(saves->description(), &abort);
		if(abort)	return false;
		saves->setDescription(desc);
	}

	bool ok = true;

	if(type == newType) {
		switch(type) {
		case SavecardData::Pc:
			ok = saves->save2PC(0, path);
			break;
		case SavecardData::PcSlot:
			ok = saves->saveDirectory();
			break;
		case SavecardData::Psv:
		case SavecardData::Ps:
		case SavecardData::Vgs:
		case SavecardData::Gme:
		case SavecardData::Vmp:
			ok = saves->save(path, newType);
			break;
		case SavecardData::Unknown:
		case SavecardData::Undefined:
			qWarning() << "not possible case";
			return false;
		}
	} else {
		if(newType != SavecardData::Pc && newType != SavecardData::Psv) {

			if(type == SavecardData::PcSlot
					|| type == SavecardData::Undefined
					|| type == SavecardData::Pc || type == SavecardData::Psv) {
				QList<int> selected_files;

				if(saves->saveCount() == 1) {
					selected_files.append(0);
				} else {
					selected_files = selectSavesDialog(true);
					if(selected_files.isEmpty())	return false;
				}

				QByteArray MCHeader;
				if(type != SavecardData::Psv) {
					SaveData tempSave;
					HeaderDialog dialog(&tempSave, this, HeaderDialog::CreateView);
					if(dialog.exec() != QDialog::Accepted) return false;
					MCHeader = tempSave.saveMCHeader();
				} else {
					MCHeader = saves->getSaves().first()->saveMCHeader();
				}

				ok = saves->save2PS(selected_files, path, newType, MCHeader);
			} else {
				ok = saves->save(path, newType);
			}
		} else { // saveOne (PC & PSV)
			int id;

			if(saves->saveCount() == 1) {
				id = 0;
			} else {
				// Need selection by user
				QList<int> selected_files = selectSavesDialog(false, newType == SavecardData::Pc);
				if(selected_files.isEmpty())	return false;
				id = selected_files.first();
			}

			if(newType == SavecardData::Pc) {
				ok = saves->save2PC(id, path);
			} else {
				QByteArray MCHeader;
				if(!saves->getSaves().first()->hasMCHeader()) {
					SaveData tempSave;
					HeaderDialog dialog(&tempSave, this, HeaderDialog::CreateView);
					if(dialog.exec() != QDialog::Accepted) return false;
					MCHeader = tempSave.saveMCHeader();
				} else {
					MCHeader = saves->getSaves().first()->saveMCHeader();
				}
				ok = saves->save2PSV(id, path, MCHeader);
			}
		}
	}

	if(!ok) {
		QMessageBox::critical(this, tr("Erreur"), saves->errorString());
	} else if(!saves->errorString().isEmpty()) {
		QMessageBox::warning(this, tr("Attention"), saves->errorString());
	}

	setTitle();

	return ok;
}

QByteArray Window::descGme(const QString &desc, bool *abort)
{
	QDialog dialog(this, Qt::Dialog | Qt::WindowCloseButtonHint);
	dialog.setWindowTitle(tr("Commentaire"));

	QTextEdit *textEdit = new QTextEdit(&dialog);
	textEdit->setPlainText(desc);

	QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
													   Qt::Horizontal, &dialog);
	QVBoxLayout *layout = new QVBoxLayout(&dialog);
	layout->addWidget(textEdit);
	layout->addWidget(buttonBox);

	connect(buttonBox, SIGNAL(accepted()), &dialog, SLOT(accept()));
	connect(buttonBox, SIGNAL(rejected()), &dialog, SLOT(reject()));

	if(dialog.exec() != QDialog::Accepted) {
		*abort = true;
		return QByteArray();
	}
	*abort = false;

	return textEdit->toPlainText().toLatin1().leftJustified(3840, '\x00', true);
}

void Window::properties()
{
	if(!saves)		return;

	int saveCount = 0, firstSaveID = -1, saveID = 0;

	foreach(const SaveData *saveData, saves->getSaves()) {
		if(!saveData->isDelete()) {
			saveCount++;
			if(firstSaveID == -1) {
				firstSaveID = saveID;
			}
		}
		if(saveCount > 1)	break;
		++saveID;
	}

	if(saveCount == 0)	return;
	if(saveCount == 1) {
		saveList->view()->properties(firstSaveID);
		return;
	}

	SelectSavesDialog *dialog = new SelectSavesDialog(saves->getSaves(), false, false, this);

	if(dialog->exec() == QDialog::Accepted) {
		QList<int> selected_files = dialog->selectedSaves();
		saveList->view()->properties(selected_files.first());
	}
}

QList<int> Window::selectSavesDialog(bool multiSelection, bool onlyFF8)
{
	SelectSavesDialog *dialog = new SelectSavesDialog(saves->getSaves(), multiSelection, onlyFF8, this);

	if(dialog->exec() == QDialog::Accepted) {
		return dialog->selectedSaves();
	}
	return QList<int>();
}

void Window::fillMenuRecent()
{
	menuRecent->clear();
	
	for(int i=0 ; i<Config::recentFilesSize() ; ++i)
		menuRecent->addAction(tr("%1 : %2").arg(i+1,2,10).arg(Config::recentFile(i)))->setData(i);

	menuRecent->setEnabled(Config::hasRecentFiles());
}

void Window::editView(SaveData *saveData)
{
	menuBar->hide();
	menuBar->setEnabled(false);
	if(!editor) {
		editor = new Editor(this);
		connect(editor, SIGNAL(accepted()), SLOT(saveView()));
		connect(editor, SIGNAL(rejected()), SLOT(saveView()));
		stackedLayout->addWidget(editor);
	}
	editor->show();
	editor->load(saveData, saves->type() == SavecardData::Pc || saves->type() == SavecardData::PcSlot);
	stackedLayout->setCurrentWidget(editor);
	setTitle(saveData->id());
	saves->setIsTheLastEdited(saveData->id());
	if(taskbarButton)	taskbarButton->setOverlayIcon(saveData->saveIcon().icon());
	else				setWindowIcon(QIcon(saveData->saveIcon().icon()));
}

void Window::saveView()
{
	if(!saveList) {
		saveList = new SavecardWidget(this);
		connect(saveList->view(), SIGNAL(changed()), SLOT(setModified()));
		connect(saveList->view(), SIGNAL(released(SaveData*)), SLOT(editView(SaveData*)));
		stackedLayout->addWidget(saveList);
	} else {
		stackedLayout->setCurrentWidget(saveList);
		if(editor)	editor->hide();
		menuBar->show();
		menuBar->setEnabled(true);
		if(taskbarButton)	taskbarButton->setOverlayIcon(QPixmap());
		else				setWindowIcon(qApp->windowIcon());
		setTitle();
		setModified(saves->isModified());
		saveList->view()->update();
	}
}

void Window::save()
{
	bool saved = true, hasPath = saves->hasPath();
	SavecardData::Type type = saves->type();

	if(!hasPath || type == SavecardData::Vmp || type == SavecardData::Psv) {
		saved = exportAs();
		if(!hasPath && saved) {
			setTitle();
		}
	} else {
		saved = exportAs(type, saves->path());
	}

	if(saved) {
		setModified(false);
		saves->setModified(false);
		saveList->view()->update();
	}
}

void Window::mode(bool mode)
{
	Config::setValue(Config::Mode, mode);
	if(saves && editor) {
		editor->updateMode(mode);
	}
}

void Window::changeFrame(QAction *action)
{
	if(action->data().toInt()==0)
		Config::setValue(Config::FreqAuto, true);
	else {
		Config::setValue(Config::Freq, action->data());
		Config::setValue(Config::FreqAuto, false);
	}

	foreach(QAction *act, menuFrame->actions())
		act->setChecked(false);
	action->setChecked(true);

	if(saves) {
		if(editor)	editor->updateTime();
		saveList->view()->update();
	}
}

void Window::font(bool font)
{
	Config::setValue(Config::Font, font ? "hr" : "");
	FF8Text::reloadFont();
	if(saves) {
		saveList->view()->update();
	} else {
		startWidget->update();
	}
}

QStringList Window::availableLanguages()
{
	QDir dir(Config::translationDir());
	QStringList languages, stringList = dir.entryList(QStringList("hyne_*.qm"), QDir::Files, QDir::Name);

	languages.append("Français|fr");

	QTranslator translator;
	foreach(QString str, stringList) {
		translator.load(str, Config::translationDir());
		QString lang = translator.translate("Window", QString::fromUtf8("Français").toLatin1().data(), "Your translation language");

		str = str.mid(5);
		languages.append(lang + "|" + str.left(str.size()-3));
	}

	return languages;
}

QString Window::chooseLangDialog()
{
	QStringList langs = availableLanguages();
	if(langs.size() <= 1) {
		// Doesn't ask for language if there is just one available
		return langs.isEmpty() ? QString() : langs.first();
	}

	const QString chooseStr("Choose your language");
	QDialog *dialog = new QDialog(0, Qt::Dialog | Qt::WindowCloseButtonHint);
	dialog->setWindowTitle(chooseStr);
	QLabel *label = new QLabel(chooseStr + ":", dialog);
	QComboBox *comboBox = new QComboBox(dialog);
	foreach(const QString &str, langs)
		comboBox->addItem(str.left(str.lastIndexOf("|")), str.mid(str.lastIndexOf("|")+1));

	QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok, Qt::Horizontal, dialog);

	QGridLayout *layout = new QGridLayout(dialog);
	layout->addWidget(label, 0, 0);
	layout->addWidget(comboBox, 0, 1);
	layout->addWidget(buttonBox, 1, 0, 1, 2);

	connect(buttonBox, SIGNAL(accepted()), dialog, SLOT(accept()));

	if(dialog->exec() == QDialog::Accepted) {
		return comboBox->itemData(comboBox->currentIndex()).toString();
	}

	return QString();
}

void Window::changeLanguage(QAction *action)
{
	if(Config::value(Config::Lang) != action->data()) {
		Config::setValue(Config::Lang, action->data());
		foreach(QAction *act, menuLang->actions())
			act->setChecked(false);
		action->setChecked(true);

		restartNow();
	} else {
		action->setChecked(true);
	}
}

void Window::changeFF8Version(QAction *action)
{
	Config::setSelectedFF8Installation(FF8Installation::Type(menuVersion->actions().indexOf(action)));
	foreach(QAction *act, menuVersion->actions())
		act->setChecked(false);
	action->setChecked(true);
}

void Window::newWindow()
{
	(new Window())->show();
}

void Window::restartNow()
{
	QString str_title, str_text;
	if(Config::translator->load("hyne_" + Config::value(Config::Lang), qApp->applicationDirPath())) {
		str_title = Config::translator->translate("Window", QString::fromUtf8("Paramètres modifiés").toLatin1().data());
		str_text = Config::translator->translate("Window", QString::fromUtf8("Relancez le programme pour que les paramètres prennent effet.").toLatin1().data());
    }
    else {
        str_title = "Paramètres modifiés";
        str_text = "Relancez le programme pour que les paramètres prennent effet.";
	}
	Data::reload();
    QMessageBox::information(this, str_title, str_text);
}

/*void Window::changeEvent(QEvent *e)
{
	if(e->type() == QEvent::LanguageChange) {

	} else {
		QWidget::changeEvent(e);
	}
}*/

void Window::runFF8()
{
	QString appPath = Config::ff8Installation().appPath(), exeFilename = appPath % "/" % Config::ff8Installation().exeFilename();
	if(!QProcess::startDetached(QString("\"%1\"").arg(exeFilename), QStringList(), appPath)) {
		QMessageBox::warning(this, tr("Erreur"), tr("Final Fantasy VIII n'a pas pu être lancé.\n%1").arg(exeFilename));
	}
}

void Window::updateMetadata()
{
	MetadataDialog dialog(Config::ff8Installations(), this);
	dialog.exec();
}

void Window::fullScreen()
{
	if(isFullScreen())	showNormal();
	else				showFullScreen();
}

void Window::about()
{
	QDialog about(this, Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint);
	about.setFixedSize(224, 272);
	about.setWindowTitle(tr("À propos"));
	
	QFont font;
	font.setPointSize(12);
	
	QLabel image(&about);
	image.setPixmap(QPixmap(":/images/about.png"));
	image.move(about.width()-20-image.sizeHint().height(), 80);
	
	QLabel desc1(QString("%1 %2").arg(PROG_NAME, PROG_VERSION), &about);
	desc1.setFont(font);
	desc1.setFixedWidth(about.width());
	desc1.setAlignment(Qt::AlignHCenter);

	font.setPointSize(8);

	QLabel desc2(tr("Par myst6re<br/><a href=\"https://sourceforge.net/projects/hyne/\">https://sourceforge.net/projects/hyne/</a><br/><br/>75% modifiable<br/><br/>Merci à :<br/> - Qhimm<br/> - Cyberman<br/> - sithlord48<br/> - Aladore384<br/> - suloku<br/><br/>Traducteurs :<br/> - Anglais : myst6re, Vgr<br/> - Japonais : Asa, Sharleen"), &about);
	desc2.setTextInteractionFlags(Qt::LinksAccessibleByMouse | Qt::LinksAccessibleByKeyboard);
	desc2.setTextFormat(Qt::RichText);
	desc2.setOpenExternalLinks(true);
	desc2.move(9, 24);
	desc2.setFont(font);

	QLabel desc3(QString("Qt %1").arg(QT_VERSION_STR), &about);
	QPalette pal = desc3.palette();
	pal.setColor(QPalette::WindowText, QColor(0xAA,0xAA,0xAA));
	desc3.setPalette(pal);
	desc3.move(about.width()-8-desc3.sizeHint().width(), about.height()-8-desc3.sizeHint().height());
	desc3.setFont(font);
	
	QPushButton button(tr("&Fermer"), &about);
	button.move(8, about.height()-8-button.sizeHint().height());
	connect(&button, SIGNAL(released()), &about, SLOT(close()));
	
	// Set default icon if needed
	QStyle *style = about.style();
	if(style->styleHint(QStyle::SH_DialogButtonBox_ButtonsHaveIcons))
		button.setIcon(style->standardIcon(QStyle::SP_DialogCloseButton, 0, &about));
	if(style != QApplication::style()) // Propagate style
		button.setStyle(style);

	about.exec();
}