File: ConnectionEditorDialog.cpp

package info (click to toggle)
l2tp-ipsec-vpn 1.0.9-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,396 kB
  • sloc: cpp: 9,952; xml: 400; makefile: 139; sh: 78
file content (273 lines) | stat: -rw-r--r-- 9,747 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
 * $Id: ConnectionEditorDialog.cpp 120 2012-03-08 04:57:41Z werner $
 *
 * File:   ConnectionEditorDialog.cpp
 * Author: Werner Jaeger
 *
 * Created on July 18, 2010, 1:09 PM
 *
 * Copyright 2010 Werner Jaeger.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <QInputDialog>
#include <QMessageBox>
#include <QFile>

#include "conf/ConfWriter.h"
#include "models/ConnectionsModel.h"
#include "settings/ConnectionSettings.h"
#include "settings/Preferences.h"
#include "util/GlobalFunctions.h"
#include "ConnectionSettingsDialog.h"
#include "ConnectionEditorDialog.h"
#include "PreferencesEditorDialog.h"

ConnectionEditorDialog::ConnectionEditorDialog(QWidget* pParent) : QDialog(pParent), m_pConnectionsModel(new ConnectionsModel()), m_pConnectionSettings(new ConnectionSettings())
{
   m_Widget.setupUi(this);

   m_Widget.m_pConnections->setModel(m_pConnectionsModel);
   m_Widget.m_pConnections->horizontalHeader()->setStretchLastSection(true);
   m_Widget.m_pConnections->setSelectionBehavior(QAbstractItemView::SelectRows);

   connect(m_Widget.m_pPreferencesCommandLinkButton, SIGNAL(clicked()), this, SLOT(editPreferences()));
   connect(m_Widget.m_pAdd, SIGNAL(clicked()), SLOT(addConnection()));
   connect(m_Widget.m_pEdit, SIGNAL(clicked()), SLOT(editConnection()));
   connect(m_Widget.m_pDelete, SIGNAL(clicked()), SLOT(removeConnection()));
   connect(m_Widget.m_pButtonBox, SIGNAL(helpRequested()), SLOT(onHelpRequested()));

   if (m_pConnectionsModel->rowCount() > 0)
      m_Widget.m_pConnections->setCurrentIndex(m_pConnectionsModel->index(0, 0));
   else
      enableDeleteAndEdit(false);

   m_Widget.m_pConnections->setFocus();

   m_pConnectionSettings->clearChanged();
}

ConnectionEditorDialog::~ConnectionEditorDialog()
{
   delete m_pConnectionsModel;
   delete m_pConnectionSettings;
}

void ConnectionEditorDialog::onHelpRequested() const
{
   ::showHelp("Installing_L2TP_over_IPsec_VPN_Manager");
}

void ConnectionEditorDialog::accept()
{
   if (m_pConnectionSettings->hasChanged())
   {
      applySettings(m_pConnectionsModel, m_pConnectionSettings, windowTitle(), m_Widget.m_pConnections);
      m_pConnectionSettings->clearChanged();
   }

   QDialog::accept();
}

void ConnectionEditorDialog::reject()
{
   if (m_pConnectionSettings->hasChanged())
   {
      applySettings(m_pConnectionsModel, m_pConnectionSettings, windowTitle(), m_Widget.m_pConnections);
      m_pConnectionSettings->clearChanged();
   }

   QDialog::reject();
}

bool ConnectionEditorDialog::applySettings(ConnectionsModel* pConnectionsModel, ConnectionSettings* pConnectionSettings, const QString strMsgBoxTitle, QWidget* pFocus)
{
   const OpenSSLSettings openSSLSettings(Preferences().openSSLSettings());
   const int iConnections(pConnectionSettings->connections());

   bool fRet(true);

   if (iConnections > 0)
   {
      if (pConnectionsModel->isWriteable())
      {
         if (pFocus)
            QMessageBox::information(NULL, strMsgBoxTitle, tr("You need to reconnect for your changes to take effect!"));

         fRet = ConfWriter::write(ConfWriter::IPsec);
         if (fRet) fRet = ConfWriter::write(ConfWriter::IPsecSECRET);
         if (fRet) fRet = ConfWriter::write(ConfWriter::L2TP);

         for (int i = 0; fRet && i < iConnections; i++)
         {
            const QString strConnectionName(pConnectionSettings->connection(i));

            fRet = ConfWriter::write(ConfWriter::PPP, strConnectionName);
            if (fRet)
            {
               const QString strDNSConfInstance(QCoreApplication::instance()->objectName() + "-" +strConnectionName);
               const QString strDNSConfFile(ConfWriter::fileName(ConfWriter::PPPDNSCONF, strDNSConfInstance));
               const PppIpSettings ipSettings(pConnectionSettings->pppSettings(strConnectionName).ipSettings());

               if (ipSettings.usePeerDns() || (ipSettings.alternateDnsServerAddress().isEmpty() && ipSettings.preferredDnsServerAddress().isEmpty() && ipSettings.searchDomains().isEmpty()))
               {
                  if (QFile::exists(strDNSConfFile))
                     QFile::remove(strDNSConfFile);
               }
               else
                  fRet = ConfWriter::write(ConfWriter::PPPDNSCONF, strDNSConfInstance);
            }
         }

         if (fRet) fRet = ConfWriter::write(ConfWriter::PPPUPSCRIPT);
         if (fRet) fRet = ConfWriter::write(ConfWriter::PPPDOWNSCRIPT);
         if (fRet) fRet = ConfWriter::write(ConfWriter::GETIPSECINFO);
         if (fRet) fRet = ConfWriter::write(ConfWriter::RSYSLOG);
         if (fRet)
         {
            if (openSSLSettings.enginePath().isEmpty() || openSSLSettings.pkcs11Path().isEmpty() || openSSLSettings.engineId().isEmpty())
            {
               const QString strOpenSSLConfFile(ConfWriter::fileName(ConfWriter::OPENSSL));
               if (QFile::exists(strOpenSSLConfFile))
                  QFile::remove(strOpenSSLConfFile);
            }
            else
               fRet = ConfWriter::write(ConfWriter::OPENSSL);
         }
      }
      else
      {
         if (pFocus)
            QMessageBox::critical(NULL, tr("Apply settings"), tr("You do not have the permission to apply settings"));

         fRet = false;
      }
   }

   if (pFocus)
      pFocus->setFocus();

    return(fRet);
}

void ConnectionEditorDialog::editPreferences()
{
   PreferencesEditorDialog preferences;
   preferences.exec();
   m_Widget.m_pConnections->setFocus();
}

void ConnectionEditorDialog::addConnection()
{
   if (m_pConnectionsModel->isWriteable())
   {
      const QString strTitle(tr("New Connection"));

      QString strName("");
      bool fOk = true;
      ConnectionsModel::Result result(ConnectionsModel::InvalidName);
      while (fOk && result != ConnectionsModel::Ok)
      {
         strName = QInputDialog::getText(this, strTitle, tr("Connection name:"), QLineEdit::Normal, strName, &fOk);

         if (fOk)
         {
            const int iRow = m_pConnectionsModel->rowCount();
            result = m_pConnectionsModel->addRow(strName);
            switch (result)
            {
               case ConnectionsModel::Ok:
                  m_Widget.m_pConnections->setCurrentIndex(m_pConnectionsModel->index(iRow, 0));
                  enableDeleteAndEdit(true);
                  emit connectionAdded(strName);
                  break;

               case ConnectionsModel::DuplicateName:
                  QMessageBox::critical(this, strTitle, tr("A connection with name '%1' already exists").arg(strName));
                  break;

               case ConnectionsModel::InvalidName:
                  QMessageBox::critical(this, strTitle, tr("The provided name '%1' contains invalid characters.\n\nName must start with a letter and may contain only letters, digits, periods, underscores, and hyphens.\nMaximum length is 30.").arg(strName));
                  break;

               default:
                  break;
            }
         }
      }
   }
   else
      QMessageBox::critical(this, tr("New Connection"), tr("You do not have the permission to add a connection"));

   m_Widget.m_pConnections->setFocus();
}

void ConnectionEditorDialog::editConnection()
{
   if (m_pConnectionsModel->isWriteable())
   {
      const QModelIndex index = m_Widget.m_pConnections->currentIndex();

      if (index.row() >= 0)
      {
         const QString strName = m_pConnectionsModel->data(index, Qt::DisplayRole).toString();

         ConnectionSettingsDialog settings(strName);
         settings.exec();
      }
   }
   else
      QMessageBox::critical(this, tr("Edit Connection"), tr("You do not have the permission to edit a connection"));

   m_Widget.m_pConnections->setFocus();
}

void ConnectionEditorDialog::removeConnection()
{
   if (m_pConnectionsModel->isWriteable())
   {
      const QModelIndex index = m_Widget.m_pConnections->currentIndex();

      if (index.row() >= 0)
      {
         const QString strName = m_pConnectionsModel->data(index, Qt::DisplayRole).toString();

         if (QMessageBox::question(this, tr("Delete Connection"), tr("Are you sure you wish to delete the connection '%1'?").arg(strName), QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::Yes)
         {
            if (m_pConnectionsModel->removeRow(index.row()))
            {
               const int iRows =  m_pConnectionsModel->rowCount();
               if (index.row() < iRows)
                  m_Widget.m_pConnections->setCurrentIndex(m_pConnectionsModel->index(index.row(), 0));
               else if (iRows > 0)
                  m_Widget.m_pConnections->setCurrentIndex(m_pConnectionsModel->index(index.row() - 1, 0));

               enableDeleteAndEdit(iRows > 0);
               emit connectionRemoved(strName);
            }
         }
      }
   }
   else
      QMessageBox::critical(this, tr("Delete Connection"), tr("You do not have the permission to remmove a connection"));

   m_Widget.m_pConnections->setFocus();
}

void ConnectionEditorDialog::enableDeleteAndEdit(bool fEnable)
{
   m_Widget.m_pDelete->setEnabled(fEnable);
   m_Widget.m_pEdit->setEnabled(fEnable);
}