1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
/*
* Copyright (C) 2007 Tobias Koenig <tokoe@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License version 2 as
* published by the Free Software Foundation
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "comic.h"
#include <QtCore/QDate>
#include <QtCore/QFileInfo>
#include <QtCore/QSettings>
#include <KServiceTypeTrader>
#include <KStandardDirs>
#include "cachedprovider.h"
ComicEngine::ComicEngine( QObject* parent, const QVariantList& args )
: Plasma::DataEngine( parent, args ), mEmptySuffix( false )
{
setPollingInterval( 0 );
updateFactories();
}
ComicEngine::~ComicEngine()
{
}
void ComicEngine::init()
{
connect( Solid::Networking::notifier(), SIGNAL( statusChanged( Solid::Networking::Status ) ),
this, SLOT( networkStatusChanged( Solid::Networking::Status ) ) );
}
void ComicEngine::networkStatusChanged( Solid::Networking::Status status )
{
if ( ( status == Solid::Networking::Connected || status == Solid::Networking::Unknown ) &&
!mIdentifierError.isEmpty() ) {
sourceRequestEvent( mIdentifierError );
}
}
void ComicEngine::updateFactories()
{
mFactories.clear();
KService::List services = KServiceTypeTrader::self()->query( "Plasma/Comic" );
Q_FOREACH ( const KService::Ptr &service, services ) {
mFactories.insert( service->property( "X-KDE-PluginInfo-Name", QVariant::String ).toString(),
service );
}
}
bool ComicEngine::updateSourceEvent( const QString &identifier )
{
if ( identifier == "providers" ) {
KService::List services = KServiceTypeTrader::self()->query( "Plasma/Comic" );
foreach ( const KService::Ptr &service, services ) {
QStringList data;
data << service->name();
QFileInfo file( service->icon() );
if ( file.isRelative() ) {
data << KStandardDirs::locate( "data", QString( "plasma-comic/%1.png" ).arg( service->icon() ) );
} else {
data << service->icon();
}
setData( identifier, service->property( "X-KDE-PluginInfo-Name", QVariant::String ).toString(), data );
}
return true;
} else {
// check whether it is cached already...
if ( CachedProvider::isCached( identifier ) ) {
QVariantList args;
args << "String" << identifier;
ComicProvider *provider = new CachedProvider( this, args );
connect( provider, SIGNAL( finished( ComicProvider* ) ), this, SLOT( finished( ComicProvider* ) ) );
connect( provider, SIGNAL( error( ComicProvider* ) ), this, SLOT( error( ComicProvider* ) ) );
return true;
}
// ... start a new query otherwise
const QStringList parts = identifier.split( ':', QString::KeepEmptyParts );
//: are mandatory
if ( parts.count() < 2 ) {
setData( identifier, "Error", true );
kError() << "Less than two arguments specified.";
return false;
}
if ( !mFactories.contains( parts[ 0 ] ) ) {
// User might have installed more from GHNS
updateFactories();
if ( !mFactories.contains( parts[ 0 ] ) ) {
setData( identifier, "Error", true );
kError() << identifier << "comic plugin does not seem to be installed.";
return false;
}
}
// check if there is a connection
Solid::Networking::Status status = Solid::Networking::status();
if ( status != Solid::Networking::Connected && status != Solid::Networking::Unknown ) {
mIdentifierError = identifier;
setData( identifier, "Error", true );
setData( identifier, "Identifier", identifier );
setData( identifier, "Previous identifier suffix", lastCachedIdentifier( identifier ) );
kWarning() << "No connection.";
return true;
}
const KService::Ptr service = mFactories[ parts[ 0 ] ];
bool isCurrentComic = parts[ 1 ].isEmpty();
QVariantList args;
ComicProvider *provider = 0;
const QString type = service->property( "X-KDE-PlasmaComicProvider-SuffixType", QVariant::String ).toString();
if ( type == "Date" ) {
QDate date = QDate::fromString( parts[ 1 ], Qt::ISODate );
if ( !date.isValid() )
date = QDate::currentDate();
args << "Date" << date;
} else if ( type == "Number" ) {
args << "Number" << parts[ 1 ].toInt();
} else if ( type == "String" ) {
args << "String" << parts[ 1 ];
}
args << service->storageId();
provider = service->createInstance<ComicProvider>( this, args );
if ( !provider ) {
setData( identifier, "Error", true );
kError() << identifier << "plugin could be created.";
return false;
}
provider->setIsCurrent( isCurrentComic );
connect( provider, SIGNAL( finished( ComicProvider* ) ), this, SLOT( finished( ComicProvider* ) ) );
connect( provider, SIGNAL( error( ComicProvider* ) ), this, SLOT( error( ComicProvider* ) ) );
return true;
}
}
bool ComicEngine::sourceRequestEvent( const QString &identifier )
{
setData( identifier, DataEngine::Data() );
return updateSourceEvent( identifier );
}
void ComicEngine::finished( ComicProvider *provider )
{
// sets the data
setComicData( provider );
if ( provider->image().isNull() ) {
error( provider );
return;
}
// different comic -- with no error yet -- has been chosen, old error is invalidated
QString temp = mIdentifierError.left( mIdentifierError.indexOf( ':' ) + 1 );
if ( !mIdentifierError.isEmpty() && provider->identifier().indexOf( temp ) == -1 ) {
mIdentifierError.clear();
}
// store in cache if it's not the response of a CachedProvider,
// if there is a valid image and if there is a next comic
// (if we're on today's comic it could become stale)
if ( dynamic_cast<CachedProvider*>( provider ) == 0 && !provider->image().isNull() &&
!provider->nextIdentifier().isEmpty() ) {
CachedProvider::Settings info;
info[ "websiteUrl" ] = provider->websiteUrl().prettyUrl();
info[ "shopUrl" ] = provider->shopUrl().prettyUrl();
info[ "nextIdentifier" ] = provider->nextIdentifier();
info[ "previousIdentifier" ] = provider->previousIdentifier();
info[ "title" ] = provider->name();
info[ "suffixType" ] = provider->suffixType();
info[ "lastCachedStripIdentifier" ] = provider->identifier().mid( provider->identifier().indexOf( ':' ) + 1 );
QString isLeftToRight;
QString isTopToBottom;
info[ "isLeftToRight" ] = isLeftToRight.setNum( provider->isLeftToRight() );
info[ "isTopToBottom" ] = isTopToBottom.setNum( provider->isTopToBottom() );
//data that should be only written if available
if ( !provider->comicAuthor().isEmpty() ) {
info[ "comicAuthor" ] = provider->comicAuthor();
}
if ( !provider->firstStripIdentifier().isEmpty() ) {
info[ "firstStripIdentifier" ] = provider->firstStripIdentifier();
}
if ( !provider->additionalText().isEmpty() ) {
info[ "additionalText" ] = provider->additionalText();
}
if ( !provider->stripTitle().isEmpty() ) {
info[ "stripTitle" ] = provider->stripTitle();
}
CachedProvider::storeInCache( provider->identifier(), provider->image(), info );
}
provider->deleteLater();
}
void ComicEngine::error( ComicProvider *provider )
{
// sets the data
setComicData( provider );
QString identifier( provider->identifier() );
mIdentifierError = identifier;
kWarning() << identifier << "pluging reported an error.";
/**
* Requests for the current day have no suffix (date or id)
* set initially, so we have to remove the 'faked' suffix
* here again to not confuse the applet.
*/
if ( provider->isCurrent() )
identifier = identifier.left( identifier.indexOf( ':' ) + 1 );
setData( identifier, "Identifier", identifier );
setData( identifier, "Error", true );
// if there was an error loading the last cached comic strip, do not return its id anymore
const QString lastCachedId = lastCachedIdentifier( identifier );
if ( lastCachedId != provider->identifier().mid( provider->identifier().indexOf( ':' ) + 1 ) ) {
// sets the previousIdentifier to the identifier of a strip that has been cached before
setData( identifier, "Previous identifier suffix", lastCachedId );
}
setData( identifier, "Next identifier suffix", QString() );
provider->deleteLater();
}
void ComicEngine::setComicData( ComicProvider *provider )
{
QString identifier( provider->identifier() );
/**
* Requests for the current day have no suffix (date or id)
* set initially, so we have to remove the 'faked' suffix
* here again to not confuse the applet.
*/
if ( provider->isCurrent() )
identifier = identifier.left( identifier.indexOf( ':' ) + 1 );
setData( identifier, "Image", provider->image() );
setData( identifier, "Website Url", provider->websiteUrl() );
setData( identifier, "Shop Url", provider->shopUrl() );
setData( identifier, "Next identifier suffix", provider->nextIdentifier() );
setData( identifier, "Previous identifier suffix", provider->previousIdentifier() );
setData( identifier, "Comic Author", provider->comicAuthor() );
setData( identifier, "Additional text", provider->additionalText() );
setData( identifier, "Strip title", provider->stripTitle() );
setData( identifier, "First strip identifier suffix", provider->firstStripIdentifier() );
setData( identifier, "Identifier", provider->identifier() );
setData( identifier, "Title", provider->name() );
setData( identifier, "SuffixType", provider->suffixType() );
setData( identifier, "isLeftToRight", provider->isLeftToRight() );
setData( identifier, "isTopToBottom", provider->isTopToBottom() );
setData( identifier, "Error", false );
}
QString ComicEngine::lastCachedIdentifier( const QString &identifier ) const
{
QString id = identifier.left( identifier.indexOf( ':' ) );
QString data = KStandardDirs::locateLocal( "data", "plasma_engine_comic/" );
data += QUrl::toPercentEncoding( id );
QSettings settings( data + ".conf", QSettings::IniFormat );
QString previousIdentifier = settings.value( "lastCachedStripIdentifier", QString() ).toString();
return previousIdentifier;
}
#include "comic.moc"
|