summaryrefslogtreecommitdiffstats
path: root/kdeprint/management/kmdriverdb.cpp
blob: adb7fb807dbb02fc27b5321cc0de28fb72f788a4 (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
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
/*
 *  This file is part of the KDE libraries
 *  Copyright (c) 2001 Michael Goffioul <kdeprint@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 "kmdriverdb.h"
#include "kmdbentry.h"
#include "kmdbcreator.h"
#include "kmmanager.h"
#include "kmfactory.h"
#include <kdebug.h>

#include <tqfile.h>
#include <tqtextstream.h>
#include <tqfileinfo.h>
#include <kstandarddirs.h>
#include <kapplication.h>
#include <kmessagebox.h>

KMDriverDB* KMDriverDB::m_self = 0;

KMDriverDB* KMDriverDB::self()
{
	if (!m_self)
	{
		m_self = new KMDriverDB();
		Q_CHECK_PTR(m_self);
	}
	return m_self;
}

KMDriverDB::KMDriverDB(TQObject *parent, const char *name)
: TQObject(parent,name)
{
	m_creator = new KMDBCreator(this,"db-creator");
	connect(m_creator,TQT_SIGNAL(dbCreated()),TQT_SLOT(slotDbCreated()));

	m_entries.setAutoDelete(true);
	m_pnpentries.setAutoDelete(true);
}

KMDriverDB::~KMDriverDB()
{
}

TQString KMDriverDB::dbFile()
{
	// this calls insure missing directories creation
	QString	filename = locateLocal("data",TQString::tqfromLatin1("kdeprint/printerdb_%1.txt").arg(KMFactory::self()->printSystem()));
	return filename;
}

void KMDriverDB::init(TQWidget *parent)
{
	QFileInfo	dbfi(dbFile());
	QString		dirname = KMFactory::self()->manager()->driverDirectory();
	QStringList	dbDirs = TQStringList::split(':', dirname, false);
	bool	createflag(false);

	for (TQStringList::ConstIterator it=dbDirs.begin(); it!=dbDirs.end() && !createflag; ++it)
		if (!(*it).startsWith("module:") && !m_creator->checkDriverDB(*it, dbfi.lastModified()))
			createflag = true;

	if (createflag)
	{
		// starts DB creation and wait for creator signal
		if (!m_creator->createDriverDB(dirname,dbfi.absFilePath(),parent))
			KMessageBox::error(parent, KMFactory::self()->manager()->errorMsg().prepend("<qt>").append("</qt>"));
	}
	else if (m_entries.count() == 0)
	{
		// call directly the slot as the DB won't be re-created
		// this will (re)load the driver DB
		slotDbCreated();
	}
	else
		// no need to refresh, and already loaded, just emit signal
		emit dbLoaded(false);
}

void KMDriverDB::slotDbCreated()
{
	// DB should be created, check creator status
	if (m_creator->status())
	{
		// OK, load DB and emit signal
		loadDbFile();
		emit dbLoaded(true);
	}
	else
		// error while creating DB, notify the DB widget
		emit error(KMManager::self()->errorMsg());
	// be sure to emit this signal to notify the DB widget
	//emit dbLoaded(true);
}

KMDBEntryList* KMDriverDB::findEntry(const TQString& manu, const TQString& model)
{
	TQDict<KMDBEntryList>	*models = m_entries.tqfind(manu);
	if (models)
		return models->tqfind(model);
	return 0;
}

KMDBEntryList* KMDriverDB::tqfindPnpEntry(const TQString& manu, const TQString& model)
{
	TQDict<KMDBEntryList>	*models = m_pnpentries.tqfind(manu);
	if (models)
		return models->tqfind(model);
	return 0;
}

TQDict<KMDBEntryList>* KMDriverDB::tqfindModels(const TQString& manu)
{
	return m_entries.tqfind(manu);
}

void KMDriverDB::insertEntry(KMDBEntry *entry)
{
	// first check entry
	if (!entry->validate())
	{
		kdDebug() << "Incorrect entry, skipping...(" << entry->file << ")" << endl;
		delete entry;
		return;
	}

	// insert it in normal entries
	TQDict<KMDBEntryList>	*models = m_entries.tqfind(entry->manufacturer);
	if (!models)
	{
		models = new TQDict<KMDBEntryList>(17,false);
		models->setAutoDelete(true);
		m_entries.insert(entry->manufacturer,models);
	}
	KMDBEntryList	*list = models->tqfind(entry->model);
	if (!list)
	{
		list = new KMDBEntryList;
		list->setAutoDelete(true);
		models->insert(entry->model,list);
	}
	list->append(entry);

	if (!entry->pnpmanufacturer.isEmpty() && !entry->pnpmodel.isEmpty())
	{
		// insert it in PNP entries
		models = m_pnpentries.tqfind(entry->manufacturer);
		if (!models)
		{
			models = new TQDict<KMDBEntryList>(17,false);
			models->setAutoDelete(true);
			m_pnpentries.insert(entry->manufacturer,models);
		}
		list = models->tqfind(entry->model);
		if (!list)
		{
			list = new KMDBEntryList;
			list->setAutoDelete(true);
			models->insert(entry->model,list);
		}
		list->append(entry);
	}

	// don't block GUI
	kapp->processEvents();
}

/*
  Driver DB file format:
	FILE=<path>
	MANUFACTURER=<string>
	MODEL=<string>
	PNPMANUFACTURER=<string>
	PNPMODEL=<string>
	DESCRIPTION=<string>
*/

void KMDriverDB::loadDbFile()
{
	// first clear everything
	m_entries.clear();
	m_pnpentries.clear();

	QFile	f(dbFile());
	if (f.exists() && f.open(IO_ReadOnly))
	{
		QTextStream	t(&f);
		QString		line;
		QStringList	words;
		KMDBEntry	*entry(0);

		while (!t.eof())
		{
			line = t.readLine().stripWhiteSpace();
			if (line.isEmpty())
				continue;
			int	p = line.tqfind('=');
			if (p == -1)
				continue;
			words.clear();
			words << line.left(p) << line.mid(p+1);
			if (words[0] == "FILE")
			{
				if (entry) insertEntry(entry);
				entry = new KMDBEntry;
				entry->file = words[1];
			}
			else if (words[0] == "MANUFACTURER" && entry)
				entry->manufacturer = words[1].upper();
			else if (words[0] == "MODEL" && entry)
				entry->model = words[1];
			else if (words[0] == "MODELNAME" && entry)
				entry->modelname = words[1];
			else if (words[0] == "PNPMANUFACTURER" && entry)
				entry->pnpmanufacturer = words[1].upper();
			else if (words[0] == "PNPMODEL" && entry)
				entry->pnpmodel = words[1];
			else if (words[0] == "DESCRIPTION" && entry)
				entry->description = words[1];
			else if (words[0] == "RECOMMANDED" && entry && words[1].lower() == "yes")
				entry->recommended = true;
			else if (words[0] == "DRIVERCOMMENT" && entry)
				entry->drivercomment = ("<qt>"+words[1].tqreplace("&lt;", "<").tqreplace("&gt;", ">")+"</qt>");
		}
		if (entry)
			insertEntry(entry);
	}
}
#include "kmdriverdb.moc"