cupsdcomment.cpp
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 #include "cupsdcomment.h"
00021 
00022 #include <qfile.h>
00023 #include <qregexp.h>
00024 #include <klocale.h>
00025 #include <kstandarddirs.h>
00026 
00027 QString Comment::comment()
00028 {
00029         QString str = comment_;
00030         str.replace(QRegExp("<[^>]*>"), "");
00031         str += ("#\n" + example_);
00032         return str;
00033 }
00034 
00035 QString Comment::toolTip()
00036 {
00037         QString str = comment_;
00038         str.replace(QRegExp("^#[\\s]*"), "").replace(QRegExp("\n#[\\s]*"), "\n");
00039         return i18n("Do not translate the keyword between brackets (e.g. ServerName, ServerAdmin, etc.)", str.utf8());
00040 }
00041 
00042 QString Comment::key()
00043 {
00044     return key_;
00045 }
00046 
00047 bool Comment::load(QFile *f)
00048 {
00049         comment_ = "";
00050         example_ = "";
00051     key_ = "";
00052         QString line, *current = &comment_;
00053         while (!f->atEnd())
00054         {
00055                 f->readLine(line, 1024);
00056                 if (line.left(2) == "$$")
00057                 {
00058                         current = &example_;
00059                 }
00060         else if (line.left(2) == "%%")
00061         {
00062             key_ = line.mid(2).stripWhiteSpace();
00063         }
00064                 else if (line.left(2) == "@@")
00065                 {
00066                         return true;
00067                 }
00068                 else if (line.stripWhiteSpace().isEmpty())
00069                 {
00070                         ; 
00071                 }
00072                 else
00073                 {
00074                         if (line[0] != '#') break;
00075                         else
00076                         {
00077                                 current->append(line);
00078                         }
00079                 }
00080         }
00081         return false;
00082 }
00083 
00084 
00085 
00086 QString CupsdComment::operator[] (const QString& key)
00087 {
00088         return comment(key);
00089 }
00090 
00091 QString CupsdComment::comment(const QString& key)
00092 {
00093         if (comments_.count() != 0 || loadComments())
00094     {
00095         Comment *comm = comments_.find(key);
00096         if (comm)
00097             return comm->comment();
00098     }
00099         return QString::null;
00100 }
00101 
00102 QString CupsdComment::toolTip(const QString& key)
00103 {
00104         if (comments_.count() != 0 || loadComments())
00105     {
00106         Comment *comm = comments_.find(key);
00107         if (comm)
00108             return comm->toolTip();
00109     }
00110         return QString::null;
00111 }
00112 
00113 bool CupsdComment::loadComments()
00114 {
00115         comments_.setAutoDelete(true);
00116         comments_.clear();
00117         QFile   f(locate("data", "kdeprint/cupsd.conf.template"));
00118     if (f.exists() && f.open(IO_ReadOnly))
00119     {
00120                 Comment         *comm;
00121                 while (!f.atEnd())
00122                 {
00123                         comm = new Comment();
00124                         if (!comm->load(&f))
00125                                 break;
00126                         else
00127                         {
00128                 if (comm->key().isEmpty())
00129                     delete comm;
00130                 else
00131                     comments_.insert(comm->key(), comm);
00132                         }
00133                 }
00134     }
00135         return true;
00136 }
 
This file is part of the documentation for kdeprint Library Version 3.2.0.