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> |
9 | * Copyright (C) 2010 Pau Garcia i Quiles <pgquiles@elpauer.org> |
10 | * |
11 | * This program is free software; you can redistribute it and/or modify |
12 | * it under the terms of the GNU Lesser General Public License as published by |
13 | * the Free Software Foundation; either version 2 of the License, or |
14 | * (at your option) any later version. |
15 | * |
16 | * This program is distributed in the hope that it will be useful, |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 | * GNU General Public License for more details. |
20 | * |
21 | * You should have received a copy of the GNU Lesser General Public License |
22 | * along with this program; if not, write to the Free Software |
23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
24 | * Boston, MA 02110-1301, USA. |
25 | */ |
26 | |
27 | #include "ksnapshotobject.h" |
28 | |
29 | #ifdef Q_WS_X11 |
30 | #include <fixx11h.h> |
31 | #endif |
32 | |
33 | //kde include |
34 | #include <KMessageBox> |
35 | #include <KMimeType> |
36 | #include <KImageIO> |
37 | #include <klocale.h> |
38 | #include <KTemporaryFile> |
39 | #include <kio/netaccess.h> |
40 | #include <kdebug.h> |
41 | |
42 | //Qt include |
43 | #include <QRegExp> |
44 | #include <QApplication> |
45 | #include <QImageWriter> |
46 | |
47 | KSnapshotObject::KSnapshotObject() |
48 | : rgnGrab( 0 ), |
49 | freeRgnGrab( 0 ), |
50 | grabber( 0 ) |
51 | { |
52 | } |
53 | |
54 | KSnapshotObject::~KSnapshotObject() |
55 | { |
56 | delete grabber; |
57 | } |
58 | |
59 | void KSnapshotObject::autoincFilename() |
60 | { |
61 | // Extract the filename from the path |
62 | QString name= filename.fileName(); |
63 | |
64 | // If the name contains a number then increment it |
65 | QRegExp numSearch( "(^|[^\\d])(\\d+)" ); // we want to match as far left as possible, and when the number is at the start of the name |
66 | |
67 | // Does it have a number? |
68 | int start = numSearch.lastIndexIn( name ); |
69 | if (start != -1) { |
70 | // It has a number, increment it |
71 | start = numSearch.pos( 2 ); // we are only interested in the second group |
72 | QString numAsStr = numSearch.capturedTexts()[ 2 ]; |
73 | QString number = QString::number( numAsStr.toInt() + 1 ); |
74 | number = number.rightJustified( numAsStr.length(), '0' ); |
75 | name.replace( start, numAsStr.length(), number ); |
76 | } |
77 | else { |
78 | // no number |
79 | start = name.lastIndexOf('.'); |
80 | if (start != -1) { |
81 | // has a . somewhere, e.g. it has an extension |
82 | name.insert(start, '1'); |
83 | } |
84 | else { |
85 | // no extension, just tack it on to the end |
86 | name += '1'; |
87 | } |
88 | } |
89 | |
90 | //Rebuild the path |
91 | KUrl newUrl = filename; |
92 | newUrl.setFileName( name ); |
93 | changeUrl( newUrl.url() ); |
94 | } |
95 | |
96 | |
97 | void KSnapshotObject::changeUrl( const QString &url ) |
98 | { |
99 | KUrl newURL = KUrl( url ); |
100 | if ( newURL == filename ) |
101 | return; |
102 | |
103 | filename = newURL; |
104 | refreshCaption(); |
105 | } |
106 | |
107 | |
108 | // NOTE: widget == NULL if called from dbus interface |
109 | bool KSnapshotObject::save( const QString &filename, QWidget* widget ) |
110 | { |
111 | return save( KUrl( filename ), widget); |
112 | } |
113 | |
114 | bool KSnapshotObject::save( const KUrl& url, QWidget *widget ) |
115 | { |
116 | if ( KIO::NetAccess::exists( url, KIO::NetAccess::DestinationSide, widget ) ) { |
117 | // NOTE: widget == NULL if called from dbus interface |
118 | const QString title = i18n( "File Exists" ); |
119 | const QString text = i18n( "<qt>Do you really want to overwrite <b>%1</b>?</qt>" , url.pathOrUrl()); |
120 | if (KMessageBox::Continue != KMessageBox::warningContinueCancel( widget, text, title, KGuiItem(i18n("Overwrite" )) ) ) |
121 | { |
122 | return false; |
123 | } |
124 | } |
125 | return saveEqual( url,widget ); |
126 | } |
127 | |
128 | bool KSnapshotObject::saveEqual( const KUrl& url,QWidget *widget ) |
129 | { |
130 | QByteArray type = "PNG" ; |
131 | QString mime = KMimeType::findByUrl( url.fileName(), 0, url.isLocalFile(), true )->name(); |
132 | const QStringList types = KImageIO::typeForMime(mime); |
133 | if ( !types.isEmpty() ) |
134 | type = types.first().toLatin1(); |
135 | |
136 | bool ok = false; |
137 | |
138 | if ( url.isLocalFile() ) { |
139 | QFile output( url.toLocalFile() ); |
140 | if ( output.open( QFile::WriteOnly ) ) |
141 | ok = saveImage( &output, type ); |
142 | } |
143 | else { |
144 | KTemporaryFile tmpFile; |
145 | if ( tmpFile.open() ) { |
146 | if ( saveImage( &tmpFile, type ) ) { |
147 | ok = KIO::NetAccess::upload( tmpFile.fileName(), url, widget ); |
148 | } |
149 | } |
150 | } |
151 | |
152 | QApplication::restoreOverrideCursor(); |
153 | if ( !ok ) { |
154 | kWarning() << "KSnapshot was unable to save the snapshot" ; |
155 | |
156 | const QString caption = i18n("Unable to Save Image" ); |
157 | const QString text = i18n("KSnapshot was unable to save the image to\n%1." , url.prettyUrl()); |
158 | KMessageBox::error(widget, text, caption); |
159 | } |
160 | |
161 | return ok; |
162 | } |
163 | |
164 | bool KSnapshotObject::saveImage( QIODevice *device, const QByteArray &format ) |
165 | { |
166 | QImageWriter imgWriter( device, format ); |
167 | |
168 | if ( !imgWriter.canWrite() ) { |
169 | kDebug() << "Cannot write format " << format; |
170 | return false; |
171 | } |
172 | |
173 | // For jpeg use 85% quality not the default |
174 | if ( 0 == qstricmp(format.constData(), "jpeg" ) || 0 == qstricmp(format.constData(), "jpg" ) ) { |
175 | imgWriter.setQuality( 85 ); |
176 | } |
177 | |
178 | if ( !title.isEmpty() ) |
179 | imgWriter.setText( i18n("Title" ), title ); |
180 | if ( !windowClass.isEmpty() ) |
181 | imgWriter.setText( i18n("Window Class" ), windowClass ); |
182 | |
183 | QImage snap = snapshot.toImage(); |
184 | return imgWriter.write( snap ); |
185 | } |
186 | |
187 | |