@@ -30,7 +30,6 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi
3030 ui->setupUi (this );
3131
3232 option_copy = this ->findChild <QCheckBox *>(" option_copy" );
33- option_names = this ->findChild <QCheckBox *>(" option_names" );
3433 option_rename = this ->findChild <QCheckBox *>(" option_rename" );
3534
3635 combobox_game = this ->findChild <QComboBox *>(" combobox_game" );
@@ -121,9 +120,12 @@ QList<QList<QVariant>> MainWindow::get_duplicates_from_hash(const QString hash)
121120 QSqlQuery query (database);
122121
123122 query.exec (query_standalone.arg (hash));
124- if (query.next ()) { // found standalone match
125- if (query.value (2 ) == selected_game) {
126- matches.append ({hash_from_int (query.value (0 )), query.value (1 ), 3 , " " });
123+ if (query.isSelect ()) { // found standalone match
124+ while (query.next ()) {
125+ if (query.value (2 ) == selected_game) {
126+ matches.append ({hash_from_int (query.value (0 )), query.value (1 ), 3 , " " });
127+ query.last ();
128+ }
127129 }
128130 } else {
129131 qWarning (" Texture not found in vanilla database, skipping..." );
@@ -146,66 +148,78 @@ QList<QList<QVariant>> MainWindow::get_duplicates_from_hash(const QString hash)
146148 return matches;
147149}
148150
149- void MainWindow::generate_file_paths () {
150- // if we're in string mode, generate "fake" QFileInfos to fill file_paths
151- file_paths.clear ();
152- for (QString s : ui->text_edit_left ->toPlainText ().split (" \n " , QString::SkipEmptyParts)) {
153- file_paths.append (QFileInfo (s));
151+ void MainWindow::go_initialize () {
152+ // options
153+ file_mode = ui->text_edit_left ->isReadOnly ();
154+ copy_mode = ui->option_copy ->isChecked ();
155+ rename_mode = ui->option_rename ->isChecked ();
156+
157+ // if we're in string mode, generate "fake" QFileInfos to fill file_paths, otherwise do nothing
158+ if (!file_mode) {
159+ file_paths.clear ();
160+ for (QString s : ui->text_edit_left ->toPlainText ().split (" \n " , QString::SkipEmptyParts)) {
161+ file_paths.append (QFileInfo (s));
162+ }
154163 }
155164}
156165
157- QList<QString> MainWindow::get_hashes_from_name (const QString name) {
166+ /*
167+ * go_rename_mode: attempt to find the hashes and names of the files in text_edit_left
168+ * Rename selected files, if any, and display result
169+ */
170+ void MainWindow::go_rename_mode () {
171+ QString result;
172+ QTextStream ts (&result, QIODevice::WriteOnly);
173+ QString selected_game (QString::number (combobox_game->currentIndex () + 1 ));
158174 QSqlQuery query (database);
159- QList<QString> matches;
160-
161- query.exec (query_name.arg (name)); // get all hashes that match the name
162- while (query.next ()) { // for each match, get all reusable textures
163- matches.append (query.value (0 ).toString ());
164- }
165- return matches;
166- }
167175
168- void MainWindow::rename () {
176+ qDebug ( " Starting search... " );
169177 for (const QFileInfo &entry : file_paths) { // iterate over the selected files
178+ QString old_path = entry.filePath ();
179+ QString new_path;
180+
170181 QRegularExpressionMatch match = regex_hash.match (entry.fileName ());
171182
172- // check presence of hash in filename
173- QList<QString> hashes;
174- if (match.hasMatch ()) {
183+ if (match.hasMatch ()) { // check presence of hash in filename
175184 qDebug (" Found matching hash for %s" , match.captured (0 ).toStdString ().c_str ());
176185
177- QString groupid;
178- QList<QList<QVariant>> matches; // { crc, name, grade, notes }
179- QString selected_game (QString::number (ui->combobox_game ->currentIndex () + 1 ));
180- QSqlQuery query (database);
181-
182- query.exec (query_standalone.arg (match.captured (0 )));
183- if (query.next ()) { // found standalone match
184- if (query.value (2 ) == selected_game) {
185- QString crc = hash_from_int (query.value (0 ));
186- QString name = query.value (1 ).toString ();
187- QFile::rename (entry.filePath (), QDir (path_dest).filePath (QString (" %1_%2.%3" )).arg (name, crc, entry.suffix ()));
186+ query.exec (query_name_from_hash_game.arg (match.captured (0 ), selected_game));
187+ if (query.next ()) { // the matching hash corresponds to a texture
188+ qDebug (" Matching hash corresponds to a texture" );
189+ QString game = query.value (2 ).toString ();
190+ QString crc = hash_from_int (query.value (0 ));
191+ QString name = query.value (1 ).toString ();
192+
193+ new_path = QDir (path_dest).filePath (QString (" %1_%2.%3" )).arg (name, crc, entry.suffix ());
194+ ts << (QString (" %1 -> %2<br>" )).arg (crc, name);
195+
196+ if (file_mode) {
197+ QFile::rename (old_path, new_path);
188198 }
189199 }
190- }
200+ } else { // no hash in filename, but we'll attempt to find it from the texture name
201+ query.exec (query_hash_from_name.arg (entry.baseName ())); // get all hashes that match the name
202+
203+ if (query.next ()) {
204+ qDebug (" Found matching hash for %s" , entry.baseName ().toStdString ().c_str ());
205+
206+ ts << entry.baseName () << " <font color=\" orange\" >(from name)</font><br>" ;
207+ do { // for each match, get all reusable textures
208+ QString game = query.value (2 ).toString ();
209+ QString crc = hash_from_int (query.value (0 ));
210+ QString name = query.value (1 ).toString ();
211+
212+ ts << (QString (" (ME%1) %2 %3<br>" )).arg (game, crc, name);
213+ } while (query.next ());
214+ ts << " <br>" ;
215+ }
216+ }
191217 }
192- }
193218
194- void MainWindow::on_button_go_clicked () {
195- bool file_mode = ui->text_edit_left ->isReadOnly ();
196- bool copy_mode = ui->option_copy ->isChecked ();
197- bool names_mode = ui->option_names ->isChecked ();
198- bool rename_mode = ui->option_rename ->isChecked ();
199-
200- if (file_mode && rename_mode) {
201- rename ();
202- return ;
203- }
204-
205- // CHECKS
206- if (!file_mode) generate_file_paths ();
207- if (file_paths.isEmpty ()) return ;
219+ ui->text_edit_right ->setText (result);
220+ }
208221
222+ void MainWindow::go_normal_mode () {
209223 QString result;
210224 QTextStream ts (&result, QIODevice::WriteOnly);
211225 QString game (QString::number (combobox_game->currentIndex () + 1 ));
@@ -216,7 +230,6 @@ void MainWindow::on_button_go_clicked() {
216230 }
217231
218232 qDebug (" Checking duplicates..." );
219- bool from_name;
220233 for (const QFileInfo &entry : file_paths) { // iterate over the selected files
221234 QRegularExpressionMatch match = regex_hash.match (entry.fileName ());
222235
@@ -225,24 +238,14 @@ void MainWindow::on_button_go_clicked() {
225238 if (match.hasMatch ()) {
226239 hashes.append (match.captured (0 ));
227240 qDebug (" Found matching hash for %s" , match.captured (0 ).toStdString ().c_str ());
228- from_name = false ;
229- } else if (names_mode) {
230- QList<QString> h = get_hashes_from_name (entry.baseName ());
231- if (!h.isEmpty ()) {
232- for (QString hash : h) {
233- hashes.append (hash_from_int (hash));
234- }
235- qDebug (" Found %d matching hashes for name %s" , hashes.size (), entry.fileName ().toStdString ().c_str ());
236- from_name = true ;
237- }
238241 }
239242
240243 QList<QList<QVariant>> search_results;
241244 for (QString hash : hashes) {
242245 search_results = get_duplicates_from_hash (hash);
243246
244247 if (!search_results.isEmpty ()) {
245- ts << hash << (from_name ? " <font color= \" orange \" >(from name)</font>< br>" : " <br> " ) ;
248+ ts << hash << " < br>" ;
246249 for (QList<QVariant> search : search_results) {
247250 QString crc = search[0 ].toString ();
248251 QString name = search[1 ].toString ();
@@ -272,6 +275,20 @@ void MainWindow::on_button_go_clicked() {
272275 }
273276}
274277
278+ void MainWindow::on_button_go_clicked () {
279+ // INITIALIZATION
280+ go_initialize ();
281+
282+ // if there is nothing to do, return immediately
283+ if (file_paths.isEmpty ()) return ;
284+
285+ if (rename_mode) {
286+ go_rename_mode ();
287+ } else {
288+ go_normal_mode ();
289+ }
290+ }
291+
275292void MainWindow::on_button_clear_clicked () {
276293 qDebug (" Clearing data" );
277294 ui->text_edit_right ->clear ();
@@ -291,12 +308,14 @@ void MainWindow::run_copy_thread(MainWindow *w) {
291308 file = w->copy_queue .dequeue ();
292309
293310
294- if (QFile (file.second ).exists () && QFile (file.second ).size () < QFile (file.first ).size ()) {
295- qDebug (" Destination already exists, but this file is larger." );
296- qDebug (" Copying %s to %s" , file.first .toStdString ().c_str (), file.second .toStdString ().c_str ());
297- QFile::remove (file.second );
298- QFile::copy (file.first , file.second );
299- } else if (!QFile (file.second ).exists ()) {
311+ if (QFile (file.second ).exists ()) {
312+ qDebug (" Destination already exists, renaming" );
313+ int i = 1 ;
314+ while (QFile (file.second + " _" + i).exists ()) { i++; }
315+
316+ qDebug (" Copying %s to %s" , file.first .toStdString ().c_str (), (file.second + " _" + i).toStdString ().c_str ());
317+ QFile::copy (file.first , file.second + " _" + i);
318+ } else {
300319 qDebug (" Copying %s to %s" , file.first .toStdString ().c_str (), file.second .toStdString ().c_str ());
301320 QFile::copy (file.first , file.second );
302321 }
0 commit comments