~ubuntu-branches/ubuntu/edgy/gwenview/edgy

« back to all changes in this revision

Viewing changes to src/gvcore/fileopobject.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2005-12-06 17:59:19 UTC
  • mfrom: (1.1.3 upstream) (2.1.1 sarge)
  • Revision ID: james.westby@ubuntu.com-20051206175919-8yv9nfmw0pws6p52
Tags: 1.3.1-0ubuntu1
* Sync with Debian
* Edit kde.mk for .pot generation

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// vim: set tabstop=4 shiftwidth=4 noexpandtab
 
2
/*
 
3
Gwenview - A simple image viewer for KDE
 
4
Copyright 2000-2004 Aur�ien G�eau
 
5
 
 
6
This program is free software; you can redistribute it and/or
 
7
modify it under the terms of the GNU General Public License
 
8
as published by the Free Software Foundation; either version 2
 
9
of the License, or (at your option) any later version.
 
10
 
 
11
This program 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
 
14
GNU General Public License for more details.
 
15
 
 
16
You should have received a copy of the GNU General Public License
 
17
along with this program; if not, write to the Free Software
 
18
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
19
 
 
20
*/
 
21
 
 
22
// Qt
 
23
#include <qfile.h>
 
24
#include <qstylesheet.h>
 
25
#include <qwidget.h>
 
26
 
 
27
// KDE
 
28
#include <kdeversion.h>
 
29
#include <kfiledialog.h>
 
30
#include <kfilefiltercombo.h>
 
31
#include <kglobalsettings.h>
 
32
#include <kinputdialog.h>
 
33
#include <klocale.h>
 
34
#include <kmessagebox.h>
 
35
#include <kstandarddirs.h>
 
36
#include <kurlcombobox.h>
 
37
 
 
38
// Local
 
39
#include "fileoperation.h"
 
40
#include "fileopobject.moc"
 
41
namespace Gwenview {
 
42
 
 
43
 
 
44
/**
 
45
 * A tweaked KFileDialog used to select an existing directory. More efficient
 
46
 * than KDirSelectDialog, since it provides access to bookmarks and let you
 
47
 * create a dir.
 
48
 */
 
49
class DirSelectDialog : public KFileDialog {
 
50
public:
 
51
        DirSelectDialog(const QString& startDir, QWidget* parent)
 
52
        : KFileDialog(startDir, QString::null, parent, "dirselectdialog", true) {
 
53
                locationEdit->setEnabled(false);
 
54
                filterWidget->setEnabled(false);
 
55
                setMode(KFile::Directory | KFile::ExistingOnly);
 
56
 
 
57
                // Cast to avoid gcc being confused
 
58
                setPreviewWidget(static_cast<KPreviewWidgetBase*>(0));
 
59
        }
 
60
};
 
61
 
 
62
 
 
63
//-FileOpObject--------------------------------------------------------------------
 
64
FileOpObject::FileOpObject(const KURL& url,QWidget* parent)
 
65
: mParent(parent)
 
66
{
 
67
        mURLList.append(url);
 
68
}
 
69
 
 
70
 
 
71
FileOpObject::FileOpObject(const KURL::List& list,QWidget* parent)
 
72
: mParent(parent), mURLList(list)
 
73
{}
 
74
 
 
75
 
 
76
void FileOpObject::slotResult(KIO::Job* job) {
 
77
        if (job->error()) {
 
78
                job->showErrorDialog(mParent);
 
79
        }
 
80
 
 
81
        emit success();
 
82
 
 
83
// Let's shoot ourself in the foot...
 
84
        delete this;
 
85
}
 
86
 
 
87
 
 
88
//-FileOpCopyToObject--------------------------------------------------------------
 
89
 
 
90
 
 
91
void FileOpCopyToObject::operator()() {
 
92
        KURL destURL;
 
93
 
 
94
        if (FileOperation::confirmCopy()) {
 
95
                QString destDir = FileOperation::destDir();
 
96
                if( !destDir.isEmpty()) {
 
97
                        destDir += "/";
 
98
                }
 
99
                if (mURLList.size()==1) {
 
100
                        destURL=KFileDialog::getSaveURL(destDir + mURLList.first().fileName(),
 
101
                                        QString::null, mParent, i18n("Copy File"));
 
102
                } else {
 
103
                        DirSelectDialog dialog(destDir, mParent);
 
104
                        dialog.setCaption(i18n("Select Folder Where the Files Will be Copied"));
 
105
                        dialog.exec();
 
106
                        destURL=dialog.selectedURL();
 
107
                }
 
108
        } else {
 
109
                destURL.setPath(FileOperation::destDir());
 
110
        }
 
111
        if (destURL.isEmpty()) return;
 
112
 
 
113
// Copy the file
 
114
        KIO::Job* copyJob=KIO::copy(mURLList,destURL,true);
 
115
        copyJob->setWindow(mParent->topLevelWidget());
 
116
        connect( copyJob, SIGNAL( result(KIO::Job*) ),
 
117
                this, SLOT( slotResult(KIO::Job*) ) );
 
118
 
 
119
}
 
120
 
 
121
 
 
122
//-FileOpMoveToObject--------------------------------------------------------------
 
123
void FileOpMoveToObject::operator()() {
 
124
        KURL destURL;
 
125
 
 
126
        if (FileOperation::confirmMove()) {
 
127
                QString destDir = FileOperation::destDir();
 
128
                if( !destDir.isEmpty()) {
 
129
                        destDir += "/";
 
130
                }
 
131
                if (mURLList.size()==1) {
 
132
                        destURL=KFileDialog::getSaveURL(destDir + mURLList.first().fileName(),
 
133
                                        QString::null, mParent, i18n("Move File"));
 
134
                } else {
 
135
                        DirSelectDialog dialog(destDir, mParent);
 
136
                        dialog.setCaption(i18n("Select Folder Where the Files Will be Moved"));
 
137
                        dialog.exec();
 
138
                        destURL=dialog.selectedURL();
 
139
                }
 
140
        } else {
 
141
                destURL.setPath(FileOperation::destDir());
 
142
        }
 
143
        if (destURL.isEmpty()) return;
 
144
 
 
145
// Move the file
 
146
        KIO::Job* moveJob=KIO::move(mURLList,destURL,true);
 
147
        moveJob->setWindow(mParent->topLevelWidget());
 
148
        connect( moveJob, SIGNAL( result(KIO::Job*) ),
 
149
                this, SLOT( slotResult(KIO::Job*) ) );
 
150
 
 
151
}
 
152
 
 
153
 
 
154
//-FileOpTrashObject---------------------------------------------------------------
 
155
void FileOpTrashObject::operator()() {
 
156
#if KDE_IS_VERSION( 3, 3, 89 )
 
157
        KURL trashURL("trash:/");
 
158
#else
 
159
        KURL trashURL;
 
160
        // Get the trash path (and make sure it exists)
 
161
        QString trashPath=KGlobalSettings::trashPath();
 
162
        if ( !QFile::exists(trashPath) ) {
 
163
                KStandardDirs::makeDir( QFile::encodeName(trashPath) );
 
164
        }
 
165
        trashURL.setPath(trashPath);
 
166
 
 
167
        // Check we don't want to trash the trash
 
168
        KURL::List::ConstIterator it=mURLList.begin();
 
169
        for (; it!=mURLList.end(); ++it) {
 
170
                if ( (*it).isLocalFile() && (*it).path(1)==trashPath ) {
 
171
                        KMessageBox::sorry(0, i18n("You cannot trash the trash bin."));
 
172
                        return;
 
173
                }
 
174
        }
 
175
#endif
 
176
 
 
177
        // Confirm operation
 
178
        if (FileOperation::confirmDelete()) {
 
179
                int response;
 
180
                if (mURLList.count()>1) {
 
181
                        QStringList fileList;
 
182
                        KURL::List::ConstIterator it=mURLList.begin();
 
183
                        for (; it!=mURLList.end(); ++it) {
 
184
                                fileList.append((*it).filename());
 
185
                        }
 
186
                        response=KMessageBox::warningContinueCancelList(mParent,
 
187
                                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"));
 
188
                } else {
 
189
                        QString filename=QStyleSheet::escape(mURLList.first().filename());
 
190
                        response=KMessageBox::warningContinueCancel(mParent,
 
191
                                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"));
 
192
                }
 
193
                if (response!=KMessageBox::Continue) return;
 
194
        }
 
195
 
 
196
        // Go do it
 
197
        if (mURLList.count()==1) {
 
198
                // If there's only one file, KIO::move will think we want to overwrite
 
199
                // the trash dir with the file to trash, so we add the file name
 
200
                trashURL.addPath(mURLList.first().fileName());
 
201
        }
 
202
        KIO::Job* job=KIO::move(mURLList,trashURL);
 
203
        job->setWindow(mParent->topLevelWidget());
 
204
        connect( job, SIGNAL( result(KIO::Job*) ),
 
205
                this, SLOT( slotResult(KIO::Job*) ) );
 
206
}
 
207
 
 
208
//-FileOpRealDeleteObject----------------------------------------------------------
 
209
void FileOpRealDeleteObject::operator()() {
 
210
        // Confirm operation
 
211
        if (FileOperation::confirmDelete()) {
 
212
                int response;
 
213
                if (mURLList.count()>1) {
 
214
                        QStringList fileList;
 
215
                        KURL::List::ConstIterator it=mURLList.begin();
 
216
                        for (; it!=mURLList.end(); ++it) {
 
217
                                fileList.append((*it).filename());
 
218
                        }
 
219
                        response=KMessageBox::warningContinueCancelList(mParent,
 
220
                                i18n("Do you really want to delete these files?"),fileList,
 
221
                                i18n("Delete Files"),
 
222
#if KDE_IS_VERSION(3, 3, 0)
 
223
                                KStdGuiItem::del()
 
224
#else
 
225
                                KGuiItem( i18n( "&Delete" ), "editdelete", i18n( "Delete item(s)" ) )
 
226
#endif
 
227
                                );
 
228
                } else {
 
229
                        QString filename=QStyleSheet::escape(mURLList.first().filename());
 
230
                        response=KMessageBox::warningContinueCancel(mParent,
 
231
                                i18n("<p>Do you really want to delete <b>%1</b>?</p>").arg(filename),
 
232
                                i18n("Delete File"),
 
233
#if KDE_IS_VERSION(3, 3, 0)
 
234
                                KStdGuiItem::del()
 
235
#else
 
236
                                KGuiItem( i18n( "&Delete" ), "editdelete", i18n( "Delete item(s)" ) )
 
237
#endif
 
238
                                );
 
239
                }
 
240
                if (response!=KMessageBox::Continue) return;
 
241
        }
 
242
 
 
243
        // Delete the file
 
244
        KIO::Job* removeJob=KIO::del(mURLList,false,true);
 
245
        removeJob->setWindow(mParent->topLevelWidget());
 
246
        connect( removeJob, SIGNAL( result(KIO::Job*) ),
 
247
                this, SLOT( slotResult(KIO::Job*) ) );
 
248
}
 
249
 
 
250
 
 
251
//-FileOpRenameObject--------------------------------------------------------------
 
252
void FileOpRenameObject::operator()() {
 
253
        bool ok;
 
254
        KURL srcURL=mURLList.first();
 
255
 
 
256
// Prompt for the new filename
 
257
        QString filename=QStyleSheet::escape(srcURL.filename());
 
258
        mNewFilename=KInputDialog::getText(i18n("Renaming File"),
 
259
                i18n("<p>Rename file <b>%1</b> to:</p>").arg(filename),
 
260
                srcURL.filename(),
 
261
                &ok,mParent);
 
262
 
 
263
        if (!ok) return;
 
264
 
 
265
// Rename the file
 
266
        KURL destURL=srcURL;
 
267
        destURL.setFileName(mNewFilename);
 
268
        KIO::Job* job=KIO::move(srcURL,destURL);
 
269
        job->setWindow(mParent->topLevelWidget());
 
270
        connect( job, SIGNAL( result(KIO::Job*) ),
 
271
                this, SLOT( slotResult(KIO::Job*) ) );
 
272
}
 
273
 
 
274
 
 
275
void FileOpRenameObject::slotResult(KIO::Job* job) {
 
276
        if (job->error()) {
 
277
                job->showErrorDialog(mParent);
 
278
        }
 
279
 
 
280
        emit success();
 
281
        emit renamed(mNewFilename);
 
282
 
 
283
// Let's shoot ourself in the foot...
 
284
        delete this;
 
285
}
 
286
 
 
287
} // namespace