]> Shamusworld >> Repos - virtualjaguar/blob - src/gui/filethread.cpp
Added missing images, added some fixes to ZIP file support
[virtualjaguar] / src / gui / filethread.cpp
1 //
2 // filethread.cpp - File discovery thread
3 //
4 // by James L. Hammons
5 // (C) 2010 Underground Software
6 //
7 // JLH = James L. Hammons <jlhamm@acm.org>
8 //
9 // Who  When        What
10 // ---  ----------  -------------------------------------------------------------
11 // JLH  01/28/2010  Created this file
12 // JLH  02/16/2010  Moved RomIdentifier stuff to its own file
13 //
14
15 #include "filethread.h"
16
17 #include <QtGui>
18 #include "crc32.h"
19 #include "filedb.h"
20 #include "settings.h"
21
22 FileThread::FileThread(QObject * parent/*= 0*/): QThread(parent), abort(false)
23 {
24 }
25
26 FileThread::~FileThread()
27 {
28         mutex.lock();
29         abort = true;
30         condition.wakeOne();
31         mutex.unlock();
32
33         wait();
34 }
35
36 void FileThread::Go(void)
37 {
38         QMutexLocker locker(&mutex);
39         start();
40 }
41
42 /*
43 Our strategy here is like so:
44 Look at the files in the directory pointed to by ROMPath.
45 For each file in the directory, take the CRC32 of it and compare it to the CRC
46 in the romList[]. If there's a match, put it in a list and note it's index value
47 in romList for future reference.
48
49 When constructing the list, use the index to pull up an image of the cart and
50 put that in the list. User picks from a graphical image of the cart.
51
52 Ideally, the label will go into the archive along with the ROM image, but that's
53 for the future...
54 Maybe box art, screenshots will go as well...
55 */
56
57 //
58 // Here's the thread's actual execution path...
59 //
60 void FileThread::run(void)
61 {
62         QDir romDir(vjs.ROMPath);
63 //      QDir romDir("../virtualjaguar/roms/rarities/");
64         QFileInfoList list = romDir.entryInfoList();
65
66 /*
67 Another thing we'll probably have to do here is check for compressed files and
68 decompress/fish around in them to find what we need. :-P
69 */
70
71         for(int i=0; i<list.size(); i++)
72         {
73                 if (abort)
74 #if 1
75 {
76 printf("FileThread: Aborting!!!\n");
77 #endif
78                         return;
79 #if 1
80 }
81 #endif
82
83                 QFileInfo fileInfo = list.at(i);
84                 QFile file(romDir.filePath(fileInfo.fileName()));
85                 uint8 * buffer = new uint8[fileInfo.size()];
86
87                 if (file.open(QIODevice::ReadOnly))
88                 {
89                         file.read((char *)buffer, fileInfo.size());
90                         uint32 crc = crc32_calcCheckSum(buffer, fileInfo.size());
91                         file.close();
92
93                         uint32 index = FindCRCIndexInFileList(crc);
94
95 // Mebbe we should pass a index AND a QImage here???
96                         if (index != 0xFFFFFFFF && !(romList[index].flags & FF_BIOS))
97                                 emit FoundAFile(index);
98                 }
99
100                 delete[] buffer;
101         }
102 }
103
104 //
105 // Find a CRC in the ROM list (simple brute force algorithm).
106 // If it's there, return the index, otherwise return $FFFFFFFF
107 //
108 uint32 FileThread::FindCRCIndexInFileList(uint32 crc)
109 {
110         for(int i=0; romList[i].crc32!=0xFFFFFFFF; i++)
111         {
112                 if (romList[i].crc32 == crc)
113                         return i;
114         }
115
116         return 0xFFFFFFFF;
117 }
118