Переглянути джерело

Config file selection -c --config options on the commandline.

Mihawk 12 роки тому
батько
коміт
67698fd4b7
6 змінених файлів з 72 додано та 26 видалено
  1. 51 5
      App.cpp
  2. 1 0
      App.h
  3. 0 1
      ServerQuery.cpp
  4. 17 0
      Settings.cpp
  5. 1 1
      Settings.h
  6. 2 19
      main.cpp

+ 51 - 5
App.cpp

@@ -10,16 +10,30 @@
 #include <QtSql/QSqlQuery>
 #include <QCryptographicHash>
 #include <QHostAddress>
+#include <QThread>
+#include <QTimer>
 #include "SshClient.h"
 #include "ActiveClient.h"
 #include "Settings.h"
 
+class Sleeper: public QThread
+{
+public:
+  static void msleep(unsigned long msecs)
+  {
+    QThread::msleep(msecs);
+  }
+};
+
 App::App(int &argc, char **argv) :
   QCoreApplication(argc, argv),
   myServer(new QTcpServer()),
   mySocketConnectedFlag(false),
   myQWNETSshClient(new SshClient(this))
 {
+  if(!parseCommandLine())
+    return;
+
   print("CIMS Bot Service v0.101\n========================================================\n");
 
   setApplicationName("CIMSBOT");
@@ -27,10 +41,7 @@ App::App(int &argc, char **argv) :
   setOrganizationName("CIMS");
   setApplicationVersion("0.101");
 
-  //  myServer->listen(QHostAddress::Any, 45000);
-  //	connect(myServer, SIGNAL(newConnection()), SLOT(onNewConnection()));
-
-	myClientsFrameTimerID = startTimer(0);
+  myClientsFrameTimerID = startTimer(0);
 
   myQWNETSshClient->connectToHost("stomp", "b4r.org");
 
@@ -46,6 +57,41 @@ App::~App()
   delete myServer;
 }
 
+bool App::parseCommandLine()
+{
+  if(argc() < 2)
+    return true;
+
+  QStringList args = App::arguments();
+  QString arg;
+  QStringList::const_iterator itr;
+  for(itr = args.constBegin()+1; itr != args.constEnd(); ++itr)
+  {
+    arg = *itr;
+    if(arg == "--help" || arg == "-h")
+    {
+      printf("Usage: %s [--config/-c config_file] [-h]\n", args.at(0).section("/", -1).toAscii().data());
+      QTimer::singleShot(0, this, SLOT(quit()));
+      return false;
+    }
+    else if(arg == "--config" || arg == "-c")
+    {
+      itr++;
+      if(itr == args.constEnd())
+      {
+        printf("--config: Expected config file path.\n");
+        QTimer::singleShot(0, this, SLOT(quit()));
+        return false;
+      }
+      arg = *itr;
+      printf("Using config file [%s]...\n", arg.toAscii().data());
+      Settings::globalInstance()->changeConfigFile(*itr);
+      return true;
+    }
+  }
+  return true;
+}
+
 void App::onNewConnection()
 {
 	if(mySocketConnectedFlag)
@@ -416,8 +462,8 @@ void App::timerEvent(QTimerEvent *e)
 		{
       ac->run();
     }
-		return;
 	}
+  Sleeper::msleep(1);
 }
 
 void App::requestBroadcast(const QString &type, const QString &user, const QString &server, const QString &message)

+ 1 - 0
App.h

@@ -61,6 +61,7 @@ private:
 	void									setNick(const QString& args);
 	void									setPing(const QString& args);
 	void									help();
+  bool                  parseCommandLine();
 
 private slots:
 	/* TCP server */

+ 0 - 1
ServerQuery.cpp

@@ -31,7 +31,6 @@ ServerQuery::ServerQuery(QObject *parent) :
   myPort(27500),
   myActiveFlag(false)
 {
-  //  connect(mySocket, SIGNAL(readyRead()), SLOT(parseServerInfo()));
   connect(mySocket, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(socketError(QAbstractSocket::SocketError)));
 }
 

+ 17 - 0
Settings.cpp

@@ -21,6 +21,7 @@ along with this program.  If not, see < http://www.gnu.org/licenses/ >.
 #include <QSettings>
 #include <QCoreApplication>
 #include <QStringList>
+#include <stdio.h>
 
 Settings* Settings::ourInstance = NULL;
 QSettings* Settings::ourSettings;
@@ -35,6 +36,22 @@ Settings::~Settings()
   delete ourSettings;
 }
 
+bool Settings::changeConfigFile(const QString &fileName)
+{
+  delete ourSettings;
+  ourSettings = new QSettings(fileName, QSettings::IniFormat);
+
+  QSettings::Status status = ourSettings->status();
+  if(status != QSettings::NoError)
+  {
+    printf("Failed to load config file [%s]. Falling back to default config file.\n", fileName.toAscii().data());
+    delete ourSettings;
+    ourSettings = NULL;
+    return false;
+  }
+  return true;
+}
+
 Settings* Settings::globalInstance()
 {
   if(!ourInstance)

+ 1 - 1
Settings.h

@@ -28,7 +28,7 @@ class Settings
 {
 public:
   static Settings*  globalInstance();
-
+  bool              changeConfigFile(const QString& fileName);
   QStringList       serverList();
   void              setServerList(QStringList& list);
   QString           quakeFolder() const;

+ 2 - 19
main.cpp

@@ -1,25 +1,8 @@
-#include "Client.h"
 #include "App.h"
-#include <QThread>
-#include "Settings.h"
-
-class Sleeper: public QThread
-{
-public:
-	static void msleep(unsigned long msecs)
-	{
-		QThread::msleep(msecs);
-	}
-};
 
 int main(int argc, char **argv)
 {
-	App a(argc, argv);
+  App a(argc, argv);
 
-  for(;;)
-	{
-		App::processEvents();
-		Sleeper::msleep(1);
-	}
-	return 0;
+  return a.exec();
 }