1 /***************************************************************************
2 Copyright 2006 David Nolden <david.nolden.kdevelop@art-master.de>
3 ***************************************************************************/
5 /***************************************************************************
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
12 ***************************************************************************/
14 #include "teamworkfoldermanager.h"
15 #include <interfaces/icore.h>
16 #include "kdevteamworkplugin.h"
17 #include <kio/netaccess.h>
19 #include <QStringList>
26 /* Exclude this file from doublequote_chars check as krazy doesn't understand
28 //krazy:excludeall=doublequote_chars
30 TeamworkFolderManager
* TeamworkFolderManager::m_self
= 0;
32 KUrl
TeamworkFolderManager::workspaceDirectory() {
33 return m_self
->m_workspaceDir
;
36 TeamworkFolderManager::TeamworkFolderManager( const KUrl
& directory
) {
43 u
.addPath( ".teamwork" );
48 TeamworkFolderManager
* TeamworkFolderManager::self() {
52 KUrl
TeamworkFolderManager::teamworkAbsolute( const QString
& subDirectory
, const QString
& subFolder
) {
53 KUrl
ret( self()->m_teamworkDir
);
54 ret
.addPath( subFolder
);
55 if( subDirectory
.startsWith( ret
.toLocalFile() ) ) return subDirectory
;
56 ret
.addPath( subDirectory
);
61 KUrl
TeamworkFolderManager::workspaceAbsolute( const QString
& subDirectory
, const QString
& subFolder
) {
62 KUrl
ret( self()->m_workspaceDir
);
63 ret
.addPath( subFolder
);
64 if( subDirectory
.startsWith( ret
.toLocalFile() ) ) return subDirectory
;
65 ret
.addPath( subDirectory
);
70 void TeamworkFolderManager::createTeamworkFolder() throw(QString
) {
71 KUrl ul
= self()->m_workspaceDir
;
73 ul
.addPath( ".teamwork" );
74 if ( !KIO::NetAccess::exists( ul
, KIO::NetAccess::SourceSide
, 0 ) )
75 if( !KIO::NetAccess::mkdir( ul
, 0 ) )
76 throw QString( "could not create \"%1\"" ).arg( ul
.prettyUrl() );
80 KUrl
TeamworkFolderManager::createUniqueDirectory( const QString
& subFolder
, const QString
& name
, const QString
& namePrefix
, const QString
& nameSuffix
) throw(QString
) {
81 if( self()->m_workspaceDir
.isEmpty() )
82 throw QString( "no teamwork-workspace-directory set" );
83 KUrl ul
= self()->m_workspaceDir
;
85 ul
.addPath( ".teamwork" );
86 if ( !KIO::NetAccess::exists( ul
, KIO::NetAccess::SourceSide
, 0 ) )
87 KIO::NetAccess::mkdir( ul
, 0 );
89 if ( !KIO::NetAccess::exists( ul
, KIO::NetAccess::SourceSide
, 0 ) )
90 throw QString( "could not create .teamwork-directory" );
93 QStringList sub
= subFolder
.split( "/" );
94 while( !sub
.isEmpty() ) {
95 ul
.addPath( sub
.front() );
96 if ( !KIO::NetAccess::exists( ul
, KIO::NetAccess::SourceSide
, 0 ) )
97 KIO::NetAccess::mkdir( ul
, 0 );
99 if ( !KIO::NetAccess::exists( ul
, KIO::NetAccess::SourceSide
, 0 ) )
100 throw QString( "could not create directory %1 directory" ).arg( ul
.prettyUrl() );
107 nu
.addPath( namePrefix
+ name
+ nameSuffix
);
110 if( !KIO::NetAccess::exists( nu
, KIO::NetAccess::SourceSide
, 0 ) ){
111 KIO::NetAccess::mkdir( nu
, 0 );
113 if ( KIO::NetAccess::exists( nu
, KIO::NetAccess::SourceSide
, 0 ) )
117 ///If the file exists try it with additional date/time
119 nu
.addPath( namePrefix
+ name
+ QDateTime::currentDateTime().toString( Qt::ISODate
) + nameSuffix
);
122 if( !KIO::NetAccess::exists( nu
, KIO::NetAccess::SourceSide
, 0 ) ) {
123 KIO::NetAccess::mkdir( nu
, 0 );
125 if ( KIO::NetAccess::exists( nu
, KIO::NetAccess::SourceSide
, 0 ) )
129 ///If even this file exists, add a suffix behind the date
130 for( int a
= 0; a
< 100; a
++ ) {
132 nu
.addPath( namePrefix
+ QString("_%1_").arg( a
) + name
+ QDateTime::currentDateTime().toString( Qt::ISODate
) + nameSuffix
);
134 if( !KIO::NetAccess::exists( nu
, KIO::NetAccess::SourceSide
, 0 ) ){
135 KIO::NetAccess::mkdir( nu
, 0 );
137 if ( KIO::NetAccess::exists( nu
, KIO::NetAccess::SourceSide
, 0 ) )
142 throw QString( "failed to allocate filename for %1" ).arg( name
+ "." + nameSuffix
);
145 KUrl
TeamworkFolderManager::createUniqueFile( const QString
& subFolder
, const QString
& extension
, const QString
& name
, const QString
& namePrefix
, const QString
& nameSuffix
) throw(QString
) {
146 if( self()->m_workspaceDir
.isEmpty() )
147 throw QString( "no teamwork-workspace-directory set" );
148 KUrl ul
= self()->m_workspaceDir
;
150 ul
.addPath( ".teamwork" );
151 if ( !KIO::NetAccess::exists( ul
, KIO::NetAccess::SourceSide
, 0 ) )
152 KIO::NetAccess::mkdir( ul
, 0 );
154 if ( !KIO::NetAccess::exists( ul
, KIO::NetAccess::SourceSide
, 0 ) )
155 throw QString( "could not create .teamwork-directory" );
158 QStringList sub
= subFolder
.split( "/" );
159 while( !sub
.isEmpty() ) {
160 ul
.addPath( sub
.front() );
161 if ( !KIO::NetAccess::exists( ul
, KIO::NetAccess::SourceSide
, 0 ) )
162 KIO::NetAccess::mkdir( ul
, 0 );
164 if ( !KIO::NetAccess::exists( ul
, KIO::NetAccess::SourceSide
, 0 ) )
165 throw QString( "could not create directory %1 directory" ).arg( ul
.prettyUrl() );
172 nu
.addPath( namePrefix
+ name
+ nameSuffix
+ "." + extension
);
175 if( !KIO::NetAccess::exists( nu
, KIO::NetAccess::SourceSide
, 0 ) ){
176 if( createFile( nu
) )
180 ///If the file exists try it with additional date/time
182 nu
.addPath( namePrefix
+ name
+ QDateTime::currentDateTime().toString( Qt::ISODate
) + nameSuffix
+ "." + extension
);
185 if( !KIO::NetAccess::exists( nu
, KIO::NetAccess::SourceSide
, 0 ) ) {
186 if( createFile( nu
) )
190 ///If even this file exists, add a suffix behind the date
191 for( int a
= 0; a
< 100; a
++ ) {
193 nu
.addPath( namePrefix
+ QString("_%1_").arg( a
) + name
+ QDateTime::currentDateTime().toString( Qt::ISODate
) + nameSuffix
+ "." + extension
);
195 if( !KIO::NetAccess::exists( nu
, KIO::NetAccess::SourceSide
, 0 ) ){
196 if( createFile( nu
) )
201 throw QString( "failed to allocate filename for %1" ).arg( name
+ "." + nameSuffix
);
204 KUrl
TeamworkFolderManager::createUniqueFile( const QString
& subFolder
, const QString
& fileName
, const QString
& namePrefix
, const QString
& nameSuffix
) throw(QString
) {
205 QFileInfo
i( fileName
);
207 u
.addPath( i
.path() );
208 return createUniqueFile( u
.toLocalFile(), i
.completeSuffix(), i
.baseName(), namePrefix
, nameSuffix
);
211 void TeamworkFolderManager::registerTempItem( const KUrl
& u
) {
213 self()->m_tempItems
[teamworkAbsolute(u
.toLocalFile()).pathOrUrl()] = true;
215 self()->m_tempItems
[u
.pathOrUrl()] = true;
218 QString
TeamworkFolderManager::teamworkRelative( const KUrl
& url
, const QString
& subfolder
) {
219 KUrl u
= self()->m_teamworkDir
;
220 u
.addPath( subfolder
);
221 u
.adjustPath( KUrl::AddTrailingSlash
);
222 return KUrl::relativeUrl( u
, url
);
225 QString
TeamworkFolderManager::workspaceRelative( const KUrl
& url
, const QString
& subfolder
) {
226 KUrl u
= self()->m_workspaceDir
;
227 u
.addPath( subfolder
);
228 u
.adjustPath( KUrl::AddTrailingSlash
);
229 //kDebug(9500) << "workspaceRelative(" << url << "," << subfolder << ") called. Worspace:" << self()->m_workspaceDir << "base:" << u << "result:" << KUrl::relativeUrl( u, url );
230 return KUrl::relativeUrl( u
, url
);
233 TeamworkFolderManager::~TeamworkFolderManager() {
234 for( QMap
< QString
, bool >::iterator it
= m_tempItems
.begin();it
!= m_tempItems
.end(); ++it
) {
235 ///First, make sure that the file is really a subfolder of the .teamwork-directory
238 if( (f
.path()).startsWith( m_teamworkDir
.path(KUrl::AddTrailingSlash
) ) && (f
.pathOrUrl()).startsWith( m_teamworkDir
.pathOrUrl() ) ) {
239 if( ! KIO::NetAccess::del( f
, 0 ) )
240 kDebug(9500) << "TeamworkFolderManager error: File" << f
.prettyUrl() << "could not be deleted";
242 kDebug(9500) << "TeamworkFolderManager error: File" << f
.prettyUrl() << "was registered as temporary file, but is not in folder" << m_teamworkDir
;
248 ///@todo use netaccess!
249 bool TeamworkFolderManager::createFile( const KUrl
& url
) {
250 QFile
f( url
.path() );
251 if( f
.open(QIODevice::WriteOnly
) )
258 // kate: space-indent on; indent-width 2; tab-width 2; replace-tabs on