+#include <QtGui/QDialog>
+#include <libconfig.h>
+
+#include "options.h"
+
+OptionsDialog::OptionsDialog(QWidget * parent)
+ : QDialog(parent)
+{
+ setupUi(this);
+
+ connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
+ connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
+
+ loadConfig("vlp.cfg");
+}
+
+OptionsDialog::~OptionsDialog()
+{
+}
+
+/**
+ * Adds IP address to the configuration.
+ * Additional window is displayed to add address to the list
+ */
+void OptionsDialog::on_addConnectionButton_clicked()
+{
+ QDialog d(this, Qt::Dialog);
+ QLabel lab("IP Address:", &d);
+ QLineEdit ed("", &d);
+ QPushButton ob("", &d);
+ QPushButton cb("", &d);
+
+ if (connections) {
+ ob.setGeometry(30, 60, 80, 30);
+ ob.setText("Ok");
+ ob.setDefault(TRUE);
+ lab.setGeometry(10, 10, 60, 30);
+ lab.setText("Address");
+ ed.setGeometry(70, 10, 140, 30);
+ cb.setGeometry(130, 60, 80, 30);
+ cb.setText("Cancel");
+ d.resize(240, 100);
+ connect(&ob, SIGNAL(clicked()), &d, SLOT(accept()));
+ connect(&cb, SIGNAL(clicked()), &d, SLOT(reject()));
+ if (d.exec())
+ if (strcmp(ed.text().toAscii().data(), "") != 0) {
+ connections->addItem(ed.text());
+ }
+ }
+}
+
+/**
+ * Deletes current address from available connections.
+ */
+void OptionsDialog::on_delConnectionButton_clicked()
+{
+ if (connections->currentRow() != -1) {
+ connections->takeItem(connections->currentRow());
+ }
+}
+
+/**
+ * Loads configuration from the given file.
+ *
+ * @see QKernel::LoadConfig
+ *
+ * @param fname Filename of the configuration file.
+ */
+void OptionsDialog::loadConfig(char * fname)
+{
+ config_t cfg;
+ config_setting_t *setting;
+ const char *str;
+
+ /* Hack for checking if file exists without using external libs.*/
+ FILE * file = fopen(fname, "rt");
+ if (!file) {
+ fprintf(stderr, "Error: Cannot load configuration file %s!\n", fname);
+ exit(3);
+ }
+ /* File exists, so file has been locked. Release it. */
+
+ config_init(&cfg);
+
+ /* Read the file. If there is an error, report it and exit. */
+ if (!config_read(&cfg, file)) {
+ fprintf(stderr, "%s! In file %s, line %d\n",
+ config_error_text(&cfg),
+ config_error_file(&cfg),
+ config_error_line(&cfg));
+ config_destroy(&cfg);
+ fclose(file);
+ exit(3);
+ }
+
+ setting = config_lookup(&cfg, "node_number");
+ if (setting) {
+ nodeNumber->setValue(config_setting_get_int(setting));
+ } else {
+ fprintf(stderr, "%s! In file %s, '%s' was not found.\n",
+ "Warning", fname, "node_number");
+ config_destroy(&cfg);
+ fclose(file);
+ exit(3);
+ }
+
+ setting = config_lookup(&cfg, "type");
+ if (setting) {
+ /* same as strcmp(..) == 0 */
+ if (!strcmp(config_setting_get_string(setting), "explicit")) {
+ explicitConnectionMode->setEnabled(true);
+ registrationConnectionMode->setDisabled(true);
+ } else {
+ registrationConnectionMode->setEnabled(true);
+ explicitConnectionMode->setDisabled(true);
+ }
+ } else {
+ fprintf(stderr, "%s! In file %s, '%s' was not found.\n", "Warning", fname, "type");
+ }
+
+ setting = config_lookup(&cfg, "host");
+ if (setting) {
+ switch(config_setting_type(setting)) {
+ /* TODO: Deprecated. Made for back compatibility. */
+ case CONFIG_TYPE_STRING:
+ connections->addItem(config_setting_get_string(setting));
+ break;
+ case CONFIG_TYPE_ARRAY: {
+ int size = config_setting_length(setting);
+ for (int i = 0; i < size; i++) {
+ connections->addItem(config_setting_get_string_elem(setting, i));
+ }
+ break;
+ }
+ default:
+ fprintf(stderr, "%s! In file %s, bad entry type for %s."
+ " Will not be read.\n",
+ "Error", fname, "host");
+ }
+ } else {
+ fprintf(stderr, "%s! In file %s, '%s' was not found.\n",
+ "Warning", fname, "host");
+ }
+
+ setting = config_lookup(&cfg, "progdir");
+ if (setting){
+ programsDirectory->setText(config_setting_get_string(setting));
+ } else {
+ fprintf(stderr, "%s! In file %s, '%s' was not found.\n", "Warning", fname, "progdir");
+ }
+
+ setting = config_lookup(&cfg, "homedir");
+ if (setting) {
+ homeDir = config_setting_get_string(setting);
+ } else {
+ fprintf(stderr, "%s! In file %s, '%s' was not found.\n", "Warning", fname, "homedir");
+ }
+
+ config_destroy(&cfg);
+ fclose(file);
+}
+
+void OptionsDialog::saveConfig(char * fname)
+{
+ config_t cfg;
+ config_setting_t *root;
+ config_setting_t *setting;
+ config_init(&cfg);
+
+ root = config_root_setting(&cfg);
+
+ setting = config_setting_add(root, "progdir", CONFIG_TYPE_STRING);
+ config_setting_set_string(setting, programsDirectory->text().toStdString().c_str());
+
+ setting = config_setting_add(root, "node_number", CONFIG_TYPE_INT);
+ config_setting_set_int(setting, nodeNumber->value());
+
+ setting = config_setting_add(root, "homedir", CONFIG_TYPE_STRING);
+ config_setting_set_string(setting, homeDir);
+
+ setting = config_setting_add(root, "type", CONFIG_TYPE_STRING);
+ if (explicitConnectionMode->isChecked()) {
+ config_setting_set_string(setting, "explicit");
+
+ config_setting_t *hosts = NULL;
+ hosts = config_setting_add(root, "host", CONFIG_TYPE_ARRAY);
+ for(int i = 0; i < connections->count(); i++) {
+ setting = config_setting_add(hosts, NULL, CONFIG_TYPE_STRING);
+ config_setting_set_string(setting, connections->item(i)->text().toStdString().c_str());
+ }
+ } else {
+ config_setting_set_string(setting, "register");
+ }
+
+ if (!config_write_file(&cfg, fname)) {
+ fprintf(stderr, "Error while writing to file: %s.\n", fname);
+ }
+ config_destroy(&cfg);
+}