summaryrefslogtreecommitdiffstats
path: root/kdeprint/cups/kmwquota.cpp
blob: 67245fb2014d5dadcdcdb37902cd54bd0873377d (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
164
165
166
167
168
169
170
171
/*
 *  This file is part of the KDE libraries
 *  Copyright (c) 2001 Michael Goffioul <tdeprint@swing.be>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License version 2 as published by the Free Software Foundation.
 *
 *  This library 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
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 **/

#include <config.h>

#include "kmwquota.h"
#include "kmwizard.h"
#include "kmprinter.h"

#include <tqspinbox.h>
#include <tqlabel.h>
#include <tqcombobox.h>
#include <tqlayout.h>
#include <klocale.h>

#ifdef HAVE_LIMITS_H
#include <limits.h>
#endif

#define	N_TIME_LIMITS	6
static int time_periods[] = {
	1,		// second
	60,		// minute
	3600,		// hour
	86400,		// day
	604800,		// week
	2592000		// month (30 days)
};
static const char* time_keywords[] = {
	I18N_NOOP("second(s)"),
	I18N_NOOP("minute(s)"),
	I18N_NOOP("hour(s)"),
	I18N_NOOP("day(s)"),
	I18N_NOOP("week(s)"),
	I18N_NOOP("month(s)")
};

int findUnit(int& period)
{
	int	unit(0);
	for (int i=N_TIME_LIMITS-1;i>=0;i--)
	{
		if (period < time_periods[i])
			continue;
		int	d = period / time_periods[i];
		if ((d*time_periods[i]) == period)
		{
			unit = i;
			break;
		}
	}
	period /= time_periods[unit];
	return unit;
}

const char* unitKeyword(int i)
{ return time_keywords[i]; }

KMWQuota::KMWQuota(TQWidget *parent, const char *name)
: KMWizardPage(parent, name)
{
	m_ID = KMWizard::Custom+3;
	m_title = i18n("Printer Quota Settings");
	m_nextpage = KMWizard::Custom+4;

	m_period = new TQSpinBox(this);
	m_period->setRange(-1, INT_MAX);
	m_period->setSpecialValueText(i18n("No quota"));
	m_sizelimit = new TQSpinBox(this);
	m_sizelimit->setRange(0, INT_MAX);
	m_sizelimit->setSpecialValueText(i18n("None"));
	m_pagelimit = new TQSpinBox(this);
	m_pagelimit->setRange(0, INT_MAX);
	m_pagelimit->setSpecialValueText(i18n("None"));
	m_timeunit = new TQComboBox(this);
	for (int i=0;i<N_TIME_LIMITS;i++)
		m_timeunit->insertItem(i18n(time_keywords[i]));
	m_timeunit->setCurrentItem(3);

	TQLabel	*lab1 = new TQLabel(i18n("&Period:"), this);
	TQLabel	*lab2 = new TQLabel(i18n("&Size limit (KB):"), this);
	TQLabel	*lab3 = new TQLabel(i18n("&Page limit:"), this);

	lab1->setBuddy(m_period);
	lab2->setBuddy(m_sizelimit);
	lab3->setBuddy(m_pagelimit);

	TQLabel	*lab4 = new TQLabel(i18n("<p>Set here the quota for this printer. Using limits of <b>0</b> means "
					"that no quota will be used. This is equivalent to set quota period to "
					"<b><nobr>No quota</nobr></b> (-1). Quota limits are defined on a per-user base and "
					"applied to all users.</p>"), this);

	TQGridLayout	*l0 = new TQGridLayout(this, 5, 3, 0, 10);
	l0->setRowStretch(4, 1);
	l0->setColStretch(1, 1);
	l0->addMultiCellWidget(lab4, 0, 0, 0, 2);
	l0->addWidget(lab1, 1, 0);
	l0->addWidget(lab2, 2, 0);
	l0->addWidget(lab3, 3, 0);
	l0->addWidget(m_period, 1, 1);
	l0->addWidget(m_timeunit, 1, 2);
	l0->addMultiCellWidget(m_sizelimit, 2, 2, 1, 2);
	l0->addMultiCellWidget(m_pagelimit, 3, 3, 1, 2);
}

KMWQuota::~KMWQuota()
{
}

bool KMWQuota::isValid(TQString& msg)
{
	if (m_period->value() >= 0 && m_sizelimit->value() == 0 && m_pagelimit->value() == 0)
	{
		msg = i18n("You must specify at least one quota limit.");
		return false;
	}
	return true;
}

void KMWQuota::initPrinter(KMPrinter *p)
{
	int	qu(-1), si(0), pa(0), un(3);
	qu = p->option("job-quota-period").toInt();
	si = p->option("job-k-limit").toInt();
	pa = p->option("job-page-limit").toInt();
	if (si == 0 && pa == 0)
		// no quota
		qu = -1;
	m_sizelimit->setValue(si);
	m_pagelimit->setValue(pa);
	if (qu > 0)
	{
		un = findUnit(qu);
	}
	m_timeunit->setCurrentItem(un);
	m_period->setValue(qu);
}

void KMWQuota::updatePrinter(KMPrinter *p)
{
	int	qu(m_period->value()), si(m_sizelimit->value()), pa(m_pagelimit->value());
	if (qu == -1)
	{
		// no quota, set limits to 0
		si = 0;
		pa = 0;
		qu = 0;
	}
	qu *= time_periods[m_timeunit->currentItem()];

	p->setOption("job-quota-period", TQString::number(qu));
	p->setOption("job-k-limit", TQString::number(si));
	p->setOption("job-page-limit", TQString::number(pa));
}
#include "kmwquota.moc"