KDevelop API Documentation

filetemplate.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002    Copyright (C) 2002 Sandy Meier <smeier@kdevelop.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
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., 59 Temple Place - Suite 330,
00017    Boston, MA 02111-1307, USA.
00018 */
00019 
00020 #include "filetemplate.h"
00021 
00022 #include <qdatetime.h>
00023 #include <qfile.h>
00024 #include <qfileinfo.h>
00025 #include <qregexp.h>
00026 #include <qtextstream.h>
00027 
00028 #include <kstandarddirs.h>
00029 
00030 #include "kdevplugin.h"
00031 #include "kdevproject.h"
00032 #include "domutil.h"
00033 
00034 
00035 bool FileTemplate::exists(KDevPlugin *part, const QString &name, Policy p)
00036 {
00037     //QString fileName = (p == Default) ?
00038     //   (part->project()->projectDirectory() + "/templates/" + name) : name;
00039 
00040     return QFile::exists( fullPathForName(part,name,p) );
00041 }
00042 
00043 QString FileTemplate::read(KDevPlugin *part, const QString &name, Policy p)
00044 {
00045     
00046   //KDevProject *project = part->project();
00047     //QString fileName = (p == Default) ? (project->projectDirectory() +
00048 //                   "/templates/" + name) : name;
00049 
00050     return readFile(part, fullPathForName(part, name, p) );
00051 }
00052 
00053 
00054 QString FileTemplate::readFile(KDevPlugin *part, const QString &fileName)
00055 {
00056     QDomDocument &dom = *part->projectDom();
00057 
00058     QFile f(fileName);
00059     if (!f.open(IO_ReadOnly))
00060         return QString::null;
00061     QTextStream stream(&f);
00062     QString str = stream.read();
00063 
00064     QString author = DomUtil::readEntry(dom, "/general/author");
00065     QString email = DomUtil::readEntry(dom, "/general/email");
00066     QString version = DomUtil::readEntry(dom, "/general/version");
00067     QString date = QDate::currentDate().toString();
00068     QString year = QString::number(QDate::currentDate().year());
00069 
00070     str.replace(QRegExp("\\$EMAIL\\$"),email);
00071     str.replace(QRegExp("\\$AUTHOR\\$"),author);
00072     str.replace(QRegExp("\\$VERSION\\$"),version);
00073     str.replace(QRegExp("\\$DATE\\$"),date);
00074     str.replace(QRegExp("\\$YEAR\\$"),year);
00075 
00076     return str;
00077 }
00078 
00079 
00080 bool FileTemplate::copy(KDevPlugin *part, const QString &name,
00081                         const QString &dest, Policy p)
00082 {
00083     QString text = read(part, name, p);
00084 
00085     QFile f(dest);
00086     if (!f.open(IO_WriteOnly))
00087         return false;
00088 
00089     QFileInfo fi(f);
00090     QString module = fi.baseName();
00091     QString basefilename = fi.baseName(true);
00092     text.replace(QRegExp("\\$MODULE\\$"),module);
00093     text.replace(QRegExp("\\$FILENAME\\$"),basefilename);
00094 
00095     QTextStream stream(&f);
00096     stream << text;
00097     f.close();
00098 
00099     return true;
00100 }
00101 
00102 QString FileTemplate::fullPathForName(KDevPlugin *part, const QString &name,
00103                                       Policy p) {
00104     // if Policy is not default, full path is just the name
00105     if (p!=Default) return name;
00106 
00107     QString fileName;
00108     // first try project-specific
00109     if (part->project())
00110     {
00111         fileName = (part->project()->projectDirectory() + "/templates/" + name);
00112         if (QFile::exists(fileName)) return fileName;
00113     }
00114 
00115     // next try global
00116     QString globalName = ::locate("data", "kdevfilecreate/file-templates/" + name);
00117     return globalName.isNull() ? fileName : globalName;
00118 }
KDE Logo
This file is part of the documentation for KDevelop Version 3.1.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Mar 23 00:03:52 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003