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 **************************************************************************
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 *
15 **************************************************************************
25 #include <ktemporaryfile.h>
26 #include <kapplication.h>
27 #include <kgenericfactory.h>
30 #include <kio/netaccess.h>
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 )
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() ) );
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
& ) ) );
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
) );
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>)(.*)$";
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.";
119 QString foundURL
= ex
.cap ( 2 );
120 KUrl
url ( foundURL
);
123 kDebug ( 14314 ) << "Found an URL: " << foundURL
;
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
;
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() )
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
);
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()
179 m_abortMessageCheck
= true;
180 emit
abortAllOperations();
184 \fn URLPicPreviewPlugin::createPreviewPicture()
186 QString
URLPicPreviewPlugin::createPreviewPicture ( const KUrl
& url
)
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() ) )
202 tmpFile = tmp.name();
203 LinkPreview::self(this)->getPreviewPic(url).save(tmpFile, "PNG");
210 #include "urlpicpreviewplugin.moc"