summaryrefslogtreecommitdiffstats
path: root/lib/util/filetemplate.cpp
blob: e0425614a047ea62f3a6d172e6dc3c8c404c3533 (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
/* This file is part of the KDE project
   Copyright (C) 2002 Sandy Meier <smeier@tdevelop.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   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 "filetemplate.h"

#include <tqdatetime.h>
#include <tqfile.h>
#include <tqfileinfo.h>
#include <tqregexp.h>
#include <tqtextstream.h>

#include <kstandarddirs.h>

#include "kdevplugin.h"
#include "kdevproject.h"
#include "domutil.h"


bool FileTemplate::exists(KDevPlugin *part, const TQString &name, Policy p)
{
    //TQString fileName = (p == Default) ?
    //   (part->project()->projectDirectory() + "/templates/" + name) : name;

    return TQFile::exists( fullPathForName(part,name,p) );
}

TQString FileTemplate::read(KDevPlugin *part, const TQString &name, Policy p)
{
    
  //KDevProject *project = part->project();
    //TQString fileName = (p == Default) ? (project->projectDirectory() +
//					 "/templates/" + name) : name;

    return readFile(part, fullPathForName(part, name, p) );
}

TQString FileTemplate::readFile(KDevPlugin *part, const TQString &fileName)
{
    TQDomDocument &dom = *part->projectDom();

    TQFile f(fileName);
    if (!f.open(IO_ReadOnly))
        return TQString();
    TQTextStream stream(&f);
    TQString str = stream.read();

    return makeSubstitutions( dom, str );
}

TQString FileTemplate::makeSubstitutions( TQDomDocument & dom, const TQString & text )
{
    TQString author = DomUtil::readEntry(dom, "/general/author");
    TQString email = DomUtil::readEntry(dom, "/general/email");
    TQString version = DomUtil::readEntry(dom, "/general/version");
    TQString appname = DomUtil::readEntry(dom, "/general/projectname");
    TQString date = TQDate::tqcurrentDate().toString();
    TQString year = TQString::number(TQDate::tqcurrentDate().year());

    TQString str = text;
    str.replace(TQRegExp("\\$EMAIL\\$"),email);
    str.replace(TQRegExp("\\$AUTHOR\\$"),author);
    str.replace(TQRegExp("\\$VERSION\\$"),version);
    str.replace(TQRegExp("\\$DATE\\$"),date);
    str.replace(TQRegExp("\\$YEAR\\$"),year);
    str.replace(TQRegExp("\\$APPNAME\\$"),appname);
    str.replace(TQRegExp("\\$APPNAME\\$"),appname);
    str.replace(TQRegExp("\\$APPNAMEUC\\$"),appname.upper());
    str.replace(TQRegExp("\\$APPNAMELC\\$"),appname.lower());

    return str;
}


bool FileTemplate::copy(KDevPlugin *part, const TQString &name,
                        const TQString &dest, Policy p)
{
    TQString text = read(part, name, p);

    TQFile f(dest);
    if (!f.open(IO_WriteOnly))
        return false;

    TQFileInfo fi(f);
    TQString module = fi.baseName();
    TQString basefilename = fi.baseName(true);
    text.replace(TQRegExp("\\$MODULE\\$"),module);
    text.replace(TQRegExp("\\$FILENAME\\$"),basefilename);

    TQTextStream stream(&f);
    stream << text;
    f.close();

    return true;
}

TQString FileTemplate::fullPathForName(KDevPlugin *part, const TQString &name,
                                      Policy p) {
    // if Policy is not default, full path is just the name
    if (p!=Default) return name;

    TQString fileName;
    // first try project-specific
    if (part->project())
    {
        fileName = (part->project()->projectDirectory() + "/templates/" + name);
        if (TQFile::exists(fileName)) return fileName;
    }

    // next try global
    TQString globalName = ::locate("data", "kdevfilecreate/file-templates/" + name);
    return globalName.isNull() ? fileName : globalName;
}