this is KDE 4.3.0 here now. Fix ABI and API
[kdegraphics.git] / ksnapshot / ksnapshotobject.cpp
blob6a6378f1b616eb4a8c27e340a65e86b4073444b8
1 /*
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"
28 #include <fixx11h.h>
29 //kde include
30 #include <KMessageBox>
31 #include <KMimeType>
32 #include <KImageIO>
33 #include <klocale.h>
34 #include <KTemporaryFile>
35 #include <kio/netaccess.h>
36 #include <kdebug.h>
38 //Qt include
39 #include <QRegExp>
40 #include <QApplication>
41 #include <QImageWriter>
43 KSnapshotObject::KSnapshotObject()
47 KSnapshotObject::~KSnapshotObject()
49 delete grabber;
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 );
62 if (start != -1) {
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 );
70 else {
71 // no number
72 start = name.lastIndexOf('.');
73 if (start != -1) {
74 // has a . somewhere, e.g. it has an extension
75 name.insert(start, '1');
77 else {
78 // no extension, just tack it on to the end
79 name += '1';
83 //Rebuild the path
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 )
94 return;
96 filename = newURL;
97 refreshCaption();
101 bool KSnapshotObject::save( const QString &filename, QWidget* widget )
103 return save( KUrl( filename ), widget);
106 bool KSnapshotObject::save( const KUrl& url, QWidget *widget )
108 if ( KIO::NetAccess::exists( url, KIO::NetAccess::DestinationSide, widget ) ) {
109 const QString title = i18n( "File Exists" );
110 const QString text = i18n( "<qt>Do you really want to overwrite <b>%1</b>?</qt>" , url.prettyUrl());
111 if (KMessageBox::Continue != KMessageBox::warningContinueCancel( widget, text, title, KGuiItem(i18n("Overwrite")) ) )
113 return false;
116 return saveEqual( url,widget );
119 bool KSnapshotObject::saveEqual( const KUrl& url,QWidget *widget )
121 QByteArray type = "PNG";
122 QString mime = KMimeType::findByUrl( url.fileName(), 0, url.isLocalFile(), true )->name();
123 QStringList types = KImageIO::typeForMime(mime);
124 if ( !types.isEmpty() )
125 type = types.first().toLatin1();
127 bool ok = false;
129 if ( url.isLocalFile() ) {
130 QFile output( url.path() );
131 if ( output.open( QFile::WriteOnly ) )
132 ok = saveImage( &output, type );
134 else {
135 KTemporaryFile tmpFile;
136 if ( tmpFile.open() ) {
137 if ( saveImage( &tmpFile, type ) ) {
138 ok = KIO::NetAccess::upload( tmpFile.fileName(), url, widget );
143 QApplication::restoreOverrideCursor();
144 if ( !ok ) {
145 kWarning() << "KSnapshot was unable to save the snapshot" ;
147 QString caption = i18n("Unable to Save Image");
148 QString text = i18n("KSnapshot was unable to save the image to\n%1.", url.prettyUrl());
149 KMessageBox::error(widget, text, caption);
152 return ok;
155 bool KSnapshotObject::saveImage( QIODevice *device, const QByteArray &format )
157 QImageWriter imgWriter( device, format );
159 if ( !imgWriter.canWrite() ) {
160 kDebug() << "Cannot write format " << format;
161 return false;
164 // For jpeg use 85% quality not the default
165 if ( 0 == qstricmp(format.constData(), "jpeg") || 0 == qstricmp(format.constData(), "jpg") ) {
166 imgWriter.setQuality( 85 );
169 if ( !title.isEmpty() )
170 imgWriter.setText( i18n("Title"), title );
171 if ( !windowClass.isEmpty() )
172 imgWriter.setText( i18n("Window Class"), windowClass );
174 QImage snap = snapshot.toImage();
175 return imgWriter.write( snap );