kfilemetainfo.cpp

00001 /*
00002  *  This file is part of the KDE libraries
00003  *  Copyright (C) 2001-2002 Rolf Magnus <ramagnus@kde.org>
00004  *  Copyright (C) 2001-2002 Carsten Pfeiffer <pfeiffer@kde.org>
00005  *
00006  *  This library is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU Library General Public
00008  *  License as published by the Free Software Foundation version 2.0.
00009  *
00010  *  This library 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 GNU
00013  *  Library General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU Library General Public License
00016  *  along with this library; see the file COPYING.LIB.  If not, write to
00017  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  *  Boston, MA 02110-1301, USA.
00019  *
00020  *  $Id: kfilemetainfo.cpp 585308 2006-09-16 23:00:15Z kling $
00021  */
00022 
00023 #include <assert.h>
00024 
00025 #include <qshared.h>
00026 #include <qdict.h>
00027 
00028 #include <ktrader.h>
00029 #include <kstaticdeleter.h>
00030 #include <kparts/componentfactory.h>
00031 #include <kuserprofile.h>
00032 #include <kdebug.h>
00033 #include <kmimetype.h>
00034 #include <kdatastream.h> // needed for serialization of bool
00035 #include <klocale.h>
00036 #include <kio/global.h>
00037 
00038 #include "kfilemetainfo.h"
00039 
00040 // shared data of a KFileMetaInfoItem
00041 class KFileMetaInfoItem::Data : public QShared
00042 {
00043 public:
00044     Data( const KFileMimeTypeInfo::ItemInfo* mti, const QString& _key,
00045           const QVariant& _value )
00046         : QShared(),
00047           mimeTypeInfo( mti ),
00048           key( _key ),
00049           value( _value ),
00050           dirty( false ),
00051           added( false ),
00052           removed( false )
00053     {}
00054 
00055     // we use this one for the streaming operators
00056     Data() : mimeTypeInfo( 0L )
00057     {}
00058 
00059     ~Data()
00060     {
00061         if ( this == null ) // only the null item owns its mimeTypeInfo
00062             delete mimeTypeInfo;
00063     }
00064 
00065     const KFileMimeTypeInfo::ItemInfo*  mimeTypeInfo;
00066     // mimeTypeInfo has the key, too, but only for non-variable ones
00067     QString                             key;
00068     QVariant                            value;
00069     bool                                dirty    :1;
00070     bool                                added    :1;
00071     bool                                removed  :1;
00072 
00073     static Data* null;
00074     static Data* makeNull();
00075 };
00076 
00077 //this is our null data
00078 KFileMetaInfoItem::Data* KFileMetaInfoItem::Data::null = 0L;
00079 static KStaticDeleter<KFileMetaInfoItem::Data> sd_KFileMetaInfoItemData;
00080 
00081 KFileMetaInfoItem::Data* KFileMetaInfoItem::Data::makeNull()
00082 {
00083     if (!null)
00084     {
00085         // We deliberately do not reset "null" after it has been destroyed!
00086         // Otherwise we will run into problems later in ~KFileMetaInfoItem
00087         // where the d-pointer is compared against null.
00088 
00089         KFileMimeTypeInfo::ItemInfo* info = new KFileMimeTypeInfo::ItemInfo();
00090         null = new Data(info, QString::null, QVariant());
00091         sd_KFileMetaInfoItemData.setObject( null );
00092     }
00093     return null;
00094 }
00095 
00096 KFileMetaInfoItem::KFileMetaInfoItem( const KFileMimeTypeInfo::ItemInfo* mti,
00097                                       const QString& key, const QVariant& value )
00098     : d( new Data( mti, key, value ) )
00099 {
00100 }
00101 
00102 KFileMetaInfoItem::KFileMetaInfoItem( const KFileMetaInfoItem& item )
00103 {
00104     // operator= does everything that's necessary
00105     d = Data::makeNull();
00106     *this = item;
00107 }
00108 
00109 KFileMetaInfoItem::KFileMetaInfoItem()
00110 {
00111     d = Data::makeNull();
00112 }
00113 
00114 KFileMetaInfoItem::~KFileMetaInfoItem()
00115 {
00116     deref();
00117 }
00118 
00119 const KFileMetaInfoItem& KFileMetaInfoItem::operator=
00120                                               (const KFileMetaInfoItem & item )
00121 {
00122     if (d != item.d)
00123     {
00124         // first deref the old one
00125         deref();
00126         d = item.d;
00127         // and now ref the new one
00128         ref();
00129     }
00130 
00131     return *this;
00132 }
00133 
00134 bool KFileMetaInfoItem::setValue( const QVariant& value )
00135 {
00136     // We don't call makeNull here since it isn't necassery, see deref()
00137     if ( d == Data::null ) return false;
00138 
00139     if ( ! (d->mimeTypeInfo->attributes() & KFileMimeTypeInfo::Modifiable ) ||
00140          ! (value.canCast(d->mimeTypeInfo->type())))
00141     {
00142         kdDebug(7033) << "setting the value of " << key() << "failed\n";
00143         return false;
00144     }
00145 
00146 //    kdDebug(7033) << key() << ".setValue()\n";
00147 
00148     if ( d->value == value )
00149         return true;
00150 
00151     d->dirty = true;
00152     d->value = value;
00153     // If we don't cast (and test for canCast in the above if), QVariant is
00154     // very picky about types (e.g. QString vs. QCString or int vs. uint)
00155     d->value.cast(d->mimeTypeInfo->type());
00156 
00157     return true;
00158 }
00159 
00160 bool KFileMetaInfoItem::isRemoved() const
00161 {
00162     return d->removed;
00163 }
00164 
00165 QString KFileMetaInfoItem::key() const
00166 {
00167     return d->key;
00168 }
00169 
00170 QString KFileMetaInfoItem::translatedKey() const
00171 {
00172     // are we a variable key?
00173     if (d->mimeTypeInfo->key().isNull())
00174     {
00175         // then try if we have luck with i18n()
00176         return i18n(d->key.utf8());
00177     }
00178 
00179     return d->mimeTypeInfo->translatedKey();
00180 }
00181 
00182 const QVariant& KFileMetaInfoItem::value() const
00183 {
00184     return d->value;
00185 }
00186 
00187 QString KFileMetaInfoItem::string( bool mangle ) const
00188 {
00189     return d->mimeTypeInfo->string(d->value, mangle);
00190 }
00191 
00192 QVariant::Type KFileMetaInfoItem::type() const
00193 {
00194     return d->mimeTypeInfo->type();
00195 }
00196 
00197 uint KFileMetaInfoItem::unit() const
00198 {
00199     return d->mimeTypeInfo->unit();
00200 }
00201 
00202 bool KFileMetaInfoItem::isModified() const
00203 {
00204     return d->dirty;
00205 }
00206 
00207 QString KFileMetaInfoItem::prefix() const
00208 {
00209     return d->mimeTypeInfo->prefix();
00210 }
00211 
00212 QString KFileMetaInfoItem::suffix() const
00213 {
00214     return d->mimeTypeInfo->suffix();
00215 }
00216 
00217 uint KFileMetaInfoItem::hint() const
00218 {
00219     return d->mimeTypeInfo->hint();
00220 }
00221 
00222 uint KFileMetaInfoItem::attributes() const
00223 {
00224     return d->mimeTypeInfo->attributes();
00225 }
00226 
00227 bool KFileMetaInfoItem::isEditable() const
00228 {
00229     return d->mimeTypeInfo->attributes() & KFileMimeTypeInfo::Modifiable;
00230 }
00231 
00232 bool KFileMetaInfoItem::isValid() const
00233 {
00234     // We don't call makeNull here since it isn't necassery:
00235     // If d is equal to null it means that null is initialized already.
00236     // null is 0L when it hasn't been initialized and d is never 0L.
00237     return d != Data::null;
00238 }
00239 
00240 void KFileMetaInfoItem::setAdded()
00241 {
00242     d->added = true;
00243 }
00244 
00245 void KFileMetaInfoItem::setRemoved()
00246 {
00247     d->removed = true;
00248 }
00249 
00250 void KFileMetaInfoItem::ref()
00251 {
00252     if (d != Data::null) d->ref();
00253 }
00254 
00255 void KFileMetaInfoItem::deref()
00256 {
00257     // We don't call makeNull here since it isn't necassery:
00258     // If d is equal to null it means that null is initialized already.
00259     // null is 0L when it hasn't been initialized and d is never 0L.
00260     if ((d != Data::null) && d->deref())
00261     {
00262 //        kdDebug(7033) << "item " << d->key
00263 //                      << " is finally deleted\n";
00264         delete d;
00265         d = 0;
00266     }
00267 }
00268 
00271 
00272 // shared data of a KFileMetaInfo
00273 class KFileMetaInfo::Data : public QShared
00274 {
00275 public:
00276     Data(const KURL& _url, uint _what)
00277         : QShared(),
00278           url(_url),
00279           what(_what),
00280           mimeTypeInfo( 0L )
00281     {}
00282 
00283     // wee use this one for the streaming operators
00284     Data() {};
00285 
00286     KURL                              url;
00287     uint                              what;
00288     QMap<QString, KFileMetaInfoGroup> groups;
00289     const KFileMimeTypeInfo*          mimeTypeInfo;
00290     QStringList                       removedGroups;
00291 
00292     static Data* null;
00293     static Data* makeNull();
00294 
00295 };
00296 
00297 KFileMetaInfo::KFileMetaInfo( const QString& path, const QString& mimeType,
00298                               uint what )
00299 {
00300     KURL u;
00301 
00302     u.setPath(path);
00303     init(u, mimeType, what);
00304 }
00305 
00306 KFileMetaInfo::KFileMetaInfo( const KURL& url, const QString& mimeType,
00307                               uint what )
00308 {
00309     init(url, mimeType, what);
00310 }
00311 
00312 void KFileMetaInfo::init( const KURL& url, const QString& mimeType,
00313                           uint what )
00314 {
00315     d = new Data( url, what );
00316 
00317     QString mT;
00318     if (mimeType.isEmpty())
00319         mT = KMimeType::findByURL(url)->name();
00320     else
00321         mT = mimeType;
00322 
00323     // let's "share our property"
00324     KFileMetaInfo item(*this);
00325 
00326     //kdDebug() << k_funcinfo << mT << " " << url << endl;
00327 
00328     d->mimeTypeInfo = KFileMetaInfoProvider::self()->mimeTypeInfo( mT, url.protocol() );
00329     if ( d->mimeTypeInfo )
00330     {
00331         //kdDebug(7033) << "Found mimetype info for " << mT /* or protocol*/ << endl;
00332         KFilePlugin *p = plugin();
00333         Q_ASSERT( p );
00334         if ( p && !p->readInfo( item, what) )
00335         {
00336             deref();
00337             d = Data::makeNull();
00338         }
00339     }
00340     else
00341     {
00342 //        kdDebug(7033) << "No mimetype info for " << mimeType << endl;
00343         deref();
00344         d = Data::makeNull();
00345     }
00346 }
00347 
00348 KFileMetaInfo::KFileMetaInfo( const KFileMetaInfo& original )
00349 {
00350     // operator= does everything that's necessary
00351     d = Data::makeNull();
00352     *this = original;
00353 }
00354 
00355 KFileMetaInfo::KFileMetaInfo()
00356 {
00357     d = Data::makeNull();
00358 }
00359 
00360 KFileMetaInfo::~KFileMetaInfo()
00361 {
00362     deref();
00363 }
00364 
00365 QStringList KFileMetaInfo::supportedGroups() const
00366 {
00367     assert(isValid());
00368     return d->mimeTypeInfo->supportedGroups();
00369 }
00370 
00371 QStringList KFileMetaInfo::supportedKeys() const
00372 {
00373     assert(isValid());
00374     return d->mimeTypeInfo->supportedKeys();
00375 }
00376 
00377 QStringList KFileMetaInfo::groups() const
00378 {
00379     QStringList list;
00380     QMapConstIterator<QString, KFileMetaInfoGroup> it = d->groups.begin();
00381     for ( ; it != d->groups.end(); ++it )
00382         list += (*it).name();
00383 
00384     return list;
00385 }
00386 
00387 QStringList KFileMetaInfo::editableGroups() const
00388 {
00389     QStringList list;
00390     QStringList supported = supportedGroups();
00391     QStringList::ConstIterator it = supported.begin();
00392     for ( ; it != supported.end(); ++it ) {
00393         const KFileMimeTypeInfo::GroupInfo * groupInfo = d->mimeTypeInfo->groupInfo( *it );
00394         if ( groupInfo && groupInfo->attributes() &
00395              (KFileMimeTypeInfo::Addable | KFileMimeTypeInfo::Removable) )
00396             list.append( *it );
00397     }
00398 
00399     return list;
00400 }
00401 
00402 QStringList KFileMetaInfo::preferredGroups() const
00403 {
00404     assert(isValid());
00405     QStringList list = groups();
00406     QStringList newlist;
00407     QStringList preferred = d->mimeTypeInfo->preferredGroups();
00408     QStringList::Iterator pref;
00409 
00410     // move all keys from the preferred groups that are in our list to a new list
00411     for ( pref = preferred.begin(); pref != preferred.end(); ++pref )
00412     {
00413         QStringList::Iterator group = list.find(*pref);
00414         if ( group != list.end() )
00415         {
00416              newlist.append( *group );
00417              list.remove(group);
00418         }
00419     }
00420 
00421     // now the old list only contains the non-preferred items, so we
00422     // add the remaining ones to newlist
00423     newlist += list;
00424 
00425     return newlist;
00426 }
00427 
00428 QStringList KFileMetaInfo::preferredKeys() const
00429 {
00430     QStringList newlist;
00431 
00432     QStringList list = preferredGroups();
00433     for (QStringList::Iterator git = list.begin(); git != list.end(); ++git)
00434     {
00435         newlist += d->groups[*git].preferredKeys();
00436     }
00437 
00438     return newlist;
00439 }
00440 
00441 KFileMetaInfoGroup KFileMetaInfo::group(const QString& key) const
00442 {
00443     QMapIterator<QString,KFileMetaInfoGroup> it = d->groups.find( key );
00444     if ( it != d->groups.end() )
00445         return it.data();
00446     else
00447         return KFileMetaInfoGroup();
00448 }
00449 
00450 bool KFileMetaInfo::addGroup( const QString& name )
00451 {
00452     assert(isValid());
00453     if ( d->mimeTypeInfo->supportedGroups().contains(name) &&
00454          ! d->groups.contains(name) )
00455     {
00456         KFileMetaInfoGroup group( name, d->mimeTypeInfo );
00457 
00458         // add all the items that can't be added by the user later
00459         const KFileMimeTypeInfo::GroupInfo* ginfo = d->mimeTypeInfo->groupInfo(name);
00460         Q_ASSERT(ginfo);
00461         if (!ginfo) return false;
00462 
00463         QStringList keys = ginfo->supportedKeys();
00464         for (QStringList::Iterator it = keys.begin(); it != keys.end(); ++it)
00465         {
00466             const KFileMimeTypeInfo::ItemInfo* iteminfo = ginfo->itemInfo(*it);
00467             Q_ASSERT(ginfo);
00468             if (!iteminfo) return false;
00469 
00470             if ( !(iteminfo->attributes() & KFileMimeTypeInfo::Addable) &&
00471                   (iteminfo->attributes() & KFileMimeTypeInfo::Modifiable))
00472             {
00473                 // append it now or never
00474                 group.appendItem(iteminfo->key(), QVariant());
00475             }
00476 
00477         }
00478 
00479         d->groups.insert(name, group);
00480         group.setAdded();
00481         return true;
00482     }
00483 
00484     return false;
00485 }
00486 
00487 bool KFileMetaInfo::removeGroup( const QString& name )
00488 {
00489     QMapIterator<QString, KFileMetaInfoGroup> it = d->groups.find(name);
00490     if ( (it==d->groups.end()) ||
00491         !((*it).attributes() & KFileMimeTypeInfo::Removable))
00492         return false;
00493 
00494     d->groups.remove(it);
00495     d->removedGroups.append(name);
00496     return true;
00497 }
00498 
00499 QStringList KFileMetaInfo::removedGroups()
00500 {
00501     return d->removedGroups;
00502 }
00503 
00504 const KFileMetaInfo& KFileMetaInfo::operator= (const KFileMetaInfo& info )
00505 {
00506     if (d != info.d)
00507     {
00508         deref();
00509         // first deref the old one
00510         d = info.d;
00511         // and now ref the new one
00512         ref();
00513     }
00514     return *this;
00515 }
00516 
00517 bool KFileMetaInfo::isValid() const
00518 {
00519     // We don't call makeNull here since it isn't necassery, see deref()
00520     return d != Data::null;
00521 }
00522 
00523 bool KFileMetaInfo::isEmpty() const
00524 {
00525     for (QMapIterator<QString, KFileMetaInfoGroup> it = d->groups.begin();
00526          it!=d->groups.end(); ++it)
00527         if (!(*it).isEmpty())
00528             return false;
00529     return true;
00530 }
00531 
00532 bool KFileMetaInfo::applyChanges()
00533 {
00534     return applyChanges( path() );
00535 }
00536 
00537 bool KFileMetaInfo::applyChanges( const QString& path )
00538 {
00539     bool doit = false;
00540 
00541 //    kdDebug(7033) << "KFileMetaInfo::applyChanges()\n";
00542 
00543     // look up if we need to write to the file
00544     QMapConstIterator<QString, KFileMetaInfoGroup> it;
00545     for (it = d->groups.begin(); it!=d->groups.end() && !doit; ++it)
00546     {
00547         if ( (*it).isModified() )
00548             doit = true;
00549 
00550         else
00551         {
00552             QStringList keys = it.data().keys();
00553             for (QStringList::Iterator it2 = keys.begin(); it2!=keys.end(); ++it2)
00554             {
00555                 if ( (*it)[*it2].isModified() )
00556                 {
00557                     doit = true;
00558                     break;
00559                 }
00560             }
00561         }
00562     }
00563 
00564     if (!doit)
00565     {
00566         kdDebug(7033) << "Don't need to write, nothing changed\n";
00567         return true;
00568     }
00569 
00570     KFilePlugin* p = plugin();
00571     if (!p) return false;
00572 
00573 //    kdDebug(7033) << "Ok, trying to write the info\n";
00574 
00575     KURL savedURL = url();
00576     d->url = KURL();
00577     d->url.setPath( path );
00578     
00579     bool ret = p->writeInfo(*this);
00580     
00581     d->url = savedURL;
00582     return ret;
00583 }
00584 
00585 KFilePlugin * const KFileMetaInfo::plugin() const
00586 {
00587     assert(isValid());
00588     KFileMetaInfoProvider* prov = KFileMetaInfoProvider::self();
00589     return prov->plugin( d->mimeTypeInfo->mimeType(), d->url.protocol() );
00590 }
00591 
00592 QString KFileMetaInfo::mimeType() const
00593 {
00594     assert(isValid());
00595     return d->mimeTypeInfo->mimeType();
00596 }
00597 
00598 bool KFileMetaInfo::contains(const QString& key) const
00599 {
00600     QStringList glist = groups();
00601     for (QStringList::Iterator it = glist.begin(); it != glist.end(); ++it)
00602     {
00603         KFileMetaInfoGroup g = d->groups[*it];
00604         if (g.contains(key)) return true;
00605     }
00606     return false;
00607 }
00608 
00609 bool KFileMetaInfo::containsGroup(const QString& key) const
00610 {
00611     return groups().contains(key);
00612 }
00613 
00614 KFileMetaInfoItem KFileMetaInfo::item( const QString& key) const
00615 {
00616     QStringList groups = preferredGroups();
00617     for (QStringList::Iterator it = groups.begin(); it != groups.end(); ++it)
00618     {
00619         KFileMetaInfoItem i = d->groups[*it][key];
00620         if (i.isValid()) return i;
00621     }
00622     return KFileMetaInfoItem();
00623 }
00624 
00625 KFileMetaInfoItem KFileMetaInfo::item(const KFileMetaInfoItem::Hint hint) const
00626 {
00627     QStringList groups = preferredGroups();
00628     QStringList::ConstIterator it;
00629     for (it = groups.begin(); it != groups.end(); ++it)
00630     {
00631         KFileMetaInfoItem i = d->groups[*it].item(hint);
00632         if (i.isValid()) return i;
00633     }
00634     return KFileMetaInfoItem();
00635 }
00636 
00637 KFileMetaInfoItem KFileMetaInfo::saveItem( const QString& key,
00638                                            const QString& preferredGroup,
00639                                            bool createGroup )
00640 {
00641     assert(isValid());
00642     // try the preferred groups first
00643     if ( !preferredGroup.isEmpty() ) {
00644         QMapIterator<QString,KFileMetaInfoGroup> it =
00645             d->groups.find( preferredGroup );
00646 
00647         // try to create the preferred group, if necessary
00648         if ( it == d->groups.end() && createGroup ) {
00649             const KFileMimeTypeInfo::GroupInfo *groupInfo =
00650                 d->mimeTypeInfo->groupInfo( preferredGroup );
00651             if ( groupInfo && groupInfo->supportedKeys().contains( key ) ) {
00652                 if ( addGroup( preferredGroup ) )
00653                     it = d->groups.find( preferredGroup );
00654             }
00655         }
00656 
00657         if ( it != d->groups.end() ) {
00658             KFileMetaInfoItem item = it.data().addItem( key );
00659             if ( item.isValid() )
00660                 return item;
00661         }
00662     }
00663 
00664     QStringList groups = preferredGroups();
00665 
00666     KFileMetaInfoItem item;
00667 
00668     QStringList::ConstIterator groupIt = groups.begin();
00669     for ( ; groupIt != groups.end(); ++groupIt )
00670     {
00671         QMapIterator<QString,KFileMetaInfoGroup> it = d->groups.find( *groupIt );
00672         if ( it != d->groups.end() )
00673         {
00674             KFileMetaInfoGroup group = it.data();
00675             item = findEditableItem( group, key );
00676             if ( item.isValid() )
00677                 return item;
00678         }
00679         else // not existant -- try to create the group
00680         {
00681             const KFileMimeTypeInfo::GroupInfo *groupInfo =
00682                 d->mimeTypeInfo->groupInfo( *groupIt );
00683             if ( groupInfo && groupInfo->supportedKeys().contains( key ) )
00684             {
00685                 if ( addGroup( *groupIt ) )
00686                 {
00687                     KFileMetaInfoGroup group = d->groups[*groupIt];
00688                     KFileMetaInfoItem item = group.addItem( key );
00689                     if ( item.isValid() )
00690                         return item;
00691 //                     else ### add when removeGroup() is implemented :)
00692 //                         removeGroup( *groupIt ); // couldn't add item -> remove
00693                 }
00694             }
00695         }
00696     }
00697 
00698     // finally check for variable items
00699 
00700     return item;
00701 }
00702 
00703 KFileMetaInfoItem KFileMetaInfo::findEditableItem( KFileMetaInfoGroup& group,
00704                                                    const QString& key )
00705 {
00706     assert(isValid());
00707     KFileMetaInfoItem item = group.addItem( key );
00708     if ( item.isValid() && item.isEditable() )
00709          return item;
00710 
00711     if ( (d->mimeTypeInfo->groupInfo( group.name() )->attributes() & KFileMimeTypeInfo::Addable) )
00712         return item;
00713 
00714     return KFileMetaInfoItem();
00715 }
00716 
00717 KFileMetaInfoGroup KFileMetaInfo::appendGroup(const QString& name)
00718 {
00719     assert(isValid());
00720     if ( d->mimeTypeInfo->supportedGroups().contains(name) &&
00721          ! d->groups.contains(name) )
00722     {
00723         KFileMetaInfoGroup group( name, d->mimeTypeInfo );
00724         d->groups.insert(name, group);
00725         return group;
00726     }
00727 
00728     else {
00729         kdWarning(7033) << "Someone's trying to add a KFileMetaInfoGroup which is not supported or already existing: " << name << endl;
00730         return KFileMetaInfoGroup();
00731     }
00732 }
00733 
00734 QString KFileMetaInfo::path() const
00735 {
00736     return d->url.isLocalFile() ? d->url.path() : QString::null;
00737 }
00738 
00739 KURL KFileMetaInfo::url() const
00740 {
00741     return d->url;
00742 }
00743 
00744 void KFileMetaInfo::ref()
00745 {
00746     if (d != Data::null) d->ref();
00747 
00748 }
00749 
00750 void KFileMetaInfo::deref()
00751 {
00752     // We don't call makeNull here since it isn't necassery:
00753     // If d is equal to null it means that null is initialized already.
00754     // null is 0L when it hasn't been initialized and d is never 0L.
00755     if ((d != Data::null) && d->deref())
00756     {
00757 //        kdDebug(7033) << "metainfo object for " << d->url.path << " is finally deleted\n";
00758         delete d;
00759         d = 0;
00760     }
00761 
00762 }
00763 
00764 
00765 KFileMetaInfo::Data* KFileMetaInfo::Data::null = 0L;
00766 static KStaticDeleter<KFileMetaInfo::Data> sd_KFileMetaInfoData;
00767 
00768 KFileMetaInfo::Data* KFileMetaInfo::Data::makeNull()
00769 {
00770     if (!null)
00771         // We deliberately do not reset "null" after it has been destroyed!
00772         // Otherwise we will run into problems later in ~KFileMetaInfoItem
00773         // where the d-pointer is compared against null.
00774     null = sd_KFileMetaInfoData.setObject( new KFileMetaInfo::Data(KURL(), 0) );
00775     return null;
00776 }
00777 
00780 
00781 KFilePlugin::KFilePlugin( QObject *parent, const char *name,
00782                           const QStringList& /*args*/)
00783     : QObject( parent, name )
00784 {
00785 //    kdDebug(7033) << "loaded a plugin for " << name << endl;
00786 }
00787 
00788 KFilePlugin::~KFilePlugin()
00789 {
00790 //    kdDebug(7033) << "unloaded a plugin for " << name() << endl;
00791 }
00792 
00793 KFileMimeTypeInfo * KFilePlugin::addMimeTypeInfo( const QString& mimeType )
00794 {
00795     return KFileMetaInfoProvider::self()->addMimeTypeInfo( mimeType );
00796 }
00797 
00798 void KFilePlugin::virtual_hook( int, void* )
00799 { /*BASE::virtual_hook( id, data );*/ }
00800 
00801 
00802 KFileMimeTypeInfo::GroupInfo*  KFilePlugin::addGroupInfo(KFileMimeTypeInfo* info,
00803                   const QString& key, const QString& translatedKey) const
00804 {
00805     return info->addGroupInfo(key, translatedKey);
00806 }
00807 
00808 void KFilePlugin::setAttributes(KFileMimeTypeInfo::GroupInfo* gi, uint attr) const
00809 {
00810     gi->m_attr = attr;
00811 }
00812 
00813 void KFilePlugin::addVariableInfo(KFileMimeTypeInfo::GroupInfo* gi,
00814                                   QVariant::Type type, uint attr) const
00815 {
00816     gi->addVariableInfo(type, attr);
00817 }
00818 
00819 KFileMimeTypeInfo::ItemInfo* KFilePlugin::addItemInfo(KFileMimeTypeInfo::GroupInfo* gi,
00820                                                      const QString& key,
00821                                                      const QString& translatedKey,
00822                                                      QVariant::Type type)
00823 {
00824     return gi->addItemInfo(key, translatedKey, type);
00825 }
00826 
00827 void KFilePlugin::setAttributes(KFileMimeTypeInfo::ItemInfo* item, uint attr)
00828 {
00829     item->m_attr = attr;
00830 }
00831 
00832 void KFilePlugin::setHint(KFileMimeTypeInfo::ItemInfo* item, uint hint)
00833 {
00834     item->m_hint = hint;
00835 }
00836 
00837 void KFilePlugin::setUnit(KFileMimeTypeInfo::ItemInfo* item, uint unit)
00838 {
00839     item->m_unit = unit;
00840     // set prefix and suffix
00841     switch (unit)
00842     {
00843         case KFileMimeTypeInfo::Seconds:
00844             item->m_suffix = i18n("s"); break;
00845 
00846         case KFileMimeTypeInfo::MilliSeconds:
00847             item->m_suffix = i18n("ms"); break;
00848 
00849         case KFileMimeTypeInfo::BitsPerSecond:
00850             item->m_suffix = i18n("bps"); break;
00851 
00852         case KFileMimeTypeInfo::Pixels:
00853             item->m_suffix = i18n("pixels"); break;
00854 
00855         case KFileMimeTypeInfo::Inches:
00856             item->m_suffix = i18n("in"); break;
00857 
00858         case KFileMimeTypeInfo::Centimeters:
00859             item->m_suffix = i18n("cm"); break;
00860 
00861         case KFileMimeTypeInfo::Bytes:
00862             item->m_suffix = i18n("B"); break;
00863 
00864         case KFileMimeTypeInfo::KiloBytes:
00865             item->m_suffix = i18n("KB"); break;
00866 
00867         case KFileMimeTypeInfo::FramesPerSecond:
00868             item->m_suffix = i18n("fps"); break;
00869 
00870         case KFileMimeTypeInfo::DotsPerInch:
00871             item->m_suffix = i18n("dpi"); break;
00872 
00873         case KFileMimeTypeInfo::BitsPerPixel:
00874             item->m_suffix = i18n("bpp"); break;
00875 
00876         case KFileMimeTypeInfo::Hertz:
00877             item->m_suffix = i18n("Hz"); break;
00878 
00879         case KFileMimeTypeInfo::Millimeters:
00880             item->m_suffix = i18n("mm");
00881     }
00882 }
00883 
00884 void KFilePlugin::setPrefix(KFileMimeTypeInfo::ItemInfo* item, const QString& prefix)
00885 {
00886     item->m_prefix = prefix;
00887 }
00888 
00889 void KFilePlugin::setSuffix(KFileMimeTypeInfo::ItemInfo* item, const QString& suffix)
00890 {
00891     item->m_suffix = suffix;
00892 }
00893 
00894 KFileMetaInfoGroup KFilePlugin::appendGroup(KFileMetaInfo& info, const QString& key)
00895 {
00896     return info.appendGroup(key);
00897 }
00898 
00899 void KFilePlugin::appendItem(KFileMetaInfoGroup& group, const QString& key, QVariant value)
00900 {
00901     group.appendItem(key, value);
00902 }
00903 
00906 
00907 
00908 KFileMetaInfoProvider * KFileMetaInfoProvider::s_self;
00909 static KStaticDeleter<KFileMetaInfoProvider> sd;
00910 
00911 KFileMetaInfoProvider * KFileMetaInfoProvider::self()
00912 {
00913     if ( !s_self )
00914         s_self = sd.setObject( s_self, new KFileMetaInfoProvider() );
00915 
00916     return s_self;
00917 }
00918 
00919 KFileMetaInfoProvider::KFileMetaInfoProvider()
00920 {
00921     m_plugins.setAutoDelete( true );
00922 }
00923 
00924 KFileMetaInfoProvider::~KFileMetaInfoProvider()
00925 {
00926     m_plugins.clear();
00927     sd.setObject( 0 );
00928 }
00929 
00930 KFilePlugin* KFileMetaInfoProvider::loadPlugin( const QString& mimeType, const QString& protocol )
00931 {
00932     //kdDebug() << "loadPlugin: mimeType=" << mimeType << " protocol=" << protocol << endl;
00933     // Currently the idea is: either the mimetype is set or the protocol, but not both.
00934     // We need PNG fileinfo, and trash: fileinfo, but not "PNG in the trash".
00935     QString queryMimeType, query;
00936     if ( !mimeType.isEmpty() ) {
00937         query = "(not exist [X-KDE-Protocol])";
00938         queryMimeType = mimeType;
00939     } else {
00940         query = QString::fromLatin1( "[X-KDE-Protocol] == '%1'" ).arg(protocol);
00941         // querying for a protocol: we have no mimetype, so we need to use KFilePlugin as one
00942         queryMimeType = "KFilePlugin";
00943         // hopefully using KFilePlugin as genericMimeType too isn't a problem
00944     }
00945     const KTrader::OfferList offers = KTrader::self()->query( queryMimeType, "KFilePlugin", query, QString::null );
00946     if ( offers.isEmpty() )
00947         return 0;
00948     KService::Ptr service = *(offers.begin());
00949     Q_ASSERT( service && service->isValid() );
00950     if ( !service || !service->isValid() )
00951         return 0;
00952 
00953     KFilePlugin* plugin = KParts::ComponentFactory::createInstanceFromService<KFilePlugin>
00954                           ( service, this, mimeType.local8Bit() );
00955     if (!plugin)
00956         kdWarning(7033) << "error loading the plugin from " << service->desktopEntryPath() << endl;
00957 
00958     return plugin;
00959 }
00960 
00961 KFilePlugin* KFileMetaInfoProvider::loadAndRegisterPlugin( const QString& mimeType, const QString& protocol )
00962 {
00963     Q_ASSERT( m_pendingMimetypeInfos.isEmpty() );
00964     m_pendingMimetypeInfos.clear();
00965 
00966     KFilePlugin* plugin = loadPlugin( mimeType, protocol );
00967     if ( !plugin ) {
00968         // No plugin found. Remember that, to save time.
00969         m_plugins.insert( protocol.isEmpty() ? mimeType : protocol, new CachedPluginInfo );
00970         return 0;
00971     }
00972 
00973     if ( !protocol.isEmpty() ) {
00974         // Protocol-metainfo: only one entry
00975         Q_ASSERT( m_pendingMimetypeInfos.count() == 1 );
00976         KFileMimeTypeInfo* info = m_pendingMimetypeInfos[ protocol ];
00977         Q_ASSERT( info );
00978         m_plugins.insert( protocol, new CachedPluginInfo( plugin, info, true ) );
00979     } else {
00980         // Mimetype-metainfo: the plugin can register itself for multiple mimetypes, remember them all
00981         bool first = true;
00982         QDictIterator<KFileMimeTypeInfo> it( m_pendingMimetypeInfos );
00983         for( ; it.current(); ++it ) {
00984             KFileMimeTypeInfo* info = it.current();
00985             m_plugins.insert( it.currentKey(), new CachedPluginInfo( plugin, info, first ) );
00986             first = false;
00987         }
00988         // Hopefully the above includes the mimetype we asked for!
00989         if ( m_pendingMimetypeInfos.find( mimeType ) == 0 )
00990             kdWarning(7033) << plugin->className() << " was created for " << mimeType << " but doesn't call addMimeTypeInfo for it!" << endl;
00991     }
00992     m_pendingMimetypeInfos.clear();
00993     return plugin;
00994 }
00995 
00996 KFilePlugin * KFileMetaInfoProvider::plugin(const QString& mimeType)
00997 {
00998     return plugin( mimeType, QString::null );
00999 }
01000 
01001 KFilePlugin * KFileMetaInfoProvider::plugin(const QString& mimeType, const QString& protocol)
01002 {
01003     //kdDebug(7033) << "plugin() : looking for plugin for protocol=" << protocol << " mimeType=" << mimeType << endl;
01004 
01005     if ( !protocol.isEmpty() ) {
01006         CachedPluginInfo *cache = m_plugins.find( protocol );
01007         if ( cache && cache->plugin ) {
01008             return cache->plugin;
01009         }
01010         if ( !cache ) {
01011             KFilePlugin* plugin = loadAndRegisterPlugin( QString::null, protocol );
01012             if ( plugin )
01013                 return plugin;
01014         }
01015     }
01016 
01017     CachedPluginInfo *cache = m_plugins.find( mimeType );
01018     if ( cache ) {
01019         return cache-&g