summaryrefslogtreecommitdiffstats
path: root/src/gvcore/fileopobject.cpp
blob: fe1dde6888b36ae42ebe3b1671ddbf08a8ad3ecb (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
// vim: set tabstop=4 shiftwidth=4 noexpandtab
/*
Gwenview - A simple image viewer for TDE
Copyright 2000-2004 Aur�ien G�eau

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.

*/

// TQt
#include <tqfile.h>
#include <tqstylesheet.h>
#include <tqwidget.h>

// KDE
#include <tdeversion.h>
#include <tdefiledialog.h>
#include <tdefilefiltercombo.h>
#include <tdeglobalsettings.h>
#include <klineedit.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <kstandarddirs.h>
#include <kurlcombobox.h>

// Local
#include "deletedialog.h"
#include "fileoperation.h"
#include "fileopobject.moc"
#include "fileoperationconfig.h"
#include "inputdialog.h"
namespace Gwenview {


/**
 * A tweaked KFileDialog used to select an existing directory. More efficient
 * than KDirSelectDialog, since it provides access to bookmarks and let you
 * create a dir.
 */
class DirSelectDialog : public KFileDialog {
public:
	DirSelectDialog(const TQString& startDir, TQWidget* parent)
	: KFileDialog(startDir, TQString(), parent, "dirselectdialog", true) {
		locationEdit->setEnabled(false);
		filterWidget->setEnabled(false);
		setMode(KFile::Directory | KFile::ExistingOnly);

		// Cast to avoid gcc being confused
		setPreviewWidget(static_cast<KPreviewWidgetBase*>(0));
	}
};


//-FileOpObject--------------------------------------------------------------------
FileOpObject::FileOpObject(const KURL& url,TQWidget* parent)
: mParent(parent)
{
	mURLList.append(url);
}


FileOpObject::FileOpObject(const KURL::List& list,TQWidget* parent)
: mParent(parent), mURLList(list)
{}


void FileOpObject::slotResult(TDEIO::Job* job) {
	if (job->error()) {
		job->showErrorDialog(mParent);
	}

	emit success();

// Let's shoot ourself in the foot...
	delete this;
}


void FileOpObject::polishJob(TDEIO::Job* job) {
	job->setWindow(mParent->topLevelWidget());
	connect( job, TQT_SIGNAL( result(TDEIO::Job*) ),
		this, TQT_SLOT( slotResult(TDEIO::Job*) ) );
}


//-FileOpCopyToObject--------------------------------------------------------------


void FileOpCopyToObject::operator()() {
	KURL destURL;

	if (FileOperationConfig::confirmCopy()) {
		TQString destDir = FileOperationConfig::destDir();
		if( !destDir.isEmpty()) {
			destDir += "/";
		}
		if (mURLList.size()==1) {
			destURL=KFileDialog::getSaveURL(destDir + mURLList.first().fileName(),
					TQString(), mParent, i18n("Copy File"));
		} else {
			DirSelectDialog dialog(destDir, mParent);
			dialog.setCaption(i18n("Select Folder Where Files Will be Copied"));
			dialog.exec();
			destURL=dialog.selectedURL();
		}
	} else {
		destURL.setPath(FileOperationConfig::destDir());
	}
	if (destURL.isEmpty()) return;

// Copy the file
	TDEIO::Job* job=TDEIO::copy(mURLList,destURL,true);
	polishJob(job);

}


//-FileOpCopyToObject--------------------------------------------------------------


void FileOpLinkToObject::operator()() {
	KURL destURL;

	if (FileOperationConfig::confirmCopy()) {
		TQString destDir = FileOperationConfig::destDir();
		if( !destDir.isEmpty()) {
			destDir += "/";
		}
		if (mURLList.size()==1) {
			destURL=KFileDialog::getSaveURL(destDir + mURLList.first().fileName(),
					TQString(), mParent, i18n("Link File"));
		} else {
			DirSelectDialog dialog(destDir, mParent);
			dialog.setCaption(i18n("Select Folder Where the Files Will be Linked"));
			dialog.exec();
			destURL=dialog.selectedURL();
		}
	} else {
		destURL.setPath(FileOperationConfig::destDir());
	}
	if (destURL.isEmpty()) return;

// Copy the file
	TDEIO::Job* job=TDEIO::link(mURLList,destURL,true);
	polishJob(job);
}


//-FileOpMoveToObject--------------------------------------------------------------
void FileOpMoveToObject::operator()() {
	KURL destURL;

	if (FileOperationConfig::confirmMove()) {
		TQString destDir = FileOperationConfig::destDir();
		if( !destDir.isEmpty()) {
			destDir += "/";
		}
		if (mURLList.size()==1) {
			destURL=KFileDialog::getSaveURL(destDir + mURLList.first().fileName(),
					TQString(), mParent, i18n("Move File"));
		} else {
			DirSelectDialog dialog(destDir, mParent);
			dialog.setCaption(i18n("Select Folder Where Files Will be Moved"));
			dialog.exec();
			destURL=dialog.selectedURL();
		}
	} else {
		destURL.setPath(FileOperationConfig::destDir());
	}
	if (destURL.isEmpty()) return;

// Move the file
	TDEIO::Job* job=TDEIO::move(mURLList,destURL,true);
	polishJob(job);
}


//-FileOpMakeDirObject-------------------------------------------------------------
void FileOpMakeDirObject::operator()() {
	InputDialog dlg(mParent);
	dlg.setCaption( i18n("Creating Folder") );
	dlg.setLabel( i18n("Enter the name of the new folder:") );
	dlg.setButtonOK( KGuiItem(i18n("Create Folder"), "folder-new") );
	if (!dlg.exec()) return;
	
	TQString newDir = dlg.lineEdit()->text();

	KURL newURL(mURLList.first());
	newURL.addPath(newDir);
    TDEIO::Job* job=TDEIO::mkdir(newURL);
	polishJob(job);
}


static TDEIO::Job* createTrashJob(KURL::List lst) {
	KURL trashURL("trash:/");
	// Go do it
	if (lst.count()==1) {
		// If there's only one file, TDEIO::move will think we want to overwrite
		// the trash dir with the file to trash, so we add the file name
		trashURL.addPath(lst.first().fileName());
	}
	return TDEIO::move(lst, trashURL);
}

static TDEIO::Job* createDeleteJob(KURL::List lst) {
	return TDEIO::del(lst, false, true);
}


//-FileOpDelObject-----------------------------------------------------------------
void FileOpDelObject::operator()() {
	bool shouldDelete;
	if (FileOperationConfig::confirmDelete()) {
		DeleteDialog dlg(mParent);
		dlg.setURLList(mURLList);
		if (!dlg.exec()) return;
		shouldDelete = dlg.shouldDelete();
	} else {
		shouldDelete = not FileOperationConfig::deleteToTrash();
	}
		

	TDEIO::Job* job;
	if (shouldDelete) {
		job = createDeleteJob(mURLList);
	} else {
		job = createTrashJob(mURLList);
	}
	polishJob(job);
}


//-FileOpTrashObject---------------------------------------------------------------
void FileOpTrashObject::operator()() {
	// Confirm operation
	if (FileOperationConfig::confirmDelete()) {
		int response;
		if (mURLList.count()>1) {
			TQStringList fileList;
			KURL::List::ConstIterator it=mURLList.begin();
			for (; it!=mURLList.end(); ++it) {
				fileList.append((*it).filename());
			}
			response=KMessageBox::warningContinueCancelList(mParent,
				i18n("Do you really want to trash these files?"),fileList,i18n("Trash used as a verb", "Trash Files"),KGuiItem(i18n("Trash used as a verb", "&Trash"),"edittrash"));
		} else {
			TQString filename=TQStyleSheet::escape(mURLList.first().filename());
			response=KMessageBox::warningContinueCancel(mParent,
				i18n("<p>Do you really want to move <b>%1</b> to the trash?</p>").arg(filename),i18n("Trash used as a verb", "Trash File"),KGuiItem(i18n("Trash used as a verb", "&Trash"),"edittrash"));
		}
		if (response!=KMessageBox::Continue) return;
	}

	TDEIO::Job* job = createTrashJob(mURLList);
	polishJob(job);
}

//-FileOpRealDeleteObject----------------------------------------------------------
void FileOpRealDeleteObject::operator()() {
	// Confirm operation
	if (FileOperationConfig::confirmDelete()) {
		int response;
		if (mURLList.count()>1) {
			TQStringList fileList;
			KURL::List::ConstIterator it=mURLList.begin();
			for (; it!=mURLList.end(); ++it) {
				fileList.append((*it).filename());
			}
			response=KMessageBox::warningContinueCancelList(mParent,
				i18n("Do you really want to delete these files?"),fileList,
				i18n("Delete Files"),
				KStdGuiItem::del()
				);
		} else {
			TQString filename=TQStyleSheet::escape(mURLList.first().filename());
			response=KMessageBox::warningContinueCancel(mParent,
				i18n("<p>Do you really want to delete <b>%1</b>?</p>").arg(filename),
				i18n("Delete File"),
				KStdGuiItem::del()
				);
		}
		if (response!=KMessageBox::Continue) return;
	}

	// Delete the file
	TDEIO::Job* job = createDeleteJob(mURLList);
	polishJob(job);
}


//-FileOpRenameObject--------------------------------------------------------------
void FileOpRenameObject::operator()() {
	KURL srcURL=mURLList.first();

	// Prompt for the new filename
	TQString filename = srcURL.filename();
	InputDialog dlg(mParent);
	dlg.setCaption(i18n("Renaming File"));
	dlg.setLabel(i18n("<p>Rename file <b>%1</b> to:</p>").arg(TQStyleSheet::escape(filename)));
	dlg.setButtonOK( KGuiItem(i18n("&Rename"), "edit") );

	dlg.lineEdit()->setText(filename);
	int extPos = filename.findRev('.');
	if (extPos != -1) {
		if (filename.mid(extPos - 4, 4) == ".tar") {
			// Special case: *.tar.*
			extPos -= 4;
		}
		dlg.lineEdit()->setSelection(0, extPos);
	}
	if (!dlg.exec()) return;
	mNewFilename = dlg.lineEdit()->text();

	// Rename the file
	KURL destURL=srcURL;
	destURL.setFileName(mNewFilename);
	TDEIO::Job* job=TDEIO::move(srcURL,destURL);
	polishJob(job);
}


void FileOpRenameObject::slotResult(TDEIO::Job* job) {
	if (job->error()) {
		job->showErrorDialog(mParent);
	}

	emit success();
	emit renamed(mNewFilename);

// Let's shoot ourself in the foot...
	delete this;
}

} // namespace