Show invite menu in wlm chat window immediately
[kdenetwork.git] / kopete / plugins / urlpicpreview / urlpicpreviewplugin.cpp
blob875bcf1fe1533a85c91d57162d1eaddec9994a44
1 /*
2 urlpicpreviewplugin.cpp
4 Copyright (c) 2005 by Heiko Schaefer <heiko@rangun.de>
6 Kopete (c) 2002-2007 by the Kopete developers <kopete-devel@kde.org>
8 **************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; version 2, or (at your option) version 3 *
13 * of the License. *
14 * *
15 **************************************************************************
18 // Qt
19 #include <qimage.h>
20 #include <qregexp.h>
22 // KDE
23 #include <kdebug.h>
24 #include <kimageio.h>
25 #include <ktemporaryfile.h>
26 #include <kapplication.h>
27 #include <kgenericfactory.h>
29 // KIO
30 #include <kio/netaccess.h>
32 // Kopete
33 #include "linkpreview.h"
34 #include "kopeteuiglobal.h"
35 #include "urlpicpreviewplugin.h"
36 #include "urlpicpreviewconfig.h"
37 #include "kopetechatsessionmanager.h"
39 K_PLUGIN_FACTORY( URLPicPreviewPluginFactory, registerPlugin<URLPicPreviewPlugin>(); )
40 K_EXPORT_PLUGIN( URLPicPreviewPluginFactory( "kopete_urlpicpreview" ) )
42 URLPicPreviewPlugin::URLPicPreviewPlugin ( QObject* parent, const QVariantList& /* args */ )
43 : Kopete::Plugin ( URLPicPreviewPluginFactory::componentData(), parent ), m_pic ( NULL ), m_abortMessageCheck ( false )
46 kDebug ( 14314 );
48 Kopete::ChatSessionManager * chatSessionManager = Kopete::ChatSessionManager::self();
49 connect ( chatSessionManager, SIGNAL ( aboutToDisplay ( Kopete::Message& ) ),
50 this, SLOT ( aboutToDisplay ( Kopete::Message& ) ) );
52 connect ( this, SIGNAL ( readyForUnload() ), this, SLOT ( readyForUnload() ) );
54 m_pic = new QImage;
57 URLPicPreviewPlugin::~URLPicPreviewPlugin()
60 kDebug ( 14314 ) << "Removing temporary files...";
61 for ( int i = 0; i < m_tmpFileRegistry.count(); i++ )
63 KIO::NetAccess::removeTempFile ( m_tmpFileRegistry[i] );
66 disconnect ( this, SLOT ( aboutToDisplay ( Kopete::Message& ) ) );
68 delete m_pic;
70 kDebug ( 14314 );
73 /*!
74 \fn URLPicPreviewPlugin::aboutToDiplay(Kopete::Message& message)
76 void URLPicPreviewPlugin::aboutToDisplay ( Kopete::Message& message )
78 if ( message.direction() == Kopete::Message::Inbound )
80 // reread configuration
81 URLPicPreviewConfig::self()->readConfig();
83 QRegExp ex ( "(<a href=\")([^\"]*)(\" )?([^<]*)(</a>)(.*)$" );
84 QString myParsedBody = message.parsedBody();
85 if ( ex.indexIn ( myParsedBody ) != -1 )
87 // Only change message if it contains urls
88 message.setHtmlBody ( prepareBody ( myParsedBody ) );
93 /**
94 * @brief Recursively searches the message, downloads and replaces all found imgages
96 * @param parsedBody the parsed body of the message
98 * @return a new message body with the images as preview
100 QString URLPicPreviewPlugin::prepareBody ( const QString& parsedBody, uint previewCount )
103 kDebug ( 14314 ) << "Searching for URLs to pictures";
105 static const QString rex = "(<a href=\")([^\"]*)(\" )?([^<]*)(</a>)(.*)$";
106 // Caps: 1 2 3 4 5 6
108 QRegExp ex ( rex );
109 QString myParsedBody = parsedBody;
111 kDebug ( 14314 ) << "Analyzing message: \"" << myParsedBody << "\"";
113 if ( ex.indexIn ( myParsedBody ) == -1 || ( previewCount >= URLPicPreviewConfig::self()->previewAmount() ) || m_abortMessageCheck )
115 kDebug ( 14314 ) << "No more URLs found in message.";
116 return myParsedBody;
119 QString foundURL = ex.cap ( 2 );
120 KUrl url ( foundURL );
121 QString tmpFile;
123 kDebug ( 14314 ) << "Found an URL: " << foundURL;
125 if ( url.isValid() )
127 kDebug ( 14314 ) << "URL \"" << foundURL << "\" is valid.";
129 if ( !( tmpFile = createPreviewPicture ( url ) ).isEmpty() )
131 if ( URLPicPreviewConfig::self()->scaling() )
133 int width = URLPicPreviewConfig::self()->previewScaleWidth();
134 kDebug ( 14314 ) << "Try to scale the image to width: " << width;
135 if ( m_pic->load ( tmpFile ) )
137 // resize but keep aspect ratio
138 if ( m_pic->width() > width )
140 if ( ! ( (*m_pic = m_pic->scaledToWidth ( width ) ) ).save ( tmpFile, "PNG" ) )
142 kWarning ( 14314 ) << "Could not save scaled image" << tmpFile;
146 else
148 kWarning ( 14314 ) << "Could not load image " << tmpFile;
152 myParsedBody.replace ( QRegExp ( rex ), QString ( "<a href=\"%1\" title=\"%2\">%3</a><br /><img align=\"center\" src=\"%4\" title=\"" + i18n ( "Preview of:" ) + " %5\" /><br />" ).arg ( foundURL ).arg ( foundURL ).arg ( foundURL ).arg ( tmpFile ).arg ( foundURL ) );
154 if ( URLPicPreviewConfig::self()->previewRestriction() )
156 previewCount++;
157 kDebug ( 14314 ) << "Updating previewCount: " << previewCount;
160 kDebug ( 14314 ) << "Registering temporary file for deletion.";
161 m_tmpFileRegistry.append ( tmpFile );
162 return myParsedBody + prepareBody ( ex.cap ( 6 ), previewCount );
165 else
167 kWarning ( 14314 ) << "URL \"" << foundURL << "\" is invalid. Ignoring.";
170 return myParsedBody.replace ( QRegExp ( rex ), ex.cap ( 1 ) + ex.cap ( 2 ) + ex.cap ( 3 ) + ex.cap ( 4 ) + ex.cap ( 5 ) ) + prepareBody ( ex.cap ( 6 ), previewCount );
174 \fn URLPicPreviewPlugin::abortAllOperations()
176 void URLPicPreviewPlugin::readyForUnload()
178 kDebug ( 14314 );
179 m_abortMessageCheck = true;
180 emit abortAllOperations();
184 \fn URLPicPreviewPlugin::createPreviewPicture()
186 QString URLPicPreviewPlugin::createPreviewPicture ( const KUrl& url )
188 QString tmpFile;
190 if ( !url.fileName ( ).isEmpty() &&
191 KIO::NetAccess::mimetype ( url, Kopete::UI::Global::mainWidget() ).startsWith ( "image/" ) )
193 if ( !KIO::NetAccess::download ( url, tmpFile, Kopete::UI::Global::mainWidget() ) )
195 return QString();
198 else
199 { // Experimental
201 KTempFile tmp;
202 tmpFile = tmp.name();
203 LinkPreview::self(this)->getPreviewPic(url).save(tmpFile, "PNG");
207 return tmpFile;
210 #include "urlpicpreviewplugin.moc"