File: AbstractConfWriter.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 (184 lines) | stat: -rw-r--r-- 6,069 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
/*
 * $Id: AbstractConfWriter.cpp 110 2011-10-22 12:02:21Z werner $
 *
 * File:   AbstractConfWriter.cpp
 * Author: Werner Jaeger
 *
 * Created on July 23, 2010, 8:43 AM
 *
 * 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 <QResource>
#include <QDateTime>
#include <QFile>
#include <QFileInfo>
#include <QDir>
#include <QTextStream>
#include <QCoreApplication>
#include "AbstractConfWriter.h"
#include "VPNControlTask.h"
#include "pkcs11/SmartCardState.h"

static const char* const FILENAME = "FILENAME";
static const char* const CREATIONDATE = "CREATIONDATE";
static const char* const APPNAME = "APPNAME";
static const char* const APPVERSION = "APPVERSION";
static const char* const APPPFILEPATH = "APPPFILEPATH";

AbstractConfWriter::AbstractConfWriter(const QString& strTemplateKey, const QString& strWriteTo, FileType type) :
   m_strTemplateKey(strTemplateKey), m_strWriteTo(strWriteTo), m_Type(type),
   m_strInstance(QString("")), m_strFileName(strWriteTo), m_pDictionary(NULL), m_fTemplatesInitialized(false)
{
}

AbstractConfWriter::~AbstractConfWriter()
{
   if (m_pDictionary)
      delete m_pDictionary;
}

const QString& AbstractConfWriter::write()
{
   readTemplate(m_strTemplateKey);

   if (m_strLastErrorMsg.isEmpty())
   {
      newDictionary();
      fill();
      save();
   }

   return(m_strLastErrorMsg);
}

const QString& AbstractConfWriter::instance() const
{
   return(m_strInstance);
}

void AbstractConfWriter::setInstance(const QString& strInstance)
{
   m_strInstance = strInstance.isNull() ? "" : strInstance;

   const QFileInfo fileInfo(m_strWriteTo);
   m_strFileName = m_strInstance.trimmed().isEmpty() ? m_strWriteTo : fileInfo.path() + "/" + m_strInstance + (fileInfo.fileName().isEmpty() ? "" : "." + fileInfo.fileName());
}

const QString& AbstractConfWriter::fileName() const
{
   return(m_strFileName);
}

void AbstractConfWriter::addErrorMsg(const QString& strErrorMsg)
{
   m_strLastErrorMsg.append(strErrorMsg + '\n');
}

void AbstractConfWriter::save()
{
   dictionary()->SetValue(FILENAME, fileName().toAscii().constData());
   dictionary()->SetValue(CREATIONDATE, QDateTime::currentDateTime().toString().toAscii().constData());
   dictionary()->SetValue(APPNAME, QCoreApplication::instance()->applicationName().toAscii().constData());
   dictionary()->SetValue(APPVERSION, QCoreApplication::instance()->applicationVersion().toAscii().constData());
   dictionary()->SetValue(APPPFILEPATH, QCoreApplication::instance()->applicationFilePath().toAscii().constData());

   std::string strOut;
   ctemplate::Template* const pTpl = ctemplate::Template::GetTemplate(templateKey().toAscii().constData(), ctemplate::DO_NOT_STRIP);

   if (pTpl)
   {
      if (pTpl->Expand(&strOut, dictionary()))
      {
   //   ctemplate::ExpandTemplate(key().toAscii().constData(), ctemplate::DO_NOT_STRIP, dictionary(), &strOut);

         QFile outFile(fileName());
         QDir outFileDir(QFileInfo(outFile).absoluteDir());

         bool fOk(true);
         if (!outFileDir.exists())
         {
            const QString strDirName(outFileDir.dirName());
            outFileDir.cdUp();
            fOk = outFileDir.mkpath(strDirName);
         }

         if (fOk)
         {
//            if (outFile.exists())
//               fOk = outFile.copy(fileName() + "." + QDateTime::currentDateTime().toString("yyyyMMddhhmmss").toAscii().constData() + ".~");

            if (fOk)
            {
               if (outFile.open(QIODevice::WriteOnly | QIODevice::Text))
               {
                  switch (m_Type)
                  {
                     case EXECUTABLE:
                        outFile.setPermissions(outFile.permissions() | QFile::ExeUser | QFile::ExeGroup | QFile::ExeOther);
                        break;

                     case SECRET:
                        outFile.setPermissions(QFile::ReadOwner | QFile::WriteOwner);
                        break;

                     default:
                        ;
                  }

                  QTextStream out(&outFile);
                  out << strOut.data();
                  outFile.close();
               }
               else
                  addErrorMsg(QObject::tr("Failed to open configuration file '%1'.").arg(outFile.fileName()));
            }
            else
               addErrorMsg(QObject::tr("Failed to backup file '%1'.").arg(outFile.fileName()));
         }
         else
            addErrorMsg(QObject::tr("Failed to create directory '%1'.").arg(outFileDir.absolutePath()));
      }
      else
         addErrorMsg(QObject::tr("Failed to expand template '%1'.").arg(templateKey()));
   }
   else
      addErrorMsg(QObject::tr("Failed to get template '%1'.").arg(templateKey()));
}

void AbstractConfWriter::readTemplate(const QString& strKey)
{
   if (!m_fTemplatesInitialized)
   {
      const QResource rsc(":/templates/" + strKey + ".tpl");

      if (rsc.isValid())
      {
         ctemplate::Template::StringToTemplateCache(m_strTemplateKey.toStdString(), QString(reinterpret_cast<const char*>(rsc.data())).toStdString());
         m_fTemplatesInitialized = true;
      }
      else
         addErrorMsg(QObject::tr("Failed to open template file ':/templates/%1.tpl'.").arg(strKey));
   }
}

void AbstractConfWriter::newDictionary()
{
   if (m_pDictionary)
      delete m_pDictionary;

   m_pDictionary = new ctemplate::TemplateDictionary(m_strTemplateKey.toAscii().constData());
}