/*
 *  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 <tqtextstream.h>
#include <tqmap.h>

#include <kstandarddirs.h>
#include <tdelocale.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 TDE 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");
}