1 /*	Copyright 2008 Filipe Azevedo <pasnox@gmail.com>
2 
3 	This file is part of Yabause.
4 
5 	Yabause 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.
9 
10 	Yabause is distributed in the hope that it will be useful,
11 	but WITHOUT ANY WARRANTY; without even the implied warranty of
12 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 	GNU General Public License for more details.
14 
15 	You should have received a copy of the GNU General Public License
16 	along with Yabause; if not, write to the Free Software
17 	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
18 */
19 #include "UIBackupRam.h"
20 #include "../CommonDialogs.h"
21 #include "../QtYabause.h"
22 
23 u32 currentbupdevice = 0;
24 deviceinfo_struct* devices = NULL;
25 int numbupdevices = 0;
26 saveinfo_struct* saves = NULL;
27 int numsaves = 0;
28 
UIBackupRam(QWidget * p)29 UIBackupRam::UIBackupRam( QWidget* p )
30 	: QDialog( p )
31 {
32 	//setup dialog
33 	setupUi( this );
34 	if ( p && !p->isFullScreen() )
35 		setWindowFlags( Qt::Sheet );
36 
37 	// get available devices
38 	if ( ( devices = BupGetDeviceList( &numbupdevices ) ) == NULL )
39 		return;
40 
41 	// add to combobox
42 	for ( int i = 0; i < numbupdevices; i++ )
43 		cbDeviceList->addItem( devices[i].name, devices[i].id );
44 
45 	// get save list for current devices
46 	refreshSaveList();
47 
48 	// retranslate widgets
49 	QtYabause::retranslateWidget( this );
50 }
51 
refreshSaveList()52 void UIBackupRam::refreshSaveList()
53 {
54 	// blocks
55 	u32 fs = 0, ms = 0;
56 	u32 id = cbDeviceList->itemData( cbDeviceList->currentIndex() ).toInt();
57 
58 	// clear listwidget
59 	lwSaveList->clear();
60 
61 	// get save list
62 	saves = BupGetSaveList(MSH2, id, &numsaves);
63 
64 	// add item to listwidget
65 	for ( int i = 0; i < numsaves; i++ )
66 		lwSaveList->addItem( saves[i].filename );
67 
68 	// set infos about blocks
69 	BupGetStats(MSH2, id, &fs, &ms );
70 	lBlocks->setText( QtYabause::translate( "%1/%2 blocks free" ).arg( fs ).arg( ms ) );
71 
72 	// enable/disable button delete according to available item
73 	pbDelete->setEnabled( lwSaveList->count() );
74 
75 	// select first item in the item list
76 	if ( lwSaveList->count() )
77 		lwSaveList->setCurrentRow( 0 );
78 	on_lwSaveList_itemSelectionChanged();
79 }
80 
on_cbDeviceList_currentIndexChanged(int)81 void UIBackupRam::on_cbDeviceList_currentIndexChanged( int )
82 { refreshSaveList(); }
83 
on_lwSaveList_itemSelectionChanged()84 void UIBackupRam::on_lwSaveList_itemSelectionChanged()
85 {
86 	// get current save id
87 	int id = lwSaveList->currentRow();
88 
89 	// update gui
90 	if ( id != -1 )
91 	{
92 		leFileName->setText( saves[id].filename );
93 		leComment->setText( saves[id].comment );
94 		switch ( saves[id].language )
95 		{
96 			case 0:
97 				leLanguage->setText( QtYabause::translate( "Japanese" ) );
98 				break;
99 			case 1:
100 				leLanguage->setText( QtYabause::translate( "English" ) );
101 				break;
102 			case 2:
103 				leLanguage->setText( QtYabause::translate( "French" ) );
104 				break;
105 			case 3:
106 				leLanguage->setText( QtYabause::translate( "German" ) );
107 				break;
108 			case 4:
109 				leLanguage->setText( QtYabause::translate( "Spanish" ) );
110 				break;
111 			case 5:
112 				leLanguage->setText( QtYabause::translate( "Italian" ) );
113 				break;
114 			default:
115 				leLanguage->setText( QtYabause::translate( "Unknown (%1)" ).arg( saves[id].language ) );
116 				break;
117 		}
118 		leDataSize->setText( QString::number( saves[id].datasize ) );
119 		leBlockSize->setText( QString::number( saves[id].blocksize ) );
120 	}
121 	else
122 	{
123 		// clear gui
124 		leFileName->clear();
125 		leComment->clear();
126 		leLanguage->clear();
127 		leDataSize->clear();
128 		leBlockSize->clear();
129 	}
130 }
131 
on_pbDelete_clicked()132 void UIBackupRam::on_pbDelete_clicked()
133 {
134 	if ( QListWidgetItem* it = lwSaveList->selectedItems().value( 0 ) )
135 	{
136 		u32 id = cbDeviceList->itemData( cbDeviceList->currentIndex() ).toInt();
137 		if ( CommonDialogs::question( QtYabause::translate( "Are you sure you want to delete '%1' ?" ).arg( it->text() ) ) )
138 		{
139 			BupDeleteSave(MSH2, id, it->text().toLatin1().constData() );
140 			refreshSaveList();
141 		}
142 	}
143 }
144 
on_pbFormat_clicked()145 void UIBackupRam::on_pbFormat_clicked()
146 {
147 	u32 id = cbDeviceList->itemData( cbDeviceList->currentIndex() ).toInt();
148 	if ( CommonDialogs::question( QtYabause::translate( "Are you sure you want to format '%1' ?" ).arg( devices[id].name ) ) )
149 	{
150 		BupFormat( id );
151 		refreshSaveList();
152 	}
153 }
154