Fix warnings related to const char* -> char* conversion
[vlp.git] / src / kernel / kernel.cpp
index 8bd0311c246cbdd23b259fb30e221964dae41570..4db3eeeecd5950c3600cfc1c6fa3f7ae5389af18 100644 (file)
 #include <X11/Xlib.h>
 
 #include "kernel.h"
-#include "kernelwindow.h"
+#include "ui/kernelwindow.h"
 #include "lock.h"
 #include "options.h"
+#include "kill_interpreter_dialog.h"
+#include "connect_dialog.h"
+
+#include "vlp/config.h"
 
 /* File resides in top directory (where are Makefiles)*/
 #include "../../config.h"
@@ -137,7 +141,7 @@ QKernel::QKernel(int argc, char **argv)
                q.mkpath(getRemoteDir());
        }
 
-       info_messages = TRUE;
+       info_messages = actionInfo_messages->isChecked();
        wait_for_info = FALSE;
 
        setWindowTitle(PACKAGE_NAME);
@@ -148,11 +152,15 @@ QKernel::QKernel(int argc, char **argv)
        freeINTid = 1;
        ActiveConnections = 0;
        strcpy(LockPasswd, "");
+
        loadConfig(getConfigFilePath());
+
        RunNetModule();
 
        Net_Notify = new QSocketNotifier(net_sock, QSocketNotifier::Read, this);
        connect(Net_Notify, SIGNAL(activated(int)), this, SLOT(NetMessage()));
+
+       WriteMessage("\n " PACKAGE_STRING ": READY \n");
 }
 
 QString QKernel::getConfigFilePath()
@@ -190,94 +198,19 @@ void QKernel::loadConfig(const QString & fname)
  */
 void QKernel::loadConfig(const char * fname)
 {
-       fprintf(stderr, "2: %s\n", fname);
-       config_t cfg;
-       config_setting_t *setting;
-
-       /* 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);
-       }
+       loglan::vlp::Config config;
+       if(config.load(fname)) {
+               NodeNumber = config.getNodeNumber();
+               ConType = config.getConnectionType();
 
-       setting = config_lookup(&cfg, "node_number");
-       if (setting) {
-               NodeNumber = 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")) {
-                       ConType = 1;
-               } else {
-                       ConType = 2;
-               }
-       } 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:
-                       ConnectList.append(new ConnectEntry((char*)
-                                       config_setting_get_string(setting)));
-                       break;
-               case CONFIG_TYPE_ARRAY: {
-                       int size = config_setting_length(setting);
-                       for (int i = 0; i < size; i++) {
-                               ConnectList.append(new ConnectEntry((char*)
-                                       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");
+               std::vector<std::string> hosts = config.getHosts();
+               for (int i = 0; i < hosts.size(); i++) {
+                       ConnectList.append(new ConnectEntry(hosts[i].c_str()));
                }
-       } else {
-               fprintf(stderr, "%s! In file %s, '%s' was not found.\n", 
-                                               "Warning", fname, "host");
-       }
 
-       setting = config_lookup(&cfg, "progdir");
-       if (setting){
-               strncpy(progdir, config_setting_get_string(setting), 256);
-       } else {
-               fprintf(stderr, "%s! In file %s, '%s' was not found.\n", 
-                                               "Warning", fname, "progdir");
+               strncpy(progdir, config.getProgramDir(), 256);
        }
-
-       config_destroy(&cfg);
-       fclose(file);
 }
 
 /**
@@ -295,6 +228,7 @@ void QKernel::on_actionExecute_triggered()
                if (i > 0)
                        s.remove(i, 4);
 
+               // @TODO: if no interpreter is running will result in killing app
                RunIntModule((char*)s.toAscii().data(), 0);
        }
 }
@@ -321,6 +255,13 @@ void QKernel::on_actionEditor_triggered()
                        WriteMessage("Executing logedit failed!");
                }
        }
+       else if (pid < 0) {
+               WriteMessage("fork(logedit) failed!");
+               WriteMessage("Exiting...");
+               sleep(2);
+               on_actionQuit_triggered();
+               exit(3);
+       }
 }
 
 /**
@@ -349,6 +290,13 @@ void QKernel::on_actionHelp_triggered()
                        WriteMessage("Executing loghelp failed!");
                }
        }
+       else if (pid < 0) {
+               WriteMessage("fork(loghelp) failed!");
+               WriteMessage("Exiting...");
+               sleep(2);
+               on_actionQuit_triggered();
+               exit(3);
+       }
 }
 
 /**
@@ -360,6 +308,7 @@ void QKernel::RunGraphModule(char *sk)
 
        QString program = getHomeDir();
        program += "/modules/loggraph";
+
        pid_t pid = fork();
        if (pid == 0) {
                if (execl(program.toStdString().c_str(),
@@ -460,30 +409,16 @@ void QKernel::RunNetModule()
  */
 void QKernel::on_actionConnect_triggered()
 {
-       QDialog d(this, Qt::Dialog);
-       QLabel lab("IP Address:", &d);
-       QLineEdit ed("", &d);
-       QPushButton ob("", &d);
-       QPushButton cb("", &d);
        MESSAGE m;
 
-       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()) {
+       ConnectDialog dialog(this);
+       dialog.setWindowTitle("IP Address:");
+
+       if (dialog.exec()) {
                m.msg_type = MSG_NET;
                m.param.pword[0] = NET_CONNECT_TO;
-               strcpy(m.param.pstr, ed.text().toAscii().data());
-               write(net_sock, &m, sizeof(MESSAGE)); 
+               strcpy(m.param.pstr, dialog.getAddress().toStdString().c_str());
+               write(net_sock, &m, sizeof(MESSAGE));
        }
 }
 
@@ -592,41 +527,21 @@ void QKernel::on_actionMessage_triggered()
  */
 void QKernel::on_actionKill_triggered()
 {
-       QDialog *dlg;
-       QLineEdit *nodenr;
-       MESSAGE m;
-       InterpEntry *pom;
-
-       dlg = new QDialog(this, Qt::Dialog);
-       dlg->setWindowTitle("Kill interpreter");
-
-       nodenr = new QLineEdit("", dlg); 
-       nodenr->setGeometry(90, 10, 50, 30);
-
-       QLabel * tmpQLabel = new QLabel("Interp. ID:", dlg);
-       tmpQLabel->setGeometry(10, 10, 77, 30);
-
-       QPushButton * ob = new QPushButton("Kill", dlg);
-       ob->setGeometry( 160, 10, 80, 30);
-       ob->setDefault(TRUE);
+       KillInterpreterDialog dialog(this);
+       dialog.setWindowTitle("Kill interpreter");
 
-       QPushButton * cb = new QPushButton("Cancel", dlg);
-       cb->setGeometry(260, 10, 80, 30);
-       dlg->resize(360, 50);
-
-       connect(ob, SIGNAL(clicked()), dlg, SLOT(accept()));
-       connect(cb, SIGNAL(clicked()), dlg, SLOT(reject())); 
+       MESSAGE m;
+       InterpEntry *interpreter;
 
-       if (dlg->exec()) {
+       if (dialog.exec()) {
                m.msg_type = MSG_INT;
                m.param.pword[0] = INT_KILL;
-               pom = findINTbyID(atoi(nodenr->text().toAscii().data()));
-               if (pom != NULL) {
-                       if (!(pom->remote))
-                               write(pom->sock, &m, sizeof(MESSAGE));
+               interpreter = findINTbyID(dialog.getInterpreterId());
+               if (interpreter != NULL) {
+                       if (!(interpreter->remote))
+                               write(interpreter->sock, &m, sizeof(MESSAGE));
                        else
-                               WriteMessage("This is a remote instance of "
-                                                               "a program!");
+                               WriteMessage("This is a remote instance of a program!");
                }
                else {
                        WriteMessage("Interpreter not found");
@@ -819,7 +734,7 @@ void QKernel::IntMessage(int sock)
  * Writes message to kernel logger.
  * @parame msg String with message to log
  */
-void QKernel::WriteMessage(char *msg)
+void QKernel::WriteMessage(const char *msg)
 {
        int x;
        int y;
@@ -852,9 +767,8 @@ void QKernel::WriteMessage(char *msg)
 void QKernel::on_actionInfo_messages_triggered()
 {
        if (toolsMenu != NULL) {
-               info_messages = !actionInfo_messages->isChecked();
+               info_messages = actionInfo_messages->isChecked();
                actionInfo_messages->setChecked(info_messages);
-               fprintf(stderr, "Info is checked? %s\n", info_messages ? "yes" : "no");
        }
 }
 
@@ -885,7 +799,7 @@ void QKernel::on_actionLock_console_triggered()
        if (lockDialog.exec()) {
                QString password = lockDialog.getPassword();
                if (lockDialog.getPassword().size() > 0) {
-                       strcpy(LockPasswd, password.toAscii().data());
+                       strcpy(LockPasswd, password.toStdString().c_str());
                        lockDialog.retype();
 
                        if (lockDialog.exec()) {
@@ -914,29 +828,11 @@ void QKernel::on_actionLock_console_triggered()
  */
 void QKernel::on_actionUnlock_console_triggered()
 {
-       QDialog d(this, Qt::Dialog);
-       d.setWindowTitle("Enter password");
-
-       QLabel lab("Password:", &d);
-       lab.setGeometry(10, 10, 60, 30);
-
-       QLineEdit ed("", &d);
-       ed.setGeometry(70, 10, 140, 30);
-       ed.setEchoMode(QLineEdit::Password);
-
-       QPushButton ob("Ok", &d);
-       ob.setGeometry(30, 60, 80, 30);
-       ob.setDefault(TRUE);
-       connect(&ob, SIGNAL(clicked()), &d, SLOT(accept()));
-
-       QPushButton cb("Cancel", &d);
-       cb.setGeometry(130, 60, 80, 30);
-       connect(&cb, SIGNAL(clicked()), &d, SLOT(reject()));
-
-       d.resize(240, 100);
+       LockDialog lockDialog(this);
 
-       if (d.exec()) {
-               if (strcmp(ed.text().toAscii().data(), LockPasswd) == 0) {
+       if (lockDialog.exec()) {
+               QString password = lockDialog.getPassword();
+               if (strcmp(password.toStdString().c_str(), LockPasswd) == 0) {
                        setLocked(FALSE);
                        WriteMessage("CONSOLE UNLOCKED");
                } else {
@@ -948,14 +844,6 @@ void QKernel::on_actionUnlock_console_triggered()
        }
 }
 
-/**
- * Writes init message in kernel
- */
-void QKernel::InitMessage()
-{
-       WriteMessage("\n " PACKAGE_STRING ": READY \n");
-}
-
 /**
  * Finds Interpreter by its socket
  * @param _id ID of the socket
@@ -1235,7 +1123,6 @@ int main(int argc, char **argv)
        QApplication * app = new QApplication(argc, argv);
        QKernel kernel(argc, argv);
        kernel.show();
-       kernel.InitMessage();
        
        return app->exec();
 }