Blokkal
an Extendable KDE Blogging Client
SourceForge.net Logo

blokkalfileengine.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2007 - 2008 by Martin Mueller                           *
00003  *   orvio@orvio.de                                                        *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #include "blokkalfileengine.h"
00021 
00022 #include "blokkalaccount.h"
00023 #include "blokkalaccountmanager.h"
00024 #include "blokkalblog.h"
00025 #include "blokkalprotocol.h"
00026 #include "blokkalpluginmanager.h"
00027 #include "blokkalutils.h"
00028 
00029 #include <QBuffer>
00030 #include <QPixmap>
00031 
00032 #include <kurl.h>
00033 #include <kdebug.h>
00034 #include <kiconloader.h>
00035 #include <kicon.h>
00036 
00037 //-----------------
00038 //FileEngineHandler
00039 //-----------------
00040 QAbstractFileEngine * Blokkal::FileEngineHandler::create( const QString & fileName ) const
00041 {
00042         KUrl url( fileName );
00043         if( url.protocol() != "blokkal" || url.hasHost() || !url.hasPath() ) {
00044                 return 0;
00045         }
00046 
00047         FileEngine * const engine = new FileEngine();
00048         engine->setFileName( fileName );
00049         return engine;
00050 }
00051 
00052 //----------
00053 //FileEngine
00054 //----------
00055 class Blokkal::FileEngine::Private {
00056 public:
00057         Private( void ) :
00058         buffer( new QBuffer() ){}
00059 
00060         QBuffer * const buffer;
00061         KUrl url;
00062 };
00063 
00064 Blokkal::FileEngine::FileEngine( void ) :
00065 QAbstractFileEngine(),
00066 d( new Private() )
00067 {
00068 }
00069 
00070 Blokkal::FileEngine::~FileEngine( void )
00071 {
00072         d->buffer->close();
00073         delete d->buffer;
00074         delete d;
00075 }
00076 
00077 bool Blokkal::FileEngine::caseSensitive( void ) const
00078 {
00079         return TRUE;
00080 }
00081 
00082 bool Blokkal::FileEngine::close( void )
00083 {
00084         d->buffer->close();
00085         return TRUE;
00086 }
00087 
00088 bool Blokkal::FileEngine::copy( const QString & newName )
00089 {
00090         Q_UNUSED( newName );
00091         return FALSE;
00092 }
00093 
00094 QStringList Blokkal::FileEngine::entryList( QDir::Filters filters, const QStringList & filterNames ) const
00095 {
00096         Q_UNUSED( filters );
00097         Q_UNUSED( filterNames );
00098         return QStringList();
00099 }
00100 
00101 QAbstractFileEngine::FileFlags Blokkal::FileEngine::fileFlags( FileFlags type ) const
00102 {
00103         Q_UNUSED( type );
00104         return 0;
00105 }
00106 
00107 QString Blokkal::FileEngine::fileName( FileName file ) const
00108 {
00109         Q_UNUSED( file );
00110         return d->url.url();
00111 }
00112 
00113 QDateTime Blokkal::FileEngine::fileTime( FileTime time ) const
00114 {
00115         Q_UNUSED( time );
00116         return QDateTime();
00117 }
00118 
00119 bool Blokkal::FileEngine::isRelativePath( void ) const
00120 {
00121         return FALSE;
00122 }
00123 
00124 bool Blokkal::FileEngine::isSequential( void ) const
00125 {
00126         return FALSE;
00127 }
00128 
00129 bool Blokkal::FileEngine::mkdir( const QString & dirName, bool createParentDirectories ) const
00130 {
00131         Q_UNUSED( dirName );
00132         Q_UNUSED( createParentDirectories );
00133         return FALSE;
00134 }
00135 
00136 bool Blokkal::FileEngine::open( QIODevice::OpenMode mode )
00137 {
00138         QStringList path = d->url.path( KUrl::RemoveTrailingSlash ).split( '/', QString::KeepEmptyParts );
00139         path.removeFirst();
00140         QPixmap icon;
00141 
00142         if( path[0] == "accounts" ) {
00143                 //Testing:
00144                 //blokkal:///accounts/[accountId]/icon
00145                 //blokkal:///accounts/[accountId]/stateicon
00146                 //blokkal:///accounts/[accountId]/blogs/[blogId]/icon
00147                 //blokkal:///accounts/[accountId]/blogs/[blogId]/stateicon
00148                 if( path.size() >= 3 ) {
00149                         QString accountName = path[1];
00150                         Account * account = AccountManager::self()->account( accountName );
00151                         int accountSpan = 0;
00152                         
00153                         if( !account ) {
00154                                 accountSpan = 1;
00155                                 for( ;path.size() >= ( 3 + accountSpan ); ++accountSpan ) {
00156                                         //probe deeper to identify accounts with names containing a slash
00157                                         accountName += '/' + path[1 + accountSpan];
00158                                         account = AccountManager::self()->account( accountName );
00159                                         if( account ) {
00160                                                 break;
00161                                         }
00162                                 }
00163                         }
00164                         
00165                         if( account ) {
00166                                 if( path[2 + accountSpan] == "icon" ) {
00167                                         icon = account->icon().pixmap(16,16);
00168                                 }
00169                                 else if( path[2 + accountSpan] == "stateicon" ) {
00170                                         icon = account->isConnected() ? account->icon().pixmap(16,16) : Utils::fadePixmap( account->icon().pixmap(16,16) );
00171                                 }
00172                                 else if( path[2 + accountSpan] == "blogs"
00173                                          && path.size() == ( 5 + accountSpan ) )
00174                                 {
00175                                         //Testing:
00176                                         //blokkal:///accounts/[accountId]/blogs/[blogId]/icon
00177                                         //blokkal:///accounts/[accountId]/blogs/[blogId]/stateicon
00178                                         Blog * blog = account->blog( path[3 + accountSpan] );
00179                                         if( blog ) {
00180                                                 if( path[4 + accountSpan] == "icon" ) {
00181                                                         icon = blog->icon().pixmap(16,16);
00182                                                 }
00183                                                 else if( path[4 + accountSpan] == "stateicon" ) {
00184                                                         icon = blog->account()->isConnected() ? blog->icon().pixmap(16,16) : Utils::fadePixmap( blog->icon().pixmap(16,16) );
00185                                                 }
00186                                                 else {
00187                                                         kDebug() << "no such blog resource: " << path[4 + accountSpan] << endl;
00188                                                 }
00189                                         }
00190                                         else {
00191                                                 kDebug() << "no such blog: " << path[3 + accountSpan] << endl;
00192                                         }
00193                                 }
00194                                 else {
00195                                         kDebug() << "no such account resource: " << path[2 + accountSpan] << endl;
00196                                 }
00197                         }
00198                         else {
00199                                         kDebug() << "no such account: " << accountName << endl;
00200                         }
00201                 }
00202                 else {
00203                         kDebug() << "incomplete account resource path" << endl;
00204                 }
00205         }
00206         else if( path[0] == "protocols" ) {
00207                 //Testing:
00208                 //blokkal:///protocols/[protocolname]/icon
00209                 if( path.size() == 3 ) {
00210                         Protocol * protocol = PluginManager::self()->protocol( path[1] );
00211                         if( protocol ) {
00212                                 if( path[2] == "icon") {
00213                                         icon = protocol->icon().pixmap(16,16);
00214                                 }
00215                                 else {
00216                                         kDebug() << "no such protocol resource: " << path[2] << endl;
00217                                 }
00218                         }
00219                         else {
00220                                 kDebug() << "no such protocol: " << path[1] << endl;
00221                         }
00222                 }
00223                 else {
00224                         kDebug() << "incomplete protocol resource path" << endl;
00225                 }
00226         }
00227         else if( path[0] == "smallicons") {
00228                 //Testing
00229                 //blokkal:///smallicons/[iconname]
00230                 if( path.size() == 2 ) {
00231                         icon = SmallIcon( path[1] );
00232                 }
00233                 else {
00234                         kDebug() << "incomplete smallicons resource path" << endl;
00235                 }               
00236         }
00237         else {
00238                 kDebug() << "unknown base resource name: " << path[0] << endl;
00239         }
00240 
00241         if( !icon.isNull() ) {
00242                 d->buffer->open( QIODevice::WriteOnly );
00243                 icon.save( d->buffer, "PNG" );
00244                 d->buffer->close();
00245                 d->buffer->open( mode );
00246         }
00247 
00248         return !icon.isNull();
00249 }
00250 
00251 QString Blokkal::FileEngine::owner( FileOwner owner ) const
00252 {
00253         Q_UNUSED( owner );
00254         return QString::null;
00255 }
00256 
00257 unsigned int Blokkal::FileEngine::ownerId( FileOwner owner ) const
00258 {
00259         Q_UNUSED( owner );
00260         return -2;
00261 }
00262 
00263 qint64 Blokkal::FileEngine::pos( void ) const
00264 {
00265         return d->buffer->pos();
00266 }
00267 
00268 qint64 Blokkal::FileEngine::read( char * data, qint64 maxlen )
00269 {
00270         return d->buffer->read( data, maxlen );
00271 }
00272 
00273 qint64 Blokkal::FileEngine::readLine( char * data, qint64 maxlen )
00274 {
00275         return read( data, maxlen );
00276 }
00277 
00278 bool Blokkal::FileEngine::remove( void )
00279 {
00280         return FALSE;
00281 }
00282 
00283 bool Blokkal::FileEngine::rename( const QString & newName )
00284 {
00285         Q_UNUSED( newName );
00286         return FALSE;
00287 }
00288 
00289 bool Blokkal::FileEngine::rmdir( const QString & dirName, bool recurseParentDirectories ) const
00290 {
00291         Q_UNUSED( dirName );
00292         Q_UNUSED( recurseParentDirectories );
00293         return FALSE;
00294 }
00295 
00296 bool Blokkal::FileEngine::seek( qint64 offset )
00297 {
00298         return d->buffer->seek( offset );
00299 }
00300 
00301 void Blokkal::FileEngine::setFileName( const QString & file )
00302 {
00303         d->url = KUrl( file );
00304 }
00305 
00306 bool Blokkal::FileEngine::setPermissions( uint perms )
00307 {
00308         Q_UNUSED( perms );
00309         return FALSE;
00310 }
00311 
00312 bool Blokkal::FileEngine::setSize( qint64 size )
00313 {
00314         Q_UNUSED( size );
00315         return FALSE;
00316 }
00317 
00318 qint64 Blokkal::FileEngine::size( void ) const
00319 {
00320         return d->buffer->size();
00321 }
00322 
00323 qint64 Blokkal::FileEngine::write( const char * data, qint64 len )
00324 {
00325         Q_UNUSED( data );
00326         Q_UNUSED( len );
00327         return -1;
00328 }