File: confoption.cpp

package info (click to toggle)
kde-config-systemd 1.2.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 1,560 kB
  • ctags: 688
  • sloc: cpp: 3,306; makefile: 9; sh: 2
file content (616 lines) | stat: -rw-r--r-- 19,117 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/*******************************************************************************
 * Copyright (C) 2013-2015 Ragnar Thomsen <rthomsen6@gmail.com>                *
 *                                                                             *
 * 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 2 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 "confoption.h"
#include "fsutil.h"

#include <QDebug>

// Initialize two static class members
QStringList confOption::capabilities = QStringList() << "CAP_AUDIT_CONTROL" << "CAP_AUDIT_WRITE" << "CAP_BLOCK_SUSPEND" << "CAP_CHOWN"
                                           << "CAP_DAC_OVERRIDE" << "CAP_DAC_READ_SEARCH" << "CAP_FOWNER" << "CAP_FSETID" 
                                           << "CAP_IPC_LOCK" << "CAP_IPC_OWNER" << "CAP_KILL" << "CAP_LEASE" << "CAP_LINUX_IMMUTABLE"
                                           << "CAP_MAC_ADMIN" << "CAP_MAC_OVERRIDE" << "CAP_MKNOD" << "CAP_NET_ADMIN" << "CAP_NET_BIND_SERVICE"
                                           << "CAP_NET_BROADCAST" << "CAP_NET_RAW" << "CAP_SETGID" << "CAP_SETFCAP" << "CAP_SETPCAP"
                                           << "CAP_SETUID" << "CAP_SYS_ADMIN" << "CAP_SYS_BOOT" << "CAP_SYS_CHROOT" << "CAP_SYS_MODULE"
                                           << "CAP_SYS_NICE" << "CAP_SYS_PACCT" << "CAP_SYS_PTRACE" << "CAP_SYS_RAWIO" << "CAP_SYS_RESOURCE"
                                           << "CAP_SYS_TIME" << "CAP_SYS_TTY_CONFIG" << "CAP_SYSLOG" << "CAP_WAKE_ALARM";

bool confOption::operator==(const confOption& other) const
{
  // Required for indexOf()
  if (uniqueName == other.uniqueName)
    return true;
  else
    return false;
}

confOption::confOption()
{
}

confOption::confOption(QString newName)
{
  // Create an instance necessary for indexOf() or ==
  uniqueName = newName;
}

confOption::confOption(QVariantMap map)
{
  file = static_cast<confFile>(map["file"].toInt());
  realName = map["name"].toString();
  uniqueName = QString(map["name"].toString() + '_' + QString::number(file));
  type = static_cast<settingType>(map["type"].toInt());
  defVal = map["defVal"];
  possibleVals = map["possibleVals"].toStringList();
  if (map.contains("defUnit"))
    defUnit = static_cast<timeUnit>(map["defUnit"].toInt());
  if (map.contains("defReadUnit"))
    defReadUnit = static_cast<timeUnit>(map["defReadUnit"].toInt());
  // These two are not used for anything currently
  // minUnit = static_cast<timeUnit>(map["minUnit"].toInt());
  // maxUnit = static_cast<timeUnit>(map["maxUnit"].toInt());
  if (map.contains("minVal"))
    minVal = map["minVal"].toLongLong();
  if (map.contains("maxVal"))
    maxVal = map["maxVal"].toLongLong();
  toolTip = map["toolTip"].toString();

  hasNsec = map["hasNsec"].toBool();

  if (type == MULTILIST)
  {
    // Create a map where all possibleVals are set to false.
    QVariantMap defMap;
    foreach (const QString &s, possibleVals) {
      defMap[s] = false;
    }
    defVal = defMap;
  }
  if (type == RESLIMIT)
    defVal = -1;

  value = defVal;
}

int confOption::setValue(QVariant newVal)
{
  qDebug() << "Setting " << realName << " to " << newVal;
  value = newVal;
  return 0;
}

int confOption::setValueFromFile(QString line)
{
  // Used to set values in confOptions from a file line
  
  QString rval = line.section('=',1).trimmed();

  qDebug() << "setting " << realName << " to " << rval << " (from file)";
    
  if (type == BOOL)
  {
    if (rval == "true" || rval == "on" || rval == "yes")
    {
      value = true;
      return 0;
    }
    else if (rval == "false" || rval == "off" || rval == "no")
    {
      value = false;
      return 0;
    }
    qDebug() << rval << "is not a valid value for setting" << realName << ". Ignoring...";
    return -1;
  }

  else if (type == INTEGER)
  {
    bool ok;
    qlonglong rvalToNmbr = rval.toLongLong(&ok);
    if (ok && rvalToNmbr >= minVal && rvalToNmbr <= maxVal)
    {
      value = rvalToNmbr;
      return 0;
    }
    qDebug() << rval << "is not a valid value for setting" << realName << ". Ignoring...";
    return -1;
  }

  else if (type == STRING)
  {
    value = rval;
    return 0;
  }  
  
  else if (type == LIST)
  {
    if (realName == "ShowStatus") // ShowStatus needs special treatment
    {
      if (rval.toLower() == "true" || rval.toLower() == "on")
        rval = "yes";
      else if (rval.toLower() == "false" || rval.toLower() == "off")
        rval = "no";
    }
    if (possibleVals.contains(rval))
    {
      value = rval.toLower();
      return 0;
    }
    qDebug() << rval << "is not a valid value for setting" << realName << ". Ignoring...";
    value = defVal;
    return -1;
  }

  else if (type == MULTILIST)
  {
    QVariantMap map;

    QStringList readList = rval.split(' ', QString::SkipEmptyParts);
    for (int i = 0; i < readList.size(); ++i)
    {
      if (!possibleVals.contains(readList.at(i)))
      {
        qDebug() << rval << "is not a valid value for setting" << realName << ". Ignoring...";
        return -1;
      }
    }
    
    for (int i = 0; i < possibleVals.size(); ++i)
    {
      if (readList.contains(possibleVals.at(i)))
        map[possibleVals.at(i)] = true;
      else
        map[possibleVals.at(i)] = false;
    }

    value = map;
    return 0;

  }  
  
  else if (type == TIME)
  {
    int pos = 0;
    QRegExp rxValid;

    // These regex check whether rval is a valid time interval
    if (hasNsec)
      rxValid = QRegExp("^(?:\\d*\\.?\\d *(ns|nsec|us|usec|ms|msec|s|sec|second|seconds|m|min|minute|minutes|h|hr|hour|hours|d|day|days|w|week|weeks|month|months|y|year|years)? *)+$");
    else
      rxValid = QRegExp("^(?:\\d*\\.?\\d *(us|usec|ms|msec|s|sec|second|seconds|m|min|minute|minutes|h|hr|hour|hours|d|day|days|w|week|weeks|month|months|y|year|years)? *)+$");

    pos = rxValid.indexIn(rval);

    if (pos > -1)
    {
      pos = 0;
      seconds secs(0);

      // This regex parses individual elements of the time interval
      QRegExp rxTimeParse = QRegExp("(\\d*\\.?\\d+) *([a-z]*)");

      while ((pos = rxTimeParse.indexIn(rval, pos)) != -1)
      {
        if (rxTimeParse.cap(2) == "ns" ||
            rxTimeParse.cap(2) == "nsec" )
        {
          nanoseconds ns(rxTimeParse.cap(1).trimmed().toDouble());
          secs += ns;
        }
        else if (rxTimeParse.cap(2) == "us" ||
            rxTimeParse.cap(2) == "usec" )
        {
          microseconds us(rxTimeParse.cap(1).trimmed().toDouble());
          secs += us;
        }
        else if (rxTimeParse.cap(2) == "ms" ||
            rxTimeParse.cap(2) == "msec" )
        {
          milliseconds ms(rxTimeParse.cap(1).trimmed().toDouble());
          secs += ms;
        }
        else if (rxTimeParse.cap(2) == "s" ||
                 rxTimeParse.cap(2) == "sec" ||
                 rxTimeParse.cap(2) == "second" ||
                 rxTimeParse.cap(2) == "seconds" )
        {
          seconds s(rxTimeParse.cap(1).trimmed().toDouble());
          secs += s;
        }
        else if (rxTimeParse.cap(2) == "m" ||
                 rxTimeParse.cap(2) == "min" ||
                 rxTimeParse.cap(2) == "minute" ||
                 rxTimeParse.cap(2) == "minutes" )
        {
          minutes min(rxTimeParse.cap(1).trimmed().toDouble());
          secs += min;
        }
        else if (rxTimeParse.cap(2) == "h" ||
                 rxTimeParse.cap(2) == "hr" ||
                 rxTimeParse.cap(2) == "hour" ||
                 rxTimeParse.cap(2) == "hours" )
        {
          hours hr(rxTimeParse.cap(1).trimmed().toDouble());
          secs += hr;
        }
        else if (rxTimeParse.cap(2) == "d" ||
                 rxTimeParse.cap(2) == "day" ||
                 rxTimeParse.cap(2) == "days")
        {
          days dy(rxTimeParse.cap(1).trimmed().toDouble());
          secs += dy;
        }
        else if (rxTimeParse.cap(2) == "w" ||
                 rxTimeParse.cap(2) == "week" ||
                 rxTimeParse.cap(2) == "weeks")
        {
          weeks w(rxTimeParse.cap(1).trimmed().toDouble());
          secs += w;
        }
        else if (rxTimeParse.cap(2) == "month" ||
                 rxTimeParse.cap(2) == "months")
        {
          months m(rxTimeParse.cap(1).trimmed().toDouble());
          secs += m;
        }
        else if (rxTimeParse.cap(2) == "y" ||
                 rxTimeParse.cap(2) == "year" ||
                 rxTimeParse.cap(2) == "years")
        {
          years y(rxTimeParse.cap(1).trimmed().toDouble());
          secs += y;
        }

        else if (rxTimeParse.cap(2).isEmpty())
        {
          // unitless number, convert it from defReadUnit to seconds
          seconds tmpSeconds(convertTimeUnit(rxTimeParse.cap(1).trimmed().toDouble(), defReadUnit, timeUnit::s).toDouble());
          secs += tmpSeconds;
        }

        pos += rxTimeParse.matchedLength();
      }

      // Convert the read value in seconds to defUnit
      if (defUnit == ns)
        value = nanoseconds(secs).count();
      else if (defUnit == us)
        value = microseconds(secs).count();
      else if (defUnit == ms)
        value = milliseconds(secs).count();
      else if (defUnit == s)
        value = secs.count();
      else if (defUnit == min)
        value = minutes(secs).count();
      else if (defUnit == h)
        value = hours(secs).count();
      else if (defUnit == d)
        value = days(secs).count();
      else if (defUnit == w)
        value = weeks(secs).count();
      else if (defUnit == month)
        value = months(secs).count();
      else if (defUnit == year)
        value = years(secs).count();

      value = value.toULongLong(); // Convert to ulonglong (we don't support float in ui)
      return 0;

    }
    else
    {
      qDebug() << rval << "is not a valid value for setting" << realName << ". Ignoring...";
      return -1;
    }
  }
  
  else if (type == RESLIMIT)
  {
    bool ok;
    int nmbr = rval.toUInt(&ok);
    if (ok)
    {
      value = nmbr;
      return 0;
    }
    else if (rval.toLower().trimmed() == "infinity" || rval.trimmed().isEmpty())
    {
      value = -1;
      return 0;
    }
    qDebug() << rval << "is not a valid value for setting" << realName << ". Ignoring...";
    return -1;
    
  }
  
  else if (type == SIZE)
  {
    // RegExp to match a number (possibly with decimals) followed by a
    // size unit (or no unit for byte), or a % (for RuntimeDirectorySize)
    QRegExp rxSize;
    if (realName == "RuntimeDirectorySize")
      rxSize = QRegExp("^(\\d+\\.?\\d*)(K|M|G|T|P|E|%)?$");
    else
      rxSize = QRegExp("^(\\d+\\.?\\d*)(K|M|G|T|P|E)?$");

    int pos = 0;
    pos = rxSize.indexIn(rval);

    if (pos > -1 && rxSize.cap(0) == rval.trimmed())
    {
      // convert the specified size unit to megabytes
      if (rxSize.cap(2) == "K")
        value = rxSize.cap(1).toDouble() / 1024;
      else if (rxSize.cap(2) == "M")
        value = rxSize.cap(1).toDouble();
      else if (rxSize.cap(2) == "G")
        value = rxSize.cap(1).toDouble() * 1024;
      else if (rxSize.cap(2) == "T")
        value = rxSize.cap(1).toDouble() * 1024 * 1024;
      else if (rxSize.cap(2) == "P")
        value = rxSize.cap(1).toDouble() * 1024 * 1024 * 1024;
      else if (rxSize.cap(2) == "E")
        value = rxSize.cap(1).toDouble() * 1024 * 1024 * 1024 * 1024;
      else
        value = rxSize.cap(1).toDouble() / 1024 / 1024;
      
      if (realName == "RuntimeDirectorySize")
      {
        // This option supports both size units and %
        if (rxSize.cap(2) == "%") {
          value = rxSize.cap(1).toInt();

        } else {

          bool ok = false;
          qulonglong sizeMB = getPartitionSize("/run", &ok) / 1024 / 1024;
          if (!ok) {
            qDebug() << rval << "cannot be interpreted correctly with an unknown partition size. Ignoring...";
            return -1;
          }

          // Convert the size to percentage of physical RAM. The volatile
          // partition size should be half the physical RAM.
          value = 50 * value.toDouble() / sizeMB;
        }
      }

      // Convert from double to ulonglong (we don't support float in ui)
      value = value.toULongLong();
      return 0;
    }
    else
    {
      qDebug() << rval << "is not a valid value for setting" << realName << ". Ignoring...";
      return -1;
    }
   
  }
  return -1;
}

bool confOption::isDefault() const
{
  if (value == defVal)
    return true;
  else
    return false;
}

void confOption::setToDefault()
{
  value = defVal;
}

QVariant confOption::getValue() const
{
  return value;
}

QString confOption::getValueAsString() const
{
  // Used when fetching data for the QTableView
  if (type == MULTILIST)
  {
    QVariantMap map = value.toMap();
    QString mapAsString;
    for(QVariantMap::const_iterator iter = map.constBegin(); iter != map.constEnd(); ++iter)
    {
      if (iter.value() == true && mapAsString.isEmpty())
        mapAsString = QString(iter.key());
      else if (iter.value() == true)
        mapAsString = QString(mapAsString + ' ' + iter.key());
    }
    return mapAsString;
  }
  return value.toString();
}

QString confOption::getLineForFile() const
{
  // Used for saving to conf files

  if (value == defVal)
  {
    // value is set to default
    return QString('#' + realName + "=\n");
  }
  else
  {
    // value is not default
    if (type == BOOL)
    {
      if (value.toBool())
        return QString(realName + '=' + "yes\n");
      else
        return QString(realName + '=' + "no\n");
    }

    else if (type == MULTILIST)
    {
      if (!value.toMap().isEmpty())
      {
        QVariantMap map = value.toMap();
        QString ret;
        for(QVariantMap::const_iterator iter = map.constBegin(); iter != map.constEnd(); ++iter)
        {
          if (iter.value().toBool())
            ret = QString(ret + iter.key() + ' ');
        }
        return QString(realName + '=' + ret.trimmed() + '\n');
      }
    }

    else if (type == TIME)
    {
      if (value.toULongLong() == 0)
        return QString(realName + '=' + value.toString() + '\n');
      else
      {
        if (defUnit == ns)
          return QString(realName + '=' + value.toString() + "ns\n");
        else if (defUnit == us)
          return QString(realName + '=' + value.toString() + "us\n");
        else if (defUnit == ms)
          return QString(realName + '=' + value.toString() + "ms\n");
        else if (defUnit == s)
          return QString(realName + '=' + value.toString() + "s\n");
        else if (defUnit == min)
          return QString(realName + '=' + value.toString() + "min\n");
        else if (defUnit == h)
          return QString(realName + '=' + value.toString() + "h\n");
        else if (defUnit == d)
          return QString(realName + '=' + value.toString() + "d\n");
        else if (defUnit == w)
          return QString(realName + '=' + value.toString() + "w\n");
        else if (defUnit == month)
          return QString(realName + '=' + value.toString() + "month\n");
        else if (defUnit == year)
          return QString(realName + '=' + value.toString() + "year\n");
       }
    }

    else if (type == SIZE)
    {
      return QString(realName + '=' + value.toString() + "M\n");
    }

    return QString(realName + '=' + value.toString() + '\n');
  } // not default
}

QString confOption::getFilename() const
{
  if (file == SYSTEMD)
    return QString("system.conf");
  else if (file == JOURNALD)
    return QString("journald.conf");
  else if (file == LOGIND)
    return QString("logind.conf");
  else if (file == COREDUMP)
    return QString("coredump.conf");
  return QString("");
}

QString confOption::getTimeUnit() const
{
  QStringList timeUnitAsString = QStringList() << "ns" << "us" << "ms" << "s" <<
                                          "min" << "h" << "days" << "weeks" <<
                                          "month" << "year";
  return timeUnitAsString.at(defUnit);
}

QVariant confOption::convertTimeUnit(double value, timeUnit oldTime, timeUnit newTime)
{
  QVariant convertedVal;
  seconds tmpSecs(0);
  
  if (oldTime == ns)
  {
    nanoseconds val(value);
    tmpSecs = chrono::duration_cast<seconds>(val);
  }
  else if (oldTime == us)
  {
    microseconds val(value);
    tmpSecs = chrono::duration_cast<seconds>(val);
  }
  else if (oldTime == ms)
  {
    milliseconds val(value);
    tmpSecs = chrono::duration_cast<seconds>(val);
  }
  else if (oldTime == s)
  {
    seconds val(value);
    tmpSecs = chrono::duration_cast<seconds>(val);
  }
  else if (oldTime == min)
  {
    minutes val(value);
    tmpSecs = chrono::duration_cast<seconds>(val);
  }
  else if (oldTime == h)
  {
    hours val(value);
    tmpSecs = chrono::duration_cast<seconds>(val);
  }
  else if (oldTime == d)
  {
    days val(value);
    tmpSecs = chrono::duration_cast<seconds>(val);
  }
  else if (oldTime == w)
  {
    weeks val(value);
    tmpSecs = chrono::duration_cast<seconds>(val);
  }
  else if (oldTime == month)
  {
    months val(value);
    tmpSecs = chrono::duration_cast<seconds>(val);
  }
  else if (oldTime == year)
  {
    years val(value);
    tmpSecs = chrono::duration_cast<seconds>(val);
  }
    
  if (newTime == ns)
    convertedVal = chrono::duration_cast<nanoseconds>(tmpSecs).count();
  else if (newTime == us)
    convertedVal = chrono::duration_cast<microseconds>(tmpSecs).count();
  else if (newTime == ms)
    convertedVal = chrono::duration_cast<milliseconds>(tmpSecs).count();
  else if (newTime == s)
    convertedVal = chrono::duration_cast<seconds>(tmpSecs).count();
  else if (newTime == min)
    convertedVal = chrono::duration_cast<minutes>(tmpSecs).count();
  else if (newTime == h)
    convertedVal = chrono::duration_cast<hours>(tmpSecs).count();
  else if (newTime == d)
    convertedVal = chrono::duration_cast<days>(tmpSecs).count();
  else if (newTime == w)
    convertedVal = chrono::duration_cast<weeks>(tmpSecs).count();
  else if (newTime == month)
    convertedVal = chrono::duration_cast<months>(tmpSecs).count();
  else if (newTime == year)
    convertedVal = chrono::duration_cast<years>(tmpSecs).count();

  return convertedVal;
}