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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
|
/*
Copyright 2004 Jonathan Riddell <jr@jriddell.org>
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 Steet, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "gvimagepart.moc"
#include <tqapplication.h>
#include <tqcursor.h>
#include <tqfile.h>
#include <tqpoint.h>
#include <tdeaction.h>
#include <tdeapplication.h>
#include <tdeconfig.h>
#include <kdebug.h>
#include <kdirlister.h>
#include <tdefiledialog.h>
#include <kiconloader.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <kmimetype.h>
#include <tdeparts/genericfactory.h>
#include <gvcore/cache.h>
#include <gvcore/document.h>
#include <gvcore/fileoperation.h>
#include <gvcore/printdialog.h>
#include <gvcore/imageview.h>
#include <gvcore/imageloader.h>
#include <gvcore/mimetypeutils.h>
#include "config.h"
namespace Gwenview {
// For now let's duplicate
const char CONFIG_CACHE_GROUP[]="cache";
#undef ENABLE_LOG
#undef LOG
#define ENABLE_LOG
#ifdef ENABLE_LOG
#define LOG(x) kdDebug() << k_funcinfo << x << endl
#else
#define LOG(x) ;
#endif
static bool storeData(TQWidget* parent, TQFile* file, const TQByteArray& data) {
uint sizeWritten = file->writeBlock(data);
if (sizeWritten != data.size()) {
KMessageBox::error(
parent,
i18n("Could not save image to a temporary file"));
return false;
}
return true;
}
//Factory Code
typedef KParts::GenericFactory<GVImagePart> GVImageFactory;
K_EXPORT_COMPONENT_FACTORY( libgvimagepart /*library name*/, GVImageFactory )
GVImagePart::GVImagePart(TQWidget* parentWidget, const char* /*widgetName*/, TQObject* parent,
const char* name, const TQStringList &)
: KParts::ReadOnlyPart( parent, name )
, mPrefetch( NULL )
, mLastDirection( DirectionUnknown ) {
GVImageFactory::instance()->iconLoader()->addAppDir( "gwenview");
setInstance( GVImageFactory::instance() );
TDEGlobal::locale()->insertCatalogue("gwenview");
TDEGlobal::locale()->setActiveCatalogue("gwenview");
mBrowserExtension = new GVImagePartBrowserExtension(this);
// Create the widgets
mDocument = new Document(this);
connect( mDocument, TQT_SIGNAL( loading()), TQT_SLOT( slotLoading()));
connect( mDocument, TQT_SIGNAL( loaded(const KURL&)), TQT_SLOT( slotLoaded(const KURL&)));
mImageView = new ImageView(parentWidget, mDocument, actionCollection());
connect( mImageView, TQT_SIGNAL(requestContextMenu(const TQPoint&)),
this, TQT_SLOT(openContextMenu(const TQPoint&)) );
setWidget(mImageView);
mDirLister = new KDirLister;
mDirLister->setAutoErrorHandlingEnabled( false, 0 );
mDirLister->setMainWindow(TDEApplication::kApplication()->mainWidget());
connect( mDirLister, TQT_SIGNAL( clear()), TQT_SLOT( dirListerClear()));
connect( mDirLister, TQT_SIGNAL( newItems( const KFileItemList& )),
TQT_SLOT( dirListerNewItems( const KFileItemList& )));
connect(mDirLister,TQT_SIGNAL(deleteItem(KFileItem*)),
TQT_SLOT(dirListerDeleteItem(KFileItem*)) );
TQStringList mimeTypes=MimeTypeUtils::rasterImageMimeTypes();
mDirLister->setMimeFilter(mimeTypes);
mPreviousImage=new TDEAction(i18n("&Previous Image"),
TQApplication::reverseLayout() ? "1rightarrow":"1leftarrow", Key_BackSpace,
this,TQT_SLOT(slotSelectPrevious()), actionCollection(), "previous");
mNextImage=new TDEAction(i18n("&Next Image"),
TQApplication::reverseLayout() ? "1leftarrow":"1rightarrow", Key_Space,
this,TQT_SLOT(slotSelectNext()), actionCollection(), "next");
updateNextPrevious();
KStdAction::saveAs( this, TQT_SLOT(saveAs()), actionCollection(), "saveAs" );
new TDEAction(i18n("Rotate &Left"), "object-rotate-left", CTRL + Key_L, this, TQT_SLOT(rotateLeft()), actionCollection(), "rotate_left");
new TDEAction(i18n("Rotate &Right"), "object-rotate-right", CTRL + Key_R, this, TQT_SLOT(rotateRight()), actionCollection(), "rotate_right");
setXMLFile( "gvimagepart/gvimagepart.rc" );
}
GVImagePart::~GVImagePart() {
delete mDirLister;
}
void GVImagePart::partActivateEvent(KParts::PartActivateEvent* event) {
if (event->activated()) {
TDEConfig* config=new TDEConfig("gwenviewrc");
Cache::instance()->readConfig(config,CONFIG_CACHE_GROUP);
delete config;
}
KParts::ReadOnlyPart::partActivateEvent( event );
}
void GVImagePart::guiActivateEvent( KParts::GUIActivateEvent* event) {
// Stolen from TDEHTMLImage
//
// prevent the base implementation from emitting setWindowCaption with
// our url. It destroys our pretty, previously caption. Konq saves/restores
// the caption for us anyway.
if (event->activated()) {
return;
}
KParts::ReadOnlyPart::guiActivateEvent(event);
}
TDEAboutData* GVImagePart::createAboutData() {
TDEAboutData* aboutData = new TDEAboutData( "gvimagepart", I18N_NOOP("GVImagePart"),
"0.1", I18N_NOOP("Image Viewer"),
TDEAboutData::License_GPL,
"(c) 2004, Jonathan Riddell <jr@jriddell.org>");
return aboutData;
}
bool GVImagePart::openURL(const KURL& url) {
if (!url.isValid()) {
return false;
}
KURL oldURLDir = m_url;
oldURLDir.setFileName( TQString() );
KURL newURLDir = url;
newURLDir.setFileName( TQString() );
bool sameDir = oldURLDir == newURLDir;
m_url = url;
emit started( 0 );
if( mDocument->url() == url ) { // reload button in Konqy - setURL would return immediately
mDocument->reload();
} else {
mDocument->setURL(url);
}
if( !sameDir ) {
mDirLister->openURL(mDocument->dirURL());
mLastDirection = DirectionUnknown;
}
return true;
}
TQString GVImagePart::filePath() {
return m_file;
}
void GVImagePart::slotLoading() {
emit setWindowCaption(mDocument->url().filename() + " - " + i18n("Loading..."));
// Set the location bar URL because we can arrive here if the user click on
// previous/next, which do not use openURLRequest
emit mBrowserExtension->setLocationBarURL(mDocument->url().pathOrURL());
updateNextPrevious();
}
void GVImagePart::slotLoaded(const KURL& url) {
TQString caption = url.filename() + TQString(" - %1x%2").arg(mDocument->width()).arg(mDocument->height());
emit setWindowCaption(caption);
emit completed();
emit setStatusBarText(i18n("Done."));
prefetchDone();
mPrefetch = ImageLoader::loader( mLastDirection == DirectionPrevious ? previousURL() : nextURL(),
this, BUSY_PRELOADING );
connect( mPrefetch, TQT_SIGNAL( imageLoaded( bool )), TQT_SLOT( prefetchDone()));
}
void GVImagePart::prefetchDone() {
if( mPrefetch != NULL ) {
mPrefetch->release( this );
}
mPrefetch = NULL;
}
void GVImagePart::print() {
KPrinter printer;
printer.setDocName( m_url.filename() );
KPrinter::addDialogPage( new PrintDialogPage( mDocument, mImageView, "GV page"));
if (printer.setup(mImageView, TQString(), true)) {
mDocument->print(&printer);
}
}
void GVImagePart::rotateLeft() {
mDocument->transform(ImageUtils::ROT_270);
}
void GVImagePart::rotateRight() {
mDocument->transform(ImageUtils::ROT_90);
}
void GVImagePart::dirListerClear() {
mImagesInDirectory.clear();
updateNextPrevious();
}
void GVImagePart::dirListerNewItems( const KFileItemList& list ) {
TQPtrListIterator<KFileItem> it(list);
for( ; it.current(); ++it ) {
mImagesInDirectory.append( (*it)->name());
}
qHeapSort( mImagesInDirectory );
updateNextPrevious();
}
void GVImagePart::dirListerDeleteItem( KFileItem* item ) {
mImagesInDirectory.remove( item->name());
updateNextPrevious();
}
void GVImagePart::updateNextPrevious() {
TQStringList::ConstIterator current = mImagesInDirectory.find( mDocument->filename());
if( current == mImagesInDirectory.end()) {
mNextImage->setEnabled( false );
mPreviousImage->setEnabled( false );
return;
}
mPreviousImage->setEnabled( current != mImagesInDirectory.begin());
++current;
mNextImage->setEnabled( current != mImagesInDirectory.end());
}
KURL GVImagePart::nextURL() const {
TQStringList::ConstIterator current = mImagesInDirectory.find( mDocument->filename());
if( current == mImagesInDirectory.end()) {
return KURL();
}
++current;
if( current == mImagesInDirectory.end()) {
return KURL();
}
KURL newURL = mDocument->dirURL();
newURL.setFileName( *current );
return newURL;
}
void GVImagePart::slotSelectNext() {
KURL newURL = nextURL();
if( newURL.isEmpty()) return;
mLastDirection = DirectionNext;
// Do not use mBrowserExtension->openURLRequest to avoid switching to
// another KPart
openURL(newURL);
emit mBrowserExtension->openURLNotify();
}
KURL GVImagePart::previousURL() const {
TQStringList::ConstIterator current = mImagesInDirectory.find( mDocument->filename());
if( current == mImagesInDirectory.end() || current == mImagesInDirectory.begin()) {
return KURL();
}
--current;
KURL newURL = mDocument->dirURL();
newURL.setFileName( *current );
return newURL;
}
void GVImagePart::slotSelectPrevious() {
KURL newURL = previousURL();
if( newURL.isEmpty()) return;
mLastDirection = DirectionPrevious;
openURL(newURL);
emit mBrowserExtension->openURLNotify();
}
void GVImagePart::saveAs() {
if (!mDocument->isModified()) {
saveOriginalAs();
return;
}
if (mDocument->canBeSaved()) {
mDocument->saveAs();
return;
}
KGuiItem saveItem(i18n("&Save Original"), "document-save-as");
int result = KMessageBox::warningContinueCancel(
widget(),
i18n("Gwenview KPart can't save the modifications you made. Do you want to save the original image?"),
i18n("Warning"),
saveItem);
if (result == KMessageBox::Cancel) return;
saveOriginalAs();
}
void GVImagePart::showJobError(TDEIO::Job* job) {
if (job->error() != 0) {
job->showErrorDialog(widget());
}
}
void GVImagePart::saveOriginalAs() {
KURL srcURL = mDocument->url();
KURL dstURL = KFileDialog::getSaveURL(
srcURL.fileName(),
TQString(),
widget());
if (!dstURL.isValid()) return;
// Try to get data from the cache to avoid downloading the image again.
TQByteArray data = Cache::instance()->file(srcURL);
if (data.size() == 0) {
// We need to read the image again. Let TDEIO::copy do the work.
TDEIO::Job* job = TDEIO::copy(srcURL, dstURL);
job->setWindow(widget());
connect(job, TQT_SIGNAL(result(TDEIO::Job*)),
this, TQT_SLOT(showJobError(TDEIO::Job*)) );
return;
}
if (dstURL.isLocalFile()) {
// Destination is a local file, store it ourself
TQString path = dstURL.path();
TQFile file(path);
if (!file.open(IO_WriteOnly)) {
KMessageBox::error(
widget(),
i18n("Could not open '%1' for writing.").arg(path));
return;
}
storeData(widget(), &file, data);
return;
}
// We need to send the data to a remote location
new DataUploader(widget(), data, dstURL);
}
DataUploader::DataUploader(TQWidget* dialogParent, const TQByteArray& data, const KURL& dstURL)
: mDialogParent(dialogParent)
{
mTempFile.setAutoDelete(true);
// Store it in a temp file
if (! storeData(dialogParent, mTempFile.file(), data) ) return;
// Now upload it
KURL tmpURL;
tmpURL.setPath(mTempFile.name());
TDEIO::Job* job = TDEIO::copy(tmpURL, dstURL);
job->setWindow(dialogParent);
connect(job, TQT_SIGNAL(result(TDEIO::Job*)),
this, TQT_SLOT(slotJobFinished(TDEIO::Job*)) );
}
void DataUploader::slotJobFinished(TDEIO::Job* job) {
if (job->error() != 0) {
job->showErrorDialog(mDialogParent);
}
delete this;
}
/**
* Overload KXMLGUIClient so that we can call setXML
*/
class PopupGUIClient : public KXMLGUIClient {
public:
PopupGUIClient( TDEInstance *inst, const TQString &doc ) {
setInstance( inst );
setXML( doc );
}
};
void GVImagePart::openContextMenu(const TQPoint& pos) {
TQString doc = KXMLGUIFactory::readConfigFile( "gvimagepartpopup.rc", true, instance() );
PopupGUIClient guiClient(instance(), doc);
KStdAction::saveAs( this, TQT_SLOT(saveAs()), guiClient.actionCollection(), "saveAs" );
KParts::URLArgs urlArgs;
urlArgs.serviceType = mDocument->mimeType();
KParts::BrowserExtension::PopupFlags flags =
KParts::BrowserExtension::ShowNavigationItems
| KParts::BrowserExtension::ShowUp
| KParts::BrowserExtension::ShowReload;
emit mBrowserExtension->popupMenu(&guiClient, pos, m_url, urlArgs, flags, S_IFREG);
}
/***** GVImagePartBrowserExtension *****/
GVImagePartBrowserExtension::GVImagePartBrowserExtension(GVImagePart* viewPart, const char* name)
:KParts::BrowserExtension(viewPart, name) {
mGVImagePart = viewPart;
emit enableAction("print", true );
}
GVImagePartBrowserExtension::~GVImagePartBrowserExtension() {
}
void GVImagePartBrowserExtension::print() {
mGVImagePart->print();
}
} // namespace
|