/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. #include <QFile>
  21. #include <QInputDialog>
  22. #include <QMessageBox>
  23. #include "clipboard.h"
  24. #include "epxsubmit_dialog.h"
  25. #include "viewhex_dialog.h"
  26. Clipboard::Clipboard( QString libraryLoc, patchExchange *main_epx, QWidget *parent)
  27. : QWidget( parent )
  28. {
  29. libraryLocation = libraryLoc;
  30. setupUi( this );
  31. epx = main_epx;
  32. // connect the buttons
  33. connect( clearClipboard_pushButton, SIGNAL(clicked()), this, SLOT( clearAll() ));
  34. connect( deleteClipboard_pushButton, SIGNAL(clicked()), this, SLOT( deleteItem() ));
  35. connect( renameClipboard_pushButton, SIGNAL(clicked()), this, SLOT( renameItem() ));
  36. connect( viewHexClipboard_pushButton, SIGNAL(clicked()), this, SLOT( viewHex() ));
  37. connect( exchangeClipboard_pushButton, SIGNAL(clicked()), this, SLOT( exportToEPX() ));
  38. connect( exportClipboard_pushButton, SIGNAL(clicked()), this, SLOT( exportToFile() ));
  39. // connect other interesting events
  40. connect( clipboard_listWidget, SIGNAL( itemSelectionChanged( ) ), this, SLOT( selectionChanged( ) ) ) ;
  41. load();
  42. selectionChanged();
  43. if (clipboard_listWidget->count() == 0) clearClipboard_pushButton->setEnabled( false );
  44. }
  45. Clipboard::~Clipboard()
  46. {
  47. }
  48. void Clipboard::clearAll( ) {
  49. if (QMessageBox::question (this, "EWItool - Clear Clipboard",
  50. tr ("Do you realy want to clear the Clipboard completely?" ),
  51. QMessageBox::No | QMessageBox::Yes
  52. ) == QMessageBox::No) return;
  53. clipboard_listWidget->clear();
  54. clipboard_list.clear();
  55. QString fileName = libraryLocation + "/CLIPBOARD.CLP";
  56. QFile file(fileName);
  57. file.remove();
  58. clearClipboard_pushButton->setEnabled( false );
  59. }
  60. void Clipboard::load( ) {
  61. patch_t tmp_patch;
  62. QString fileName = libraryLocation + CLIPBOARD_FILE;
  63. QFile file(fileName);
  64. if (!file.open(QFile::ReadOnly)) {
  65. return;
  66. }
  67. clipboard_listWidget->clear();
  68. clipboard_list.clear();
  69. QDataStream inp(&file);
  70. while (!inp.atEnd()) {
  71. inp.readRawData( (char *) &tmp_patch, EWI_PATCH_LENGTH );
  72. clipboard_listWidget->addItem( ewi4000sPatch::toQString( tmp_patch.parameters.name ) );
  73. clipboard_list.append( tmp_patch );
  74. }
  75. }
  76. void Clipboard::save( ) {
  77. /* this is not invoked directly by the user, but every function that changes the clipboard
  78. contents should call this once it has finished making changes to commit the clipoard to disk */
  79. QString fileName = libraryLocation + CLIPBOARD_FILE;
  80. QFile file(fileName);
  81. if (!file.open(QFile::WriteOnly)) {
  82. QMessageBox::warning(this, tr("EWItool"),
  83. tr("Cannot write file %1:\n%2.")
  84. .arg(fileName)
  85. .arg(file.errorString()));
  86. return;
  87. }
  88. QDataStream out(&file);
  89. for (int p = 0; p < clipboard_list.count(); p++ ) {
  90. out.writeRawData( clipboard_list.at(p).whole_patch, EWI_PATCH_LENGTH );
  91. }
  92. //statusBar()->showMessage(tr("Clipboard saved"), STATUS_MSG_TIMEOUT);
  93. }
  94. void Clipboard::appendItem( patch_t patch ) {
  95. QString npname;
  96. npname = ewi4000sPatch::toQString( patch.parameters.name );
  97. clipboard_listWidget->addItem( npname );
  98. clipboard_list.append( patch );
  99. save();
  100. clearClipboard_pushButton->setEnabled( true );
  101. }
  102. void Clipboard::deleteItem() {
  103. if (QMessageBox::question (this, "EWItool - Delete Item",
  104. tr ("Do you realy want to delete this item from the Clipboard?" ),
  105. QMessageBox::No | QMessageBox::Yes
  106. ) == QMessageBox::No) return;
  107. int row = clipboard_listWidget->currentRow();
  108. if (row != -1) {
  109. delete clipboard_listWidget->takeItem( row );
  110. clipboard_list.removeAt( row );
  111. }
  112. save();
  113. if (clipboard_listWidget->count() == 0) clearClipboard_pushButton->setEnabled( false );
  114. }
  115. void Clipboard::renameItem() {
  116. if (clipboard_list.count() > 0 && clipboard_listWidget->currentRow() > -1) {
  117. // Get new name from the user
  118. bool ok;
  119. QListWidgetItem *curitem = clipboard_listWidget->currentItem();
  120. int r = clipboard_listWidget->row (curitem);
  121. QString text = curitem->text();
  122. QString new_name = QInputDialog::getText (this, "EWItool - Rename Patch", "New Patch Name", QLineEdit::Normal, text, &ok);
  123. if (!ok || new_name.isEmpty()) return;
  124. new_name = new_name.simplified();
  125. if (new_name.length() > EWI_PATCHNAME_LENGTH) {
  126. QMessageBox::warning (this, tr ("EWItool - Rename"),
  127. tr ("Patch name too long!\nEnter up to %1 characters").arg (EWI_PATCHNAME_LENGTH));
  128. renameItem();
  129. }
  130. // check name not already on clipboard
  131. if (clipboard_listWidget->findItems (new_name, Qt::MatchExactly).count() > 0) {
  132. QMessageBox::warning (this, tr ("EWItool - Rename"),
  133. tr ("That name already used on the Clipboard!\nPlease try again"));
  134. renameItem();
  135. }
  136. // Save patch in the Clipboard
  137. clipboard_listWidget->takeItem (r);
  138. delete curitem;
  139. clipboard_listWidget->insertItem (r, new_name);
  140. clipboard_listWidget->setCurrentRow (r);
  141. QByteArray ba = new_name.leftJustified (EWI_PATCHNAME_LENGTH, ' ').toLatin1();
  142. memcpy ( (void *) &clipboard_list[r].parameters.name, (void *) ba.data(), EWI_PATCHNAME_LENGTH);
  143. save();
  144. }
  145. }
  146. void Clipboard::viewHex() {
  147. if (clipboard_list.count() > 0 && clipboard_listWidget->currentRow() > -1) {
  148. const char *raw_patch = &clipboard_list.at( clipboard_listWidget->currentRow() ).whole_patch[0];
  149. QString hex_patch;
  150. hex_patch = ewi4000sPatch::hexify( (char *) raw_patch, true );
  151. viewHex_dialog *h = new viewHex_dialog( hex_patch );
  152. h->exec();
  153. }
  154. }
  155. void Clipboard::exportToEPX() {
  156. if ( clipboard_list.count() > 0 && clipboard_listWidget->currentRow() > -1 ) {
  157. patch_t e_patch;
  158. e_patch = clipboard_list.at( clipboard_listWidget->currentRow() );
  159. // get extra info for EPX from the user
  160. epxSubmit_dialog *d = new epxSubmit_dialog( ewi4000sPatch::toQString( e_patch.parameters.name ) );
  161. if ( d->exec() ) {
  162. // submit the patch
  163. e_patch.parameters.mode = EWI_EDIT;
  164. e_patch.parameters.patch_num = 0x00;
  165. QSettings *settings = new QSettings( "EWItool", "EWItool" );
  166. epx->insertPatch(
  167. settings->value( "PatchExchange/Server" ).toString(),
  168. settings->value( "PatchExchange/UserID" ).toString(),
  169. settings->value( "PatchExchange/Password" ).toString(),
  170. ewi4000sPatch::toQString( e_patch.parameters.name ),
  171. d->p_origin,
  172. d->p_type,
  173. d->p_desc,
  174. d->p_private,
  175. d->p_tags,
  176. ewi4000sPatch::hexify( e_patch.whole_patch, false )
  177. );
  178. }
  179. delete d;
  180. }
  181. }
  182. /**
  183. * Exports an item from the Clipboard into the "export" subdir
  184. */
  185. void Clipboard::exportToFile() {
  186. patch_t e_patch;
  187. QString e_name = libraryLocation + EXPORT_DIR + "/" + clipboard_listWidget->currentItem()->text() + ".syx";
  188. QFile file(e_name);
  189. if (!file.open(QFile::WriteOnly)) {
  190. QMessageBox::warning(this, tr("EWItool"),
  191. tr("Cannot write file %1:\n%2.")
  192. .arg(e_name)
  193. .arg(file.errorString()));
  194. return;
  195. }
  196. e_patch = clipboard_list.at( clipboard_listWidget->currentRow() );
  197. e_patch.parameters.mode = EWI_SAVE;
  198. e_patch.parameters.patch_num = EXPORT_PATCH_NUM;
  199. QDataStream out(&file);
  200. out.writeRawData( e_patch.whole_patch, EWI_PATCH_LENGTH );
  201. //out.writeRawData( e_patch.whole_patch, EWI_PATCH_LENGTH );
  202. QMessageBox::information(this, tr("EWItool - Patch Export"),
  203. tr("Patch exported to ") + e_name );
  204. }
  205. void Clipboard::selectionChanged( ) {
  206. if (clipboard_listWidget->selectedItems().empty()) { // no row selected
  207. deleteClipboard_pushButton->setEnabled( false );
  208. renameClipboard_pushButton->setEnabled( false );
  209. viewHexClipboard_pushButton->setEnabled( false );
  210. exchangeClipboard_pushButton->setEnabled( false );
  211. exportClipboard_pushButton->setEnabled( false );
  212. }
  213. else {
  214. deleteClipboard_pushButton->setEnabled( true );
  215. renameClipboard_pushButton->setEnabled( true );
  216. viewHexClipboard_pushButton->setEnabled( true );
  217. exchangeClipboard_pushButton->setEnabled( true );
  218. exportClipboard_pushButton->setEnabled( true );
  219. }
  220. }
  221. bool Clipboard::onClipboard( QString pname ) {
  222. if (clipboard_listWidget->findItems( pname, Qt::MatchExactly ).count() > 0)
  223. return TRUE;
  224. else
  225. return FALSE;
  226. }
  227. int Clipboard::count() {
  228. return clipboard_list.size();
  229. }
  230. QString Clipboard::getNameAt( int i ) {
  231. return QString( clipboard_list.at( i ).parameters.name );
  232. }
  233. patch_t Clipboard::getPatchAt( int i ) {
  234. return clipboard_list.at( i );
  235. }