[105ed6]: src / kdevplugins / ktlproject / ktlprojectmanager.cpp Maximize Restore History

Download this file

ktlprojectmanager.cpp    355 lines (303 with data), 11.7 kB

  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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
Manage KTechLab project files.
Copyright (C) 2009-2010 Julian B채ume <julian@svg4all.de>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "ktlprojectmanager.h"
#include "ktlfolderitem.h"
#include <project/projectmodel.h>
#include <KGenericFactory>
#include <KAboutData>
#include <KMessageBox>
#include <interfaces/iproject.h>
#include <KActionCollection>
#include <sublime/mainwindow.h>
#include <QFile>
#include <QStack>
#include <QXmlSimpleReader>
#include <QDir>
using namespace KDevelop;
class KDevelop::KTLProjectManagerPrivate
{
public:
/**
* Find sub-project represented by @param item in the projects DOM document.
* This will return the root-node (i.e. documentElement) if sub-project
* is not found.
* @param item - the ProjectFolderItem representing the sub-project
* @param name - the value of the name attribute of the element to look for
* @return a QDomElement representing @param item in the DOM document or the document element
*/
QDomElement findElementInDocument( ProjectBaseItem *item, const QString &name )
{
QStack<QString> domPath;
domPath.push( name );
//TODO: can this be done without dynamic_cast?
ProjectFolderItem *wItem = dynamic_cast<ProjectFolderItem*>( item->parent() );
if (!wItem) {
//this must be the project root
return projectDomDocument.documentElement();
}
while (!wItem->isProjectRoot()){
domPath.push( wItem->folderName() );
//TODO: can this be done without dynamic_cast?
wItem = dynamic_cast<ProjectFolderItem*>( wItem->parent() );
};
QDomElement child = projectDomDocument.documentElement();
while ( !domPath.isEmpty() ){
QString nextFolder = domPath.pop();
QDomElement d = child.firstChildElement("item");
while (!d.isNull() && d.attribute("name") != nextFolder){
if (d.attribute("name") == ""){
kWarning() << "no, this shouldn't happen";
return QDomElement();
}
d = d.nextSiblingElement("item");
}
child = d;
}
return child;
}
void writeProjectToDisk()
{
QFile file( projectFile.toLocalFile() );
if ( !file.open( QIODevice::ReadWrite ) )
{
KMessageBox::sorry( 0l, i18n("Could not open %1 for writing").arg(projectFile.toLocalFile()) );
return;
}
file.resize(0);
file.write( projectDomDocument.toByteArray() );
file.close();
}
void removeItemFromDocument( ProjectBaseItem *item, const QString &name )
{
QDomElement child = findElementInDocument( item, name );
child.parentNode().removeChild(child);
}
void updateItemFromDocument( ProjectBaseItem *item, const QString &name )
{
QDomElement child = findElementInDocument( item, name );
KUrl folder;
if (item->folder())
folder = item->folder()->url();
if (item->file())
folder = item->file()->url();
QString relativeFileName =
KUrl::relativePath( projectFile.directory(), folder.directory() );
relativeFileName.append(folder.fileName());
QDomElement newNode = child.cloneNode().toElement();
newNode.setAttribute("name",folder.fileName());
newNode.setAttribute("url",relativeFileName);
child.parentNode().replaceChild( newNode, child );
}
QDomDocument projectDomDocument;
IProject *project;
KUrl projectFile;
};
K_PLUGIN_FACTORY(KTLProjectManagerFactory, registerPlugin<KTLProjectManager>(); )
K_EXPORT_PLUGIN(KTLProjectManagerFactory(KAboutData("ktlprojectmanager","ktlprojectmanager",ki18n("KTechLab Project Manager"), "0.1", ki18n("A plugin to support KTechLab project management"), KAboutData::License_GPL)))
KTLProjectManager::KTLProjectManager( QObject *parent, const QVariantList &args )
: IPlugin( KTLProjectManagerFactory::componentData(), parent ), IProjectFileManager( ),
d( new KTLProjectManagerPrivate() )
{
KDEV_USE_EXTENSION_INTERFACE( KDevelop::IProjectFileManager )
}
KTLProjectManager::~KTLProjectManager()
{
}
ProjectFileItem* KTLProjectManager::addFile( const KUrl& folder, ProjectFolderItem* parent )
{
if (!folder.isValid() && folder.isLocalFile())
return 0;
//create file on disk
QFile newFile(folder.toLocalFile());
if (!newFile.exists()){
newFile.open(QIODevice::ReadWrite);
newFile.close();
}
// add file to project
ProjectFileItem *item = new ProjectFileItem( parent->project(), folder, parent );
QString relativeFileName =
KUrl::relativePath( d->projectFile.directory(), folder.directory(KUrl::AppendTrailingSlash) );
relativeFileName.append(folder.fileName());
QDomElement itemElement = d->projectDomDocument.createElement("item");
itemElement.setAttribute("url", relativeFileName );
itemElement.setAttribute("name", item->fileName() );
itemElement.setAttribute("type", "File");
QDomElement folderElement = d->findElementInDocument( parent, parent->folderName() );
folderElement.appendChild( itemElement );
d->writeProjectToDisk();
return item;
}
ProjectFolderItem* KTLProjectManager::addFolder( const KUrl& folder, ProjectFolderItem* parent )
{
if (!folder.isValid() && folder.isLocalFile())
return 0;
//create folder on disk
QDir newFolder(folder.directory());
if (newFolder.exists()){
newFolder.mkdir(folder.fileName());
}
// add file to project
ProjectFolderItem *item = new ProjectFolderItem( parent->project(), folder, parent );
QString relativeFileName =
KUrl::relativePath( d->projectFile.directory(), newFolder.absolutePath() );
QDomElement itemElement = d->projectDomDocument.createElement("item");
itemElement.setAttribute("url", relativeFileName );
itemElement.setAttribute("name", item->folderName() );
itemElement.setAttribute("type", "Program");
QDomElement folderElement = d->findElementInDocument( parent, parent->folderName() );
folderElement.appendChild( itemElement );
d->writeProjectToDisk();
return item;
}
IProjectFileManager::Features KTLProjectManager::features() const
{
return Features( Files | Folders | Targets );
}
ProjectFolderItem* KTLProjectManager::import( IProject* project )
{
ProjectFolderItem *rootItem = new KTLFolderItem( project, project->folder() );
rootItem->setProjectRoot(true);
d->projectFile = rootItem->project()->folder();
d->projectFile.addPath(rootItem->project()->name()+".ktechlab");
QFile file( d->projectFile.toLocalFile() );
if ( !file.open( QIODevice::ReadOnly ) )
{
KMessageBox::sorry( 0l, i18n("Could not open %1 for reading").arg(d->projectFile.toLocalFile()) );
return 0;
}
d->projectDomDocument = QDomDocument( "KTechlab" );
QString errorMessage;
if ( !d->projectDomDocument.setContent( &file, &errorMessage ) )
{
KMessageBox::sorry( 0l, i18n("Couldn't parse xml:\n%1").arg(errorMessage) );
return 0;
}
file.close();
return rootItem;
}
QList<ProjectFolderItem*> KTLProjectManager::parse( ProjectFolderItem *item )
{
QList<ProjectFolderItem*> result;
QDomElement root = d->findElementInDocument( item, item->folderName() );
QDomNode node = root.firstChild();
while ( !node.isNull() )
{
QDomElement childElement = node.toElement();
if ( !childElement.isNull() )
{
const QString tagName = childElement.tagName();
if ( tagName == "item" )
{
QString type = childElement.attribute( "type", QString() );
QString name = childElement.attribute( "name", QString() );
KUrl itemUrl( item->project()->folder(), childElement.attribute( "url", QString() ) );
if ( type == "File" )
{
ProjectFileItem *child = new ProjectFileItem(item->project(), itemUrl, item);
kDebug() << "Created file:" << child->fileName();
} else if ( !type.isEmpty() )
{
itemUrl.addPath(name);
KTLFolderItem *child = new KTLFolderItem(item->project(), itemUrl, item);
result.append(child);
}
}
}
node = node.nextSibling();
}
return result;
}
bool KTLProjectManager::reload( ProjectBaseItem* item )
{
ProjectFolderItem *folder = item->folder();
if (!folder)
return false;
folder->removeRows( 0, folder->rowCount() );
QList<ProjectFolderItem*> folders = parse(folder);
//recursively reload subfolders
foreach(ProjectFolderItem *f, folders){
reload(f);
}
return true;
}
bool KTLProjectManager::removeFile( ProjectFileItem* file )
{
KUrl url = file->url();
//remove file from disk
QFile localFile(url.toLocalFile());
if (!localFile.exists() || !localFile.remove())
kWarning() << "Couldn't remove file: " << url.toLocalFile();
//remove file from project
//TODO: can this be done without dynamic_cast?
ProjectFolderItem *parent = dynamic_cast<ProjectFolderItem*>(file->parent());
if (!parent)
return false;
d->removeItemFromDocument( file, file->fileName() );
parent->removeRow( file->row() );
d->writeProjectToDisk();
return true;
}
bool KTLProjectManager::removeFolder( ProjectFolderItem* folder )
{
//remove all children, is this needed?
foreach(ProjectFileItem *item, folder->fileList()){
removeFile(item);
}
foreach(ProjectFolderItem *item, folder->folderList()){
removeFolder(item);
}
QDir dir(folder->url().directory());
if (!dir.rmdir(folder->url().toLocalFile()))
return false;
d->removeItemFromDocument( folder, folder->folderName() );
folder->parent()->removeRow( folder->row() );
d->writeProjectToDisk();
return true;
}
bool KTLProjectManager::renameFile( ProjectFileItem* oldFile, const KUrl& newFile )
{
KUrl oldFileUrl = oldFile->url();
QFile file(oldFileUrl.toLocalFile());
if (!file.rename(newFile.toLocalFile()))
return false;
QString oldFileName = oldFile->fileName();
oldFile->setUrl(newFile);
d->updateItemFromDocument( oldFile, oldFileName );
d->writeProjectToDisk();
return true;
}
bool KTLProjectManager::renameFolder( ProjectFolderItem* oldFolder, const KUrl& newFolder )
{
KUrl oldFolderUrl = oldFolder->url();
QDir folder(oldFolderUrl.directory());
if (!folder.rename(oldFolder->folderName(), newFolder.fileName()))
return false;
QString oldFolderName = oldFolder->folderName();
oldFolder->setUrl(newFolder);
d->updateItemFromDocument( oldFolder, oldFolderName );
d->writeProjectToDisk();
return true;
}
bool KTLProjectManager::reload(ProjectFolderItem* item)
{
return false;
}
void KTLProjectManager::createActionsForMainWindow( Sublime::MainWindow* window, QString& xmlFile, KActionCollection& actions )
{
//TODO: implement me
IPlugin::createActionsForMainWindow( window, xmlFile, actions );
}
#include "ktlprojectmanager.moc"