src/file.cpp

changeset 290
be0c367e7420
parent 289
d7bf5c11d299
child 292
4779ca562d5e
equal deleted inserted replaced
289:d7bf5c11d299 290:be0c367e7420
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */ 17 */
18 18
19 #include <QMessageBox> 19 #include <QMessageBox>
20 #include <QFileDialog> 20 #include <QFileDialog>
21 #include <qprogressbar.h>
21 #include <QDir> 22 #include <QDir>
22 #include <qthread.h> 23 #include <qthread.h>
23 24
24 #include <stdlib.h> 25 #include <stdlib.h>
25 #include "common.h" 26 #include "common.h"
35 36
36 cfg (str, io_ldpath, ""); 37 cfg (str, io_ldpath, "");
37 cfg (str, io_recentfiles, ""); 38 cfg (str, io_recentfiles, "");
38 39
39 static bool g_loadingMainFile = false; 40 static bool g_loadingMainFile = false;
41 PrimitiveLister* g_activePrimLister = null;
42 bool g_primListerMutex = false;
43 vector<PrimitiveInfo> g_Primitives;
40 44
41 // ============================================================================= 45 // =============================================================================
42 namespace LDPaths { 46 namespace LDPaths {
43 static str pathError; 47 static str pathError;
44 48
924 } 928 }
925 929
926 g_loadedFiles.clear (); 930 g_loadedFiles.clear ();
927 g_loadedFiles << filesUsed; 931 g_loadedFiles << filesUsed;
928 } 932 }
933
934 // =============================================================================
935 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
936 // =============================================================================
937 void recursiveGetFilenames (QDir dir, vector<str>& fnames) {
938 QFileInfoList flist = dir.entryInfoList ();
939 for (const QFileInfo& info : flist) {
940 if (info.fileName () == "." || info.fileName () == "..")
941 continue; // skip . and ..
942
943 if (info.isDir ())
944 recursiveGetFilenames (QDir (info.absoluteFilePath ()), fnames);
945 else
946 fnames << info.absoluteFilePath ();
947 }
948 }
949
950 void PrimitiveLister::work () {
951 g_activePrimLister = this;
952 m_prims.clear ();
953
954 QDir dir (LDPaths::prims ());
955 assert (dir.exists ());
956
957 ulong baselen = dir.absolutePath ().length ();
958
959 vector<str> fnames;
960 recursiveGetFilenames (dir, fnames);
961 emit starting (fnames.size ());
962
963 ulong i = 0;
964 for (str fname : fnames) {
965 File f (fname, File::Read);
966
967 PrimitiveInfo info;
968 info.name = fname.mid (baselen + 1); // make full path relative
969 info.name.replace ('/', '\\'); // use DOS backslashes, they're expected
970
971 if (!f.readLine (info.title))
972 info.title = "";
973
974 info.title = info.title.simplified ();
975
976 if (info.title[0] == '0') {
977 info.title.remove (0, 1); // remove 0
978 info.title = info.title.simplified ();
979 }
980
981 m_prims << info;
982 emit update (++i);
983 }
984
985 // Save to a config file
986 File conf (config::dirpath () + "prims.cfg", File::Write);
987 for (PrimitiveInfo& info : m_prims)
988 fprint (conf, "%1 %2\n", info.name, info.title);
989
990 conf.close ();
991
992 g_primListerMutex = true;
993 g_Primitives = m_prims;
994 g_primListerMutex = false;
995
996 g_activePrimLister = null;
997 emit workDone ();
998 }
999
1000 void PrimitiveLister::start () {
1001 if (g_activePrimLister)
1002 return;
1003
1004 PrimitiveLister* lister = new PrimitiveLister;
1005 QThread* listerThread = new QThread;
1006 lister->moveToThread (listerThread);
1007 connect (lister, SIGNAL (starting (ulong)), g_win, SLOT (primitiveLoaderStart (ulong)));
1008 connect (lister, SIGNAL (update (ulong)), g_win, SLOT (primitiveLoaderUpdate (ulong)));
1009 connect (lister, SIGNAL (workDone ()), g_win, SLOT (primitiveLoaderEnd ()));
1010 connect (listerThread, SIGNAL (started ()), lister, SLOT (work ()));
1011 connect (listerThread, SIGNAL (finished ()), lister, SLOT (deleteLater ()));
1012 listerThread->start ();
1013 }
1014
1015 void loadPrimitiveInfo () {
1016 g_Primitives.clear ();
1017
1018 // Try to load prims.cfg
1019 File conf (config::dirpath () + "prims.cfg", File::Read);
1020 if (!conf) {
1021 // No prims.cfg, build it
1022 PrimitiveLister::start ();
1023 return;
1024 }
1025
1026 for (str line : conf) {
1027 int space = line.indexOf (" ");
1028 if (space == -1)
1029 continue;
1030
1031 PrimitiveInfo info;
1032 info.name = line.left (space);
1033 info.title = line.mid (space + 1);
1034 g_Primitives << info;
1035 }
1036 }

mercurial