summaryrefslogtreecommitdiffstats
path: root/kioslave/svn/ksvnd/ksvnd.cpp
blob: 7442ee95ed73cf073a073bd44ac1aadc75602c5e (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
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
/*
    This file is part of the KDE Project

    Copyright (C) 2003, 2004 Mickael Marchand <marchand@kde.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    version 2 as published by the Free Software Foundation.

    This software 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 library; see the file COPYING. If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include <kapplication.h>
#include <klocale.h>
#include <kdebug.h>
#include <kmessagebox.h>
#include <qdir.h>
#include <qfile.h>

#include "config.h"

#include "ksvnd.h"
#include "commitdlg.h"

extern "C" {
    KDE_EXPORT KDEDModule *create_ksvnd(const QCString &name) {
       return new KSvnd(name);
    }
}

KSvnd::KSvnd(const QCString &name)
 : KDEDModule(name) {
}

KSvnd::~KSvnd() {
}

QString KSvnd::commitDialog(QString modifiedFiles) {
	CommitDlg commitDlg;
	commitDlg.setLog( modifiedFiles );
	int result = commitDlg.exec();
	if ( result == QDialog::Accepted ) {
		return commitDlg.logMessage();
	} else
		return QString::null;
}

bool KSvnd::AreAnyFilesInSvn( const KURL::List& wclist ) {
	for ( QValueListConstIterator<KURL> it = wclist.begin(); it != wclist.end() ; ++it ) {
		kdDebug( 7128 ) << "Checking file " << ( *it ) << endl;
		QDir bdir ( ( *it ).path() );
		if ( bdir.exists() && QFile::exists( ( *it ).path() + "/.svn/entries" ) ) {
			return true;
		} else if ( !bdir.exists() ) {
			if ( isFileInSvnEntries( ( *it ).fileName(), ( *it ).directory() + "/.svn/entries" ) || isFileInExternals ( ( *it ).fileName(), ( *it ).directory()+"/.svn/dir-props" ) )
				return true;
		}
	}
	return false;
}

bool KSvnd::AreAnyFilesNotInSvn( const KURL::List& wclist ) {
	for ( QValueListConstIterator<KURL> it = wclist.begin(); it != wclist.end() ; ++it ) {
		kdDebug( 7128 ) << "Checking file " << ( *it ) << endl;
		QDir bdir ( ( *it ).path() );
		if ( bdir.exists() && !QFile::exists( ( *it ).path() + "/.svn/entries" ) ) {
			return true;
		} else if ( !bdir.exists() ) {
			if ( !isFileInSvnEntries( ( *it ).fileName(),( *it ).directory() + "/.svn/entries" ) && !isFileInExternals ( ( *it ).fileName(), ( *it ).directory()+"/.svn/dir-props" ) ) 
				return true;
		}
	}
	return false;
}

bool KSvnd::AreAllFilesInSvn( const KURL::List& wclist ) {
	for ( QValueListConstIterator<KURL> it = wclist.begin(); it != wclist.end() ; ++it ) {
		kdDebug( 7128 ) << "Checking file " << ( *it ) << endl;
		QDir bdir ( ( *it ).path() );
		if ( bdir.exists() && !QFile::exists( ( *it ).path() + "/.svn/entries" ) ) {
			return false;
		} else if ( !bdir.exists() ) {
			if ( !isFileInSvnEntries( ( *it ).fileName(),( *it ).directory() + "/.svn/entries" ) && !isFileInExternals ( ( *it ).fileName(), ( *it ).directory()+"/.svn/dir-props" )  )
				return false;
		}
	}
	return true;
}

bool KSvnd::AreAllFilesNotInSvn( const KURL::List& wclist ) {
	for ( QValueListConstIterator<KURL> it = wclist.begin(); it != wclist.end() ; ++it ) {
		kdDebug( 7128 ) << "Checking file " << ( *it ) << endl;
		QDir bdir ( ( *it ).path() );
		if ( bdir.exists() && QFile::exists( ( *it ).path() + "/.svn/entries" ) ) {
			return false;
		} else if ( !bdir.exists() ) {
			if ( isFileInSvnEntries( ( *it ).fileName(),( *it ).directory() + "/.svn/entries" ) || isFileInExternals ( ( *it ).fileName(), ( *it ).directory()+"/.svn/dir-props" ) )
				return false;
		}
	}
	return true;
}

bool KSvnd::isFileInSvnEntries ( const QString filename, const QString entfile ) {
	QFile file( entfile );
	if ( file.open( IO_ReadOnly ) ) {
		QTextStream stream( &file );
		QString line;
		while ( !stream.atEnd() ) {
			line = stream.readLine().simplifyWhiteSpace();
			if ( line == "name=\""+ filename + "\"" ) {
				file.close();
				return true;
			}
		}
		file.close();
	}
	return false;
}

bool KSvnd::isFileInExternals ( const QString filename, const QString propfile ) {
	QFile file( propfile );
	if ( file.open( IO_ReadOnly ) ) {
		QTextStream stream( &file );
		QStringList line;
		while ( !stream.atEnd() )
			line << stream.readLine().simplifyWhiteSpace();
		for ( uint i = 0 ; i < line.count(); i++ ) {
			if ( line[ i ] == "K 13"  && line[ i+1 ] == "svn:externals" ) { //Key 13 : svn:externals
				//next line should be "V xx"
				if ( line [ i+2 ].startsWith( "V " ) ) {
					//ok browse the values now
					i+=2;
					while ( i < line.count() ) {
						if ( line[ i ].startsWith( filename+" " ) ) { //found it !
							file.close( );
							return true;
						} else if ( line[ i ].isEmpty() ) {
							file.close( );
							return false; //we are out of svn:externals now...
						}
						i++;
					}
				}
			}
		}
		file.close();
	}
	return false;
}

bool KSvnd::anyNotValidWorkingCopy( const KURL::List& wclist ) {
	bool result = true; //one negative match is enough
	for ( QValueListConstIterator<KURL> it = wclist.begin(); it != wclist.end() ; ++it ) {
		//exception for .svn dirs
		if ( ( *it ).path(-1).endsWith( "/.svn" ) )
			return true;
		//if is a directory check whether it contains a .svn/entries file
		QDir dir( ( *it ).path() );
		if ( dir.exists() ) { //it's a dir
			if ( !QFile::exists( ( *it ).path() + "/.svn/entries" ) )
				result = false;
		}

		//else check if ./.svn/entries exists
		if ( !QFile::exists( ( *it ).directory() + "/.svn/entries" ) )
			result = false;
	}
	return result;
}

bool KSvnd::anyValidWorkingCopy( const KURL::List& wclist ) {
	for ( QValueListConstIterator<KURL> it = wclist.begin(); it != wclist.end() ; ++it ) {
		//skip .svn dirs
		if ( ( *it ).path(-1).endsWith( "/.svn" ) )
			continue;
		//if is a directory check whether it contains a .svn/entries file
		QDir dir( ( *it ).path() );
		if ( dir.exists() ) { //it's a dir
			if ( QFile::exists( ( *it ).path() + "/.svn/entries" ) )
				return true;
		}

		//else check if ./.svn/entries exists
		if ( QFile::exists( ( *it ).directory() + "/.svn/entries" ) )
			return true;
	}
	return false;
}

int KSvnd::getStatus( const KURL::List& list ) {
	int result = 0;
	uint files = 0, folders = 0, parentsentries = 0, parentshavesvn = 0, subdirhavesvn = 0, external = 0;
	for ( QValueListConstIterator<KURL> it = list.begin(); it != list.end() ; ++it ) {
		if ( isFolder ( ( *it ) ) ) {
			folders++;
		} else {
			files++;
		}
		if ( isFileInSvnEntries ( (*it).filename(),( *it ).directory() + "/.svn/entries" ) ) { // normal subdir known in the working copy
			parentsentries++;
		} else if ( isFolder( *it ) ) { // other subfolders (either another module checkouted or an external, or something not known at all)
			if ( QFile::exists( ( *it ).path() + "/.svn/entries" ) ) 
				subdirhavesvn++;
			if ( isFileInExternals( (*it).filename(), ( *it ).directory() + "/.svn/dir-props" ) ) {
				external++;
			}
		}
		if ( ( isFolder( ( *it ) ) && QFile::exists( ( *it ).directory() + "../.svn/entries" ) ) || QFile::exists( ( *it ).directory() + "/.svn/entries" ) ) //parent has a .svn ?
			parentshavesvn++;
	}
	if ( files > 0 ) 
		result |= SomeAreFiles;
	if ( folders == list.count() ) {
		result |= AllAreFolders;
		result |= SomeAreFolders;
	}
	if ( folders > 0 )
		result |= SomeAreFolders;
	if ( parentsentries == list.count() ) {
		result |= AllAreInParentsEntries;
		result |= SomeAreInParentsEntries;
	} else if ( parentsentries != 0 )
		result |= SomeAreInParentsEntries;
	if ( parentshavesvn == list.count() ) {
		result |= AllParentsHaveSvn;
		result |= SomeParentsHaveSvn;
	} else if ( parentshavesvn > 0 )
		result |= SomeParentsHaveSvn;
	if ( subdirhavesvn == list.count() ) {
		result |= AllHaveSvn;
		result |= SomeHaveSvn;
	} else if ( subdirhavesvn > 0 )
		result |= SomeHaveSvn;
	if ( external == list.count() ) {
		result |= AllAreExternalToParent;
		result |= SomeAreExternalToParent;
	} else if ( external > 0 )
		result |= SomeAreExternalToParent;
	
	return result;
}

bool KSvnd::isFolder( const KURL& url ) {
	QDir d( url.path() );
	return d.exists();
}

QStringList KSvnd::getActionMenu ( const KURL::List &list ) {
	QStringList result;
	int listStatus = getStatus( list );

	if ( !(listStatus & SomeAreInParentsEntries) &&
	     !(listStatus & SomeAreExternalToParent) &&
	     !(listStatus & SomeHaveSvn)) {
		if( list.size() == 1 && listStatus & SomeAreFolders) {
			result << "Checkout";
			result << "Export";
//			result << "CreateRepository";
			result << "Import";
		}
	} else if ( (listStatus & AllAreInParentsEntries) ) {
		result << "Diff";
		//In SVN
//		result << "ShowLog";
//		result << "CheckForModifications";
//		result << "RevisionGraph";
//		result << "_SEPARATOR_";
//		result << "Update to revision..."
		result << "Rename";
		result << "Delete";
		if( listStatus & SomeAreFolders && !(listStatus & SomeAreFiles)) {
			result << "Revert";
//			result << "Cleanup";
		}
		result << "_SEPARATOR_";
//		result << "BranchTag";
		result << "Switch";
		result << "Merge";	
		if( listStatus & SomeAreFolders && !(listStatus & SomeAreFiles)) {
//			result << "Export";
//			result << "Relocate";
			result << "_SEPARATOR_";
			result << "Add";		
		}
		result << "_SEPARATOR_";
		if( listStatus & SomeAreFiles && !(listStatus & SomeAreFolders)) {
			result << "Blame";
		}
		result << "CreatePatch";
		
		if( list.size() == 1 && listStatus & SomeAreFolders) {
//			result << "ApplyPatchToFolder";
		}
	}
	return result;
}

QStringList KSvnd::getTopLevelActionMenu ( const KURL::List &list ) {
	QStringList result;
	int listStatus = getStatus( list );

	
	if ( ( listStatus & AllParentsHaveSvn &&
			( ( listStatus & SomeAreExternalToParent ) || ( listStatus & SomeAreInParentsEntries ) ) 
				|| ( listStatus & SomeHaveSvn ) )
		) {
		result << "Update";
		result << "Commit";
	}

	return result;
}

#if 0
void KSvnd::notify(const QString& path, int action, int kind, const QString& mime_type, int content_state, int prop_state, long int revision, const QString& userstring) {
	kdDebug(7128) << "KDED/Subversion : notify " << path << " action : " << action << " mime_type : " << mime_type << " content_state : " << content_state << " prop_state : " << prop_state << " revision : " << revision << " userstring : " << userstring << endl; 
	QByteArray params;

	QDataStream stream(params, IO_WriteOnly);
	stream << path << action << kind << mime_type << content_state << prop_state << revision << userstring;

	emitDCOPSignal( "subversionNotify(QString,int,int,QString,int,int,long int,QString)", params );
}

void KSvnd::status(const QString& path, int text_status, int prop_status, int repos_text_status, int repos_prop_status, long int rev ) {
	kdDebug(7128) << "KDED/Subversion : status " << path << " " << text_status << " " << prop_status << " "
			<< repos_text_status << " " << repos_prop_status << " " << rev << endl;
	QByteArray params;

	QDataStream stream(params, IO_WriteOnly);
	stream << path << text_status << prop_status << repos_text_status << repos_prop_status << rev;

	emitDCOPSignal( "subversionStatus(QString,int,int,int,int,long int)", params );
}

void KSvnd::popupMessage( const QString& message ) {
	kdDebug(7128) << "KDED/Subversion : popupMessage" << message << endl;
	KMessageBox::information(0, message, i18n( "Subversion" ) );
}
#endif

#include "ksvnd.moc"