PageRenderTime 48ms CodeModel.GetById 14ms app.highlight 30ms RepoModel.GetById 1ms app.codeStats 0ms

/clipboard.cpp

http://ewitool.googlecode.com/
C++ | 311 lines | 202 code | 76 blank | 33 comment | 29 complexity | fdcb156b4a84092288c4bf3acf0f384a MD5 | raw file
  1/***************************************************************************
  2 *   Copyright (C) 2008 by Steve Merrony   *
  3 *   steve@brahma   *
  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 3 of the License, or     *
  8 *   (at your option) any later version.                                   *
  9 *                                                                         *
 10 *   This program 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 this program; if not, write to the                         *
 17 *   Free Software Foundation, Inc.,                                       *
 18 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 19 ***************************************************************************/
 20
 21#include <QFile>
 22#include <QInputDialog>
 23#include <QMessageBox>
 24
 25#include "clipboard.h"
 26
 27#include "epxsubmit_dialog.h"
 28#include "viewhex_dialog.h"
 29
 30Clipboard::Clipboard( QString libraryLoc, patchExchange *main_epx, QWidget *parent)
 31	: QWidget( parent )
 32{
 33	libraryLocation = libraryLoc;
 34	
 35	setupUi( this );
 36	
 37	epx = main_epx;
 38	
 39	// connect the buttons
 40	connect( clearClipboard_pushButton, SIGNAL(clicked()), this, SLOT( clearAll() ));
 41	connect( deleteClipboard_pushButton, SIGNAL(clicked()), this, SLOT( deleteItem() ));
 42	connect( renameClipboard_pushButton, SIGNAL(clicked()), this, SLOT( renameItem() ));
 43	connect( viewHexClipboard_pushButton, SIGNAL(clicked()), this, SLOT( viewHex() ));
 44	connect( exchangeClipboard_pushButton, SIGNAL(clicked()), this, SLOT( exportToEPX() ));
 45	connect( exportClipboard_pushButton, SIGNAL(clicked()), this, SLOT( exportToFile() ));
 46	
 47	// connect other interesting events
 48	connect( clipboard_listWidget, SIGNAL( itemSelectionChanged( ) ), this, SLOT( selectionChanged( ) ) ) ;
 49	
 50	load();
 51	selectionChanged();
 52	if (clipboard_listWidget->count() == 0) clearClipboard_pushButton->setEnabled( false );
 53}
 54
 55
 56Clipboard::~Clipboard()
 57{
 58}
 59
 60void Clipboard::clearAll( ) {
 61	
 62	if (QMessageBox::question (this, "EWItool - Clear Clipboard",
 63		tr ("Do you realy want to clear the Clipboard completely?" ),
 64			QMessageBox::No | QMessageBox::Yes
 65							  ) ==  QMessageBox::No) return;
 66	
 67	clipboard_listWidget->clear();
 68	clipboard_list.clear();
 69	
 70	QString fileName = libraryLocation + "/CLIPBOARD.CLP";
 71	QFile file(fileName);
 72	file.remove();
 73	clearClipboard_pushButton->setEnabled( false );
 74}
 75
 76void Clipboard::load(  ) {
 77	
 78	patch_t tmp_patch;
 79	QString fileName = libraryLocation + CLIPBOARD_FILE;
 80	
 81	QFile file(fileName);
 82	if (!file.open(QFile::ReadOnly)) {
 83		return;
 84	}
 85
 86	clipboard_listWidget->clear();
 87	clipboard_list.clear();
 88	
 89	QDataStream inp(&file);
 90	while (!inp.atEnd()) {
 91		inp.readRawData( (char *) &tmp_patch, EWI_PATCH_LENGTH );
 92		clipboard_listWidget->addItem( ewi4000sPatch::toQString( tmp_patch.parameters.name ) );
 93		clipboard_list.append( tmp_patch );
 94	}
 95}
 96
 97void Clipboard::save(  ) {
 98	
 99	/* 	this is not invoked directly by the user, but every function that changes the clipboard 
100	contents should call this once it has finished making changes to commit the clipoard to disk */
101	
102	QString fileName = libraryLocation + CLIPBOARD_FILE;
103	
104	QFile file(fileName);
105	if (!file.open(QFile::WriteOnly)) {
106		QMessageBox::warning(this, tr("EWItool"),
107							 tr("Cannot write file %1:\n%2.")
108									 .arg(fileName)
109									 .arg(file.errorString()));
110		return;
111	}
112
113	QDataStream out(&file);
114	for (int p = 0; p < clipboard_list.count(); p++ ) {
115		out.writeRawData( clipboard_list.at(p).whole_patch, EWI_PATCH_LENGTH );
116	}
117	//statusBar()->showMessage(tr("Clipboard saved"), STATUS_MSG_TIMEOUT);
118}
119
120void Clipboard::appendItem( patch_t patch ) {
121	
122	QString npname;
123	
124	npname = ewi4000sPatch::toQString( patch.parameters.name );
125	
126	clipboard_listWidget->addItem( npname );
127	clipboard_list.append( patch );
128	
129	save();
130	
131	clearClipboard_pushButton->setEnabled( true );
132}
133
134void Clipboard::deleteItem() {
135	
136	if (QMessageBox::question (this, "EWItool - Delete Item",
137		tr ("Do you realy want to delete this item from the Clipboard?" ),
138			QMessageBox::No | QMessageBox::Yes
139							  ) ==  QMessageBox::No) return;
140	int row = clipboard_listWidget->currentRow();
141	if (row != -1) {
142		delete clipboard_listWidget->takeItem( row );
143		clipboard_list.removeAt( row );
144	}
145	save();
146	
147	if (clipboard_listWidget->count() == 0) clearClipboard_pushButton->setEnabled( false );
148}
149
150void Clipboard::renameItem() {
151	if (clipboard_list.count() > 0 && clipboard_listWidget->currentRow() > -1) {
152
153		// Get new name from the user
154		bool ok;
155
156		QListWidgetItem *curitem = clipboard_listWidget->currentItem();
157		int r = clipboard_listWidget->row (curitem);
158		QString text = curitem->text();
159		QString new_name = QInputDialog::getText (this, "EWItool - Rename Patch", "New Patch Name", QLineEdit::Normal, text, &ok);
160
161		if (!ok || new_name.isEmpty()) return;
162
163		new_name = new_name.simplified();
164
165		if (new_name.length() > EWI_PATCHNAME_LENGTH) {
166			QMessageBox::warning (this, tr ("EWItool - Rename"),
167								  tr ("Patch name too long!\nEnter up to %1 characters").arg (EWI_PATCHNAME_LENGTH));
168			renameItem();
169		}
170
171		// check name not already on clipboard
172		if (clipboard_listWidget->findItems (new_name, Qt::MatchExactly).count() > 0) {
173			QMessageBox::warning (this, tr ("EWItool - Rename"),
174								  tr ("That name already used on the Clipboard!\nPlease try again"));
175			renameItem();
176		}
177
178		// Save patch in the Clipboard
179		clipboard_listWidget->takeItem (r);
180		delete curitem;
181		clipboard_listWidget->insertItem (r, new_name);
182		clipboard_listWidget->setCurrentRow (r);
183		QByteArray ba = new_name.leftJustified (EWI_PATCHNAME_LENGTH, ' ').toLatin1();
184		memcpy ( (void *) &clipboard_list[r].parameters.name, (void *) ba.data(), EWI_PATCHNAME_LENGTH);
185		
186		save();
187	}
188}
189
190void Clipboard::viewHex() {
191	if (clipboard_list.count() > 0 && clipboard_listWidget->currentRow() > -1) {
192		const char *raw_patch = &clipboard_list.at( clipboard_listWidget->currentRow() ).whole_patch[0];
193		QString hex_patch;
194		hex_patch = ewi4000sPatch::hexify( (char *) raw_patch, true );
195		viewHex_dialog *h = new viewHex_dialog( hex_patch );
196		h->exec();
197	}
198}
199
200void Clipboard::exportToEPX() {
201	if ( clipboard_list.count() > 0 && clipboard_listWidget->currentRow() > -1 ) {
202
203		patch_t e_patch;
204		
205		e_patch = clipboard_list.at( clipboard_listWidget->currentRow() );
206		// get extra info for EPX from the user
207		epxSubmit_dialog *d = new epxSubmit_dialog( ewi4000sPatch::toQString( e_patch.parameters.name ) );
208		if ( d->exec() ) {
209			// submit the patch
210			e_patch.parameters.mode = EWI_EDIT;
211			e_patch.parameters.patch_num = 0x00;
212			QSettings *settings = new QSettings( "EWItool", "EWItool" );
213			epx->insertPatch(
214			    settings->value( "PatchExchange/Server" ).toString(),
215			    settings->value( "PatchExchange/UserID" ).toString(),
216			    settings->value( "PatchExchange/Password" ).toString(),
217				ewi4000sPatch::toQString( e_patch.parameters.name ),
218			    d->p_origin,
219			    d->p_type,
220			    d->p_desc,
221			    d->p_private,
222			    d->p_tags,
223			    ewi4000sPatch::hexify( e_patch.whole_patch, false )
224			);
225
226		}
227
228		delete d;
229	}
230}
231
232/**
233 * Exports an item from the Clipboard into the "export" subdir
234 */
235void Clipboard::exportToFile() {
236	
237	patch_t e_patch;
238	
239	QString e_name = libraryLocation + EXPORT_DIR + "/" + clipboard_listWidget->currentItem()->text() + ".syx";
240	QFile file(e_name);
241	if (!file.open(QFile::WriteOnly)) {
242		QMessageBox::warning(this, tr("EWItool"),
243							 tr("Cannot write file %1:\n%2.")
244									 .arg(e_name)
245									 .arg(file.errorString()));
246		return;
247	}
248	
249	e_patch = clipboard_list.at( clipboard_listWidget->currentRow() );
250	e_patch.parameters.mode = EWI_SAVE;
251	e_patch.parameters.patch_num = EXPORT_PATCH_NUM;
252	
253	QDataStream out(&file);
254	out.writeRawData( e_patch.whole_patch, EWI_PATCH_LENGTH );
255	//out.writeRawData( e_patch.whole_patch, EWI_PATCH_LENGTH );
256	QMessageBox::information(this, tr("EWItool - Patch Export"),
257							 tr("Patch exported to ") + e_name );
258	
259	
260}
261
262
263void Clipboard::selectionChanged( ) {
264		
265	if (clipboard_listWidget->selectedItems().empty()) { // no row selected
266		deleteClipboard_pushButton->setEnabled( false );
267		renameClipboard_pushButton->setEnabled( false );
268		viewHexClipboard_pushButton->setEnabled( false );
269		exchangeClipboard_pushButton->setEnabled( false );
270		exportClipboard_pushButton->setEnabled( false );
271	}
272	else {
273		deleteClipboard_pushButton->setEnabled( true );
274		renameClipboard_pushButton->setEnabled( true );
275		viewHexClipboard_pushButton->setEnabled( true );
276		exchangeClipboard_pushButton->setEnabled( true );
277		exportClipboard_pushButton->setEnabled( true );
278	}
279	
280}
281
282bool Clipboard::onClipboard( QString pname ) {
283	
284	if (clipboard_listWidget->findItems( pname, Qt::MatchExactly ).count() > 0)
285		return TRUE;
286	else
287		return FALSE;
288	
289}
290
291int Clipboard::count() {
292	
293	return clipboard_list.size();
294	
295}
296
297QString Clipboard::getNameAt( int i ) {
298	
299	return QString( clipboard_list.at( i ).parameters.name );
300	
301}
302
303patch_t Clipboard::getPatchAt( int i ) {
304	
305	return clipboard_list.at( i );
306	
307}
308
309
310
311