summaryrefslogtreecommitdiffstats
path: root/tdeprint/rlpr/kmrlprmanager.cpp
blob: 553d9be13bcc4c98a9d281fb8236bc7d06176cd1 (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
/*
 *  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 "kmrlprmanager.h"
#include "kmprinter.h"

#include <tqfile.h>
#include <tqfileinfo.h>
#include <textstream.h>
#include <tqmap.h>

#include <kstandarddirs.h>
#include <klocale.h>

KMRlprManager::KMRlprManager(TQObject *parent, const char *name, const TQStringList & /*args*/)
: KMManager(parent,name)
{
	setHasManagement(true);
	setPrinterOperationMask(KMManager::PrinterCreation|KMManager::PrinterRemoval|KMManager::PrinterTesting);
}

KMRlprManager::~KMRlprManager()
{
}

bool KMRlprManager::createPrinter(KMPrinter *p)
{
	if (p->name().isEmpty())
		setErrorMsg(i18n("Empty printer name."));
	else if (p->option("host").isEmpty())
		setErrorMsg(i18n("Empty host name."));
	else if (p->option("queue").isEmpty())
		setErrorMsg(i18n("Empty queue name."));
	else
	{
		KMPrinter	*pr = new KMPrinter(*p);
		addPrinter(pr);
		savePrinters();
		return true;
	}
	return false;
}

bool KMRlprManager::removePrinter(KMPrinter *p)
{
	if (m_printers.findRef(p) == -1)
		setErrorMsg(i18n("Printer not found."));
	else
	{
		m_printers.removeRef(p);
		savePrinters();
		return true;
	}
	return false;
}

bool KMRlprManager::testPrinter(KMPrinter *)
{
	setErrorMsg(i18n("Not implemented yet."));
	return false;
}

void KMRlprManager::listPrinters()
{
	TQFileInfo	pfi(printerFile());
	if (pfi.exists() && (!m_checktime.isValid() || m_checktime < pfi.lastModified()))
	{
		loadPrintersConf(pfi.absFilePath());
		m_checktime = pfi.lastModified();
	}
	else
		discardAllPrinters(false);
}

void KMRlprManager::loadPrintersConf(const TQString& filename)
{
	TQFile	f(filename);
	if (f.exists() && f.open(IO_ReadOnly))
	{
		TQTextStream	t(&f);
		TQString		line;
		while (!t.eof())
		{
			line = t.readLine().stripWhiteSpace();
			if (line.isEmpty() || line[0] == '#')
				continue;
			TQStringList	w = TQStringList::split('\t',line,true);
			if (w.count() < 3)
				continue;

			KMPrinter	*printer = new KMPrinter;
			printer->setName(w[0]);
			printer->setPrinterName(w[0]);
			printer->setType(KMPrinter::Printer);
			printer->setOption("host",w[1]);
			printer->setOption("queue",w[2]);
			if (w.count() > 3)
			{
				printer->setDescription(w[3]);
				if (w.count() > 4) printer->setLocation(w[4]);
			}
			printer->setState(KMPrinter::Idle);
			printer->setDevice(TQString::fromLatin1("lpd://%1/%2").arg(w[1]).arg(w[2]));

			addPrinter(printer);
		}
	}
}

void KMRlprManager::savePrinters()
{
	savePrintersConf(printerFile());
}

void KMRlprManager::savePrintersConf(const TQString& filename)
{
	TQFile	f(filename);
	if (f.open(IO_WriteOnly))
	{
		TQTextStream	t(&f);
		t << "# File generated by KDE print system. Don't edit by hand." << endl;
		TQPtrListIterator<KMPrinter>	it(m_printers);
		for (;it.current();++it)
		{
			if (!it.current()->name().isEmpty() && it.current()->instanceName().isEmpty())
			{
				TQString	host = it.current()->option("host");
				TQString	queue = it.current()->option("queue");
				if (!host.isEmpty() && !queue.isEmpty())
				{
					t << it.current()->name() << '\t' << host << '\t' << queue;
					t << '\t' << it.current()->description() << '\t' << it.current()->location() << endl;
				}
			}
		}
	}
}

TQString KMRlprManager::printerFile()
{
	return locateLocal("data","tdeprint/printers.conf");
}