00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <iostream>
00020
00021 #include <qptrlist.h>
00022
00023 #include <kaboutdata.h>
00024 #include <kapplication.h>
00025 #include <kcmdlineargs.h>
00026 #include <kfilemetainfo.h>
00027 #include <klocale.h>
00028 #include <kpropertiesdialog.h>
00029
00030 #include "fileprops.h"
00031
00032 #define KFILEVERSION "0.1"
00033 #define INDENT "\t"
00034
00035 using namespace std;
00036
00037 FileProps::FileProps( const char *argument, const QString& path )
00038 : m_argument( argument ),
00039 m_dirty( false )
00040 {
00041 m_info = new KFileMetaInfo( path );
00042 }
00043
00044 FileProps::~FileProps()
00045 {
00046 sync();
00047 delete m_info;
00048 }
00049
00050 bool FileProps::sync()
00051 {
00052 if ( !m_dirty )
00053 return true;
00054
00055 return m_info->applyChanges();
00056 }
00057
00058 bool FileProps::isValid() const
00059 {
00060 return m_info->isValid();
00061 }
00062
00063 QStringList FileProps::supportedGroups() const
00064 {
00065 return m_info->supportedGroups();
00066 }
00067
00068 QStringList FileProps::availableGroups() const
00069 {
00070 return m_info->groups();
00071 }
00072
00073 QStringList FileProps::supportedKeys( const QString& group ) const
00074 {
00075 KFileMetaInfoGroup g = m_info->group( group );
00076 return g.supportedKeys();
00077 }
00078
00079 QStringList FileProps::availableKeys( const QString& group ) const
00080 {
00081 KFileMetaInfoGroup g = m_info->group( group );
00082 return g.keys();
00083 }
00084
00085 QStringList FileProps::preferredKeys( const QString& group ) const
00086 {
00087 KFileMetaInfoGroup g = m_info->group( group );
00088 return g.preferredKeys();
00089 }
00090
00091 QString FileProps::getValue( const QString& group,
00092 const QString& key ) const
00093 {
00094 KFileMetaInfoGroup g = m_info->group( group );
00095 return g[key].string();
00096 }
00097
00098 bool FileProps::setValue( const QString& group,
00099 const QString& key, const QString &value )
00100 {
00101 KFileMetaInfoGroup g = m_info->group( group );
00102 bool ok = g[key].setValue( value );
00103 m_dirty |= ok;
00104 return ok;
00105 }
00106
00107 QStringList FileProps::allValues( const QString& group ) const
00108 {
00109 KFileMetaInfoGroup g = m_info->group( group );
00110 return createKeyValueList( g, g.keys() );
00111 }
00112
00113 QStringList FileProps::preferredValues( const QString& group ) const
00114 {
00115 KFileMetaInfoGroup g = m_info->group( group );
00116 return createKeyValueList( g, g.preferredKeys() );
00117 }
00118
00119
00120
00121 QStringList FileProps::createKeyValueList( const KFileMetaInfoGroup& g,
00122 const QStringList& keys )
00123 {
00124 QStringList result;
00125 QStringList::ConstIterator it = keys.begin();
00126
00127 for ( ; it != keys.end(); ++it ) {
00128 KFileMetaInfoItem item = g.item( *it );
00129 QString tmp = item.translatedKey() + ":\t\t" + item.string();
00130 result.append( tmp );
00131 }
00132
00133 return result;
00134 }
00135
00138
00139
00140
00141
00142
00143 static KCmdLineOptions options[] =
00144 {
00145 { "m", 0, 0 },
00146 { "nomimetype", I18N_NOOP("Print the mimetype of the given file(s)"), 0 },
00147
00148 { "ls", 0, 0 },
00149 { "listsupported <mimetype>",
00150 I18N_NOOP("List all supported metadata keys of the given file(s). "
00151 "If mimetype is not specified, the mimetype of the given "
00152 "files is used." ), "file" },
00153
00154 { "lp", 0, 0 },
00155 { "listpreferred <mimetype>",
00156 I18N_NOOP("List all preferred metadata keys of the given file(s). "
00157 "If mimetype is not specified, the mimetype of the given "
00158 "files is used." ), "file" },
00159
00160 { "la", 0, 0 },
00161 { "listavailable",
00162 I18N_NOOP("List all metadata keys which have a value in the given "
00163 "file(s)."), 0 },
00164
00165 { "sm", 0, 0 },
00166 { "supportedMimetypes",
00167 I18N_NOOP("Prints all mimetypes for which metadata support is "
00168 "available."), 0 },
00169
00170 { "q", 0, 0 },
00171 { "quiet",
00172 I18N_NOOP("Don't print a warning when more than one file was given "
00173 "and they don't have all the same mimetype."), 0 },
00174
00175 { "av", 0, 0 },
00176 { "allValues",
00177 I18N_NOOP("Prints all metadata values, available in the given "
00178 "file(s)."), 0 },
00179
00180 { "pv", 0, 0 },
00181 { "preferredValues",
00182 I18N_NOOP("Prints the preferred metadata values, available in the "
00183 "given file(s)."), 0 },
00184
00185 { "dialog",
00186 I18N_NOOP("Opens a KDE properties dialog to allow viewing and "
00187 "modifying of metadata of the given file(s)"), 0 },
00188
00189 { "getValue <key>",
00190 I18N_NOOP("Prints the value for 'key' of the given file(s). 'key' "
00191 "may also be a comma-separated list of keys"), 0 },
00192
00193 { "setValue <key=value>",
00194 I18N_NOOP("Attempts to set the value 'value' for the metadata key "
00195 "'key' for the given file(s)"), 0 },
00196
00197 { "+[files]",
00198 I18N_NOOP("The file (or a number of files) to operate on."), 0 },
00199 { 0, 0, 0 }
00200 };
00201
00202
00203
00204
00205
00206
00207 static void printSupportedMimeTypes()
00208 {
00209 QStringList allMimeTypes = KFileMetaInfoProvider::self()->supportedMimeTypes();
00210 if ( allMimeTypes.isEmpty() )
00211 {
00212 cout <<
00213 i18n("No support for metadata extraction found.").local8Bit()
00214 << endl;
00215 return;
00216 }
00217
00218 cout << i18n("Supported MimeTypes:").local8Bit() << endl;
00219
00220 QStringList::ConstIterator it = allMimeTypes.begin();
00221 for ( ; it != allMimeTypes.end(); it++ )
00222 cout << (*it).local8Bit() << endl;
00223 }
00224
00225
00226 static KFileItemList * fileItemList( const KCmdLineArgs *args )
00227 {
00228 KFileItemList * items = new KFileItemList();
00229 items->setAutoDelete( true );
00230 for ( int i = 0; i < args->count(); i++ )
00231 items->append( new KFileItem( KFileItem::Unknown,
00232 KFileItem::Unknown,
00233 args->url( i ) ));
00234 return items;
00235 }
00236
00237 static void showPropertiesDialog( const KCmdLineArgs *args )
00238 {
00239 KFileItemList *items = fileItemList( args );
00240 new KPropertiesDialog( *items, 0L, "props dialog", true );
00241 delete items;
00242 }
00243
00244 static void printMimeTypes( const KCmdLineArgs *args )
00245 {
00246 for ( int i = 0; i < args->count(); i++ )
00247 {
00248 KURL url = args->url( i );
00249 KMimeType::Ptr mt = KMimeType::findByURL( url );
00250 cout << args->arg(i) << ": " << mt->comment().local8Bit() << " ("
00251 << mt->name().local8Bit() << ")" << endl;
00252 }
00253 }
00254
00255 static void printList( const QStringList& list )
00256 {
00257 QStringList::ConstIterator it = list.begin();
00258 for ( ; it != list.end(); ++it )
00259 cout << INDENT << (*it).local8Bit() << endl;
00260 cout << endl;
00261 }
00262
00263 static void processMetaDataOptions( const QPtrList<FileProps> propList,
00264 KCmdLineArgs *args )
00265 {
00266
00267
00268
00269 FileProps *props;
00270 QPtrListIterator<FileProps> it( propList );
00271 for ( ; (props = it.current()); ++it )
00272 {
00273 cout << props->argument() << ":" << endl;
00274
00275 if ( args->isSet( "listsupported" ) )
00276 {
00277 cout << i18n("Supported Keys").local8Bit() << endl;
00278 printList( props->supportedKeys() );
00279 }
00280 if ( args->isSet( "listpreferred" ) )
00281 {
00282 cout << i18n("Preferred Keys").local8Bit() << endl;
00283 printList( props->preferredKeys() );
00284 }
00285 if ( args->isSet( "listavailable" ) )
00286 {
00287 cout << "TODO :)" << endl;
00288 }
00289
00290
00291
00292
00293 if ( args->isSet( "getValue" ) )
00294 {
00295
00296 }
00297 if ( args->isSet( "allValues" ) )
00298 {
00299 QStringList groups = props->availableGroups();
00300 QStringList::ConstIterator group = groups.begin();
00301 for ( ; group != groups.end(); ++group )
00302 {
00303 cout << (*group).local8Bit() << endl;
00304 printList( props->allValues( *group ) );
00305 }
00306 }
00307 if ( args->isSet( "preferredValues" ) && !args->isSet("allValues") )
00308 {
00309 QStringList groups = props->availableGroups();
00310 QStringList::ConstIterator group = groups.begin();
00311 for ( ; group != groups.end(); ++group )
00312 {
00313 cout << (*group).local8Bit() << endl;
00314 printList( props->preferredValues( *group ) );
00315 }
00316
00317 }
00318 }
00319
00320
00321 }
00322
00323 int main( int argc, char **argv )
00324 {
00325 KAboutData about(
00326 "kfile", I18N_NOOP( "kfile" ), KFILEVERSION,
00327 I18N_NOOP("A commandline tool to read and modify metadata of files." ),
00328 KAboutData::License_LGPL, "(c) 2002, Carsten Pfeiffer",
00329 0 , "http://devel-home.kde.org/~pfeiffer/",
00330 "pfeiffer@kde.org" );
00331
00332 about.addAuthor( "Carsten Pfeiffer", 0, "pfeiffer@kde.org",
00333 "http://devel-home.kde.org/~pfeiffer/" );
00334
00335 KGlobal::locale()->setMainCatalogue("kdelibs");
00336
00337 KCmdLineArgs::init( argc, argv, &about );
00338
00339 KCmdLineArgs::addCmdLineOptions( options );
00340
00341 KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
00342 bool useGUI = args->isSet( "dialog" );
00343
00344 KApplication app( useGUI, useGUI );
00345
00346 QPtrList<FileProps> m_props;
00347 m_props.setAutoDelete( true );
00348
00349 bool quiet = args->isSet( "quiet" );
00350
00351 if ( args->isSet( "supportedMimetypes" ) )
00352 printSupportedMimeTypes();
00353
00354 int files = args->count();
00355 if ( files == 0 )
00356 KCmdLineArgs::usage( i18n("No files specified") );
00357
00358 QString mimeType;
00359
00360 for ( int i = 0; i < files; i++ )
00361 {
00362 if ( args->isSet( "dialog" ) )
00363 {
00364 showPropertiesDialog( args );
00365 return true;
00366 }
00367
00368 if ( args->isSet( "mimetype" ) )
00369 printMimeTypes( args );
00370
00371 FileProps *props = new FileProps( args->arg(i),
00372 args->url(i).path() );
00373 if ( props->isValid() )
00374 m_props.append( props );
00375 else if ( !quiet )
00376 cerr << args->arg(i) << ": " <<
00377 i18n("Cannot determine metadata").local8Bit() << endl;
00378 }
00379
00380
00381 processMetaDataOptions( m_props, args );
00382
00383 return 0;
00384 }