kfilemetainfo.h
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 #ifndef KILEMETAINFO_H 00021 #define KILEMETAINFO_H 00022 00023 /* Hack for HPUX: Namespace pollution 00024 m_unit is a define in <sys/sysmacros.h> */ 00025 #define m_unit outouftheway_m_unit 00026 00027 #include <tqdict.h> 00028 #include <tqvariant.h> 00029 #include <tqobject.h> 00030 #include <tqstring.h> 00031 #include <kurl.h> 00032 00033 #undef m_unit 00034 00035 class TQValidator; 00036 class KFilePlugin; 00037 class KFileMetaInfoGroup; 00038 00050 class KIO_EXPORT KFileMimeTypeInfo 00051 { 00052 // the plugin needs to be a friend because it puts the data into the object, 00053 // and it should be the only one allowed to do this. 00054 friend class KFilePlugin; 00055 friend class KFileMetaInfoProvider; 00056 00057 public: 00058 KFileMimeTypeInfo() {} 00059 00064 enum Attributes 00065 { 00066 Addable = 1, 00067 Removable = 2, 00068 Modifiable = 4, 00069 Cumulative = 8, 00072 Cummulative = Cumulative, 00073 Averaged = 16, 00075 MultiLine = 32, 00079 SqueezeText = 64 00083 }; 00084 00089 enum Hint { 00090 NoHint = 0, 00091 Name = 1, 00092 Author = 2, 00093 Description = 3, 00094 Width = 4, 00095 Height = 5, 00096 Size = 6, 00097 Bitrate = 7, 00098 Length = 8, 00099 Hidden = 9, 00100 Thumbnail = 10 00101 00102 }; 00103 00111 enum Unit { 00112 NoUnit = 0, 00113 Seconds = 1, 00114 MilliSeconds = 2, 00115 BitsPerSecond = 3, 00116 Pixels = 4, 00117 Inches = 5, 00118 Centimeters = 6, 00119 Bytes = 7, 00120 FramesPerSecond = 8, 00121 DotsPerInch = 9, 00122 BitsPerPixel = 10, 00123 Hertz = 11, 00124 KiloBytes = 12, 00125 Millimeters = 13 00126 }; 00127 00128 00129 class ItemInfo; 00130 00138 class KIO_EXPORT GroupInfo 00139 { 00140 00141 friend class KFilePlugin; 00142 friend class KFileMimeTypeInfo; 00143 public: 00154 TQStringList supportedKeys() const 00155 { 00156 return m_supportedKeys; 00157 } 00158 00165 const TQString& name() const 00166 { 00167 return m_name; 00168 } 00169 00177 const TQString& translatedName() const 00178 { 00179 return m_translatedName; 00180 } 00181 00189 const ItemInfo * itemInfo( const TQString& key ) const; 00190 00196 uint attributes() const 00197 { 00198 return m_attr; 00199 } 00200 00205 bool supportsVariableKeys() const 00206 { 00207 return m_variableItemInfo; 00208 } 00209 00217 const ItemInfo* variableItemInfo( ) const 00218 { 00219 return m_variableItemInfo; 00220 } 00221 00223 ~GroupInfo(); 00224 private: 00226 GroupInfo( const TQString& name, const TQString& translatedName); 00227 00229 KFileMimeTypeInfo::ItemInfo* addItemInfo( const TQString& key, 00230 const TQString& translatedKey, 00231 TQVariant::Type type); 00232 00234 void addVariableInfo( TQVariant::Type type, uint attr ); 00235 00236 TQString m_name; 00237 TQString m_translatedName; 00238 TQStringList m_supportedKeys; 00239 uint m_attr; 00240 ItemInfo* m_variableItemInfo; 00241 TQDict<ItemInfo> m_itemDict; 00242 00243 }; 00244 00250 class KIO_EXPORT ItemInfo 00251 { 00252 friend class KFilePlugin; 00253 friend class GroupInfo; 00254 public: 00256 ItemInfo() {} // ### should be private? 00257 00265 const TQString& prefix() const 00266 { 00267 return m_prefix; 00268 } 00269 00276 const TQString& suffix() const 00277 { 00278 return m_suffix; 00279 } 00280 00287 TQVariant::Type type() const 00288 { 00289 return m_type; 00290 } 00291 00296 const TQString& key() const 00297 { 00298 return m_key; 00299 } 00300 00312 TQString string( const TQVariant& value, bool mangle = true ) const; 00313 00319 bool isVariableItem() const 00320 { 00321 // every valid item is supposed to have a non-null key 00322 return key().isNull(); 00323 } 00324 00331 const TQString& translatedKey() const 00332 { 00333 return m_translatedKey; 00334 } 00335 00341 uint attributes() const 00342 { 00343 return m_attr; 00344 } 00345 00351 uint hint() const 00352 { 00353 return m_hint; 00354 } 00355 00361 uint unit() const 00362 { 00363 return m_unit; 00364 } 00365 00366 private: 00368 ItemInfo(const TQString& key, const TQString& translatedKey, 00369 TQVariant::Type type) 00370 : m_key(key), m_translatedKey(translatedKey), 00371 m_type(type), 00372 m_attr(0), m_unit(NoUnit), m_hint(NoHint), 00373 m_prefix(TQString::null), m_suffix(TQString::null) 00374 {} 00375 00376 TQString m_key; 00377 TQString m_translatedKey; 00378 TQVariant::Type m_type; 00379 uint m_attr; 00380 uint m_unit; 00381 uint m_hint; 00382 TQString m_prefix; 00383 TQString m_suffix; 00384 }; 00385 00386 // ### could it be made private? Would this be BC? 00387 ~KFileMimeTypeInfo(); 00388 00400 TQValidator * createValidator(const TQString& group, const TQString& key, 00401 TQObject *parent = 0, const char *name = 0) const; 00402 00409 TQStringList supportedGroups() const; 00410 00417 TQStringList translatedGroups() const; 00418 00425 TQStringList preferredGroups() const 00426 { 00427 return m_preferredGroups; 00428 } 00429 00435 TQString mimeType() const {return m_mimeType;} 00436 00444 const GroupInfo * groupInfo( const TQString& group ) const; 00445 00446 // always returning stringlists which the user has to iterate and use them 00447 // to look up the real items sounds strange to me. I think we should add 00448 // our own iterators some time (somewhere in the future ;) 00449 00456 TQStringList supportedKeys() const; 00457 00463 TQStringList preferredKeys() const 00464 { 00465 return m_preferredKeys; 00466 } 00467 00468 // ### shouldn't this be private? BC? 00469 GroupInfo * addGroupInfo( const TQString& name, 00470 const TQString& translatedName); 00471 00472 TQString m_translatedName; 00473 TQStringList m_supportedKeys; 00474 uint m_attr; 00475 // bool m_supportsVariableKeys : 1; 00476 TQDict<ItemInfo> m_itemDict; 00477 00478 // ### this should be made private instead, but this would be BIC 00479 protected: 00481 KFileMimeTypeInfo( const TQString& mimeType ); 00482 00483 TQDict<GroupInfo> m_groups; 00484 TQString m_mimeType; 00485 TQStringList m_preferredKeys; // same as KFileMetaInfoProvider::preferredKeys() 00486 TQStringList m_preferredGroups; // same as KFileMetaInfoProvider::preferredKeys() 00487 }; 00488 00489 00496 class KIO_EXPORT KFileMetaInfoItem 00497 { 00498 public: 00499 class Data; 00500 typedef KFileMimeTypeInfo::Hint Hint; 00501 typedef KFileMimeTypeInfo::Unit Unit; 00502 typedef KFileMimeTypeInfo::Attributes Attributes; 00503 00509 // ### hmm, then it should be private 00510 KFileMetaInfoItem( const KFileMimeTypeInfo::ItemInfo* mti, 00511 const TQString& key, const TQVariant& value); 00512 00516 KFileMetaInfoItem( const KFileMetaInfoItem & item ); 00517 00527 const KFileMetaInfoItem& operator= (const KFileMetaInfoItem & item ); 00528 00532 KFileMetaInfoItem(); 00533 00534 ~KFileMetaInfoItem(); 00535 00541 TQString key() const; 00542 00549 TQString translatedKey() const; 00550 00556 const TQVariant& value() const; 00557 00566 TQString string( bool mangle = true ) const; 00567 00574 bool setValue( const TQVariant& value ); 00575 00581 TQVariant::Type type() const; 00582 00592 bool isEditable() const; 00593 00602 bool isRemoved() const; 00603 00612 bool isModified() const; 00613 00620 TQString prefix() const; 00621 00628 TQString suffix() const; 00629 00635 uint hint() const; 00636 00643 uint unit() const; 00644 00651 uint attributes() const; 00652 00661 bool isValid() const; 00662 00663 KIO_EXPORT friend TQDataStream& operator >>(TQDataStream& s, KFileMetaInfoItem& ); 00664 KIO_EXPORT friend TQDataStream& operator >>(TQDataStream& s, KFileMetaInfoGroup& ); 00665 KIO_EXPORT friend TQDataStream& operator <<(TQDataStream& s, const KFileMetaInfoItem& ); 00666 friend class KFileMetaInfoGroup; 00667 00668 protected: 00669 void setAdded(); 00670 void setRemoved(); 00671 00672 void ref(); 00673 void deref(); 00674 00675 Data *d; 00676 }; 00677 00684 class KIO_EXPORT KFileMetaInfoGroup 00685 { 00686 friend class KFilePlugin; 00687 friend class KFileMetaInfo; 00688 KIO_EXPORT friend TQDataStream& operator >>(TQDataStream& s, KFileMetaInfoGroup& ); 00689 KIO_EXPORT friend TQDataStream& operator <<(TQDataStream& s, const KFileMetaInfoGroup& ); 00690 00691 public: 00692 class Data; 00698 // ### hmm, then it should be private 00699 KFileMetaInfoGroup( const TQString& name, const KFileMimeTypeInfo* info ); 00700 00704 KFileMetaInfoGroup( const KFileMetaInfoGroup& original ); 00705 00715 const KFileMetaInfoGroup& operator= (const KFileMetaInfoGroup& info ); 00716 00722 KFileMetaInfoGroup(); 00723 00724 ~KFileMetaInfoGroup(); 00725 00734 bool isValid() const; 00735 00742 bool isEmpty() const; 00743 00752 bool isModified() const; 00753 00758 KFileMetaInfoItem operator[]( const TQString& key ) const 00759 { return item( key ); } 00760 00767 KFileMetaInfoItem item( const TQString& key ) const; 00768 00775 KFileMetaInfoItem item( uint hint ) const; 00776 00784 const TQVariant value( const TQString& key ) const 00785 { 00786 const KFileMetaInfoItem &i = item( key ); 00787 return i.value(); 00788 } 00789 00800 TQStringList supportedKeys() const; 00801 00808 bool supportsVariableKeys() const; 00809 00815 bool contains( const TQString& key ) const; 00816 00822 TQStringList keys() const; 00823 00829 TQStringList preferredKeys() const; 00830 00837 // ### do we really want to support that? 00838 // let's not waste time on thinking about it. Let's just kick it for now 00839 // and add it in 4.0 if needed ;) 00840 // const TQMemArray<TQVariant::Type>& types( const TQString& key ) const; 00841 00850 KFileMetaInfoItem addItem( const TQString& key ); 00851 00861 bool removeItem(const TQString& key); 00862 00868 TQStringList removedItems(); 00869 00875 TQString name() const; 00876 00884 TQString translatedName() const; 00885 00891 uint attributes() const; 00892 00893 protected: 00894 void setAdded(); 00895 KFileMetaInfoItem appendItem( const TQString& key, const TQVariant& value); 00896 00897 Data* d; 00898 void ref(); 00899 void deref(); 00900 00901 }; 00902 00903 00906 00907 00926 class KIO_EXPORT KFileMetaInfo 00927 { 00928 public: 00929 typedef KFileMimeTypeInfo::Hint Hint; 00930 typedef KFileMimeTypeInfo::Unit Unit; 00931 typedef KFileMimeTypeInfo::Attributes Attributes; 00932 class Data; 00933 00938 enum What 00939 { 00940 Fastest = 0x1, 00943 DontCare = 0x2, 00944 00945 TechnicalInfo = 0x4, 00948 ContentInfo = 0x8, 00950 ExtenedAttr = 0x10, 00952 Thumbnail = 0x20, 00954 Preferred = 0x40, 00955 Everything = 0xffff 00956 00957 }; 00958 00979 KFileMetaInfo( const TQString& path, 00980 const TQString& mimeType = TQString::null, 00981 uint what = Fastest); 00982 00990 KFileMetaInfo( const KURL& url, 00991 const TQString& mimeType = TQString::null, 00992 uint what = Fastest); 00993 00998 KFileMetaInfo(); 00999 01006 KFileMetaInfo( const KFileMetaInfo& original); 01007 01008 ~KFileMetaInfo(); 01009 01020 const KFileMetaInfo& operator= (const KFileMetaInfo& info ); 01021 01022 01028 TQStringList groups() const; 01029 01035 TQStringList supportedGroups() const; 01036 01042 TQStringList preferredGroups() const; 01043 01049 TQStringList preferredKeys() const; 01050 01056 TQStringList supportedKeys() const; 01057 01063 TQStringList editableGroups() const; 01064 01065 // I'd like to keep those for lookup without group, at least the hint 01066 // version 01073 KFileMetaInfoItem item(const TQString& key) const; 01080 KFileMetaInfoItem item(const KFileMetaInfoItem::Hint hint) const; 01081 01090 KFileMetaInfoItem saveItem( const TQString& key, 01091 const TQString& preferredGroup = TQString::null, 01092 bool createGroup = true ); 01093 01100 KFileMetaInfoGroup group(const TQString& key) const; 01101 01108 KFileMetaInfoGroup operator[] (const TQString& key) const 01109 { 01110 return group(key); 01111 } 01112 01123 bool addGroup( const TQString& name ); 01124 01134 bool removeGroup( const TQString& name ); 01135 01141 TQStringList removedGroups(); 01142 01150 bool applyChanges(); 01151 01159 bool applyChanges(const TQString& path); 01160 01167 bool contains( const TQString& key ) const; 01168 01175 bool containsGroup( const TQString& key ) const; 01176 01183 const TQVariant value( const TQString& key ) const 01184 { 01185 return item(key).value(); 01186 } 01187 01188 01195 bool isValid() const; 01196 01203 bool isEmpty() const; 01204 01210 TQString mimeType() const; 01211 01217 TQString path() const; 01218 01224 KURL url() const; 01225 01226 KIO_EXPORT friend TQDataStream& operator >>(TQDataStream& s, KFileMetaInfo& ); 01227 KIO_EXPORT friend TQDataStream& operator <<(TQDataStream& s, const KFileMetaInfo& ); 01228 friend class KFilePlugin; 01229 01230 protected: 01231 KFileMetaInfoGroup appendGroup(const TQString& name); 01232 01237 KFilePlugin * const plugin() const; 01238 01239 void ref(); 01240 void deref(); 01241 01242 Data* d; 01243 01244 private: 01245 KFileMetaInfoItem findEditableItem( KFileMetaInfoGroup& group, 01246 const TQString& key ); 01247 01248 void init( const KURL& url, 01249 const TQString& mimeType = TQString::null, 01250 uint what = Fastest); 01251 }; 01252 01255 01256 01394 class KIO_EXPORT KFilePlugin : public TQObject 01395 { 01396 Q_OBJECT 01397 01398 public: 01412 KFilePlugin( TQObject *parent, const char *name, 01413 const TQStringList& args ); 01414 01418 virtual ~KFilePlugin(); 01419 01432 virtual bool readInfo( KFileMetaInfo& info, 01433 uint what = KFileMetaInfo::Fastest ) = 0; 01434 01442 virtual bool writeInfo( const KFileMetaInfo& info ) const 01443 { 01444 Q_UNUSED(info); 01445 return true; 01446 } 01447 01462 virtual TQValidator* createValidator( const TQString& mimeType, 01463 const TQString& group, 01464 const TQString& key, 01465 TQObject* parent, 01466 const char* name) const 01467 { 01468 Q_UNUSED(mimeType); Q_UNUSED(group);Q_UNUSED(key); 01469 Q_UNUSED(parent);Q_UNUSED(name); 01470 return 0; 01471 } 01472 01473 protected: 01474 01482 KFileMimeTypeInfo * addMimeTypeInfo( const TQString& mimeType ); 01483 // ### do we need this, if it only calls the provider? 01484 // IMHO the Plugin shouldn't call its provider. 01485 // DF: yes we need this. A plugin can create more than one mimetypeinfo. 01486 // What sucks though, is to let plugins do that in their ctor. 01487 // Would be much simpler to have a virtual init method for that, 01488 // so that the provider can set up stuff with the plugin pointer first! 01489 01506 KFileMimeTypeInfo::GroupInfo* addGroupInfo(KFileMimeTypeInfo* info, 01507 const TQString& key, const TQString& translatedKey) const; 01508 01516 void setAttributes(KFileMimeTypeInfo::GroupInfo* gi, uint attr) const; 01517 01518 void addVariableInfo(KFileMimeTypeInfo::GroupInfo* gi, TQVariant::Type type, 01519 uint attr) const; 01520 01535 KFileMimeTypeInfo::ItemInfo* addItemInfo(KFileMimeTypeInfo::GroupInfo* gi, 01536 const TQString& key, 01537 const TQString& translatedKey, 01538 TQVariant::Type type); 01539 01549 void setAttributes(KFileMimeTypeInfo::ItemInfo* item, uint attr); 01550 01560 void setHint(KFileMimeTypeInfo::ItemInfo* item, uint hint); 01561 01572 void setUnit(KFileMimeTypeInfo::ItemInfo* item, uint unit); 01573 01582 void setPrefix(KFileMimeTypeInfo::ItemInfo* item, const TQString& prefix); 01583 01592 void setSuffix(KFileMimeTypeInfo::ItemInfo* item, const TQString& suffix); 01593 01605 KFileMetaInfoGroup appendGroup(KFileMetaInfo& info, const TQString& key); 01606 01615 void appendItem(KFileMetaInfoGroup& group, const TQString& key, TQVariant value); 01616 01617 TQStringList m_preferredKeys; 01618 TQStringList m_preferredGroups; 01619 01620 protected: 01628 virtual void virtual_hook( int id, void* data ); 01629 private: 01630 class KFilePluginPrivate; 01631 KFilePluginPrivate *d; 01632 }; 01633 01636 01637 01646 class KIO_EXPORT KFileMetaInfoProvider: private QObject 01647 { 01648 friend class KFilePlugin; 01649 01650 Q_OBJECT 01651 public: 01652 virtual ~KFileMetaInfoProvider(); 01653 01654 static KFileMetaInfoProvider * self(); 01655 01660 KFilePlugin * plugin( const TQString& mimeType ); // KDE4: merge with method below 01661 01667 KFilePlugin * plugin( const TQString& mimeType, const TQString& protocol ); 01668 01669 const KFileMimeTypeInfo * mimeTypeInfo( const TQString& mimeType ); // KDE4: merge with below 01670 const KFileMimeTypeInfo * mimeTypeInfo( const TQString& mimeType, const TQString& protocol ); 01671 01672 TQStringList preferredKeys( const TQString& mimeType ) const; 01673 TQStringList preferredGroups( const TQString& mimeType ) const; 01674 01676 TQStringList supportedMimeTypes() const; 01677 01678 protected: // ## should be private, right? 01679 KFileMetaInfoProvider(); 01680 01681 private: 01682 01683 // Data structure: 01684 // Mimetype or Protocol -> { Plugin and MimeTypeInfo } 01685 // The {} struct is CachedPluginInfo 01686 struct CachedPluginInfo 01687 { 01688 CachedPluginInfo() : plugin( 0 ), mimeTypeInfo( 0 ), ownsPlugin( false ) {} 01689 CachedPluginInfo( KFilePlugin* p, KFileMimeTypeInfo* i, bool owns ) 01690 : plugin( p ), mimeTypeInfo( i ), ownsPlugin( owns ) {} 01691 // auto-delete behavior 01692 ~CachedPluginInfo() { 01693 if ( ownsPlugin ) delete plugin; 01694 delete mimeTypeInfo; 01695 } 01696 01697 // If plugin and mimeTypeInfo are 0, means that no plugin is available. 01698 KFilePlugin* plugin; 01699 KFileMimeTypeInfo* mimeTypeInfo; 01700 // The problem here is that plugin can be shared in multiple instances, 01701 // so the memory management isn't easy. KDE4 solution: use KSharedPtr? 01702 // For now we flag one copy of the KFilePlugin pointer as being "owned". 01703 bool ownsPlugin; 01704 }; 01705 01706 // The key is either a mimetype or a protocol. Those things don't look the same 01707 // so there's no need for two QDicts. 01708 TQDict<CachedPluginInfo> m_plugins; 01709 01710 // This data is aggregated during the creation of a plugin, 01711 // before being moved to the appropriate CachedPluginInfo(s) 01712 // At any other time than during the loading of a plugin, this dict is EMPTY. 01713 // Same key as in m_plugins: mimetype or protocol 01714 TQDict<KFileMimeTypeInfo> m_pendingMimetypeInfos; 01715 01716 private: 01717 static KFileMetaInfoProvider * s_self; 01718 01719 KFilePlugin* loadPlugin( const TQString& mimeType, const TQString& protocol ); 01720 KFilePlugin* loadAndRegisterPlugin( const TQString& mimeType, const TQString& protocol ); 01721 KFileMimeTypeInfo * addMimeTypeInfo( const TQString& mimeType ); 01722 01723 class KFileMetaInfoProviderPrivate; 01724 KFileMetaInfoProviderPrivate *d; 01725 01726 }; 01727 01728 KIO_EXPORT TQDataStream& operator <<(TQDataStream& s, const KFileMetaInfoItem& ); 01729 KIO_EXPORT TQDataStream& operator >>(TQDataStream& s, KFileMetaInfoItem& ); 01730 01731 KIO_EXPORT TQDataStream& operator <<(TQDataStream& s, const KFileMetaInfoGroup& ); 01732 KIO_EXPORT TQDataStream& operator >>(TQDataStream& s, KFileMetaInfoGroup& ); 01733 01734 KIO_EXPORT TQDataStream& operator <<(TQDataStream& s, const KFileMetaInfo& ); 01735 KIO_EXPORT TQDataStream& operator >>(TQDataStream& s, KFileMetaInfo& ); 01736 01737 01738 #endif // KILEMETAINFO_H