2 * Copyright (C) 1997-2008 Richard J. Moore <rich@kde.org>
3 * Copyright (C) 2000 Matthias Ettrich <ettrich@troll.no>
4 * Copyright (C) 2002 Aaron J. Seigo <aseigo@kde.org>
5 * Copyright (C) 2003 Nadeem Hasan <nhasan@kde.org>
6 * Copyright (C) 2004 Bernd Brandstetter <bbrand@freenet.de>
7 * Copyright (C) 2006-2008 Urs Wolfer <uwolfer @ kde.org>
8 * Copyright (C) 2007 Montel Laurent <montel@kde.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
26 #include "ksnapshotobject.h"
30 #include <KMessageBox>
34 #include <KTemporaryFile>
35 #include <kio/netaccess.h>
40 #include <QApplication>
41 #include <QImageWriter>
43 KSnapshotObject::KSnapshotObject()
47 KSnapshotObject::~KSnapshotObject()
52 void KSnapshotObject::autoincFilename()
54 // Extract the filename from the path
55 QString name
= filename
.fileName();
57 // If the name contains a number then increment it
58 QRegExp
numSearch( "(^|[^\\d])(\\d+)" ); // we want to match as far left as possible, and when the number is at the start of the name
60 // Does it have a number?
61 int start
= numSearch
.lastIndexIn( name
);
63 // It has a number, increment it
64 start
= numSearch
.pos( 2 ); // we are only interested in the second group
65 QString numAsStr
= numSearch
.capturedTexts()[ 2 ];
66 QString number
= QString::number( numAsStr
.toInt() + 1 );
67 number
= number
.rightJustified( numAsStr
.length(), '0' );
68 name
.replace( start
, numAsStr
.length(), number
);
72 start
= name
.lastIndexOf('.');
74 // has a . somewhere, e.g. it has an extension
75 name
.insert(start
, '1');
78 // no extension, just tack it on to the end
84 KUrl newUrl
= filename
;
85 newUrl
.setFileName( name
);
86 changeUrl( newUrl
.url() );
90 void KSnapshotObject::changeUrl( const QString
&url
)
92 KUrl newURL
= KUrl( url
);
93 if ( newURL
== filename
)
101 // NOTE: widget == NULL if called from dbus interface
102 bool KSnapshotObject::save( const QString
&filename
, QWidget
* widget
)
104 return save( KUrl( filename
), widget
);
107 bool KSnapshotObject::save( const KUrl
& url
, QWidget
*widget
)
109 if ( KIO::NetAccess::exists( url
, KIO::NetAccess::DestinationSide
, widget
) ) {
110 // NOTE: widget == NULL if called from dbus interface
111 const QString title
= i18n( "File Exists" );
112 const QString text
= i18n( "<qt>Do you really want to overwrite <b>%1</b>?</qt>" , url
.prettyUrl());
113 if (KMessageBox::Continue
!= KMessageBox::warningContinueCancel( widget
, text
, title
, KGuiItem(i18n("Overwrite")) ) )
118 return saveEqual( url
,widget
);
121 bool KSnapshotObject::saveEqual( const KUrl
& url
,QWidget
*widget
)
123 QByteArray type
= "PNG";
124 QString mime
= KMimeType::findByUrl( url
.fileName(), 0, url
.isLocalFile(), true )->name();
125 QStringList types
= KImageIO::typeForMime(mime
);
126 if ( !types
.isEmpty() )
127 type
= types
.first().toLatin1();
131 if ( url
.isLocalFile() ) {
132 QFile
output( url
.path() );
133 if ( output
.open( QFile::WriteOnly
) )
134 ok
= saveImage( &output
, type
);
137 KTemporaryFile tmpFile
;
138 if ( tmpFile
.open() ) {
139 if ( saveImage( &tmpFile
, type
) ) {
140 ok
= KIO::NetAccess::upload( tmpFile
.fileName(), url
, widget
);
145 QApplication::restoreOverrideCursor();
147 kWarning() << "KSnapshot was unable to save the snapshot" ;
149 QString caption
= i18n("Unable to Save Image");
150 QString text
= i18n("KSnapshot was unable to save the image to\n%1.", url
.prettyUrl());
151 KMessageBox::error(widget
, text
, caption
);
157 bool KSnapshotObject::saveImage( QIODevice
*device
, const QByteArray
&format
)
159 QImageWriter
imgWriter( device
, format
);
161 if ( !imgWriter
.canWrite() ) {
162 kDebug() << "Cannot write format " << format
;
166 // For jpeg use 85% quality not the default
167 if ( 0 == qstricmp(format
.constData(), "jpeg") || 0 == qstricmp(format
.constData(), "jpg") ) {
168 imgWriter
.setQuality( 85 );
171 if ( !title
.isEmpty() )
172 imgWriter
.setText( i18n("Title"), title
);
173 if ( !windowClass
.isEmpty() )
174 imgWriter
.setText( i18n("Window Class"), windowClass
);
176 QImage snap
= snapshot
.toImage();
177 return imgWriter
.write( snap
);