File: validity.cpp

package info (click to toggle)
xca 2.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,328 kB
  • sloc: cpp: 30,584; sh: 341; xml: 74; makefile: 56; python: 34
file content (149 lines) | stat: -rw-r--r-- 2,618 bytes parent folder | download | duplicates (2)
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
/* vi: set sw=4 ts=4:
 *
 * Copyright (C) 2003 - 2012 Christian Hohnstaedt.
 *
 * All rights reserved.
 */

#include "validity.h"

#include <QDateTime>
#include "lib/asn1time.h"
#include "lib/func.h"


Validity::Validity(QWidget *parent) : QDateTimeEdit(parent)
{
	setTimeSpec(Qt::UTC);
	setNow();
	hideTime(false);
	connect(this, SIGNAL(timeChanged(const QTime &)),
			this, SLOT(setMyTime(const QTime &)));
	updateFormatString();
}

Validity::~Validity()
{
}

a1time Validity::getDate() const
{
	a1time date(dateTime());
	QTime time;

	if (midnight) {
		time = endDate ? QTime(23,59,59) : QTime(0,0,0);
		date.setTimeSpec(Qt::UTC);
	} else {
		time = date.time();
		time.setHMS(time.hour(), time.minute(), 0);
	}
	date.setTime(time);
	return date;
}

void Validity::localTime(int state)
{
	if (midnight)
		return;
	switch (state) {
	case Qt::Checked:
		setTimeSpec(Qt::LocalTime);
		setDateTime(dateTime().toLocalTime());
		break;
	case Qt::Unchecked:
		setTimeSpec(Qt::UTC);
		setDateTime(dateTime().toUTC());
		break;
	}
	updateFormatString();
}

void Validity::hideTimeCheck(int state)
{
	switch (state) {
	case Qt::Checked:
		hideTime(true);
		break;
	case Qt::Unchecked:
		hideTime(false);
		break;
	}
}

void Validity::hideTime(bool hide)
{
	if (hide) {
		if (!midnight && endDate)
			setDateTime(dateTime().addDays(-1));
		midnight = true;
	} else {
		if (midnight && endDate)
			setDateTime(dateTime().addDays(1));
		midnight = false;
		setTime(mytime);
	}
	updateFormatString();
}

void Validity::updateFormatString()
{
	QString formatDate = tr("yyyy-MM-dd hh:mm");
	QString format;

	if (midnight) {
		if (!endDate)
			format = QTime(0,0,0).toString(formatDate);
		else
			format = QTime(23,59,59).toString(formatDate);
	} else {
		format = formatDate;
	}
	if (timeSpec() == Qt::UTC || midnight) {
		format += " 'GMT'";
	} else {
		format += QString(" '%1'")
			.arg(QTime::currentTime().toString("t"));
	}
	setDisplayFormat(format);
}

void Validity::setDate(const a1time &a)
{
	setDateTime(a);
	setMyTime(a.time());
}

void Validity::setDiff(const Validity *start, int number, int range)
{
	QDateTime dt = start->dateTime();

	switch (range) {
		case 0: dt = dt.addDays(number); break;
		case 1: dt = dt.addMonths(number); break;
		case 2: dt = dt.addYears(number); break;
	}

	// one day less if we go from 0:00:00 to 23:59:59
	if (midnight)
		dt = dt.addDays(-1);

	setDateTime(dt);
	setMyTime(start->mytime);
}

void Validity::setNow()
{
	setDate(a1time());
}

void Validity::setMyTime(const QTime &time)
{
	mytime = time;
}

void Validity::setEndDate(bool ed)
{
	endDate = ed;
	hideTime(midnight);
}