summaryrefslogtreecommitdiff
path: root/src/common/fqterm_config.cpp
blob: c9fa26c35032f8db5c9fd61cbef39a8531ddb457 (plain)
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
// SPDX-License-Identifier: GPL-2.0-or-later

#include <QTextStream>
#include <QFile>
#include <QString>
#include <QTextCodec>

#include "fqterm_trace.h"
#include "fqterm_config.h"

namespace FQTerm {

FQTermConfig::FQTermConfig(const QString &szFileName) {
  load(szFileName);
}

FQTermConfig::~FQTermConfig(){

}

bool FQTermConfig::save(const QString &szFileName) {
  QFile file(szFileName);
  if (!file.open(QIODevice::WriteOnly)) {
    FQ_TRACE("config", 0) << "Failed to open the file for writing: "
                          << szFileName;
    return false;
  }

  QTextStream os;
  os.setDevice(&file);
  saveToStream(os);
  // 	os.unsetDevice();
  file.close();


  return true;
}

bool FQTermConfig::loadFromStream(QTextStream &is) {
  QString strLine, strSection;

  data.clear();

  is.setCodec(QTextCodec::codecForName("UTF-8"));

  while (!is.atEnd()) {
    strLine = is.readLine().trimmed();
    if (strLine.isEmpty() || strLine[0] == '#') {
      continue;
    }

    if (strLine.left(1) == "[" && strLine.right(1) == "]") {
      strSection = strLine.mid(1, strLine.length() - 2);
      addSection(strSection);
    } else {
      QString strValue = strLine.section('=', 1).trimmed();
      setItemValue(strSection, strLine.section('=', 0, 0).trimmed(),
                   strValue.isNull() ? QString(""): strValue);
    }
  }
  return true;
}

bool FQTermConfig::saveToStream(QTextStream &os) {
  QString strLine, strSection;
  Section::iterator iStr;

  os.setCodec(QTextCodec::codecForName("UTF-8"));

  for (StrSecMap::iterator iSec = data.begin(); iSec != data.end(); ++iSec) {
    os << '[' << iSec.key() << "]\n";
    for (iStr = iSec.value().begin(); iStr != iSec.value().end(); ++iStr) {
      os << iStr.key() << '=' << iStr.value() << '\n';
    }
    os << '\n';
  }
  return true;
}


bool FQTermConfig::addSection(const QString &szSection) {
  if (hasSection(szSection)) {
    return false;
  }
  Section sec;
  data[szSection] = sec;
  return true;
}

bool FQTermConfig::hasSection(const QString &szSection) const 
{
  return data.find(szSection) != data.end();
}

bool FQTermConfig::setItemValue(const QString &szSection, const QString
                                &szItemName, const QString &szItemValue) {
  if (!hasSection(szSection))
    if (!addSection(szSection)) {
      return false;
	}

  data[szSection][szItemName] = szItemValue;
  return true;
}

QString FQTermConfig::getItemValue(const QString &szSection,
                                   const QString &szItemName) const {
  if (hasSection(szSection))
    if (data[szSection].find(szItemName) != data[szSection].end())
      if (!data[szSection][szItemName].isEmpty()) {
        return data[szSection][szItemName];
      }
  return "";
}

bool FQTermConfig::renameSection(const QString &szSection, const QString
                                 &szNewName) {
  if (hasSection(szNewName) || !hasSection(szSection)) {
    return false;
  }

  if (!addSection(szNewName)) {
    return false;
  }
  data[szNewName] = data[szSection];

  return deleteSection(szSection);
}

bool FQTermConfig::deleteSection(const QString &szSection) {
  if (hasSection(szSection)) {
    data.remove(szSection);
    return true;
  }
  return false;
}

bool FQTermConfig::deleteItem(const QString &szSection, const QString
                              &szItemName) {
  if (hasSection(szSection))
	if (data[szSection].find(szItemName) != data[szSection].end()) {
      data[szSection].remove(szItemName);
      return true;
	}
  return false;
}

bool FQTermConfig::load(const QString &filename) {
  QFile file(filename);
  if (!file.open(QIODevice::ReadOnly)) {
    FQ_TRACE("config", 0) << "Failed to open the file for reading "
                          << filename;
    return false;
  }
  QTextStream is;
  is.setDevice(&file);
  loadFromStream(is);
  //is.unsetDevice();
  file.close();
  return true;
}

}  // namespace FQTerm