1 /*
2 This file is part of kdepim.
3
4 Copyright (c) 2004 Cornelius Schumacher <schumacher@kde.org>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20 */
21
22 #include <QtCore/QStringList>
23
24 #include "file.h"
25 #include <QDebug>
26
27 using namespace KODE;
28
29 class File::Private
30 {
31 public:
Private()32 Private()
33 : mProject()
34 {
35 }
36
37 QString mHeaderFilename;
38 QString mImplFilename;
39 QString mNameSpace;
40 QString mProject;
41 QStringList mCopyrightStrings;
42 License mLicense;
43 QStringList mIncludes;
44 Class::List mClasses;
45 Variable::List mFileVariables;
46 Function::List mFileFunctions;
47 Enum::List mFileEnums;
48 QStringList mExternCDeclarations;
49 Code mFileCode;
50 };
51
File()52 File::File()
53 : d( new Private )
54 {
55 }
56
File(const File & other)57 File::File( const File &other )
58 : d( new Private )
59 {
60 *d = *other.d;
61 }
62
~File()63 File::~File()
64 {
65 delete d;
66 }
67
operator =(const File & other)68 File& File::operator=( const File &other )
69 {
70 if ( this == &other )
71 return *this;
72
73 *d = *other.d;
74
75 return *this;
76 }
77
setFilename(const QString & filename)78 void File::setFilename( const QString& filename )
79 {
80 d->mImplFilename = filename + ".cpp";
81 d->mHeaderFilename = filename + ".h";
82 }
83
setImplementationFilename(const QString & filename)84 void File::setImplementationFilename( const QString &filename )
85 {
86 d->mImplFilename = filename;
87 }
88
setHeaderFilename(const QString & filename)89 void File::setHeaderFilename( const QString &filename )
90 {
91 d->mHeaderFilename = filename;
92 }
93
filenameHeader() const94 QString File::filenameHeader() const
95 {
96 if ( !d->mHeaderFilename.isEmpty() )
97 return d->mHeaderFilename;
98
99 if ( !d->mClasses.isEmpty() ) {
100 QString className = d->mClasses[ 0 ].name();
101 return className.toLower() + ".h";
102 }
103
104 return QString();
105 }
106
filenameImplementation() const107 QString File::filenameImplementation() const
108 {
109 if ( !d->mImplFilename.isEmpty() )
110 return d->mImplFilename;
111
112 if ( !d->mClasses.isEmpty() ) {
113 QString className = d->mClasses[ 0 ].name();
114 return className.toLower() + ".cpp";
115 }
116
117 return QString();
118 }
119
setNameSpace(const QString & nameSpace)120 void File::setNameSpace( const QString &nameSpace )
121 {
122 d->mNameSpace = nameSpace;
123 }
124
nameSpace() const125 QString File::nameSpace() const
126 {
127 return d->mNameSpace;
128 }
129
setProject(const QString & project)130 void File::setProject( const QString &project )
131 {
132 if ( project.isEmpty() )
133 return;
134
135 d->mProject = project;
136 }
137
project() const138 QString File::project() const
139 {
140 return d->mProject;
141 }
142
addCopyright(int year,const QString & name,const QString & email)143 void File::addCopyright( int year, const QString &name, const QString &email )
144 {
145 QString str = "Copyright (c) " + QString::number( year ) + ' ' + name + " <"
146 + email + '>';
147
148 d->mCopyrightStrings.append( str );
149 }
150
copyrightStrings() const151 QStringList File::copyrightStrings() const
152 {
153 return d->mCopyrightStrings;
154 }
155
setLicense(const License & license)156 void File::setLicense( const License &license )
157 {
158 d->mLicense = license;
159 }
160
license() const161 License File::license() const
162 {
163 return d->mLicense;
164 }
165
addInclude(const QString & _include)166 void File::addInclude( const QString &_include )
167 {
168 QString include = _include;
169 if ( !include.endsWith( ".h" ) )
170 include.append( ".h" );
171
172 if ( !d->mIncludes.contains( include ) )
173 d->mIncludes.append( include );
174 }
175
includes() const176 QStringList File::includes() const
177 {
178 return d->mIncludes;
179 }
180
insertClass(const Class & newClass)181 void File::insertClass( const Class &newClass )
182 {
183 Q_ASSERT(!newClass.name().isEmpty());
184 Class::List::Iterator it;
185 for ( it = d->mClasses.begin(); it != d->mClasses.end(); ++it ) {
186 if ( (*it).qualifiedName() == newClass.qualifiedName() ) {
187 // This happens often, probably due to usage of shared types
188 //qDebug() << "WARNING: Already having class" << newClass.qualifiedName() << "in file" << filenameHeader() << filenameImplementation();
189 it = d->mClasses.erase( it );
190 d->mClasses.insert( it, newClass );
191 return;
192 }
193 }
194
195 d->mClasses.append( newClass );
196 }
197
classes() const198 Class::List File::classes() const
199 {
200 return d->mClasses;
201 }
202
hasClass(const QString & name)203 bool File::hasClass( const QString &name )
204 {
205 Class::List::ConstIterator it;
206 for ( it = d->mClasses.constBegin(); it != d->mClasses.constEnd(); ++it ) {
207 if ( (*it).name() == name )
208 break;
209 }
210
211 return it != d->mClasses.constEnd();
212 }
213
findClass(const QString & name)214 Class File::findClass( const QString &name )
215 {
216 Class::List::ConstIterator it;
217 for ( it = d->mClasses.constBegin(); it != d->mClasses.constEnd(); ++it ) {
218 if ( (*it).name() == name )
219 return *it;
220 }
221
222 return Class();
223 }
224
addFileVariable(const Variable & variable)225 void File::addFileVariable( const Variable &variable )
226 {
227 d->mFileVariables.append( variable );
228 }
229
fileVariables() const230 Variable::List File::fileVariables() const
231 {
232 return d->mFileVariables;
233 }
234
addFileFunction(const Function & function)235 void File::addFileFunction( const Function &function )
236 {
237 d->mFileFunctions.append( function );
238 }
239
fileFunctions() const240 Function::List File::fileFunctions() const
241 {
242 return d->mFileFunctions;
243 }
244
addFileEnum(const Enum & enumValue)245 void File::addFileEnum( const Enum &enumValue )
246 {
247 d->mFileEnums.append( enumValue );
248 }
249
fileEnums() const250 Enum::List File::fileEnums() const
251 {
252 return d->mFileEnums;
253 }
254
addExternCDeclaration(const QString & externalCDeclaration)255 void File::addExternCDeclaration( const QString &externalCDeclaration )
256 {
257 d->mExternCDeclarations.append( externalCDeclaration );
258 }
259
externCDeclarations() const260 QStringList File::externCDeclarations() const
261 {
262 return d->mExternCDeclarations;
263 }
264
addFileCode(const Code & code)265 void File::addFileCode( const Code &code )
266 {
267 d->mFileCode = code;
268 }
269
fileCode() const270 Code File::fileCode() const
271 {
272 return d->mFileCode;
273 }
274
clearClasses()275 void File::clearClasses()
276 {
277 d->mClasses.clear();
278 }
279
clearFileFunctions()280 void File::clearFileFunctions()
281 {
282 d->mFileFunctions.clear();
283 }
284
clearFileVariables()285 void File::clearFileVariables()
286 {
287 d->mFileVariables.clear();
288 }
289
clearCode()290 void File::clearCode()
291 {
292 clearClasses();
293 clearFileFunctions();
294 clearFileVariables();
295 }
296