File indexing completed on 2023-05-30 11:30:46
0001 /** 0002 * Copyright (C) 2004 Scott Wheeler <wheeler@kde.org> 0003 * 0004 * This program is free software; you can redistribute it and/or modify it under 0005 * the terms of the GNU General Public License as published by the Free Software 0006 * Foundation; either version 2 of the License, or (at your option) any later 0007 * version. 0008 * 0009 * This program is distributed in the hope that it will be useful, but WITHOUT ANY 0010 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 0011 * PARTICULAR PURPOSE. See the GNU General Public License for more details. 0012 * 0013 * You should have received a copy of the GNU General Public License along with 0014 * this program. If not, see <http://www.gnu.org/licenses/>. 0015 */ 0016 0017 #ifndef FILEHANDLEPROPERTIES_H 0018 #define FILEHANDLEPROPERTIES_H 0019 0020 #include <QMap> 0021 #include <QStringList> 0022 0023 /* 0024 * These ugly macros make possible a property registration system that makes it 0025 * easy to add properties to the FileHandle that can be accessed via the DBus 0026 * interface. 0027 * 0028 * Properties should actually be added to the filehandle.cpp file. This file 0029 * is just here to separate out some of the macro-related ugliness. 0030 */ 0031 0032 #define AddProperty(name, method) \ 0033 namespace FileHandleProperties \ 0034 { \ 0035 struct name##Property : public Property \ 0036 { \ 0037 virtual QString value(const FileHandle &f) const override final \ 0038 { \ 0039 return f.method; \ 0040 } \ 0041 static const int dummy; \ 0042 }; \ 0043 static name##Property name##Instance; \ 0044 const int name##Property::dummy = addToPropertyMap(#name, &name##Instance); \ 0045 } 0046 0047 #define AddNumberProperty(name, method) \ 0048 namespace FileHandleProperties \ 0049 { \ 0050 struct name##Property : public Property \ 0051 { \ 0052 virtual QString value(const FileHandle &f) const override final \ 0053 { \ 0054 return QString::number(f.method); \ 0055 } \ 0056 static const int dummy; \ 0057 }; \ 0058 static name##Property name##Instance; \ 0059 const int name##Property::dummy = addToPropertyMap(#name, &name##Instance); \ 0060 } 0061 0062 namespace FileHandleProperties 0063 { 0064 struct Property 0065 { 0066 virtual ~Property() {} 0067 virtual QString value(const FileHandle &) const 0068 { 0069 return QString(); 0070 } 0071 }; 0072 0073 static QMap<QByteArray, const Property *> propertyMap; 0074 0075 static int addToPropertyMap(const QByteArray &name, Property *property) 0076 { 0077 propertyMap[name] = property; 0078 return 0; 0079 } 0080 0081 static QString property(const FileHandle &file, const QByteArray &key) 0082 { 0083 return propertyMap.contains(key) ? propertyMap[key]->value(file) : QString(); 0084 } 0085 0086 static QStringList properties() 0087 { 0088 static QStringList l; 0089 0090 if(l.isEmpty()) { 0091 QMap<QByteArray, const Property *>::ConstIterator it = propertyMap.constBegin(); 0092 for(; it != propertyMap.constEnd(); ++it) 0093 l.append(QString(it.key())); 0094 } 0095 return l; 0096 } 0097 } 0098 0099 #endif 0100 0101 // vim: set et sw=4 tw=0 sta: