On Thursday 09 August 2007 14:21:10 Aurélien Gâteau wrote:
> On Thursday 09 August 2007 11:36:58 Andreas Schneider wrote:
> > Hi,
> >
> > I just wanted to start wengophone in the debug directory. I know there
> > is a resource parameter, but I can't find it out cause passing --help
> > already tries to access the resource directory.
> >
> > This is stupid. Initialization is done to early.
>
> Agreed, problem is: right now CommandLineParser requires the Config to be
> loaded. I'll try to address that this afternoon.
Here it is. Attached patch moves loading of configuration to
CommandLineParser. This way it can handles --help first, then load config,
then set config related options.
It's not the nicest way to do it, it would be nicer to have CommandLineParser
uncoupled from Config, by providing the command line options as accessors in
the class. But I didn't have time for that.
I am going to be away for the next two weeks so I won't commit it, in case it
breaks something. You can commit it if you want. Otherwise I'll commit it
when I am back to work, provided it does not introduce any bug.
Aurélien
Index: src/model/config/CommandLineParser.cpp
===================================================================
--- src/model/config/CommandLineParser.cpp (révision 12203)
+++ src/model/config/CommandLineParser.cpp (copie de travail)
@@ -20,6 +20,7 @@
#include "CommandLineParser.h"
#include <model/config/Config.h>
+#include <model/config/ConfigImporter.h>
#include <model/config/ConfigManager.h>
#include <model/config/ConfigManagerFileStorage.h>
@@ -33,25 +34,44 @@
#include <util/Path.h>
#include <util/StringList.h>
-CommandLineParser::CommandLineParser(int argc, char * argv[]) {
- bool reloadConfig = false;
+#include <cutil/global.h>
- Config & config = ConfigManager::getInstance().getCurrentConfig();
-
- _severalWengoPhoneAllowed = false;
+static const char* CONFIG_SUBDIR = "/config/";
- //Set executable name
- std::string executableName;
- if (argv[0]) {
- char * p = strrchr(argv[0], Path::getPathSeparator()[0]);
- executableName = std::string(p ? p + 1 : argv[0]);
- config.set(Config::EXECUTABLE_NAME_KEY, executableName);
+static std::string getDefaultResourcesDir() {
+ // Init resourcesDir
+ std::string resourcesDir;
+#if defined(OS_WINDOWS)
+ resourcesDir = Path::getApplicationDirPath();
+#elif defined(OS_MACOSX)
+ resourcesDir = Path::getApplicationResourcesDirPath();
+#elif defined(OS_LINUX) and defined(OW_RESOURCEDIR)
+ resourcesDir = OW_RESOURCEDIR + File::getPathSeparator();
+#endif
+
+ //Check if resourcesDir exist, if not then Path::getApplicationDirPath()
+ if (!File::exists(resourcesDir)) {
+ resourcesDir = Path::getApplicationDirPath();
}
- //Reset all key to default
- config.resetToDefaultValue(Config::CMDLINE_BACKGROUND_MODE_ENABLE_KEY);
- config.resetToDefaultValue(Config::CMDLINE_PLACECALL_KEY);
+ return resourcesDir;
+}
+static std::string getDefaultUserConfigDir(const std::string& binaryName) {
+ std::string configDir = Path::getConfigurationDirPath();
+#if defined(OS_LINUX)
+ configDir += '.';
+#endif
+ configDir += binaryName;
+ configDir += "/";
+ return configDir;
+}
+
+// FIXME: binaryName should get read from buildconfig, but it's only available
+// in the qt subdir for now :-(
+CommandLineParser::CommandLineParser(const std::string& binaryName, int argc, char * argv[]) {
+ _severalWengoPhoneAllowed = false;
+
try {
options_description desc("Allowed options");
desc.add_options()
@@ -69,69 +89,86 @@
variables_map vm;
store(parse_command_line(argc, argv, desc), vm);
+ // Handle help first
if (vm.count("help")) {
cout << desc << endl;
exit(0);
}
- if (vm.count("background")) {
- LOG_DEBUG("run in background mode");
- config.set(Config::CMDLINE_BACKGROUND_MODE_ENABLE_KEY, true);
- }
-
- if (vm.count("several")) {
- LOG_DEBUG("allow several WengoPhone at the same time");
- _severalWengoPhoneAllowed = true;
- }
-
+ // Now init config
+ std::string resourcesDir = getDefaultResourcesDir();
if (vm.count("resources")) {
- std::string newPath = vm["resources"].as<string>();
- config.setResourcesDir(newPath);
+ resourcesDir = vm["resources"].as<string>();
// change config directory
- if (File::isDirectory(newPath)) {
- LOG_INFO("changing resources dir to " + newPath);
- reloadConfig = true;
+ if (File::isDirectory(resourcesDir)) {
+ LOG_INFO("Changing resources dir to " + resourcesDir);
} else {
- LOG_ERROR("Can't set resources dir: " + newPath + " does not exist or is not a directory");
+ LOG_ERROR("Can't set resources dir: " + resourcesDir + " does not exist or is not a directory");
}
}
-
+
+ std::string userConfigDir = getDefaultUserConfigDir(binaryName);
if (vm.count("configpath")) {
- std::string newPath = vm["configpath"].as<string>();
+ userConfigDir = vm["configpath"].as<string>();
std::string pathSeparator = File::getPathSeparator();
- int newpathSize = newPath.size();
- if (newPath[newpathSize-1] != pathSeparator[0]) {
+ int size = userConfigDir.size();
+ if (userConfigDir[size-1] != pathSeparator[0]) {
// remove " at the end
- if (newPath[newpathSize-1] == '\"') {
- newPath = newPath.substr(0,newpathSize-1);
+ if (userConfigDir[size-1] == '\"') {
+ userConfigDir = userConfigDir.substr(0,size-1);
}
////
// add path separator
- newPath += pathSeparator;
+ userConfigDir += pathSeparator;
////
}
+ LOG_INFO("Changing user configuration dir to " + userConfigDir);
+ }
- // create directory if it does'nt exist
- if (!File::exists(newPath)) {
- File::createPath(newPath);
- }
- ////
-
- // change config directory
- if (File::isDirectory(newPath)) {
- LOG_DEBUG("change configuration path to " + newPath);
- config.setConfigDir(newPath);
- reloadConfig = true;
-
- } else {
- LOG_ERROR("Can not change configuration path because " + newPath + " is NOT a directory");
- }
- ////
+ // create directory if it doesn't exist
+ if (!File::exists(userConfigDir)) {
+ File::createPath(userConfigDir);
}
+ if (!File::isDirectory(userConfigDir)) {
+ LOG_ERROR("Can not change user configuration dir because " + userConfigDir + " is NOT a directory");
+ }
+ // Init config
+ ConfigManagerFileStorage configManagerStorage(ConfigManager::getInstance());
+ configManagerStorage.loadSystemConfig(resourcesDir + CONFIG_SUBDIR);
+ Config::setConfigDir(userConfigDir);
+ ConfigImporter importer;
+ importer.importConfig();
+ configManagerStorage.loadUserConfig(userConfigDir);
+
+ Config & config = ConfigManager::getInstance().getCurrentConfig();
+ config.setResourcesDir(resourcesDir);
+
+ //Set executable name
+ std::string executableName;
+ if (argv[0]) {
+ char * p = strrchr(argv[0], Path::getPathSeparator()[0]);
+ executableName = std::string(p ? p + 1 : argv[0]);
+ config.set(Config::EXECUTABLE_NAME_KEY, executableName);
+ }
+
+ //Reset all key to default
+ config.resetToDefaultValue(Config::CMDLINE_BACKGROUND_MODE_ENABLE_KEY);
+ config.resetToDefaultValue(Config::CMDLINE_PLACECALL_KEY);
+
+ if (vm.count("background")) {
+ LOG_DEBUG("run in background mode");
+ config.set(Config::CMDLINE_BACKGROUND_MODE_ENABLE_KEY, true);
+ }
+
+ if (vm.count("several")) {
+ LOG_DEBUG("allow several WengoPhone at the same time");
+ _severalWengoPhoneAllowed = true;
+ }
+
if (vm.count("command")) {
static String commandCall = "call/";
String command = String(vm["command"].as<string>());
@@ -149,12 +186,6 @@
catch(exception & e) {
cerr << e.what() << endl;
}
-
- if (reloadConfig) {
- ConfigManagerFileStorage configManagerStorage(ConfigManager::getInstance());
- configManagerStorage.load();
- }
-
}
CommandLineParser::~CommandLineParser() {
Index: src/model/config/Config.cpp
===================================================================
--- src/model/config/Config.cpp (révision 12203)
+++ src/model/config/Config.cpp (copie de travail)
@@ -210,22 +210,6 @@
// Version is set to sticky otherwise it would be impossible to detect
// config version changes
addStickyKey(CONFIG_VERSION_KEY);
-
- // Init resourcesDir
- std::string resourcesDir;
-#if defined(OS_WINDOWS)
- resourcesDir = Path::getApplicationDirPath();
-#elif defined(OS_MACOSX)
- resourcesDir = Path::getApplicationResourcesDirPath();
-#elif defined(OS_LINUX) and defined(OW_RESOURCEDIR)
- resourcesDir = OW_RESOURCEDIR + File::getPathSeparator();
-
- //Check if resourcesDir exist, if not then Path::getApplicationDirPath()
- if (!File::exists(resourcesDir)) {
- resourcesDir = Path::getApplicationDirPath();
- }
-#endif
- setResourcesDir(resourcesDir);
}
Config::~Config() {
Index: src/model/config/CommandLineParser.h
===================================================================
--- src/model/config/CommandLineParser.h (révision 12203)
+++ src/model/config/CommandLineParser.h (copie de travail)
@@ -20,17 +20,20 @@
#ifndef OWCOMMANDLINEPARSER_H
#define OWCOMMANDLINEPARSER_H
+#include <string>
+
/**
* Parse command line option & set associated key in Config.
*
* @ingroup model
* @author Mathieu Stute
* @euthor Xavier Desjardins
+ * @euthor Aurelien Gateau
*/
class CommandLineParser {
public:
- CommandLineParser(int argc, char * argv[]);
+ CommandLineParser(const std::string& binaryName, int argc, char * argv[]);
~CommandLineParser();
Index: src/model/config/ConfigManagerFileStorage.cpp
===================================================================
--- src/model/config/ConfigManagerFileStorage.cpp (révision 12203)
+++ src/model/config/ConfigManagerFileStorage.cpp (copie de travail)
@@ -35,16 +35,8 @@
ConfigManagerFileStorage::~ConfigManagerFileStorage() {
}
-bool ConfigManagerFileStorage::load() {
- if (!loadSystemConfig()) {
- return false;
- }
- return loadUserConfig();
-}
-
-bool ConfigManagerFileStorage::loadSystemConfig() {
+bool ConfigManagerFileStorage::loadSystemConfig(const std::string& systemDir) {
std::string systemData;
- std::string systemDir = _configManager.getCurrentConfig().getResourcesDir() + "/config/" ;
FileReader systemFile(systemDir + CONFIG_FILENAME);
if (!systemFile.open()) {
@@ -57,9 +49,8 @@
}
-bool ConfigManagerFileStorage::loadUserConfig() {
+bool ConfigManagerFileStorage::loadUserConfig(const std::string& userDir) {
std::string userData;
- std::string userDir = _configManager.getCurrentConfig().getConfigDir();
FileReader userFile(userDir + CONFIG_FILENAME);
if (userFile.open()) {
userData = userFile.read();
Index: src/model/config/ConfigManagerFileStorage.h
===================================================================
--- src/model/config/ConfigManagerFileStorage.h (révision 12203)
+++ src/model/config/ConfigManagerFileStorage.h (copie de travail)
@@ -20,6 +20,8 @@
#ifndef CONFIGMANAGERFILESTORAGE_H
#define CONFIGMANAGERFILESTORAGE_H
+#include <string>
+
class ConfigManager;
/**
@@ -35,12 +37,10 @@
virtual ~ConfigManagerFileStorage();
- bool load();
+ bool loadSystemConfig(const std::string& systemDir);
- bool loadSystemConfig();
+ bool loadUserConfig(const std::string& userDir);
- bool loadUserConfig();
-
bool save();
private:
Index: src/presentation/main.cpp
===================================================================
--- src/presentation/main.cpp (révision 12203)
+++ src/presentation/main.cpp (copie de travail)
@@ -20,8 +20,6 @@
#include <system/Processes.h>
#include <model/WengoPhone.h>
-#include <model/config/ConfigImporter.h>
-#include <model/config/ConfigManagerFileStorage.h>
#include <model/config/ConfigManager.h>
#include <model/config/Config.h>
#include <model/config/CommandLineParser.h>
@@ -107,17 +105,6 @@
return stream.str();
}
-static void initConfigDir(Config& config) {
- std::string configDir = Path::getConfigurationDirPath();
-#if defined(OS_LINUX)
- configDir += '.';
-#endif
- configDir += BINARY_NAME;
- configDir += "/";
- File::createPath(configDir);
- config.setConfigDir(configDir);
-}
-
#if defined(OS_MACOSX) || defined(OS_LINUX)
static void sigpipe_catcher(int sig) {
#ifndef NDEBUG
@@ -181,8 +168,21 @@
#endif
#endif
+ //Init presentation factories before parsing the command line so that Qt or
+ //Gtk get a chance to parse their command line options ('-style' for Qt for
+ //example)
+ PFactory * pFactory = NULL;
+#ifdef GTKINTERFACE
+ pFactory = new GtkFactory(argc, argv);
+#else
+ pFactory = new QtFactory(argc, argv);
+#endif
+ PFactory::setFactory(pFactory);
+
+ //Parse commandline, and load config
+ CommandLineParser cmdLineParser(BINARY_NAME, argc, argv);
+
Config & config = ConfigManager::getInstance().getCurrentConfig();
- initConfigDir(config);
//FIXME: remove this line
config.fixWenboxConfigHack();
@@ -192,14 +192,6 @@
//Remove WengoPhone Classic from startup registry
ClassicExterminator::removeClassicFromStartup();
- //Loads the configuration: this is the first thing to do before anything else
- ConfigManagerFileStorage configManagerStorage(ConfigManager::getInstance());
- configManagerStorage.loadSystemConfig();
- ConfigImporter importer;
- importer.importConfig();
- configManagerStorage.loadUserConfig();
- ////
-
#ifdef CC_MSVC
MSVCMemoryDump * memoryDump = new MSVCMemoryDump("WengoPhoneNG", WengoPhoneBuildId::getSvnRevision());
memoryDump->setGetAdditionalInfo(getAdditionalInfo);
@@ -211,19 +203,6 @@
#endif
- //Graphical interface implementation
- PFactory * pFactory = NULL;
-#ifdef GTKINTERFACE
- pFactory = new GtkFactory(argc, argv);
-#else
- pFactory = new QtFactory(argc, argv);
-#endif
- PFactory::setFactory(pFactory);
-
- // Init parser after presentation factories so that Qt or Gtk get a chance
- // to parse their command line options ('-style' for Qt for example)
- CommandLineParser cmdLineParser(argc, argv);
-
//No 2 qtwengophone at the same time
if ( ((Processes::isRunning("qtwengophone.exe")) || (Processes::isRunning("qtwengophone.ex"))) &&
!cmdLineParser.isSeveralWengoPhoneAllowed()
_______________________________________________
Wengophone-devel mailing list
[email protected]
http://dev.openwengo.com/mailman/listinfo/wengophone-devel