Persistent settings + autostart + security tab

Change-Id: Iec35f732268caa99ff541e6d5d6478049ec41d50
diff --git a/linux/ndnxcontrolcenter.desktop b/linux/ndnxcontrolcenter.desktop
new file mode 100644
index 0000000..972aed5
--- /dev/null
+++ b/linux/ndnxcontrolcenter.desktop
@@ -0,0 +1,2 @@
+[Desktop Entry]
+Exec=/home/iliamo/Qtprojects/untitled2-build-desktop-Qt_4_8_1_in_PATH__System__Release/untitled2
diff --git a/linux/network-manager.cpp b/linux/network-manager.cpp
index 22e63d2..9950c8c 100644
--- a/linux/network-manager.cpp
+++ b/linux/network-manager.cpp
@@ -16,6 +16,10 @@
         return;
     }
 
+    autoconfigProcess = new QProcess();
+    connect(autoconfigProcess,SIGNAL(finished(int)),this,SLOT(autoconfigFinished()));
+    connect(autoconfigProcess, SIGNAL(error(QProcess::ProcessError)), this, SLOT(autoconfigFinished()));
+
     QDBusConnection::systemBus().connect("org.freedesktop.NetworkManager",
                                           "/org/freedesktop/NetworkManager",
                                           "org.freedesktop.NetworkManager",
@@ -32,6 +36,21 @@
 
 void NetworkManager::autoconfigDaemon()
 {
-    QProcess *process = new QProcess();
-    process->start(NDND_AUTOCONFIG_COMMAND);
+    if(IsAutoconfigRunning())
+        return;
+
+     qDebug() << "started running";
+     isAutoconfigRunning = true;
+     autoconfigProcess->start(NDND_AUTOCONFIG_COMMAND);
+}
+
+void NetworkManager::autoconfigFinished()
+{
+    qDebug() << "stoped running";
+    isAutoconfigRunning = false;
+}
+
+bool NetworkManager::IsAutoconfigRunning()
+{
+    return isAutoconfigRunning;
 }
diff --git a/linux/network-manager.h b/linux/network-manager.h
index 11e5ec7..f0c192d 100644
--- a/linux/network-manager.h
+++ b/linux/network-manager.h
@@ -6,6 +6,7 @@
  */
 
 #include <QtDBus>
+#include <QThread>
 
 #ifndef NETWORKMANAGER_H
 #define NETWORKMANAGER_H
@@ -21,12 +22,18 @@
 
 public:
     NetworkManager();
+    bool IsAutoconfigRunning();
+
+public slots:
+    void autoconfigDaemon();
 
 private:
-    void autoconfigDaemon();
+    QProcess * autoconfigProcess;
+    bool isAutoconfigRunning;
 
 private slots:
     void stateChanged(uint state);
+    void autoconfigFinished();
 };
 
 
diff --git a/linux/tray-menu.cpp b/linux/tray-menu.cpp
index 40eb98e..daf28d5 100644
--- a/linux/tray-menu.cpp
+++ b/linux/tray-menu.cpp
@@ -26,11 +26,20 @@
 #include <QDebug>
 #include <QtXml>
 #include <QStandardItemModel>
+#include <QDir>
 
 TrayMenu::TrayMenu(QWidget *parent) :
     QMainWindow(parent),
     ui(new Ui::TrayMenu)
 {
+    networkManager = new NetworkManager();
+
+    QCoreApplication::setOrganizationName("UCLA");
+    QCoreApplication::setOrganizationDomain("named-data.net");
+    QCoreApplication::setApplicationName("NDNx Control Center");
+
+    persistentSettings = new QSettings();
+
     ui->setupUi(this);
 
     createTrayIcon();
@@ -38,9 +47,7 @@
     createToolbar();
     createTableView();
 
-    startDaemon();
-
-    networkManager = new NetworkManager();
+    loadSettings();
 
     connect(ui->openTrafficMapButton, SIGNAL(pressed()), this, SLOT(openTrafficMap()));
     connect(ui->openRoutingStatusButton, SIGNAL(pressed()), this, SLOT(openRoutingStatus()));
@@ -51,6 +58,7 @@
     connect(ui->loginStartCheckbox, SIGNAL(stateChanged(int)), this, SLOT(changeLoginStart()));
     connect(ui->shutdownCheckbox, SIGNAL(stateChanged(int)), this, SLOT(changeShutdownExit()));
     connect(ui->tableView, SIGNAL(clicked(QModelIndex)), this, SLOT(selectTableRow()));
+    connect(ui->openNdnCertificationButton, SIGNAL(released()), this, SLOT(openCertificationPage()));
 
     statusUpdateThread = new QThread();
     statusUpdateThread->start();
@@ -62,36 +70,136 @@
     trayIcon->show();
 }
 
+void TrayMenu::loadSettings()
+{
+    QVariant value;
+
+    value = persistentSettings->value(ALLOW_SOFTWARE_UPDATES);
+    if(!value.isNull())
+    {
+        allowAutomaticUpdates = value.toBool();
+        ui->softwareUpdateCheckbox->setChecked(allowAutomaticUpdates);
+    }
+    else
+    {
+        ui->softwareUpdateCheckbox->setChecked(true);
+        changeSoftwareUpdate();
+    }
+
+    value = persistentSettings->value(ENABLE_HUB_DISCOVERY);
+    if(!value.isNull())
+    {
+        enableHubDiscovery = value.toBool();
+        ui->hubDiscoveryCheckbox->setChecked(enableHubDiscovery);
+    }
+    else
+    {
+        ui->hubDiscoveryCheckbox->setChecked(true);
+        changeHubDiscovery();
+    }
+
+    value = persistentSettings->value(ENABLE_START_ON_LOGIN);
+    if(!value.isNull())
+    {
+        enableStartOnLogin = value.toBool();
+        ui->loginStartCheckbox->setChecked(enableStartOnLogin);
+    }
+    else
+    {
+        ui->loginStartCheckbox->setChecked(false);
+        changeLoginStart();
+    }
+
+    value = persistentSettings->value(SHUTDOWN_ON_EXIT);
+    if(!value.isNull())
+    {
+        shutdownOnExit = value.toBool();
+        ui->shutdownCheckbox->setChecked(shutdownOnExit);
+    }
+    else
+    {
+        ui->shutdownCheckbox->setChecked(false);
+        changeShutdownExit();
+    }
+}
+
 void TrayMenu::changeSoftwareUpdate()
 {
     if(ui->softwareUpdateCheckbox->isChecked())
+    {
         allowAutomaticUpdates = true;
+        persistentSettings->setValue(ALLOW_SOFTWARE_UPDATES, true);
+    }
     else
+    {
         allowAutomaticUpdates = false;
+        persistentSettings->setValue(ALLOW_SOFTWARE_UPDATES, false);
+    }
 }
 
 void TrayMenu::changeHubDiscovery()
 {
     if(ui->hubDiscoveryCheckbox->isChecked())
+    {
         enableHubDiscovery = true;
+        persistentSettings->setValue(ENABLE_HUB_DISCOVERY, true);
+    }
     else
+    {
         enableHubDiscovery = false;
+        persistentSettings->setValue(ENABLE_HUB_DISCOVERY, false);
+    }
 }
 
 void TrayMenu::changeLoginStart()
 {
     if(ui->loginStartCheckbox->isChecked())
+    {
         enableStartOnLogin = true;
+        persistentSettings->setValue(ENABLE_START_ON_LOGIN, true);
+        makeAutostartDirectory();
+    }
     else
+    {
         enableStartOnLogin = false;
+        persistentSettings->setValue(ENABLE_START_ON_LOGIN, false);
+
+        QProcess *process = new QProcess();
+        QStringList arguments;
+        arguments << QDir::homePath() + AUTOSTART_DIRECTORY + SHORTCUT_FILE;
+        process->start("rm", arguments);
+    }
+}
+
+void TrayMenu::makeAutostartDirectory()
+{
+    QProcess *process = new QProcess();
+    connect(process, SIGNAL(finished(int)), this, SLOT(copyFile()));
+    QStringList arguments;
+    arguments << QDir::homePath() + AUTOSTART_DIRECTORY;
+    process->start("mkdir", arguments);
+}
+
+void TrayMenu::copyFile()
+{
+    QProcess *process = new QProcess();
+    QStringList arguments;
+    arguments << QApplication::applicationDirPath() + "/" + SHORTCUT_FILE << QDir::homePath() + AUTOSTART_DIRECTORY;
+    process->start("cp",arguments);
 }
 
 void TrayMenu::changeShutdownExit()
 {
     if(ui->shutdownCheckbox->isChecked())
+    {
         shutdownOnExit = true;
+        persistentSettings->setValue(SHUTDOWN_ON_EXIT, true);
+    }
     else
+    {
         shutdownOnExit = false;
+        persistentSettings->setValue(SHUTDOWN_ON_EXIT, false);
+    }
 }
 
 void TrayMenu::showFibInputDialog()
@@ -110,6 +218,11 @@
     QDesktopServices::openUrl(QUrl("http://netlab.cs.memphis.edu/script/htm/status.htm", QUrl::TolerantMode));
 }
 
+void TrayMenu::openCertificationPage()
+{
+    QDesktopServices::openUrl(QUrl("http://ndncert.named-data.net", QUrl::TolerantMode));
+}
+
 void TrayMenu::createTrayIcon()
 {
     trayIconMenu = new QMenu(this);
@@ -209,14 +322,19 @@
 
     dialog->hide();
 
-    QProcess *process = new QProcess(this);
-    process->start(NDND_FIB_COMMAND,arguments);
+    QProcess *process = new QProcess();
+    process->start(NDND_FIB_COMMAND, arguments);
 }
 
 void TrayMenu::confirmQuit()
 {
-    QuitDialog dialog(this);
-    dialog.exec();
+    if(shutdownOnExit)
+        terminateDaemonAndClose();
+    else
+    {
+        QuitDialog dialog(this);
+        dialog.exec();
+    }
 }
 
 void TrayMenu::terminateDaemonAndClose()
@@ -256,12 +374,6 @@
         trayIcon->setIcon(QIcon(":/resource/Resources/icon-disconnected-white.png"));
 }
 
-void TrayMenu::startDaemon()
-{
-    QProcess *process = new QProcess(this);
-    process->start(NDND_START_COMMAND);
-}
-
 void TrayMenu::daemonStatusUpdate()
 {
     QXmlQuery query(QXmlQuery::XSLT20);
@@ -270,9 +382,32 @@
     query.setQuery(QUrl("qrc:/resource/Resources/status.xslt")); // TODO: I suspect it's being read from HDD each time
     query.evaluateTo(&statusXml);
 
+    if(statusXml == "") // there was an error during Query evaluation
+    {
+        daemonStarted = false;
+        setIcon(false);
+        statusIndicator->setText(tr("Starting..."));
+
+        QProcess *process = new QProcess();
+        if(enableHubDiscovery)
+            connect(process, SIGNAL(finished(int)), networkManager, SLOT(autoconfigDaemon()));
+        process->start(NDND_START_COMMAND);
+    }
+    else
+    {
+        daemonStarted = true;
+        setIcon(true);
+        statusIndicator->setText(tr("Active"));
+    }
+
     query.setQuery(QUrl("qrc:/resource/Resources/status-to-fib.xslt"));  // TODO: I suspect it's being read from HDD each time
     query.evaluateTo(&fibContentsXml);
 
+    if ((enableHubDiscovery) && (fibContentsXml.indexOf("ndn:/autoconf-route",0,Qt::CaseInsensitive) == -1))
+    {
+        networkManager->autoconfigDaemon();
+    }
+
     QDomDocument xmldoc;
     QDomElement root;
 
@@ -294,12 +429,6 @@
         QDomNode ip =  properties.at(1);
         QDomNode prefix = properties.at(2);
 
-        // bad: causes scroll to go up every time and overall flickering
-        /*QList<QStandardItem*> row = QList<QStandardItem*>() << new QStandardItem(prefix.toElement().text())
-                                                                       << new QStandardItem(faceID.toElement().text())
-                                                                       << new QStandardItem(ip.toElement().text());
-        model->appendRow(row);*/
-
         model->setItem(row, 0, new QStandardItem(prefix.toElement().text()));
         model->setItem(row, 1, new QStandardItem(faceID.toElement().text()));
         model->setItem(row, 2, new QStandardItem(ip.toElement().text()));
@@ -344,8 +473,8 @@
     QStringList arguments;
     arguments << "del" << prefix->text() << "face" << faceID->text();
 
-    QProcess *process = new QProcess(this);
-    process->start(NDND_FIB_COMMAND,arguments);
+    QProcess *process = new QProcess();
+    process->start(NDND_FIB_COMMAND, arguments);
 }
 
 TrayMenu::~TrayMenu()
diff --git a/linux/tray-menu.h b/linux/tray-menu.h
index c5dfd66..a40faca 100644
--- a/linux/tray-menu.h
+++ b/linux/tray-menu.h
@@ -18,6 +18,8 @@
 #include <QXmlStreamReader>
 #include <QStandardItemModel>
 #include <QtXml>
+#include <QThread>
+#include <QSettings>
 
 #include "fib-input-dialog.h"
 #include "quit-dialog.h"
@@ -28,6 +30,14 @@
 #define NDND_STATUS_COMMAND "/usr/local/bin/ndndsmoketest"
 #define NDND_FIB_COMMAND "/usr/local/bin/ndndc"
 
+#define ALLOW_SOFTWARE_UPDATES "AllowAutomaticUpdates"
+#define ENABLE_HUB_DISCOVERY "EnableHubDiscovery"
+#define ENABLE_START_ON_LOGIN "enableStartOnLogin"
+#define SHUTDOWN_ON_EXIT "ShutdownOnExit"
+
+#define AUTOSTART_DIRECTORY "/.config/autostart/"
+#define SHORTCUT_FILE "ndnxcontrolcenter.desktop"
+
 namespace Ui
 {
     class TrayMenu;
@@ -51,7 +61,12 @@
     void closeEvent(QCloseEvent *); // Overriding the window's close event
     void showEvent(QShowEvent * event); //Overriding the window's show event
 
-    void startDaemon();
+    void loadSettings();
+    void makeAutostartDirectory();
+
+    bool daemonStarted;
+
+    QSettings *persistentSettings;
 
     QSystemTrayIcon *trayIcon;
     QMenu *trayIconMenu;
@@ -89,6 +104,7 @@
     void securitySettingsClicked();
     void openTrafficMap();
     void openRoutingStatus();
+    void openCertificationPage();
     void displayPopup();
     void confirmQuit();
     void showFibInputDialog();
@@ -101,6 +117,7 @@
     void changeHubDiscovery();
     void changeLoginStart();
     void changeShutdownExit();
+    void copyFile();
 };
 
 #endif // TRAYMENU_H
diff --git a/linux/traymenu.ui b/linux/traymenu.ui
index 428f343..63f586a 100644
--- a/linux/traymenu.ui
+++ b/linux/traymenu.ui
@@ -23,28 +23,67 @@
    <bool>true</bool>
   </property>
   <widget class="QWidget" name="centralWidget">
+   <property name="enabled">
+    <bool>true</bool>
+   </property>
+   <property name="sizePolicy">
+    <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
+     <horstretch>0</horstretch>
+     <verstretch>0</verstretch>
+    </sizepolicy>
+   </property>
+   <property name="minimumSize">
+    <size>
+     <width>550</width>
+     <height>300</height>
+    </size>
+   </property>
+   <property name="maximumSize">
+    <size>
+     <width>550</width>
+     <height>300</height>
+    </size>
+   </property>
+   <property name="baseSize">
+    <size>
+     <width>0</width>
+     <height>0</height>
+    </size>
+   </property>
    <widget class="QWidget" name="generalSettingsWidget" native="true">
     <property name="geometry">
      <rect>
       <x>10</x>
       <y>50</y>
-      <width>521</width>
-      <height>241</height>
+      <width>520</width>
+      <height>240</height>
      </rect>
     </property>
+    <property name="sizePolicy">
+     <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
+      <horstretch>0</horstretch>
+      <verstretch>0</verstretch>
+     </sizepolicy>
+    </property>
+    <property name="maximumSize">
+     <size>
+      <width>520</width>
+      <height>240</height>
+     </size>
+    </property>
     <widget class="QGroupBox" name="groupBox">
      <property name="geometry">
       <rect>
        <x>10</x>
-       <y>0</y>
-       <width>471</width>
+       <y>10</y>
+       <width>500</width>
        <height>141</height>
       </rect>
      </property>
      <property name="styleSheet">
       <string notr="true">QGroupBox#groupBox { 
-border: 2px solid gray; 
-border-radius: 7px; 
+border: 1px solid gray; 
+border-radius: 5px; 
 }</string>
      </property>
      <property name="title">
@@ -56,7 +95,7 @@
      <widget class="QCheckBox" name="shutdownCheckbox">
       <property name="geometry">
        <rect>
-        <x>0</x>
+        <x>10</x>
         <y>110</y>
         <width>441</width>
         <height>22</height>
@@ -74,7 +113,7 @@
      <widget class="QCheckBox" name="softwareUpdateCheckbox">
       <property name="geometry">
        <rect>
-        <x>0</x>
+        <x>10</x>
         <y>80</y>
         <width>441</width>
         <height>22</height>
@@ -92,7 +131,7 @@
      <widget class="QCheckBox" name="hubDiscoveryCheckbox">
       <property name="geometry">
        <rect>
-        <x>0</x>
+        <x>10</x>
         <y>50</y>
         <width>431</width>
         <height>22</height>
@@ -110,7 +149,7 @@
      <widget class="QCheckBox" name="loginStartCheckbox">
       <property name="geometry">
        <rect>
-        <x>0</x>
+        <x>10</x>
         <y>20</y>
         <width>451</width>
         <height>22</height>
@@ -125,25 +164,20 @@
        <string>Automatically start NDNx Control Center at login</string>
       </property>
      </widget>
-     <zorder>shutdownCheckbox</zorder>
-     <zorder>softwareUpdateCheckbox</zorder>
-     <zorder>hubDiscoveryCheckbox</zorder>
-     <zorder>loginStartCheckbox</zorder>
-     <zorder>forwardingSettingsWidget</zorder>
     </widget>
     <widget class="QGroupBox" name="groupBox_2">
      <property name="geometry">
       <rect>
        <x>10</x>
-       <y>150</y>
-       <width>471</width>
-       <height>81</height>
+       <y>160</y>
+       <width>500</width>
+       <height>75</height>
       </rect>
      </property>
      <property name="styleSheet">
       <string notr="true">QGroupBox#groupBox_2 { 
-border: 2px solid gray; 
-border-radius: 7px; 
+border: 1px solid gray; 
+border-radius: 5px; 
 }</string>
      </property>
      <property name="title">
@@ -182,10 +216,22 @@
      <rect>
       <x>10</x>
       <y>50</y>
-      <width>521</width>
-      <height>251</height>
+      <width>530</width>
+      <height>250</height>
      </rect>
     </property>
+    <property name="sizePolicy">
+     <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
+      <horstretch>0</horstretch>
+      <verstretch>0</verstretch>
+     </sizepolicy>
+    </property>
+    <property name="maximumSize">
+     <size>
+      <width>530</width>
+      <height>250</height>
+     </size>
+    </property>
     <widget class="QLabel" name="label">
      <property name="geometry">
       <rect>
@@ -238,21 +284,60 @@
       <string>-</string>
      </property>
     </widget>
-    <zorder>label</zorder>
-    <zorder>tableView</zorder>
-    <zorder>addFibButton</zorder>
-    <zorder>deleteFibButton</zorder>
-    <zorder>securitySettingsWidget</zorder>
    </widget>
    <widget class="QWidget" name="securitySettingsWidget" native="true">
     <property name="geometry">
      <rect>
       <x>10</x>
       <y>50</y>
-      <width>521</width>
-      <height>241</height>
+      <width>520</width>
+      <height>240</height>
      </rect>
     </property>
+    <property name="sizePolicy">
+     <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
+      <horstretch>0</horstretch>
+      <verstretch>0</verstretch>
+     </sizepolicy>
+    </property>
+    <property name="maximumSize">
+     <size>
+      <width>520</width>
+      <height>240</height>
+     </size>
+    </property>
+    <widget class="QGroupBox" name="groupBox_3">
+     <property name="geometry">
+      <rect>
+       <x>10</x>
+       <y>10</y>
+       <width>500</width>
+       <height>200</height>
+      </rect>
+     </property>
+     <property name="styleSheet">
+      <string notr="true">QGroupBox#groupBox_3 { 
+border: 1px solid gray; 
+border-radius: 5px; 
+}</string>
+     </property>
+     <property name="title">
+      <string/>
+     </property>
+     <widget class="QPushButton" name="openNdnCertificationButton">
+      <property name="geometry">
+       <rect>
+        <x>10</x>
+        <y>20</y>
+        <width>201</width>
+        <height>27</height>
+       </rect>
+      </property>
+      <property name="text">
+       <string>Obtain NDN Certificate</string>
+      </property>
+     </widget>
+    </widget>
    </widget>
   </widget>
   <widget class="QStatusBar" name="statusBar"/>