kmdriverdb.cpp

00001 /*
00002  *  This file is part of the KDE libraries
00003  *  Copyright (c) 2001 Michael Goffioul <kdeprint@swing.be>
00004  *
00005  *  This library is free software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU Library General Public
00007  *  License version 2 as published by the Free Software Foundation.
00008  *
00009  *  This library is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  *  Library General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU Library General Public License
00015  *  along with this library; see the file COPYING.LIB.  If not, write to
00016  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  *  Boston, MA 02110-1301, USA.
00018  **/
00019 
00020 #include "kmdriverdb.h"
00021 #include "kmdbentry.h"
00022 #include "kmdbcreator.h"
00023 #include "kmmanager.h"
00024 #include "kmfactory.h"
00025 #include <kdebug.h>
00026 
00027 #include <qfile.h>
00028 #include <qtextstream.h>
00029 #include <qfileinfo.h>
00030 #include <kstandarddirs.h>
00031 #include <kapplication.h>
00032 #include <kmessagebox.h>
00033 
00034 KMDriverDB* KMDriverDB::m_self = 0;
00035 
00036 KMDriverDB* KMDriverDB::self()
00037 {
00038     if (!m_self)
00039     {
00040         m_self = new KMDriverDB();
00041         Q_CHECK_PTR(m_self);
00042     }
00043     return m_self;
00044 }
00045 
00046 KMDriverDB::KMDriverDB(QObject *parent, const char *name)
00047 : QObject(parent,name)
00048 {
00049     m_creator = new KMDBCreator(this,"db-creator");
00050     connect(m_creator,SIGNAL(dbCreated()),SLOT(slotDbCreated()));
00051 
00052     m_entries.setAutoDelete(true);
00053     m_pnpentries.setAutoDelete(true);
00054 }
00055 
00056 KMDriverDB::~KMDriverDB()
00057 {
00058 }
00059 
00060 QString KMDriverDB::dbFile()
00061 {
00062     // this calls insure missing directories creation
00063     QString filename = locateLocal("data",QString::fromLatin1("kdeprint/printerdb_%1.txt").arg(KMFactory::self()->printSystem()));
00064     return filename;
00065 }
00066 
00067 void KMDriverDB::init(QWidget *parent)
00068 {
00069     QFileInfo   dbfi(dbFile());
00070     QString     dirname = KMFactory::self()->manager()->driverDirectory();
00071     QStringList dbDirs = QStringList::split(':', dirname, false);
00072     bool    createflag(false);
00073 
00074     for (QStringList::ConstIterator it=dbDirs.begin(); it!=dbDirs.end() && !createflag; ++it)
00075         if (!(*it).startsWith("module:") && !m_creator->checkDriverDB(*it, dbfi.lastModified()))
00076             createflag = true;
00077 
00078     if (createflag)
00079     {
00080         // starts DB creation and wait for creator signal
00081         if (!m_creator->createDriverDB(dirname,dbfi.absFilePath(),parent))
00082             KMessageBox::error(parent, KMFactory::self()->manager()->errorMsg().prepend("<qt>").append("</qt>"));
00083     }
00084     else if (m_entries.count() == 0)
00085     {
00086         // call directly the slot as the DB won't be re-created
00087         // this will (re)load the driver DB
00088         slotDbCreated();
00089     }
00090     else
00091         // no need to refresh, and already loaded, just emit signal
00092         emit dbLoaded(false);
00093 }
00094 
00095 void KMDriverDB::slotDbCreated()
00096 {
00097     // DB should be created, check creator status
00098     if (m_creator->status())
00099     {
00100         // OK, load DB and emit signal
00101         loadDbFile();
00102         emit dbLoaded(true);
00103     }
00104     else
00105         // error while creating DB, notify the DB widget
00106         emit error(KMManager::self()->errorMsg());
00107     // be sure to emit this signal to notify the DB widget
00108     //emit dbLoaded(true);
00109 }
00110 
00111 KMDBEntryList* KMDriverDB::findEntry(const QString& manu, const QString& model)
00112 {
00113     QDict<KMDBEntryList>    *models = m_entries.find(manu);
00114     if (models)
00115         return models->find(model);
00116     return 0;
00117 }
00118 
00119 KMDBEntryList* KMDriverDB::findPnpEntry(const QString& manu, const QString& model)
00120 {
00121     QDict<KMDBEntryList>    *models = m_pnpentries.find(manu);
00122     if (models)
00123         return models->find(model);
00124     return 0;
00125 }
00126 
00127 QDict<KMDBEntryList>* KMDriverDB::findModels(const QString& manu)
00128 {
00129     return m_entries.find(manu);
00130 }
00131 
00132 void KMDriverDB::insertEntry(KMDBEntry *entry)
00133 {
00134     // first check entry
00135     if (!entry->validate())
00136     {
00137         kdDebug() << "Incorrect entry, skipping...(" << entry->file << ")" << endl;
00138         delete entry;
00139         return;
00140     }
00141 
00142     // insert it in normal entries
00143     QDict<KMDBEntryList>    *models = m_entries.find(entry->manufacturer);
00144     if (!models)
00145     {
00146         models = new QDict<KMDBEntryList>(17,false);
00147         models->setAutoDelete(true);
00148         m_entries.insert(entry->manufacturer,models);
00149     }
00150     KMDBEntryList   *list = models->find(entry->model);
00151     if (!list)
00152     {
00153         list = new KMDBEntryList;
00154         list->setAutoDelete(true);
00155         models->insert(entry->model,list);
00156     }
00157     list->append(entry);
00158 
00159     if (!entry->pnpmanufacturer.isEmpty() && !entry->pnpmodel.isEmpty())
00160     {
00161         // insert it in PNP entries
00162         models = m_pnpentries.find(entry->manufacturer);
00163         if (!models)
00164         {
00165             models = new QDict<KMDBEntryList>(17,false);
00166             models->setAutoDelete(true);
00167             m_pnpentries.insert(entry->manufacturer,models);
00168         }
00169         list = models->find(entry->model);
00170         if (!list)
00171         {
00172             list = new KMDBEntryList;
00173             list->setAutoDelete(true);
00174             models->insert(entry->model,list);
00175         }
00176         list->append(entry);
00177     }
00178 
00179     // don't block GUI
00180     kapp->processEvents();
00181 }
00182 
00183 /*
00184   Driver DB file format:
00185     FILE=<path>
00186     MANUFACTURER=<string>
00187     MODEL=<string>
00188     PNPMANUFACTURER=<string>
00189     PNPMODEL=<string>
00190     DESCRIPTION=<string>
00191 */
00192 
00193 void KMDriverDB::loadDbFile()
00194 {
00195     // first clear everything
00196     m_entries.clear();
00197     m_pnpentries.clear();
00198 
00199     QFile   f(dbFile());
00200     if (f.exists() && f.open(IO_ReadOnly))
00201     {
00202         QTextStream t(&f);
00203         QString     line;
00204         QStringList words;
00205         KMDBEntry   *entry(0);
00206 
00207         while (!t.eof())
00208         {
00209             line = t.readLine().stripWhiteSpace();
00210             if (line.isEmpty())
00211                 continue;
00212             int p = line.find('=');
00213             if (p == -1)
00214                 continue;
00215             words.clear();
00216             words << line.left(p) << line.mid(p+1);
00217             if (words[0] == "FILE")
00218             {
00219                 if (entry) insertEntry(entry);
00220                 entry = new KMDBEntry;
00221                 entry->file = words[1];
00222             }
00223             else if (words[0] == "MANUFACTURER" && entry)
00224                 entry->manufacturer = words[1].upper();
00225             else if (words[0] == "MODEL" && entry)
00226                 entry->model = words[1];
00227             else if (words[0] == "MODELNAME" && entry)
00228                 entry->modelname = words[1];
00229             else if (words[0] == "PNPMANUFACTURER" && entry)
00230                 entry->pnpmanufacturer = words[1].upper();
00231             else if (words[0] == "PNPMODEL" && entry)
00232                 entry->pnpmodel = words[1];
00233             else if (words[0] == "DESCRIPTION" && entry)
00234                 entry->description = words[1];
00235             else if (words[0] == "RECOMMANDED" && entry && words[1].lower() == "yes")
00236                 entry->recommended = true;
00237             else if (words[0] == "DRIVERCOMMENT" && entry)
00238                 entry->drivercomment = ("<qt>"+words[1].replace("&lt;", "<").replace("&gt;", ">")+"</qt>");
00239         }
00240         if (entry)
00241             insertEntry(entry);
00242     }
00243 }
00244 #include "kmdriverdb.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys