QTfrontend/util/namegen.cpp
changeset 6160 863d3edf5690
parent 6060 fdfc01419815
child 6167 728cabee2c9f
--- a/QTfrontend/util/namegen.cpp	Thu Oct 20 13:10:24 2011 +0200
+++ b/QTfrontend/util/namegen.cpp	Thu Oct 20 23:25:16 2011 +0200
@@ -21,10 +21,11 @@
 #include <QTextStream>
 #include <QStringList>
 #include <QLineEdit>
-#include "namegen.h"
+
 #include "hwform.h"
-#include "hwconsts.h"
+#include "HWDataManager.h"
 
+#include "namegen.h"
 
 HWNamegen::HWNamegen() {}
 
@@ -123,17 +124,13 @@
 {
     QStringList list;
 
-    QFile file;
+    // find .txt to load the names from
+    QFile * file = HWDataManager::instance().findFileForRead(QString(
+                                                "Names/%1.txt").arg(filename));
 
-    // find .cfg to load the names from
-    file.setFileName(QString("%1/Data/Names/%2.txt").arg(cfgdir->absolutePath()).arg(filename));
-    if (!file.exists())
-        file.setFileName(QString("%1/Names/%2.txt").arg(datadir->absolutePath()).arg(filename));
-
-    if (file.open(QIODevice::ReadOnly | QIODevice::Text))
+    if (file->exists() && file->open(QIODevice::ReadOnly | QIODevice::Text))
     {
-
-        QTextStream in(&file);
+        QTextStream in(file);
         while (!in.atEnd())
         {
             QString line = in.readLine();
@@ -142,6 +139,9 @@
         }
     }
 
+    // this QFile isn't needed any further
+    delete file;
+
     if (list.size() == 0)
          list.append(filename);
 
@@ -153,17 +153,13 @@
 {
     QStringList list;
 
-    QFile file;
+    // find .cfg to load the dicts from
+    QFile * file = HWDataManager::instance().findFileForRead(QString(
+                                                "Names/%1.cfg").arg(hatname));
 
-    // find .cfg to load the names from
-    file.setFileName(QString("%1/Data/Names/%2.cfg").arg(cfgdir->absolutePath()).arg(hatname));
-    if (!file.exists())
-        file.setFileName(QString("%1/Names/%2.cfg").arg(datadir->absolutePath()).arg(hatname));
-
-
-    if (file.open(QIODevice::ReadOnly | QIODevice::Text))
+    if (file->exists() && file->open(QIODevice::ReadOnly | QIODevice::Text))
     {
-        QTextStream in(&file);
+        QTextStream in(file);
         while (!in.atEnd())
         {
             QString line = in.readLine();
@@ -172,6 +168,9 @@
         }
     }
 
+    // this QFile isn't needed any further
+    delete file;
+
     if (list.size() == 0)
          list.append(QString("generic"));
 
@@ -181,56 +180,60 @@
 // loades types from ini files. returns true on success.
 bool HWNamegen::loadTypes()
 {
-    QFile file;
+    typesAvailable = false;
 
-    // find .cfg to load the names from
-    file.setFileName(QString("%1/Data/Names/types.ini").arg(cfgdir->absolutePath()));
-    if (!file.exists())
-        file.setFileName(QString("%1/Names/types.ini").arg(datadir->absolutePath()));
+    // find .ini to load the names from
+    QFile * file =
+          HWDataManager::instance().findFileForRead(QString("Names/types.ini"));
 
 
-    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
-        return false;
-
-    int counter = 0; //counter starts with 0 (teamnames mode)
-    TypesTeamnames.append(QStringList());
-    TypesHatnames.append(QStringList());
+    if (file->exists() && file->open(QIODevice::ReadOnly | QIODevice::Text))
+    {
 
-    QTextStream in(&file);
-    while (!in.atEnd())
-    {
-        QString line = in.readLine();
-        if (line == QString("#####"))
+        int counter = 0; //counter starts with 0 (teamnames mode)
+        TypesTeamnames.append(QStringList());
+        TypesHatnames.append(QStringList());
+
+        QTextStream in(file);
+        while (!in.atEnd())
         {
-            counter++; //toggle mode (teamnames || hats)
-            if ((counter%2) == 0)
+            QString line = in.readLine();
+            if (line == QString("#####"))
             {
-                TypesTeamnames.append(QStringList());
-                TypesHatnames.append(QStringList());
+                counter++; //toggle mode (teamnames || hats)
+                if ((counter%2) == 0)
+                {
+                    TypesTeamnames.append(QStringList());
+                    TypesHatnames.append(QStringList());
+                }
             }
-        }
-        else if ((line == QString("*****")) || (line == QString("*END*")))
-        {
-            typesAvailable = true;
-            return true; // bye bye
-        }
-        else
-        {
-            if ((counter%2) == 0)
+            else if ((line == QString("*****")) || (line == QString("*END*")))
             {
-                // even => teamnames mode
-                TypesTeamnames[(counter/2)].append(line);
+                typesAvailable = true;
+                return true; // bye bye
             }
             else
             {
-                // odd => hats mode
-                TypesHatnames[((counter-1)/2)].append(line);
+                if ((counter%2) == 0)
+                {
+                    // even => teamnames mode
+                    TypesTeamnames[(counter/2)].append(line);
+                }
+                else
+                {
+                    // odd => hats mode
+                    TypesHatnames[((counter-1)/2)].append(line);
+                }
             }
         }
+
+        typesAvailable = true;
     }
 
-    typesAvailable = true;
-    return true;
+    // this QFile isn't needed any further
+    delete file;
+
+    return typesAvailable;
 }
 
 
@@ -240,22 +243,16 @@
     QStringList Graves;
 
     //list all available Graves
-    QDir tmpdir;
-    tmpdir.cd(cfgdir->absolutePath());
-    tmpdir.cd("Data/Graphics/Graves");
-    tmpdir.setFilter(QDir::Files);
-    Graves.append(tmpdir.entryList(QStringList("*.png")).replaceInStrings(QRegExp("^(.*)\\.png"), "\\1"));
-
-    tmpdir.cd(datadir->absolutePath());
-    tmpdir.cd("Graphics/Graves");
-    tmpdir.setFilter(QDir::Files);
-    QStringList tmpList = tmpdir.entryList(QStringList("*.png")).replaceInStrings(QRegExp("^(.*)\\.png"), "\\1");
-    for (QStringList::Iterator it = tmpList.begin(); it != tmpList.end(); ++it) 
-        if (!Graves.contains(*it,Qt::CaseInsensitive)) Graves.append(*it);
+    Graves.append(HWDataManager::instance().entryList(
+                         "Graphics/Graves",
+                         QDir::Files,
+                         QStringList("*.png")
+                     ).replaceInStrings(QRegExp("^(.*)\\.png"), "\\1")
+                 );
 
     if(Graves.size()==0)
     {
-        //do some serious error handling
+        // TODO do some serious error handling
         return "Error";
     }
 
@@ -268,15 +265,16 @@
     QStringList Forts;
 
     //list all available Forts
-    QDir tmpdir;
-    tmpdir.cd(datadir->absolutePath());
-    tmpdir.cd("Forts");
-    tmpdir.setFilter(QDir::Files);
-    Forts.append(tmpdir.entryList(QStringList("*L.png")).replaceInStrings(QRegExp("^(.*)L\\.png"), "\\1"));
+    Forts.append(HWDataManager::instance().entryList(
+                        "Forts",
+                        QDir::Files,
+                        QStringList("*L.png")
+                    ).replaceInStrings(QRegExp("^(.*)L\\.png"), "\\1")
+                );
 
     if(Forts.size()==0)
     {
-        //do some serious error handling
+        // TODO do some serious error handling
         return "Error";
     }